Flutter Engine
layer_tree.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_tree.h"
6 
7 #include "flutter/flow/frame_timings.h"
8 #include "flutter/flow/layers/layer.h"
9 #include "flutter/fml/time/time_point.h"
10 #include "flutter/fml/trace_event.h"
11 #include "third_party/skia/include/core/SkPictureRecorder.h"
12 #include "third_party/skia/include/utils/SkNWayCanvas.h"
13 
14 namespace flutter {
15 
16 LayerTree::LayerTree(const SkISize& frame_size, float device_pixel_ratio)
17  : frame_size_(frame_size),
18  device_pixel_ratio_(device_pixel_ratio),
19  rasterizer_tracing_threshold_(0),
20  checkerboard_raster_cache_images_(false),
21  checkerboard_offscreen_layers_(false) {
22  FML_CHECK(device_pixel_ratio_ != 0.0f);
23 }
24 
26  bool ignore_raster_cache) {
27  TRACE_EVENT0("flutter", "LayerTree::Preroll");
28 
29  if (!root_layer_) {
30  FML_LOG(ERROR) << "The scene did not specify any layers.";
31  return false;
32  }
33 
34  SkColorSpace* color_space =
35  frame.canvas() ? frame.canvas()->imageInfo().colorSpace() : nullptr;
37  checkerboard_raster_cache_images_);
38  MutatorsStack stack;
39  PrerollContext context = {
40  ignore_raster_cache ? nullptr : &frame.context().raster_cache(),
41  frame.gr_context(),
42  frame.view_embedder(),
43  stack,
44  color_space,
45  kGiantRect,
46  false,
47  frame.context().raster_time(),
48  frame.context().ui_time(),
49  frame.context().texture_registry(),
50  checkerboard_offscreen_layers_,
51  device_pixel_ratio_};
52 
53  root_layer_->Preroll(&context, frame.root_surface_transformation());
54  return context.surface_needs_readback;
55 }
56 
58  bool ignore_raster_cache) const {
59  TRACE_EVENT0("flutter", "LayerTree::Paint");
60 
61  if (!root_layer_) {
62  FML_LOG(ERROR) << "The scene did not specify any layers to paint.";
63  return;
64  }
65 
66  SkISize canvas_size = frame.canvas()->getBaseLayerSize();
67  SkNWayCanvas internal_nodes_canvas(canvas_size.width(), canvas_size.height());
68  internal_nodes_canvas.addCanvas(frame.canvas());
69  if (frame.view_embedder() != nullptr) {
70  auto overlay_canvases = frame.view_embedder()->GetCurrentCanvases();
71  for (size_t i = 0; i < overlay_canvases.size(); i++) {
72  internal_nodes_canvas.addCanvas(overlay_canvases[i]);
73  }
74  }
75 
76  Layer::PaintContext context = {
77  static_cast<SkCanvas*>(&internal_nodes_canvas),
78  frame.canvas(),
79  frame.gr_context(),
80  frame.view_embedder(),
81  frame.context().raster_time(),
82  frame.context().ui_time(),
83  frame.context().texture_registry(),
84  ignore_raster_cache ? nullptr : &frame.context().raster_cache(),
85  checkerboard_offscreen_layers_,
86  device_pixel_ratio_};
87 
88  if (root_layer_->needs_painting(context)) {
89  root_layer_->Paint(context);
90  }
91 }
92 
93 sk_sp<SkPicture> LayerTree::Flatten(const SkRect& bounds) {
94  TRACE_EVENT0("flutter", "LayerTree::Flatten");
95 
96  SkPictureRecorder recorder;
97  auto* canvas = recorder.beginRecording(bounds);
98 
99  if (!canvas) {
100  return nullptr;
101  }
102 
103  MutatorsStack unused_stack;
104  const Stopwatch unused_stopwatch;
105  TextureRegistry unused_texture_registry;
106  SkMatrix root_surface_transformation;
107  // No root surface transformation. So assume identity.
108  root_surface_transformation.reset();
109 
110  PrerollContext preroll_context{
111  nullptr, // raster_cache (don't consult the cache)
112  nullptr, // gr_context (used for the raster cache)
113  nullptr, // external view embedder
114  unused_stack, // mutator stack
115  nullptr, // SkColorSpace* dst_color_space
116  kGiantRect, // SkRect cull_rect
117  false, // layer reads from surface
118  unused_stopwatch, // frame time (dont care)
119  unused_stopwatch, // engine time (dont care)
120  unused_texture_registry, // texture registry (not supported)
121  false, // checkerboard_offscreen_layers
122  device_pixel_ratio_ // ratio between logical and physical
123  };
124 
125  SkISize canvas_size = canvas->getBaseLayerSize();
126  SkNWayCanvas internal_nodes_canvas(canvas_size.width(), canvas_size.height());
127  internal_nodes_canvas.addCanvas(canvas);
128 
129  Layer::PaintContext paint_context = {
130  static_cast<SkCanvas*>(&internal_nodes_canvas),
131  canvas, // canvas
132  nullptr,
133  nullptr,
134  unused_stopwatch, // frame time (dont care)
135  unused_stopwatch, // engine time (dont care)
136  unused_texture_registry, // texture registry (not supported)
137  nullptr, // raster cache
138  false, // checkerboard offscreen layers
139  device_pixel_ratio_ // ratio between logical and physical
140  };
141 
142  // Even if we don't have a root layer, we still need to create an empty
143  // picture.
144  if (root_layer_) {
145  root_layer_->Preroll(&preroll_context, root_surface_transformation);
146  // The needs painting flag may be set after the preroll. So check it after.
147  if (root_layer_->needs_painting(paint_context)) {
148  root_layer_->Paint(paint_context);
149  }
150  }
151 
152  return recorder.finishRecordingAsPicture();
153 }
154 
155 } // namespace flutter
sk_sp< SkPicture > Flatten(const SkRect &bounds)
Definition: layer_tree.cc:93
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:90
const SkMatrix & root_surface_transformation() const
void SetCheckboardCacheImages(bool checkerboard)
#define FML_LOG(severity)
Definition: logging.h:65
virtual std::vector< SkCanvas * > GetCurrentCanvases()=0
static constexpr SkRect kGiantRect
Definition: layer.h:37
bool Preroll(CompositorContext::ScopedFrame &frame, bool ignore_raster_cache=false)
Definition: layer_tree.cc:25
LayerTree(const SkISize &frame_size, float device_pixel_ratio)
Definition: layer_tree.cc:16
#define FML_CHECK(condition)
Definition: logging.h:68
TextureRegistry & texture_registry()
void Paint(CompositorContext::ScopedFrame &frame, bool ignore_raster_cache=false) const
Definition: layer_tree.cc:57
bool surface_needs_readback
Definition: layer.h:49
const Stopwatch & raster_time() const