From f7b67119d31f214ca1f101b30bfed031e69de9dc Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Tue, 18 Jul 2017 15:40:18 -0700 Subject: [PATCH] Update the content handler to use the Mozart session API. (flutter/engine#3887) --- engine/src/flutter/common/threads.h | 9 + engine/src/flutter/flow/BUILD.gn | 15 +- engine/src/flutter/flow/export_node.cc | 39 +++ engine/src/flutter/flow/export_node.h | 54 +++ .../flow/layers/backdrop_filter_layer.cc | 11 +- .../flow/layers/backdrop_filter_layer.h | 1 - .../flutter/flow/layers/child_scene_layer.cc | 54 +-- .../flutter/flow/layers/child_scene_layer.h | 14 +- .../flutter/flow/layers/clip_path_layer.cc | 39 ++- .../src/flutter/flow/layers/clip_path_layer.h | 4 +- .../flutter/flow/layers/clip_rect_layer.cc | 36 +- .../src/flutter/flow/layers/clip_rect_layer.h | 4 +- .../flutter/flow/layers/clip_rrect_layer.cc | 43 ++- .../flutter/flow/layers/clip_rrect_layer.h | 4 +- .../flutter/flow/layers/color_filter_layer.cc | 6 +- .../flutter/flow/layers/color_filter_layer.h | 1 - .../flutter/flow/layers/container_layer.cc | 64 ++-- .../src/flutter/flow/layers/container_layer.h | 23 +- engine/src/flutter/flow/layers/layer.cc | 19 +- engine/src/flutter/flow/layers/layer.h | 28 +- engine/src/flutter/flow/layers/layer_tree.cc | 23 +- engine/src/flutter/flow/layers/layer_tree.h | 13 +- .../src/flutter/flow/layers/opacity_layer.cc | 26 +- .../src/flutter/flow/layers/opacity_layer.h | 7 +- .../flow/layers/physical_model_layer.cc | 95 +++--- .../flow/layers/physical_model_layer.h | 20 +- .../src/flutter/flow/layers/picture_layer.cc | 7 +- .../flutter/flow/layers/shader_mask_layer.cc | 6 +- .../flutter/flow/layers/shader_mask_layer.h | 5 +- .../flutter/flow/layers/transform_layer.cc | 34 +- .../src/flutter/flow/layers/transform_layer.h | 4 +- .../src/flutter/flow/scene_update_context.cc | 320 +++++++++++++----- .../src/flutter/flow/scene_update_context.h | 176 +++++++--- engine/src/flutter/lib/ui/BUILD.gn | 2 + engine/src/flutter/lib/ui/compositing.dart | 31 +- .../lib/ui/compositing/scene_builder.cc | 9 +- .../lib/ui/compositing/scene_builder.h | 3 +- .../flutter/lib/ui/compositing/scene_host.cc | 44 +++ .../flutter/lib/ui/compositing/scene_host.h | 52 +++ engine/src/flutter/lib/ui/dart_ui.cc | 1 + engine/src/flutter/vulkan/BUILD.gn | 5 +- .../src/flutter/vulkan/vulkan_proc_table.cc | 25 +- engine/src/flutter/vulkan/vulkan_proc_table.h | 10 +- 43 files changed, 912 insertions(+), 474 deletions(-) create mode 100644 engine/src/flutter/flow/export_node.cc create mode 100644 engine/src/flutter/flow/export_node.h create mode 100644 engine/src/flutter/lib/ui/compositing/scene_host.cc create mode 100644 engine/src/flutter/lib/ui/compositing/scene_host.h diff --git a/engine/src/flutter/common/threads.h b/engine/src/flutter/common/threads.h index 7846c2a3e7..f9058b17ca 100644 --- a/engine/src/flutter/common/threads.h +++ b/engine/src/flutter/common/threads.h @@ -7,6 +7,15 @@ #include "lib/ftl/tasks/task_runner.h" +#define ASSERT_IS_PLATFORM_THREAD \ + FTL_DCHECK(::blink::Threads::Platform()->RunsTasksOnCurrentThread()); +#define ASSERT_IS_GPU_THREAD \ + FTL_DCHECK(::blink::Threads::Gpu()->RunsTasksOnCurrentThread()); +#define ASSERT_IS_UI_THREAD \ + FTL_DCHECK(::blink::Threads::UI()->RunsTasksOnCurrentThread()); +#define ASSERT_IS_IO_THREAD \ + FTL_DCHECK(::blink::Threads::IO()->RunsTasksOnCurrentThread()); + namespace blink { class Threads { diff --git a/engine/src/flutter/flow/BUILD.gn b/engine/src/flutter/flow/BUILD.gn index 112ec6389c..e278ed10e0 100644 --- a/engine/src/flutter/flow/BUILD.gn +++ b/engine/src/flutter/flow/BUILD.gn @@ -47,8 +47,6 @@ source_set("flow") { "raster_cache.h", "raster_cache_key.cc", "raster_cache_key.h", - "scene_update_context.cc", - "scene_update_context.h", ] deps = [ @@ -62,12 +60,21 @@ source_set("flow") { if (is_fuchsia) { sources += [ + "export_node.cc", + "export_node.h", "layers/child_scene_layer.cc", "layers/child_scene_layer.h", + "scene_update_context.cc", + "scene_update_context.h", ] + + deps += [ + "//apps/mozart/lib/scene:client", + "//apps/mozart/services/scene", + ] + public_deps = [ - "//apps/mozart/lib/skia", - "//apps/mozart/services/composition", + "//magenta/system/ulib/mx", ] } } diff --git a/engine/src/flutter/flow/export_node.cc b/engine/src/flutter/flow/export_node.cc new file mode 100644 index 0000000000..be4a1cea1d --- /dev/null +++ b/engine/src/flutter/flow/export_node.cc @@ -0,0 +1,39 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/flow/export_node.h" + +namespace flow { + +ExportNode::ExportNode(uint32_t export_token_handle) + : export_token_(export_token_handle) {} + +ExportNode::~ExportNode() { + // TODO(MZ-190): Ensure the node is properly unregistered on the rasterizer + // thread by attaching it to the update context in some way. +} + +void ExportNode::Bind(SceneUpdateContext& context, + mozart::client::ContainerNode& container, + const SkPoint& offset, + float scale, + bool hit_testable) { + ftl::MutexLocker lock(&mutex_); + + if (export_token_) { + node_.reset(new mozart::client::EntityNode(container.session())); + node_->Export(std::move(export_token_)); + } + + if (node_) { + container.AddChild(*node_); + node_->SetTranslation(offset.x(), offset.y(), 0.f); + node_->SetScale(scale, scale, 1.f); + node_->SetHitTestBehavior(hit_testable + ? mozart2::HitTestBehavior::kDefault + : mozart2::HitTestBehavior::kSuppress); + } +} + +} // namespace flow diff --git a/engine/src/flutter/flow/export_node.h b/engine/src/flutter/flow/export_node.h new file mode 100644 index 0000000000..da46794188 --- /dev/null +++ b/engine/src/flutter/flow/export_node.h @@ -0,0 +1,54 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_FLOW_EXPORT_NODE_H_ +#define FLUTTER_FLOW_EXPORT_NODE_H_ + +#include + +#include + +#include "apps/mozart/lib/scene/client/resources.h" +#include "flutter/flow/scene_update_context.h" +#include "lib/ftl/build_config.h" +#include "lib/ftl/macros.h" +#include "lib/ftl/memory/ref_counted.h" +#include "lib/ftl/synchronization/mutex.h" +#include "lib/ftl/synchronization/thread_annotations.h" +#include "third_party/skia/include/core/SkPoint.h" + +namespace flow { + +// Represents a node which is being exported from the session. +// This object is created on the UI thread but the entity node it contains +// must be created and destroyed by the rasterizer thread. +// +// Therefore this object is thread-safe. +class ExportNode : public ftl::RefCountedThreadSafe { + public: + ExportNode(uint32_t export_token_handle); + + // Binds the export token to the entity node and adds it as a child of + // the specified container. + void Bind(SceneUpdateContext& context, + mozart::client::ContainerNode& container, + const SkPoint& offset, + float scale, + bool hit_testable); + + private: + ~ExportNode(); + + ftl::Mutex mutex_; + mx::eventpair export_token_ FTL_GUARDED_BY(mutex_); + std::unique_ptr node_ FTL_GUARDED_BY(mutex_); + + FRIEND_MAKE_REF_COUNTED(ExportNode); + FRIEND_REF_COUNTED_THREAD_SAFE(ExportNode); + FTL_DISALLOW_COPY_AND_ASSIGN(ExportNode); +}; + +} // namespace flow + +#endif // FLUTTER_FLOW_EXPORT_NODE_H_ diff --git a/engine/src/flutter/flow/layers/backdrop_filter_layer.cc b/engine/src/flutter/flow/layers/backdrop_filter_layer.cc index 7b16fc3466..0077f6f87e 100644 --- a/engine/src/flutter/flow/layers/backdrop_filter_layer.cc +++ b/engine/src/flutter/flow/layers/backdrop_filter_layer.cc @@ -8,15 +8,16 @@ namespace flow { -BackdropFilterLayer::BackdropFilterLayer() {} +BackdropFilterLayer::BackdropFilterLayer() = default; -BackdropFilterLayer::~BackdropFilterLayer() {} +BackdropFilterLayer::~BackdropFilterLayer() = default; void BackdropFilterLayer::Paint(PaintContext& context) { TRACE_EVENT0("flutter", "BackdropFilterLayer::Paint"); - Layer::AutoSaveLayer( - context, - SkCanvas::SaveLayerRec{&paint_bounds(), nullptr, filter_.get(), 0}); + FTL_DCHECK(needs_painting()); + + Layer::AutoSaveLayer(context, SkCanvas::SaveLayerRec{&paint_bounds(), nullptr, + filter_.get(), 0}); PaintChildren(context); } diff --git a/engine/src/flutter/flow/layers/backdrop_filter_layer.h b/engine/src/flutter/flow/layers/backdrop_filter_layer.h index e38891234f..6e016e7801 100644 --- a/engine/src/flutter/flow/layers/backdrop_filter_layer.h +++ b/engine/src/flutter/flow/layers/backdrop_filter_layer.h @@ -16,7 +16,6 @@ class BackdropFilterLayer : public ContainerLayer { void set_filter(sk_sp filter) { filter_ = std::move(filter); } - protected: void Paint(PaintContext& context) override; private: diff --git a/engine/src/flutter/flow/layers/child_scene_layer.cc b/engine/src/flutter/flow/layers/child_scene_layer.cc index 2af48ea070..b5f3d5dc12 100644 --- a/engine/src/flutter/flow/layers/child_scene_layer.cc +++ b/engine/src/flutter/flow/layers/child_scene_layer.cc @@ -4,9 +4,6 @@ #include "flutter/flow/layers/child_scene_layer.h" -#include "apps/mozart/lib/skia/type_converters.h" -#include "apps/mozart/services/composition/nodes.fidl.h" - namespace flow { ChildSceneLayer::ChildSceneLayer() = default; @@ -15,49 +12,30 @@ ChildSceneLayer::~ChildSceneLayer() = default; void ChildSceneLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) { set_needs_system_composite(true); - transform_.setIdentity(); - transform_.preTranslate(offset_.x(), offset_.y()); - float inverse_device_pixel_ratio = 1.f / device_pixel_ratio_; - transform_.preScale(inverse_device_pixel_ratio, inverse_device_pixel_ratio); - - SkRect bounds = - SkRect::MakeXYWH(offset_.x(), offset_.y(), - physical_size_.width() * inverse_device_pixel_ratio, - physical_size_.height() * inverse_device_pixel_ratio); - set_paint_bounds(bounds); - context->child_paint_bounds = bounds; } void ChildSceneLayer::Paint(PaintContext& context) { - FTL_DCHECK(false) << "Failed to composite child scene."; + FTL_NOTREACHED() << "This layer never needs painting."; } -void ChildSceneLayer::UpdateScene(SceneUpdateContext& context, - mozart::Node* container) { +void ChildSceneLayer::UpdateScene(SceneUpdateContext& context) { FTL_DCHECK(needs_system_composite()); - auto resource = mozart::Resource::New(); - resource->set_scene(mozart::SceneResource::New()); - resource->get_scene()->scene_token = mozart::SceneToken::New(); - resource->get_scene()->scene_token->value = scene_token_; - - auto node = mozart::Node::New(); - if (!hit_testable_) { - node->hit_test_behavior = mozart::HitTestBehavior::New(); - node->hit_test_behavior->visibility = - mozart::HitTestBehavior::Visibility::INVISIBLE; - node->hit_test_behavior->prune = true; + // TODO(MZ-191): Set clip. + // It's worth asking whether all children should be clipped implicitly + // or whether we should leave this up to the Flutter application to decide. + // In some situations, it might be useful to allow children to draw + // outside of their layout bounds. + // + // float inverse_device_pixel_ratio = 1.f / device_pixel_ratio_; + // SkRect bounds = + // SkRect::MakeXYWH(offset_.x(), offset_.y(), + // physical_size_.width() * inverse_device_pixel_ratio, + // physical_size_.height() * inverse_device_pixel_ratio); + if (export_node_) { + context.AddChildScene(export_node_.get(), offset_, device_pixel_ratio_, + hit_testable_); } - node->op = mozart::NodeOp::New(); - node->op->set_scene(mozart::SceneNodeOp::New()); - node->op->get_scene()->scene_resource_id = - context.AddResource(std::move(resource)); - node->content_clip = mozart::RectF::New(); - node->content_clip->width = physical_size_.width(); - node->content_clip->height = physical_size_.height(); - node->content_transform = mozart::Transform::From(transform_); - - context.AddChildNode(container, std::move(node)); } } // namespace flow diff --git a/engine/src/flutter/flow/layers/child_scene_layer.h b/engine/src/flutter/flow/layers/child_scene_layer.h index 93640ec18a..9d66ad5b37 100644 --- a/engine/src/flutter/flow/layers/child_scene_layer.h +++ b/engine/src/flutter/flow/layers/child_scene_layer.h @@ -5,8 +5,8 @@ #ifndef FLUTTER_FLOW_LAYERS_CHILD_SCENE_LAYER_H_ #define FLUTTER_FLOW_LAYERS_CHILD_SCENE_LAYER_H_ +#include "flutter/flow/export_node.h" #include "flutter/flow/layers/layer.h" -#include "apps/mozart/services/composition/scenes.fidl.h" namespace flow { @@ -25,22 +25,24 @@ class ChildSceneLayer : public Layer { physical_size_ = physical_size; } - void set_scene_token(uint32_t scene_token) { scene_token_ = scene_token; } + void set_export_node(ftl::RefPtr export_node) { + export_node_ = std::move(export_node); + } void set_hit_testable(bool hit_testable) { hit_testable_ = hit_testable; } void Preroll(PrerollContext* context, const SkMatrix& matrix) override; + void Paint(PaintContext& context) override; - void UpdateScene(SceneUpdateContext& context, - mozart::Node* container) override; + + void UpdateScene(SceneUpdateContext& context) override; private: SkPoint offset_; float device_pixel_ratio_ = 1.0; SkISize physical_size_; - uint32_t scene_token_ = 0; + ftl::RefPtr export_node_; bool hit_testable_ = true; - SkMatrix transform_; FTL_DISALLOW_COPY_AND_ASSIGN(ChildSceneLayer); }; diff --git a/engine/src/flutter/flow/layers/clip_path_layer.cc b/engine/src/flutter/flow/layers/clip_path_layer.cc index 05a1c89290..39f0e17885 100644 --- a/engine/src/flutter/flow/layers/clip_path_layer.cc +++ b/engine/src/flutter/flow/layers/clip_path_layer.cc @@ -5,37 +5,48 @@ #include "flutter/flow/layers/clip_path_layer.h" #if defined(OS_FUCHSIA) -#include "apps/mozart/lib/skia/type_converters.h" // nogncheck -#include "apps/mozart/services/composition/nodes.fidl.h" // nogncheck + +#include "apps/mozart/lib/scene/session_helpers.h" // nogncheck + #endif // defined(OS_FUCHSIA) namespace flow { -ClipPathLayer::ClipPathLayer() {} +ClipPathLayer::ClipPathLayer() = default; -ClipPathLayer::~ClipPathLayer() {} +ClipPathLayer::~ClipPathLayer() = default; void ClipPathLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) { - PrerollChildren(context, matrix); - if (!context->child_paint_bounds.intersect(clip_path_.getBounds())) - context->child_paint_bounds.setEmpty(); - set_paint_bounds(context->child_paint_bounds); + SkRect child_paint_bounds = SkRect::MakeEmpty(); + PrerollChildren(context, matrix, &child_paint_bounds); + + if (child_paint_bounds.intersect(clip_path_.getBounds())) { + set_paint_bounds(child_paint_bounds); + } } #if defined(OS_FUCHSIA) -void ClipPathLayer::UpdateScene(SceneUpdateContext& context, - mozart::Node* container) { - auto node = mozart::Node::New(); - node->content_clip = mozart::RectF::From(clip_path_.getBounds()); - UpdateSceneChildrenInsideNode(context, container, std::move(node)); +void ClipPathLayer::UpdateScene(SceneUpdateContext& context) { + FTL_DCHECK(needs_system_composite()); + + // TODO(MZ-140): Must be able to specify paths as shapes to nodes. + // Treating the shape as a rectangle for now. + auto bounds = clip_path_.getBounds(); + mozart::client::Rectangle shape(context.session(), // session + bounds.width(), // width + bounds.height() // height + ); + + SceneUpdateContext::Clip clip(context, shape, bounds); + UpdateSceneChildren(context); } #endif // defined(OS_FUCHSIA) void ClipPathLayer::Paint(PaintContext& context) { TRACE_EVENT0("flutter", "ClipPathLayer::Paint"); - FTL_DCHECK(!needs_system_composite()); + FTL_DCHECK(needs_painting()); Layer::AutoSaveLayer save(context, paint_bounds(), nullptr); context.canvas.clipPath(clip_path_, true); diff --git a/engine/src/flutter/flow/layers/clip_path_layer.h b/engine/src/flutter/flow/layers/clip_path_layer.h index a62d66aa42..1b2354a342 100644 --- a/engine/src/flutter/flow/layers/clip_path_layer.h +++ b/engine/src/flutter/flow/layers/clip_path_layer.h @@ -16,13 +16,11 @@ class ClipPathLayer : public ContainerLayer { void set_clip_path(const SkPath& clip_path) { clip_path_ = clip_path; } - protected: void Preroll(PrerollContext* context, const SkMatrix& matrix) override; void Paint(PaintContext& context) override; #if defined(OS_FUCHSIA) - void UpdateScene(SceneUpdateContext& context, - mozart::Node* container) override; + void UpdateScene(SceneUpdateContext& context) override; #endif // defined(OS_FUCHSIA) private: diff --git a/engine/src/flutter/flow/layers/clip_rect_layer.cc b/engine/src/flutter/flow/layers/clip_rect_layer.cc index 1c6d6748a9..e8a08253bb 100644 --- a/engine/src/flutter/flow/layers/clip_rect_layer.cc +++ b/engine/src/flutter/flow/layers/clip_rect_layer.cc @@ -4,38 +4,40 @@ #include "flutter/flow/layers/clip_rect_layer.h" -#if defined(OS_FUCHSIA) -#include "apps/mozart/lib/skia/type_converters.h" // nogncheck -#include "apps/mozart/services/composition/nodes.fidl.h" // nogncheck -#endif // defined(OS_FUCHSIA) - namespace flow { -ClipRectLayer::ClipRectLayer() {} +ClipRectLayer::ClipRectLayer() = default; -ClipRectLayer::~ClipRectLayer() {} +ClipRectLayer::~ClipRectLayer() = default; void ClipRectLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) { - PrerollChildren(context, matrix); - if (!context->child_paint_bounds.intersect(clip_rect_)) - context->child_paint_bounds.setEmpty(); - set_paint_bounds(context->child_paint_bounds); + 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); + } } #if defined(OS_FUCHSIA) -void ClipRectLayer::UpdateScene(SceneUpdateContext& context, - mozart::Node* container) { - auto node = mozart::Node::New(); - node->content_clip = mozart::RectF::From(clip_rect_); - UpdateSceneChildrenInsideNode(context, container, std::move(node)); +void ClipRectLayer::UpdateScene(SceneUpdateContext& context) { + FTL_DCHECK(needs_system_composite()); + + mozart::client::Rectangle shape(context.session(), // session + clip_rect_.width(), // width + clip_rect_.height() // height + ); + + SceneUpdateContext::Clip clip(context, shape, clip_rect_); + UpdateSceneChildren(context); } #endif // defined(OS_FUCHSIA) void ClipRectLayer::Paint(PaintContext& context) { TRACE_EVENT0("flutter", "ClipRectLayer::Paint"); - FTL_DCHECK(!needs_system_composite()); + FTL_DCHECK(needs_painting()); SkAutoCanvasRestore save(&context.canvas, true); context.canvas.clipRect(paint_bounds()); diff --git a/engine/src/flutter/flow/layers/clip_rect_layer.h b/engine/src/flutter/flow/layers/clip_rect_layer.h index 5b09e00636..51416f20fa 100644 --- a/engine/src/flutter/flow/layers/clip_rect_layer.h +++ b/engine/src/flutter/flow/layers/clip_rect_layer.h @@ -16,13 +16,11 @@ class ClipRectLayer : public ContainerLayer { void set_clip_rect(const SkRect& clip_rect) { clip_rect_ = clip_rect; } - protected: void Preroll(PrerollContext* context, const SkMatrix& matrix) override; void Paint(PaintContext& context) override; #if defined(OS_FUCHSIA) - void UpdateScene(SceneUpdateContext& context, - mozart::Node* container) override; + void UpdateScene(SceneUpdateContext& context) override; #endif // defined(OS_FUCHSIA) private: diff --git a/engine/src/flutter/flow/layers/clip_rrect_layer.cc b/engine/src/flutter/flow/layers/clip_rrect_layer.cc index 8eea82c8f2..f26670b287 100644 --- a/engine/src/flutter/flow/layers/clip_rrect_layer.cc +++ b/engine/src/flutter/flow/layers/clip_rrect_layer.cc @@ -4,38 +4,47 @@ #include "flutter/flow/layers/clip_rrect_layer.h" -#if defined(OS_FUCHSIA) -#include "apps/mozart/lib/skia/type_converters.h" // nogncheck -#include "apps/mozart/services/composition/nodes.fidl.h" // nogncheck -#endif // defined(OS_FUCHSIA) - namespace flow { -ClipRRectLayer::ClipRRectLayer() {} +ClipRRectLayer::ClipRRectLayer() = default; -ClipRRectLayer::~ClipRRectLayer() {} +ClipRRectLayer::~ClipRRectLayer() = default; void ClipRRectLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) { - PrerollChildren(context, matrix); - if (!context->child_paint_bounds.intersect(clip_rrect_.getBounds())) - context->child_paint_bounds.setEmpty(); - set_paint_bounds(context->child_paint_bounds); + SkRect child_paint_bounds = SkRect::MakeEmpty(); + PrerollChildren(context, matrix, &child_paint_bounds); + + if (child_paint_bounds.intersect(clip_rrect_.getBounds())) { + set_paint_bounds(child_paint_bounds); + } } #if defined(OS_FUCHSIA) -void ClipRRectLayer::UpdateScene(SceneUpdateContext& context, - mozart::Node* container) { - auto node = mozart::Node::New(); - node->content_clip = mozart::RectF::From(clip_rrect_.getBounds()); - UpdateSceneChildrenInsideNode(context, container, std::move(node)); +void ClipRRectLayer::UpdateScene(SceneUpdateContext& context) { + FTL_DCHECK(needs_system_composite()); + + // TODO(MZ-137): Need to be able to express the radii as vectors. + mozart::client::RoundedRectangle shape( + context.session(), // session + clip_rrect_.width(), // width + clip_rrect_.height(), // height + clip_rrect_.radii(SkRRect::kUpperLeft_Corner).x(), // top_left_radius + clip_rrect_.radii(SkRRect::kUpperRight_Corner).x(), // top_right_radius + clip_rrect_.radii(SkRRect::kLowerRight_Corner) + .x(), // bottom_right_radius + clip_rrect_.radii(SkRRect::kLowerLeft_Corner).x() // bottom_left_radius + ); + + SceneUpdateContext::Clip clip(context, shape, clip_rrect_.getBounds()); + UpdateSceneChildren(context); } #endif // defined(OS_FUCHSIA) void ClipRRectLayer::Paint(PaintContext& context) { TRACE_EVENT0("flutter", "ClipRRectLayer::Paint"); - FTL_DCHECK(!needs_system_composite()); + FTL_DCHECK(needs_painting()); Layer::AutoSaveLayer save(context, paint_bounds(), nullptr); context.canvas.clipRRect(clip_rrect_, true); diff --git a/engine/src/flutter/flow/layers/clip_rrect_layer.h b/engine/src/flutter/flow/layers/clip_rrect_layer.h index 79b8ae554c..67ba034536 100644 --- a/engine/src/flutter/flow/layers/clip_rrect_layer.h +++ b/engine/src/flutter/flow/layers/clip_rrect_layer.h @@ -16,13 +16,11 @@ class ClipRRectLayer : public ContainerLayer { void set_clip_rrect(const SkRRect& clip_rrect) { clip_rrect_ = clip_rrect; } - protected: void Preroll(PrerollContext* context, const SkMatrix& matrix) override; void Paint(PaintContext& context) override; #if defined(OS_FUCHSIA) - void UpdateScene(SceneUpdateContext& context, - mozart::Node* container) override; + void UpdateScene(SceneUpdateContext& context) override; #endif // defined(OS_FUCHSIA) private: diff --git a/engine/src/flutter/flow/layers/color_filter_layer.cc b/engine/src/flutter/flow/layers/color_filter_layer.cc index e5808b1607..0471d58da3 100644 --- a/engine/src/flutter/flow/layers/color_filter_layer.cc +++ b/engine/src/flutter/flow/layers/color_filter_layer.cc @@ -6,12 +6,14 @@ namespace flow { -ColorFilterLayer::ColorFilterLayer() {} +ColorFilterLayer::ColorFilterLayer() = default; -ColorFilterLayer::~ColorFilterLayer() {} +ColorFilterLayer::~ColorFilterLayer() = default; void ColorFilterLayer::Paint(PaintContext& context) { TRACE_EVENT0("flutter", "ColorFilterLayer::Paint"); + FTL_DCHECK(needs_painting()); + sk_sp color_filter = SkColorFilter::MakeModeFilter(color_, blend_mode_); SkPaint paint; diff --git a/engine/src/flutter/flow/layers/color_filter_layer.h b/engine/src/flutter/flow/layers/color_filter_layer.h index df5e7d71df..f327f121a4 100644 --- a/engine/src/flutter/flow/layers/color_filter_layer.h +++ b/engine/src/flutter/flow/layers/color_filter_layer.h @@ -18,7 +18,6 @@ class ColorFilterLayer : public ContainerLayer { void set_blend_mode(SkBlendMode blend_mode) { blend_mode_ = blend_mode; } - protected: void Paint(PaintContext& context) override; private: diff --git a/engine/src/flutter/flow/layers/container_layer.cc b/engine/src/flutter/flow/layers/container_layer.cc index c320e6896f..d9665d2d00 100644 --- a/engine/src/flutter/flow/layers/container_layer.cc +++ b/engine/src/flutter/flow/layers/container_layer.cc @@ -6,11 +6,9 @@ namespace flow { -ContainerLayer::ContainerLayer() { - ctm_.setIdentity(); -} +ContainerLayer::ContainerLayer() {} -ContainerLayer::~ContainerLayer() {} +ContainerLayer::~ContainerLayer() = default; void ContainerLayer::Add(std::unique_ptr layer) { layer->set_parent(this); @@ -19,60 +17,52 @@ void ContainerLayer::Add(std::unique_ptr layer) { void ContainerLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) { TRACE_EVENT0("flutter", "ContainerLayer::Preroll"); - PrerollChildren(context, matrix); - set_paint_bounds(context->child_paint_bounds); + + SkRect child_paint_bounds = SkRect::MakeEmpty(); + PrerollChildren(context, matrix, &child_paint_bounds); + set_paint_bounds(child_paint_bounds); } void ContainerLayer::PrerollChildren(PrerollContext* context, - const SkMatrix& matrix) { - SkRect child_paint_bounds = SkRect::MakeEmpty(); + const SkMatrix& child_matrix, + SkRect* child_paint_bounds) { for (auto& layer : layers_) { PrerollContext child_context = *context; - FTL_DCHECK(child_context.child_paint_bounds.isEmpty()); - layer->Preroll(&child_context, matrix); - if (layer->needs_system_composite()) - set_needs_system_composite(true); - child_paint_bounds.join(child_context.child_paint_bounds); - } - context->child_paint_bounds = child_paint_bounds; + layer->Preroll(&child_context, child_matrix); - if (needs_system_composite()) - ctm_ = matrix; + if (layer->needs_system_composite()) { + set_needs_system_composite(true); + } + child_paint_bounds->join(layer->paint_bounds()); + } } void ContainerLayer::PaintChildren(PaintContext& context) const { - FTL_DCHECK(!needs_system_composite()); + FTL_DCHECK(needs_painting()); + // Intentionally not tracing here as there should be no self-time // and the trace event on this common function has a small overhead. - for (auto& layer : layers_) - layer->Paint(context); + for (auto& layer : layers_) { + if (layer->needs_painting()) { + layer->Paint(context); + } + } } #if defined(OS_FUCHSIA) -void ContainerLayer::UpdateScene(SceneUpdateContext& context, - mozart::Node* container) { - UpdateSceneChildren(context, container); +void ContainerLayer::UpdateScene(SceneUpdateContext& context) { + UpdateSceneChildren(context); } -void ContainerLayer::UpdateSceneChildrenInsideNode(SceneUpdateContext& context, - mozart::Node* container, - mozart::NodePtr node) { +void ContainerLayer::UpdateSceneChildren(SceneUpdateContext& context) { FTL_DCHECK(needs_system_composite()); - UpdateSceneChildren(context, node.get()); - context.FinalizeCurrentPaintTaskIfNeeded(node.get(), ctm()); - context.AddChildNode(container, std::move(node)); -} -void ContainerLayer::UpdateSceneChildren(SceneUpdateContext& context, - mozart::Node* container) { - FTL_DCHECK(needs_system_composite()); + // Paint all of the layers which need to be drawn into the container. + // These may be flattened down to a containing for (auto& layer : layers_) { if (layer->needs_system_composite()) { - context.FinalizeCurrentPaintTaskIfNeeded(container, ctm()); - layer->UpdateScene(context, container); - } else { - context.AddLayerToCurrentPaintTask(layer.get()); + layer->UpdateScene(context); } } } diff --git a/engine/src/flutter/flow/layers/container_layer.h b/engine/src/flutter/flow/layers/container_layer.h index 0aaa0d6425..f21fa15a0b 100644 --- a/engine/src/flutter/flow/layers/container_layer.h +++ b/engine/src/flutter/flow/layers/container_layer.h @@ -18,31 +18,26 @@ class ContainerLayer : public Layer { void Add(std::unique_ptr layer); void Preroll(PrerollContext* context, const SkMatrix& matrix) override; - void PrerollChildren(PrerollContext* context, const SkMatrix& matrix); - - void PaintChildren(PaintContext& context) const; #if defined(OS_FUCHSIA) - void UpdateScene(SceneUpdateContext& context, - mozart::Node* container) override; - void UpdateSceneChildrenInsideNode(SceneUpdateContext& context, - mozart::Node* container, - mozart::NodePtr node); - void UpdateSceneChildren(SceneUpdateContext& context, - mozart::Node* container); + void UpdateScene(SceneUpdateContext& context) override; #endif // defined(OS_FUCHSIA) const std::vector>& layers() const { return layers_; } protected: - // Valid only after preroll when needs_system_composite() is true. - const SkMatrix& ctm() const { return ctm_; } + void PrerollChildren(PrerollContext* context, + const SkMatrix& child_matrix, + SkRect* child_paint_bounds); + void PaintChildren(PaintContext& context) const; + +#if defined(OS_FUCHSIA) + void UpdateSceneChildren(SceneUpdateContext& context); +#endif // defined(OS_FUCHSIA) private: std::vector> layers_; - SkMatrix ctm_; - FTL_DISALLOW_COPY_AND_ASSIGN(ContainerLayer); }; diff --git a/engine/src/flutter/flow/layers/layer.cc b/engine/src/flutter/flow/layers/layer.cc index c285a3f58d..67148dc0c0 100644 --- a/engine/src/flutter/flow/layers/layer.cc +++ b/engine/src/flutter/flow/layers/layer.cc @@ -12,33 +12,26 @@ namespace flow { Layer::Layer() : parent_(nullptr), needs_system_composite_(false), - has_paint_bounds_(false), - paint_bounds_() {} + paint_bounds_(SkRect::MakeEmpty()) {} Layer::~Layer() = default; -void Layer::Preroll(PrerollContext* context, const SkMatrix& matrix) { - if (!has_paint_bounds()) { - set_paint_bounds(SkRect::MakeEmpty()); - } -} +void Layer::Preroll(PrerollContext* context, const SkMatrix& matrix) {} #if defined(OS_FUCHSIA) -void Layer::UpdateScene(SceneUpdateContext& context, mozart::Node* container) {} -#endif +void Layer::UpdateScene(SceneUpdateContext& context) {} +#endif // defined(OS_FUCHSIA) Layer::AutoSaveLayer::AutoSaveLayer(const PaintContext& paint_context, const SkRect& bounds, const SkPaint* paint) - : paint_context_(paint_context), - bounds_(bounds) { + : paint_context_(paint_context), bounds_(bounds) { paint_context_.canvas.saveLayer(bounds_, paint); } Layer::AutoSaveLayer::AutoSaveLayer(const PaintContext& paint_context, const SkCanvas::SaveLayerRec& layer_rec) - : paint_context_(paint_context), - bounds_(*layer_rec.fBounds) { + : paint_context_(paint_context), bounds_(*layer_rec.fBounds) { paint_context_.canvas.saveLayer(layer_rec); } diff --git a/engine/src/flutter/flow/layers/layer.h b/engine/src/flutter/flow/layers/layer.h index c42abe802c..7bcecd40cc 100644 --- a/engine/src/flutter/flow/layers/layer.h +++ b/engine/src/flutter/flow/layers/layer.h @@ -10,7 +10,6 @@ #include "flutter/flow/instrumentation.h" #include "flutter/flow/raster_cache.h" -#include "flutter/flow/scene_update_context.h" #include "flutter/glue/trace_event.h" #include "lib/ftl/build_config.h" #include "lib/ftl/logging.h" @@ -24,7 +23,16 @@ #include "third_party/skia/include/core/SkRRect.h" #include "third_party/skia/include/core/SkRect.h" +#if defined(OS_FUCHSIA) + +#include "apps/mozart/lib/scene/client/resources.h" //nogncheck +#include "apps/mozart/lib/scene/client/session.h" //nogncheck +#include "flutter/flow/scene_update_context.h" //nogncheck + +#endif // defined(OS_FUCHSIA) + namespace flow { + class ContainerLayer; class Layer { @@ -70,8 +78,8 @@ class Layer { virtual void Paint(PaintContext& context) = 0; #if defined(OS_FUCHSIA) - virtual void UpdateScene(SceneUpdateContext& context, - mozart::Node* container); + // Updates the system composited scene. + virtual void UpdateScene(SceneUpdateContext& context); #endif ContainerLayer* parent() const { return parent_; } @@ -83,23 +91,19 @@ class Layer { needs_system_composite_ = value; } - // subclasses should assume this will be true by the time Paint() is called - bool has_paint_bounds() const { return has_paint_bounds_; } - - const SkRect& paint_bounds() const { - FTL_DCHECK(has_paint_bounds_); - return paint_bounds_; - } + const SkRect& paint_bounds() const { return paint_bounds_; } + // This must be set by the time Preroll() returns otherwise the layer will + // be assumed to have empty paint bounds (paints no content). void set_paint_bounds(const SkRect& paint_bounds) { - has_paint_bounds_ = true; paint_bounds_ = paint_bounds; } + bool needs_painting() const { return !paint_bounds_.isEmpty(); } + private: ContainerLayer* parent_; bool needs_system_composite_; - bool has_paint_bounds_; // if false, paint_bounds_ is not valid SkRect paint_bounds_; FTL_DISALLOW_COPY_AND_ASSIGN(Layer); diff --git a/engine/src/flutter/flow/layers/layer_tree.cc b/engine/src/flutter/flow/layers/layer_tree.cc index aab6df5e84..20dd342327 100644 --- a/engine/src/flutter/flow/layers/layer_tree.cc +++ b/engine/src/flutter/flow/layers/layer_tree.cc @@ -11,12 +11,11 @@ namespace flow { LayerTree::LayerTree() : frame_size_{}, - scene_version_(0), rasterizer_tracing_threshold_(0), checkerboard_raster_cache_images_(false), checkerboard_offscreen_layers_(false) {} -LayerTree::~LayerTree() {} +LayerTree::~LayerTree() = default; void LayerTree::Raster(CompositorContext::ScopedFrame& frame, bool ignore_raster_cache) { @@ -40,15 +39,21 @@ void LayerTree::Preroll(CompositorContext::ScopedFrame& frame, #if defined(OS_FUCHSIA) void LayerTree::UpdateScene(SceneUpdateContext& context, - mozart::Node* container) { + mozart::client::ContainerNode& container) { TRACE_EVENT0("flutter", "LayerTree::UpdateScene"); + SceneUpdateContext::Frame frame( + context, + SkRRect::MakeRect( + SkRect::MakeIWH(frame_size_.width(), frame_size_.height())), + SK_ColorTRANSPARENT, 0.f, 1.f, 1.f); if (root_layer_->needs_system_composite()) { - root_layer_->UpdateScene(context, container); - } else { - context.AddLayerToCurrentPaintTask(root_layer_.get()); + root_layer_->UpdateScene(context); } - context.FinalizeCurrentPaintTaskIfNeeded(container, SkMatrix::I()); + if (root_layer_->needs_painting()) { + frame.AddPaintedLayer(root_layer_.get()); + } + container.AddChild(frame.entity_node()); } #endif @@ -58,7 +63,9 @@ void LayerTree::Paint(CompositorContext::ScopedFrame& frame) { frame.context().memory_usage(), checkerboard_offscreen_layers_}; TRACE_EVENT0("flutter", "LayerTree::Paint"); - root_layer_->Paint(context); + + if (root_layer_->needs_painting()) + root_layer_->Paint(context); } } // namespace flow diff --git a/engine/src/flutter/flow/layers/layer_tree.h b/engine/src/flutter/flow/layers/layer_tree.h index 90863df3b9..1b7e8fcaca 100644 --- a/engine/src/flutter/flow/layers/layer_tree.h +++ b/engine/src/flutter/flow/layers/layer_tree.h @@ -31,10 +31,8 @@ class LayerTree { bool ignore_raster_cache = false); #if defined(OS_FUCHSIA) - // TODO(abarth): Integrate scene updates with the rasterization pass so that - // we can draw on top of child scenes (and so that we can apply clips and - // blending operations to child scene). - void UpdateScene(SceneUpdateContext& context, mozart::Node* container); + void UpdateScene(SceneUpdateContext& context, + mozart::client::ContainerNode& container); #endif void Paint(CompositorContext::ScopedFrame& frame); @@ -49,12 +47,6 @@ class LayerTree { void set_frame_size(const SkISize& frame_size) { frame_size_ = frame_size; } - uint32_t scene_version() const { return scene_version_; } - - void set_scene_version(uint32_t scene_version) { - scene_version_ = scene_version; - } - void set_construction_time(const ftl::TimeDelta& delta) { construction_time_ = delta; } @@ -82,7 +74,6 @@ class LayerTree { private: SkISize frame_size_; // Physical pixels. - uint32_t scene_version_; std::unique_ptr root_layer_; ftl::TimeDelta construction_time_; uint32_t rasterizer_tracing_threshold_; diff --git a/engine/src/flutter/flow/layers/opacity_layer.cc b/engine/src/flutter/flow/layers/opacity_layer.cc index 27a988df27..32eb634c61 100644 --- a/engine/src/flutter/flow/layers/opacity_layer.cc +++ b/engine/src/flutter/flow/layers/opacity_layer.cc @@ -4,35 +4,15 @@ #include "flutter/flow/layers/opacity_layer.h" -#if defined(OS_FUCHSIA) -#include "apps/mozart/lib/skia/type_converters.h" // nogncheck -#include "apps/mozart/services/composition/nodes.fidl.h" // nogncheck -#endif // defined(OS_FUCHSIA) - namespace flow { -OpacityLayer::OpacityLayer() {} +OpacityLayer::OpacityLayer() = default; -OpacityLayer::~OpacityLayer() {} - -#if defined(OS_FUCHSIA) - -void OpacityLayer::UpdateScene(SceneUpdateContext& context, - mozart::Node* container) { - auto node = mozart::Node::New(); - node->op = mozart::NodeOp::New(); - node->op->set_layer(mozart::LayerNodeOp::New()); - node->op->get_layer()->layer_rect = mozart::RectF::From(paint_bounds()); - node->op->get_layer()->blend = mozart::Blend::New(); - node->op->get_layer()->blend->alpha = alpha_; - UpdateSceneChildrenInsideNode(context, container, std::move(node)); -} - -#endif // defined(OS_FUCHSIA) +OpacityLayer::~OpacityLayer() = default; void OpacityLayer::Paint(PaintContext& context) { TRACE_EVENT0("flutter", "OpacityLayer::Paint"); - FTL_DCHECK(!needs_system_composite()); + FTL_DCHECK(needs_painting()); SkPaint paint; paint.setAlpha(alpha_); diff --git a/engine/src/flutter/flow/layers/opacity_layer.h b/engine/src/flutter/flow/layers/opacity_layer.h index 1ccea1023a..c1bdbc158a 100644 --- a/engine/src/flutter/flow/layers/opacity_layer.h +++ b/engine/src/flutter/flow/layers/opacity_layer.h @@ -16,13 +16,10 @@ class OpacityLayer : public ContainerLayer { void set_alpha(int alpha) { alpha_ = alpha; } - protected: void Paint(PaintContext& context) override; -#if defined(OS_FUCHSIA) - void UpdateScene(SceneUpdateContext& context, - mozart::Node* container) override; -#endif // defined(OS_FUCHSIA) + // TODO(chinmaygarde): Once MZ-139 is addressed, introduce a new node in the + // session scene hierarchy. private: int alpha_; diff --git a/engine/src/flutter/flow/layers/physical_model_layer.cc b/engine/src/flutter/flow/layers/physical_model_layer.cc index bc9de32fcb..d6dc5e92d2 100644 --- a/engine/src/flutter/flow/layers/physical_model_layer.cc +++ b/engine/src/flutter/flow/layers/physical_model_layer.cc @@ -7,45 +7,63 @@ #include "flutter/flow/paint_utils.h" #include "third_party/skia/include/utils/SkShadowUtils.h" -#if defined(OS_FUCHSIA) -#include "apps/mozart/lib/skia/type_converters.h" // nogncheck -#include "apps/mozart/services/composition/nodes.fidl.h" // nogncheck -#endif // defined(OS_FUCHSIA) - namespace flow { -PhysicalModelLayer::PhysicalModelLayer() - : rrect_(SkRRect::MakeEmpty()) {} +PhysicalModelLayer::PhysicalModelLayer() : rrect_(SkRRect::MakeEmpty()) {} -PhysicalModelLayer::~PhysicalModelLayer() {} +PhysicalModelLayer::~PhysicalModelLayer() = default; -void PhysicalModelLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) { - PrerollChildren(context, matrix); +void PhysicalModelLayer::Preroll(PrerollContext* context, + const SkMatrix& matrix) { + SkRect child_paint_bounds; + PrerollChildren(context, matrix, &child_paint_bounds); - // Add some margin to the paint bounds to leave space for the shadow. - // The margin is hardcoded to an arbitrary maximum for now because Skia - // doesn't provide a way to calculate it. - SkRect bounds(rrect_.getBounds()); - bounds.outset(20.0, 20.0); - set_paint_bounds(bounds); + if (elevation_ == 0) { + set_paint_bounds(rrect_.getBounds()); + } else { +#if defined(OS_FUCHSIA) + // Let the system compositor draw all shadows for us. + set_needs_system_composite(true); +#else + // Add some margin to the paint bounds to leave space for the shadow. + // The margin is hardcoded to an arbitrary maximum for now because Skia + // doesn't provide a way to calculate it. We fill this whole region + // and clip children to it so we don't need to join the child paint bounds. + SkRect bounds(rrect_.getBounds()); + bounds.outset(20.0, 20.0); + set_paint_bounds(bounds); +#endif // defined(OS_FUCHSIA) + } - context->child_paint_bounds = bounds; +#if defined(OS_FUCHSIA) + if (needs_system_composite()) { + scale_x_ = matrix.getScaleX(); + scale_y_ = matrix.getScaleY(); + } +#endif // defined(OS_FUCHSIA) } #if defined(OS_FUCHSIA) -void PhysicalModelLayer::UpdateScene(SceneUpdateContext& context, - mozart::Node* container) { - context.AddLayerToCurrentPaintTask(this); - auto node = mozart::Node::New(); - node->content_clip = mozart::RectF::From(rrect_.getBounds()); - UpdateSceneChildrenInsideNode(context, container, std::move(node)); +void PhysicalModelLayer::UpdateScene(SceneUpdateContext& context) { + FTL_DCHECK(needs_system_composite()); + + SceneUpdateContext::Frame frame(context, rrect_, color_, elevation_, scale_x_, + scale_y_); + for (auto& layer : layers()) { + if (layer->needs_painting()) { + frame.AddPaintedLayer(layer.get()); + } + } + + UpdateSceneChildren(context); } #endif // defined(OS_FUCHSIA) void PhysicalModelLayer::Paint(PaintContext& context) { TRACE_EVENT0("flutter", "PhysicalModelLayer::Paint"); + FTL_DCHECK(needs_painting()); SkPath path; path.addRRect(rrect_); @@ -55,9 +73,6 @@ void PhysicalModelLayer::Paint(PaintContext& context) { SkColorGetA(color_) != 0xff); } - if (needs_system_composite()) - return; - SkPaint paint; paint.setColor(color_); context.canvas.drawPath(path, paint); @@ -74,22 +89,20 @@ void PhysicalModelLayer::Paint(PaintContext& context) { DrawCheckerboard(&context.canvas, rrect_.getBounds()); } -void PhysicalModelLayer::DrawShadow(SkCanvas* canvas, const SkPath& path, - SkColor color, double elevation, +void PhysicalModelLayer::DrawShadow(SkCanvas* canvas, + const SkPath& path, + SkColor color, + float elevation, bool transparentOccluder) { - SkShadowFlags flags = transparentOccluder ? - SkShadowFlags::kTransparentOccluder_ShadowFlag : - SkShadowFlags::kNone_ShadowFlag; - const SkRect& bounds = path.getBounds(); - SkScalar shadow_x = (bounds.left() + bounds.right()) / 2; - SkScalar shadow_y = bounds.top() - 600.0f; - SkShadowUtils::DrawShadow(canvas, path, - elevation, - SkPoint3::Make(shadow_x, shadow_y, 600.0f), - 800.0f, - 0.039f, 0.25f, - color, - flags); + SkShadowFlags flags = transparentOccluder + ? SkShadowFlags::kTransparentOccluder_ShadowFlag + : SkShadowFlags::kNone_ShadowFlag; + const SkRect& bounds = path.getBounds(); + SkScalar shadow_x = (bounds.left() + bounds.right()) / 2; + SkScalar shadow_y = bounds.top() - 600.0f; + SkShadowUtils::DrawShadow(canvas, path, elevation, + SkPoint3::Make(shadow_x, shadow_y, 600.0f), 800.0f, + 0.039f, 0.25f, color, flags); } } // namespace flow diff --git a/engine/src/flutter/flow/layers/physical_model_layer.h b/engine/src/flutter/flow/layers/physical_model_layer.h index 138dd8171f..653e1839c7 100644 --- a/engine/src/flutter/flow/layers/physical_model_layer.h +++ b/engine/src/flutter/flow/layers/physical_model_layer.h @@ -15,25 +15,31 @@ class PhysicalModelLayer : public ContainerLayer { ~PhysicalModelLayer() override; void set_rrect(const SkRRect& rrect) { rrect_ = rrect; } - void set_elevation(double elevation) { elevation_ = elevation; } + void set_elevation(float elevation) { elevation_ = elevation; } void set_color(SkColor color) { color_ = color; } - static void DrawShadow(SkCanvas* canvas, const SkPath& path, - SkColor color, double elevation, bool transparentOccluder); + static void DrawShadow(SkCanvas* canvas, + const SkPath& path, + SkColor color, + float elevation, + bool transparentOccluder); - protected: void Preroll(PrerollContext* context, const SkMatrix& matrix) override; void Paint(PaintContext& context) override; #if defined(OS_FUCHSIA) - void UpdateScene(SceneUpdateContext& context, - mozart::Node* container) override; + void UpdateScene(SceneUpdateContext& context) override; #endif // defined(OS_FUCHSIA) private: SkRRect rrect_; - double elevation_; + float elevation_; SkColor color_; + +#if defined(OS_FUCHSIA) + SkScalar scale_x_; + SkScalar scale_y_; +#endif // defined(OS_FUCHSIA) }; } // namespace flow diff --git a/engine/src/flutter/flow/layers/picture_layer.cc b/engine/src/flutter/flow/layers/picture_layer.cc index 66bb07f421..cedeec474c 100644 --- a/engine/src/flutter/flow/layers/picture_layer.cc +++ b/engine/src/flutter/flow/layers/picture_layer.cc @@ -9,7 +9,7 @@ namespace flow { -PictureLayer::PictureLayer() {} +PictureLayer::PictureLayer() = default; PictureLayer::~PictureLayer() { // The picture may contain references to textures that are associated @@ -29,13 +29,12 @@ void PictureLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) { SkRect bounds = picture_->cullRect().makeOffset(offset_.x(), offset_.y()); set_paint_bounds(bounds); - context->child_paint_bounds = bounds; } void PictureLayer::Paint(PaintContext& context) { - FTL_DCHECK(picture_); - TRACE_EVENT0("flutter", "PictureLayer::Paint"); + FTL_DCHECK(picture_); + FTL_DCHECK(needs_painting()); SkAutoCanvasRestore save(&context.canvas, true); context.canvas.translate(offset_.x(), offset_.y()); diff --git a/engine/src/flutter/flow/layers/shader_mask_layer.cc b/engine/src/flutter/flow/layers/shader_mask_layer.cc index 5485104b24..447b867b36 100644 --- a/engine/src/flutter/flow/layers/shader_mask_layer.cc +++ b/engine/src/flutter/flow/layers/shader_mask_layer.cc @@ -6,12 +6,14 @@ namespace flow { -ShaderMaskLayer::ShaderMaskLayer() {} +ShaderMaskLayer::ShaderMaskLayer() = default; -ShaderMaskLayer::~ShaderMaskLayer() {} +ShaderMaskLayer::~ShaderMaskLayer() = default; void ShaderMaskLayer::Paint(PaintContext& context) { TRACE_EVENT0("flutter", "ShaderMaskLayer::Paint"); + FTL_DCHECK(needs_painting()); + Layer::AutoSaveLayer(context, paint_bounds(), nullptr); PaintChildren(context); diff --git a/engine/src/flutter/flow/layers/shader_mask_layer.h b/engine/src/flutter/flow/layers/shader_mask_layer.h index 774283e22f..21c1d4ee81 100644 --- a/engine/src/flutter/flow/layers/shader_mask_layer.h +++ b/engine/src/flutter/flow/layers/shader_mask_layer.h @@ -20,11 +20,8 @@ class ShaderMaskLayer : public ContainerLayer { void set_mask_rect(const SkRect& mask_rect) { mask_rect_ = mask_rect; } - void set_blend_mode(SkBlendMode blend_mode) { - blend_mode_ = blend_mode; - } + void set_blend_mode(SkBlendMode blend_mode) { blend_mode_ = blend_mode; } - protected: void Paint(PaintContext& context) override; private: diff --git a/engine/src/flutter/flow/layers/transform_layer.cc b/engine/src/flutter/flow/layers/transform_layer.cc index f60cd23c39..6479163b57 100644 --- a/engine/src/flutter/flow/layers/transform_layer.cc +++ b/engine/src/flutter/flow/layers/transform_layer.cc @@ -4,39 +4,37 @@ #include "flutter/flow/layers/transform_layer.h" -#if defined(OS_FUCHSIA) -#include "apps/mozart/lib/skia/type_converters.h" // nogncheck -#include "apps/mozart/services/composition/nodes.fidl.h" // nogncheck -#endif // defined(OS_FUCHSIA) - namespace flow { -TransformLayer::TransformLayer() {} +TransformLayer::TransformLayer() = default; -TransformLayer::~TransformLayer() {} +TransformLayer::~TransformLayer() = default; void TransformLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) { - SkMatrix childMatrix; - childMatrix.setConcat(matrix, transform_); - PrerollChildren(context, childMatrix); - transform_.mapRect(&context->child_paint_bounds); - set_paint_bounds(context->child_paint_bounds); + SkMatrix child_matrix; + child_matrix.setConcat(matrix, transform_); + + SkRect child_paint_bounds = SkRect::MakeEmpty(); + PrerollChildren(context, child_matrix, &child_paint_bounds); + + transform_.mapRect(&child_paint_bounds); + set_paint_bounds(child_paint_bounds); } #if defined(OS_FUCHSIA) -void TransformLayer::UpdateScene(SceneUpdateContext& context, - mozart::Node* container) { - auto node = mozart::Node::New(); - node->content_transform = mozart::Transform::From(transform_); - UpdateSceneChildrenInsideNode(context, container, std::move(node)); +void TransformLayer::UpdateScene(SceneUpdateContext& context) { + FTL_DCHECK(needs_system_composite()); + + SceneUpdateContext::Transform transform(context, transform_); + UpdateSceneChildren(context); } #endif // defined(OS_FUCHSIA) void TransformLayer::Paint(PaintContext& context) { TRACE_EVENT0("flutter", "TransformLayer::Paint"); - FTL_DCHECK(!needs_system_composite()); + FTL_DCHECK(needs_painting()); SkAutoCanvasRestore save(&context.canvas, true); context.canvas.concat(transform_); diff --git a/engine/src/flutter/flow/layers/transform_layer.h b/engine/src/flutter/flow/layers/transform_layer.h index dee5fc5796..e3f47f4462 100644 --- a/engine/src/flutter/flow/layers/transform_layer.h +++ b/engine/src/flutter/flow/layers/transform_layer.h @@ -16,13 +16,11 @@ class TransformLayer : public ContainerLayer { void set_transform(const SkMatrix& transform) { transform_ = transform; } - protected: void Preroll(PrerollContext* context, const SkMatrix& matrix) override; void Paint(PaintContext& context) override; #if defined(OS_FUCHSIA) - void UpdateScene(SceneUpdateContext& context, - mozart::Node* container) override; + void UpdateScene(SceneUpdateContext& context) override; #endif // defined(OS_FUCHSIA) private: diff --git a/engine/src/flutter/flow/scene_update_context.cc b/engine/src/flutter/flow/scene_update_context.cc index 1e3dece8ce..822851227d 100644 --- a/engine/src/flutter/flow/scene_update_context.cc +++ b/engine/src/flutter/flow/scene_update_context.cc @@ -4,124 +4,272 @@ #include "flutter/flow/scene_update_context.h" -#if defined(OS_FUCHSIA) - -#include "apps/mozart/lib/skia/skia_vmo_surface.h" // nogncheck -#include "apps/mozart/lib/skia/type_converters.h" // nogncheck -#include "apps/mozart/services/composition/scenes.fidl.h" // nogncheck +#include "flutter/flow/export_node.h" #include "flutter/flow/layers/layer.h" +#include "flutter/flow/matrix_decomposition.h" #include "flutter/glue/trace_event.h" namespace flow { -SceneUpdateContext::CurrentPaintTask::CurrentPaintTask() - : bounds(SkRect::MakeEmpty()) {} - -void SceneUpdateContext::CurrentPaintTask::Clear() { - bounds = SkRect::MakeEmpty(); - layers.clear(); -} - -SceneUpdateContext::SceneUpdateContext(mozart::SceneUpdate* update, +SceneUpdateContext::SceneUpdateContext(mozart::client::Session* session, SurfaceProducer* surface_producer) - : update_(update), surface_producer_(surface_producer) {} + : session_(session), surface_producer_(surface_producer) { + FTL_DCHECK(surface_producer_ != nullptr); +} SceneUpdateContext::~SceneUpdateContext() = default; -void SceneUpdateContext::AddLayerToCurrentPaintTask(Layer* layer) { - current_paint_task_.bounds.join(layer->paint_bounds()); - current_paint_task_.layers.push_back(layer); +void SceneUpdateContext::AddChildScene(ExportNode* export_node, + SkPoint offset, + float device_pixel_ratio, + bool hit_testable) { + FTL_DCHECK(top_entity_); + + export_node->Bind(*this, top_entity_->entity_node(), offset, + 1.f / device_pixel_ratio, hit_testable); } -void SceneUpdateContext::FinalizeCurrentPaintTaskIfNeeded( - mozart::Node* container, - const SkMatrix& ctm) { - if (mozart::NodePtr node = FinalizeCurrentPaintTask(ctm)) - AddChildNode(container, std::move(node)); +void SceneUpdateContext::CreateFrame(mozart::client::EntityNode& entity_node, + const SkRRect& rrect, + SkColor color, + SkScalar scale_x, + SkScalar scale_y, + const SkRect& paint_bounds, + std::vector paint_layers) { + // Frames always clip their children. + entity_node.SetClip(0u, true /* clip to self */); + + // We don't need a shape if the frame is zero size. + if (rrect.isEmpty()) + return; + + // Add a part which represents the frame's geometry for clipping purposes + // and possibly for its texture. + // TODO(MZ-137): Need to be able to express the radii as vectors. + SkRect shape_bounds = rrect.getBounds(); + mozart::client::RoundedRectangle shape( + session_, // session + rrect.width(), // width + rrect.height(), // height + rrect.radii(SkRRect::kUpperLeft_Corner).x(), // top_left_radius + rrect.radii(SkRRect::kUpperRight_Corner).x(), // top_right_radius + rrect.radii(SkRRect::kLowerRight_Corner).x(), // bottom_right_radius + rrect.radii(SkRRect::kLowerLeft_Corner).x() // bottom_left_radius + ); + mozart::client::ShapeNode shape_node(session_); + shape_node.SetShape(shape); + shape_node.SetTranslation(shape_bounds.width() * 0.5f + shape_bounds.left(), + shape_bounds.height() * 0.5f + shape_bounds.top(), + 0.f); + entity_node.AddPart(shape_node); + + // Check whether the painted layers will be visible. + if (paint_bounds.isEmpty() || !paint_bounds.intersects(shape_bounds)) + paint_layers.clear(); + + // Check whether a solid color will suffice. + if (paint_layers.empty()) { + SetShapeColor(shape_node, color); + return; + } + + // If the painted area only covers a portion of the frame then we can + // reduce the texture size by drawing just that smaller area. + SkRect inner_bounds = shape_bounds; + inner_bounds.intersect(paint_bounds); + if (inner_bounds != shape_bounds && rrect.contains(inner_bounds)) { + SetShapeColor(shape_node, color); + + mozart::client::Rectangle inner_shape(session_, inner_bounds.width(), + inner_bounds.height()); + mozart::client::ShapeNode inner_node(session_); + inner_node.SetShape(inner_shape); + inner_node.SetTranslation(inner_bounds.width() * 0.5f + inner_bounds.left(), + inner_bounds.height() * 0.5f + inner_bounds.top(), + 0.f); + entity_node.AddPart(inner_node); + SetShapeTextureOrColor(inner_node, color, scale_x, scale_y, inner_bounds, + std::move(paint_layers)); + return; + } + + // Apply a texture to the whole shape. + SetShapeTextureOrColor(shape_node, color, scale_x, scale_y, shape_bounds, + std::move(paint_layers)); } -mozart::NodePtr SceneUpdateContext::FinalizeCurrentPaintTask( - const SkMatrix& ctm) { - if (current_paint_task_.layers.empty()) +void SceneUpdateContext::SetShapeTextureOrColor( + mozart::client::ShapeNode& shape_node, + SkColor color, + SkScalar scale_x, + SkScalar scale_y, + const SkRect& paint_bounds, + std::vector paint_layers) { + mozart::client::Image* image = GenerateImageIfNeeded( + color, scale_x, scale_y, paint_bounds, std::move(paint_layers)); + if (image != nullptr) { + mozart::client::Material material(session_); + material.SetTexture(*image); + shape_node.SetMaterial(material); + return; + } + + SetShapeColor(shape_node, color); +} + +void SceneUpdateContext::SetShapeColor(mozart::client::ShapeNode& shape_node, + SkColor color) { + if (SkColorGetA(color) == 0) + return; + + mozart::client::Material material(session_); + material.SetColor(SkColorGetR(color), SkColorGetG(color), SkColorGetB(color), + SkColorGetA(color)); + shape_node.SetMaterial(material); +} + +mozart::client::Image* SceneUpdateContext::GenerateImageIfNeeded( + SkColor color, + SkScalar scale_x, + SkScalar scale_y, + const SkRect& paint_bounds, + std::vector paint_layers) { + // Bail if there's nothing to paint. + if (paint_layers.empty()) return nullptr; - const SkRect& bounds = current_paint_task_.bounds; + // Bail if the physical bounds are empty after rounding. + SkISize physical_size = SkISize::Make(paint_bounds.width() * scale_x, + paint_bounds.height() * scale_y); + if (physical_size.isEmpty()) + return nullptr; - SkScalar scaleX = ctm.getScaleX(); - SkScalar scaleY = ctm.getScaleY(); + // Acquire a surface from the surface producer and register the paint tasks. + auto surface = surface_producer_->ProduceSurface(physical_size); - SkISize physical_size = - SkISize::Make(bounds.width() * scaleX, bounds.height() * scaleY); - - if (physical_size.isEmpty()) { - current_paint_task_.Clear(); + if (!surface) { + FTL_LOG(ERROR) << "Could not acquire a surface from the surface producer " + "of size: " + << physical_size.width() << "x" << physical_size.height(); return nullptr; } - mozart::ImagePtr image; - PaintTask task; - task.surface = surface_producer_->ProduceSurface(physical_size, &image); - task.left = bounds.left(); - task.top = bounds.top(); - task.scaleX = scaleX; - task.scaleY = scaleY; - task.layers = std::move(current_paint_task_.layers); + auto image = surface->GetImage(); - FTL_DCHECK(task.surface) << "Failed to create surface size=" - << physical_size.width() << "x" - << physical_size.height(); - - paint_tasks_.push_back(task); - - auto resource = mozart::Resource::New(); - resource->set_image(mozart::ImageResource::New()); - resource->get_image()->image = std::move(image); - - auto node = mozart::Node::New(); - node->op = mozart::NodeOp::New(); - node->op->set_image(mozart::ImageNodeOp::New()); - node->op->get_image()->content_rect = mozart::RectF::From(bounds); - node->op->get_image()->image_resource_id = AddResource(std::move(resource)); - - current_paint_task_.Clear(); - return node; + // Enqueue the paint task. + paint_tasks_.push_back({.surface = std::move(surface), + .left = paint_bounds.left(), + .top = paint_bounds.top(), + .scale_x = scale_x, + .scale_y = scale_y, + .background_color = color, + .layers = std::move(paint_layers)}); + return image; } -uint32_t SceneUpdateContext::AddResource(mozart::ResourcePtr resource) { - uint32_t resource_id = next_resource_id_++; - update_->resources.insert(resource_id, std::move(resource)); - return resource_id; -} - -void SceneUpdateContext::AddChildNode(mozart::Node* container, - mozart::NodePtr child) { - uint32_t node_id = next_node_id_++; - update_->nodes.insert(node_id, std::move(child)); - container->child_node_ids.push_back(node_id); -} - -void SceneUpdateContext::ExecutePaintTasks( - CompositorContext::ScopedFrame& frame) { +std::vector> +SceneUpdateContext::ExecutePaintTasks(CompositorContext::ScopedFrame& frame) { TRACE_EVENT0("flutter", "SceneUpdateContext::ExecutePaintTasks"); - + std::vector> surfaces_to_submit; for (auto& task : paint_tasks_) { FTL_DCHECK(task.surface); - SkCanvas* canvas = task.surface->getCanvas(); + SkCanvas* canvas = task.surface->GetSkiaSurface()->getCanvas(); Layer::PaintContext context = {*canvas, frame.context().frame_time(), frame.context().engine_time(), - frame.context().memory_usage(), - false}; - - canvas->clear(SK_ColorTRANSPARENT); - canvas->scale(task.scaleX, task.scaleY); + frame.context().memory_usage(), false}; + canvas->restoreToCount(1); + canvas->save(); + canvas->clear(task.background_color); + canvas->scale(task.scale_x, task.scale_y); canvas->translate(-task.left, -task.top); - for (Layer* layer : task.layers) + for (Layer* layer : task.layers) { layer->Paint(context); + } + surfaces_to_submit.emplace_back(std::move(task.surface)); } - paint_tasks_.clear(); + return surfaces_to_submit; +} + +SceneUpdateContext::Entity::Entity(SceneUpdateContext& context) + : context_(context), + previous_entity_(context.top_entity_), + entity_node_(context.session()) { + if (previous_entity_) + previous_entity_->entity_node_.AddChild(entity_node_); + context.top_entity_ = this; +} + +SceneUpdateContext::Entity::~Entity() { + FTL_DCHECK(context_.top_entity_ == this); + context_.top_entity_ = previous_entity_; +} + +SceneUpdateContext::Clip::Clip(SceneUpdateContext& context, + mozart::client::Shape& shape, + const SkRect& shape_bounds) + : Entity(context) { + mozart::client::ShapeNode shape_node(context.session()); + shape_node.SetShape(shape); + shape_node.SetTranslation(shape_bounds.width() * 0.5f + shape_bounds.left(), + shape_bounds.height() * 0.5f + shape_bounds.top(), + 0.f); + + entity_node().AddPart(shape_node); + entity_node().SetClip(0u, true /* clip to self */); +} + +SceneUpdateContext::Clip::~Clip() = default; + +SceneUpdateContext::Transform::Transform(SceneUpdateContext& context, + const SkMatrix& transform) + : Entity(context) { + // TODO(MZ-192): The perspective and shear components in the matrix + // are not handled correctly. + MatrixDecomposition decomposition(transform); + if (decomposition.IsValid()) { + entity_node().SetTranslation(decomposition.translation().x(), // + decomposition.translation().y(), // + decomposition.translation().z() // + ); + entity_node().SetScale(decomposition.scale().x(), // + decomposition.scale().y(), // + decomposition.scale().z() // + ); + entity_node().SetRotation(decomposition.rotation().fData[0], // + decomposition.rotation().fData[1], // + decomposition.rotation().fData[2], // + decomposition.rotation().fData[3] // + ); + } +} + +SceneUpdateContext::Transform::~Transform() = default; + +SceneUpdateContext::Frame::Frame(SceneUpdateContext& context, + const SkRRect& rrect, + SkColor color, + float elevation, + SkScalar scale_x, + SkScalar scale_y) + : Entity(context), + rrect_(rrect), + color_(color), + scale_x_(scale_x), + scale_y_(scale_y), + paint_bounds_(SkRect::MakeEmpty()) { + entity_node().SetTranslation(0.f, 0.f, elevation); +} + +SceneUpdateContext::Frame::~Frame() { + context().CreateFrame(entity_node(), rrect_, color_, scale_x_, scale_y_, + paint_bounds_, std::move(paint_layers_)); +} + +void SceneUpdateContext::Frame::AddPaintedLayer(Layer* layer) { + FTL_DCHECK(layer->needs_painting()); + paint_layers_.push_back(layer); + paint_bounds_.join(layer->paint_bounds()); } } // namespace flow - -#endif // defined(OS_FUCHSIA) diff --git a/engine/src/flutter/flow/scene_update_context.h b/engine/src/flutter/flow/scene_update_context.h index 4771487cfb..539ceae29e 100644 --- a/engine/src/flutter/flow/scene_update_context.h +++ b/engine/src/flutter/flow/scene_update_context.h @@ -2,12 +2,13 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef FLUTTER_FLOW_MOZART_SCENE_UPDATE_CONTEXT_H_ -#define FLUTTER_FLOW_MOZART_SCENE_UPDATE_CONTEXT_H_ +#ifndef FLUTTER_FLOW_SCENE_UPDATE_CONTEXT_H_ +#define FLUTTER_FLOW_SCENE_UPDATE_CONTEXT_H_ #include #include +#include "apps/mozart/lib/scene/client/resources.h" #include "flutter/flow/compositor_context.h" #include "lib/ftl/build_config.h" #include "lib/ftl/logging.h" @@ -15,82 +16,157 @@ #include "third_party/skia/include/core/SkRect.h" #include "third_party/skia/include/core/SkSurface.h" -#if defined(OS_FUCHSIA) -#include "apps/mozart/services/composition/nodes.fidl.h" // nogncheck -#include "apps/mozart/services/composition/resources.fidl.h" // nogncheck -#include "apps/mozart/services/images/image.fidl.h" // nogncheck -#endif // defined(OS_FUCHSIA) - -namespace mozart { -class BufferProducer; -class Node; -class SceneUpdate; -} // namespace mozart - namespace flow { -class Layer; -class SceneUpdateContext; -#if defined(OS_FUCHSIA) +class Layer; +class ExportNode; class SceneUpdateContext { public: + class SurfaceProducerSurface { + public: + virtual ~SurfaceProducerSurface() = default; + + virtual size_t AdvanceAndGetAge() = 0; + + virtual bool FlushSessionAcquireAndReleaseEvents() = 0; + + virtual bool IsValid() const = 0; + + virtual SkISize GetSize() const = 0; + + virtual void SignalWritesFinished( + std::function on_writes_committed) = 0; + + virtual mozart::client::Image* GetImage() = 0; + + virtual sk_sp GetSkiaSurface() const = 0; + }; + class SurfaceProducer { public: - virtual ~SurfaceProducer() {} - virtual sk_sp ProduceSurface(SkISize size, - mozart::ImagePtr* out_image) = 0; + virtual std::unique_ptr ProduceSurface( + const SkISize& size) = 0; + + virtual void SubmitSurface( + std::unique_ptr surface) = 0; }; - SceneUpdateContext(mozart::SceneUpdate* update, + SceneUpdateContext(mozart::client::Session* session, SurfaceProducer* surface_producer); + ~SceneUpdateContext(); - mozart::SceneUpdate* update() const { return update_; } + mozart::client::Session* session() { return session_; } - void AddLayerToCurrentPaintTask(Layer* layer); - void FinalizeCurrentPaintTaskIfNeeded(mozart::Node* container, - const SkMatrix& ctm); + class Entity { + public: + Entity(SceneUpdateContext& context); + ~Entity(); - uint32_t AddResource(mozart::ResourcePtr resource); - void AddChildNode(mozart::Node* container, mozart::NodePtr child); + SceneUpdateContext& context() { return context_; } + mozart::client::EntityNode& entity_node() { return entity_node_; } - void ExecutePaintTasks(CompositorContext::ScopedFrame& frame); + private: + SceneUpdateContext& context_; + Entity* const previous_entity_; + + mozart::client::EntityNode entity_node_; + }; + + class Clip : public Entity { + public: + Clip(SceneUpdateContext& context, + mozart::client::Shape& shape, + const SkRect& shape_bounds); + ~Clip(); + }; + + class Transform : public Entity { + public: + Transform(SceneUpdateContext& context, const SkMatrix& transform); + ~Transform(); + }; + + class Frame : public Entity { + public: + Frame(SceneUpdateContext& context, + const SkRRect& rrect, + SkColor color, + float elevation, + SkScalar scale_x, + SkScalar scale_y); + ~Frame(); + + void AddPaintedLayer(Layer* layer); + + private: + const SkRRect& rrect_; + SkColor const color_; + SkScalar const scale_x_; + SkScalar const scale_y_; + + std::vector paint_layers_; + SkRect paint_bounds_; + }; + + void AddChildScene(ExportNode* export_node, + SkPoint offset, + float device_pixel_ratio, + bool hit_testable); + + // TODO(chinmaygarde): This method must submit the surfaces as soon as paint + // tasks are done. However, given that there is no support currently for + // Vulkan semaphores, we need to submit all the surfaces after an explicit + // CPU wait. Once Vulkan semaphores are available, this method must return + // void and the implementation must submit surfaces on its own as soon as the + // specific canvas operations are done. + FTL_WARN_UNUSED_RESULT + std::vector> ExecutePaintTasks( + CompositorContext::ScopedFrame& frame); private: - mozart::NodePtr FinalizeCurrentPaintTask(const SkMatrix& ctm); - - struct CurrentPaintTask { - CurrentPaintTask(); - void Clear(); - - SkRect bounds; - std::vector layers; - }; - struct PaintTask { - sk_sp surface; + std::unique_ptr surface; SkScalar left; SkScalar top; - SkScalar scaleX; - SkScalar scaleY; + SkScalar scale_x; + SkScalar scale_y; + SkColor background_color; std::vector layers; }; - mozart::SceneUpdate* update_; - SurfaceProducer* surface_producer_; + void CreateFrame(mozart::client::EntityNode& entity_node, + const SkRRect& rrect, + SkColor color, + SkScalar scale_x, + SkScalar scale_y, + const SkRect& paint_bounds, + std::vector paint_layers); + void SetShapeTextureOrColor(mozart::client::ShapeNode& shape_node, + SkColor color, + SkScalar scale_x, + SkScalar scale_y, + const SkRect& paint_bounds, + std::vector paint_layers); + void SetShapeColor(mozart::client::ShapeNode& shape_node, SkColor color); + mozart::client::Image* GenerateImageIfNeeded( + SkColor color, + SkScalar scale_x, + SkScalar scale_y, + const SkRect& paint_bounds, + std::vector paint_layers); + + Entity* top_entity_ = nullptr; + + mozart::client::Session* const session_; + SurfaceProducer* const surface_producer_; - CurrentPaintTask current_paint_task_; std::vector paint_tasks_; - uint32_t next_resource_id_ = 1; - uint32_t next_node_id_ = 1; - FTL_DISALLOW_COPY_AND_ASSIGN(SceneUpdateContext); }; -#endif // defined(OS_FUCHSIA) - } // namespace flow -#endif // FLUTTER_FLOW_MOZART_SCENE_UPDATE_CONTEXT_H_ +#endif // FLUTTER_FLOW_SCENE_UPDATE_CONTEXT_H_ diff --git a/engine/src/flutter/lib/ui/BUILD.gn b/engine/src/flutter/lib/ui/BUILD.gn index 8c0dbded34..ef0fd76044 100644 --- a/engine/src/flutter/lib/ui/BUILD.gn +++ b/engine/src/flutter/lib/ui/BUILD.gn @@ -8,6 +8,8 @@ source_set("ui") { "compositing/scene.h", "compositing/scene_builder.cc", "compositing/scene_builder.h", + "compositing/scene_host.cc", + "compositing/scene_host.h", "dart_runtime_hooks.cc", "dart_runtime_hooks.h", "dart_ui.cc", diff --git a/engine/src/flutter/lib/ui/compositing.dart b/engine/src/flutter/lib/ui/compositing.dart index cdde2b1668..b97c12d08a 100644 --- a/engine/src/flutter/lib/ui/compositing.dart +++ b/engine/src/flutter/lib/ui/compositing.dart @@ -199,16 +199,12 @@ class SceneBuilder extends NativeFieldWrapperClass2 { /// (Fuchsia-only) Adds a scene rendered by another application to the scene /// for this application. - /// - /// Applications typically obtain scene tokens when embedding other views via - /// the Fuchsia view manager, but this function is agnostic as to the source - /// of scene token. void addChildScene({ Offset offset: Offset.zero, double devicePixelRatio: 1.0, int physicalWidth: 0, int physicalHeight: 0, - int sceneToken, + SceneHost sceneHost, bool hitTestable: true }) { _addChildScene(offset.dx, @@ -216,7 +212,7 @@ class SceneBuilder extends NativeFieldWrapperClass2 { devicePixelRatio, physicalWidth, physicalHeight, - sceneToken, + sceneHost, hitTestable); } void _addChildScene(double dx, @@ -224,7 +220,7 @@ class SceneBuilder extends NativeFieldWrapperClass2 { double devicePixelRatio, int physicalWidth, int physicalHeight, - int sceneToken, + SceneHost sceneHost, bool hitTestable) native "SceneBuilder_addChildScene"; /// Sets a threshold after which additional debugging information should be recorded. @@ -268,3 +264,24 @@ class SceneBuilder extends NativeFieldWrapperClass2 { /// cannot be used further. Scene build() native "SceneBuilder_build"; } + +/// (Fuchsia-only) Hosts content provided by another application. +class SceneHost extends NativeFieldWrapperClass2 { + /// Creates a host for a child scene. + /// + /// The export token is bound to a scene graph node which acts as a container + /// for the child's content. The creator of the scene host is responsible for + /// sending the corresponding import token (the other endpoint of the event pair) + /// to the child. + /// + /// The scene host takes ownership of the provided export token handle. + SceneHost(int export_token_handle) { + _constructor(export_token_handle); + } + void _constructor(int export_token_handle) native "SceneHost_constructor"; + + /// Releases the resources associated with the child scene host. + /// + /// After calling this function, the child scene host cannot be used further. + void dispose() native "SceneHost_dispose"; +} diff --git a/engine/src/flutter/lib/ui/compositing/scene_builder.cc b/engine/src/flutter/lib/ui/compositing/scene_builder.cc index 76106c4452..1645781c99 100644 --- a/engine/src/flutter/lib/ui/compositing/scene_builder.cc +++ b/engine/src/flutter/lib/ui/compositing/scene_builder.cc @@ -52,7 +52,7 @@ IMPLEMENT_WRAPPERTYPEINFO(ui, SceneBuilder); V(SceneBuilder, addChildScene) \ V(SceneBuilder, addPerformanceOverlay) \ V(SceneBuilder, setRasterizerTracingThreshold) \ - V(SceneBuilder, setCheckerboardOffscreenLayers) \ + V(SceneBuilder, setCheckerboardOffscreenLayers) \ V(SceneBuilder, setCheckerboardRasterCacheImages) \ V(SceneBuilder, build) @@ -163,7 +163,8 @@ void SceneBuilder::pushPhysicalModel(const RRect& rrect, if (!cullRect.intersect(rrect.sk_rrect.rect(), m_cullRects.top())) cullRect = SkRect::MakeEmpty(); - std::unique_ptr layer(new flow::PhysicalModelLayer()); + std::unique_ptr layer( + new flow::PhysicalModelLayer()); layer->set_rrect(rrect.sk_rrect); layer->set_elevation(elevation); layer->set_color(color); @@ -221,7 +222,7 @@ void SceneBuilder::addChildScene(double dx, double devicePixelRatio, int physicalWidth, int physicalHeight, - uint32_t sceneToken, + SceneHost* sceneHost, bool hitTestable) { #if defined(OS_FUCHSIA) if (!m_currentLayer) @@ -236,7 +237,7 @@ void SceneBuilder::addChildScene(double dx, layer->set_offset(SkPoint::Make(dx, dy)); layer->set_device_pixel_ratio(devicePixelRatio); layer->set_physical_size(SkISize::Make(physicalWidth, physicalHeight)); - layer->set_scene_token(sceneToken); + layer->set_export_node(sceneHost->exportNode()); layer->set_hit_testable(hitTestable); m_currentLayer->Add(std::move(layer)); #endif diff --git a/engine/src/flutter/lib/ui/compositing/scene_builder.h b/engine/src/flutter/lib/ui/compositing/scene_builder.h index c66569be4b..95d43b8087 100644 --- a/engine/src/flutter/lib/ui/compositing/scene_builder.h +++ b/engine/src/flutter/lib/ui/compositing/scene_builder.h @@ -11,6 +11,7 @@ #include "flutter/flow/layers/container_layer.h" #include "flutter/lib/ui/compositing/scene.h" +#include "flutter/lib/ui/compositing/scene_host.h" #include "flutter/lib/ui/painting/image_filter.h" #include "flutter/lib/ui/painting/path.h" #include "flutter/lib/ui/painting/picture.h" @@ -61,7 +62,7 @@ class SceneBuilder : public ftl::RefCountedThreadSafe, double devicePixelRatio, int physicalWidth, int physicalHeight, - uint32_t sceneToken, + SceneHost* sceneHost, bool hitTestable); void setRasterizerTracingThreshold(uint32_t frameInterval); diff --git a/engine/src/flutter/lib/ui/compositing/scene_host.cc b/engine/src/flutter/lib/ui/compositing/scene_host.cc new file mode 100644 index 0000000000..1635b64bc7 --- /dev/null +++ b/engine/src/flutter/lib/ui/compositing/scene_host.cc @@ -0,0 +1,44 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/lib/ui/compositing/scene_host.h" + +#include "lib/tonic/dart_args.h" +#include "lib/tonic/dart_binding_macros.h" +#include "lib/tonic/dart_library_natives.h" + +namespace blink { + +static void SceneHost_constructor(Dart_NativeArguments args) { + DartCallConstructor(&SceneHost::create, args); +} + +IMPLEMENT_WRAPPERTYPEINFO(ui, SceneHost); + +#define FOR_EACH_BINDING(V) V(SceneHost, dispose) + +FOR_EACH_BINDING(DART_NATIVE_CALLBACK) + +void SceneHost::RegisterNatives(tonic::DartLibraryNatives* natives) { + natives->Register({{"SceneHost_constructor", SceneHost_constructor, 2, true}, + FOR_EACH_BINDING(DART_REGISTER_NATIVE)}); +} + +ftl::RefPtr SceneHost::create(int export_token_handle) { + return ftl::MakeRefCounted(export_token_handle); +} + +SceneHost::SceneHost(int export_token_handle) { +#if defined(OS_FUCHSIA) + export_node_ = ftl::MakeRefCounted(export_token_handle); +#endif +} + +SceneHost::~SceneHost() {} + +void SceneHost::dispose() { + ClearDartWrapper(); +} + +} // namespace blink diff --git a/engine/src/flutter/lib/ui/compositing/scene_host.h b/engine/src/flutter/lib/ui/compositing/scene_host.h new file mode 100644 index 0000000000..33e7ee53df --- /dev/null +++ b/engine/src/flutter/lib/ui/compositing/scene_host.h @@ -0,0 +1,52 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_LIB_UI_COMPOSITING_SCENE_HOST_H_ +#define FLUTTER_LIB_UI_COMPOSITING_SCENE_HOST_H_ + +#include + +#include "lib/tonic/dart_wrappable.h" + +#if defined(OS_FUCHSIA) +#include "flutter/flow/export_node.h" +#endif + +namespace tonic { +class DartLibraryNatives; +} // namespace tonic + +namespace blink { + +class SceneHost : public ftl::RefCountedThreadSafe, + public tonic::DartWrappable { + DEFINE_WRAPPERTYPEINFO(); + FRIEND_MAKE_REF_COUNTED(SceneHost); + + public: + static ftl::RefPtr create(int export_token_handle); + + ~SceneHost() override; + +#if defined(OS_FUCHSIA) + const ftl::RefPtr& exportNode() const { + return export_node_; + } +#endif + + void dispose(); + + static void RegisterNatives(tonic::DartLibraryNatives* natives); + + private: +#if defined(OS_FUCHSIA) + ftl::RefPtr export_node_; +#endif + + explicit SceneHost(int export_token_handle); +}; + +} // namespace blink + +#endif // FLUTTER_LIB_UI_COMPOSITING_SCENE_HOST_H_ diff --git a/engine/src/flutter/lib/ui/dart_ui.cc b/engine/src/flutter/lib/ui/dart_ui.cc index f394412c69..ae7e8a1ee3 100644 --- a/engine/src/flutter/lib/ui/dart_ui.cc +++ b/engine/src/flutter/lib/ui/dart_ui.cc @@ -64,6 +64,7 @@ void DartUI::InitForGlobal() { PictureRecorder::RegisterNatives(g_natives); Scene::RegisterNatives(g_natives); SceneBuilder::RegisterNatives(g_natives); + SceneHost::RegisterNatives(g_natives); SemanticsUpdate::RegisterNatives(g_natives); SemanticsUpdateBuilder::RegisterNatives(g_natives); Vertices::RegisterNatives(g_natives); diff --git a/engine/src/flutter/vulkan/BUILD.gn b/engine/src/flutter/vulkan/BUILD.gn index 1c24eedc8b..8ff1e710af 100644 --- a/engine/src/flutter/vulkan/BUILD.gn +++ b/engine/src/flutter/vulkan/BUILD.gn @@ -63,5 +63,8 @@ source_set("vulkan") { "//third_party/skia:gpu", ] - public_configs = [ ":vulkan_config", "//third_party/skia:skia_private" ] + public_configs = [ + ":vulkan_config", + "//third_party/skia:skia_private", + ] } diff --git a/engine/src/flutter/vulkan/vulkan_proc_table.cc b/engine/src/flutter/vulkan/vulkan_proc_table.cc index 2584c2861c..39a087ac5f 100644 --- a/engine/src/flutter/vulkan/vulkan_proc_table.cc +++ b/engine/src/flutter/vulkan/vulkan_proc_table.cc @@ -74,16 +74,16 @@ bool VulkanProcTable::SetupInstanceProcAddresses( ACQUIRE_PROC(CreateDevice, handle); ACQUIRE_PROC(DestroyDevice, handle); ACQUIRE_PROC(DestroyInstance, handle); + ACQUIRE_PROC(DestroySurfaceKHR, handle); ACQUIRE_PROC(EnumerateDeviceLayerProperties, handle); ACQUIRE_PROC(EnumeratePhysicalDevices, handle); + ACQUIRE_PROC(GetDeviceProcAddr, handle); ACQUIRE_PROC(GetPhysicalDeviceFeatures, handle); ACQUIRE_PROC(GetPhysicalDeviceQueueFamilyProperties, handle); ACQUIRE_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR, handle); ACQUIRE_PROC(GetPhysicalDeviceSurfaceFormatsKHR, handle); ACQUIRE_PROC(GetPhysicalDeviceSurfacePresentModesKHR, handle); ACQUIRE_PROC(GetPhysicalDeviceSurfaceSupportKHR, handle); - ACQUIRE_PROC(GetDeviceProcAddr, handle); - ACQUIRE_PROC(DestroySurfaceKHR, handle); #if OS_ANDROID ACQUIRE_PROC(CreateAndroidSurfaceKHR, handle); @@ -92,10 +92,10 @@ bool VulkanProcTable::SetupInstanceProcAddresses( #if OS_FUCHSIA [this, &handle]() -> bool { ACQUIRE_PROC(CreateMagmaSurfaceKHR, handle); + ACQUIRE_PROC(ExportDeviceMemoryMAGMA, handle); ACQUIRE_PROC(GetPhysicalDeviceMagmaPresentationSupportKHR, handle); return true; }(); - #endif // OS_FUCHSIA // The debug report functions are optional. We don't want proc acquisition to @@ -116,28 +116,37 @@ bool VulkanProcTable::SetupInstanceProcAddresses( bool VulkanProcTable::SetupDeviceProcAddresses( const VulkanHandle& handle) { ACQUIRE_PROC(AcquireNextImageKHR, handle); - ACQUIRE_PROC(CreateSwapchainKHR, handle); - ACQUIRE_PROC(DestroySwapchainKHR, handle); - ACQUIRE_PROC(GetSwapchainImagesKHR, handle); - ACQUIRE_PROC(QueuePresentKHR, handle); ACQUIRE_PROC(AllocateCommandBuffers, handle); + ACQUIRE_PROC(AllocateMemory, handle); ACQUIRE_PROC(BeginCommandBuffer, handle); + ACQUIRE_PROC(BindImageMemory, handle); ACQUIRE_PROC(CmdPipelineBarrier, handle); ACQUIRE_PROC(CreateCommandPool, handle); ACQUIRE_PROC(CreateFence, handle); + ACQUIRE_PROC(CreateImage, handle); ACQUIRE_PROC(CreateSemaphore, handle); + ACQUIRE_PROC(CreateSwapchainKHR, handle); ACQUIRE_PROC(DestroyCommandPool, handle); ACQUIRE_PROC(DestroyFence, handle); + ACQUIRE_PROC(DestroyImage, handle); ACQUIRE_PROC(DestroySemaphore, handle); + ACQUIRE_PROC(DestroySwapchainKHR, handle); ACQUIRE_PROC(DeviceWaitIdle, handle); ACQUIRE_PROC(EndCommandBuffer, handle); ACQUIRE_PROC(FreeCommandBuffers, handle); + ACQUIRE_PROC(FreeMemory, handle); ACQUIRE_PROC(GetDeviceQueue, handle); + ACQUIRE_PROC(GetImageMemoryRequirements, handle); + ACQUIRE_PROC(GetSwapchainImagesKHR, handle); + ACQUIRE_PROC(QueuePresentKHR, handle); ACQUIRE_PROC(QueueSubmit, handle); + ACQUIRE_PROC(QueueWaitIdle, handle); ACQUIRE_PROC(ResetCommandBuffer, handle); ACQUIRE_PROC(ResetFences, handle); ACQUIRE_PROC(WaitForFences, handle); - +#if OS_FUCHSIA + ACQUIRE_PROC(ExportDeviceMemoryMAGMA, handle); +#endif // OS_FUCHSIA device_ = {handle, nullptr}; return true; } diff --git a/engine/src/flutter/vulkan/vulkan_proc_table.h b/engine/src/flutter/vulkan/vulkan_proc_table.h index 039929880e..b9e68986db 100644 --- a/engine/src/flutter/vulkan/vulkan_proc_table.h +++ b/engine/src/flutter/vulkan/vulkan_proc_table.h @@ -65,12 +65,15 @@ class VulkanProcTable : public ftl::RefCountedThreadSafe { DEFINE_PROC(AcquireNextImageKHR); DEFINE_PROC(AllocateCommandBuffers); + DEFINE_PROC(AllocateMemory); DEFINE_PROC(BeginCommandBuffer); + DEFINE_PROC(BindImageMemory); DEFINE_PROC(CmdPipelineBarrier); DEFINE_PROC(CreateCommandPool); DEFINE_PROC(CreateDebugReportCallbackEXT); DEFINE_PROC(CreateDevice); DEFINE_PROC(CreateFence); + DEFINE_PROC(CreateImage); DEFINE_PROC(CreateInstance); DEFINE_PROC(CreateSemaphore); DEFINE_PROC(CreateSwapchainKHR); @@ -78,6 +81,7 @@ class VulkanProcTable : public ftl::RefCountedThreadSafe { DEFINE_PROC(DestroyDebugReportCallbackEXT); DEFINE_PROC(DestroyDevice); DEFINE_PROC(DestroyFence); + DEFINE_PROC(DestroyImage); DEFINE_PROC(DestroyInstance); DEFINE_PROC(DestroySemaphore); DEFINE_PROC(DestroySurfaceKHR); @@ -89,26 +93,30 @@ class VulkanProcTable : public ftl::RefCountedThreadSafe { DEFINE_PROC(EnumerateInstanceLayerProperties); DEFINE_PROC(EnumeratePhysicalDevices); DEFINE_PROC(FreeCommandBuffers); + DEFINE_PROC(FreeMemory); DEFINE_PROC(GetDeviceProcAddr); DEFINE_PROC(GetDeviceQueue); + DEFINE_PROC(GetImageMemoryRequirements); DEFINE_PROC(GetInstanceProcAddr); DEFINE_PROC(GetPhysicalDeviceFeatures); DEFINE_PROC(GetPhysicalDeviceQueueFamilyProperties); DEFINE_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR); DEFINE_PROC(GetPhysicalDeviceSurfaceFormatsKHR); DEFINE_PROC(GetPhysicalDeviceSurfacePresentModesKHR); + DEFINE_PROC(GetPhysicalDeviceSurfaceSupportKHR); DEFINE_PROC(GetSwapchainImagesKHR); DEFINE_PROC(QueuePresentKHR); DEFINE_PROC(QueueSubmit); + DEFINE_PROC(QueueWaitIdle); DEFINE_PROC(ResetCommandBuffer); DEFINE_PROC(ResetFences); DEFINE_PROC(WaitForFences); - DEFINE_PROC(GetPhysicalDeviceSurfaceSupportKHR); #if OS_ANDROID DEFINE_PROC(CreateAndroidSurfaceKHR); #endif // OS_ANDROID #if OS_FUCHSIA DEFINE_PROC(CreateMagmaSurfaceKHR); + DEFINE_PROC(ExportDeviceMemoryMAGMA); DEFINE_PROC(GetPhysicalDeviceMagmaPresentationSupportKHR); #endif // OS_FUCHSIA