diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_external_view_embedder.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_external_view_embedder.cc index be43799f7a..c2538feb3a 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_external_view_embedder.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_external_view_embedder.cc @@ -104,8 +104,7 @@ void FlatlandExternalViewEmbedder::BeginFrame( // Reset for new frame. Reset(); frame_size_ = frame_size; - - // TODO(fxbug.dev/94000): Handle device pixel ratio. + frame_dpr_ = device_pixel_ratio; // Create the root layer. frame_layers_.emplace( @@ -180,6 +179,10 @@ void FlatlandExternalViewEmbedder::SubmitFrame( { TRACE_EVENT0("flutter", "SubmitLayers"); + // First re-scale everything according to the DPR. + const float inv_dpr = 1.0f / frame_dpr_; + flatland_->flatland()->SetScale(root_transform_id_, {inv_dpr, inv_dpr}); + size_t flatland_layer_index = 0; for (const auto& layer_id : frame_composition_order_) { const auto& layer = frame_layers_.find(layer_id); @@ -463,6 +466,7 @@ void FlatlandExternalViewEmbedder::Reset() { frame_layers_.clear(); frame_composition_order_.clear(); frame_size_ = SkISize::Make(0, 0); + frame_dpr_ = 1.f; // Clear all children from root. for (const auto& transform : child_transforms_) { diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_external_view_embedder.h b/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_external_view_embedder.h index b7019be4f5..a32e66fa4f 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_external_view_embedder.h +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_external_view_embedder.h @@ -190,6 +190,7 @@ class FlatlandExternalViewEmbedder final std::vector frame_composition_order_; std::vector child_transforms_; SkISize frame_size_ = SkISize::Make(0, 0); + float frame_dpr_ = 1.f; FML_DISALLOW_COPY_AND_ASSIGN(FlatlandExternalViewEmbedder); }; diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_platform_view.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_platform_view.cc index af7b9ee35f..6b9a5dfbd6 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_platform_view.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_platform_view.cc @@ -75,10 +75,16 @@ void FlatlandPlatformView::OnGetLayout( view_logical_size_ = {static_cast(info.logical_size().width), static_cast(info.logical_size().height)}; - // TODO(fxbug.dev/94000): Set device pixel ratio. + float pixel_ratio = 1.0f; + if (info.has_device_pixel_ratio()) { + // Flatland returns a Vec2 for DPR but both values should be identical. + FML_DCHECK(info.device_pixel_ratio().x == info.device_pixel_ratio().y); + view_pixel_ratio_ = info.device_pixel_ratio().x; + pixel_ratio = *view_pixel_ratio_; + } SetViewportMetrics({ - 1, // device_pixel_ratio + pixel_ratio, // device_pixel_ratio view_logical_size_.value()[0], // physical_width view_logical_size_.value()[1], // physical_height 0.0f, // physical_padding_top diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/tests/flatland_external_view_embedder_unittests.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/tests/flatland_external_view_embedder_unittests.cc index 0298537cc2..fac48844c7 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/tests/flatland_external_view_embedder_unittests.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/tests/flatland_external_view_embedder_unittests.cc @@ -211,7 +211,8 @@ Matcher IsFlutterGraph( parent_viewport_watcher, const fuchsia::ui::views::ViewportCreationToken& viewport_creation_token, const fuchsia::ui::views::ViewRef& view_ref, - std::vector>> layer_matchers = {}) { + std::vector>> layer_matchers = {}, + fuchsia::math::VecF scale = FakeTransform::kDefaultScale) { auto viewport_token_koids = GetKoids(viewport_creation_token); auto view_ref_koids = GetKoids(view_ref); auto watcher_koids = GetKoids(parent_viewport_watcher); @@ -220,7 +221,7 @@ Matcher IsFlutterGraph( /*content_map*/ _, /*transform_map*/ _, Pointee(FieldsAre( /*id*/ _, FakeTransform::kDefaultTranslation, - FakeTransform::kDefaultScale, FakeTransform::kDefaultOrientation, + /*scale*/ scale, FakeTransform::kDefaultOrientation, /*clip_bounds*/ _, FakeTransform::kDefaultOpacity, /*children*/ ElementsAreArray(layer_matchers), /*content*/ Eq(nullptr), /*num_hit_regions*/ _)), @@ -524,7 +525,7 @@ TEST_F(FlatlandExternalViewEmbedderTest, SceneWithOneView) { const int kOpacity = 200; const float kOpacityFloat = 200 / 255.0f; - const fuchsia::math::VecF kScale{0.5f, 0.9f}; + const fuchsia::math::VecF kScale{3.0f, 4.0f}; auto matrix = SkMatrix::I(); matrix.setScaleX(kScale.x); @@ -550,13 +551,17 @@ TEST_F(FlatlandExternalViewEmbedderTest, SceneWithOneView) { child_view_id, child_view_occlusion_hint, /*hit_testable=*/false, /*focusable=*/false); + // We must take into account the effect of DPR on the view scale. + const float kDPR = 2.0f; + const float kInvDPR = 1.f / kDPR; + // Draw the scene. The scene graph shouldn't change yet. const SkISize frame_size_signed = SkISize::Make(512, 512); const fuchsia::math::SizeU frame_size{ static_cast(frame_size_signed.width()), static_cast(frame_size_signed.height())}; DrawFrameWithView( - external_view_embedder, frame_size_signed, 1.f, child_view_id, + external_view_embedder, frame_size_signed, kDPR, child_view_id, child_view_params, [](SkCanvas* canvas) { const SkSize canvas_size = SkSize::Make(canvas->imageInfo().width(), @@ -588,6 +593,7 @@ TEST_F(FlatlandExternalViewEmbedderTest, SceneWithOneView) { loop().RunUntilIdle(); fake_flatland().FireOnNextFrameBeginEvent(WithPresentCredits(1u)); loop().RunUntilIdle(); + EXPECT_THAT( fake_flatland().graph(), IsFlutterGraph( @@ -595,7 +601,8 @@ TEST_F(FlatlandExternalViewEmbedderTest, SceneWithOneView) { {IsImageLayer(frame_size, kFirstLayerBlendMode, 1), IsViewportLayer(child_view_token, child_view_size, child_view_inset, {0, 0}, kScale, kOpacityFloat), - IsImageLayer(frame_size, kUpperLayerBlendMode, 1)})); + IsImageLayer(frame_size, kUpperLayerBlendMode, 1)}, + {kInvDPR, kInvDPR})); // Destroy the view. The scene graph shouldn't change yet. external_view_embedder.DestroyView( @@ -607,7 +614,8 @@ TEST_F(FlatlandExternalViewEmbedderTest, SceneWithOneView) { {IsImageLayer(frame_size, kFirstLayerBlendMode, 1), IsViewportLayer(child_view_token, child_view_size, child_view_inset, {0, 0}, kScale, kOpacityFloat), - IsImageLayer(frame_size, kUpperLayerBlendMode, 1)})); + IsImageLayer(frame_size, kUpperLayerBlendMode, 1)}, + {kInvDPR, kInvDPR})); // Draw another frame without the view. The scene graph shouldn't change yet. DrawSimpleFrame( @@ -629,7 +637,8 @@ TEST_F(FlatlandExternalViewEmbedderTest, SceneWithOneView) { {IsImageLayer(frame_size, kFirstLayerBlendMode, 1), IsViewportLayer(child_view_token, child_view_size, child_view_inset, {0, 0}, kScale, kOpacityFloat), - IsImageLayer(frame_size, kUpperLayerBlendMode, 1)})); + IsImageLayer(frame_size, kUpperLayerBlendMode, 1)}, + {kInvDPR, kInvDPR})); // Pump the message loop. The scene updates should propagate to flatland. loop().RunUntilIdle(); diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/tests/flatland_platform_view_unittest.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/tests/flatland_platform_view_unittest.cc index be5b4e32f1..cde88fd84d 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/tests/flatland_platform_view_unittest.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/tests/flatland_platform_view_unittest.cc @@ -290,11 +290,14 @@ class MockParentViewportWatcher } } - void SetLayout(uint32_t logical_size_x, uint32_t logical_size_y) { + void SetLayout(uint32_t logical_size_x, + uint32_t logical_size_y, + float DPR = 1.0) { ::fuchsia::math::SizeU logical_size; logical_size.width = logical_size_x; logical_size.height = logical_size_y; layout_.set_logical_size(logical_size); + layout_.set_device_pixel_ratio({DPR, DPR}); if (pending_callback_valid_) { pending_layout_callback_(std::move(layout_)); @@ -649,6 +652,7 @@ TEST_F(FlatlandPlatformViewTests, CreateSurfaceTest) { // MetricsEvents sent to it via FIDL, correctly parses the metrics it receives, // and calls the SetViewportMetrics callback with the appropriate parameters. TEST_F(FlatlandPlatformViewTests, SetViewportMetrics) { + constexpr float kDPR = 2; constexpr uint32_t width = 640; constexpr uint32_t height = 480; @@ -665,10 +669,10 @@ TEST_F(FlatlandPlatformViewTests, SetViewportMetrics) { RunLoopUntilIdle(); EXPECT_EQ(delegate.metrics(), flutter::ViewportMetrics()); - watcher.SetLayout(width, height); + watcher.SetLayout(width, height, kDPR); RunLoopUntilIdle(); EXPECT_EQ(delegate.metrics(), - flutter::ViewportMetrics(1.0, width, height, -1.0)); + flutter::ViewportMetrics(kDPR, width, height, -1.0)); } // This test makes sure that the PlatformView correctly registers semantics