From 4324ca81226947896230dab8a0d203c835bf67d2 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Mon, 15 Feb 2021 23:17:38 -0800 Subject: [PATCH] Fabric: Using SurfaceHandler API in Binding.cpp on Android Summary: Now we use SurfaceHandler-based APIs to control surfaces in `Binding.cpp` on Android instead of using Scheduler-based APIs. This is a transitional change; eventually, we will need to wrap C++ SurfaceHandler's into JNI wrappers. For now, it will allow to clean up the C++ part. Changelog: [Internal] Fabric-specific internal change. Reviewed By: JoshuaGross Differential Revision: D26375641 fbshipit-source-id: 6f293e79cecf50de72294e90d5243ebb02d71236 --- .../com/facebook/react/fabric/jni/Binding.cpp | 75 ++++++++++++++----- .../com/facebook/react/fabric/jni/Binding.h | 4 + 2 files changed, 61 insertions(+), 18 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.cpp b/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.cpp index 9a05002b0cd193..685c8777864594 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.cpp +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.cpp @@ -251,17 +251,24 @@ void Binding::startSurface( return; } - LayoutContext context; - context.pointScaleFactor = pointScaleFactor_; - scheduler->startSurface( - surfaceId, - moduleName->toStdString(), - initialProps->consume(), - {}, - context); + auto layoutContext = LayoutContext{}; + layoutContext.pointScaleFactor = pointScaleFactor_; + + auto surfaceHandler = SurfaceHandler{moduleName->toStdString(), surfaceId}; + surfaceHandler.setProps(initialProps->consume()); + surfaceHandler.constraintLayout({}, layoutContext); - scheduler->findMountingCoordinator(surfaceId)->setMountingOverrideDelegate( + scheduler->registerSurface(surfaceHandler); + + surfaceHandler.start(); + + surfaceHandler.getMountingCoordinator()->setMountingOverrideDelegate( animationDriver_); + + { + std::unique_lock lock(surfaceHandlerRegistryMutex_); + surfaceHandlerRegistry_.emplace(surfaceId, std::move(surfaceHandler)); + } } void Binding::startSurfaceWithConstraints( @@ -306,15 +313,21 @@ void Binding::startSurfaceWithConstraints( constraints.layoutDirection = isRTL ? LayoutDirection::RightToLeft : LayoutDirection::LeftToRight; - scheduler->startSurface( - surfaceId, - moduleName->toStdString(), - initialProps->consume(), - constraints, - context); + auto surfaceHandler = SurfaceHandler{moduleName->toStdString(), surfaceId}; + surfaceHandler.setProps(initialProps->consume()); + surfaceHandler.constraintLayout(constraints, context); + + scheduler->registerSurface(surfaceHandler); - scheduler->findMountingCoordinator(surfaceId)->setMountingOverrideDelegate( + surfaceHandler.start(); + + surfaceHandler.getMountingCoordinator()->setMountingOverrideDelegate( animationDriver_); + + { + std::unique_lock lock(surfaceHandlerRegistryMutex_); + surfaceHandlerRegistry_.emplace(surfaceId, std::move(surfaceHandler)); + } } void Binding::renderTemplateToSurface(jint surfaceId, jstring uiTemplate) { @@ -346,7 +359,21 @@ void Binding::stopSurface(jint surfaceId) { return; } - scheduler->stopSurface(surfaceId); + { + std::unique_lock lock(surfaceHandlerRegistryMutex_); + + auto iterator = surfaceHandlerRegistry_.find(surfaceId); + + if (iterator == surfaceHandlerRegistry_.end()) { + LOG(ERROR) << "Binding::stopSurface: Surface with given id is not found"; + return; + } + + auto surfaceHandler = std::move(iterator->second); + surfaceHandlerRegistry_.erase(iterator); + surfaceHandler.stop(); + scheduler->unregisterSurface(surfaceHandler); + } } static inline float scale(Float value, Float pointScaleFactor) { @@ -399,7 +426,19 @@ void Binding::setConstraints( constraints.layoutDirection = isRTL ? LayoutDirection::RightToLeft : LayoutDirection::LeftToRight; - scheduler->constraintSurfaceLayout(surfaceId, constraints, context); + { + std::shared_lock lock(surfaceHandlerRegistryMutex_); + + auto iterator = surfaceHandlerRegistry_.find(surfaceId); + + if (iterator == surfaceHandlerRegistry_.end()) { + LOG(ERROR) << "Binding::setConstraints: Surface with given id is not found"; + return; + } + + auto &surfaceHandler = iterator->second; + surfaceHandler.constraintLayout(constraints, context); + } } void Binding::installFabricUIManager( diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.h b/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.h index 23ce763dc1ba01..f0216d0cdeebeb 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.h +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.h @@ -164,6 +164,10 @@ class Binding : public jni::HybridClass, std::shared_ptr scheduler_; std::mutex schedulerMutex_; + better::map surfaceHandlerRegistry_{}; + better::shared_mutex + surfaceHandlerRegistryMutex_; // Protects `surfaceHandlerRegistry_`. + std::recursive_mutex commitMutex_; float pointScaleFactor_ = 1;