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