Skip to content

Commit cdce14f

Browse files
mdvaccafacebook-github-bot
authored andcommitted
Implement PlatformColor in Fabric Android
Summary: This diff implements PlatformColor in Fabric Android changelog: [internal] internal Reviewed By: JoshuaGross Differential Revision: D29841461 fbshipit-source-id: 63a523626b021c634bc399e749b639b55730391a
1 parent 8066bc9 commit cdce14f

File tree

8 files changed

+169
-16
lines changed

8 files changed

+169
-16
lines changed

ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.facebook.debug.tags.ReactDebugOverlayTags;
3636
import com.facebook.infer.annotation.ThreadConfined;
3737
import com.facebook.proguard.annotations.DoNotStripAny;
38+
import com.facebook.react.bridge.ColorPropConverter;
3839
import com.facebook.react.bridge.LifecycleEventListener;
3940
import com.facebook.react.bridge.NativeArray;
4041
import com.facebook.react.bridge.NativeMap;
@@ -458,6 +459,14 @@ private long measure(
458459
null);
459460
}
460461

462+
@SuppressWarnings("unused")
463+
public int getColor(int surfaceId, ReadableMap platformColor) {
464+
ThemedReactContext context =
465+
mMountingManager.getSurfaceManagerEnforced(surfaceId, "getColor").getContext();
466+
Integer color = ColorPropConverter.getColor(platformColor, context);
467+
return color != null ? color : 0;
468+
}
469+
461470
@SuppressWarnings("unused")
462471
private long measure(
463472
int surfaceId,

ReactCommon/react/renderer/graphics/Android.mk

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LOCAL_MODULE := react_render_graphics
1111

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

14-
LOCAL_SHARED_LIBRARIES := libfolly_json libreact_debug
14+
LOCAL_SHARED_LIBRARIES := libfolly_json libreact_debug libfb libfbjni libfolly_json glog
1515

1616
LOCAL_STATIC_LIBRARIES :=
1717

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

2727
include $(BUILD_SHARED_LIBRARY)
2828

29+
$(call import-module,glog)
30+
$(call import-module,fbjni)
31+
$(call import-module,fb)
2932
$(call import-module,folly)
3033
$(call import-module,react/debug)

ReactCommon/react/renderer/graphics/BUCK

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ load(
33
"ANDROID",
44
"APPLE",
55
"CXX",
6+
"FBJNI_TARGET",
67
"fb_xplat_cxx_test",
78
"get_apple_compiler_flags",
89
"get_apple_inspector_flags",
910
"get_preprocessor_flags_for_build_mode",
11+
"react_native_target",
1012
"react_native_xplat_target",
1113
"rn_xplat_cxx_library",
1214
"subdir_glob",
@@ -51,16 +53,25 @@ rn_xplat_cxx_library(
5153
"platform/cxx/react/renderer/graphics/**/*.cpp",
5254
],
5355
),
56+
fbandroid_allow_jni_merging = True,
57+
fbandroid_deps = [
58+
FBJNI_TARGET,
59+
react_native_target("jni/react/jni:jni"),
60+
],
5461
fbandroid_exported_headers = subdir_glob(
5562
[
63+
("platform/android/react/renderer/graphics", "**/*.h"),
5664
("platform/cxx/react/renderer/graphics", "**/*.h"),
5765
],
66+
exclude = ["platform/cxx/react/renderer/graphics/PlatformColorParser.h"],
5867
prefix = "react/renderer/graphics",
5968
),
6069
fbandroid_srcs = glob(
6170
[
6271
"platform/cxx/react/renderer/graphics/**/*.cpp",
72+
"platform/android/react/renderer/graphics/**/*.cpp",
6373
],
74+
exclude = ["platform/cxx/react/renderer/graphics/PlatformColorParser.h"],
6475
),
6576
fbobjc_compiler_flags = APPLE_COMPILER_FLAGS,
6677
fbobjc_preprocessor_flags = get_preprocessor_flags_for_build_mode() + get_apple_inspector_flags(),

ReactCommon/react/renderer/graphics/conversions.h

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
#pragma once
99

1010
#include <better/map.h>
11-
#include <folly/dynamic.h>
1211
#include <glog/logging.h>
1312
#include <react/debug/react_native_assert.h>
1413
#include <react/renderer/core/PropsParserContext.h>
1514
#include <react/renderer/core/RawProps.h>
1615
#include <react/renderer/graphics/Color.h>
1716
#include <react/renderer/graphics/Geometry.h>
17+
#include <react/renderer/graphics/PlatformColorParser.h>
1818

1919
namespace facebook {
2020
namespace react {
@@ -25,29 +25,28 @@ inline void fromRawValue(
2525
const PropsParserContext &context,
2626
const RawValue &value,
2727
SharedColor &result) {
28-
float red = 0;
29-
float green = 0;
30-
float blue = 0;
31-
float alpha = 0;
28+
ColorComponents colorComponents = {0, 0, 0, 0};
3229

3330
if (value.hasType<int>()) {
3431
auto argb = (int64_t)value;
3532
auto ratio = 255.f;
36-
alpha = ((argb >> 24) & 0xFF) / ratio;
37-
red = ((argb >> 16) & 0xFF) / ratio;
38-
green = ((argb >> 8) & 0xFF) / ratio;
39-
blue = (argb & 0xFF) / ratio;
33+
colorComponents.alpha = ((argb >> 24) & 0xFF) / ratio;
34+
colorComponents.red = ((argb >> 16) & 0xFF) / ratio;
35+
colorComponents.green = ((argb >> 8) & 0xFF) / ratio;
36+
colorComponents.blue = (argb & 0xFF) / ratio;
4037
} else if (value.hasType<std::vector<float>>()) {
4138
auto items = (std::vector<float>)value;
4239
auto length = items.size();
4340
react_native_assert(length == 3 || length == 4);
44-
red = items.at(0);
45-
green = items.at(1);
46-
blue = items.at(2);
47-
alpha = length == 4 ? items.at(3) : 1.0f;
41+
colorComponents.red = items.at(0);
42+
colorComponents.green = items.at(1);
43+
colorComponents.blue = items.at(2);
44+
colorComponents.alpha = length == 4 ? items.at(3) : 1.0f;
45+
} else {
46+
colorComponents = parsePlatformColor(context, value);
4847
}
4948

50-
result = colorFromComponents({red, green, blue, alpha});
49+
result = colorFromComponents(colorComponents);
5150
}
5251

5352
#ifdef ANDROID
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its 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 <fbjni/fbjni.h>
11+
#include <react/jni/ReadableNativeMap.h>
12+
#include <react/renderer/core/PropsParserContext.h>
13+
#include <react/renderer/core/RawProps.h>
14+
#include <react/renderer/graphics/ColorComponents.h>
15+
16+
using namespace facebook::jni;
17+
18+
namespace facebook {
19+
namespace react {
20+
21+
inline ColorComponents parsePlatformColor(
22+
const PropsParserContext &context,
23+
const RawValue &value) {
24+
ColorComponents colorComponents = {0, 0, 0, 0};
25+
26+
if (value.hasType<better::map<std::string, std::vector<std::string>>>()) {
27+
auto map = (better::map<std::string, std::vector<std::string>>)value;
28+
auto resourcePaths = map["resource_paths"];
29+
auto dynamicResourcePaths = folly::dynamic::array();
30+
for (const auto &resourcePath : resourcePaths) {
31+
dynamicResourcePaths.push_back(resourcePath);
32+
}
33+
folly::dynamic dynamicPlatformColor = folly::dynamic::object();
34+
dynamicPlatformColor["resource_paths"] = dynamicResourcePaths;
35+
36+
auto fabricUIManager =
37+
context.contextContainer.at<jni::global_ref<jobject>>(
38+
"FabricUIManager");
39+
40+
static auto getColorFromJava =
41+
facebook::jni::findClassStatic(
42+
"com/facebook/react/fabric/FabricUIManager")
43+
->getMethod<jint(jint, ReadableMap::javaobject)>("getColor");
44+
45+
local_ref<ReadableNativeMap::javaobject> dynamicPlatformColorRNM =
46+
ReadableNativeMap::newObjectCxxArgs(dynamicPlatformColor);
47+
local_ref<ReadableMap::javaobject> dynamicPlatformColorRM =
48+
make_local(reinterpret_cast<ReadableMap::javaobject>(
49+
dynamicPlatformColorRNM.get()));
50+
51+
auto color = getColorFromJava(
52+
fabricUIManager, context.surfaceId, dynamicPlatformColorRM.get());
53+
54+
dynamicPlatformColorRM.reset();
55+
dynamicPlatformColorRNM.reset();
56+
57+
auto argb = (int64_t)color;
58+
auto ratio = 255.f;
59+
colorComponents.alpha = ((argb >> 24) & 0xFF) / ratio;
60+
colorComponents.red = ((argb >> 16) & 0xFF) / ratio;
61+
colorComponents.green = ((argb >> 8) & 0xFF) / ratio;
62+
colorComponents.blue = (argb & 0xFF) / ratio;
63+
}
64+
65+
return colorComponents;
66+
}
67+
68+
} // namespace react
69+
} // namespace facebook
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its 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 <react/renderer/core/PropsParserContext.h>
11+
#include <react/renderer/core/RawProps.h>
12+
#include <react/renderer/graphics/ColorComponents.h>
13+
14+
namespace facebook {
15+
namespace react {
16+
17+
inline ColorComponents parsePlatformColor(
18+
const PropsParserContext &context,
19+
const RawValue &value) {
20+
float alpha = 0;
21+
float red = 0;
22+
float green = 0;
23+
float blue = 0;
24+
25+
return {red, green, blue, alpha};
26+
}
27+
28+
} // namespace react
29+
} // namespace facebook
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its 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 <react/renderer/core/PropsParserContext.h>
11+
#include <react/renderer/core/RawProps.h>
12+
#include <react/renderer/graphics/ColorComponents.h>
13+
14+
namespace facebook {
15+
namespace react {
16+
17+
inline ColorComponents parsePlatformColor(
18+
const PropsParserContext &context,
19+
const RawValue &value) {
20+
// TODO T87791134: implement parsing of PlatformColor for iOS
21+
float alpha = 0;
22+
float red = 0;
23+
float green = 0;
24+
float blue = 0;
25+
26+
return {red, green, blue, alpha};
27+
}
28+
29+
} // namespace react
30+
} // namespace facebook

packages/react-native-codegen/DEFS.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,10 +457,11 @@ def rn_codegen_components(
457457
# Tests
458458
fb_xplat_cxx_test(
459459
name = "generated_tests-{}".format(name),
460-
srcs = [
460+
srcs = [] if ANDROID else [
461461
":{}".format(generate_tests_cpp_name),
462462
],
463463
apple_sdks = (IOS, MACOSX),
464+
fbandroid_use_instrumentation_test = True,
464465
compiler_flags = [
465466
"-fexceptions",
466467
"-frtti",
@@ -471,6 +472,8 @@ def rn_codegen_components(
471472
labels = library_labels + ["codegen_rule"],
472473
platforms = (ANDROID, APPLE, CXX),
473474
deps = [
475+
YOGA_CXX_TARGET,
476+
react_native_xplat_target("react/renderer/core:core"),
474477
"//xplat/third-party/gmock:gtest",
475478
":generated_components-{}".format(name),
476479
],

0 commit comments

Comments
 (0)