From 0d819c95f3d756a94af245d84b3926531247d84a Mon Sep 17 00:00:00 2001 From: Ruslan Shestopalyuk Date: Tue, 7 May 2024 03:49:46 -0700 Subject: [PATCH] Move fabric.Binding* to Kotlin (#44438) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/44438 # Changelog: [Internal] - As in the title. Differential Revision: D57046289 --- .../ReactAndroid/api/ReactAndroid.api | 2 +- .../com/facebook/react/fabric/Binding.java | 69 ---------- .../java/com/facebook/react/fabric/Binding.kt | 72 +++++++++++ .../facebook/react/fabric/BindingImpl.java | 121 ------------------ .../com/facebook/react/fabric/BindingImpl.kt | 113 ++++++++++++++++ 5 files changed, 186 insertions(+), 191 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/Binding.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/Binding.kt delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/BindingImpl.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/BindingImpl.kt diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index d4e9ddf113cb41..1070c0b1546d97 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -2474,7 +2474,7 @@ public abstract interface class com/facebook/react/fabric/Binding { public abstract fun unregisterSurface (Lcom/facebook/react/fabric/SurfaceHandlerBinding;)V } -public class com/facebook/react/fabric/BindingImpl : com/facebook/react/fabric/Binding { +public final class com/facebook/react/fabric/BindingImpl : com/facebook/react/fabric/Binding { public fun ()V public fun driveCxxAnimations ()V public fun getInspectorDataForInstance (Lcom/facebook/react/fabric/events/EventEmitterWrapper;)Lcom/facebook/react/bridge/ReadableNativeMap; diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/Binding.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/Binding.java deleted file mode 100644 index 8490c1f4b4cfee..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/Binding.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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. - */ - -package com.facebook.react.fabric; - -import androidx.annotation.NonNull; -import com.facebook.react.bridge.NativeMap; -import com.facebook.react.bridge.ReadableNativeMap; -import com.facebook.react.bridge.RuntimeExecutor; -import com.facebook.react.bridge.RuntimeScheduler; -import com.facebook.react.fabric.events.EventBeatManager; -import com.facebook.react.fabric.events.EventEmitterWrapper; - -public interface Binding { - - void startSurface(int surfaceId, @NonNull String moduleName, @NonNull NativeMap initialProps); - - void startSurfaceWithConstraints( - int surfaceId, - String moduleName, - NativeMap initialProps, - float minWidth, - float maxWidth, - float minHeight, - float maxHeight, - float offsetX, - float offsetY, - boolean isRTL, - boolean doLeftAndRightSwapInRTL); - - void stopSurface(int surfaceId); - - void setPixelDensity(float pointScaleFactor); - - void setConstraints( - int surfaceId, - float minWidth, - float maxWidth, - float minHeight, - float maxHeight, - float offsetX, - float offsetY, - boolean isRTL, - boolean doLeftAndRightSwapInRTL); - - void driveCxxAnimations(); - - void reportMount(int surfaceId); - - ReadableNativeMap getInspectorDataForInstance(EventEmitterWrapper eventEmitterWrapper); - - void register( - @NonNull RuntimeExecutor runtimeExecutor, - @NonNull RuntimeScheduler runtimeScheduler, - @NonNull FabricUIManager fabricUIManager, - @NonNull EventBeatManager eventBeatManager, - @NonNull ComponentFactory componentFactory, - @NonNull ReactNativeConfig reactNativeConfig); - - void unregister(); - - void registerSurface(SurfaceHandlerBinding surfaceHandler); - - void unregisterSurface(SurfaceHandlerBinding surfaceHandler); -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/Binding.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/Binding.kt new file mode 100644 index 00000000000000..6a80cb1d747a3d --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/Binding.kt @@ -0,0 +1,72 @@ +/* + * 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. + */ + +package com.facebook.react.fabric + +import com.facebook.react.bridge.NativeMap +import com.facebook.react.bridge.ReadableNativeMap +import com.facebook.react.bridge.RuntimeExecutor +import com.facebook.react.bridge.RuntimeScheduler +import com.facebook.react.fabric.events.EventBeatManager +import com.facebook.react.fabric.events.EventEmitterWrapper + +public interface Binding { + public fun startSurface(surfaceId: Int, moduleName: String, initialProps: NativeMap) + + public fun startSurfaceWithConstraints( + surfaceId: Int, + moduleName: String?, + initialProps: NativeMap?, + minWidth: Float, + maxWidth: Float, + minHeight: Float, + maxHeight: Float, + offsetX: Float, + offsetY: Float, + isRTL: Boolean, + doLeftAndRightSwapInRTL: Boolean + ) + + public fun stopSurface(surfaceId: Int) + + public fun setPixelDensity(pointScaleFactor: Float) + + public fun setConstraints( + surfaceId: Int, + minWidth: Float, + maxWidth: Float, + minHeight: Float, + maxHeight: Float, + offsetX: Float, + offsetY: Float, + isRTL: Boolean, + doLeftAndRightSwapInRTL: Boolean + ) + + public fun driveCxxAnimations() + + public fun reportMount(surfaceId: Int) + + public fun getInspectorDataForInstance( + eventEmitterWrapper: EventEmitterWrapper? + ): ReadableNativeMap? + + public fun register( + runtimeExecutor: RuntimeExecutor, + runtimeScheduler: RuntimeScheduler, + fabricUIManager: FabricUIManager, + eventBeatManager: EventBeatManager, + componentFactory: ComponentFactory, + reactNativeConfig: ReactNativeConfig + ) + + public fun unregister() + + public fun registerSurface(surfaceHandler: SurfaceHandlerBinding?) + + public fun unregisterSurface(surfaceHandler: SurfaceHandlerBinding?) +} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/BindingImpl.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/BindingImpl.java deleted file mode 100644 index 324a530abed008..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/BindingImpl.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * 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. - */ - -package com.facebook.react.fabric; - -import android.annotation.SuppressLint; -import androidx.annotation.NonNull; -import com.facebook.infer.annotation.Nullsafe; -import com.facebook.jni.HybridData; -import com.facebook.proguard.annotations.DoNotStrip; -import com.facebook.react.bridge.NativeMap; -import com.facebook.react.bridge.ReadableNativeMap; -import com.facebook.react.bridge.RuntimeExecutor; -import com.facebook.react.bridge.RuntimeScheduler; -import com.facebook.react.common.mapbuffer.MapBufferSoLoader; -import com.facebook.react.fabric.events.EventBeatManager; -import com.facebook.react.fabric.events.EventEmitterWrapper; -import com.facebook.react.uimanager.PixelUtil; - -@Nullsafe(Nullsafe.Mode.LOCAL) -@DoNotStrip -@SuppressLint("MissingNativeLoadLibrary") -public class BindingImpl implements Binding { - - static { - FabricSoLoader.staticInit(); - MapBufferSoLoader.staticInit(); - } - - @DoNotStrip private final HybridData mHybridData; - - private static native HybridData initHybrid(); - - public BindingImpl() { - mHybridData = initHybrid(); - } - - private native void installFabricUIManager( - RuntimeExecutor runtimeExecutor, - RuntimeScheduler runtimeScheduler, - FabricUIManager uiManager, - EventBeatManager eventBeatManager, - ComponentFactory componentsRegistry, - Object reactNativeConfig); - - @Override - public native void startSurface( - int surfaceId, @NonNull String moduleName, @NonNull NativeMap initialProps); - - @Override - public native void startSurfaceWithConstraints( - int surfaceId, - String moduleName, - NativeMap initialProps, - float minWidth, - float maxWidth, - float minHeight, - float maxHeight, - float offsetX, - float offsetY, - boolean isRTL, - boolean doLeftAndRightSwapInRTL); - - @Override - public native void stopSurface(int surfaceId); - - @Override - public native void setPixelDensity(float pointScaleFactor); - - @Override - public native void setConstraints( - int surfaceId, - float minWidth, - float maxWidth, - float minHeight, - float maxHeight, - float offsetX, - float offsetY, - boolean isRTL, - boolean doLeftAndRightSwapInRTL); - - public native void driveCxxAnimations(); - - public native void reportMount(int surfaceId); - - public native ReadableNativeMap getInspectorDataForInstance( - EventEmitterWrapper eventEmitterWrapper); - - public void register( - @NonNull RuntimeExecutor runtimeExecutor, - @NonNull RuntimeScheduler runtimeScheduler, - @NonNull FabricUIManager fabricUIManager, - @NonNull EventBeatManager eventBeatManager, - @NonNull ComponentFactory componentFactory, - @NonNull ReactNativeConfig reactNativeConfig) { - fabricUIManager.setBinding(this); - installFabricUIManager( - runtimeExecutor, - runtimeScheduler, - fabricUIManager, - eventBeatManager, - componentFactory, - reactNativeConfig); - - setPixelDensity(PixelUtil.getDisplayMetricDensity()); - } - - private native void uninstallFabricUIManager(); - - public void unregister() { - uninstallFabricUIManager(); - } - - public native void registerSurface(SurfaceHandlerBinding surfaceHandler); - - public native void unregisterSurface(SurfaceHandlerBinding surfaceHandler); -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/BindingImpl.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/BindingImpl.kt new file mode 100644 index 00000000000000..f80e983a4d4d24 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/BindingImpl.kt @@ -0,0 +1,113 @@ +/* + * 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. + */ + +package com.facebook.react.fabric + +import android.annotation.SuppressLint +import com.facebook.jni.HybridData +import com.facebook.proguard.annotations.DoNotStrip +import com.facebook.react.bridge.NativeMap +import com.facebook.react.bridge.ReadableNativeMap +import com.facebook.react.bridge.RuntimeExecutor +import com.facebook.react.bridge.RuntimeScheduler +import com.facebook.react.common.mapbuffer.MapBufferSoLoader +import com.facebook.react.fabric.events.EventBeatManager +import com.facebook.react.fabric.events.EventEmitterWrapper +import com.facebook.react.uimanager.PixelUtil.getDisplayMetricDensity + +@DoNotStrip +@SuppressLint("MissingNativeLoadLibrary") +public class BindingImpl : Binding { + @DoNotStrip private val hybridData: HybridData = initHybrid() + + private external fun installFabricUIManager( + runtimeExecutor: RuntimeExecutor, + runtimeScheduler: RuntimeScheduler, + uiManager: FabricUIManager, + eventBeatManager: EventBeatManager, + componentsRegistry: ComponentFactory, + reactNativeConfig: Any + ) + + external override fun startSurface(surfaceId: Int, moduleName: String, initialProps: NativeMap) + + external override fun startSurfaceWithConstraints( + surfaceId: Int, + moduleName: String?, + initialProps: NativeMap?, + minWidth: Float, + maxWidth: Float, + minHeight: Float, + maxHeight: Float, + offsetX: Float, + offsetY: Float, + isRTL: Boolean, + doLeftAndRightSwapInRTL: Boolean + ) + + external override fun stopSurface(surfaceId: Int) + + external override fun setPixelDensity(pointScaleFactor: Float) + + external override fun setConstraints( + surfaceId: Int, + minWidth: Float, + maxWidth: Float, + minHeight: Float, + maxHeight: Float, + offsetX: Float, + offsetY: Float, + isRTL: Boolean, + doLeftAndRightSwapInRTL: Boolean + ) + + external override fun driveCxxAnimations() + + external override fun reportMount(surfaceId: Int) + + external override fun getInspectorDataForInstance( + eventEmitterWrapper: EventEmitterWrapper? + ): ReadableNativeMap? + + override fun register( + runtimeExecutor: RuntimeExecutor, + runtimeScheduler: RuntimeScheduler, + fabricUIManager: FabricUIManager, + eventBeatManager: EventBeatManager, + componentFactory: ComponentFactory, + reactNativeConfig: ReactNativeConfig + ) { + fabricUIManager.setBinding(this) + installFabricUIManager( + runtimeExecutor, + runtimeScheduler, + fabricUIManager, + eventBeatManager, + componentFactory, + reactNativeConfig) + setPixelDensity(getDisplayMetricDensity()) + } + + private external fun uninstallFabricUIManager() + + override fun unregister() { + uninstallFabricUIManager() + } + + external override fun registerSurface(surfaceHandler: SurfaceHandlerBinding?) + + external override fun unregisterSurface(surfaceHandler: SurfaceHandlerBinding?) + + private companion object { + init { + FabricSoLoader.staticInit() + MapBufferSoLoader.staticInit() + } + + private external fun initHybrid(): HybridData + } +}