未验证 提交 9d716751 编写于 作者: L liyuqian 提交者: GitHub

Ensure OpacityLayer to have a single child (#8362)

It ensures that every OpacityLayer can be optimized by retained
rendering.

Tested with `flutter test --local-engine=host_debug_unopt` in
flutter/flutter/packages/flutter.

Related issues: #21756 #23535
上级 131cc625
......@@ -35,6 +35,9 @@ class ContainerLayer : public Layer {
void UpdateSceneChildren(SceneUpdateContext& context);
#endif // defined(OS_FUCHSIA)
// For OpacityLayer to restructure to have a single child.
void ClearChildren() { layers_.clear(); }
private:
std::vector<std::shared_ptr<Layer>> layers_;
......
......@@ -4,18 +4,38 @@
#include "flutter/flow/layers/opacity_layer.h"
#include "flutter/flow/layers/transform_layer.h"
namespace flow {
OpacityLayer::OpacityLayer() = default;
OpacityLayer::~OpacityLayer() = default;
void OpacityLayer::EnsureSingleChild() {
FML_DCHECK(layers().size() > 0); // OpacityLayer should never be a leaf
if (layers().size() == 1) {
return;
}
auto new_child = std::make_shared<flow::TransformLayer>();
for (auto& child : layers()) {
new_child->Add(child);
}
ClearChildren();
Add(new_child);
}
void OpacityLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {
EnsureSingleChild();
SkMatrix child_matrix = matrix;
child_matrix.postTranslate(offset_.fX, offset_.fY);
ContainerLayer::Preroll(context, child_matrix);
set_paint_bounds(paint_bounds().makeOffset(offset_.fX, offset_.fY));
if (context->raster_cache && layers().size() == 1 &&
// See |EnsureSingleChild|.
FML_DCHECK(layers().size() == 1);
if (context->raster_cache &&
SkRect::Intersects(context->cull_rect, paint_bounds())) {
Layer* child = layers()[0].get();
SkMatrix ctm = child_matrix;
......@@ -41,12 +61,14 @@ void OpacityLayer::Paint(PaintContext& context) const {
context.leaf_nodes_canvas->getTotalMatrix()));
#endif
// See |EnsureSingleChild|.
FML_DCHECK(layers().size() == 1);
// Embedded platform views are changing the canvas in the middle of the paint
// traversal. To make sure we paint on the right canvas, when the embedded
// platform views preview is enabled (context.view_embedded is not null) we
// don't use the cache.
if (context.view_embedder == nullptr && layers().size() == 1 &&
context.raster_cache) {
if (context.view_embedder == nullptr && context.raster_cache) {
const SkMatrix& ctm = context.leaf_nodes_canvas->getTotalMatrix();
RasterCacheResult child_cache =
context.raster_cache->Get(layers()[0].get(), ctm);
......
......@@ -28,6 +28,16 @@ class OpacityLayer : public ContainerLayer {
int alpha_;
SkPoint offset_;
// Restructure (if necessary) OpacityLayer to have only one child.
//
// This is needed to ensure that retained rendering can always be applied to
// save the costly saveLayer.
//
// If there are multiple children, this creates a new identity TransformLayer,
// sets all children to be the TransformLayer's children, and sets that
// TransformLayer as the single child of this OpacityLayer.
void EnsureSingleChild();
FML_DISALLOW_COPY_AND_ASSIGN(OpacityLayer);
};
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册