Flutter Engine
layer.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/flow/layers/layer.h"
6 
7 #include "flutter/flow/paint_utils.h"
8 #include "third_party/skia/include/core/SkColorFilter.h"
9 
10 namespace flutter {
11 
13  : paint_bounds_(SkRect::MakeEmpty()),
14  unique_id_(NextUniqueID()),
15  original_layer_id_(unique_id_),
16  subtree_has_platform_view_(false) {}
17 
18 Layer::~Layer() = default;
19 
20 uint64_t Layer::NextUniqueID() {
21  static std::atomic<uint64_t> nextID(1);
22  uint64_t id;
23  do {
24  id = nextID.fetch_add(1);
25  } while (id == 0); // 0 is reserved for an invalid id.
26  return id;
27 }
28 
29 void Layer::Preroll(PrerollContext* context, const SkMatrix& matrix) {}
30 
31 Layer::AutoPrerollSaveLayerState::AutoPrerollSaveLayerState(
32  PrerollContext* preroll_context,
33  bool save_layer_is_active,
34  bool layer_itself_performs_readback)
35  : preroll_context_(preroll_context),
36  save_layer_is_active_(save_layer_is_active),
37  layer_itself_performs_readback_(layer_itself_performs_readback) {
38  if (save_layer_is_active_) {
39  prev_surface_needs_readback_ = preroll_context_->surface_needs_readback;
40  preroll_context_->surface_needs_readback = false;
41  }
42 }
43 
45  PrerollContext* preroll_context,
46  bool save_layer_is_active,
47  bool layer_itself_performs_readback) {
48  return Layer::AutoPrerollSaveLayerState(preroll_context, save_layer_is_active,
49  layer_itself_performs_readback);
50 }
51 
53  if (save_layer_is_active_) {
54  preroll_context_->surface_needs_readback =
55  (prev_surface_needs_readback_ || layer_itself_performs_readback_);
56  }
57 }
58 
59 Layer::AutoSaveLayer::AutoSaveLayer(const PaintContext& paint_context,
60  const SkRect& bounds,
61  const SkPaint* paint,
62  SaveMode save_mode)
63  : paint_context_(paint_context),
64  bounds_(bounds),
65  canvas_(save_mode == SaveMode::kInternalNodesCanvas
66  ? *(paint_context.internal_nodes_canvas)
67  : *(paint_context.leaf_nodes_canvas)) {
68  canvas_.saveLayer(bounds_, paint);
69 }
70 
71 Layer::AutoSaveLayer::AutoSaveLayer(const PaintContext& paint_context,
72  const SkCanvas::SaveLayerRec& layer_rec,
73  SaveMode save_mode)
74  : paint_context_(paint_context),
75  bounds_(*layer_rec.fBounds),
76  canvas_(save_mode == SaveMode::kInternalNodesCanvas
77  ? *(paint_context.internal_nodes_canvas)
78  : *(paint_context.leaf_nodes_canvas)) {
79  canvas_.saveLayer(layer_rec);
80 }
81 
83  const PaintContext& paint_context,
84  const SkRect& bounds,
85  const SkPaint* paint,
86  SaveMode save_mode) {
87  return Layer::AutoSaveLayer(paint_context, bounds, paint, save_mode);
88 }
89 
91  const PaintContext& paint_context,
92  const SkCanvas::SaveLayerRec& layer_rec,
93  SaveMode save_mode) {
94  return Layer::AutoSaveLayer(paint_context, layer_rec, save_mode);
95 }
96 
98  if (paint_context_.checkerboard_offscreen_layers) {
99  DrawCheckerboard(&canvas_, bounds_);
100  }
101  canvas_.restore();
102 }
103 
104 } // namespace flutter
static AutoPrerollSaveLayerState Create(PrerollContext *preroll_context, bool save_layer_is_active=true, bool layer_itself_performs_readback=false)
Definition: layer.cc:44
virtual void Preroll(PrerollContext *context, const SkMatrix &matrix)
Definition: layer.cc:29
SkCanvas * leaf_nodes_canvas
Definition: layer.h:145
int32_t id
SkCanvas * internal_nodes_canvas
Definition: layer.h:144
virtual ~Layer()
void DrawCheckerboard(SkCanvas *canvas, SkColor c1, SkColor c2, int size)
Definition: paint_utils.cc:29
static AutoSaveLayer Create(const PaintContext &paint_context, const SkRect &bounds, const SkPaint *paint, SaveMode save_mode=SaveMode::kInternalNodesCanvas)
Definition: layer.cc:82