Flutter Engine
raster_cache_unittests.cc
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 #include "flutter/flow/raster_cache.h"
6 
7 #include "flutter/flow/testing/mock_raster_cache.h"
8 #include "gtest/gtest.h"
9 #include "third_party/skia/include/core/SkCanvas.h"
10 #include "third_party/skia/include/core/SkPaint.h"
11 #include "third_party/skia/include/core/SkPicture.h"
12 #include "third_party/skia/include/core/SkPictureRecorder.h"
13 
14 namespace flutter {
15 namespace testing {
16 namespace {
17 
18 sk_sp<SkPicture> GetSamplePicture() {
19  SkPictureRecorder recorder;
20  recorder.beginRecording(SkRect::MakeWH(150, 100));
21  SkPaint paint;
22  paint.setColor(SK_ColorRED);
23  recorder.getRecordingCanvas()->drawRect(SkRect::MakeXYWH(10, 10, 80, 80),
24  paint);
25  return recorder.finishRecordingAsPicture();
26 }
27 
28 } // namespace
29 
30 TEST(RasterCache, SimpleInitialization) {
32  ASSERT_TRUE(true);
33 }
34 
35 TEST(RasterCache, ThresholdIsRespected) {
36  size_t threshold = 2;
37  flutter::RasterCache cache(threshold);
38 
39  SkMatrix matrix = SkMatrix::I();
40 
41  auto picture = GetSamplePicture();
42 
43  SkCanvas dummy_canvas;
44 
45  PrerollContextHolder preroll_context_holder = GetSamplePrerollContextHolder();
46 
47  ASSERT_FALSE(cache.Prepare(&preroll_context_holder.preroll_context,
48  picture.get(), true, false, matrix));
49  // 1st access.
50  ASSERT_FALSE(cache.Draw(*picture, dummy_canvas));
51 
52  cache.SweepAfterFrame();
53 
54  ASSERT_FALSE(cache.Prepare(&preroll_context_holder.preroll_context,
55  picture.get(), true, false, matrix));
56 
57  // 2nd access.
58  ASSERT_FALSE(cache.Draw(*picture, dummy_canvas));
59 
60  cache.SweepAfterFrame();
61 
62  // Now Prepare should cache it.
63  ASSERT_TRUE(cache.Prepare(&preroll_context_holder.preroll_context,
64  picture.get(), true, false, matrix));
65  ASSERT_TRUE(cache.Draw(*picture, dummy_canvas));
66 }
67 
68 TEST(RasterCache, AccessThresholdOfZeroDisablesCaching) {
69  size_t threshold = 0;
70  flutter::RasterCache cache(threshold);
71 
72  SkMatrix matrix = SkMatrix::I();
73 
74  auto picture = GetSamplePicture();
75 
76  SkCanvas dummy_canvas;
77 
78  PrerollContextHolder preroll_context_holder = GetSamplePrerollContextHolder();
79 
80  ASSERT_FALSE(cache.Prepare(&preroll_context_holder.preroll_context,
81  picture.get(), true, false, matrix));
82 
83  ASSERT_FALSE(cache.Draw(*picture, dummy_canvas));
84 }
85 
86 TEST(RasterCache, PictureCacheLimitPerFrameIsRespectedWhenZero) {
87  size_t picture_cache_limit_per_frame = 0;
88  flutter::RasterCache cache(3, picture_cache_limit_per_frame);
89 
90  SkMatrix matrix = SkMatrix::I();
91 
92  auto picture = GetSamplePicture();
93 
94  SkCanvas dummy_canvas;
95 
96  PrerollContextHolder preroll_context_holder = GetSamplePrerollContextHolder();
97 
98  ASSERT_FALSE(cache.Prepare(&preroll_context_holder.preroll_context,
99  picture.get(), true, false, matrix));
100 
101  ASSERT_FALSE(cache.Draw(*picture, dummy_canvas));
102 }
103 
104 TEST(RasterCache, SweepsRemoveUnusedFrames) {
105  size_t threshold = 1;
106  flutter::RasterCache cache(threshold);
107 
108  SkMatrix matrix = SkMatrix::I();
109 
110  auto picture = GetSamplePicture();
111 
112  SkCanvas dummy_canvas;
113 
114  PrerollContextHolder preroll_context_holder = GetSamplePrerollContextHolder();
115 
116  ASSERT_FALSE(cache.Prepare(&preroll_context_holder.preroll_context,
117  picture.get(), true, false, matrix)); // 1
118  ASSERT_FALSE(cache.Draw(*picture, dummy_canvas));
119 
120  cache.SweepAfterFrame();
121 
122  ASSERT_TRUE(cache.Prepare(&preroll_context_holder.preroll_context,
123  picture.get(), true, false, matrix)); // 2
124  ASSERT_TRUE(cache.Draw(*picture, dummy_canvas));
125 
126  cache.SweepAfterFrame();
127  cache.SweepAfterFrame(); // Extra frame without a Get image access.
128 
129  ASSERT_FALSE(cache.Draw(*picture, dummy_canvas));
130 }
131 
132 // Construct a cache result whose device target rectangle rounds out to be one
133 // pixel wider than the cached image. Verify that it can be drawn without
134 // triggering any assertions.
135 TEST(RasterCache, DeviceRectRoundOut) {
136  size_t threshold = 1;
137  flutter::RasterCache cache(threshold);
138 
139  SkPictureRecorder recorder;
140  SkRect logical_rect = SkRect::MakeLTRB(28, 0, 354.56731, 310.288);
141  recorder.beginRecording(logical_rect);
142  SkPaint paint;
143  paint.setColor(SK_ColorRED);
144  recorder.getRecordingCanvas()->drawRect(logical_rect, paint);
145  sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
146 
147  SkMatrix ctm = SkMatrix::MakeAll(1.3312, 0, 233, 0, 1.3312, 206, 0, 0, 1);
148 
149  SkCanvas canvas(100, 100, nullptr);
150  canvas.setMatrix(ctm);
151 
152  PrerollContextHolder preroll_context_holder = GetSamplePrerollContextHolder();
153 
154  ASSERT_FALSE(cache.Prepare(&preroll_context_holder.preroll_context,
155  picture.get(), true, false, ctm));
156  ASSERT_FALSE(cache.Draw(*picture, canvas));
157  cache.SweepAfterFrame();
158  ASSERT_TRUE(cache.Prepare(&preroll_context_holder.preroll_context,
159  picture.get(), true, false, ctm));
160  ASSERT_TRUE(cache.Draw(*picture, canvas));
161 
162  canvas.translate(248, 0);
163  ASSERT_TRUE(cache.Draw(*picture, canvas));
164 }
165 
166 } // namespace testing
167 } // namespace flutter
bool Prepare(PrerollContext *context, SkPicture *picture, bool is_complex, bool will_change, const SkMatrix &untranslated_matrix, const SkPoint &offset=SkPoint())
PrerollContextHolder GetSamplePrerollContextHolder()
bool Draw(const SkPicture &picture, SkCanvas &canvas) const
TEST(DisplayListCanvas, DrawPaint)