Skip to content

Commit

Permalink
Shared Fabric color implementation across platforms
Browse files Browse the repository at this point in the history
Summary: Changelog: [Internal] Align C++ implementations of SharedColor

Reviewed By: mdvacca

Differential Revision: D40632527

fbshipit-source-id: 8ebca5157e5898de4311015c92b5a72dca7197d3
  • Loading branch information
javache authored and facebook-github-bot committed Nov 7, 2022
1 parent 3823703 commit 3e44d20
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 186 deletions.
7 changes: 7 additions & 0 deletions ReactCommon/react/renderer/.clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,11 @@ clang-analyzer-valist.Uninitialized,
clang-analyzer-valist.Unterminated,
google-build-using-namespace,
'

CheckOptions:
- key: performance-unnecessary-value-param.AllowedTypes
value: '[Pp]ointer$;[Pp]tr$;[Rr]ef(erence)?$;'
- key: performance-unnecessary-copy-initialization.AllowedTypes
value: '[Pp]ointer$;[Pp]tr$;[Rr]ef(erence)?$'

...
4 changes: 0 additions & 4 deletions ReactCommon/react/renderer/graphics/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,13 @@ rn_xplat_cxx_library(
fbandroid_exported_headers = subdir_glob(
[
("platform/android/react/renderer/graphics", "**/*.h"),
("platform/cxx/react/renderer/graphics", "**/*.h"),
],
exclude = ["platform/cxx/react/renderer/graphics/PlatformColorParser.h"],
prefix = "react/renderer/graphics",
),
fbandroid_srcs = glob(
[
"platform/cxx/react/renderer/graphics/**/*.cpp",
"platform/android/react/renderer/graphics/**/*.cpp",
],
exclude = ["platform/cxx/react/renderer/graphics/PlatformColorParser.h"],
),
fbobjc_compiler_flags = APPLE_COMPILER_FLAGS,
fbobjc_preprocessor_flags = get_preprocessor_flags_for_build_mode() + get_apple_inspector_flags(),
Expand Down
11 changes: 3 additions & 8 deletions ReactCommon/react/renderer/graphics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@ add_compile_options(
-Wno-gnu-zero-variadic-macro-arguments
-DLOG_TAG=\"Fabric\")

file(GLOB react_render_graphics_SRC CONFIGURE_DEPENDS
*.cpp
platform/cxx/react/renderer/graphics/*.cpp)

add_library(react_render_graphics
SHARED
${react_render_graphics_SRC})
file(GLOB react_render_graphics_SRC CONFIGURE_DEPENDS *.cpp)
add_library(react_render_graphics SHARED ${react_render_graphics_SRC})

target_include_directories(react_render_graphics
PUBLIC
${REACT_COMMON_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/platform/cxx/
${CMAKE_CURRENT_SOURCE_DIR}/platform/android/
)

target_link_libraries(react_render_graphics glog fb fbjni folly_runtime react_debug)
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ SharedColor colorFromComponents(ColorComponents components) {
((int)round(components.blue * ratio) & 0xff)};
}

ColorComponents colorComponentsFromColor(SharedColor const &sharedColor) {
ColorComponents colorComponentsFromColor(SharedColor sharedColor) {
float ratio = 255;
Color color = *sharedColor;
return ColorComponents{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,29 @@
#pragma once

#include <cmath>
#include <optional>
#include <functional>
#include <limits>

#include <folly/Hash.h>
#include <react/renderer/graphics/ColorComponents.h>
#include <react/renderer/graphics/Float.h>

namespace facebook {
namespace react {
namespace facebook::react {

using Color = int32_t;

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

SharedColor() : color_(UndefinedColor) {}

SharedColor(const SharedColor &sharedColor) : color_(sharedColor.color_) {}

SharedColor(Color color) : color_(color) {}

SharedColor &operator=(const SharedColor &sharedColor) {
color_ = sharedColor.color_;
return *this;
}

Color operator*() const {
return color_;
}
Expand Down Expand Up @@ -62,12 +59,11 @@ SharedColor clearColor();
SharedColor blackColor();
SharedColor whiteColor();

} // namespace react
} // namespace facebook
} // namespace facebook::react

template <>
struct std::hash<facebook::react::SharedColor> {
std::size_t operator()(facebook::react::SharedColor const &color) const {
return hash<int>()(*color);
size_t operator()(facebook::react::SharedColor color) const {
return std::hash<decltype(*color)>{}(*color);
}
};
10 changes: 1 addition & 9 deletions ReactCommon/react/renderer/graphics/conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,9 @@ inline void fromRawValue(
}

#ifdef ANDROID

inline int toAndroidRepr(const SharedColor &color) {
ColorComponents components = colorComponentsFromColor(color);
auto ratio = 255.f;
return (
((int)round(components.alpha * ratio) & 0xff) << 24 |
((int)round(components.red * ratio) & 0xff) << 16 |
((int)round(components.green * ratio) & 0xff) << 8 |
((int)round(components.blue * ratio) & 0xff));
return *color;
}

#endif

inline std::string toString(const SharedColor &value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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 <limits>

namespace facebook {
namespace react {

/*
* Exact type of float numbers which ideally should match a type behing
* platform- and chip-architecture-specific float type.
*/
using Float = float;

} // namespace react
} // namespace facebook
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#pragma once

#include <fbjni/fbjni.h>
#include <react/jni/ReadableNativeMap.h>
#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/core/RawProps.h>
#include <react/renderer/graphics/ColorComponents.h>
Expand Down

This file was deleted.

63 changes: 0 additions & 63 deletions ReactCommon/react/renderer/graphics/platform/ios/Color.cpp

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#import "RCTPlatformColorUtils.h"

#import <Foundation/Foundation.h>
#import <React/RCTUtils.h>
#import <UIKit/UIKit.h>

#include <string>
Expand Down Expand Up @@ -180,7 +179,7 @@
static inline facebook::react::ColorComponents _ColorComponentsFromUIColor(UIColor *color)
{
CGFloat rgba[4];
RCTGetRGBAColorComponents(color.CGColor, rgba);
[color getRed:&rgba[0] green:&rgba[1] blue:&rgba[2] alpha:&rgba[3]];
return {(float)rgba[0], (float)rgba[1], (float)rgba[2], (float)rgba[3]};
}

Expand Down
4 changes: 2 additions & 2 deletions packages/rn-tester/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ SPEC CHECKSUMS:
FlipperKit: cbdee19bdd4e7f05472a66ce290f1b729ba3cb86
fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9
glog: 04b94705f318337d7ead9e6d17c019bd9b1f6b1b
hermes-engine: 445a2267b04cb39ca4a0b2d6758b5a0e5a58ccad
hermes-engine: 05b2399259b25f6c105858adc778c04342c1f424
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
OpenSSL-Universal: ebc357f1e6bc71fa463ccb2fe676756aff50e88c
RCT-Folly: 424b8c9a7a0b9ab2886ffe9c3b041ef628fd4fb1
Expand Down Expand Up @@ -989,7 +989,7 @@ SPEC CHECKSUMS:
React-rncore: 665c70690f404bbfa3948148de72689672a906d2
React-runtimeexecutor: 97dca9247f4d3cfe0733384b189c6930fbd402b7
ReactCommon: b1f213aa09e3dfd0a89389b5023fdb1cd6528e96
ScreenshotManager: 06cb3d1794c8082d92b3e91813d1678d0977a4fb
ScreenshotManager: cf552c19152e3357f08875fc2f85adb2dee6a66b
SocketRocket: fccef3f9c5cedea1353a9ef6ada904fde10d6608
Yoga: 1b1a12ff3d86a10565ea7cbe057d42f5e5fb2a07
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a
Expand Down

0 comments on commit 3e44d20

Please sign in to comment.