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
10
11#include <optional>
12#include <utility>
13#include <vector>
14
16#include "flutter/fml/macros.h"
20
21namespace flutter {
22namespace testing {
23
24// This fixture allows generating tests which can |Paint()| and |Preroll()|
25// |Layer|'s.
26// |LayerTest| is a default implementation based on |::testing::Test|.
27//
28// By default the preroll and paint contexts will not use a raster cache.
29// If a test needs to verify the proper operation of a layer in the presence
30// of a raster cache then a number of options can be enabled by using the
31// methods |LayerTestBase::use_null_raster_cache()|,
32// |LayerTestBase::use_mock_raster_cache()| or
33// |LayerTestBase::use_skia_raster_cache()|
34//
35// |BaseT| should be the base test type, such as |::testing::Test| below.
36template <typename BaseT>
37class LayerTestBase : public CanvasTestBase<BaseT> {
38 using TestT = CanvasTestBase<BaseT>;
39
40 const DlRect k_dl_bounds_ = DlRect::MakeWH(500, 500);
41
42 public:
44 : texture_registry_(std::make_shared<TextureRegistry>()),
45 preroll_context_{
46 // clang-format off
47 .raster_cache = nullptr,
48 .gr_context = nullptr,
49 .view_embedder = nullptr,
50 .state_stack = preroll_state_stack_,
51 .dst_color_space = TestT::mock_color_space(),
52 .surface_needs_readback = false,
53 .raster_time = raster_time_,
54 .ui_time = ui_time_,
55 .texture_registry = texture_registry_,
56 .has_platform_view = false,
57 .raster_cached_entries = &cacheable_items_,
58 // clang-format on
59 },
60 display_list_builder_(k_dl_bounds_),
61 display_list_paint_context_{
62 // clang-format off
63 .state_stack = display_list_state_stack_,
64 .canvas = &display_list_builder_,
65 .gr_context = nullptr,
66 .view_embedder = nullptr,
67 .raster_time = raster_time_,
68 .ui_time = ui_time_,
69 .texture_registry = texture_registry_,
70 .raster_cache = nullptr,
71 // clang-format on
72 } {
74 preroll_state_stack_.set_preroll_delegate(kGiantRect, DlMatrix());
75 display_list_state_stack_.set_delegate(&display_list_builder_);
76 }
77
78 /**
79 * @brief Use no raster cache in the preroll_context() and
80 * paint_context() structures.
81 *
82 * This method must be called before using the preroll_context() and
83 * paint_context() structures in calls to the Layer::Preroll() and
84 * Layer::Paint() methods. This is the default mode of operation.
85 *
86 * @see use_mock_raster_cache()
87 * @see use_skia_raster_cache()
88 */
89 void use_null_raster_cache() { set_raster_cache_(nullptr); }
90
91 /**
92 * @brief Use a mock raster cache in the preroll_context() and
93 * paint_context() structures.
94 *
95 * This method must be called before using the preroll_context() and
96 * paint_context() structures in calls to the Layer::Preroll() and
97 * Layer::Paint() methods. The mock raster cache behaves like a normal
98 * raster cache with respect to decisions about when layers and pictures
99 * should be cached, but it does not incur the overhead of rendering the
100 * layers or caching the resulting pixels.
101 *
102 * @see use_null_raster_cache()
103 * @see use_skia_raster_cache()
104 */
106 set_raster_cache_(std::make_unique<MockRasterCache>());
107 }
108
109 /**
110 * @brief Use a normal 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 Skia raster cache will behave identically
116 * to the raster cache typically used when handling a frame on a device
117 * including rendering the contents of pictures and layers to an
118 * SkImage, but using a software rather than a hardware renderer.
119 *
120 * @see use_null_raster_cache()
121 * @see use_mock_raster_cache()
122 */
124 set_raster_cache_(std::make_unique<RasterCache>());
125 }
126
127 std::vector<RasterCacheItem*>& cacheable_items() { return cacheable_items_; }
128
129 std::shared_ptr<TextureRegistry> texture_registry() {
130 return texture_registry_;
131 }
132 RasterCache* raster_cache() { return raster_cache_.get(); }
133 PrerollContext* preroll_context() { return &preroll_context_; }
134 PaintContext& paint_context() { return display_list_paint_context_; }
136 return display_list_paint_context_;
137 }
138
139 sk_sp<DisplayList> display_list() {
140 if (display_list_ == nullptr) {
141 display_list_ = display_list_builder_.Build();
142 }
143 return display_list_;
144 }
145
147 display_list_ = nullptr;
148 // Build() will leave the builder in a state to start recording a new DL
149 display_list_builder_.Build();
150 // Make sure we are starting from a fresh state stack
151 FML_DCHECK(display_list_state_stack_.is_empty());
152 }
153
154 private:
155 void set_raster_cache_(std::unique_ptr<RasterCache> raster_cache) {
156 raster_cache_ = std::move(raster_cache);
157 preroll_context_.raster_cache = raster_cache_.get();
158 display_list_paint_context_.raster_cache = raster_cache_.get();
159 }
160
161 LayerStateStack preroll_state_stack_;
162 FixedRefreshRateStopwatch raster_time_;
164 std::shared_ptr<TextureRegistry> texture_registry_;
165
166 std::unique_ptr<RasterCache> raster_cache_;
167 PrerollContext preroll_context_;
168 DisplayListBuilder display_list_builder_;
169 LayerStateStack display_list_state_stack_;
170 sk_sp<DisplayList> display_list_;
171 PaintContext display_list_paint_context_;
172
173 std::vector<RasterCacheItem*> cacheable_items_;
174
176};
178
179} // namespace testing
180} // namespace flutter
181
182#endif // FLUTTER_FLOW_TESTING_LAYER_TEST_H_
sk_sp< DisplayList > Build()
Definition dl_builder.cc:66
Used for fixed refresh rate cases.
Definition stopwatch.h:80
void set_delegate(DlCanvas *canvas)
void set_preroll_delegate(const DlRect &cull_rect, const DlMatrix &matrix)
sk_sp< SkColorSpace > mock_color_space()
Definition canvas_test.h:20
std::vector< RasterCacheItem * > & cacheable_items()
Definition layer_test.h:127
PrerollContext * preroll_context()
Definition layer_test.h:133
void use_skia_raster_cache()
Use a normal raster cache in the preroll_context() and paint_context() structures.
Definition layer_test.h:123
sk_sp< DisplayList > display_list()
Definition layer_test.h:139
void use_mock_raster_cache()
Use a mock raster cache in the preroll_context() and paint_context() structures.
Definition layer_test.h:105
std::shared_ptr< TextureRegistry > texture_registry()
Definition layer_test.h:129
void use_null_raster_cache()
Use no raster cache in the preroll_context() and paint_context() structures.
Definition layer_test.h:89
PaintContext & display_list_paint_context()
Definition layer_test.h:135
#define FML_DCHECK(condition)
Definition logging.h:122
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
impeller::Matrix DlMatrix
static constexpr DlRect kGiantRect
Definition layer.h:40
Definition ref_ptr.h:261
static constexpr TRect MakeWH(Type width, Type height)
Definition rect.h:140