Skip to content

Commit 3e44d20

Browse files
javachefacebook-github-bot
authored andcommitted
Shared Fabric color implementation across platforms
Summary: Changelog: [Internal] Align C++ implementations of SharedColor Reviewed By: mdvacca Differential Revision: D40632527 fbshipit-source-id: 8ebca5157e5898de4311015c92b5a72dca7197d3
1 parent 3823703 commit 3e44d20

File tree

12 files changed

+49
-186
lines changed

12 files changed

+49
-186
lines changed

ReactCommon/react/renderer/.clang-tidy

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,11 @@ clang-analyzer-valist.Uninitialized,
185185
clang-analyzer-valist.Unterminated,
186186
google-build-using-namespace,
187187
'
188+
189+
CheckOptions:
190+
- key: performance-unnecessary-value-param.AllowedTypes
191+
value: '[Pp]ointer$;[Pp]tr$;[Rr]ef(erence)?$;'
192+
- key: performance-unnecessary-copy-initialization.AllowedTypes
193+
value: '[Pp]ointer$;[Pp]tr$;[Rr]ef(erence)?$'
194+
188195
...

ReactCommon/react/renderer/graphics/BUCK

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,13 @@ rn_xplat_cxx_library(
5656
fbandroid_exported_headers = subdir_glob(
5757
[
5858
("platform/android/react/renderer/graphics", "**/*.h"),
59-
("platform/cxx/react/renderer/graphics", "**/*.h"),
6059
],
61-
exclude = ["platform/cxx/react/renderer/graphics/PlatformColorParser.h"],
6260
prefix = "react/renderer/graphics",
6361
),
6462
fbandroid_srcs = glob(
6563
[
66-
"platform/cxx/react/renderer/graphics/**/*.cpp",
6764
"platform/android/react/renderer/graphics/**/*.cpp",
6865
],
69-
exclude = ["platform/cxx/react/renderer/graphics/PlatformColorParser.h"],
7066
),
7167
fbobjc_compiler_flags = APPLE_COMPILER_FLAGS,
7268
fbobjc_preprocessor_flags = get_preprocessor_flags_for_build_mode() + get_apple_inspector_flags(),

ReactCommon/react/renderer/graphics/CMakeLists.txt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,13 @@ add_compile_options(
1515
-Wno-gnu-zero-variadic-macro-arguments
1616
-DLOG_TAG=\"Fabric\")
1717

18-
file(GLOB react_render_graphics_SRC CONFIGURE_DEPENDS
19-
*.cpp
20-
platform/cxx/react/renderer/graphics/*.cpp)
21-
22-
add_library(react_render_graphics
23-
SHARED
24-
${react_render_graphics_SRC})
18+
file(GLOB react_render_graphics_SRC CONFIGURE_DEPENDS *.cpp)
19+
add_library(react_render_graphics SHARED ${react_render_graphics_SRC})
2520

2621
target_include_directories(react_render_graphics
2722
PUBLIC
2823
${REACT_COMMON_DIR}
29-
${CMAKE_CURRENT_SOURCE_DIR}/platform/cxx/
24+
${CMAKE_CURRENT_SOURCE_DIR}/platform/android/
3025
)
3126

3227
target_link_libraries(react_render_graphics glog fb fbjni folly_runtime react_debug)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ SharedColor colorFromComponents(ColorComponents components) {
2626
((int)round(components.blue * ratio) & 0xff)};
2727
}
2828

29-
ColorComponents colorComponentsFromColor(SharedColor const &sharedColor) {
29+
ColorComponents colorComponentsFromColor(SharedColor sharedColor) {
3030
float ratio = 255;
3131
Color color = *sharedColor;
3232
return ColorComponents{

ReactCommon/react/renderer/graphics/platform/ios/Color.h renamed to ReactCommon/react/renderer/graphics/Color.h

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,29 @@
88
#pragma once
99

1010
#include <cmath>
11-
#include <optional>
11+
#include <functional>
12+
#include <limits>
1213

13-
#include <folly/Hash.h>
1414
#include <react/renderer/graphics/ColorComponents.h>
15-
#include <react/renderer/graphics/Float.h>
1615

17-
namespace facebook {
18-
namespace react {
16+
namespace facebook::react {
1917

2018
using Color = int32_t;
2119

20+
/*
21+
* On Android, a color can be represented as 32 bits integer, so there is no
22+
* need to instantiate complex color objects and then pass them as shared
23+
* pointers. Hense instead of using shared_ptr, we use a simple wrapper class
24+
* which provides a pointer-like interface.
25+
*/
2226
class SharedColor {
2327
public:
2428
static const Color UndefinedColor = std::numeric_limits<Color>::max();
2529

2630
SharedColor() : color_(UndefinedColor) {}
2731

28-
SharedColor(const SharedColor &sharedColor) : color_(sharedColor.color_) {}
29-
3032
SharedColor(Color color) : color_(color) {}
3133

32-
SharedColor &operator=(const SharedColor &sharedColor) {
33-
color_ = sharedColor.color_;
34-
return *this;
35-
}
36-
3734
Color operator*() const {
3835
return color_;
3936
}
@@ -62,12 +59,11 @@ SharedColor clearColor();
6259
SharedColor blackColor();
6360
SharedColor whiteColor();
6461

65-
} // namespace react
66-
} // namespace facebook
62+
} // namespace facebook::react
6763

6864
template <>
6965
struct std::hash<facebook::react::SharedColor> {
70-
std::size_t operator()(facebook::react::SharedColor const &color) const {
71-
return hash<int>()(*color);
66+
size_t operator()(facebook::react::SharedColor color) const {
67+
return std::hash<decltype(*color)>{}(*color);
7268
}
7369
};

ReactCommon/react/renderer/graphics/conversions.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,9 @@ inline void fromRawValue(
5050
}
5151

5252
#ifdef ANDROID
53-
5453
inline int toAndroidRepr(const SharedColor &color) {
55-
ColorComponents components = colorComponentsFromColor(color);
56-
auto ratio = 255.f;
57-
return (
58-
((int)round(components.alpha * ratio) & 0xff) << 24 |
59-
((int)round(components.red * ratio) & 0xff) << 16 |
60-
((int)round(components.green * ratio) & 0xff) << 8 |
61-
((int)round(components.blue * ratio) & 0xff));
54+
return *color;
6255
}
63-
6456
#endif
6557

6658
inline std::string toString(const SharedColor &value) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 <limits>
11+
12+
namespace facebook {
13+
namespace react {
14+
15+
/*
16+
* Exact type of float numbers which ideally should match a type behing
17+
* platform- and chip-architecture-specific float type.
18+
*/
19+
using Float = float;
20+
21+
} // namespace react
22+
} // namespace facebook

ReactCommon/react/renderer/graphics/platform/android/react/renderer/graphics/PlatformColorParser.h

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

1010
#include <fbjni/fbjni.h>
11-
#include <react/jni/ReadableNativeMap.h>
1211
#include <react/renderer/core/PropsParserContext.h>
1312
#include <react/renderer/core/RawProps.h>
1413
#include <react/renderer/graphics/ColorComponents.h>

ReactCommon/react/renderer/graphics/platform/cxx/react/renderer/graphics/Color.h

Lines changed: 0 additions & 80 deletions
This file was deleted.

ReactCommon/react/renderer/graphics/platform/ios/Color.cpp

Lines changed: 0 additions & 63 deletions
This file was deleted.

ReactCommon/react/renderer/graphics/platform/ios/RCTPlatformColorUtils.mm

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#import "RCTPlatformColorUtils.h"
99

1010
#import <Foundation/Foundation.h>
11-
#import <React/RCTUtils.h>
1211
#import <UIKit/UIKit.h>
1312

1413
#include <string>
@@ -180,7 +179,7 @@
180179
static inline facebook::react::ColorComponents _ColorComponentsFromUIColor(UIColor *color)
181180
{
182181
CGFloat rgba[4];
183-
RCTGetRGBAColorComponents(color.CGColor, rgba);
182+
[color getRed:&rgba[0] green:&rgba[1] blue:&rgba[2] alpha:&rgba[3]];
184183
return {(float)rgba[0], (float)rgba[1], (float)rgba[2], (float)rgba[3]};
185184
}
186185

packages/rn-tester/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ SPEC CHECKSUMS:
951951
FlipperKit: cbdee19bdd4e7f05472a66ce290f1b729ba3cb86
952952
fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9
953953
glog: 04b94705f318337d7ead9e6d17c019bd9b1f6b1b
954-
hermes-engine: 445a2267b04cb39ca4a0b2d6758b5a0e5a58ccad
954+
hermes-engine: 05b2399259b25f6c105858adc778c04342c1f424
955955
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
956956
OpenSSL-Universal: ebc357f1e6bc71fa463ccb2fe676756aff50e88c
957957
RCT-Folly: 424b8c9a7a0b9ab2886ffe9c3b041ef628fd4fb1
@@ -989,7 +989,7 @@ SPEC CHECKSUMS:
989989
React-rncore: 665c70690f404bbfa3948148de72689672a906d2
990990
React-runtimeexecutor: 97dca9247f4d3cfe0733384b189c6930fbd402b7
991991
ReactCommon: b1f213aa09e3dfd0a89389b5023fdb1cd6528e96
992-
ScreenshotManager: 06cb3d1794c8082d92b3e91813d1678d0977a4fb
992+
ScreenshotManager: cf552c19152e3357f08875fc2f85adb2dee6a66b
993993
SocketRocket: fccef3f9c5cedea1353a9ef6ada904fde10d6608
994994
Yoga: 1b1a12ff3d86a10565ea7cbe057d42f5e5fb2a07
995995
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a

0 commit comments

Comments
 (0)