blob: b7538f1e9a7c308cce035ed19d964c71daa38351 [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 Kleinc11530e2014-06-24 11:29:06 -040012#include "SkDrawPictureCallback.h"
Mike Klein271a0302014-09-23 15:28:38 -040013#include "SkDropShadowImageFilter.h"
piotaixr65151752014-10-16 11:58:39 -070014#include "SkImagePriv.h"
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000015#include "SkRecord.h"
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000016#include "SkRecordDraw.h"
Mike Klein271a0302014-09-23 15:28:38 -040017#include "SkRecordOpts.h"
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000018#include "SkRecorder.h"
19#include "SkRecords.h"
piotaixr65151752014-10-16 11:58:39 -070020#include "SkSurface.h"
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000021
22static const int W = 1920, H = 1080;
23
Mike Kleinc11530e2014-06-24 11:29:06 -040024class JustOneDraw : public SkDrawPictureCallback {
25public:
26 JustOneDraw() : fCalls(0) {}
27
28 virtual bool abortDrawing() SK_OVERRIDE { return fCalls++ > 0; }
29private:
30 int fCalls;
31};
32
33DEF_TEST(RecordDraw_Abort, r) {
34 // Record two commands.
35 SkRecord record;
36 SkRecorder recorder(&record, W, H);
37 recorder.drawRect(SkRect::MakeWH(200, 300), SkPaint());
38 recorder.clipRect(SkRect::MakeWH(100, 200));
39
40 SkRecord rerecord;
41 SkRecorder canvas(&rerecord, W, H);
42
43 JustOneDraw callback;
reed6be2aa92014-11-18 11:08:05 -080044 SkRecordDraw(record, &canvas, NULL, 0, NULL/*bbh*/, &callback);
Mike Kleinc11530e2014-06-24 11:29:06 -040045
46 REPORTER_ASSERT(r, 3 == rerecord.count());
47 assert_type<SkRecords::Save> (r, rerecord, 0);
48 assert_type<SkRecords::DrawRect>(r, rerecord, 1);
49 assert_type<SkRecords::Restore> (r, rerecord, 2);
50}
51
52DEF_TEST(RecordDraw_Unbalanced, r) {
53 SkRecord record;
54 SkRecorder recorder(&record, W, H);
55 recorder.save(); // We won't balance this, but SkRecordDraw will for us.
56
57 SkRecord rerecord;
58 SkRecorder canvas(&rerecord, W, H);
reed6be2aa92014-11-18 11:08:05 -080059 SkRecordDraw(record, &canvas, NULL, 0, NULL/*bbh*/, NULL/*callback*/);
Mike Kleinc11530e2014-06-24 11:29:06 -040060
61 REPORTER_ASSERT(r, 4 == rerecord.count());
62 assert_type<SkRecords::Save> (r, rerecord, 0);
63 assert_type<SkRecords::Save> (r, rerecord, 1);
64 assert_type<SkRecords::Restore> (r, rerecord, 2);
65 assert_type<SkRecords::Restore> (r, rerecord, 3);
66}
67
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000068DEF_TEST(RecordDraw_SetMatrixClobber, r) {
69 // Set up an SkRecord that just scales by 2x,3x.
70 SkRecord scaleRecord;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000071 SkRecorder scaleCanvas(&scaleRecord, W, H);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000072 SkMatrix scale;
73 scale.setScale(2, 3);
74 scaleCanvas.setMatrix(scale);
75
76 // Set up an SkRecord with an initial +20, +20 translate.
77 SkRecord translateRecord;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000078 SkRecorder translateCanvas(&translateRecord, W, H);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000079 SkMatrix translate;
80 translate.setTranslate(20, 20);
81 translateCanvas.setMatrix(translate);
82
reed6be2aa92014-11-18 11:08:05 -080083 SkRecordDraw(scaleRecord, &translateCanvas, NULL, 0, NULL/*bbh*/, NULL/*callback*/);
Mike Kleinc11530e2014-06-24 11:29:06 -040084 REPORTER_ASSERT(r, 4 == translateRecord.count());
85 assert_type<SkRecords::SetMatrix>(r, translateRecord, 0);
86 assert_type<SkRecords::Save> (r, translateRecord, 1);
87 assert_type<SkRecords::SetMatrix>(r, translateRecord, 2);
88 assert_type<SkRecords::Restore> (r, translateRecord, 3);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000089
90 // When we look at translateRecord now, it should have its first +20,+20 translate,
91 // then a 2x,3x scale that's been concatted with that +20,+20 translate.
92 const SkRecords::SetMatrix* setMatrix;
93 setMatrix = assert_type<SkRecords::SetMatrix>(r, translateRecord, 0);
94 REPORTER_ASSERT(r, setMatrix->matrix == translate);
95
Mike Kleinc11530e2014-06-24 11:29:06 -040096 setMatrix = assert_type<SkRecords::SetMatrix>(r, translateRecord, 2);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000097 SkMatrix expected = scale;
98 expected.postConcat(translate);
99 REPORTER_ASSERT(r, setMatrix->matrix == expected);
100}
mtkleina723b572014-08-15 11:49:49 -0700101
102struct TestBBH : public SkBBoxHierarchy {
mtklein4477c3c2014-10-27 10:27:10 -0700103 virtual void insert(SkAutoTMalloc<SkRect>* boundsArray, int N) SK_OVERRIDE {
104 fEntries.setCount(N);
105 for (int i = 0; i < N; i++) {
106 Entry e = { (unsigned)i, (*boundsArray)[i] };
107 fEntries[i] = e;
108 }
mtkleina723b572014-08-15 11:49:49 -0700109 }
mtkleina723b572014-08-15 11:49:49 -0700110
mtklein6bd41962014-10-02 07:41:56 -0700111 virtual void search(const SkRect& query, SkTDArray<unsigned>* results) const SK_OVERRIDE {}
tomhudson158fcaa2014-11-19 10:41:14 -0800112 virtual size_t bytesUsed() const SK_OVERRIDE { return 0; }
mtkleina723b572014-08-15 11:49:49 -0700113
114 struct Entry {
mtklein6bd41962014-10-02 07:41:56 -0700115 unsigned opIndex;
mtklein533eb782014-08-27 10:39:42 -0700116 SkRect bounds;
mtkleina723b572014-08-15 11:49:49 -0700117 };
mtklein6bd41962014-10-02 07:41:56 -0700118 SkTDArray<Entry> fEntries;
mtkleina723b572014-08-15 11:49:49 -0700119};
120
mtklein937c9c72014-09-02 15:19:48 -0700121// Like a==b, with a little slop recognizing that float equality can be weird.
122static bool sloppy_rect_eq(SkRect a, SkRect b) {
123 SkRect inset(a), outset(a);
124 inset.inset(1, 1);
125 outset.outset(1, 1);
126 return outset.contains(b) && !inset.contains(b);
127}
128
mtkleina723b572014-08-15 11:49:49 -0700129// This test is not meant to make total sense yet. It's testing the status quo
130// of SkRecordFillBounds(), which itself doesn't make total sense yet.
131DEF_TEST(RecordDraw_BBH, r) {
mtkleina723b572014-08-15 11:49:49 -0700132 SkRecord record;
mtkleina723b572014-08-15 11:49:49 -0700133 SkRecorder recorder(&record, W, H);
134 recorder.save();
135 recorder.clipRect(SkRect::MakeWH(400, 500));
136 recorder.scale(2, 2);
137 recorder.drawRect(SkRect::MakeWH(320, 240), SkPaint());
138 recorder.restore();
139
mtklein937c9c72014-09-02 15:19:48 -0700140 TestBBH bbh;
robertphillips4d52afe2014-11-03 08:19:44 -0800141 SkRecordFillBounds(SkRect::MakeWH(SkIntToScalar(W), SkIntToScalar(H)), record, &bbh);
mtkleina723b572014-08-15 11:49:49 -0700142
mtklein6bd41962014-10-02 07:41:56 -0700143 REPORTER_ASSERT(r, bbh.fEntries.count() == 5);
144 for (int i = 0; i < bbh.fEntries.count(); i++) {
145 REPORTER_ASSERT(r, bbh.fEntries[i].opIndex == (unsigned)i);
mtkleina723b572014-08-15 11:49:49 -0700146
mtklein6bd41962014-10-02 07:41:56 -0700147 REPORTER_ASSERT(r, sloppy_rect_eq(SkRect::MakeWH(400, 480), bbh.fEntries[i].bounds));
mtkleina723b572014-08-15 11:49:49 -0700148 }
149}
mtklein00f30bd2014-09-02 12:03:31 -0700150
mtklein937c9c72014-09-02 15:19:48 -0700151// A regression test for crbug.com/409110.
152DEF_TEST(RecordDraw_TextBounds, r) {
153 SkRecord record;
154 SkRecorder recorder(&record, W, H);
155
156 // Two Chinese characters in UTF-8.
157 const char text[] = { '\xe6', '\xbc', '\xa2', '\xe5', '\xad', '\x97' };
158 const size_t bytes = SK_ARRAY_COUNT(text);
159
160 const SkScalar xpos[] = { 10, 20 };
161 recorder.drawPosTextH(text, bytes, xpos, 30, SkPaint());
162
163 const SkPoint pos[] = { {40, 50}, {60, 70} };
164 recorder.drawPosText(text, bytes, pos, SkPaint());
165
166 TestBBH bbh;
robertphillips4d52afe2014-11-03 08:19:44 -0800167 SkRecordFillBounds(SkRect::MakeWH(SkIntToScalar(W), SkIntToScalar(H)), record, &bbh);
mtklein6bd41962014-10-02 07:41:56 -0700168 REPORTER_ASSERT(r, bbh.fEntries.count() == 2);
mtklein937c9c72014-09-02 15:19:48 -0700169
mtkleined167ac2014-10-29 16:07:10 -0700170 // We can make these next assertions confidently because SkRecordFillBounds
171 // builds its bounds by overestimating font metrics in a platform-independent way.
172 // If that changes, these tests will need to be more flexible.
robertphillips4d52afe2014-11-03 08:19:44 -0800173 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[0].bounds, SkRect::MakeLTRB(0, 0, 140, 60)));
174 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[1].bounds, SkRect::MakeLTRB(0, 20, 180, 100)));
mtklein937c9c72014-09-02 15:19:48 -0700175}
176
mtklein00f30bd2014-09-02 12:03:31 -0700177// Base test to ensure start/stop range is respected
178DEF_TEST(RecordDraw_PartialStartStop, r) {
179 static const int kWidth = 10, kHeight = 10;
180
181 SkRect r1 = { 0, 0, kWidth, kHeight };
182 SkRect r2 = { 0, 0, kWidth, kHeight/2 };
183 SkRect r3 = { 0, 0, kWidth/2, kHeight };
184 SkPaint p;
185
186 SkRecord record;
187 SkRecorder recorder(&record, kWidth, kHeight);
188 recorder.drawRect(r1, p);
189 recorder.drawRect(r2, p);
190 recorder.drawRect(r3, p);
191
192 SkRecord rerecord;
193 SkRecorder canvas(&rerecord, kWidth, kHeight);
reed6be2aa92014-11-18 11:08:05 -0800194 SkRecordPartialDraw(record, &canvas, NULL, 0, r1, 1, 2, SkMatrix::I()); // replay just drawRect of r2
mtklein00f30bd2014-09-02 12:03:31 -0700195
196 REPORTER_ASSERT(r, 3 == rerecord.count());
197 assert_type<SkRecords::Save> (r, rerecord, 0);
198 assert_type<SkRecords::DrawRect> (r, rerecord, 1);
199 assert_type<SkRecords::Restore> (r, rerecord, 2);
200
201 const SkRecords::DrawRect* drawRect = assert_type<SkRecords::DrawRect>(r, rerecord, 1);
202 REPORTER_ASSERT(r, drawRect->rect == r2);
203}
204
205// Check that clears are converted to drawRects
206DEF_TEST(RecordDraw_PartialClear, r) {
207 static const int kWidth = 10, kHeight = 10;
208
209 SkRect rect = { 0, 0, kWidth, kHeight };
210
211 SkRecord record;
212 SkRecorder recorder(&record, kWidth, kHeight);
213 recorder.clear(SK_ColorRED);
214
215 SkRecord rerecord;
216 SkRecorder canvas(&rerecord, kWidth, kHeight);
reed6be2aa92014-11-18 11:08:05 -0800217 SkRecordPartialDraw(record, &canvas, NULL, 0, rect, 0, 1, SkMatrix::I()); // replay just the clear
mtklein00f30bd2014-09-02 12:03:31 -0700218
219 REPORTER_ASSERT(r, 3 == rerecord.count());
220 assert_type<SkRecords::Save> (r, rerecord, 0);
221 assert_type<SkRecords::DrawRect>(r, rerecord, 1);
222 assert_type<SkRecords::Restore> (r, rerecord, 2);
223
224 const SkRecords::DrawRect* drawRect = assert_type<SkRecords::DrawRect>(r, rerecord, 1);
225 REPORTER_ASSERT(r, drawRect->rect == rect);
226 REPORTER_ASSERT(r, drawRect->paint.getColor() == SK_ColorRED);
227}
Mike Klein271a0302014-09-23 15:28:38 -0400228
229// A regression test for crbug.com/415468 and skbug.com/2957.
mtklein8e393bf2014-10-01 12:48:58 -0700230//
231// This also now serves as a regression test for crbug.com/418417. We used to adjust the
232// bounds for the saveLayer, clip, and restore to be greater than the bounds of the picture.
233// (We were applying the saveLayer paint to the bounds after restore, which makes no sense.)
Mike Klein271a0302014-09-23 15:28:38 -0400234DEF_TEST(RecordDraw_SaveLayerAffectsClipBounds, r) {
235 SkRecord record;
236 SkRecorder recorder(&record, 50, 50);
237
238 // We draw a rectangle with a long drop shadow. We used to not update the clip
239 // bounds based on SaveLayer paints, so the drop shadow could be cut off.
240 SkPaint paint;
sugoi234f0362014-10-23 13:59:52 -0700241 paint.setImageFilter(SkDropShadowImageFilter::Create(20, 0, 0, 0, SK_ColorBLACK,
242 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode))->unref();
Mike Klein271a0302014-09-23 15:28:38 -0400243
244 recorder.saveLayer(NULL, &paint);
245 recorder.clipRect(SkRect::MakeWH(20, 40));
246 recorder.drawRect(SkRect::MakeWH(20, 40), SkPaint());
247 recorder.restore();
248
mtklein8e393bf2014-10-01 12:48:58 -0700249 // Under the original bug, the right edge value of the drawRect would be 20 less than asserted
250 // here because we intersected it with a clip that had not been adjusted for the drop shadow.
251 //
252 // The second bug showed up as adjusting the picture bounds (0,0,50,50) by the drop shadow too.
253 // The saveLayer, clipRect, and restore bounds were incorrectly (0,0,70,50).
Mike Klein271a0302014-09-23 15:28:38 -0400254 TestBBH bbh;
robertphillips4d52afe2014-11-03 08:19:44 -0800255 SkRecordFillBounds(SkRect::MakeWH(50, 50), record, &bbh);
mtklein6bd41962014-10-02 07:41:56 -0700256 REPORTER_ASSERT(r, bbh.fEntries.count() == 4);
257 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[0].bounds, SkRect::MakeLTRB(0, 0, 50, 50)));
258 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[1].bounds, SkRect::MakeLTRB(0, 0, 50, 50)));
259 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[2].bounds, SkRect::MakeLTRB(0, 0, 40, 40)));
260 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[3].bounds, SkRect::MakeLTRB(0, 0, 50, 50)));
Mike Klein271a0302014-09-23 15:28:38 -0400261}
piotaixr65151752014-10-16 11:58:39 -0700262
robertphillips4d52afe2014-11-03 08:19:44 -0800263// When a saveLayer provides an explicit bound and has a complex paint (e.g., one that
264// affects transparent black), that bound should serve to shrink the area of the required
265// backing store.
266DEF_TEST(RecordDraw_SaveLayerBoundsAffectsClipBounds, r) {
267 SkRecord record;
268 SkRecorder recorder(&record, 50, 50);
269
270 SkPaint p;
271 p.setXfermodeMode(SkXfermode::kSrc_Mode);
272
273 SkRect bounds = SkRect::MakeLTRB(10, 10, 40, 40);
274 recorder.saveLayer(&bounds, &p);
275 recorder.drawRect(SkRect::MakeLTRB(20, 20, 30, 30), SkPaint());
276 recorder.restore();
277
278 TestBBH bbh;
279 SkRecordFillBounds(SkRect::MakeWH(50, 50), record, &bbh);
280 REPORTER_ASSERT(r, bbh.fEntries.count() == 3);
281 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[0].bounds, SkRect::MakeLTRB(10, 10, 40, 40)));
282 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[1].bounds, SkRect::MakeLTRB(20, 20, 30, 30)));
283 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[2].bounds, SkRect::MakeLTRB(10, 10, 40, 40)));
284}
285
piotaixr65151752014-10-16 11:58:39 -0700286DEF_TEST(RecordDraw_drawImage, r){
287 class SkCanvasMock : public SkCanvas {
288 public:
mtkleinad8aa1d2014-11-11 18:52:02 -0800289 SkCanvasMock(int width, int height) : SkCanvas(width, height) {
piotaixr65151752014-10-16 11:58:39 -0700290 this->resetTestValues();
291 }
292 virtual ~SkCanvasMock() {}
293 virtual void drawImage(const SkImage* image, SkScalar left, SkScalar top,
294 const SkPaint* paint = NULL) SK_OVERRIDE {
295
296 fDrawImageCalled = true;
297 }
298
299 virtual void drawImageRect(const SkImage* image, const SkRect* src,
300 const SkRect& dst,
301 const SkPaint* paint = NULL) SK_OVERRIDE {
302 fDrawImageRectCalled = true;
303 }
304
305 void resetTestValues() {
306 fDrawImageCalled = fDrawImageRectCalled = false;
307 }
308
309 bool fDrawImageCalled;
310 bool fDrawImageRectCalled;
piotaixr65151752014-10-16 11:58:39 -0700311 };
312
313 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(10, 10));
314 surface->getCanvas()->clear(SK_ColorGREEN);
315 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
316
317 SkCanvasMock canvas(10, 10);
318
319 {
320 SkRecord record;
321 SkRecorder recorder(&record, 10, 10);
322 recorder.drawImage(image, 0, 0);
reed6be2aa92014-11-18 11:08:05 -0800323 SkRecordDraw(record, &canvas, NULL, 0, NULL, 0);
piotaixr65151752014-10-16 11:58:39 -0700324 }
325 REPORTER_ASSERT(r, canvas.fDrawImageCalled);
326 canvas.resetTestValues();
327
328 {
329 SkRecord record;
330 SkRecorder recorder(&record, 10, 10);
331 recorder.drawImageRect(image, 0, SkRect::MakeWH(10, 10));
reed6be2aa92014-11-18 11:08:05 -0800332 SkRecordDraw(record, &canvas, NULL, 0, NULL, 0);
piotaixr65151752014-10-16 11:58:39 -0700333 }
334 REPORTER_ASSERT(r, canvas.fDrawImageRectCalled);
335
336}