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.
This commit is contained in:
Jeff Brown
2017-07-24 19:17:55 -07:00
committed by GitHub
parent d7fd10f9f6
commit adef60fe5a
13 changed files with 109 additions and 103 deletions

View File

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

View File

@@ -34,7 +34,6 @@ class ExportNode : public ftl::RefCountedThreadSafe<ExportNode> {
void Bind(SceneUpdateContext& context,
mozart::client::ContainerNode& container,
const SkPoint& offset,
float scale,
bool hit_testable);
private:

View File

@@ -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_);
}
}

View File

@@ -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<ExportNode> 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<ExportNode> export_node_;
bool hit_testable_ = true;

View File

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

View File

@@ -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);
};

View File

@@ -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());

View File

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

View File

@@ -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<Layer*> 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) {

View File

@@ -52,13 +52,6 @@ class SceneUpdateContext {
std::unique_ptr<SurfaceProducerSurface> 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<Layer*> 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<Layer*> paint_layers);
void SetShapeTextureOrColor(mozart::client::ShapeNode& shape_node,
@@ -158,10 +164,14 @@ class SceneUpdateContext {
std::vector<Layer*> 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<PaintTask> paint_tasks_;
FTL_DISALLOW_COPY_AND_ASSIGN(SceneUpdateContext);

View File

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

View File

@@ -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<flow::ChildSceneLayer> 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));

View File

@@ -59,9 +59,8 @@ class SceneBuilder : public ftl::RefCountedThreadSafe<SceneBuilder>,
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);