Skip to content

Commit

Permalink
Re-land "Custom compositor layers must take into account the device p…
Browse files Browse the repository at this point in the history
…ixel ratio."

This reverts commit 6c2381d and applies iOS fixes.
  • Loading branch information
chinmaygarde authored Oct 17, 2019
1 parent 6a19886 commit c92613b
Show file tree
Hide file tree
Showing 15 changed files with 273 additions and 22 deletions.
4 changes: 3 additions & 1 deletion flow/embedded_views.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ class ExternalViewEmbedder {
// sets the stage for the next pre-roll.
virtual void CancelFrame() = 0;

virtual void BeginFrame(SkISize frame_size, GrContext* context) = 0;
virtual void BeginFrame(SkISize frame_size,
GrContext* context,
double device_pixel_ratio) = 0;

virtual void PrerollCompositeEmbeddedView(
int view_id,
Expand Down
7 changes: 7 additions & 0 deletions flow/layers/layer_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,15 @@ class LayerTree {
checkerboard_offscreen_layers_ = checkerboard;
}

void set_device_pixel_ratio(double device_pixel_ratio) {
device_pixel_ratio_ = device_pixel_ratio;
}

double device_pixel_ratio() const { return device_pixel_ratio_; }

private:
SkISize frame_size_ = SkISize::MakeEmpty(); // Physical pixels.
double device_pixel_ratio_ = 1.0;
std::shared_ptr<Layer> root_layer_;
fml::TimePoint build_start_;
fml::TimePoint build_finish_;
Expand Down
1 change: 1 addition & 0 deletions shell/common/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ void Engine::Render(std::unique_ptr<flutter::LayerTree> layer_tree) {
return;

layer_tree->set_frame_size(frame_size);
layer_tree->set_device_pixel_ratio(viewport_metrics_.device_pixel_ratio);
animator_->Render(std::move(layer_tree));
}

Expand Down
3 changes: 2 additions & 1 deletion shell/common/rasterizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ RasterStatus Rasterizer::DrawToSurface(flutter::LayerTree& layer_tree) {

if (external_view_embedder != nullptr) {
external_view_embedder->BeginFrame(layer_tree.frame_size(),
surface_->GetContext());
surface_->GetContext(),
layer_tree.device_pixel_ratio());
embedder_root_surface = external_view_embedder->GetRootSurface();
}

Expand Down
2 changes: 1 addition & 1 deletion shell/platform/darwin/ios/ios_surface_gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class IOSSurfaceGL final : public IOSSurface,
void CancelFrame() override;

// |ExternalViewEmbedder|
void BeginFrame(SkISize frame_size, GrContext* context) override;
void BeginFrame(SkISize frame_size, GrContext* context, double device_pixel_ratio) override;

// |ExternalViewEmbedder|
void PrerollCompositeEmbeddedView(int view_id,
Expand Down
2 changes: 1 addition & 1 deletion shell/platform/darwin/ios/ios_surface_gl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
}

// |ExternalViewEmbedder|
void IOSSurfaceGL::BeginFrame(SkISize frame_size, GrContext* context) {
void IOSSurfaceGL::BeginFrame(SkISize frame_size, GrContext* context, double device_pixel_ratio) {
FlutterPlatformViewsController* platform_views_controller = GetPlatformViewsController();
FML_CHECK(platform_views_controller != nullptr);
platform_views_controller->SetFrameSize(frame_size);
Expand Down
2 changes: 1 addition & 1 deletion shell/platform/darwin/ios/ios_surface_metal.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class IOSSurfaceMetal final : public IOSSurface,
void CancelFrame() override;

// |ExternalViewEmbedder|
void BeginFrame(SkISize frame_size, GrContext* context) override;
void BeginFrame(SkISize frame_size, GrContext* context, double device_pixel_ratio) override;

// |ExternalViewEmbedder|
void PrerollCompositeEmbeddedView(int view_id,
Expand Down
4 changes: 3 additions & 1 deletion shell/platform/darwin/ios/ios_surface_metal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@
[CATransaction commit];
}

void IOSSurfaceMetal::BeginFrame(SkISize frame_size, GrContext* context) {
void IOSSurfaceMetal::BeginFrame(SkISize frame_size,
GrContext* context,
double device_pixel_ratio) {
FlutterPlatformViewsController* platform_views_controller = GetPlatformViewsController();
FML_CHECK(platform_views_controller != nullptr);
platform_views_controller->SetFrameSize(frame_size);
Expand Down
2 changes: 1 addition & 1 deletion shell/platform/darwin/ios/ios_surface_software.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class IOSSurfaceSoftware final : public IOSSurface,
void CancelFrame() override;

// |ExternalViewEmbedder|
void BeginFrame(SkISize frame_size, GrContext* context) override;
void BeginFrame(SkISize frame_size, GrContext* context, double device_pixel_ratio) override;

// |ExternalViewEmbedder|
void PrerollCompositeEmbeddedView(int view_id,
Expand Down
4 changes: 3 additions & 1 deletion shell/platform/darwin/ios/ios_surface_software.mm
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@
}

// |ExternalViewEmbedder|
void IOSSurfaceSoftware::BeginFrame(SkISize frame_size, GrContext* context) {
void IOSSurfaceSoftware::BeginFrame(SkISize frame_size,
GrContext* context,
double device_pixel_ratio) {
FlutterPlatformViewsController* platform_views_controller = GetPlatformViewsController();
FML_CHECK(platform_views_controller != nullptr);
platform_views_controller->SetFrameSize(frame_size);
Expand Down
6 changes: 6 additions & 0 deletions shell/platform/embedder/embedder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,12 @@ FlutterEngineResult FlutterEngineSendWindowMetricsEvent(
metrics.physical_height = SAFE_ACCESS(flutter_metrics, height, 0.0);
metrics.device_pixel_ratio = SAFE_ACCESS(flutter_metrics, pixel_ratio, 1.0);

if (metrics.device_pixel_ratio <= 0.0) {
FML_LOG(ERROR) << "Device pixel ratio invalid: "
<< metrics.device_pixel_ratio;
return LOG_EMBEDDER_ERROR(kInvalidArguments);
}

return reinterpret_cast<flutter::EmbedderEngine*>(engine)->SetViewportMetrics(
std::move(metrics))
? kSuccess
Expand Down
42 changes: 30 additions & 12 deletions shell/platform/embedder/embedder_external_view_embedder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,12 @@ static SkISize TransformedSurfaceSize(const SkISize& size,

// |ExternalViewEmbedder|
void EmbedderExternalViewEmbedder::BeginFrame(SkISize frame_size,
GrContext* context) {
GrContext* context,
double device_pixel_ratio) {
Reset();

pending_frame_size_ = frame_size;
pending_device_pixel_ratio_ = device_pixel_ratio;
pending_surface_transformation_ = GetSurfaceTransformation();

const auto surface_size = TransformedSurfaceSize(
Expand Down Expand Up @@ -155,7 +158,8 @@ SkCanvas* EmbedderExternalViewEmbedder::CompositeEmbeddedView(int view_id) {
static FlutterLayer MakeBackingStoreLayer(
const SkISize& frame_size,
const FlutterBackingStore* store,
const SkMatrix& surface_transformation) {
const SkMatrix& surface_transformation,
double device_pixel_ratio) {
FlutterLayer layer = {};

layer.struct_size = sizeof(layer);
Expand All @@ -166,7 +170,9 @@ static FlutterLayer MakeBackingStoreLayer(
SkRect::MakeWH(frame_size.width(), frame_size.height());

const auto transformed_layer_bounds =
surface_transformation.mapRect(layer_bounds);
SkMatrix::Concat(surface_transformation,
SkMatrix::MakeScale(device_pixel_ratio))
.mapRect(layer_bounds);

layer.offset.x = transformed_layer_bounds.x();
layer.offset.y = transformed_layer_bounds.y();
Expand All @@ -190,7 +196,8 @@ static FlutterPlatformView MakePlatformView(
static FlutterLayer MakePlatformViewLayer(
const EmbeddedViewParams& params,
const FlutterPlatformView& platform_view,
const SkMatrix& surface_transformation) {
const SkMatrix& surface_transformation,
double device_pixel_ratio) {
FlutterLayer layer = {};

layer.struct_size = sizeof(layer);
Expand All @@ -204,7 +211,9 @@ static FlutterLayer MakePlatformViewLayer(
);

const auto transformed_layer_bounds =
surface_transformation.mapRect(layer_bounds);
SkMatrix::Concat(surface_transformation,
SkMatrix::MakeScale(device_pixel_ratio))
.mapRect(layer_bounds);

layer.offset.x = transformed_layer_bounds.x();
layer.offset.y = transformed_layer_bounds.y();
Expand Down Expand Up @@ -235,8 +244,11 @@ bool EmbedderExternalViewEmbedder::SubmitFrame(GrContext* context) {
{
// The root surface is expressed as a layer.
presented_layers.push_back(MakeBackingStoreLayer(
pending_frame_size_, root_render_target_->GetBackingStore(),
pending_surface_transformation_));
pending_frame_size_, // frame size
root_render_target_->GetBackingStore(), // backing store
pending_surface_transformation_, // surface transformation
pending_device_pixel_ratio_ // device pixel ratio
));
}

const auto surface_size = TransformedSurfaceSize(
Expand All @@ -262,9 +274,12 @@ bool EmbedderExternalViewEmbedder::SubmitFrame(GrContext* context) {
// The layers presented to the embedder will contain a back pointer to this
// struct. It is safe to deallocate when the embedder callback is done.
presented_platform_views[view_id] = MakePlatformView(view_id);
presented_layers.push_back(
MakePlatformViewLayer(params, presented_platform_views.at(view_id),
pending_surface_transformation_));
presented_layers.push_back(MakePlatformViewLayer(
params, // embedded view params
presented_platform_views.at(view_id), // platform view
pending_surface_transformation_, // surface transformation
pending_device_pixel_ratio_ // device pixel ratio
));

if (!pending_canvas_spies_.at(view_id)->DidDrawIntoCanvas()) {
// Nothing was drawn into the overlay canvas, we don't need to composite
Expand Down Expand Up @@ -312,8 +327,11 @@ bool EmbedderExternalViewEmbedder::SubmitFrame(GrContext* context) {
// Indicate a layer for the backing store containing contents rendered by
// Flutter.
presented_layers.push_back(MakeBackingStoreLayer(
pending_frame_size_, render_target->GetBackingStore(),
pending_surface_transformation_));
pending_frame_size_, // frame size
render_target->GetBackingStore(), // backing store
pending_surface_transformation_, // surface transformation
pending_device_pixel_ratio_ // device pixel ratio
));
}

{
Expand Down
5 changes: 4 additions & 1 deletion shell/platform/embedder/embedder_external_view_embedder.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ class EmbedderExternalViewEmbedder final : public ExternalViewEmbedder {
void CancelFrame() override;

// |ExternalViewEmbedder|
void BeginFrame(SkISize frame_size, GrContext* context) override;
void BeginFrame(SkISize frame_size,
GrContext* context,
double device_pixel_ratio) override;

// |ExternalViewEmbedder|
void PrerollCompositeEmbeddedView(
Expand Down Expand Up @@ -127,6 +129,7 @@ class EmbedderExternalViewEmbedder final : public ExternalViewEmbedder {
RegistryKey::Equal>;

SkISize pending_frame_size_ = SkISize::Make(0, 0);
double pending_device_pixel_ratio_ = 1.0;
SkMatrix pending_surface_transformation_;
std::map<ViewIdentifier, std::unique_ptr<SkPictureRecorder>>
pending_recorders_;
Expand Down
13 changes: 13 additions & 0 deletions shell/platform/embedder/fixtures/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,16 @@ void verify_b141980393() {
};
window.scheduleFrame();
}

@pragma('vm:entry-point')
void can_display_platform_view_with_pixel_ratio() {
window.onBeginFrame = (Duration duration) {
SceneBuilder builder = SceneBuilder();
builder.pushOffset(0.0, 20.0);
builder.addPlatformView(42, width: 400.0, height: 280.0);
builder.addPicture(Offset(0.0, 0.0), CreateSimplePicture());
builder.pop();
window.render(builder.build());
};
window.scheduleFrame();
}
Loading

0 comments on commit c92613b

Please sign in to comment.