Skip to content

Commit

Permalink
Implement PlatformColor in Fabric Android
Browse files Browse the repository at this point in the history
Summary:
This diff implements PlatformColor in Fabric Android

changelog: [internal] internal

Reviewed By: JoshuaGross

Differential Revision: D29841461

fbshipit-source-id: 63a523626b021c634bc399e749b639b55730391a
  • Loading branch information
mdvacca authored and facebook-github-bot committed Jul 31, 2021
1 parent 8066bc9 commit cdce14f
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.facebook.debug.tags.ReactDebugOverlayTags;
import com.facebook.infer.annotation.ThreadConfined;
import com.facebook.proguard.annotations.DoNotStripAny;
import com.facebook.react.bridge.ColorPropConverter;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.NativeArray;
import com.facebook.react.bridge.NativeMap;
Expand Down Expand Up @@ -458,6 +459,14 @@ private long measure(
null);
}

@SuppressWarnings("unused")
public int getColor(int surfaceId, ReadableMap platformColor) {
ThemedReactContext context =
mMountingManager.getSurfaceManagerEnforced(surfaceId, "getColor").getContext();
Integer color = ColorPropConverter.getColor(platformColor, context);
return color != null ? color : 0;
}

@SuppressWarnings("unused")
private long measure(
int surfaceId,
Expand Down
5 changes: 4 additions & 1 deletion ReactCommon/react/renderer/graphics/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ LOCAL_MODULE := react_render_graphics

LOCAL_SRC_FILES := $(wildcard $(LOCAL_PATH)/*.cpp $(LOCAL_PATH)/platform/cxx/react/renderer/graphics/*.cpp)

LOCAL_SHARED_LIBRARIES := libfolly_json libreact_debug
LOCAL_SHARED_LIBRARIES := libfolly_json libreact_debug libfb libfbjni libfolly_json glog

LOCAL_STATIC_LIBRARIES :=

Expand All @@ -26,5 +26,8 @@ LOCAL_CFLAGS += -fexceptions -frtti -std=c++17 -Wall

include $(BUILD_SHARED_LIBRARY)

$(call import-module,glog)
$(call import-module,fbjni)
$(call import-module,fb)
$(call import-module,folly)
$(call import-module,react/debug)
11 changes: 11 additions & 0 deletions ReactCommon/react/renderer/graphics/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ load(
"ANDROID",
"APPLE",
"CXX",
"FBJNI_TARGET",
"fb_xplat_cxx_test",
"get_apple_compiler_flags",
"get_apple_inspector_flags",
"get_preprocessor_flags_for_build_mode",
"react_native_target",
"react_native_xplat_target",
"rn_xplat_cxx_library",
"subdir_glob",
Expand Down Expand Up @@ -51,16 +53,25 @@ rn_xplat_cxx_library(
"platform/cxx/react/renderer/graphics/**/*.cpp",
],
),
fbandroid_allow_jni_merging = True,
fbandroid_deps = [
FBJNI_TARGET,
react_native_target("jni/react/jni:jni"),
],
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
27 changes: 13 additions & 14 deletions ReactCommon/react/renderer/graphics/conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
#pragma once

#include <better/map.h>
#include <folly/dynamic.h>
#include <glog/logging.h>
#include <react/debug/react_native_assert.h>
#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/core/RawProps.h>
#include <react/renderer/graphics/Color.h>
#include <react/renderer/graphics/Geometry.h>
#include <react/renderer/graphics/PlatformColorParser.h>

