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
This commit is contained in:
Valentin Shergin
2021-02-15 23:19:55 -08:00
committed by Facebook GitHub Bot
parent 5d500f4dbc
commit 4324ca8122
2 changed files with 61 additions and 18 deletions
@@ -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_;
scheduler->findMountingCoordinator(surfaceId)->setMountingOverrideDelegate(
auto surfaceHandler = SurfaceHandler{moduleName->toStdString(), surfaceId};
surfaceHandler.setProps(initialProps->consume());
surfaceHandler.constraintLayout({}, layoutContext);
scheduler->registerSurface(surfaceHandler);
surfaceHandler.start();
surfaceHandler.getMountingCoordinator()->setMountingOverrideDelegate(
animationDriver_);
{
std::unique_lock<better::shared_mutex> 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->findMountingCoordinator(surfaceId)->setMountingOverrideDelegate(
scheduler->registerSurface(surfaceHandler);
surfaceHandler.start();
surfaceHandler.getMountingCoordinator()->setMountingOverrideDelegate(
animationDriver_);
{
std::unique_lock<better::shared_mutex> 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<better::shared_mutex> 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<better::shared_mutex> 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(
@@ -164,6 +164,10 @@ class Binding : public jni::HybridClass<Binding>,
std::shared_ptr<Scheduler> scheduler_;
std::mutex schedulerMutex_;
better::map<SurfaceId, SurfaceHandler> surfaceHandlerRegistry_{};
better::shared_mutex
surfaceHandlerRegistryMutex_; // Protects `surfaceHandlerRegistry_`.
std::recursive_mutex commitMutex_;
float pointScaleFactor_ = 1;