Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit df5f62f

Browse files
Add a flag that checkerboards offscreen render targets created by SkCanvas.saveLayer (#3685)
Fixes flutter/flutter#9473
1 parent f99d3ef commit df5f62f

22 files changed

Lines changed: 174 additions & 64 deletions

‎flow/BUILD.gn‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ source_set("flow") {
3636
"layers/shader_mask_layer.h",
3737
"layers/transform_layer.cc",
3838
"layers/transform_layer.h",
39+
"paint_utils.cc",
40+
"paint_utils.h",
3941
"process_info.h",
4042
"raster_cache.cc",
4143
"raster_cache.h",

‎flow/layers/backdrop_filter_layer.cc‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ BackdropFilterLayer::~BackdropFilterLayer() {}
1414

1515
void BackdropFilterLayer::Paint(PaintContext& context) {
1616
TRACE_EVENT0("flutter", "BackdropFilterLayer::Paint");
17-
SkAutoCanvasRestore save(&context.canvas, false);
18-
context.canvas.saveLayer(
17+
Layer::AutoSaveLayer(
18+
context,
1919
SkCanvas::SaveLayerRec{&paint_bounds(), nullptr, filter_.get(), 0});
2020
PaintChildren(context);
2121
}

‎flow/layers/clip_path_layer.cc‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ void ClipPathLayer::Paint(PaintContext& context) {
3737
TRACE_EVENT0("flutter", "ClipPathLayer::Paint");
3838
FTL_DCHECK(!needs_system_composite());
3939

40-
SkAutoCanvasRestore save(&context.canvas, false);
41-
context.canvas.saveLayer(&paint_bounds(), nullptr);
40+
Layer::AutoSaveLayer save(context, paint_bounds(), nullptr);
4241
context.canvas.clipPath(clip_path_, true);
4342
PaintChildren(context);
4443
}

‎flow/layers/clip_rrect_layer.cc‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ void ClipRRectLayer::Paint(PaintContext& context) {
3737
TRACE_EVENT0("flutter", "ClipRRectLayer::Paint");
3838
FTL_DCHECK(!needs_system_composite());
3939

40-
SkAutoCanvasRestore save(&context.canvas, false);
41-
context.canvas.saveLayer(&paint_bounds(), nullptr);
40+
Layer::AutoSaveLayer save(context, paint_bounds(), nullptr);
4241
context.canvas.clipRRect(clip_rrect_, true);
4342
PaintChildren(context);
4443
}

‎flow/layers/color_filter_layer.cc‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ void ColorFilterLayer::Paint(PaintContext& context) {
1717
SkPaint paint;
1818
paint.setColorFilter(std::move(color_filter));
1919

20-
SkAutoCanvasRestore save(&context.canvas, false);
21-
context.canvas.saveLayer(&paint_bounds(), &paint);
20+
Layer::AutoSaveLayer(context, paint_bounds(), nullptr);
2221
PaintChildren(context);
2322
}
2423

‎flow/layers/layer.cc‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "flutter/flow/layers/layer.h"
66

7+
#include "flutter/flow/paint_utils.h"
78
#include "third_party/skia/include/core/SkColorFilter.h"
89

910
namespace flow {
@@ -26,4 +27,26 @@ void Layer::Preroll(PrerollContext* context, const SkMatrix& matrix) {
2627
void Layer::UpdateScene(SceneUpdateContext& context, mozart::Node* container) {}
2728
#endif
2829

30+
Layer::AutoSaveLayer::AutoSaveLayer(const PaintContext& paint_context,
31+
const SkRect& bounds,
32+
const SkPaint* paint)
33+
: paint_context_(paint_context),
34+
bounds_(bounds) {
35+
paint_context_.canvas.saveLayer(bounds_, paint);
36+
}
37+
38+
Layer::AutoSaveLayer::AutoSaveLayer(const PaintContext& paint_context,
39+
const SkCanvas::SaveLayerRec& layer_rec)
40+
: paint_context_(paint_context),
41+
bounds_(*layer_rec.fBounds) {
42+
paint_context_.canvas.saveLayer(layer_rec);
43+
}
44+
45+
Layer::AutoSaveLayer::~AutoSaveLayer() {
46+
if (paint_context_.checkerboard_offscreen_layers) {
47+
DrawCheckerboard(&paint_context_.canvas, bounds_);
48+
}
49+
paint_context_.canvas.restore();
50+
}
51+
2952
} // namespace flow

‎flow/layers/layer.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ class Layer {
4545
const Stopwatch& frame_time;
4646
const Stopwatch& engine_time;
4747
const CounterValues& memory_usage;
48+
const bool checkerboard_offscreen_layers;
49+
};
50+
51+
// Calls SkCanvas::saveLayer and restores the layer upon destruction. Also
52+
// draws a checkerboard over the layer if that is enabled in the PaintContext.
53+
class AutoSaveLayer {
54+
public:
55+
AutoSaveLayer(const PaintContext& paint_context,
56+
const SkRect& bounds,
57+
const SkPaint* paint);
58+
59+
AutoSaveLayer(const PaintContext& paint_context,
60+
const SkCanvas::SaveLayerRec& layer_rec);
61+
62+
~AutoSaveLayer();
63+
64+
private:
65+
const PaintContext& paint_context_;
66+
const SkRect bounds_;
4867
};
4968

5069
virtual void Paint(PaintContext& context) = 0;

‎flow/layers/layer_tree.cc‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ LayerTree::LayerTree()
1313
: frame_size_{},
1414
scene_version_(0),
1515
rasterizer_tracing_threshold_(0),
16-
checkerboard_raster_cache_images_(false) {}
16+
checkerboard_raster_cache_images_(false),
17+
checkerboard_offscreen_layers_(false) {}
1718

1819
LayerTree::~LayerTree() {}
1920

@@ -52,7 +53,8 @@ void LayerTree::UpdateScene(SceneUpdateContext& context,
5253
void LayerTree::Paint(CompositorContext::ScopedFrame& frame) {
5354
Layer::PaintContext context = {frame.canvas(), frame.context().frame_time(),
5455
frame.context().engine_time(),
55-
frame.context().memory_usage()};
56+
frame.context().memory_usage(),
57+
checkerboard_offscreen_layers_};
5658
TRACE_EVENT0("flutter", "LayerTree::Paint");
5759
root_layer_->Paint(context);
5860
}

‎flow/layers/layer_tree.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,18 @@ class LayerTree {
7676
checkerboard_raster_cache_images_ = checkerboard;
7777
}
7878

79+
void set_checkerboard_offscreen_layers(bool checkerboard) {
80+
checkerboard_offscreen_layers_ = checkerboard;
81+
}
82+
7983
private:
8084
SkISize frame_size_; // Physical pixels.
8185
uint32_t scene_version_;
8286
std::unique_ptr<Layer> root_layer_;
8387
ftl::TimeDelta construction_time_;
8488
uint32_t rasterizer_tracing_threshold_;
8589
bool checkerboard_raster_cache_images_;
90+
bool checkerboard_offscreen_layers_;
8691

8792
FTL_DISALLOW_COPY_AND_ASSIGN(LayerTree);
8893
};

‎flow/layers/opacity_layer.cc‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ void OpacityLayer::Paint(PaintContext& context) {
3737
SkPaint paint;
3838
paint.setAlpha(alpha_);
3939

40-
SkAutoCanvasRestore save(&context.canvas, false);
41-
context.canvas.saveLayer(&paint_bounds(), &paint);
40+
Layer::AutoSaveLayer save(context, paint_bounds(), &paint);
4241
PaintChildren(context);
4342
}
4443

0 commit comments

Comments
 (0)