Skip to content

Commit

Permalink
Add Android-specific ImageRequestParams (facebook#47930)
Browse files Browse the repository at this point in the history
Summary:

This diff adds a list of props that will be used by the Android `ImagePrefetcher` to create an `ImageRequest`. This list is derived from all the props that `ReactImageView` uses to create its `ImageOptions` and `ImageRequest` objects.
Changelog: [Internal]

Differential Revision: D66453306
  • Loading branch information
dmytrorykun authored and facebook-github-bot committed Nov 26, 2024
1 parent 21b131a commit 669bcd5
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,24 @@ void ImageShadowNode::updateStateIfNeeded() {
auto newImageSource = getImageSource();
const auto& oldImageRequestParams = savedState.getImageRequestParams();
const auto& imageProps = getConcreteProps();
const auto& newImageRequestParams = ImageRequestParams(imageProps.blurRadius);
const auto& newImageRequestParams = ImageRequestParams(
imageProps.blurRadius
#ifdef ANDROID
,
imageProps.defaultSource,
imageProps.resizeMode,
imageProps.resizeMethod,
// TODO: should we resizeMultiplier * imageSource.scale ?
imageProps.resizeMultiplier,
imageProps.shouldNotify,
imageProps.overlayColor,
imageProps.tintColor,
imageProps.fadeDuration,
imageProps.progressiveRenderingEnabled,
imageProps.loadingIndicatorSource,
imageProps.internal_analyticTag
#endif
);

if (oldImageSource == newImageSource &&
oldImageRequestParams == newImageRequestParams) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,83 @@

#pragma once

#include <utility>

#include <react/renderer/graphics/Color.h>
#include <react/renderer/graphics/Float.h>
#include <react/renderer/imagemanager/primitives.h>

namespace facebook::react {

class ImageRequestParams {
public:
ImageRequestParams() = default;
ImageRequestParams(Float blurRadius) : blurRadius(blurRadius) {}
ImageRequestParams(
Float blurRadius,
ImageSource defaultSource,
ImageResizeMode resizeMode,
std::string resizeMethod,
Float resizeMultiplier,
bool shouldNotify,
SharedColor overlayColor,
SharedColor tintColor,
Float fadeDuration,
bool progressiveRenderingEnabled,
ImageSource loadingIndicatorSource,
std::string analyticTag)
: blurRadius(blurRadius),
defaultSource(std::move(defaultSource)),
resizeMode(resizeMode),
resizeMethod(std::move(resizeMethod)),
resizeMultiplier(resizeMultiplier),
shouldNotify(shouldNotify),
overlayColor(overlayColor),
tintColor(tintColor),
fadeDuration(fadeDuration),
progressiveRenderingEnabled(progressiveRenderingEnabled),
loadingIndicatorSource(std::move(loadingIndicatorSource)),
analyticTag(std::move(analyticTag)) {}

Float blurRadius{};
ImageSource defaultSource{};
ImageResizeMode resizeMode{ImageResizeMode::Stretch};
std::string resizeMethod{};
Float resizeMultiplier{};
bool shouldNotify{};
SharedColor overlayColor{};
SharedColor tintColor{};
Float fadeDuration{};
bool progressiveRenderingEnabled{};
ImageSource loadingIndicatorSource{};
std::string analyticTag{};

bool operator==(const ImageRequestParams& rhs) const {
return this->blurRadius == rhs.blurRadius;
return std::tie(
this->blurRadius,
this->defaultSource,
this->resizeMode,
this->resizeMethod,
this->resizeMultiplier,
this->shouldNotify,
this->overlayColor,
this->tintColor,
this->fadeDuration,
this->progressiveRenderingEnabled,
this->loadingIndicatorSource,
this->analyticTag) ==
std::tie(
rhs.blurRadius,
rhs.defaultSource,
rhs.resizeMode,
rhs.resizeMethod,
rhs.resizeMultiplier,
rhs.shouldNotify,
rhs.overlayColor,
rhs.tintColor,
rhs.fadeDuration,
rhs.progressiveRenderingEnabled,
rhs.loadingIndicatorSource,
rhs.analyticTag);
}

bool operator!=(const ImageRequestParams& rhs) const {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include <string>

#include <react/renderer/core/graphicsConversions.h>
#include <react/renderer/imagemanager/ImageRequestParams.h>
#include <react/renderer/imagemanager/primitives.h>
#include <react/renderer/mapbuffer/MapBuffer.h>
#include <react/renderer/mapbuffer/MapBufferBuilder.h>

namespace facebook::react {

inline std::string toString(const ImageResizeMode& value) {
switch (value) {
case ImageResizeMode::Cover:
return "cover";
case ImageResizeMode::Contain:
return "contain";
case ImageResizeMode::Stretch:
return "stretch";
case ImageResizeMode::Center:
return "center";
case ImageResizeMode::Repeat:
return "repeat";
case ImageResizeMode::None:
return "none";
}
}

constexpr static MapBuffer::Key IS_KEY_URI = 0;
constexpr static MapBuffer::Key IS_KEY_DEFAULT_SRC = 1;
constexpr static MapBuffer::Key IS_KEY_RESIZE_MODE = 2;
constexpr static MapBuffer::Key IS_KEY_RESIZE_METHOD = 3;
constexpr static MapBuffer::Key IS_KEY_BLUR_RADIUS = 4;
constexpr static MapBuffer::Key IS_KEY_VIEW_WIDTH = 5;
constexpr static MapBuffer::Key IS_KEY_VIEW_HEIGHT = 6;
constexpr static MapBuffer::Key IS_KEY_RESIZE_MULTIPLIER = 7;
constexpr static MapBuffer::Key IS_KEY_SHOULD_NOTIFY_LOAD_EVENTS = 8;
constexpr static MapBuffer::Key IS_KEY_OVERLAY_COLOR = 9;
constexpr static MapBuffer::Key IS_KEY_TINT_COLOR = 10;
constexpr static MapBuffer::Key IS_KEY_FADE_DURATION = 11;
constexpr static MapBuffer::Key IS_KEY_PROGRESSIVE_RENDERING_ENABLED = 12;
constexpr static MapBuffer::Key IS_KEY_LOADING_INDICATOR_SRC = 13;
constexpr static MapBuffer::Key IS_KEY_ANALYTIC_TAG = 14;

inline void serializeImageSource(
MapBufferBuilder& builder,
const ImageSource& imageSource) {
builder.putString(IS_KEY_URI, imageSource.uri);
builder.putDouble(IS_KEY_VIEW_WIDTH, imageSource.size.width);
builder.putDouble(IS_KEY_VIEW_HEIGHT, imageSource.size.height);
}

inline void serializeImageRequestParams(
MapBufferBuilder& builder,
const ImageRequestParams& imageRequestParams) {
builder.putString(IS_KEY_DEFAULT_SRC, imageRequestParams.defaultSource.uri);
builder.putString(
IS_KEY_RESIZE_MODE, toString(imageRequestParams.resizeMode));
builder.putString(IS_KEY_RESIZE_METHOD, imageRequestParams.resizeMethod);
builder.putDouble(IS_KEY_BLUR_RADIUS, imageRequestParams.blurRadius);
builder.putDouble(
IS_KEY_RESIZE_MULTIPLIER, imageRequestParams.resizeMultiplier);
builder.putBool(
IS_KEY_SHOULD_NOTIFY_LOAD_EVENTS, imageRequestParams.shouldNotify);
if (isColorMeaningful(imageRequestParams.overlayColor)) {
builder.putInt(
IS_KEY_OVERLAY_COLOR, toAndroidRepr(imageRequestParams.overlayColor));
}
if (isColorMeaningful(imageRequestParams.tintColor)) {
builder.putInt(
IS_KEY_TINT_COLOR, toAndroidRepr(imageRequestParams.tintColor));
}
builder.putDouble(IS_KEY_FADE_DURATION, imageRequestParams.fadeDuration);
builder.putBool(
IS_KEY_PROGRESSIVE_RENDERING_ENABLED,
imageRequestParams.progressiveRenderingEnabled);
builder.putString(
IS_KEY_LOADING_INDICATOR_SRC,
imageRequestParams.loadingIndicatorSource.uri);
builder.putString(IS_KEY_ANALYTIC_TAG, imageRequestParams.analyticTag);
}

inline MapBuffer serializeImageRequest(
const ImageSource& imageSource,
const ImageRequestParams& imageRequestParams) {
auto builder = MapBufferBuilder();
serializeImageSource(builder, imageSource);
serializeImageRequestParams(builder, imageRequestParams);
return builder.build();
}

} // namespace facebook::react

0 comments on commit 669bcd5

Please sign in to comment.