blob: cf138b8d4ed124720d9aac59edfa021e7de37605 [file] [log] [blame]
commit-bot@chromium.orgc4b21e62014-04-11 18:33:31 +00001/*
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
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +00008#include "Test.h"
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +00009#include "RecordTestUtils.h"
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000010
11#include "SkDebugCanvas.h"
Mike Klein271a0302014-09-23 15:28:38 -040012#include "SkDropShadowImageFilter.h"
piotaixr65151752014-10-16 11:58:39 -070013#include "SkImagePriv.h"
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000014#include "SkRecord.h"
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000015#include "SkRecordDraw.h"
Mike Klein271a0302014-09-23 15:28:38 -040016#include "SkRecordOpts.h"
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000017#include "SkRecorder.h"
18#include "SkRecords.h"
piotaixr65151752014-10-16 11:58:39 -070019#include "SkSurface.h"
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000020
21static const int W = 1920, H = 1080;
22
robertphillips783fe162015-01-07 07:28:41 -080023class JustOneDraw : public SkPicture::AbortCallback {
Mike Kleinc11530e2014-06-24 11:29:06 -040024public:
25 JustOneDraw() : fCalls(0) {}
26
mtklein72c9faa2015-01-09 10:06:39 -080027 bool abort() SK_OVERRIDE { return fCalls++ > 0; }
Mike Kleinc11530e2014-06-24 11:29:06 -040028private:
29 int fCalls;
30};
31
reed2ff1fce2014-12-11 07:07:37 -080032DEF_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::SetMatrix>(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
Mike Kleinc11530e2014-06-24 11:29:06 -040059DEF_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;
reed1bdfd3f2014-11-24 14:41:51 -080070 SkRecordDraw(record, &canvas, NULL, NULL, 0, NULL/*bbh*/, &callback);
Mike Kleinc11530e2014-06-24 11:29:06 -040071
reed2ff1fce2014-12-11 07:07:37 -080072 REPORTER_ASSERT(r, 1 == count_instances_of_type<SkRecords::DrawRect>(rerecord));
73 REPORTER_ASSERT(r, 0 == count_instances_of_type<SkRecords::ClipRect>(rerecord));
Mike Kleinc11530e2014-06-24 11:29:06 -040074}
75
76DEF_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.
reed2ff1fce2014-12-11 07:07:37 -080080 recorder.scale(2, 2);
Mike Kleinc11530e2014-06-24 11:29:06 -040081
82 SkRecord rerecord;
83 SkRecorder canvas(&rerecord, W, H);
reed1bdfd3f2014-11-24 14:41:51 -080084 SkRecordDraw(record, &canvas, NULL, NULL, 0, NULL/*bbh*/, NULL/*callback*/);
Mike Kleinc11530e2014-06-24 11:29:06 -040085
reed2ff1fce2014-12-11 07:07:37 -080086 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);
Mike Kleinc11530e2014-06-24 11:29:06 -040089}
90
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000091DEF_TEST(RecordDraw_SetMatrixClobber, r) {
92 // Set up an SkRecord that just scales by 2x,3x.
93 SkRecord scaleRecord;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000094 SkRecorder scaleCanvas(&scaleRecord, W, H);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000095 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;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +0000101 SkRecorder translateCanvas(&translateRecord, W, H);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +0000102 SkMatrix translate;
103 translate.setTranslate(20, 20);
104 translateCanvas.setMatrix(translate);
105
reed1bdfd3f2014-11-24 14:41:51 -0800106 SkRecordDraw(scaleRecord, &translateCanvas, NULL, NULL, 0, NULL/*bbh*/, NULL/*callback*/);
Mike Kleinc11530e2014-06-24 11:29:06 -0400107 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);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +0000112
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
Mike Kleinc11530e2014-06-24 11:29:06 -0400119 setMatrix = assert_type<SkRecords::SetMatrix>(r, translateRecord, 2);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +0000120 SkMatrix expected = scale;
121 expected.postConcat(translate);
122 REPORTER_ASSERT(r, setMatrix->matrix == expected);
123}
mtkleina723b572014-08-15 11:49:49 -0700124
125struct TestBBH : public SkBBoxHierarchy {
mtkleinbfd5bff2015-02-10 13:44:27 -0800126 void insert(const SkRect boundsArray[], int N) SK_OVERRIDE {
mtklein4477c3c2014-10-27 10:27:10 -0700127 fEntries.setCount(N);
128 for (int i = 0; i < N; i++) {
mtkleinbfd5bff2015-02-10 13:44:27 -0800129 Entry e = { (unsigned)i, boundsArray[i] };
mtklein4477c3c2014-10-27 10:27:10 -0700130 fEntries[i] = e;
131 }
mtkleina723b572014-08-15 11:49:49 -0700132 }
mtkleina723b572014-08-15 11:49:49 -0700133
mtklein72c9faa2015-01-09 10:06:39 -0800134 void search(const SkRect& query, SkTDArray<unsigned>* results) const SK_OVERRIDE {}
135 size_t bytesUsed() const SK_OVERRIDE { return 0; }
mtkleina723b572014-08-15 11:49:49 -0700136
137 struct Entry {
mtklein6bd41962014-10-02 07:41:56 -0700138 unsigned opIndex;
mtklein533eb782014-08-27 10:39:42 -0700139 SkRect bounds;
mtkleina723b572014-08-15 11:49:49 -0700140 };
mtklein6bd41962014-10-02 07:41:56 -0700141 SkTDArray<Entry> fEntries;
mtkleina723b572014-08-15 11:49:49 -0700142};
143
mtklein937c9c72014-09-02 15:19:48 -0700144// Like a==b, with a little slop recognizing that float equality can be weird.
145static bool sloppy_rect_eq(SkRect a, SkRect b) {
146 SkRect inset(a), outset(a);
147 inset.inset(1, 1);
148 outset.outset(1, 1);
149 return outset.contains(b) && !inset.contains(b);
150}
151
mtkleina723b572014-08-15 11:49:49 -0700152// This test is not meant to make total sense yet. It's testing the status quo
153// of SkRecordFillBounds(), which itself doesn't make total sense yet.
154DEF_TEST(RecordDraw_BBH, r) {
mtkleina723b572014-08-15 11:49:49 -0700155 SkRecord record;
mtkleina723b572014-08-15 11:49:49 -0700156 SkRecorder recorder(&record, W, H);
157 recorder.save();
158 recorder.clipRect(SkRect::MakeWH(400, 500));
159 recorder.scale(2, 2);
160 recorder.drawRect(SkRect::MakeWH(320, 240), SkPaint());
161 recorder.restore();
162
mtklein937c9c72014-09-02 15:19:48 -0700163 TestBBH bbh;
robertphillips4d52afe2014-11-03 08:19:44 -0800164 SkRecordFillBounds(SkRect::MakeWH(SkIntToScalar(W), SkIntToScalar(H)), record, &bbh);
mtkleina723b572014-08-15 11:49:49 -0700165
mtklein6bd41962014-10-02 07:41:56 -0700166 REPORTER_ASSERT(r, bbh.fEntries.count() == 5);
167 for (int i = 0; i < bbh.fEntries.count(); i++) {
168 REPORTER_ASSERT(r, bbh.fEntries[i].opIndex == (unsigned)i);
mtkleina723b572014-08-15 11:49:49 -0700169
mtklein6bd41962014-10-02 07:41:56 -0700170 REPORTER_ASSERT(r, sloppy_rect_eq(SkRect::MakeWH(400, 480), bbh.fEntries[i].bounds));
mtkleina723b572014-08-15 11:49:49 -0700171 }
172}
mtklein00f30bd2014-09-02 12:03:31 -0700173
mtklein937c9c72014-09-02 15:19:48 -0700174// A regression test for crbug.com/409110.
175DEF_TEST(RecordDraw_TextBounds, r) {
176 SkRecord record;
177 SkRecorder recorder(&record, W, H);
178
179 // Two Chinese characters in UTF-8.
180 const char text[] = { '\xe6', '\xbc', '\xa2', '\xe5', '\xad', '\x97' };
181 const size_t bytes = SK_ARRAY_COUNT(text);
182
183 const SkScalar xpos[] = { 10, 20 };
184 recorder.drawPosTextH(text, bytes, xpos, 30, SkPaint());
185
186 const SkPoint pos[] = { {40, 50}, {60, 70} };
187 recorder.drawPosText(text, bytes, pos, SkPaint());
188
189 TestBBH bbh;
robertphillips4d52afe2014-11-03 08:19:44 -0800190 SkRecordFillBounds(SkRect::MakeWH(SkIntToScalar(W), SkIntToScalar(H)), record, &bbh);
mtklein6bd41962014-10-02 07:41:56 -0700191 REPORTER_ASSERT(r, bbh.fEntries.count() == 2);
mtklein937c9c72014-09-02 15:19:48 -0700192
mtklein9a5380d2014-12-16 06:31:01 -0800193 // We can make these next assertions confidently because SkRecordFillBounds
194 // builds its bounds by overestimating font metrics in a platform-independent way.
195 // If that changes, these tests will need to be more flexible.
196 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[0].bounds, SkRect::MakeLTRB(0, 0, 140, 60)));
197 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[1].bounds, SkRect::MakeLTRB(0, 20, 180, 100)));
mtklein937c9c72014-09-02 15:19:48 -0700198}
199
mtklein00f30bd2014-09-02 12:03:31 -0700200// Base test to ensure start/stop range is respected
201DEF_TEST(RecordDraw_PartialStartStop, r) {
202 static const int kWidth = 10, kHeight = 10;
203
204 SkRect r1 = { 0, 0, kWidth, kHeight };
205 SkRect r2 = { 0, 0, kWidth, kHeight/2 };
206 SkRect r3 = { 0, 0, kWidth/2, kHeight };
207 SkPaint p;
208
209 SkRecord record;
210 SkRecorder recorder(&record, kWidth, kHeight);
211 recorder.drawRect(r1, p);
212 recorder.drawRect(r2, p);
213 recorder.drawRect(r3, p);
214
215 SkRecord rerecord;
216 SkRecorder canvas(&rerecord, kWidth, kHeight);
reed8eddfb52014-12-04 07:50:14 -0800217 SkRecordPartialDraw(record, &canvas, NULL, 0, 1, 2, SkMatrix::I()); // replay just drawRect of r2
mtklein00f30bd2014-09-02 12:03:31 -0700218
reed2ff1fce2014-12-11 07:07:37 -0800219 REPORTER_ASSERT(r, 1 == count_instances_of_type<SkRecords::DrawRect>(rerecord));
220 int index = find_first_instances_of_type<SkRecords::DrawRect>(rerecord);
221 const SkRecords::DrawRect* drawRect = assert_type<SkRecords::DrawRect>(r, rerecord, index);
mtklein00f30bd2014-09-02 12:03:31 -0700222 REPORTER_ASSERT(r, drawRect->rect == r2);
223}
224
Mike Klein271a0302014-09-23 15:28:38 -0400225// A regression test for crbug.com/415468 and skbug.com/2957.
mtklein8e393bf2014-10-01 12:48:58 -0700226//
227// This also now serves as a regression test for crbug.com/418417. We used to adjust the
228// bounds for the saveLayer, clip, and restore to be greater than the bounds of the picture.
229// (We were applying the saveLayer paint to the bounds after restore, which makes no sense.)
Mike Klein271a0302014-09-23 15:28:38 -0400230DEF_TEST(RecordDraw_SaveLayerAffectsClipBounds, r) {
231 SkRecord record;
232 SkRecorder recorder(&record, 50, 50);
233
234 // We draw a rectangle with a long drop shadow. We used to not update the clip
235 // bounds based on SaveLayer paints, so the drop shadow could be cut off.
236 SkPaint paint;
sugoi234f0362014-10-23 13:59:52 -0700237 paint.setImageFilter(SkDropShadowImageFilter::Create(20, 0, 0, 0, SK_ColorBLACK,
238 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode))->unref();
Mike Klein271a0302014-09-23 15:28:38 -0400239
240 recorder.saveLayer(NULL, &paint);
241 recorder.clipRect(SkRect::MakeWH(20, 40));
242 recorder.drawRect(SkRect::MakeWH(20, 40), SkPaint());
243 recorder.restore();
244
mtklein8e393bf2014-10-01 12:48:58 -0700245 // Under the original bug, the right edge value of the drawRect would be 20 less than asserted
246 // here because we intersected it with a clip that had not been adjusted for the drop shadow.
247 //
248 // The second bug showed up as adjusting the picture bounds (0,0,50,50) by the drop shadow too.
249 // The saveLayer, clipRect, and restore bounds were incorrectly (0,0,70,50).
Mike Klein271a0302014-09-23 15:28:38 -0400250 TestBBH bbh;
robertphillips4d52afe2014-11-03 08:19:44 -0800251 SkRecordFillBounds(SkRect::MakeWH(50, 50), record, &bbh);
mtklein6bd41962014-10-02 07:41:56 -0700252 REPORTER_ASSERT(r, bbh.fEntries.count() == 4);
253 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[0].bounds, SkRect::MakeLTRB(0, 0, 50, 50)));
254 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[1].bounds, SkRect::MakeLTRB(0, 0, 50, 50)));
255 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[2].bounds, SkRect::MakeLTRB(0, 0, 40, 40)));
256 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[3].bounds, SkRect::MakeLTRB(0, 0, 50, 50)));
Mike Klein271a0302014-09-23 15:28:38 -0400257}
piotaixr65151752014-10-16 11:58:39 -0700258
robertphillips4d52afe2014-11-03 08:19:44 -0800259// When a saveLayer provides an explicit bound and has a complex paint (e.g., one that
260// affects transparent black), that bound should serve to shrink the area of the required
261// backing store.
262DEF_TEST(RecordDraw_SaveLayerBoundsAffectsClipBounds, r) {
263 SkRecord record;
264 SkRecorder recorder(&record, 50, 50);
265
266 SkPaint p;
267 p.setXfermodeMode(SkXfermode::kSrc_Mode);
268
269 SkRect bounds = SkRect::MakeLTRB(10, 10, 40, 40);
270 recorder.saveLayer(&bounds, &p);
271 recorder.drawRect(SkRect::MakeLTRB(20, 20, 30, 30), SkPaint());
272 recorder.restore();
273
274 TestBBH bbh;
275 SkRecordFillBounds(SkRect::MakeWH(50, 50), record, &bbh);
276 REPORTER_ASSERT(r, bbh.fEntries.count() == 3);
reedd990e2f2014-12-22 11:58:30 -0800277 if (!SkCanvas::Internal_Private_GetIgnoreSaveLayerBounds()) {
278 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[0].bounds, SkRect::MakeLTRB(10, 10, 40, 40)));
279 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[1].bounds, SkRect::MakeLTRB(20, 20, 30, 30)));
280 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[2].bounds, SkRect::MakeLTRB(10, 10, 40, 40)));
281 }
robertphillips4d52afe2014-11-03 08:19:44 -0800282}
283
piotaixr65151752014-10-16 11:58:39 -0700284DEF_TEST(RecordDraw_drawImage, r){
285 class SkCanvasMock : public SkCanvas {
286 public:
mtkleinad8aa1d2014-11-11 18:52:02 -0800287 SkCanvasMock(int width, int height) : SkCanvas(width, height) {
piotaixr65151752014-10-16 11:58:39 -0700288 this->resetTestValues();
289 }
piotaixr65151752014-10-16 11:58:39 -0700290
reed41af9662015-01-05 07:49:08 -0800291 void onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
292 const SkPaint* paint) SK_OVERRIDE {
piotaixr65151752014-10-16 11:58:39 -0700293 fDrawImageCalled = true;
294 }
295
reed41af9662015-01-05 07:49:08 -0800296 void onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
297 const SkPaint* paint) SK_OVERRIDE {
piotaixr65151752014-10-16 11:58:39 -0700298 fDrawImageRectCalled = true;
299 }
300
301 void resetTestValues() {
302 fDrawImageCalled = fDrawImageRectCalled = false;
303 }
304
305 bool fDrawImageCalled;
306 bool fDrawImageRectCalled;
piotaixr65151752014-10-16 11:58:39 -0700307 };
308
reed3054be12014-12-10 07:24:28 -0800309 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(10, 10));
piotaixr65151752014-10-16 11:58:39 -0700310 surface->getCanvas()->clear(SK_ColorGREEN);
311 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
312
313 SkCanvasMock canvas(10, 10);
314
315 {
316 SkRecord record;
317 SkRecorder recorder(&record, 10, 10);
318 recorder.drawImage(image, 0, 0);
reed1bdfd3f2014-11-24 14:41:51 -0800319 SkRecordDraw(record, &canvas, NULL, NULL, 0, NULL, 0);
piotaixr65151752014-10-16 11:58:39 -0700320 }
321 REPORTER_ASSERT(r, canvas.fDrawImageCalled);
322 canvas.resetTestValues();
323
324 {
325 SkRecord record;
326 SkRecorder recorder(&record, 10, 10);
327 recorder.drawImageRect(image, 0, SkRect::MakeWH(10, 10));
reed1bdfd3f2014-11-24 14:41:51 -0800328 SkRecordDraw(record, &canvas, NULL, NULL, 0, NULL, 0);
piotaixr65151752014-10-16 11:58:39 -0700329 }
330 REPORTER_ASSERT(r, canvas.fDrawImageRectCalled);
331
332}