Files
react-native/ReactCommon/react/renderer/scheduler/SurfaceManager.cpp
T
Joshua Gross 09b9422516 Pass context through to all prop parser (core changes)
Summary:
Unfortunately, parsing some props requires stateful context - namely, PlatformColor on Android. We explored several different options but they all seemed inferior to the approach of using ContextContainer, and most would require using global state.

By introducing this change everywhere as early as possible, we can avoid later pain. It is likely that some prop, on some platform, will require this mechanism. We'll be ready for it!

Because we can pass a constref of the ContextContainer through to all props and because the context and context data is never retained by prop parsers, perf and memory hit should be ~0.

This diff contains core changes only. Leaf changes to all props structs and conversions files will be in next diff(s).

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D29838789

fbshipit-source-id: f5090e7f02eb6e8fbe0ef4dd201e7d12104a3e3c
2021-07-28 20:18:20 -07:00

105 lines
2.8 KiB
C++

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "SurfaceManager.h"
#include <react/renderer/scheduler/Scheduler.h>
namespace facebook {
namespace react {
SurfaceManager::SurfaceManager(Scheduler const &scheduler) noexcept
: scheduler_(scheduler) {}
void SurfaceManager::startSurface(
SurfaceId surfaceId,
std::string const &moduleName,
folly::dynamic const &props,
LayoutConstraints const &layoutConstraints,
LayoutContext const &layoutContext) const noexcept {
{
std::unique_lock<better::shared_mutex> lock(mutex_);
auto surfaceHandler = SurfaceHandler{moduleName, surfaceId};
surfaceHandler.setContextContainer(scheduler_.getContextContainer());
registry_.emplace(surfaceId, std::move(surfaceHandler));
}
visit(surfaceId, [&](SurfaceHandler const &surfaceHandler) {
surfaceHandler.setProps(props);
surfaceHandler.constraintLayout(layoutConstraints, layoutContext);
scheduler_.registerSurface(surfaceHandler);
surfaceHandler.start();
});
}
void SurfaceManager::stopSurface(SurfaceId surfaceId) const noexcept {
visit(surfaceId, [&](SurfaceHandler const &surfaceHandler) {
surfaceHandler.stop();
scheduler_.unregisterSurface(surfaceHandler);
});
{
std::unique_lock<better::shared_mutex> lock(mutex_);
auto iterator = registry_.find(surfaceId);
registry_.erase(iterator);
}
}
Size SurfaceManager::measureSurface(
SurfaceId surfaceId,
LayoutConstraints const &layoutConstraints,
LayoutContext const &layoutContext) const noexcept {
auto size = Size{};
visit(surfaceId, [&](SurfaceHandler const &surfaceHandler) {
size = surfaceHandler.measure(layoutConstraints, layoutContext);
});
return size;
}
MountingCoordinator::Shared SurfaceManager::findMountingCoordinator(
SurfaceId surfaceId) const noexcept {
auto mountingCoordinator = MountingCoordinator::Shared{};
visit(surfaceId, [&](SurfaceHandler const &surfaceHandler) {
mountingCoordinator = surfaceHandler.getMountingCoordinator();
});
return mountingCoordinator;
}
void SurfaceManager::constraintSurfaceLayout(
SurfaceId surfaceId,
LayoutConstraints const &layoutConstraints,
LayoutContext const &layoutContext) const noexcept {
visit(surfaceId, [=](SurfaceHandler const &surfaceHandler) {
surfaceHandler.constraintLayout(layoutConstraints, layoutContext);
});
}
void SurfaceManager::visit(
SurfaceId surfaceId,
std::function<void(SurfaceHandler const &surfaceHandler)> callback)
const noexcept {
std::shared_lock<better::shared_mutex> lock(mutex_);
auto iterator = registry_.find(surfaceId);
if (iterator == registry_.end()) {
return;
}
callback(iterator->second);
}
} // namespace react
} // namespace facebook