blob: 30be24c28acbc567dc4ad6691ff9001e6c46a371 [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;
reed1bdfd3f2014-11-24 14:41:51 -080044 SkRecordDraw(record, &canvas, NULL, 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);
reed1bdfd3f2014-11-24 14:41:51 -080059 SkRecordDraw(record, &canvas, NULL, 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
reed1bdfd3f2014-11-24 14:41:51 -080083 SkRecordDraw(scaleRecord, &translateCanvas, NULL, 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);
reed8eddfb52014-12-04 07:50:14 -0800194 SkRecordPartialDraw(record, &canvas, NULL, 0, 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
Mike Klein271a0302014-09-23 15:28:38 -0400205// A regression test for crbug.com/415468 and skbug.com/2957.
mtklein8e393bf2014-10-01 12:48:58 -0700206//
207// This also now serves as a regression test for crbug.com/418417. We used to adjust the
208// bounds for the saveLayer, clip, and restore to be greater than the bounds of the picture.
209// (We were applying the saveLayer paint to the bounds after restore, which makes no sense.)
Mike Klein271a0302014-09-23 15:28:38 -0400210DEF_TEST(RecordDraw_SaveLayerAffectsClipBounds, r) {
211 SkRecord record;
212 SkRecorder recorder(&record, 50, 50);
213
214 // We draw a rectangle with a long drop shadow. We used to not update the clip
215 // bounds based on SaveLayer paints, so the drop shadow could be cut off.
216 SkPaint paint;
sugoi234f0362014-10-23 13:59:52 -0700217 paint.setImageFilter(SkDropShadowImageFilter::Create(20, 0, 0, 0, SK_ColorBLACK,
218 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode))->unref();
Mike Klein271a0302014-09-23 15:28:38 -0400219
220 recorder.saveLayer(NULL, &paint);
221 recorder.clipRect(SkRect::MakeWH(20, 40));
222 recorder.drawRect(SkRect::MakeWH(20, 40), SkPaint());
223 recorder.restore();
224
mtklein8e393bf2014-10-01 12:48:58 -0700225 // Under the original bug, the right edge value of the drawRect would be 20 less than asserted
226 // here because we intersected it with a clip that had not been adjusted for the drop shadow.
227 //
228 // The second bug showed up as adjusting the picture bounds (0,0,50,50) by the drop shadow too.
229 // The saveLayer, clipRect, and restore bounds were incorrectly (0,0,70,50).
Mike Klein271a0302014-09-23 15:28:38 -0400230 TestBBH bbh;
robertphillips4d52afe2014-11-03 08:19:44 -0800231 SkRecordFillBounds(SkRect::MakeWH(50, 50), record, &bbh);
mtklein6bd41962014-10-02 07:41:56 -0700232 REPORTER_ASSERT(r, bbh.fEntries.count() == 4);
233 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[0].bounds, SkRect::MakeLTRB(0, 0, 50, 50)));
234 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[1].bounds, SkRect::MakeLTRB(0, 0, 50, 50)));
235 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[2].bounds, SkRect::MakeLTRB(0, 0, 40, 40)));
236 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[3].bounds, SkRect::MakeLTRB(0, 0, 50, 50)));
Mike Klein271a0302014-09-23 15:28:38 -0400237}
piotaixr65151752014-10-16 11:58:39 -0700238
robertphillips4d52afe2014-11-03 08:19:44 -0800239// When a saveLayer provides an explicit bound and has a complex paint (e.g., one that
240// affects transparent black), that bound should serve to shrink the area of the required
241// backing store.
242DEF_TEST(RecordDraw_SaveLayerBoundsAffectsClipBounds, r) {
243 SkRecord record;
244 SkRecorder recorder(&record, 50, 50);
245
246 SkPaint p;
247 p.setXfermodeMode(SkXfermode::kSrc_Mode);
248
249 SkRect bounds = SkRect::MakeLTRB(10, 10, 40, 40);
250 recorder.saveLayer(&bounds, &p);
251 recorder.drawRect(SkRect::MakeLTRB(20, 20, 30, 30), SkPaint());
252 recorder.restore();
253
254 TestBBH bbh;
255 SkRecordFillBounds(SkRect::MakeWH(50, 50), record, &bbh);
256 REPORTER_ASSERT(r, bbh.fEntries.count() == 3);
257 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[0].bounds, SkRect::MakeLTRB(10, 10, 40, 40)));
258 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[1].bounds, SkRect::MakeLTRB(20, 20, 30, 30)));
259 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.fEntries[2].bounds, SkRect::MakeLTRB(10, 10, 40, 40)));
260}
261
piotaixr65151752014-10-16 11:58:39 -0700262DEF_TEST(RecordDraw_drawImage, r){
263 class SkCanvasMock : public SkCanvas {
264 public:
mtkleinad8aa1d2014-11-11 18:52:02 -0800265 SkCanvasMock(int width, int height) : SkCanvas(width, height) {
piotaixr65151752014-10-16 11:58:39 -0700266 this->resetTestValues();
267 }
268 virtual ~SkCanvasMock() {}
269 virtual void drawImage(const SkImage* image, SkScalar left, SkScalar top,
270 const SkPaint* paint = NULL) SK_OVERRIDE {
271
272 fDrawImageCalled = true;
273 }
274
275 virtual void drawImageRect(const SkImage* image, const SkRect* src,
276 const SkRect& dst,
277 const SkPaint* paint = NULL) SK_OVERRIDE {
278 fDrawImageRectCalled = true;
279 }
280
281 void resetTestValues() {
282 fDrawImageCalled = fDrawImageRectCalled = false;
283 }
284
285 bool fDrawImageCalled;
286 bool fDrawImageRectCalled;
piotaixr65151752014-10-16 11:58:39 -0700287 };
288
reeda8918a02014-12-09 13:55:20 -0800289 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(10, 10));
piotaixr65151752014-10-16 11:58:39 -0700290 surface->getCanvas()->clear(SK_ColorGREEN);
291 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
292
293 SkCanvasMock canvas(10, 10);
294
295 {
296 SkRecord record;
297 SkRecorder recorder(&record, 10, 10);
298 recorder.drawImage(image, 0, 0);
reed1bdfd3f2014-11-24 14:41:51 -0800299 SkRecordDraw(record, &canvas, NULL, NULL, 0, NULL, 0);
piotaixr65151752014-10-16 11:58:39 -0700300 }
301 REPORTER_ASSERT(r, canvas.fDrawImageCalled);
302 canvas.resetTestValues();
303
304 {
305 SkRecord record;
306 SkRecorder recorder(&record, 10, 10);
307 recorder.drawImageRect(image, 0, SkRect::MakeWH(10, 10));
reed1bdfd3f2014-11-24 14:41:51 -0800308 SkRecordDraw(record, &canvas, NULL, NULL, 0, NULL, 0);
piotaixr65151752014-10-16 11:58:39 -0700309 }
310 REPORTER_ASSERT(r, canvas.fDrawImageRectCalled);
311
312}