Flutter Engine
The Flutter Engine
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 } {
91 preroll_state_stack_.set_preroll_delegate(kGiantRect, SkMatrix::I());
92 paint_state_stack_.set_delegate(&TestT::mock_canvas());
93 display_list_state_stack_.set_delegate(&display_list_builder_);
94 }
95
96 /**
97 * @brief Use no raster cache in the preroll_context() and
98 * paint_context() structures.
99 *
100 * This method must be called before using the preroll_context() and
101 * paint_context() structures in calls to the Layer::Preroll() and
102 * Layer::Paint() methods. This is the default mode of operation.
103 *
104 * @see use_mock_raster_cache()
105 * @see use_skia_raster_cache()
106 */
107 void use_null_raster_cache() { set_raster_cache_(nullptr); }
108
109 /**
110 * @brief Use a mock raster cache in the preroll_context() and
111 * paint_context() structures.
112 *
113 * This method must be called before using the preroll_context() and
114 * paint_context() structures in calls to the Layer::Preroll() and
115 * Layer::Paint() methods. The mock raster cache behaves like a normal
116 * raster cache with respect to decisions about when layers and pictures
117 * should be cached, but it does not incur the overhead of rendering the
118 * layers or caching the resulting pixels.
119 *
120 * @see use_null_raster_cache()
121 * @see use_skia_raster_cache()
122 */
124 set_raster_cache_(std::make_unique<MockRasterCache>());
125 }
126
127 /**
128 * @brief Use a normal raster cache in the preroll_context() and
129 * paint_context() structures.
130 *
131 * This method must be called before using the preroll_context() and
132 * paint_context() structures in calls to the Layer::Preroll() and
133 * Layer::Paint() methods. The Skia raster cache will behave identically
134 * to the raster cache typically used when handling a frame on a device
135 * including rendering the contents of pictures and layers to an
136 * SkImage, but using a software rather than a hardware renderer.
137 *
138 * @see use_null_raster_cache()
139 * @see use_mock_raster_cache()
140 */
142 set_raster_cache_(std::make_unique<RasterCache>());
143 }
144
145 std::vector<RasterCacheItem*>& cacheable_items() { return cacheable_items_; }
146
147 std::shared_ptr<TextureRegistry> texture_registry() {
148 return texture_registry_;
149 }
150 RasterCache* raster_cache() { return raster_cache_.get(); }
151 PrerollContext* preroll_context() { return &preroll_context_; }
152 PaintContext& paint_context() { return paint_context_; }
154 return display_list_paint_context_;
155 }
156 LayerSnapshotStore& layer_snapshot_store() { return snapshot_store_; }
157
159 if (display_list_ == nullptr) {
160 display_list_ = display_list_builder_.Build();
161 }
162 return display_list_;
163 }
164
166 display_list_ = nullptr;
167 // Build() will leave the builder in a state to start recording a new DL
168 display_list_builder_.Build();
169 // Make sure we are starting from a fresh state stack
170 FML_DCHECK(display_list_state_stack_.is_empty());
171 }
172
174 paint_context_.enable_leaf_layer_tracing = true;
175 paint_context_.layer_snapshot_store = &snapshot_store_;
176 }
177
179 paint_context_.enable_leaf_layer_tracing = false;
180 paint_context_.layer_snapshot_store = nullptr;
181 }
182
183 private:
184 void set_raster_cache_(std::unique_ptr<RasterCache> raster_cache) {
185 raster_cache_ = std::move(raster_cache);
186 preroll_context_.raster_cache = raster_cache_.get();
187 paint_context_.raster_cache = raster_cache_.get();
188 display_list_paint_context_.raster_cache = raster_cache_.get();
189 }
190
191 LayerStateStack preroll_state_stack_;
192 LayerStateStack paint_state_stack_;
193 FixedRefreshRateStopwatch raster_time_;
195 std::shared_ptr<TextureRegistry> texture_registry_;
196
197 std::unique_ptr<RasterCache> raster_cache_;
198 PrerollContext preroll_context_;
199 PaintContext paint_context_;
200 DisplayListBuilder display_list_builder_;
201 LayerStateStack display_list_state_stack_;
202 sk_sp<DisplayList> display_list_;
203 PaintContext display_list_paint_context_;
204 LayerSnapshotStore snapshot_store_;
205
206 std::vector<RasterCacheItem*> cacheable_items_;
207
208 FML_DISALLOW_COPY_AND_ASSIGN(LayerTestBase);
209};
211
212} // namespace testing
213} // namespace flutter
214
215#endif // FLUTTER_FLOW_TESTING_LAYER_TEST_H_
static const SkMatrix & I()
Definition: SkMatrix.cpp:1544
sk_sp< DisplayList > Build()
Definition: dl_builder.cc:67
Used for fixed refresh rate cases.
Definition: stopwatch.h:77
Collects snapshots of layers during frame rasterization.
void set_preroll_delegate(const SkRect &cull_rect, const SkMatrix &matrix)
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:145
PrerollContext * preroll_context()
Definition: layer_test.h:151
void use_skia_raster_cache()
Use a normal raster cache in the preroll_context() and paint_context() structures.
Definition: layer_test.h:141
LayerSnapshotStore & layer_snapshot_store()
Definition: layer_test.h:156
sk_sp< DisplayList > display_list()
Definition: layer_test.h:158
PaintContext & paint_context()
Definition: layer_test.h:152
void use_mock_raster_cache()
Use a mock raster cache in the preroll_context() and paint_context() structures.
Definition: layer_test.h:123
std::shared_ptr< TextureRegistry > texture_registry()
Definition: layer_test.h:147
void use_null_raster_cache()
Use no raster cache in the preroll_context() and paint_context() structures.
Definition: layer_test.h:107
PaintContext & display_list_paint_context()
Definition: layer_test.h:153
#define FML_DCHECK(condition)
Definition: logging.h:103
static constexpr SkRect kGiantRect
Definition: layer.h:50
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:120
LayerSnapshotStore * layer_snapshot_store
Definition: layer.h:119