Skip to content

Commit 6c04e44

Browse files
dmytrorykunfacebook-github-bot
authored andcommitted
Introduce ImageRequestParams (facebook#47723)
Summary: Pull Request resolved: facebook#47723 Identity of `ImageRequest` is based on `ImageSource` and a subset of `ImageProps`. If any of those change, the request must be recreated and resubmitted. Currently, the relevant subset of ImageProps is represented by a single `blurRadius` prop. This list will grow in the future. In order to simplify adding new props to the image request, we introduce the `ImageRequestParams` type that will wrap all the relevant props. The alternative approach to pass `ImageProps` directly to `ImageManager` is worse because it would introduce dependency cycle between `ImageManager` and the `Image` component, and also it would require to store the props in State, which is bad. Changelog: [Internal] Differential Revision: D66172570
1 parent 4031692 commit 6c04e44

File tree

12 files changed

+160
-23
lines changed

12 files changed

+160
-23
lines changed

packages/react-native/ReactCommon/React-FabricImage.podspec

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,13 @@ Pod::Spec.new do |s|
7979
s.dependency "fast_float", "6.1.4"
8080
s.dependency "fmt", "11.0.2"
8181
s.dependency "React-featureflags"
82-
s.dependency "React-ImageManager"
8382
s.dependency "React-utils"
8483
s.dependency "Yoga"
8584

85+
add_dependency(s, "React-ImageManager", :additional_framework_paths => [
86+
"react/renderer/components/view/platform/cxx",
87+
"react/renderer/imagemanager/platform/ios",
88+
])
8689
add_dependency(s, "ReactCommon", :subspec => "turbomodule/core")
8790
add_dependency(s, "React-graphics", :additional_framework_paths => ["react/renderer/graphics/platform/ios"])
8891
add_dependency(s, "React-Fabric", :additional_framework_paths => [

packages/react-native/ReactCommon/react/renderer/components/image/ImageShadowNode.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <react/renderer/components/image/ImageShadowNode.h>
1212
#include <react/renderer/core/LayoutContext.h>
13+
#include <react/renderer/imagemanager/ImageRequestParams.h>
1314
#include "ImageState.h"
1415

1516
namespace facebook::react {
@@ -24,20 +25,30 @@ void ImageShadowNode::setImageManager(const SharedImageManager& imageManager) {
2425
void ImageShadowNode::updateStateIfNeeded() {
2526
ensureUnsealed();
2627

27-
auto imageSource = getImageSource();
28-
const auto& currentState = getStateData();
29-
bool hasSameRadius =
30-
getConcreteProps().blurRadius == currentState.getBlurRadius();
31-
bool hasSameImageSource = currentState.getImageSource() == imageSource;
28+
const auto& savedState = getStateData();
29+
const auto& oldImageSource = savedState.getImageSource();
30+
auto newImageSource = getImageSource();
31+
const auto& oldImageRequestParams = savedState.getImageRequestParams();
32+
const auto& imageProps = getConcreteProps();
33+
const auto& newImageRequestParams = ImageRequestParams(imageProps.blurRadius);
3234

33-
if (hasSameImageSource && hasSameRadius) {
35+
if (oldImageSource == newImageSource &&
36+
oldImageRequestParams == newImageRequestParams) {
3437
return;
3538
}
3639

3740
auto state = ImageState{
38-
imageSource,
39-
imageManager_->requestImage(imageSource, getSurfaceId()),
40-
getConcreteProps().blurRadius};
41+
newImageSource,
42+
imageManager_->requestImage(
43+
newImageSource,
44+
getSurfaceId()
45+
#ifdef ANDROID
46+
,
47+
newImageRequestParams,
48+
getTag()
49+
#endif
50+
),
51+
newImageRequestParams};
4152
setStateData(std::move(state));
4253
}
4354

packages/react-native/ReactCommon/react/renderer/components/image/ImageShadowNode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class ImageShadowNode final : public ConcreteViewShadowNode<
4646
const ShadowNodeFamily::Shared& /*family*/,
4747
const ComponentDescriptor& componentDescriptor) {
4848
auto imageSource = ImageSource{ImageSource::Type::Invalid};
49-
return {imageSource, {imageSource, nullptr}, 0};
49+
return {imageSource, {imageSource, nullptr}, {}};
5050
}
5151

5252
#pragma mark - LayoutableShadowNode

packages/react-native/ReactCommon/react/renderer/components/image/ImageState.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ ImageSource ImageState::getImageSource() const {
1616
const ImageRequest& ImageState::getImageRequest() const {
1717
return *imageRequest_;
1818
}
19-
20-
Float ImageState::getBlurRadius() const {
21-
return blurRadius_;
19+
const ImageRequestParams& ImageState::getImageRequestParams() const {
20+
return imageRequestParams_;
2221
}
2322

2423
} // namespace facebook::react

packages/react-native/ReactCommon/react/renderer/components/image/ImageState.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#pragma once
99

1010
#include <react/renderer/imagemanager/ImageRequest.h>
11+
#include <react/renderer/imagemanager/ImageRequestParams.h>
1112
#include <react/renderer/imagemanager/primitives.h>
1213

1314
#ifdef ANDROID
@@ -26,10 +27,10 @@ class ImageState final {
2627
ImageState(
2728
const ImageSource& imageSource,
2829
ImageRequest imageRequest,
29-
const Float blurRadius)
30+
const ImageRequestParams& imageRequestParams)
3031
: imageSource_(imageSource),
3132
imageRequest_(std::make_shared<ImageRequest>(std::move(imageRequest))),
32-
blurRadius_(blurRadius){};
33+
imageRequestParams_(imageRequestParams) {}
3334

3435
/*
3536
* Returns stored ImageSource object.
@@ -42,11 +43,13 @@ class ImageState final {
4243
*/
4344
const ImageRequest& getImageRequest() const;
4445

45-
Float getBlurRadius() const;
46-
46+
/*
47+
* Returns stored ImageRequestParams object.
48+
*/
49+
const ImageRequestParams& getImageRequestParams() const;
4750
#ifdef ANDROID
4851
ImageState(const ImageState& previousState, folly::dynamic data)
49-
: blurRadius_{0} {};
52+
: imageRequestParams_{} {};
5053

5154
/*
5255
* Empty implementation for Android because it doesn't use this class.
@@ -59,7 +62,7 @@ class ImageState final {
5962
private:
6063
ImageSource imageSource_;
6164
std::shared_ptr<ImageRequest> imageRequest_;
62-
const Float blurRadius_;
65+
ImageRequestParams imageRequestParams_;
6366
};
6467

6568
} // namespace facebook::react

packages/react-native/ReactCommon/react/renderer/imagemanager/ImageManager.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <react/renderer/core/ReactPrimitives.h>
1313
#include <react/renderer/imagemanager/ImageRequest.h>
14+
#include <react/renderer/imagemanager/ImageRequestParams.h>
1415
#include <react/renderer/imagemanager/primitives.h>
1516
#include <react/utils/ContextContainer.h>
1617

@@ -33,6 +34,12 @@ class ImageManager {
3334
const ImageSource& imageSource,
3435
SurfaceId surfaceId) const;
3536

37+
virtual ImageRequest requestImage(
38+
const ImageSource& imageSource,
39+
SurfaceId surfaceId,
40+
const ImageRequestParams& imageRequestParams,
41+
Tag tag) const;
42+
3643
private:
3744
void* self_{};
3845
};

packages/react-native/ReactCommon/react/renderer/imagemanager/platform/android/ImageManager.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,15 @@ ImageManager::~ImageManager() = default;
2020

2121
ImageRequest ImageManager::requestImage(
2222
const ImageSource& imageSource,
23-
SurfaceId /*surfaceId*/) const {
24-
// Not implemented.
23+
SurfaceId surfaceId) const {
24+
return requestImage(imageSource, surfaceId, ImageRequestParams{}, {});
25+
}
26+
27+
ImageRequest ImageManager::requestImage(
28+
const ImageSource& imageSource,
29+
SurfaceId /*surfaceId*/,
30+
const ImageRequestParams& /*imageRequestParams*/,
31+
Tag /* tag */) const {
2532
return {imageSource, nullptr, {}};
2633
}
2734

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <react/renderer/graphics/Float.h>
11+
12+
namespace facebook::react {
13+
14+
class ImageRequestParams {
15+
public:
16+
ImageRequestParams() = default;
17+
ImageRequestParams(Float blurRadius) : blurRadius(blurRadius) {}
18+
19+
Float blurRadius{};
20+
21+
bool operator==(const ImageRequestParams& rhs) const {
22+
return this->blurRadius == rhs.blurRadius;
23+
}
24+
25+
bool operator!=(const ImageRequestParams& rhs) const {
26+
return !(*this == rhs);
27+
}
28+
};
29+
30+
} // namespace facebook::react

packages/react-native/ReactCommon/react/renderer/imagemanager/platform/cxx/react/renderer/imagemanager/ImageManager.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ ImageManager::~ImageManager() {
2222

2323
ImageRequest ImageManager::requestImage(
2424
const ImageSource& imageSource,
25-
SurfaceId /*surfaceId*/) const {
25+
SurfaceId surfaceId) const {
26+
return requestImage(imageSource, surfaceId, ImageRequestParams{}, {});
27+
}
28+
29+
ImageRequest ImageManager::requestImage(
30+
const ImageSource& imageSource,
31+
SurfaceId /*surfaceId*/,
32+
const ImageRequestParams& /*imageRequestParams*/,
33+
Tag /*tag*/) const {
2634
// Not implemented.
2735
return {imageSource, nullptr, {}};
2836
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <react/renderer/graphics/Float.h>
11+
12+
namespace facebook::react {
13+
14+
class ImageRequestParams {
15+
public:
16+
ImageRequestParams() = default;
17+
explicit ImageRequestParams(Float blurRadius) : blurRadius(blurRadius) {}
18+
19+
Float blurRadius{};
20+
21+
bool operator==(const ImageRequestParams& rhs) const {
22+
return this->blurRadius == rhs.blurRadius;
23+
}
24+
25+
bool operator!=(const ImageRequestParams& rhs) const {
26+
return !(*this == rhs);
27+
}
28+
};
29+
30+
} // namespace facebook::react

0 commit comments

Comments
 (0)