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