From 2d1fabbab23bb18377b1de9be24ab9a9a68253eb Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Tue, 17 Jul 2018 18:05:40 -0700 Subject: [PATCH] Fabric: `SharedColor` for Android Summary: @public 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. Reviewed By: mdvacca Differential Revision: D8742014 fbshipit-source-id: 14109b61fd84a34989538a15bc6fe4e2a8ce83a6 --- .../fabric/attributedstring/TextAttributes.h | 8 ++-- .../graphics/platform/android/Color.cpp | 19 ++++++--- .../fabric/graphics/platform/android/Color.h | 42 +++++++++++++++++-- 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/ReactCommon/fabric/attributedstring/TextAttributes.h b/ReactCommon/fabric/attributedstring/TextAttributes.h index 584387bf547..2b8c0498219 100644 --- a/ReactCommon/fabric/attributedstring/TextAttributes.h +++ b/ReactCommon/fabric/attributedstring/TextAttributes.h @@ -31,8 +31,8 @@ public: #pragma mark - Fields // Color - SharedColor foregroundColor {nullptr}; - SharedColor backgroundColor {nullptr}; + SharedColor foregroundColor {}; + SharedColor backgroundColor {}; Float opacity {std::numeric_limits::quiet_NaN()}; // Font @@ -51,7 +51,7 @@ public: folly::Optional baseWritingDirection {}; // Decoration - SharedColor textDecorationColor {nullptr}; + SharedColor textDecorationColor {}; folly::Optional textDecorationLineType {}; folly::Optional textDecorationLineStyle {}; folly::Optional textDecorationLinePattern {}; @@ -60,7 +60,7 @@ public: // TODO: Use `Point` type instead of `Size` for `textShadowOffset` attribute. folly::Optional textShadowOffset {}; Float textShadowRadius {std::numeric_limits::quiet_NaN()}; - SharedColor textShadowColor {nullptr}; + SharedColor textShadowColor {}; // Special folly::Optional isHighlighted {}; diff --git a/ReactCommon/fabric/graphics/platform/android/Color.cpp b/ReactCommon/fabric/graphics/platform/android/Color.cpp index 3511eeb3be8..9a7e88ac248 100644 --- a/ReactCommon/fabric/graphics/platform/android/Color.cpp +++ b/ReactCommon/fabric/graphics/platform/android/Color.cpp @@ -11,13 +11,22 @@ namespace facebook { namespace react { SharedColor colorFromComponents(ColorComponents components) { - // Not implemented. - return {}; + return SharedColor( + ((int)components.alpha & 0xff) << 24 | + ((int)components.red & 0xff) << 16 | + ((int)components.green & 0xff) << 8 | + ((int)components.blue & 0xff) + ); } -ColorComponents colorComponentsFromColor(SharedColor color) { - // Not implemented. - return {}; +ColorComponents colorComponentsFromColor(SharedColor sharedColor) { + Color color = *sharedColor; + return ColorComponents { + (float)((color >> 16) & 0xff), + (float)((color >> 8) & 0xff), + (float)((color ) & 0xff), + (float)((color >> 24) & 0xff) + }; } } // namespace react diff --git a/ReactCommon/fabric/graphics/platform/android/Color.h b/ReactCommon/fabric/graphics/platform/android/Color.h index 761af1d2de0..ff6ed8ef480 100644 --- a/ReactCommon/fabric/graphics/platform/android/Color.h +++ b/ReactCommon/fabric/graphics/platform/android/Color.h @@ -7,15 +7,51 @@ #pragma once -#include +#include #include namespace facebook { namespace react { -using Color = float; -using SharedColor = std::shared_ptr; +using Color = int; + +/* + * 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::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_; + } + + operator bool() const { + return color_ != UndefinedColor; + } + +private: + Color color_; +}; SharedColor colorFromComponents(ColorComponents components); ColorComponents colorComponentsFromColor(SharedColor color);