Skip to content

Commit

Permalink
Fabric: Using SurfaceHandler API in Binding.cpp on Android
Browse files Browse the repository at this point in the history
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
  • Loading branch information
shergin authored and facebook-github-bot committed Feb 16, 2021
1 parent 5d500f4 commit 4324ca8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<better::shared_mutex> lock(surfaceHandlerRegistryMutex_);
surfaceHandlerRegistry_.emplace(surfaceId, std::move(surfaceHandler));
}
}

void Binding::startSurfaceWithConstraints(
Expand Down Expand Up @@ -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<better::shared_mutex> lock(surfaceHandlerRegistryMutex_);
surfaceHandlerRegistry_.emplace(surfaceId, std::move(surfaceHandler));
}
}

void Binding::renderTemplateToSurface(jint surfaceId, jstring uiTemplate) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4324ca8

Please sign in to comment.