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

source code of flutter_engine/flutter/flow/testing/mock_raster_cache.h