Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions flow/compositor_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
namespace flutter {

std::optional<SkRect> FrameDamage::ComputeClipRect(
flutter::LayerTree& layer_tree) {
flutter::LayerTree& layer_tree,
bool has_raster_cache) {
if (layer_tree.root_layer()) {
PaintRegionMap empty_paint_region_map;
DiffContext context(layer_tree.frame_size(),
layer_tree.device_pixel_ratio(),
layer_tree.paint_region_map(),
prev_layer_tree_ ? prev_layer_tree_->paint_region_map()
: empty_paint_region_map);
: empty_paint_region_map,
has_raster_cache);
context.PushCullRect(SkRect::MakeIWH(layer_tree.frame_size().width(),
layer_tree.frame_size().height()));
{
Expand Down Expand Up @@ -119,7 +121,9 @@ RasterStatus CompositorContext::ScopedFrame::Raster(
TRACE_EVENT0("flutter", "CompositorContext::ScopedFrame::Raster");

std::optional<SkRect> clip_rect =
frame_damage ? frame_damage->ComputeClipRect(layer_tree) : std::nullopt;
frame_damage
? frame_damage->ComputeClipRect(layer_tree, !ignore_raster_cache)
: std::nullopt;

bool root_needs_readback = layer_tree.Preroll(
*this, ignore_raster_cache, clip_rect ? *clip_rect : kGiantRect);
Expand Down
3 changes: 2 additions & 1 deletion flow/compositor_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ class FrameDamage {
// If previous layer tree is not specified, clip rect will be nullopt,
// but the paint region of layer_tree will be calculated so that it can be
// used for diffing of subsequent frames.
std::optional<SkRect> ComputeClipRect(flutter::LayerTree& layer_tree);
std::optional<SkRect> ComputeClipRect(flutter::LayerTree& layer_tree,
bool has_raster_cache);

// See Damage::frame_damage.
std::optional<SkIRect> GetFrameDamage() const {
Expand Down
6 changes: 4 additions & 2 deletions flow/diff_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ namespace flutter {
DiffContext::DiffContext(SkISize frame_size,
double frame_device_pixel_ratio,
PaintRegionMap& this_frame_paint_region_map,
const PaintRegionMap& last_frame_paint_region_map)
const PaintRegionMap& last_frame_paint_region_map,
bool has_raster_cache)
: rects_(std::make_shared<std::vector<SkRect>>()),
frame_size_(frame_size),
frame_device_pixel_ratio_(frame_device_pixel_ratio),
this_frame_paint_region_map_(this_frame_paint_region_map),
last_frame_paint_region_map_(last_frame_paint_region_map) {}
last_frame_paint_region_map_(last_frame_paint_region_map),
has_raster_cache_(has_raster_cache) {}

void DiffContext::BeginSubtree() {
state_stack_.push_back(state_);
Expand Down
9 changes: 8 additions & 1 deletion flow/diff_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class DiffContext {
explicit DiffContext(SkISize frame_size,
double device_pixel_aspect_ratio,
PaintRegionMap& this_frame_paint_region_map,
const PaintRegionMap& last_frame_paint_region_map);
const PaintRegionMap& last_frame_paint_region_map,
bool has_raster_cache);

// Starts a new subtree.
void BeginSubtree();
Expand Down Expand Up @@ -156,6 +157,11 @@ class DiffContext {
// frame layer tree.
PaintRegion GetOldLayerPaintRegion(const Layer* layer) const;

// Whether or not a raster cache is being used. If so, we must snap
// all transformations to physical pixels if the layer may be raster
// cached.
bool has_raster_cache() const { return has_raster_cache_; }

class Statistics {
public:
// Picture replaced by different picture
Expand Down Expand Up @@ -223,6 +229,7 @@ class DiffContext {

PaintRegionMap& this_frame_paint_region_map_;
const PaintRegionMap& last_frame_paint_region_map_;
bool has_raster_cache_;

void AddDamage(const SkRect& rect);

Expand Down
14 changes: 13 additions & 1 deletion flow/layers/clip_shape_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "flutter/flow/layers/cacheable_layer.h"
#include "flutter/flow/layers/container_layer.h"
#include "flutter/flow/paint_utils.h"
#include "flutter/flow/raster_cache_util.h"

namespace flutter {

Expand All @@ -32,6 +33,10 @@ class ClipShapeLayer : public CacheableContainerLayer {
context->MarkSubtreeDirty(context->GetOldLayerPaintRegion(old_layer));
}
}
if (UsesSaveLayer() && context->has_raster_cache()) {
context->SetTransform(
RasterCacheUtil::GetIntegralTransCTM(context->GetTransform()));
}
if (context->PushCullRect(clip_shape_bounds())) {
DiffChildren(context, prev);
}
Expand All @@ -45,12 +50,16 @@ class ClipShapeLayer : public CacheableContainerLayer {
if (!context->cull_rect.intersect(clip_shape_bounds())) {
context->cull_rect.setEmpty();
}
SkMatrix child_matrix = matrix;
if (context->raster_cache && uses_save_layer) {
child_matrix = RasterCacheUtil::GetIntegralTransCTM(child_matrix);
}
// We can use the raster_cache for children only when the use_save_layer is
// true so if use_save_layer is false we pass the layer_raster_item is
// nullptr which mean we don't do raster cache logic.
AutoCache cache =
AutoCache(uses_save_layer ? layer_raster_cache_item_.get() : nullptr,
context, matrix);
context, child_matrix);

Layer::AutoPrerollSaveLayerState save =
Layer::AutoPrerollSaveLayerState::Create(context, UsesSaveLayer());
Expand Down Expand Up @@ -89,6 +98,9 @@ class ClipShapeLayer : public CacheableContainerLayer {

AutoCachePaint cache_paint(context);
if (context.raster_cache) {
context.internal_nodes_canvas->setMatrix(
RasterCacheUtil::GetIntegralTransCTM(
context.leaf_nodes_canvas->getTotalMatrix()));
if (layer_raster_cache_item_->Draw(context, cache_paint.sk_paint())) {
return;
}
Expand Down
15 changes: 14 additions & 1 deletion flow/layers/color_filter_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ void ColorFilterLayer::Diff(DiffContext* context, const Layer* old_layer) {
}
}

if (context->has_raster_cache()) {
context->SetTransform(
RasterCacheUtil::GetIntegralTransCTM(context->GetTransform()));
}

DiffChildren(context, prev);

context->SetLayerPaintRegion(this, context->CurrentSubtreeRegion());
Expand All @@ -38,7 +43,12 @@ void ColorFilterLayer::Preroll(PrerollContext* context,
Layer::AutoPrerollSaveLayerState::Create(context);
AutoCache cache = AutoCache(layer_raster_cache_item_.get(), context, matrix);

ContainerLayer::Preroll(context, matrix);
SkMatrix child_matrix = matrix;
if (context->raster_cache) {
child_matrix = RasterCacheUtil::GetIntegralTransCTM(child_matrix);
}

ContainerLayer::Preroll(context, child_matrix);
// We always use a saveLayer (or a cached rendering), so we
// can always apply opacity in those cases.
context->subtree_can_inherit_opacity = true;
Expand All @@ -49,6 +59,9 @@ void ColorFilterLayer::Paint(PaintContext& context) const {
FML_DCHECK(needs_painting(context));

if (context.raster_cache) {
context.internal_nodes_canvas->setMatrix(
RasterCacheUtil::GetIntegralTransCTM(
context.leaf_nodes_canvas->getTotalMatrix()));
AutoCachePaint cache_paint(context);
if (layer_raster_cache_item_->IsCacheChildren()) {
cache_paint.setColorFilter(filter_.get());
Expand Down
14 changes: 13 additions & 1 deletion flow/layers/display_list_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ void DisplayListLayer::Diff(DiffContext* context, const Layer* old_layer) {
#endif
}
context->PushTransform(SkMatrix::Translate(offset_.x(), offset_.y()));
if (context->has_raster_cache()) {
context->SetTransform(
RasterCacheUtil::GetIntegralTransCTM(context->GetTransform()));
}
context->AddLayerBounds(display_list()->bounds());
context->SetLayerPaintRegion(this, context->CurrentSubtreeRegion());
}
Expand Down Expand Up @@ -95,9 +99,13 @@ void DisplayListLayer::Preroll(PrerollContext* context,
const SkMatrix& matrix) {
TRACE_EVENT0("flutter", "DisplayListLayer::Preroll");
DisplayList* disp_list = display_list();
SkMatrix child_matrix = matrix;
if (context->raster_cache) {
child_matrix = RasterCacheUtil::GetIntegralTransCTM(child_matrix);
}

AutoCache cache =
AutoCache(display_list_raster_cache_item_.get(), context, matrix);
AutoCache(display_list_raster_cache_item_.get(), context, child_matrix);
if (disp_list->can_apply_group_opacity()) {
context->subtree_can_inherit_opacity = true;
}
Expand All @@ -111,6 +119,10 @@ void DisplayListLayer::Paint(PaintContext& context) const {

SkAutoCanvasRestore save(context.leaf_nodes_canvas, true);
context.leaf_nodes_canvas->translate(offset_.x(), offset_.y());
if (context.raster_cache) {
context.leaf_nodes_canvas->setMatrix(RasterCacheUtil::GetIntegralTransCTM(
context.leaf_nodes_canvas->getTotalMatrix()));
}

if (context.raster_cache && display_list_raster_cache_item_) {
AutoCachePaint cache_paint(context);
Expand Down
17 changes: 16 additions & 1 deletion flow/layers/display_list_layer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,25 @@ TEST_F(DisplayListLayerDiffTest, FractionalTranslation) {
tree1.root()->Add(
CreateDisplayListLayer(display_list, SkPoint::Make(0.5, 0.5)));

auto damage = DiffLayerTree(tree1, MockLayerTree());
auto damage =
DiffLayerTree(tree1, MockLayerTree(), SkIRect::MakeEmpty(), 0, 0,
/*use_raster_cache=*/false);
EXPECT_EQ(damage.frame_damage, SkIRect::MakeLTRB(10, 10, 61, 61));
}

TEST_F(DisplayListLayerDiffTest, FractionalTranslationWithRasterCache) {
auto display_list = CreateDisplayList(SkRect::MakeLTRB(10, 10, 60, 60), 1);

MockLayerTree tree1;
tree1.root()->Add(
CreateDisplayListLayer(display_list, SkPoint::Make(0.5, 0.5)));

auto damage =
DiffLayerTree(tree1, MockLayerTree(), SkIRect::MakeEmpty(), 0, 0,
/*use_raster_cache=*/true);
EXPECT_EQ(damage.frame_damage, SkIRect::MakeLTRB(11, 11, 61, 61));
}

TEST_F(DisplayListLayerDiffTest, DisplayListCompare) {
MockLayerTree tree1;
auto display_list1 = CreateDisplayList(SkRect::MakeLTRB(10, 10, 60, 60), 1);
Expand Down
16 changes: 14 additions & 2 deletions flow/layers/image_filter_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ void ImageFilterLayer::Diff(DiffContext* context, const Layer* old_layer) {
}
}

if (context->has_raster_cache()) {
context->SetTransform(
RasterCacheUtil::GetIntegralTransCTM(context->GetTransform()));
}

if (filter_) {
auto filter = filter_->makeWithLocalMatrix(context->GetTransform());
if (filter) {
Expand All @@ -51,7 +56,11 @@ void ImageFilterLayer::Preroll(PrerollContext* context,
AutoCache cache = AutoCache(layer_raster_cache_item_.get(), context, matrix);

SkRect child_bounds = SkRect::MakeEmpty();
PrerollChildren(context, matrix, &child_bounds);
SkMatrix child_matrix = matrix;
if (context->raster_cache) {
child_matrix = RasterCacheUtil::GetIntegralTransCTM(child_matrix);
}
PrerollChildren(context, child_matrix, &child_bounds);

// We always paint with a saveLayer (or a cached rendering),
// so we can always apply opacity in any of those cases.
Expand All @@ -74,7 +83,7 @@ void ImageFilterLayer::Preroll(PrerollContext* context,
// So in here we reset the LayerRasterCacheItem cache state.
layer_raster_cache_item_->MarkNotCacheChildren();

transformed_filter_ = filter_->makeWithLocalMatrix(matrix);
transformed_filter_ = filter_->makeWithLocalMatrix(child_matrix);
if (transformed_filter_) {
layer_raster_cache_item_->MarkCacheChildren();
}
Expand All @@ -86,6 +95,9 @@ void ImageFilterLayer::Paint(PaintContext& context) const {

AutoCachePaint cache_paint(context);
if (context.raster_cache) {
context.internal_nodes_canvas->setMatrix(
RasterCacheUtil::GetIntegralTransCTM(
context.leaf_nodes_canvas->getTotalMatrix()));
if (layer_raster_cache_item_->IsCacheChildren()) {
cache_paint.setImageFilter(transformed_filter_.get());
}
Expand Down
10 changes: 10 additions & 0 deletions flow/layers/opacity_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "flutter/flow/layers/opacity_layer.h"

#include "flutter/flow/layers/cacheable_layer.h"
#include "flutter/flow/raster_cache_util.h"
#include "third_party/skia/include/core/SkPaint.h"

namespace flutter {
Expand All @@ -27,6 +28,10 @@ void OpacityLayer::Diff(DiffContext* context, const Layer* old_layer) {
}
}
context->PushTransform(SkMatrix::Translate(offset_.fX, offset_.fY));
if (context->has_raster_cache()) {
context->SetTransform(
RasterCacheUtil::GetIntegralTransCTM(context->GetTransform()));
}
DiffChildren(context, prev);
context->SetLayerPaintRegion(this, context->CurrentSubtreeRegion());
}
Expand Down Expand Up @@ -85,6 +90,11 @@ void OpacityLayer::Paint(PaintContext& context) const {

SkAutoCanvasRestore save(context.internal_nodes_canvas, true);
context.internal_nodes_canvas->translate(offset_.fX, offset_.fY);
if (context.raster_cache) {
context.internal_nodes_canvas->setMatrix(
RasterCacheUtil::GetIntegralTransCTM(
context.leaf_nodes_canvas->getTotalMatrix()));
}

SkScalar inherited_opacity = context.inherited_opacity;
SkScalar subtree_opacity = opacity() * inherited_opacity;
Expand Down
16 changes: 15 additions & 1 deletion flow/layers/opacity_layer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -644,9 +644,23 @@ TEST_F(OpacityLayerDiffTest, FractionalTranslation) {
MockLayerTree tree1;
tree1.root()->Add(layer);

auto damage = DiffLayerTree(tree1, MockLayerTree());
auto damage = DiffLayerTree(tree1, MockLayerTree(), SkIRect::MakeEmpty(), 0,
0, /*use_raster_cache=*/false);
EXPECT_EQ(damage.frame_damage, SkIRect::MakeLTRB(10, 10, 61, 61));
}

TEST_F(OpacityLayerDiffTest, FractionalTranslationWithRasterCache) {
auto picture = CreateDisplayListLayer(
CreateDisplayList(SkRect::MakeLTRB(10, 10, 60, 60), 1));
auto layer = CreateOpacityLater({picture}, 128, SkPoint::Make(0.5, 0.5));

MockLayerTree tree1;
tree1.root()->Add(layer);

auto damage = DiffLayerTree(tree1, MockLayerTree(), SkIRect::MakeEmpty(), 0,
0, /*use_raster_cache=*/true);
EXPECT_EQ(damage.frame_damage, SkIRect::MakeLTRB(11, 11, 61, 61));
}

} // namespace testing
} // namespace flutter
10 changes: 10 additions & 0 deletions flow/layers/shader_mask_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ void ShaderMaskLayer::Diff(DiffContext* context, const Layer* old_layer) {
context->MarkSubtreeDirty(context->GetOldLayerPaintRegion(old_layer));
}
}
if (context->has_raster_cache()) {
context->SetTransform(
RasterCacheUtil::GetIntegralTransCTM(context->GetTransform()));
}
DiffChildren(context, prev);

context->SetLayerPaintRegion(this, context->CurrentSubtreeRegion());
Expand All @@ -49,6 +53,12 @@ void ShaderMaskLayer::Paint(PaintContext& context) const {

AutoCachePaint cache_paint(context);

if (context.raster_cache) {
context.internal_nodes_canvas->setMatrix(
RasterCacheUtil::GetIntegralTransCTM(
context.leaf_nodes_canvas->getTotalMatrix()));
}

if (context.raster_cache) {
if (layer_raster_cache_item_->Draw(context, cache_paint.sk_paint())) {
return;
Expand Down
Loading