blob: 70f0250ccaa7307ed08939dea043db0e004494ae [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"
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000013#include "SkRecord.h"
commit-bot@chromium.orgad8ce572014-04-21 15:03:36 +000014#include "SkRecordOpts.h"
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000015#include "SkRecordDraw.h"
16#include "SkRecorder.h"
17#include "SkRecords.h"
18
19static const int W = 1920, H = 1080;
20
Mike Kleinc11530e2014-06-24 11:29:06 -040021class JustOneDraw : public SkDrawPictureCallback {
22public:
23 JustOneDraw() : fCalls(0) {}
24
25 virtual bool abortDrawing() SK_OVERRIDE { return fCalls++ > 0; }
26private:
27 int fCalls;
28};
29
30DEF_TEST(RecordDraw_Abort, r) {
31 // Record two commands.
32 SkRecord record;
33 SkRecorder recorder(&record, W, H);
34 recorder.drawRect(SkRect::MakeWH(200, 300), SkPaint());
35 recorder.clipRect(SkRect::MakeWH(100, 200));
36
37 SkRecord rerecord;
38 SkRecorder canvas(&rerecord, W, H);
39
40 JustOneDraw callback;
mtklein5ad6ee12014-08-11 08:08:43 -070041 SkRecordDraw(record, &canvas, NULL/*bbh*/, &callback);
Mike Kleinc11530e2014-06-24 11:29:06 -040042
43 REPORTER_ASSERT(r, 3 == rerecord.count());
44 assert_type<SkRecords::Save> (r, rerecord, 0);
45 assert_type<SkRecords::DrawRect>(r, rerecord, 1);
46 assert_type<SkRecords::Restore> (r, rerecord, 2);
47}
48
49DEF_TEST(RecordDraw_Unbalanced, r) {
50 SkRecord record;
51 SkRecorder recorder(&record, W, H);
52 recorder.save(); // We won't balance this, but SkRecordDraw will for us.
53
54 SkRecord rerecord;
55 SkRecorder canvas(&rerecord, W, H);
mtklein5ad6ee12014-08-11 08:08:43 -070056 SkRecordDraw(record, &canvas, NULL/*bbh*/, NULL/*callback*/);
Mike Kleinc11530e2014-06-24 11:29:06 -040057
58 REPORTER_ASSERT(r, 4 == rerecord.count());
59 assert_type<SkRecords::Save> (r, rerecord, 0);
60 assert_type<SkRecords::Save> (r, rerecord, 1);
61 assert_type<SkRecords::Restore> (r, rerecord, 2);
62 assert_type<SkRecords::Restore> (r, rerecord, 3);
63}
64
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000065DEF_TEST(RecordDraw_SetMatrixClobber, r) {
66 // Set up an SkRecord that just scales by 2x,3x.
67 SkRecord scaleRecord;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000068 SkRecorder scaleCanvas(&scaleRecord, W, H);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000069 SkMatrix scale;
70 scale.setScale(2, 3);
71 scaleCanvas.setMatrix(scale);
72
73 // Set up an SkRecord with an initial +20, +20 translate.
74 SkRecord translateRecord;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000075 SkRecorder translateCanvas(&translateRecord, W, H);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000076 SkMatrix translate;
77 translate.setTranslate(20, 20);
78 translateCanvas.setMatrix(translate);
79
mtklein5ad6ee12014-08-11 08:08:43 -070080 SkRecordDraw(scaleRecord, &translateCanvas, NULL/*bbh*/, NULL/*callback*/);
Mike Kleinc11530e2014-06-24 11:29:06 -040081 REPORTER_ASSERT(r, 4 == translateRecord.count());
82 assert_type<SkRecords::SetMatrix>(r, translateRecord, 0);
83 assert_type<SkRecords::Save> (r, translateRecord, 1);
84 assert_type<SkRecords::SetMatrix>(r, translateRecord, 2);
85 assert_type<SkRecords::Restore> (r, translateRecord, 3);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000086
87 // When we look at translateRecord now, it should have its first +20,+20 translate,
88 // then a 2x,3x scale that's been concatted with that +20,+20 translate.
89 const SkRecords::SetMatrix* setMatrix;
90 setMatrix = assert_type<SkRecords::SetMatrix>(r, translateRecord, 0);
91 REPORTER_ASSERT(r, setMatrix->matrix == translate);
92
Mike Kleinc11530e2014-06-24 11:29:06 -040093 setMatrix = assert_type<SkRecords::SetMatrix>(r, translateRecord, 2);
commit-bot@chromium.org0a98d872014-05-19 15:15:24 +000094 SkMatrix expected = scale;
95 expected.postConcat(translate);
96 REPORTER_ASSERT(r, setMatrix->matrix == expected);
97}
mtkleina723b572014-08-15 11:49:49 -070098
99struct TestBBH : public SkBBoxHierarchy {
mtklein533eb782014-08-27 10:39:42 -0700100 virtual void insert(void* data, const SkRect& bounds, bool defer) SK_OVERRIDE {
mtkleina723b572014-08-15 11:49:49 -0700101 Entry e = { (uintptr_t)data, bounds };
102 entries.push(e);
103 }
104 virtual int getCount() const SK_OVERRIDE { return entries.count(); }
105
106 virtual void flushDeferredInserts() SK_OVERRIDE {}
107
mtklein533eb782014-08-27 10:39:42 -0700108 virtual void search(const SkRect& query, SkTDArray<void*>* results) const SK_OVERRIDE {}
mtkleina723b572014-08-15 11:49:49 -0700109 virtual void clear() SK_OVERRIDE {}
110 virtual void rewindInserts() SK_OVERRIDE {}
111 virtual int getDepth() const SK_OVERRIDE { return -1; }
112
113 struct Entry {
114 uintptr_t data;
mtklein533eb782014-08-27 10:39:42 -0700115 SkRect bounds;
mtkleina723b572014-08-15 11:49:49 -0700116 };
117 SkTDArray<Entry> entries;
118};
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;
mtkleina723b572014-08-15 11:49:49 -0700140 SkRecordFillBounds(record, &bbh);
141
142 REPORTER_ASSERT(r, bbh.entries.count() == 5);
143 for (int i = 0; i < bbh.entries.count(); i++) {
144 REPORTER_ASSERT(r, bbh.entries[i].data == (uintptr_t)i);
145
mtklein937c9c72014-09-02 15:19:48 -0700146 REPORTER_ASSERT(r, sloppy_rect_eq(SkRect::MakeWH(400, 480), bbh.entries[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;
166 SkRecordFillBounds(record, &bbh);
167 REPORTER_ASSERT(r, bbh.entries.count() == 2);
168
169 // 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.
172 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.entries[0].bounds, SkRect::MakeLTRB(-86, 6, 116, 54)));
173 REPORTER_ASSERT(r, sloppy_rect_eq(bbh.entries[1].bounds, SkRect::MakeLTRB(-56, 26, 156, 94)));
174}
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}