forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_raster_cache.h
152 lines (130 loc) · 5.1 KB
/
mock_raster_cache.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_FLOW_TESTING_MOCK_RASTER_CACHE_H_
#define FLUTTER_FLOW_TESTING_MOCK_RASTER_CACHE_H_
#include <vector>
#include "flutter/flow/layers/layer.h"
#include "flutter/flow/raster_cache.h"
#include "flutter/flow/raster_cache_item.h"
#include "flutter/flow/testing/mock_layer.h"
#include "flutter/testing/mock_canvas.h"
#include "third_party/skia/include/core/SkColorSpace.h"
#include "third_party/skia/include/core/SkRefCnt.h"
#include "third_party/skia/include/core/SkSize.h"
namespace flutter {
namespace testing {
/**
* @brief A RasterCacheResult implementation that represents a cached Layer or
* DisplayList without the overhead of storage.
*
* This implementation is used by MockRasterCache only for testing proper usage
* of the RasterCache in layer unit tests.
*/
class MockRasterCacheResult : public RasterCacheResult {
public:
explicit MockRasterCacheResult(SkRect device_rect);
void draw(DlCanvas& canvas,
const DlPaint* paint = nullptr,
bool preserve_rtree = false) const override{};
SkISize image_dimensions() const override {
return SkSize::Make(device_rect_.width(), device_rect_.height()).toCeil();
};
int64_t image_bytes() const override {
return image_dimensions().area() *
SkColorTypeBytesPerPixel(kBGRA_8888_SkColorType);
}
private:
SkRect device_rect_;
};
static std::vector<RasterCacheItem*> raster_cache_items_;
/**
* @brief A RasterCache implementation that simulates the act of rendering a
* Layer or DisplayList without the overhead of rasterization or pixel storage.
* This implementation is used only for testing proper usage of the RasterCache
* in layer unit tests.
*/
class MockRasterCache : public RasterCache {
public:
explicit MockRasterCache(
size_t access_threshold = 3,
size_t picture_and_display_list_cache_limit_per_frame =
RasterCacheUtil::kDefaultPictureAndDisplayListCacheLimitPerFrame)
: RasterCache(access_threshold,
picture_and_display_list_cache_limit_per_frame) {
preroll_state_stack_.set_preroll_delegate(SkMatrix::I());
paint_state_stack_.set_delegate(&mock_canvas_);
}
void AddMockLayer(int width, int height);
void AddMockPicture(int width, int height);
private:
LayerStateStack preroll_state_stack_;
LayerStateStack paint_state_stack_;
MockCanvas mock_canvas_;
sk_sp<SkColorSpace> color_space_ = SkColorSpace::MakeSRGB();
MutatorsStack mutators_stack_;
FixedRefreshRateStopwatch raster_time_;
FixedRefreshRateStopwatch ui_time_;
std::shared_ptr<TextureRegistry> texture_registry_;
PrerollContext preroll_context_ = {
// clang-format off
.raster_cache = this,
.gr_context = nullptr,
.view_embedder = nullptr,
.state_stack = preroll_state_stack_,
.dst_color_space = color_space_,
.surface_needs_readback = false,
.raster_time = raster_time_,
.ui_time = ui_time_,
.texture_registry = texture_registry_,
.has_platform_view = false,
.has_texture_layer = false,
.raster_cached_entries = &raster_cache_items_
// clang-format on
};
PaintContext paint_context_ = {
// clang-format off
.state_stack = paint_state_stack_,
.canvas = nullptr,
.gr_context = nullptr,
.dst_color_space = color_space_,
.view_embedder = nullptr,
.raster_time = raster_time_,
.ui_time = ui_time_,
.texture_registry = texture_registry_,
.raster_cache = nullptr,
// clang-format on
};
};
struct PrerollContextHolder {
PrerollContext preroll_context;
sk_sp<SkColorSpace> srgb;
};
struct PaintContextHolder {
PaintContext paint_context;
sk_sp<SkColorSpace> srgb;
};
PrerollContextHolder GetSamplePrerollContextHolder(
LayerStateStack& state_stack,
RasterCache* raster_cache,
FixedRefreshRateStopwatch* raster_time,
FixedRefreshRateStopwatch* ui_time);
PaintContextHolder GetSamplePaintContextHolder(
LayerStateStack& state_stack,
RasterCache* raster_cache,
FixedRefreshRateStopwatch* raster_time,
FixedRefreshRateStopwatch* ui_time);
bool RasterCacheItemPrerollAndTryToRasterCache(
DisplayListRasterCacheItem& display_list_item,
PrerollContext& context,
PaintContext& paint_context,
const SkMatrix& matrix);
void RasterCacheItemPreroll(DisplayListRasterCacheItem& display_list_item,
PrerollContext& context,
const SkMatrix& matrix);
bool RasterCacheItemTryToRasterCache(
DisplayListRasterCacheItem& display_list_item,
PaintContext& paint_context);
} // namespace testing
} // namespace flutter
#endif // FLUTTER_FLOW_TESTING_MOCK_RASTER_CACHE_H_