From adef60fe5a884b7d2a1209e2a8cbf86a9f5ddcaf Mon Sep 17 00:00:00 2001 From: Jeff Brown Date: Mon, 24 Jul 2017 19:17:55 -0700 Subject: [PATCH] Use metrics provided by scene events. (flutter/engine#3922) Compute the necessary texture resolution using more accurate scaling information provided by Mozart scene node metrics events instead of the device pixel ratio provided by the Mozart view properties (which we might remove in the future). This allows us to allocate smaller textures when a Flutter view is being scaled down. --- engine/src/flutter/flow/export_node.cc | 2 - engine/src/flutter/flow/export_node.h | 1 - .../flutter/flow/layers/child_scene_layer.cc | 9 +- .../flutter/flow/layers/child_scene_layer.h | 11 +-- engine/src/flutter/flow/layers/layer_tree.cc | 9 +- engine/src/flutter/flow/layers/layer_tree.h | 8 ++ .../flow/layers/physical_model_layer.cc | 10 +-- .../flow/layers/physical_model_layer.h | 5 -- .../src/flutter/flow/scene_update_context.cc | 86 ++++++++++++------- .../src/flutter/flow/scene_update_context.h | 40 +++++---- engine/src/flutter/lib/ui/compositing.dart | 15 ++-- .../lib/ui/compositing/scene_builder.cc | 11 +-- .../lib/ui/compositing/scene_builder.h | 5 +- 13 files changed, 109 insertions(+), 103 deletions(-) diff --git a/engine/src/flutter/flow/export_node.cc b/engine/src/flutter/flow/export_node.cc index be4a1cea1d..a5462ac3c0 100644 --- a/engine/src/flutter/flow/export_node.cc +++ b/engine/src/flutter/flow/export_node.cc @@ -17,7 +17,6 @@ ExportNode::~ExportNode() { void ExportNode::Bind(SceneUpdateContext& context, mozart::client::ContainerNode& container, const SkPoint& offset, - float scale, bool hit_testable) { ftl::MutexLocker lock(&mutex_); @@ -29,7 +28,6 @@ void ExportNode::Bind(SceneUpdateContext& context, 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); diff --git a/engine/src/flutter/flow/export_node.h b/engine/src/flutter/flow/export_node.h index da46794188..7183e05ac3 100644 --- a/engine/src/flutter/flow/export_node.h +++ b/engine/src/flutter/flow/export_node.h @@ -34,7 +34,6 @@ class ExportNode : public ftl::RefCountedThreadSafe { void Bind(SceneUpdateContext& context, mozart::client::ContainerNode& container, const SkPoint& offset, - float scale, bool hit_testable); private: diff --git a/engine/src/flutter/flow/layers/child_scene_layer.cc b/engine/src/flutter/flow/layers/child_scene_layer.cc index b5f3d5dc12..b64875d76b 100644 --- a/engine/src/flutter/flow/layers/child_scene_layer.cc +++ b/engine/src/flutter/flow/layers/child_scene_layer.cc @@ -26,15 +26,8 @@ void ChildSceneLayer::UpdateScene(SceneUpdateContext& context) { // 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_); + context.AddChildScene(export_node_.get(), offset_, hit_testable_); } } diff --git a/engine/src/flutter/flow/layers/child_scene_layer.h b/engine/src/flutter/flow/layers/child_scene_layer.h index 9d66ad5b37..3ae15fa694 100644 --- a/engine/src/flutter/flow/layers/child_scene_layer.h +++ b/engine/src/flutter/flow/layers/child_scene_layer.h @@ -17,13 +17,7 @@ class ChildSceneLayer : public Layer { void set_offset(const SkPoint& offset) { offset_ = offset; } - void set_device_pixel_ratio(float device_pixel_ratio) { - device_pixel_ratio_ = device_pixel_ratio; - } - - void set_physical_size(const SkISize& physical_size) { - physical_size_ = physical_size; - } + void set_size(const SkSize& size) { size_ = size; } void set_export_node(ftl::RefPtr export_node) { export_node_ = std::move(export_node); @@ -39,8 +33,7 @@ class ChildSceneLayer : public Layer { private: SkPoint offset_; - float device_pixel_ratio_ = 1.0; - SkISize physical_size_; + SkSize size_; ftl::RefPtr export_node_; bool hit_testable_ = true; diff --git a/engine/src/flutter/flow/layers/layer_tree.cc b/engine/src/flutter/flow/layers/layer_tree.cc index 20dd342327..23716102ba 100644 --- a/engine/src/flutter/flow/layers/layer_tree.cc +++ b/engine/src/flutter/flow/layers/layer_tree.cc @@ -34,6 +34,7 @@ void LayerTree::Preroll(CompositorContext::ScopedFrame& frame, ignore_raster_cache ? nullptr : &frame.context().raster_cache(), frame.gr_context(), color_space, SkRect::MakeEmpty(), }; + root_layer_->Preroll(&context, SkMatrix::I()); } @@ -42,18 +43,20 @@ void LayerTree::UpdateScene(SceneUpdateContext& context, mozart::client::ContainerNode& container) { TRACE_EVENT0("flutter", "LayerTree::UpdateScene"); + SceneUpdateContext::Transform transform(context, 1.f / device_pixel_ratio_, + 1.f / device_pixel_ratio_, 1.f); SceneUpdateContext::Frame frame( context, SkRRect::MakeRect( - SkRect::MakeIWH(frame_size_.width(), frame_size_.height())), - SK_ColorTRANSPARENT, 0.f, 1.f, 1.f); + SkRect::MakeWH(frame_size_.width(), frame_size_.height())), + SK_ColorTRANSPARENT, 0.f); if (root_layer_->needs_system_composite()) { root_layer_->UpdateScene(context); } if (root_layer_->needs_painting()) { frame.AddPaintedLayer(root_layer_.get()); } - container.AddChild(frame.entity_node()); + container.AddChild(transform.entity_node()); } #endif diff --git a/engine/src/flutter/flow/layers/layer_tree.h b/engine/src/flutter/flow/layers/layer_tree.h index 1b7e8fcaca..e85f8165e5 100644 --- a/engine/src/flutter/flow/layers/layer_tree.h +++ b/engine/src/flutter/flow/layers/layer_tree.h @@ -31,6 +31,10 @@ class LayerTree { bool ignore_raster_cache = false); #if defined(OS_FUCHSIA) + void set_device_pixel_ratio(float device_pixel_ratio) { + device_pixel_ratio_ = device_pixel_ratio; + } + void UpdateScene(SceneUpdateContext& context, mozart::client::ContainerNode& container); #endif @@ -80,6 +84,10 @@ class LayerTree { bool checkerboard_raster_cache_images_; bool checkerboard_offscreen_layers_; +#if defined(OS_FUCHSIA) + float device_pixel_ratio_ = 1.f; +#endif + FTL_DISALLOW_COPY_AND_ASSIGN(LayerTree); }; diff --git a/engine/src/flutter/flow/layers/physical_model_layer.cc b/engine/src/flutter/flow/layers/physical_model_layer.cc index 7d7f9102ab..9ac3a565da 100644 --- a/engine/src/flutter/flow/layers/physical_model_layer.cc +++ b/engine/src/flutter/flow/layers/physical_model_layer.cc @@ -34,13 +34,6 @@ void PhysicalModelLayer::Preroll(PrerollContext* context, set_paint_bounds(bounds); #endif // defined(OS_FUCHSIA) } - -#if defined(OS_FUCHSIA) - if (needs_system_composite()) { - scale_x_ = matrix.getScaleX(); - scale_y_ = matrix.getScaleY(); - } -#endif // defined(OS_FUCHSIA) } #if defined(OS_FUCHSIA) @@ -48,8 +41,7 @@ void PhysicalModelLayer::Preroll(PrerollContext* context, void PhysicalModelLayer::UpdateScene(SceneUpdateContext& context) { FTL_DCHECK(needs_system_composite()); - SceneUpdateContext::Frame frame(context, rrect_, color_, elevation_, scale_x_, - scale_y_); + SceneUpdateContext::Frame frame(context, rrect_, color_, elevation_); for (auto& layer : layers()) { if (layer->needs_painting()) { frame.AddPaintedLayer(layer.get()); diff --git a/engine/src/flutter/flow/layers/physical_model_layer.h b/engine/src/flutter/flow/layers/physical_model_layer.h index 1c15f444d2..2f14c89b97 100644 --- a/engine/src/flutter/flow/layers/physical_model_layer.h +++ b/engine/src/flutter/flow/layers/physical_model_layer.h @@ -38,11 +38,6 @@ class PhysicalModelLayer : public ContainerLayer { float elevation_; SkColor color_; SkScalar device_pixel_ratio_; - -#if defined(OS_FUCHSIA) - SkScalar scale_x_; - SkScalar scale_y_; -#endif // defined(OS_FUCHSIA) }; } // namespace flow diff --git a/engine/src/flutter/flow/scene_update_context.cc b/engine/src/flutter/flow/scene_update_context.cc index 822851227d..1a741e19ac 100644 --- a/engine/src/flutter/flow/scene_update_context.cc +++ b/engine/src/flutter/flow/scene_update_context.cc @@ -21,19 +21,15 @@ SceneUpdateContext::~SceneUpdateContext() = default; 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); + export_node->Bind(*this, top_entity_->entity_node(), offset, hit_testable); } 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. @@ -73,6 +69,10 @@ void SceneUpdateContext::CreateFrame(mozart::client::EntityNode& entity_node, return; } + // Apply current metrics and transformation scale factors. + const float scale_x = metrics_->scale_x * top_scale_x_; + const float scale_y = metrics_->scale_y * top_scale_y_; + // 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; @@ -223,47 +223,69 @@ 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] // - ); + : Entity(context), + previous_scale_x_(context.top_scale_x_), + previous_scale_y_(context.top_scale_y_) { + if (!transform.isIdentity()) { + // 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() // + ); + context.top_scale_x_ *= decomposition.scale().x(); + context.top_scale_y_ *= decomposition.scale().y(); + + entity_node().SetRotation(decomposition.rotation().fData[0], // + decomposition.rotation().fData[1], // + decomposition.rotation().fData[2], // + decomposition.rotation().fData[3] // + ); + } } } -SceneUpdateContext::Transform::~Transform() = default; +SceneUpdateContext::Transform::Transform(SceneUpdateContext& context, + float scale_x, + float scale_y, + float scale_z) + : Entity(context), + previous_scale_x_(context.top_scale_x_), + previous_scale_y_(context.top_scale_y_) { + if (scale_x != 1.f || scale_y != 1.f || scale_z != 1.f) { + entity_node().SetScale(scale_x, scale_y, scale_z); + context.top_scale_x_ *= scale_x; + context.top_scale_y_ *= scale_y; + } +} + +SceneUpdateContext::Transform::~Transform() { + context().top_scale_x_ = previous_scale_x_; + context().top_scale_y_ = previous_scale_y_; +} SceneUpdateContext::Frame::Frame(SceneUpdateContext& context, const SkRRect& rrect, SkColor color, - float elevation, - SkScalar scale_x, - SkScalar scale_y) + float elevation) : 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); + if (elevation != 0.0) + 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_)); + context().CreateFrame(entity_node(), rrect_, color_, paint_bounds_, + std::move(paint_layers_)); } void SceneUpdateContext::Frame::AddPaintedLayer(Layer* layer) { diff --git a/engine/src/flutter/flow/scene_update_context.h b/engine/src/flutter/flow/scene_update_context.h index 539ceae29e..bfcd785409 100644 --- a/engine/src/flutter/flow/scene_update_context.h +++ b/engine/src/flutter/flow/scene_update_context.h @@ -52,13 +52,6 @@ class SceneUpdateContext { std::unique_ptr surface) = 0; }; - SceneUpdateContext(mozart::client::Session* session, - SurfaceProducer* surface_producer); - - ~SceneUpdateContext(); - - mozart::client::Session* session() { return session_; } - class Entity { public: Entity(SceneUpdateContext& context); @@ -85,7 +78,15 @@ class SceneUpdateContext { class Transform : public Entity { public: Transform(SceneUpdateContext& context, const SkMatrix& transform); + Transform(SceneUpdateContext& context, + float scale_x, + float scale_y, + float scale_z); ~Transform(); + + private: + float const previous_scale_x_; + float const previous_scale_y_; }; class Frame : public Entity { @@ -93,9 +94,7 @@ class SceneUpdateContext { Frame(SceneUpdateContext& context, const SkRRect& rrect, SkColor color, - float elevation, - SkScalar scale_x, - SkScalar scale_y); + float elevation); ~Frame(); void AddPaintedLayer(Layer* layer); @@ -103,16 +102,25 @@ class SceneUpdateContext { private: const SkRRect& rrect_; SkColor const color_; - SkScalar const scale_x_; - SkScalar const scale_y_; std::vector paint_layers_; SkRect paint_bounds_; }; + SceneUpdateContext(mozart::client::Session* session, + SurfaceProducer* surface_producer); + + ~SceneUpdateContext(); + + mozart::client::Session* session() { return session_; } + + bool has_metrics() const { return !!metrics_; } + void set_metrics(mozart2::MetricsPtr metrics) { + metrics_ = std::move(metrics); + } + 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 @@ -139,8 +147,6 @@ class SceneUpdateContext { 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, @@ -158,10 +164,14 @@ class SceneUpdateContext { std::vector paint_layers); Entity* top_entity_ = nullptr; + float top_scale_x_ = 1.f; + float top_scale_y_ = 1.f; mozart::client::Session* const session_; SurfaceProducer* const surface_producer_; + mozart2::MetricsPtr metrics_; + std::vector paint_tasks_; FTL_DISALLOW_COPY_AND_ASSIGN(SceneUpdateContext); diff --git a/engine/src/flutter/lib/ui/compositing.dart b/engine/src/flutter/lib/ui/compositing.dart index b97c12d08a..fef66bcb9f 100644 --- a/engine/src/flutter/lib/ui/compositing.dart +++ b/engine/src/flutter/lib/ui/compositing.dart @@ -201,25 +201,22 @@ class SceneBuilder extends NativeFieldWrapperClass2 { /// for this application. void addChildScene({ Offset offset: Offset.zero, - double devicePixelRatio: 1.0, - int physicalWidth: 0, - int physicalHeight: 0, + double width: 0.0, + double height: 0.0, SceneHost sceneHost, bool hitTestable: true }) { _addChildScene(offset.dx, offset.dy, - devicePixelRatio, - physicalWidth, - physicalHeight, + width, + height, sceneHost, hitTestable); } void _addChildScene(double dx, double dy, - double devicePixelRatio, - int physicalWidth, - int physicalHeight, + double width, + double height, SceneHost sceneHost, bool hitTestable) native "SceneBuilder_addChildScene"; diff --git a/engine/src/flutter/lib/ui/compositing/scene_builder.cc b/engine/src/flutter/lib/ui/compositing/scene_builder.cc index cb8b158e26..b5961c8bfa 100644 --- a/engine/src/flutter/lib/ui/compositing/scene_builder.cc +++ b/engine/src/flutter/lib/ui/compositing/scene_builder.cc @@ -224,24 +224,21 @@ void SceneBuilder::addPicture(double dx, void SceneBuilder::addChildScene(double dx, double dy, - double devicePixelRatio, - int physicalWidth, - int physicalHeight, + double width, + double height, SceneHost* sceneHost, bool hitTestable) { #if defined(OS_FUCHSIA) if (!m_currentLayer) return; - SkRect sceneRect = SkRect::MakeXYWH(dx, dy, physicalWidth / devicePixelRatio, - physicalHeight / devicePixelRatio); + SkRect sceneRect = SkRect::MakeXYWH(dx, dy, width, height); if (!SkRect::Intersects(sceneRect, m_cullRects.top())) return; std::unique_ptr layer(new flow::ChildSceneLayer()); layer->set_offset(SkPoint::Make(dx, dy)); - layer->set_device_pixel_ratio(devicePixelRatio); - layer->set_physical_size(SkISize::Make(physicalWidth, physicalHeight)); + layer->set_size(SkSize::Make(width, height)); layer->set_export_node(sceneHost->exportNode()); layer->set_hit_testable(hitTestable); m_currentLayer->Add(std::move(layer)); diff --git a/engine/src/flutter/lib/ui/compositing/scene_builder.h b/engine/src/flutter/lib/ui/compositing/scene_builder.h index 95d43b8087..43008d41f3 100644 --- a/engine/src/flutter/lib/ui/compositing/scene_builder.h +++ b/engine/src/flutter/lib/ui/compositing/scene_builder.h @@ -59,9 +59,8 @@ class SceneBuilder : public ftl::RefCountedThreadSafe, void addPicture(double dx, double dy, Picture* picture, int hints); void addChildScene(double dx, double dy, - double devicePixelRatio, - int physicalWidth, - int physicalHeight, + double width, + double height, SceneHost* sceneHost, bool hitTestable);