Add DPR To Flatland Embedder (flutter/engine#36449)

* add dpr field to flatland

* format

* more dpr updates

* add inverse

* fix error

* fix error

* fix formatting

* formatting again

* one more formatting fix

* update metrics

* fix build errors

* formatting

* test

* formatting

* test

* test

* fix typo

* clean up a bit

* format

* fix metrics

* remove line

* fix test

* remove prints

* fix formatting

* update checks

* format
This commit is contained in:
cfontas
2022-09-30 20:37:14 +00:00
committed by GitHub
parent 65a75969d4
commit b203597bab
5 changed files with 38 additions and 14 deletions

View File

@@ -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_) {

View File

@@ -190,6 +190,7 @@ class FlatlandExternalViewEmbedder final
std::vector<EmbedderLayerId> frame_composition_order_;
std::vector<fuchsia::ui::composition::TransformId> child_transforms_;
SkISize frame_size_ = SkISize::Make(0, 0);
float frame_dpr_ = 1.f;
FML_DISALLOW_COPY_AND_ASSIGN(FlatlandExternalViewEmbedder);
};

View File

@@ -75,10 +75,16 @@ void FlatlandPlatformView::OnGetLayout(
view_logical_size_ = {static_cast<float>(info.logical_size().width),
static_cast<float>(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

View File

@@ -211,7 +211,8 @@ Matcher<FakeGraph> IsFlutterGraph(
parent_viewport_watcher,
const fuchsia::ui::views::ViewportCreationToken& viewport_creation_token,
const fuchsia::ui::views::ViewRef& view_ref,
std::vector<Matcher<std::shared_ptr<FakeTransform>>> layer_matchers = {}) {
std::vector<Matcher<std::shared_ptr<FakeTransform>>> 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<FakeGraph> 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<uint32_t>(frame_size_signed.width()),
static_cast<uint32_t>(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();

View File

@@ -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