Skip to content

Commit 22d9e89

Browse files
dmytrorykunfacebook-github-bot
authored andcommitted
Introduce ImageRequestParams
Summary: 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 fb9b71b commit 22d9e89

File tree

11 files changed

+151
-22
lines changed

11 files changed

+151
-22
lines changed

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

+19-9
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,29 @@ 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+
#endif
49+
),
50+
newImageRequestParams};
4151
setStateData(std::move(state));
4252
}
4353

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

+1-1
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

+2-3
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

+9-6
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_(std::move(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

+6
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,11 @@ 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) const;
41+
3642
private:
3743
void* self_{};
3844
};

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,14 @@ ImageManager::~ImageManager() {
2222

2323
ImageRequest ImageManager::requestImage(
2424
const ImageSource& imageSource,
25-
SurfaceId /*surfaceId*/) const {
26-
// Not implemented.
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*/) const {
2733
return {imageSource, nullptr, {}};
2834
}
2935

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@ 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*/) const {
2633
// Not implemented.
2734
return {imageSource, nullptr, {}};
2835
}
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() {}
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/ios/react/renderer/imagemanager/ImageManager.mm

+8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@
3535
}
3636

3737
ImageRequest ImageManager::requestImage(const ImageSource &imageSource, SurfaceId surfaceId) const
38+
{
39+
return requestImage(imageSource, surfaceId, ImageRequestParams{});
40+
}
41+
42+
ImageRequest ImageRequest ImageManager::requestImage(
43+
const ImageSource &imageSource,
44+
SurfaceId surfaceId,
45+
ImageRequestParams & /*imageRequestParams*/) const
3846
{
3947
RCTImageManager *imageManager = (__bridge RCTImageManager *)self_;
4048
return [imageManager requestImage:imageSource surfaceId:surfaceId];
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() {}
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

0 commit comments

Comments
 (0)