Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
layer_test.h
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#ifndef FLUTTER_FLOW_TESTING_LAYER_TEST_H_
6#define FLUTTER_FLOW_TESTING_LAYER_TEST_H_
7
9#include "flutter/flow/layer_snapshot_store.h"
10#include "flutter/flow/layers/layer.h"
11
12#include <optional>
13#include <utility>
14#include <vector>
15
16#include "flutter/flow/testing/mock_raster_cache.h"
17#include "flutter/fml/macros.h"
18#include "flutter/testing/assertions_skia.h"
19#include "flutter/testing/canvas_test.h"
20#include "flutter/testing/display_list_testing.h"
21#include "flutter/testing/mock_canvas.h"
25
26namespace flutter {
27namespace testing {
28
29// This fixture allows generating tests which can |Paint()| and |Preroll()|
30// |Layer|'s.
31// |LayerTest| is a default implementation based on |::testing::Test|.
32//
33// By default the preroll and paint contexts will not use a raster cache.
34// If a test needs to verify the proper operation of a layer in the presence
35// of a raster cache then a number of options can be enabled by using the
36// methods |LayerTestBase::use_null_raster_cache()|,
37// |LayerTestBase::use_mock_raster_cache()| or
38// |LayerTestBase::use_skia_raster_cache()|
39//
40// |BaseT| should be the base test type, such as |::testing::Test| below.
41template <typename BaseT>
42class LayerTestBase : public CanvasTestBase<BaseT> {
44
45 const SkRect k_dl_bounds_ = SkRect::MakeWH(500, 500);
46
47 public:
49 : texture_registry_(std::make_shared<TextureRegistry>()),
50 preroll_context_{
51 // clang-format off
52 .raster_cache = nullptr,
53 .gr_context = nullptr,
54 .view_embedder = nullptr,
55 .state_stack = preroll_state_stack_,
56 .dst_color_space = TestT::mock_color_space(),
57 .surface_needs_readback = false,
58 .raster_time = raster_time_,
59 .ui_time = ui_time_,
60 .texture_registry = texture_registry_,
61 .has_platform_view = false,
62 .raster_cached_entries = &cacheable_items_,
63 // clang-format on
64 },
65 paint_context_{
66 // clang-format off
67 .state_stack = paint_state_stack_,
68 .canvas = &TestT::mock_canvas(),
69 .gr_context = nullptr,
70 .view_embedder = nullptr,
71 .raster_time = raster_time_,
72 .ui_time = ui_time_,
73 .texture_registry = texture_registry_,
74 .raster_cache = nullptr,
75 // clang-format on
76 },
77 display_list_builder_(k_dl_bounds_),
78 display_list_paint_context_{
79 // clang-format off
80 .state_stack = display_list_state_stack_,
81 .canvas = &display_list_builder_,
82 .gr_context = nullptr,
83 .view_embedder = nullptr,
84 .raster_time = raster_time_,
85 .ui_time = ui_time_,
86 .texture_registry = texture_registry_,
87 .raster_cache = nullptr,
88 // clang-format on
89 },
90 checkerboard_context_{
91 // clang-format off
92 .state_stack = checkerboard_state_stack_,
93 .canvas = &display_list_builder_,
94 .gr_context = nullptr,
95 .view_embedder = nullptr,
96 .raster_time = raster_time_,
97 .ui_time = ui_time_,
98 .texture_registry = texture_registry_,
99 .raster_cache = nullptr,
100 // clang-format on
101 } {
103 preroll_state_stack_.set_preroll_delegate(kGiantRect, SkMatrix::I());
104 paint_state_stack_.set_delegate(&TestT::mock_canvas());
105 display_list_state_stack_.set_delegate(&display_list_builder_);
106 checkerboard_state_stack_.set_delegate(&display_list_builder_);
107 checkerboard_state_stack_.set_checkerboard_func(draw_checkerboard);
108 checkerboard_paint_.setColor(kCheckerboardColor);
109 }
110
111 /**
112 * @brief Use no raster cache in the preroll_context() and
113 * paint_context() structures.
114 *
115 * This method must be called before using the preroll_context() and
116 * paint_context() structures in calls to the Layer::Preroll() and
117 * Layer::Paint() methods. This is the default mode of operation.
118 *
119 * @see use_mock_raster_cache()
120 * @see use_skia_raster_cache()
121 */
122 void use_null_raster_cache() { set_raster_cache_(nullptr); }
123
124 /**
125 * @brief Use a mock raster cache in the preroll_context() and
126 * paint_context() structures.
127 *
128 * This method must be called before using the preroll_context() and
129 * paint_context() structures in calls to the Layer::Preroll() and
130 * Layer::Paint() methods. The mock raster cache behaves like a normal
131 * raster cache with respect to decisions about when layers and pictures
132 * should be cached, but it does not incur the overhead of rendering the
133 * layers or caching the resulting pixels.
134 *
135 * @see use_null_raster_cache()
136 * @see use_skia_raster_cache()
137 */
139 set_raster_cache_(std::make_unique<MockRasterCache>());
140 }
141
142 /**
143 * @brief Use a normal raster cache in the preroll_context() and
144 * paint_context() structures.
145 *
146 * This method must be called before using the preroll_context() and
147 * paint_context() structures in calls to the Layer::Preroll() and
148 * Layer::Paint() methods. The Skia raster cache will behave identically
149 * to the raster cache typically used when handling a frame on a device
150 * including rendering the contents of pictures and layers to an
151 * SkImage, but using a software rather than a hardware renderer.
152 *
153 * @see use_null_raster_cache()
154 * @see use_mock_raster_cache()
155 */
157 set_raster_cache_(std::make_unique<RasterCache>());
158 }
159
160 std::vector<RasterCacheItem*>& cacheable_items() { return cacheable_items_; }
161
162 std::shared_ptr<TextureRegistry> texture_registry() {
163 return texture_registry_;
164 }
165 RasterCache* raster_cache() { return raster_cache_.get(); }
166 PrerollContext* preroll_context() { return &preroll_context_; }
167 PaintContext& paint_context() { return paint_context_; }
169 return display_list_paint_context_;
170 }
171 const DlPaint& checkerboard_paint() { return checkerboard_paint_; }
172 PaintContext& checkerboard_context() { return checkerboard_context_; }
173 LayerSnapshotStore& layer_snapshot_store() { return snapshot_store_; }
174
176 if (display_list_ == nullptr) {
177 display_list_ = display_list_builder_.Build();
178 }
179 return display_list_;
180 }
181
183 display_list_ = nullptr;
184 // Build() will leave the builder in a state to start recording a new DL
185 display_list_builder_.Build();
186 // Make sure we are starting from a fresh state stack
187 FML_DCHECK(display_list_state_stack_.is_empty());
188 }
189
191 paint_context_.enable_leaf_layer_tracing = true;
192 paint_context_.layer_snapshot_store = &snapshot_store_;
193 }
194
196 paint_context_.enable_leaf_layer_tracing = false;
197 paint_context_.layer_snapshot_store = nullptr;
198 }
199
200 private:
201 void set_raster_cache_(std::unique_ptr<RasterCache> raster_cache) {
202 raster_cache_ = std::move(raster_cache);
203 preroll_context_.raster_cache = raster_cache_.get();
204 paint_context_.raster_cache = raster_cache_.get();
205 display_list_paint_context_.raster_cache = raster_cache_.get();
206 }
207
208 static constexpr DlColor kCheckerboardColor = DlColor(0x42424242);
209
210 static void draw_checkerboard(DlCanvas* canvas, const SkRect& rect) {
211 if (canvas) {
213 paint.setColor(kCheckerboardColor);
214 canvas->DrawRect(rect, paint);
215 }
216 }
217
218 LayerStateStack preroll_state_stack_;
219 LayerStateStack paint_state_stack_;
220 LayerStateStack checkerboard_state_stack_;
221 FixedRefreshRateStopwatch raster_time_;
222 FixedRefreshRateStopwatch ui_time_;
223 std::shared_ptr<TextureRegistry> texture_registry_;
224
225 std::unique_ptr<RasterCache> raster_cache_;
226 PrerollContext preroll_context_;
227 PaintContext paint_context_;
228 DisplayListBuilder display_list_builder_;
229 LayerStateStack display_list_state_stack_;
230 sk_sp<DisplayList> display_list_;
231 PaintContext display_list_paint_context_;
232 DlPaint checkerboard_paint_;
233 PaintContext checkerboard_context_;
234 LayerSnapshotStore snapshot_store_;
235
236 std::vector<RasterCacheItem*> cacheable_items_;
237
239};
241
242} // namespace testing
243} // namespace flutter
244
245#endif // FLUTTER_FLOW_TESTING_LAYER_TEST_H_
static const SkMatrix & I()
sk_sp< DisplayList > Build()
Definition dl_builder.cc:67
Developer-facing API for rendering anything within the engine.
Definition dl_canvas.h:37
virtual void DrawRect(const SkRect &rect, const DlPaint &paint)=0
DlPaint & setColor(DlColor color)
Definition dl_paint.h:71
Collects snapshots of layers during frame rasterization.
void set_preroll_delegate(const SkRect &cull_rect, const SkMatrix &matrix)
void set_checkerboard_func(CheckerboardFunc checkerboard_func)
void set_delegate(DlCanvas *canvas)
sk_sp< SkColorSpace > mock_color_space()
Definition canvas_test.h:23
std::vector< RasterCacheItem * > & cacheable_items()
Definition layer_test.h:160
PrerollContext * preroll_context()
Definition layer_test.h:166
void use_skia_raster_cache()
Use a normal raster cache in the preroll_context() and paint_context() structures.
Definition layer_test.h:156
LayerSnapshotStore & layer_snapshot_store()
Definition layer_test.h:173
PaintContext & checkerboard_context()
Definition layer_test.h:172
sk_sp< DisplayList > display_list()
Definition layer_test.h:175
void use_mock_raster_cache()
Use a mock raster cache in the preroll_context() and paint_context() structures.
Definition layer_test.h:138
std::shared_ptr< TextureRegistry > texture_registry()
Definition layer_test.h:162
void use_null_raster_cache()
Use no raster cache in the preroll_context() and paint_context() structures.
Definition layer_test.h:122
const DlPaint & checkerboard_paint()
Definition layer_test.h:171
PaintContext & display_list_paint_context()
Definition layer_test.h:168
const Paint & paint
#define FML_DCHECK(condition)
Definition logging.h:103
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
static constexpr SkRect kGiantRect
Definition layer.h:49
Definition ref_ptr.h:256
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609
bool enable_leaf_layer_tracing
Definition layer.h:119
LayerSnapshotStore * layer_snapshot_store
Definition layer.h:118
const RasterCache * raster_cache
Definition layer.h:114
RasterCache * raster_cache
Definition layer.h:55