Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
mock_layer.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_MOCK_LAYER_H_
6#define FLUTTER_FLOW_TESTING_MOCK_LAYER_H_
7
8#include <functional>
9#include <memory>
10#include <utility>
11#include "flutter/flow/diff_context.h"
12#include "flutter/flow/layers/cacheable_layer.h"
13#include "flutter/flow/layers/container_layer.h"
14#include "flutter/flow/layers/layer.h"
15#include "flutter/flow/layers/layer_raster_cache_item.h"
16#include "flutter/flow/raster_cache.h"
17#include "flutter/flow/raster_cache_item.h"
18
19namespace flutter {
20namespace testing {
21
22// Mock implementation of the |Layer| interface that does nothing but paint
23// the specified |path| into the canvas. It records the |PrerollContext| and
24// |PaintContext| data passed in by its parent |Layer|, so the test can later
25// verify the data against expected values.
26class MockLayer : public Layer {
27 public:
28 explicit MockLayer(const SkPath& path, DlPaint paint = DlPaint());
29
30 static std::shared_ptr<MockLayer> Make(const SkPath& path,
31 DlPaint paint = DlPaint()) {
32 return std::make_shared<MockLayer>(path, std::move(paint));
33 }
34
35 static std::shared_ptr<MockLayer> MakeOpacityCompatible(const SkPath& path) {
36 auto mock_layer = std::make_shared<MockLayer>(path, DlPaint());
37 mock_layer->set_fake_opacity_compatible(true);
38 return mock_layer;
39 }
40
41 void Preroll(PrerollContext* context) override;
42 void Paint(PaintContext& context) const override;
43
44 const MutatorsStack& parent_mutators() { return parent_mutators_; }
45 const SkMatrix& parent_matrix() { return parent_matrix_; }
46 const SkRect& parent_cull_rect() { return parent_cull_rect_; }
47
48 bool IsReplacing(DiffContext* context, const Layer* layer) const override;
49 void Diff(DiffContext* context, const Layer* old_layer) override;
50 const MockLayer* as_mock_layer() const override { return this; }
51
53 return mock_flags_ & kParentHasPlatformView;
54 }
55
57 return mock_flags_ & kParentHasTextureLayer;
58 }
59
60 bool fake_has_platform_view() { return mock_flags_ & kFakeHasPlatformView; }
61
62 bool fake_reads_surface() { return mock_flags_ & kFakeReadsSurface; }
63
65 return mock_flags_ & kFakeOpacityCompatible;
66 }
67
68 bool fake_has_texture_layer() { return mock_flags_ & kFakeHasTextureLayer; }
69
71 flag ? (mock_flags_ |= kParentHasPlatformView)
72 : (mock_flags_ &= ~(kParentHasPlatformView));
73 return *this;
74 }
75
77 flag ? (mock_flags_ |= kParentHasTextureLayer)
78 : (mock_flags_ &= ~(kParentHasTextureLayer));
79 return *this;
80 }
81
83 flag ? (mock_flags_ |= kFakeHasPlatformView)
84 : (mock_flags_ &= ~(kFakeHasPlatformView));
85 return *this;
86 }
87
89 flag ? (mock_flags_ |= kFakeReadsSurface)
90 : (mock_flags_ &= ~(kFakeReadsSurface));
91 return *this;
92 }
93
95 flag ? (mock_flags_ |= kFakeOpacityCompatible)
96 : (mock_flags_ &= ~(kFakeOpacityCompatible));
97 return *this;
98 }
99
101 flag ? (mock_flags_ |= kFakeHasTextureLayer)
102 : (mock_flags_ &= ~(kFakeHasTextureLayer));
103 return *this;
104 }
105
107 expected_paint_matrix_ = matrix;
108 }
109
110 private:
111 MutatorsStack parent_mutators_;
112 SkMatrix parent_matrix_;
113 SkRect parent_cull_rect_ = SkRect::MakeEmpty();
114 SkPath fake_paint_path_;
115 DlPaint fake_paint_;
116 std::optional<SkMatrix> expected_paint_matrix_;
117
118 static constexpr int kParentHasPlatformView = 1 << 0;
119 static constexpr int kParentHasTextureLayer = 1 << 1;
120 static constexpr int kFakeHasPlatformView = 1 << 2;
121 static constexpr int kFakeReadsSurface = 1 << 3;
122 static constexpr int kFakeOpacityCompatible = 1 << 4;
123 static constexpr int kFakeHasTextureLayer = 1 << 5;
124
125 int mock_flags_ = 0;
126
128};
129
131 public:
132 // if render more than 3 frames, try to cache itself.
133 // if less 3 frames, cache his children
134 static std::shared_ptr<MockCacheableContainerLayer> CacheLayerOrChildren() {
135 return std::make_shared<MockCacheableContainerLayer>(true);
136 }
137
138 // if render more than 3 frames, try to cache itself.
139 // if less 3 frames, cache nothing
140 static std::shared_ptr<MockCacheableContainerLayer> CacheLayerOnly() {
141 return std::make_shared<MockCacheableContainerLayer>();
142 }
143
144 void Preroll(PrerollContext* context) override;
145
146 explicit MockCacheableContainerLayer(bool cache_children = false)
147 : CacheableContainerLayer(3, cache_children) {}
148};
149
155 public:
158 int render_limit = 3)
159 : MockLayer(path, std::move(paint)) {
160 raster_cache_item_ =
161 std::make_unique<MockLayerCacheableItem>(this, render_limit);
162 }
163
165 return raster_cache_item_.get();
166 }
167
168 void Preroll(PrerollContext* context) override;
169
170 private:
171 std::unique_ptr<LayerRasterCacheItem> raster_cache_item_;
172};
173
174} // namespace testing
175} // namespace flutter
176
177#endif // FLUTTER_FLOW_TESTING_MOCK_LAYER_H_
LayerRasterCacheItem(Layer *layer, int layer_cached_threshold=1, bool can_cache_children=false)
static std::shared_ptr< MockCacheableContainerLayer > CacheLayerOnly()
Definition mock_layer.h:140
MockCacheableContainerLayer(bool cache_children=false)
Definition mock_layer.h:146
void Preroll(PrerollContext *context) override
Definition mock_layer.cc:66
static std::shared_ptr< MockCacheableContainerLayer > CacheLayerOrChildren()
Definition mock_layer.h:134
const LayerRasterCacheItem * raster_cache_item() const
Definition mock_layer.h:164
MockCacheableLayer(const SkPath &path, DlPaint paint=DlPaint(), int render_limit=3)
Definition mock_layer.h:156
void Preroll(PrerollContext *context) override
Definition mock_layer.cc:75
bool IsReplacing(DiffContext *context, const Layer *layer) const override
Definition mock_layer.cc:18
const MockLayer * as_mock_layer() const override
Definition mock_layer.h:50
MockLayer & set_parent_has_platform_view(bool flag)
Definition mock_layer.h:70
void set_expected_paint_matrix(const SkMatrix &matrix)
Definition mock_layer.h:106
static std::shared_ptr< MockLayer > MakeOpacityCompatible(const SkPath &path)
Definition mock_layer.h:35
static std::shared_ptr< MockLayer > Make(const SkPath &path, DlPaint paint=DlPaint())
Definition mock_layer.h:30
const SkRect & parent_cull_rect()
Definition mock_layer.h:46
MockLayer & set_parent_has_texture_layer(bool flag)
Definition mock_layer.h:76
MockLayer & set_fake_has_texture_layer(bool flag)
Definition mock_layer.h:100
MockLayer & set_fake_has_platform_view(bool flag)
Definition mock_layer.h:82
void Diff(DiffContext *context, const Layer *old_layer) override
Definition mock_layer.cc:27
void Preroll(PrerollContext *context) override
Definition mock_layer.cc:33
MockLayer & set_fake_reads_surface(bool flag)
Definition mock_layer.h:88
const SkMatrix & parent_matrix()
Definition mock_layer.h:45
MockLayer & set_fake_opacity_compatible(bool flag)
Definition mock_layer.h:94
const MutatorsStack & parent_mutators()
Definition mock_layer.h:44
const Paint & paint
FlutterSemanticsFlag flag
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition switches.h:57
Definition ref_ptr.h:256
static constexpr SkRect MakeEmpty()
Definition SkRect.h:595