Skip to content

Commit

Permalink
Move the mutators stack handling to preroll (flutter#9651)
Browse files Browse the repository at this point in the history
* refactoring to move the mutator stack handling to preroll

* more review fixes
  • Loading branch information
Chris Yang authored and iskakaushik committed Jul 2, 2019
1 parent 511b9f2 commit 8306ee6
Show file tree
Hide file tree
Showing 18 changed files with 77 additions and 74 deletions.
8 changes: 4 additions & 4 deletions flow/embedded_views.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,14 @@ class ExternalViewEmbedder {

virtual void BeginFrame(SkISize frame_size) = 0;

virtual void PrerollCompositeEmbeddedView(int view_id) = 0;
virtual void PrerollCompositeEmbeddedView(
int view_id,
std::unique_ptr<EmbeddedViewParams> params) = 0;

virtual std::vector<SkCanvas*> GetCurrentCanvases() = 0;

// Must be called on the UI thread.
virtual SkCanvas* CompositeEmbeddedView(
int view_id,
std::unique_ptr<flutter::EmbeddedViewParams> params) = 0;
virtual SkCanvas* CompositeEmbeddedView(int view_id) = 0;

virtual bool SubmitFrame(GrContext* context);

Expand Down
4 changes: 2 additions & 2 deletions flow/layers/clip_path_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ void ClipPathLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {
SkRect previous_cull_rect = context->cull_rect;
SkRect clip_path_bounds = clip_path_.getBounds();
if (context->cull_rect.intersect(clip_path_bounds)) {
context->mutators_stack.pushClipPath(clip_path_);
SkRect child_paint_bounds = SkRect::MakeEmpty();
PrerollChildren(context, matrix, &child_paint_bounds);

if (child_paint_bounds.intersect(clip_path_bounds)) {
set_paint_bounds(child_paint_bounds);
}
context->mutators_stack.pop();
}
context->cull_rect = previous_cull_rect;
}
Expand Down Expand Up @@ -60,7 +62,6 @@ void ClipPathLayer::Paint(PaintContext& context) const {
SkAutoCanvasRestore save(context.internal_nodes_canvas, true);
context.internal_nodes_canvas->clipPath(clip_path_,
clip_behavior_ != Clip::hardEdge);
context.mutators_stack.pushClipPath(clip_path_);

if (clip_behavior_ == Clip::antiAliasWithSaveLayer) {
context.internal_nodes_canvas->saveLayer(paint_bounds(), nullptr);
Expand All @@ -69,7 +70,6 @@ void ClipPathLayer::Paint(PaintContext& context) const {
if (clip_behavior_ == Clip::antiAliasWithSaveLayer) {
context.internal_nodes_canvas->restore();
}
context.mutators_stack.pop();
}

} // namespace flutter
4 changes: 2 additions & 2 deletions flow/layers/clip_rect_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ ClipRectLayer::~ClipRectLayer() = default;
void ClipRectLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {
SkRect previous_cull_rect = context->cull_rect;
if (context->cull_rect.intersect(clip_rect_)) {
context->mutators_stack.pushClipRect(clip_rect_);
SkRect child_paint_bounds = SkRect::MakeEmpty();
PrerollChildren(context, matrix, &child_paint_bounds);

if (child_paint_bounds.intersect(clip_rect_)) {
set_paint_bounds(child_paint_bounds);
}
context->mutators_stack.pop();
}
context->cull_rect = previous_cull_rect;
}
Expand Down Expand Up @@ -50,7 +52,6 @@ void ClipRectLayer::Paint(PaintContext& context) const {
SkAutoCanvasRestore save(context.internal_nodes_canvas, true);
context.internal_nodes_canvas->clipRect(clip_rect_,
clip_behavior_ != Clip::hardEdge);
context.mutators_stack.pushClipRect(clip_rect_);

if (clip_behavior_ == Clip::antiAliasWithSaveLayer) {
context.internal_nodes_canvas->saveLayer(clip_rect_, nullptr);
Expand All @@ -59,7 +60,6 @@ void ClipRectLayer::Paint(PaintContext& context) const {
if (clip_behavior_ == Clip::antiAliasWithSaveLayer) {
context.internal_nodes_canvas->restore();
}
context.mutators_stack.pop();
}

} // namespace flutter
4 changes: 2 additions & 2 deletions flow/layers/clip_rrect_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ void ClipRRectLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {
SkRect previous_cull_rect = context->cull_rect;
SkRect clip_rrect_bounds = clip_rrect_.getBounds();
if (context->cull_rect.intersect(clip_rrect_bounds)) {
context->mutators_stack.pushClipRRect(clip_rrect_);
SkRect child_paint_bounds = SkRect::MakeEmpty();
PrerollChildren(context, matrix, &child_paint_bounds);

if (child_paint_bounds.intersect(clip_rrect_bounds)) {
set_paint_bounds(child_paint_bounds);
}
context->mutators_stack.pop();
}
context->cull_rect = previous_cull_rect;
}
Expand Down Expand Up @@ -58,7 +60,6 @@ void ClipRRectLayer::Paint(PaintContext& context) const {
SkAutoCanvasRestore save(context.internal_nodes_canvas, true);
context.internal_nodes_canvas->clipRRect(clip_rrect_,
clip_behavior_ != Clip::hardEdge);
context.mutators_stack.pushClipRRect(clip_rrect_);

if (clip_behavior_ == Clip::antiAliasWithSaveLayer) {
context.internal_nodes_canvas->saveLayer(paint_bounds(), nullptr);
Expand All @@ -67,7 +68,6 @@ void ClipRRectLayer::Paint(PaintContext& context) const {
if (clip_behavior_ == Clip::antiAliasWithSaveLayer) {
context.internal_nodes_canvas->restore();
}
context.mutators_stack.pop();
}

} // namespace flutter
2 changes: 1 addition & 1 deletion flow/layers/layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ struct PrerollContext {
RasterCache* raster_cache;
GrContext* gr_context;
ExternalViewEmbedder* view_embedder;
MutatorsStack& mutators_stack;
SkColorSpace* dst_color_space;
SkRect cull_rect;

Expand Down Expand Up @@ -83,7 +84,6 @@ class Layer {
SkCanvas* leaf_nodes_canvas;
GrContext* gr_context;
ExternalViewEmbedder* view_embedder;
MutatorsStack& mutators_stack;
const Stopwatch& raster_time;
const Stopwatch& ui_time;
TextureRegistry& texture_registry;
Expand Down
6 changes: 3 additions & 3 deletions flow/layers/layer_tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ void LayerTree::Preroll(CompositorContext::ScopedFrame& frame,
frame.canvas() ? frame.canvas()->imageInfo().colorSpace() : nullptr;
frame.context().raster_cache().SetCheckboardCacheImages(
checkerboard_raster_cache_images_);
MutatorsStack stack;
PrerollContext context = {
ignore_raster_cache ? nullptr : &frame.context().raster_cache(),
frame.gr_context(),
frame.view_embedder(),
stack,
color_space,
kGiantRect,
frame.context().raster_time(),
Expand Down Expand Up @@ -83,13 +85,11 @@ void LayerTree::Paint(CompositorContext::ScopedFrame& frame,
}
}

MutatorsStack stack;
Layer::PaintContext context = {
(SkCanvas*)&internal_nodes_canvas,
frame.canvas(),
frame.gr_context(),
frame.view_embedder(),
stack,
frame.context().raster_time(),
frame.context().ui_time(),
frame.context().texture_registry(),
Expand Down Expand Up @@ -121,6 +121,7 @@ sk_sp<SkPicture> LayerTree::Flatten(const SkRect& bounds) {
nullptr, // raster_cache (don't consult the cache)
nullptr, // gr_context (used for the raster cache)
nullptr, // external view embedder
unused_stack, // mutator stack
nullptr, // SkColorSpace* dst_color_space
kGiantRect, // SkRect cull_rect
unused_stopwatch, // frame time (dont care)
Expand All @@ -138,7 +139,6 @@ sk_sp<SkPicture> LayerTree::Flatten(const SkRect& bounds) {
canvas, // canvas
nullptr,
nullptr,
unused_stack,
unused_stopwatch, // frame time (dont care)
unused_stopwatch, // engine time (dont care)
unused_texture_registry, // texture registry (not supported)
Expand Down
8 changes: 2 additions & 6 deletions flow/layers/performance_overlay_layer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,9 @@ TEST(PerformanceOverlayLayer, Gold) {
ASSERT_TRUE(surface != nullptr);

flutter::TextureRegistry unused_texture_registry;
flutter::MutatorsStack unused_stack;
flutter::Layer::PaintContext paintContext = {
nullptr, surface->getCanvas(),
nullptr, nullptr,
unused_stack, mock_stopwatch,
mock_stopwatch, unused_texture_registry,
nullptr, false};
nullptr, surface->getCanvas(), nullptr, nullptr, mock_stopwatch,
mock_stopwatch, unused_texture_registry, nullptr, false};

// Specify font file to ensure the same font across different operation
// systems.
Expand Down
2 changes: 2 additions & 0 deletions flow/layers/physical_shape_layer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ TEST(PhysicalShapeLayer, TotalElevation) {

const Stopwatch unused_stopwatch;
TextureRegistry unused_texture_registry;
MutatorsStack unused_stack;
PrerollContext preroll_context{
nullptr, // raster_cache (don't consult the cache)
nullptr, // gr_context (used for the raster cache)
nullptr, // external view embedder
unused_stack, // mutator stack
nullptr, // SkColorSpace* dst_color_space
kGiantRect, // SkRect cull_rect
unused_stopwatch, // frame time (dont care)
Expand Down
20 changes: 9 additions & 11 deletions flow/layers/platform_view_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ void PlatformViewLayer::Preroll(PrerollContext* context,
"does not support embedding";
return;
}
context->view_embedder->PrerollCompositeEmbeddedView(view_id_);
std::unique_ptr<EmbeddedViewParams> params =
std::make_unique<EmbeddedViewParams>();
params->offsetPixels =
SkPoint::Make(matrix.getTranslateX(), matrix.getTranslateY());
params->sizePoints = size_;
params->mutatorsStack = context->mutators_stack;
context->view_embedder->PrerollCompositeEmbeddedView(view_id_,
std::move(params));
}

void PlatformViewLayer::Paint(PaintContext& context) const {
Expand All @@ -32,16 +39,7 @@ void PlatformViewLayer::Paint(PaintContext& context) const {
"does not support embedding";
return;
}
std::unique_ptr<EmbeddedViewParams> params =
std::make_unique<EmbeddedViewParams>();
SkMatrix transform = context.leaf_nodes_canvas->getTotalMatrix();
params->offsetPixels =
SkPoint::Make(transform.getTranslateX(), transform.getTranslateY());
params->sizePoints = size_;
params->mutatorsStack = context.mutators_stack;

SkCanvas* canvas =
context.view_embedder->CompositeEmbeddedView(view_id_, std::move(params));
SkCanvas* canvas = context.view_embedder->CompositeEmbeddedView(view_id_);
context.leaf_nodes_canvas = canvas;
}
} // namespace flutter
5 changes: 2 additions & 3 deletions flow/layers/transform_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ TransformLayer::~TransformLayer() = default;
void TransformLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {
SkMatrix child_matrix;
child_matrix.setConcat(matrix, transform_);

context->mutators_stack.pushTransform(transform_);
SkRect previous_cull_rect = context->cull_rect;
SkMatrix inverse_transform_;
// Perspective projections don't produce rectangles that are useful for
Expand All @@ -47,6 +47,7 @@ void TransformLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {
set_paint_bounds(child_paint_bounds);

context->cull_rect = previous_cull_rect;
context->mutators_stack.pop();
}

#if defined(OS_FUCHSIA)
Expand All @@ -66,10 +67,8 @@ void TransformLayer::Paint(PaintContext& context) const {

SkAutoCanvasRestore save(context.internal_nodes_canvas, true);
context.internal_nodes_canvas->concat(transform_);
context.mutators_stack.pushTransform(transform_);

PaintChildren(context);
context.mutators_stack.pop();
}

} // namespace flutter
2 changes: 0 additions & 2 deletions flow/raster_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ void RasterCache::Prepare(PrerollContext* context,
entry.image = Rasterize(context->gr_context, ctm, context->dst_color_space,
checkerboard_images_, layer->paint_bounds(),
[layer, context](SkCanvas* canvas) {
MutatorsStack stack;
SkISize canvas_size = canvas->getBaseLayerSize();
SkNWayCanvas internal_nodes_canvas(
canvas_size.width(), canvas_size.height());
Expand All @@ -169,7 +168,6 @@ void RasterCache::Prepare(PrerollContext* context,
canvas,
context->gr_context,
nullptr,
stack,
context->raster_time,
context->ui_time,
context->texture_registry,
Expand Down
2 changes: 0 additions & 2 deletions flow/scene_update_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,10 @@ SceneUpdateContext::ExecutePaintTasks(CompositorContext::ScopedFrame& frame) {
for (auto& task : paint_tasks_) {
FML_DCHECK(task.surface);
SkCanvas* canvas = task.surface->GetSkiaSurface()->getCanvas();
flutter::MutatorsStack stack;
Layer::PaintContext context = {canvas,
canvas,
frame.gr_context(),
nullptr,
stack,
frame.context().raster_time(),
frame.context().ui_time(),
frame.context().texture_registry(),
Expand Down
39 changes: 23 additions & 16 deletions shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,21 @@
frame_size_ = frame_size;
}

void FlutterPlatformViewsController::PrerollCompositeEmbeddedView(int view_id) {
void FlutterPlatformViewsController::PrerollCompositeEmbeddedView(
int view_id,
std::unique_ptr<EmbeddedViewParams> params) {
picture_recorders_[view_id] = std::make_unique<SkPictureRecorder>();
picture_recorders_[view_id]->beginRecording(SkRect::Make(frame_size_));
picture_recorders_[view_id]->getRecordingCanvas()->clear(SK_ColorTRANSPARENT);
composition_order_.push_back(view_id);

if (current_composition_params_.count(view_id) == 1 &&
current_composition_params_[view_id] == *params.get()) {
// Do nothing if the params didn't change.
return;
}
current_composition_params_[view_id] = EmbeddedViewParams(*params.get());
views_to_recomposite_.insert(view_id);
}

NSObject<FlutterPlatformView>* FlutterPlatformViewsController::GetPlatformViewByID(int view_id) {
Expand Down Expand Up @@ -272,15 +282,14 @@
head.layer.transform, CATransform3DMakeScale(1 / screenScale, 1 / screenScale, 1));
}

void FlutterPlatformViewsController::CompositeWithParams(
int view_id,
std::unique_ptr<flutter::EmbeddedViewParams> params) {
CGRect frame = CGRectMake(0, 0, params->sizePoints.width(), params->sizePoints.height());
void FlutterPlatformViewsController::CompositeWithParams(int view_id,
const EmbeddedViewParams& params) {
CGRect frame = CGRectMake(0, 0, params.sizePoints.width(), params.sizePoints.height());
UIView* touchInterceptor = touch_interceptors_[view_id].get();
touchInterceptor.layer.transform = CATransform3DIdentity;
touchInterceptor.frame = frame;

int currentClippingCount = CountClips(params->mutatorsStack);
int currentClippingCount = CountClips(params.mutatorsStack);
int previousClippingCount = clip_count_[view_id];
if (currentClippingCount != previousClippingCount) {
clip_count_[view_id] = currentClippingCount;
Expand All @@ -291,23 +300,19 @@
ReconstructClipViewsChain(currentClippingCount, touchInterceptor, oldPlatformViewRoot);
root_views_[view_id] = fml::scoped_nsobject<UIView>([newPlatformViewRoot retain]);
}
ApplyMutators(params->mutatorsStack, touchInterceptor);
ApplyMutators(params.mutatorsStack, touchInterceptor);
}

SkCanvas* FlutterPlatformViewsController::CompositeEmbeddedView(
int view_id,
std::unique_ptr<flutter::EmbeddedViewParams> params) {
SkCanvas* FlutterPlatformViewsController::CompositeEmbeddedView(int view_id) {
// TODO(amirh): assert that this is running on the platform thread once we support the iOS
// embedded views thread configuration.

// Do nothing if the params didn't change.
if (current_composition_params_.count(view_id) == 1 &&
current_composition_params_[view_id] == *params.get()) {
// Do nothing if the view doesn't need to be composited.
if (views_to_recomposite_.count(view_id) == 0) {
return picture_recorders_[view_id]->getRecordingCanvas();
}
current_composition_params_[view_id] = EmbeddedViewParams(*params.get());
CompositeWithParams(view_id, std::move(params));

CompositeWithParams(view_id, current_composition_params_[view_id]);
views_to_recomposite_.erase(view_id);
return picture_recorders_[view_id]->getRecordingCanvas();
}

Expand All @@ -323,6 +328,7 @@
picture_recorders_.clear();
current_composition_params_.clear();
clip_count_.clear();
views_to_recomposite_.clear();
}

bool FlutterPlatformViewsController::SubmitFrame(bool gl_rendering,
Expand Down Expand Up @@ -414,6 +420,7 @@
overlays_.erase(viewId);
current_composition_params_.erase(viewId);
clip_count_.erase(viewId);
views_to_recomposite_.erase(viewId);
}
views_to_dispose_.clear();
}
Expand Down
Loading

0 comments on commit 8306ee6

Please sign in to comment.