namespace facebook {
namespace react {
Expand All @@ -25,29 +25,28 @@ inline void fromRawValue(
const PropsParserContext &context,
const RawValue &value,
SharedColor &result) {
float red = 0;
float green = 0;
float blue = 0;
float alpha = 0;
ColorComponents colorComponents = {0, 0, 0, 0};

if (value.hasType<int>()) {
auto argb = (int64_t)value;
auto ratio = 255.f;
alpha = ((argb >> 24) & 0xFF) / ratio;
red = ((argb >> 16) & 0xFF) / ratio;
green = ((argb >> 8) & 0xFF) / ratio;
blue = (argb & 0xFF) / ratio;
colorComponents.alpha = ((argb >> 24) & 0xFF) / ratio;
colorComponents.red = ((argb >> 16) & 0xFF) / ratio;
colorComponents.green = ((argb >> 8) & 0xFF) / ratio;
colorComponents.blue = (argb & 0xFF) / ratio;
} else if (value.hasType<std::vector<float>>()) {
auto items = (std::vector<float>)value;
auto length = items.size();
react_native_assert(length == 3 || length == 4);
red = items.at(0);
green = items.at(1);
blue = items.at(2);
alpha = length == 4 ? items.at(3) : 1.0f;
colorComponents.red = items.at(0);
colorComponents.green = items.at(1);
colorComponents.blue = items.at(2);
colorComponents.alpha = length == 4 ? items.at(3) : 1.0f;
} else {
colorComponents = parsePlatformColor(context, value);
}

result = colorFromComponents({red, green, blue, alpha});
result = colorFromComponents(colorComponents);
}

#ifdef ANDROID
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) Facebook, Inc. and its 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 <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>

using namespace facebook::jni;

namespace facebook {
namespace react {

inline ColorComponents parsePlatformColor(
const PropsParserContext &context,
const RawValue &value) {
ColorComponents colorComponents = {0, 0, 0, 0};

if (value.hasType<better::map<std::string, std::vector<std::string>>>()) {
auto map = (better::map<std::string, std::vector<std::string>>)value;
auto resourcePaths = map["resource_paths"];
auto dynamicResourcePaths = folly::dynamic::array();
for (const auto &resourcePath : resourcePaths) {
dynamicResourcePaths.push_back(resourcePath);
}
folly::dynamic dynamicPlatformColor = folly::dynamic::object();
dynamicPlatformColor["resource_paths"] = dynamicResourcePaths;

auto fabricUIManager =
context.contextContainer.at<jni::global_ref<jobject>>(
"FabricUIManager");

static auto getColorFromJava =
facebook::jni::findClassStatic(
"com/facebook/react/fabric/FabricUIManager")
->getMethod<jint(jint, ReadableMap::javaobject)>("getColor");

local_ref<ReadableNativeMap::javaobject> dynamicPlatformColorRNM =
ReadableNativeMap::newObjectCxxArgs(dynamicPlatformColor);
local_ref<ReadableMap::javaobject> dynamicPlatformColorRM =
make_local(reinterpret_cast<ReadableMap::javaobject>(
dynamicPlatformColorRNM.get()));

auto color = getColorFromJava(
fabricUIManager, context.surfaceId, dynamicPlatformColorRM.get());

dynamicPlatformColorRM.reset();
dynamicPlatformColorRNM.reset();

auto argb = (int64_t)color;
auto ratio = 255.f;
colorComponents.alpha = ((argb >> 24) & 0xFF) / ratio;
colorComponents.red = ((argb >> 16) & 0xFF) / ratio;
colorComponents.green = ((argb >> 8) & 0xFF) / ratio;
colorComponents.blue = (argb & 0xFF) / ratio;
}

return colorComponents;
}

} // namespace react
} // namespace facebook
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) Facebook, Inc. and its 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 <react/renderer/core/PropsParserContext.h>
#include <react/renderer/core/RawProps.h>
#include <react/renderer/graphics/ColorComponents.h>

namespace facebook {
namespace react {

inline ColorComponents parsePlatformColor(
const PropsParserContext &context,
const RawValue &value) {
float alpha = 0;
float red = 0;
float green = 0;
float blue = 0;

return {red, green, blue, alpha};
}

} // namespace react
} // namespace facebook
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) Facebook, Inc. and its 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 <react/renderer/core/PropsParserContext.h>
#include <react/renderer/core/RawProps.h>
#include <react/renderer/graphics/ColorComponents.h>

namespace facebook {
namespace react {

inline ColorComponents parsePlatformColor(
const PropsParserContext &context,
const RawValue &value) {
// TODO T87791134: implement parsing of PlatformColor for iOS
float alpha = 0;
float red = 0;
float green = 0;
float blue = 0;

return {red, green, blue, alpha};
}

} // namespace react
} // namespace facebook
5 changes: 4 additions & 1 deletion packages/react-native-codegen/DEFS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -457,10 +457,11 @@ def rn_codegen_components(
# Tests
fb_xplat_cxx_test(
name = "generated_tests-{}".format(name),
srcs = [
srcs = [] if ANDROID else [
":{}".format(generate_tests_cpp_name),
],
apple_sdks = (IOS, MACOSX),
fbandroid_use_instrumentation_test = True,
compiler_flags = [
"-fexceptions",
"-frtti",
Expand All @@ -471,6 +472,8 @@ def rn_codegen_components(
labels = library_labels + ["codegen_rule"],
platforms = (ANDROID, APPLE, CXX),
deps = [
YOGA_CXX_TARGET,
react_native_xplat_target("react/renderer/core:core"),
"//xplat/third-party/gmock:gtest",
":generated_components-{}".format(name),
],
Expand Down

0 comments on commit cdce14f

Please sign in to comment.