blob: 2346655330793a293ea34f31b876a7f91f3c71ef [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;
mtklein5ad6ee12014-08-11 08:08:43 -070044 SkRecordDraw(record, &canvas, 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);
mtklein5ad6ee12014-08-11 08:08:43 -070059 SkRecordDraw(record, &canvas, 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
mtklein5ad6ee12014-08-11 08:08:43 -070083 SkRecordDraw(scaleRecord, &translateCanvas, 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 {}
mtkleina723b572014-08-15 11:49:49 -0700112
113 struct Entry {
mtklein6bd41962014-10-02 07:41:56 -0700114 unsigned opIndex;
mtklein533eb782014-08-27 10:39:42 -0700115 SkRect bounds;
mtkleina723b572014-08-15 11:49:49 -0700116 };
mtklein6bd41962014-10-02 07:41:56 -0700117 SkTDArray<Entry> fEntries;
mtkleina723b572014-08-15 11:49:49 -0700118};
119
mtklein937c9c72014-09-02 15:19:48 -0700120// Like a==b, with a little slop recognizing that float equality can be weird.
121static bool sloppy_rect_eq(SkRect a, SkRect b) {
122 SkRect inset(a), outset(a);
123 inset.inset(1, 1);
124 outset.outset(1, 1);
125 return outset.contains(b) && !inset.contains(b);
126}
127
mtkleina723b572014-08-15 11:49:49 -0700128// This test is not meant to make total sense yet. It's testing the status quo
129// of SkRecordFillBounds(), which itself doesn't make total sense yet.
130DEF_TEST(RecordDraw_BBH, r) {
mtkleina723b572014-08-15 11:49:49 -0700131 SkRecord record;
mtkleina723b572014-08-15 11:49:49 -0700132 SkRecorder recorder(&record, W, H);
133 recorder.save();
134 recorder.clipRect(SkRect::MakeWH(400, 500));
135 recorder.scale(2, 2);
136 recorder.drawRect(SkRect::MakeWH(320, 240), SkPaint());
137 recorder.restore();
138
mtklein937c9c72014-09-02 15:19:48 -0700139 TestBBH bbh;
robertphillips4d52afe2014-11-03 08:19:44 -0800140 SkRecordFillBounds(SkRect::MakeWH(SkIntToScalar(W), SkIntToScalar(H)), record, &bbh);
mtkleina723b572014-08-15 11:49:49 -0700141
mtklein6bd41962014-10-02 07:41:56 -0700142 REPORTER_ASSERT(r, bbh.fEntries.count() == 5);
143 for (int i = 0; i < bbh.fEntries.count(); i++) {
144 REPORTER_ASSERT(r, bbh.fEntries[i].opIndex == (unsigned)i);
mtkleina723b572014-08-15 11:49:49 -0700145
mtklein6bd41962014-10-02 07:41:56 -0700146 REPORTER_ASSERT(r, sloppy_rect_eq(SkRect::MakeWH(400, 480), bbh.fEntries[i].bounds));
mtkleina723b572014-08-15 11:49:49 -0700147 }
148}
mtklein00f30bd2014-09-02 12:03:31 -0700149
mtklein937c9c72014-09-02 15:19:48 -0700150// A regression test for crbug.com/409110.
151DEF_TEST(RecordDraw_TextBounds, r) {
152 SkRecord record;
153 SkRecorder recorder(&record, W, H);
154
155 // Two Chinese characters in UTF-8.
156 const char text[] = { '\xe6', '\xbc', '\xa2', '\xe5', '\xad', '\x97' };
157 const size_t bytes = SK_ARRAY_COUNT(text);
158
159 const SkScalar xpos[] = { 10, 20 };
160 recorder.drawPosTextH(text, bytes, xpos, 30, SkPaint());
161
162 const SkPoint pos[] = { {40, 50}, {60, 70} };
163 recorder.drawPosText(text, bytes, pos, SkPaint());
164
165 TestBBH bbh;
robertphillips4d52afe2014-11-03 08:19:44 -0800166 SkRecordFillBounds(SkRect::MakeWH(SkIntToScalar(W), SkIntToScalar(H)), record, &bbh);
mtklein6bd41962014-10-02 07:41:56 -0700167 REPORTER_ASSERT(r, bbh.fEntries.count() == 2);
mtklein937c9c72014-09-02 15:19:48 -0700168
mtkleined167ac2014-10-29 16:07:10 -0700169 // We can make these next assertions confidently because SkRecordFillBounds
170 // builds its bounds by overestimating font metrics in a platform-independent way.
171 // If that changes, these tests will need to be more flexible.
robertphillips4d52afe2014-11-03 08:19:44 -0800172 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[0].bounds, SkRect::MakeLTRB(0, 0, 140, 60)));
173 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[1].bounds, SkRect::MakeLTRB(0, 20, 180, 100)));
mtklein937c9c72014-09-02 15:19:48 -0700174}
175
mtklein00f30bd2014-09-02 12:03:31 -0700176// Base test to ensure start/stop range is respected
177DEF_TEST(RecordDraw_PartialStartStop, r) {
178 static const int kWidth = 10, kHeight = 10;
179
180 SkRect r1 = { 0, 0, kWidth, kHeight };
181 SkRect r2 = { 0, 0, kWidth, kHeight/2 };
182 SkRect r3 = { 0, 0, kWidth/2, kHeight };
183 SkPaint p;
184
185 SkRecord record;
186 SkRecorder recorder(&record, kWidth, kHeight);
187 recorder.drawRect(r1, p);
188 recorder.drawRect(r2, p);
189 recorder.drawRect(r3, p);
190
191 SkRecord rerecord;
192 SkRecorder canvas(&rerecord, kWidth, kHeight);
robertphillips4815fe52014-09-16 10:32:43 -0700193 SkRecordPartialDraw(record, &canvas, r1, 1, 2, SkMatrix::I()); // replay just drawRect of r2
mtklein00f30bd2014-09-02 12:03:31 -0700194
195 REPORTER_ASSERT(r, 3 == rerecord.count());
196 assert_type<SkRecords::Save> (r, rerecord, 0);
197 assert_type<SkRecords::DrawRect> (r, rerecord, 1);
198 assert_type<SkRecords::Restore> (r, rerecord, 2);
199
200 const SkRecords::DrawRect* drawRect = assert_type<SkRecords::DrawRect>(r, rerecord, 1);
201 REPORTER_ASSERT(r, drawRect->rect == r2);
202}
203
204// Check that clears are converted to drawRects
205DEF_TEST(RecordDraw_PartialClear, r) {
206 static const int kWidth = 10, kHeight = 10;
207
208 SkRect rect = { 0, 0, kWidth, kHeight };
209
210 SkRecord record;
211 SkRecorder recorder(&record, kWidth, kHeight);
212 recorder.clear(SK_ColorRED);
213
214 SkRecord rerecord;
215 SkRecorder canvas(&rerecord, kWidth, kHeight);
robertphillips4815fe52014-09-16 10:32:43 -0700216 SkRecordPartialDraw(record, &canvas, rect, 0, 1, SkMatrix::I()); // replay just the clear
mtklein00f30bd2014-09-02 12:03:31 -0700217
218 REPORTER_ASSERT(r, 3 == rerecord.count());
219 assert_type<SkRecords::Save> (r, rerecord, 0);
220 assert_type<SkRecords::DrawRect>(r, rerecord, 1);
221 assert_type<SkRecords::Restore> (r, rerecord, 2);
222
223 const SkRecords::DrawRect* drawRect = assert_type<SkRecords::DrawRect>(r, rerecord, 1);
224 REPORTER_ASSERT(r, drawRect->rect == rect);
225 REPORTER_ASSERT(r, drawRect->paint.getColor() == SK_ColorRED);
226}
Mike Klein271a0302014-09-23 15:28:38 -0400227
228// A regression test for crbug.com/415468 and skbug.com/2957.
mtklein8e393bf2014-10-01 12:48:58 -0700229//
230// This also now serves as a regression test for crbug.com/418417. We used to adjust the
231// bounds for the saveLayer, clip, and restore to be greater than the bounds of the picture.
232// (We were applying the saveLayer paint to the bounds after restore, which makes no sense.)
Mike Klein271a0302014-09-23 15:28:38 -0400233DEF_TEST(RecordDraw_SaveLayerAffectsClipBounds, r) {
234 SkRecord record;
235 SkRecorder recorder(&record, 50, 50);
236
237 // We draw a rectangle with a long drop shadow. We used to not update the clip
238 // bounds based on SaveLayer paints, so the drop shadow could be cut off.
239 SkPaint paint;
sugoi234f0362014-10-23 13:59:52 -0700240 paint.setImageFilter(SkDropShadowImageFilter::Create(20, 0, 0, 0, SK_ColorBLACK,
241 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode))->unref();
Mike Klein271a0302014-09-23 15:28:38 -0400242
243 recorder.saveLayer(NULL, &paint);
244 recorder.clipRect(SkRect::MakeWH(20, 40));
245 recorder.drawRect(SkRect::MakeWH(20, 40), SkPaint());
246 recorder.restore();
247
mtklein8e393bf2014-10-01 12:48:58 -0700248 // Under the original bug, the right edge value of the drawRect would be 20 less than asserted
249 // here because we intersected it with a clip that had not been adjusted for the drop shadow.
250 //
251 // The second bug showed up as adjusting the picture bounds (0,0,50,50) by the drop shadow too.
252 // The saveLayer, clipRect, and restore bounds were incorrectly (0,0,70,50).
Mike Klein271a0302014-09-23 15:28:38 -0400253 TestBBH bbh;
robertphillips4d52afe2014-11-03 08:19:44 -0800254 SkRecordFillBounds(SkRect::MakeWH(50, 50), record, &bbh);
mtklein6bd41962014-10-02 07:41:56 -0700255 REPORTER_ASSERT(r, bbh.fEntries.count() == 4);
256 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[0].bounds, SkRect::MakeLTRB(0, 0, 50, 50)));
257 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[1].bounds, SkRect::MakeLTRB(0, 0, 50, 50)));
258 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[2].bounds, SkRect::MakeLTRB(0, 0, 40, 40)));
259 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[3].bounds, SkRect::MakeLTRB(0, 0, 50, 50)));
Mike Klein271a0302014-09-23 15:28:38 -0400260}
piotaixr65151752014-10-16 11:58:39 -0700261
robertphillips4d52afe2014-11-03 08:19:44 -0800262// When a saveLayer provides an explicit bound and has a complex paint (e.g., one that
263// affects transparent black), that bound should serve to shrink the area of the required
264// backing store.
265DEF_TEST(RecordDraw_SaveLayerBoundsAffectsClipBounds, r) {
266 SkRecord record;
267 SkRecorder recorder(&record, 50, 50);
268
269 SkPaint p;
270 p.setXfermodeMode(SkXfermode::kSrc_Mode);
271
272 SkRect bounds = SkRect::MakeLTRB(10, 10, 40, 40);
273 recorder.saveLayer(&bounds, &p);
274 recorder.drawRect(SkRect::MakeLTRB(20, 20, 30, 30), SkPaint());
275 recorder.restore();
276
277 TestBBH bbh;
278 SkRecordFillBounds(SkRect::MakeWH(50, 50), record, &bbh);
279 REPORTER_ASSERT(r, bbh.fEntries.count() == 3);
280 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[0].bounds, SkRect::MakeLTRB(10, 10, 40, 40)));
281 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[1].bounds, SkRect::MakeLTRB(20, 20, 30, 30)));
282 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[2].bounds, SkRect::MakeLTRB(10, 10, 40, 40)));
283}
284
piotaixr65151752014-10-16 11:58:39 -0700285DEF_TEST(RecordDraw_drawImage, r){
286 class SkCanvasMock : public SkCanvas {
287 public:
mtkleinad8aa1d2014-11-11 18:52:02 -0800288 SkCanvasMock(int width, int height) : SkCanvas(width, height) {
piotaixr65151752014-10-16 11:58:39 -0700289 this->resetTestValues();
290 }
291 virtual ~SkCanvasMock() {}
292 virtual void drawImage(const SkImage* image, SkScalar left, SkScalar top,
293 const SkPaint* paint = NULL) SK_OVERRIDE {
294
295 fDrawImageCalled = true;
296 }
297
298 virtual void drawImageRect(const SkImage* image, const SkRect* src,
299 const SkRect& dst,
300 const SkPaint* paint = NULL) SK_OVERRIDE {
301 fDrawImageRectCalled = true;
302 }
303
304 void resetTestValues() {
305 fDrawImageCalled = fDrawImageRectCalled = false;
306 }
307
308 bool fDrawImageCalled;
309 bool fDrawImageRectCalled;
piotaixr65151752014-10-16 11:58:39 -0700310 };
311
312 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(10, 10));
313 surface->getCanvas()->clear(SK_ColorGREEN);
314 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
315
316 SkCanvasMock canvas(10, 10);
317
318 {
319 SkRecord record;
320 SkRecorder recorder(&record, 10, 10);
321 recorder.drawImage(image, 0, 0);
322 SkRecordDraw(record, &canvas, 0, 0);
323 }
324 REPORTER_ASSERT(r, canvas.fDrawImageCalled);
325 canvas.resetTestValues();
326
327 {
328 SkRecord record;
329 SkRecorder recorder(&record, 10, 10);
330 recorder.drawImageRect(image, 0, SkRect::MakeWH(10, 10));
331 SkRecordDraw(record, &canvas, 0, 0);
332 }
333 REPORTER_ASSERT(r, canvas.fDrawImageRectCalled);
334
335}