|
| 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 <string> |
| 11 | + |
| 12 | +#include <react/renderer/core/graphicsConversions.h> |
| 13 | +#include <react/renderer/imagemanager/ImageRequestParams.h> |
| 14 | +#include <react/renderer/imagemanager/primitives.h> |
| 15 | +#include <react/renderer/mapbuffer/MapBuffer.h> |
| 16 | +#include <react/renderer/mapbuffer/MapBufferBuilder.h> |
| 17 | + |
| 18 | +namespace facebook::react { |
| 19 | + |
| 20 | +inline std::string toString(const ImageResizeMode& value) { |
| 21 | + switch (value) { |
| 22 | + case ImageResizeMode::Cover: |
| 23 | + return "cover"; |
| 24 | + case ImageResizeMode::Contain: |
| 25 | + return "contain"; |
| 26 | + case ImageResizeMode::Stretch: |
| 27 | + return "stretch"; |
| 28 | + case ImageResizeMode::Center: |
| 29 | + return "center"; |
| 30 | + case ImageResizeMode::Repeat: |
| 31 | + return "repeat"; |
| 32 | + case ImageResizeMode::None: |
| 33 | + return "none"; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +constexpr static MapBuffer::Key IS_KEY_URI = 0; |
| 38 | +constexpr static MapBuffer::Key IS_KEY_DEFAULT_SRC = 1; |
| 39 | +constexpr static MapBuffer::Key IS_KEY_RESIZE_MODE = 2; |
| 40 | +constexpr static MapBuffer::Key IS_KEY_RESIZE_METHOD = 3; |
| 41 | +constexpr static MapBuffer::Key IS_KEY_BLUR_RADIUS = 4; |
| 42 | +constexpr static MapBuffer::Key IS_KEY_VIEW_WIDTH = 5; |
| 43 | +constexpr static MapBuffer::Key IS_KEY_VIEW_HEIGHT = 6; |
| 44 | +constexpr static MapBuffer::Key IS_KEY_RESIZE_MULTIPLIER = 7; |
| 45 | +constexpr static MapBuffer::Key IS_KEY_SHOULD_NOTIFY_LOAD_EVENTS = 8; |
| 46 | +constexpr static MapBuffer::Key IS_KEY_OVERLAY_COLOR = 9; |
| 47 | +constexpr static MapBuffer::Key IS_KEY_TINT_COLOR = 10; |
| 48 | +constexpr static MapBuffer::Key IS_KEY_FADE_DURATION = 11; |
| 49 | +constexpr static MapBuffer::Key IS_KEY_PROGRESSIVE_RENDERING_ENABLED = 12; |
| 50 | +constexpr static MapBuffer::Key IS_KEY_LOADING_INDICATOR_SRC = 13; |
| 51 | +constexpr static MapBuffer::Key IS_KEY_ANALYTIC_TAG = 14; |
| 52 | + |
| 53 | +inline void serializeImageSource( |
| 54 | + MapBufferBuilder& builder, |
| 55 | + const ImageSource& imageSource) { |
| 56 | + builder.putString(IS_KEY_URI, imageSource.uri); |
| 57 | + builder.putDouble(IS_KEY_VIEW_WIDTH, imageSource.size.width); |
| 58 | + builder.putDouble(IS_KEY_VIEW_HEIGHT, imageSource.size.height); |
| 59 | +} |
| 60 | + |
| 61 | +inline void serializeImageRequestParams( |
| 62 | + MapBufferBuilder& builder, |
| 63 | + const ImageRequestParams& imageRequestParams) { |
| 64 | + builder.putString(IS_KEY_DEFAULT_SRC, imageRequestParams.defaultSource.uri); |
| 65 | + builder.putString( |
| 66 | + IS_KEY_RESIZE_MODE, toString(imageRequestParams.resizeMode)); |
| 67 | + builder.putString(IS_KEY_RESIZE_METHOD, imageRequestParams.resizeMethod); |
| 68 | + builder.putDouble(IS_KEY_BLUR_RADIUS, imageRequestParams.blurRadius); |
| 69 | + builder.putDouble( |
| 70 | + IS_KEY_RESIZE_MULTIPLIER, imageRequestParams.resizeMultiplier); |
| 71 | + builder.putBool( |
| 72 | + IS_KEY_SHOULD_NOTIFY_LOAD_EVENTS, imageRequestParams.shouldNotify); |
| 73 | + if (isColorMeaningful(imageRequestParams.overlayColor)) { |
| 74 | + builder.putInt( |
| 75 | + IS_KEY_OVERLAY_COLOR, toAndroidRepr(imageRequestParams.overlayColor)); |
| 76 | + } |
| 77 | + if (isColorMeaningful(imageRequestParams.tintColor)) { |
| 78 | + builder.putInt( |
| 79 | + IS_KEY_TINT_COLOR, toAndroidRepr(imageRequestParams.tintColor)); |
| 80 | + } |
| 81 | + builder.putDouble(IS_KEY_FADE_DURATION, imageRequestParams.fadeDuration); |
| 82 | + builder.putBool( |
| 83 | + IS_KEY_PROGRESSIVE_RENDERING_ENABLED, |
| 84 | + imageRequestParams.progressiveRenderingEnabled); |
| 85 | + builder.putString( |
| 86 | + IS_KEY_LOADING_INDICATOR_SRC, |
| 87 | + imageRequestParams.loadingIndicatorSource.uri); |
| 88 | + builder.putString(IS_KEY_ANALYTIC_TAG, imageRequestParams.analyticTag); |
| 89 | +} |
| 90 | + |
| 91 | +inline MapBuffer serializeImageRequest( |
| 92 | + const ImageSource& imageSource, |
| 93 | + const ImageRequestParams& imageRequestParams) { |
| 94 | + auto builder = MapBufferBuilder(); |
| 95 | + serializeImageSource(builder, imageSource); |
| 96 | + serializeImageRequestParams(builder, imageRequestParams); |
| 97 | + return builder.build(); |
| 98 | +} |
| 99 | + |
| 100 | +} // namespace facebook::react |
0 commit comments