From 608d1fb5909a3c6574b47ecda77b7647ba5776a5 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Sun, 18 Mar 2018 19:04:13 -0700 Subject: [PATCH] `fabric/graphics` module: all about graphics Summary: `fabric/graphics` provides graphics primitives; the implementation should be platform-specific eventually. Reviewed By: fkgozali Differential Revision: D7230675 fbshipit-source-id: ff05d2673072fad5ee6f331f0155561969e1a8b6 --- ReactCommon/fabric/core/BUCK | 1 + ReactCommon/fabric/graphics/BUCK | 50 ++++++++++++++++ ReactCommon/fabric/graphics/Color.cpp | 39 ++++++++++++ ReactCommon/fabric/graphics/Color.h | 30 ++++++++++ ReactCommon/fabric/graphics/Geometry.h | 60 +++++++++++++++++++ .../graphics/graphicValuesConversions.cpp | 51 ++++++++++++++++ .../graphics/graphicValuesConversions.h | 20 +++++++ 7 files changed, 251 insertions(+) create mode 100644 ReactCommon/fabric/graphics/BUCK create mode 100644 ReactCommon/fabric/graphics/Color.cpp create mode 100644 ReactCommon/fabric/graphics/Color.h create mode 100644 ReactCommon/fabric/graphics/Geometry.h create mode 100644 ReactCommon/fabric/graphics/graphicValuesConversions.cpp create mode 100644 ReactCommon/fabric/graphics/graphicValuesConversions.h diff --git a/ReactCommon/fabric/core/BUCK b/ReactCommon/fabric/core/BUCK index ec92dc42c2c..977460fc598 100644 --- a/ReactCommon/fabric/core/BUCK +++ b/ReactCommon/fabric/core/BUCK @@ -48,5 +48,6 @@ rn_xplat_cxx_library( "xplat//folly:molly", "xplat//third-party/glog:glog", react_native_xplat_target("fabric/debug:debug"), + react_native_xplat_target("fabric/graphics:graphics"), ], ) diff --git a/ReactCommon/fabric/graphics/BUCK b/ReactCommon/fabric/graphics/BUCK new file mode 100644 index 00000000000..e1a020d730b --- /dev/null +++ b/ReactCommon/fabric/graphics/BUCK @@ -0,0 +1,50 @@ +load("//configurations/buck/apple:flag_defs.bzl", "get_debug_preprocessor_flags") +load("//ReactNative:DEFS.bzl", "IS_OSS_BUILD", "react_native_xplat_target", "rn_xplat_cxx_library", "APPLE_INSPECTOR_FLAGS") + +APPLE_COMPILER_FLAGS = [] + +if not IS_OSS_BUILD: + load("@xplat//configurations/buck/apple:flag_defs.bzl", "get_static_library_ios_flags", "flags") + APPLE_COMPILER_FLAGS = flags.get_flag_value(get_static_library_ios_flags(), 'compiler_flags') + +rn_xplat_cxx_library( + name = "graphics", + srcs = glob( + [ + "**/*.cpp", + ], + ), + headers = glob( + [ + "**/*.h", + ], + ), + header_namespace = "", + exported_headers = subdir_glob( + [ + ("", "*.h"), + ], + prefix = "fabric/graphics", + ), + compiler_flags = [ + "-std=c++14", + "-Wall", + "-fexceptions", + "-frtti", + ], + fbobjc_compiler_flags = APPLE_COMPILER_FLAGS, + fbobjc_preprocessor_flags = get_debug_preprocessor_flags() + APPLE_INSPECTOR_FLAGS, + force_static = True, + preprocessor_flags = [ + "-DLOG_TAG=\"ReactNative\"", + "-DWITH_FBSYSTRACE=1", + ], + visibility = ["PUBLIC"], + deps = [ + "xplat//fbsystrace:fbsystrace", + "xplat//folly:headers_only", + "xplat//folly:memory", + "xplat//folly:molly", + "xplat//third-party/glog:glog", + ], +) diff --git a/ReactCommon/fabric/graphics/Color.cpp b/ReactCommon/fabric/graphics/Color.cpp new file mode 100644 index 00000000000..ba6c897b493 --- /dev/null +++ b/ReactCommon/fabric/graphics/Color.cpp @@ -0,0 +1,39 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include "Color.h" +#include +#include +#include + +namespace facebook { +namespace react { + +SharedColor colorFromComponents(float red, float green, float blue, float alpha) { + const CGFloat components[] = {red, green, blue, alpha}; + CGColorRef color = CGColorCreate( + CGColorSpaceCreateDeviceRGB(), + components + ); + + return SharedColor(color, CFRelease); +} + +ColorComponents colorComponentsFromColor(SharedColor color) { + int numberOfComponents = CGColorGetNumberOfComponents(color.get()); + assert(numberOfComponents == 4); + const CGFloat *components = CGColorGetComponents(color.get()); + return ColorComponents { + (float)components[0], + (float)components[1], + (float)components[2], + (float)components[3] + }; +} + +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/graphics/Color.h b/ReactCommon/fabric/graphics/Color.h new file mode 100644 index 00000000000..0a84e7b3a97 --- /dev/null +++ b/ReactCommon/fabric/graphics/Color.h @@ -0,0 +1,30 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * + * 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 +#include + +namespace facebook { +namespace react { + +using Color = CGColor; +using SharedColor = std::shared_ptr; + +struct ColorComponents { + float red {0}; + float green {0}; + float blue {0}; + float alpha {0}; +}; + +SharedColor colorFromComponents(float red, float green, float blue, float alpha); +ColorComponents colorComponentsFromColor(SharedColor color); + +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/graphics/Geometry.h b/ReactCommon/fabric/graphics/Geometry.h new file mode 100644 index 00000000000..915cc3b7683 --- /dev/null +++ b/ReactCommon/fabric/graphics/Geometry.h @@ -0,0 +1,60 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +#pragma once + +namespace facebook { +namespace react { + +/* + * Exact type of float numbers which ideally should match a type behing + * platform- and chip-architecture-specific float type (something like + * CGFloat on iOS). + */ +using Float = double; + +/* + * Point + */ +struct Point { + Float x {0}; + Float y {0}; + + Point& operator += (const Point& rhs) { + x += rhs.x; + y += rhs.y; + return *this; + } + + friend Point operator + (Point lhs, const Point& rhs) { + return lhs += rhs; + } +}; + +/* + * Size + */ +struct Size { + Float width {0}; + Float height {0}; +}; + +/* + * Rect: Point and Size + */ +struct Rect { + Point origin {0, 0}; + Size size {0, 0}; +}; + +/* + * EdgeInsets + */ +struct EdgeInsets { + Float top {0}; + Float left {0}; + Float bottom {0}; + Float right {0}; +}; + +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/graphics/graphicValuesConversions.cpp b/ReactCommon/fabric/graphics/graphicValuesConversions.cpp new file mode 100644 index 00000000000..cfd80a4a114 --- /dev/null +++ b/ReactCommon/fabric/graphics/graphicValuesConversions.cpp @@ -0,0 +1,51 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include "graphicValuesConversions.h" + +namespace facebook { +namespace react { + +SharedColor colorFromDynamic(folly::dynamic value) { + float red; + float green; + float blue; + float alpha; + + if (value.isNumber()) { + auto argb = value.asInt(); + float ratio = 256; + alpha = ((argb >> 24) & 0xFF) / ratio; + red = ((argb >> 16) & 0xFF) / ratio; + green = ((argb >> 8) & 0xFF) / ratio; + blue = (argb & 0xFF) / ratio; + } else if (value.isArray()) { + auto size = value.size(); + assert(size == 3 || size == 4); + red = value[0].asDouble(); + green = value[1].asDouble(); + blue = value[2].asDouble(); + alpha = size == 4 ? value[3].asDouble() : 1.0; + } else { + abort(); + } + + return colorFromComponents(red, green, blue, alpha); +} + +std::string colorNameFromColor(SharedColor value) { + ColorComponents components = colorComponentsFromColor(value); + const float ratio = 256; + return "rgba(" + + folly::to(round(components.red * ratio)) + ", " + + folly::to(round(components.green * ratio)) + ", " + + folly::to(round(components.blue * ratio)) + ", " + + folly::to(round(components.alpha * ratio)) + ")"; +} + +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/graphics/graphicValuesConversions.h b/ReactCommon/fabric/graphics/graphicValuesConversions.h new file mode 100644 index 00000000000..8ee9e2799d3 --- /dev/null +++ b/ReactCommon/fabric/graphics/graphicValuesConversions.h @@ -0,0 +1,20 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * + * 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 +#include + +namespace facebook { +namespace react { + +SharedColor colorFromDynamic(folly::dynamic value); +std::string colorNameFromColor(SharedColor value); + +} // namespace react +} // namespace facebook