xref: /aosp_15_r20/external/skia/tests/RecordDrawTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2014 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/core/SkBBHFactory.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkImageFilter.h"
12 #include "include/core/SkImageInfo.h"
13 #include "include/core/SkM44.h"
14 #include "include/core/SkMatrix.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkPicture.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkSurface.h"
20 #include "include/effects/SkImageFilters.h"
21 #include "include/private/base/SkTemplates.h"
22 #include "src/core/SkRecord.h"
23 #include "src/core/SkRecordDraw.h"
24 #include "src/core/SkRecorder.h"
25 #include "src/core/SkRecords.h"
26 #include "tests/RecordTestUtils.h"
27 #include "tests/Test.h"
28 
29 using namespace skia_private;
30 
31 class SkImage;
32 
33 static const int W = 1920, H = 1080;
34 
35 class JustOneDraw : public SkPicture::AbortCallback {
36 public:
JustOneDraw()37     JustOneDraw() : fCalls(0) {}
38 
abort()39     bool abort() override { return fCalls++ > 0; }
40 private:
41     int fCalls;
42 };
43 
DEF_TEST(RecordDraw_LazySaves,r)44 DEF_TEST(RecordDraw_LazySaves, r) {
45     // Record two commands.
46     SkRecord record;
47     SkRecorder recorder(&record, W, H);
48 
49     REPORTER_ASSERT(r, 0 == record.count());
50     recorder.save();
51     REPORTER_ASSERT(r, 0 == record.count());    // the save was not recorded (yet)
52     recorder.drawColor(SK_ColorRED);
53     REPORTER_ASSERT(r, 1 == record.count());
54     recorder.scale(2, 2);
55     REPORTER_ASSERT(r, 3 == record.count());    // now we see the save
56     recorder.restore();
57     REPORTER_ASSERT(r, 4 == record.count());
58 
59     assert_type<SkRecords::DrawPaint>(r, record, 0);
60     assert_type<SkRecords::Save>     (r, record, 1);
61     assert_type<SkRecords::Scale>    (r, record, 2);
62     assert_type<SkRecords::Restore>  (r, record, 3);
63 
64     recorder.save();
65     recorder.save();
66     recorder.restore();
67     recorder.restore();
68     REPORTER_ASSERT(r, 4 == record.count());
69 }
70 
DEF_TEST(RecordDraw_Abort,r)71 DEF_TEST(RecordDraw_Abort, r) {
72     // Record two commands.
73     SkRecord record;
74     SkRecorder recorder(&record, W, H);
75     recorder.drawRect(SkRect::MakeWH(200, 300), SkPaint());
76     recorder.clipRect(SkRect::MakeWH(100, 200));
77 
78     SkRecord rerecord;
79     SkRecorder canvas(&rerecord, W, H);
80 
81     JustOneDraw callback;
82     SkRecordDraw(record, &canvas, nullptr, nullptr, 0, nullptr/*bbh*/, &callback);
83 
84     REPORTER_ASSERT(r, 1 == count_instances_of_type<SkRecords::DrawRect>(rerecord));
85     REPORTER_ASSERT(r, 0 == count_instances_of_type<SkRecords::ClipRect>(rerecord));
86 }
87 
DEF_TEST(RecordDraw_Unbalanced,r)88 DEF_TEST(RecordDraw_Unbalanced, r) {
89     SkRecord record;
90     SkRecorder recorder(&record, W, H);
91     recorder.save();  // We won't balance this, but SkRecordDraw will for us.
92     recorder.scale(2, 2);
93 
94     SkRecord rerecord;
95     SkRecorder canvas(&rerecord, W, H);
96     SkRecordDraw(record, &canvas, nullptr, nullptr, 0, nullptr/*bbh*/, nullptr/*callback*/);
97 
98     int save_count = count_instances_of_type<SkRecords::Save>(rerecord);
99     int restore_count = count_instances_of_type<SkRecords::Save>(rerecord);
100     REPORTER_ASSERT(r, save_count == restore_count);
101 }
102 
DEF_TEST(RecordDraw_SetMatrixClobber,r)103 DEF_TEST(RecordDraw_SetMatrixClobber, r) {
104     // Set up an SkRecord that just scales by 2x,3x.
105     SkRecord scaleRecord;
106     SkRecorder scaleCanvas(&scaleRecord, W, H);
107     SkMatrix scale;
108     scale.setScale(2, 3);
109     scaleCanvas.setMatrix(scale);
110 
111     // Set up an SkRecord with an initial +20, +20 translate.
112     SkRecord translateRecord;
113     SkRecorder translateCanvas(&translateRecord, W, H);
114     SkMatrix translate;
115     translate.setTranslate(20, 20);
116     translateCanvas.setMatrix(translate);
117 
118     SkRecordDraw(scaleRecord, &translateCanvas, nullptr, nullptr, 0, nullptr/*bbh*/, nullptr/*callback*/);
119     REPORTER_ASSERT(r, 4 == translateRecord.count());
120     assert_type<SkRecords::SetM44>(r, translateRecord, 0);
121     assert_type<SkRecords::Save>  (r, translateRecord, 1);
122     assert_type<SkRecords::SetM44>(r, translateRecord, 2);
123     assert_type<SkRecords::Restore>  (r, translateRecord, 3);
124 
125     // When we look at translateRecord now, it should have its first +20,+20 translate,
126     // then a 2x,3x scale that's been concatted with that +20,+20 translate.
127     const SkRecords::SetM44* setMatrix;
128     setMatrix = assert_type<SkRecords::SetM44>(r, translateRecord, 0);
129     REPORTER_ASSERT(r, setMatrix->matrix == SkM44(translate));
130 
131     setMatrix = assert_type<SkRecords::SetM44>(r, translateRecord, 2);
132     SkMatrix expected = scale;
133     expected.postConcat(translate);
134     REPORTER_ASSERT(r, setMatrix->matrix == SkM44(expected));
135 }
136 
137 // Like a==b, with a little slop recognizing that float equality can be weird.
sloppy_rect_eq(SkRect a,SkRect b)138 static bool sloppy_rect_eq(SkRect a, SkRect b) {
139     SkRect inset(a), outset(a);
140     inset.inset(1, 1);
141     outset.outset(1, 1);
142     return outset.contains(b) && !inset.contains(b);
143 }
144 
145 // TODO This would be nice, but we can't get it right today.
146 #if 0
147 DEF_TEST(RecordDraw_BasicBounds, r) {
148     SkRecord record;
149     SkRecorder recorder(&record, W, H);
150     recorder.save();
151         recorder.clipRect(SkRect::MakeWH(400, 500));
152         recorder.scale(2, 2);
153         recorder.drawRect(SkRect::MakeWH(320, 240), SkPaint());
154     recorder.restore();
155 
156     AutoTArray<SkRect> bounds(record.count());
157     SkRecordFillBounds(SkRect::MakeWH(SkIntToScalar(W), SkIntToScalar(H)), record, bounds.data());
158 
159     for (int i = 0; i < record.count(); i++) {
160         REPORTER_ASSERT(r, sloppy_rect_eq(SkRect::MakeWH(400, 480), bounds[i]));
161     }
162 }
163 #endif
164 
165 // A regression test for crbug.com/415468 and https://bug.skia.org/2957 .
166 //
167 // This also now serves as a regression test for crbug.com/418417.  We used to adjust the
168 // bounds for the saveLayer, clip, and restore to be greater than the bounds of the picture.
169 // (We were applying the saveLayer paint to the bounds after restore, which makes no sense.)
DEF_TEST(RecordDraw_SaveLayerAffectsClipBounds,r)170 DEF_TEST(RecordDraw_SaveLayerAffectsClipBounds, r) {
171     SkRecord record;
172     SkRecorder recorder(&record, 50, 50);
173 
174     // We draw a rectangle with a long drop shadow.  We used to not update the clip
175     // bounds based on SaveLayer paints, so the drop shadow could be cut off.
176     SkPaint paint;
177     paint.setImageFilter(SkImageFilters::DropShadow(20, 0, 0, 0, SK_ColorBLACK,  nullptr));
178 
179     recorder.saveLayer(nullptr, &paint);
180         recorder.clipRect(SkRect::MakeWH(20, 40));
181         recorder.drawRect(SkRect::MakeWH(20, 40), SkPaint());
182     recorder.restore();
183 
184     // Under the original bug, the right edge value of the drawRect would be 20 less than asserted
185     // here because we intersected it with a clip that had not been adjusted for the drop shadow.
186     //
187     // The second bug showed up as adjusting the picture bounds (0,0,50,50) by the drop shadow too.
188     // The saveLayer, clipRect, and restore bounds were incorrectly (0,0,70,50).
189     //
190     // Now, all recorded bounds should be (0,0,40,40), representing the union of the original
191     // draw/clip (0,0,20,40) with the 20px offset drop shadow along the x-axis (20,0,40,40).
192     // The saveLayer and restore match the output bounds of the drop shadow filter, instead of
193     // expanding to fill the entire picture.
194     AutoTArray<SkRect> bounds(record.count());
195     AutoTMalloc<SkBBoxHierarchy::Metadata> meta(record.count());
196     SkRecordFillBounds(SkRect::MakeWH(50, 50), record, bounds.data(), meta);
197     REPORTER_ASSERT(r, sloppy_rect_eq(bounds[0], SkRect::MakeLTRB(0, 0, 40, 40)));
198     REPORTER_ASSERT(r, sloppy_rect_eq(bounds[1], SkRect::MakeLTRB(0, 0, 40, 40)));
199     REPORTER_ASSERT(r, sloppy_rect_eq(bounds[2], SkRect::MakeLTRB(0, 0, 40, 40)));
200     REPORTER_ASSERT(r, sloppy_rect_eq(bounds[3], SkRect::MakeLTRB(0, 0, 40, 40)));
201 }
202 
203 // A regression test for https://g-issues.skia.org/issues/362552959.
DEF_TEST(RecordDraw_EmptySaveLayerWithBackdropFilterAffectsCullRect,r)204 DEF_TEST(RecordDraw_EmptySaveLayerWithBackdropFilterAffectsCullRect, r) {
205     SkRecord record;
206     SkRecorder recorder(&record, 50, 50);
207 
208     auto blurFilter = SkImageFilters::Blur(5.0, 5.0, SkTileMode::kDecal, nullptr);
209 
210     recorder.save();
211         recorder.clipRect(SkRect::MakeWH(40, 40));
212         recorder.saveLayer(SkCanvas::SaveLayerRec(nullptr, nullptr, blurFilter.get(),
213                            SkTileMode::kDecal, nullptr, 0));
214         recorder.restore();
215     recorder.restore();
216 
217     // Even though there is nothing drawn inside the saveLayer, the bounds should still fill the
218     // the clip region due to it affecting the backdrop.
219     AutoTArray<SkRect> bounds(record.count());
220     AutoTMalloc<SkBBoxHierarchy::Metadata> meta(record.count());
221     SkRecordFillBounds(SkRect::MakeWH(50, 50), record, bounds.data(), meta);
222     REPORTER_ASSERT(r, sloppy_rect_eq(bounds[0], SkRect::MakeLTRB(0, 0, 40, 40)));
223     REPORTER_ASSERT(r, sloppy_rect_eq(bounds[1], SkRect::MakeLTRB(0, 0, 40, 40)));
224     REPORTER_ASSERT(r, sloppy_rect_eq(bounds[2], SkRect::MakeLTRB(0, 0, 40, 40)));
225     REPORTER_ASSERT(r, sloppy_rect_eq(bounds[3], SkRect::MakeLTRB(0, 0, 40, 40)));
226     REPORTER_ASSERT(r, sloppy_rect_eq(bounds[4], SkRect::MakeLTRB(0, 0, 40, 40)));
227 }
228 
DEF_TEST(RecordDraw_Metadata,r)229 DEF_TEST(RecordDraw_Metadata, r) {
230     SkRecord record;
231     SkRecorder recorder(&record, 50, 50);
232 
233     // Just doing some mildly interesting drawing, mostly grabbed from the unit test above.
234     SkPaint paint;
235     paint.setImageFilter(SkImageFilters::DropShadow(20, 0, 0, 0, SK_ColorBLACK,  nullptr));
236 
237     recorder.saveLayer(nullptr, &paint);
238         recorder.clipRect(SkRect::MakeWH(20, 40));
239         recorder.save();
240             recorder.translate(10, 10);
241             recorder.drawRect(SkRect::MakeWH(20, 40), SkPaint());
242         recorder.restore();
243     recorder.restore();
244 
245     AutoTArray<SkRect> bounds(record.count());
246     AutoTMalloc<SkBBoxHierarchy::Metadata> meta(record.count());
247     SkRecordFillBounds(SkRect::MakeWH(50, 50), record, bounds.data(), meta);
248 
249     REPORTER_ASSERT(r, !meta[0].isDraw);  // saveLayer (not a draw, but its restore will be)
250     REPORTER_ASSERT(r, !meta[1].isDraw);  //   clip
251     REPORTER_ASSERT(r, !meta[2].isDraw);  //   save
252     REPORTER_ASSERT(r, !meta[3].isDraw);  //       translate
253     REPORTER_ASSERT(r,  meta[4].isDraw);  //       drawRect
254     REPORTER_ASSERT(r, !meta[5].isDraw);  //   restore  (paired with save, not a draw)
255     REPORTER_ASSERT(r,  meta[6].isDraw);  // restore (paired with saveLayer, a draw)
256 }
257 
258 // TODO This would be nice, but we can't get it right today.
259 #if 0
260 // When a saveLayer provides an explicit bound and has a complex paint (e.g., one that
261 // affects transparent black), that bound should serve to shrink the area of the required
262 // backing store.
263 DEF_TEST(RecordDraw_SaveLayerBoundsAffectsClipBounds, r) {
264     SkRecord record;
265     SkRecorder recorder(&record, 50, 50);
266 
267     SkPaint p;
268     p.setBlendMode(SkBlendMode::kSrc);
269 
270     SkRect layerBounds = SkRect::MakeLTRB(10, 10, 40, 40);
271     recorder.saveLayer(&layerBounds, &p);
272     recorder.drawRect(SkRect::MakeLTRB(20, 20, 30, 30), SkPaint());
273     recorder.restore();
274 
275     AutoTArray<SkRect> bounds(record.count());
276     SkRecordFillBounds(SkRect::MakeWH(50, 50), record, bounds.data());
277     REPORTER_ASSERT(r, sloppy_rect_eq(bounds[0], SkRect::MakeLTRB(10, 10, 40, 40)));
278     REPORTER_ASSERT(r, sloppy_rect_eq(bounds[1], SkRect::MakeLTRB(20, 20, 30, 30)));
279     REPORTER_ASSERT(r, sloppy_rect_eq(bounds[2], SkRect::MakeLTRB(10, 10, 40, 40)));
280 }
281 #endif
282 
DEF_TEST(RecordDraw_drawImage,r)283 DEF_TEST(RecordDraw_drawImage, r){
284     class SkCanvasMock : public SkCanvas {
285     public:
286         SkCanvasMock(int width, int height) : SkCanvas(width, height) {
287             this->resetTestValues();
288         }
289 
290         void resetTestValues() {
291             fDrawImageCalled = fDrawImageRectCalled = false;
292         }
293 
294         bool fDrawImageCalled;
295         bool fDrawImageRectCalled;
296     };
297 
298     auto surface(SkSurfaces::Raster(SkImageInfo::MakeN32Premul(10, 10)));
299     surface->getCanvas()->clear(SK_ColorGREEN);
300     sk_sp<SkImage> image(surface->makeImageSnapshot());
301 
302     SkCanvasMock canvas(10, 10);
303 }
304