blob: b3b1a37dfca281d519d053fa6b72f8d52d3baec2 [file] [log] [blame]
reed@google.com37f3ae02011-11-28 16:06:04 +00001
2/*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +00003 * Copyright 2012 Google Inc.
reed@google.com37f3ae02011-11-28 16:06:04 +00004 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +00008
9/* Description:
10 * This test defines a series of elementatry test steps that perform
11 * a single or a small group of canvas API calls. Each test step is
12 * used in several test cases that verify that different types of SkCanvas
13 * flavors and derivatives pass it and yield consistent behavior. The
14 * test cases analyse results that are queryable through the API. They do
15 * not look at rendering results.
16 *
17 * Adding test stepss:
18 * The general pattern for creating a new test step is to write a test
19 * function of the form:
20 *
rmistry@google.comd6176b02012-08-23 18:14:13 +000021 * static void MyTestStepFunction(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000022 * skiatest::Reporter* reporter,
23 * CanvasTestStep* testStep)
24 * {
25 * canvas->someCanvasAPImethod();
26 * (...)
27 * REPORTER_ASSERT_MESSAGE(reporter, (...), \
28 * testStep->assertMessage());
29 * }
30 *
31 * The definition of the test step function should be followed by an
32 * invocation of the TEST_STEP macro, which generates a class and
33 * instance for the test step:
34 *
35 * TEST_STEP(MyTestStep, MyTestStepFunction)
36 *
37 * There are also short hand macros for defining simple test steps
38 * in a single line of code. A simple test step is a one that is made
39 * of a single canvas API call.
40 *
41 * SIMPLE_TEST_STEP(MytestStep, someCanvasAPIMethod());
42 *
43 * There is another macro called SIMPLE_TEST_STEP_WITH_ASSERT that
44 * works the same way as SIMPLE_TEST_STEP, and additionally verifies
45 * that the invoked method returns a non-zero value.
46 */
reed@google.com37f3ae02011-11-28 16:06:04 +000047#include "SkBitmap.h"
48#include "SkCanvas.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000049#include "SkDeferredCanvas.h"
50#include "SkDevice.h"
51#include "SkMatrix.h"
52#include "SkNWayCanvas.h"
edisonn@google.com77909122012-10-18 15:58:23 +000053#include "SkPDFDevice.h"
54#include "SkPDFDocument.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000055#include "SkPaint.h"
56#include "SkPath.h"
57#include "SkPicture.h"
58#include "SkPictureRecord.h"
59#include "SkProxyCanvas.h"
60#include "SkRect.h"
61#include "SkRegion.h"
62#include "SkShader.h"
63#include "SkStream.h"
reed@google.com28183b42014-02-04 15:34:10 +000064#include "SkSurface.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000065#include "SkTDArray.h"
66#include "Test.h"
reed@google.com37f3ae02011-11-28 16:06:04 +000067
reed@google.com90c07ea2012-04-13 13:50:27 +000068class Canvas2CanvasClipVisitor : public SkCanvas::ClipVisitor {
69public:
70 Canvas2CanvasClipVisitor(SkCanvas* target) : fTarget(target) {}
71
72 virtual void clipRect(const SkRect& r, SkRegion::Op op, bool aa) {
73 fTarget->clipRect(r, op, aa);
74 }
75 virtual void clipPath(const SkPath& p, SkRegion::Op op, bool aa) {
76 fTarget->clipPath(p, op, aa);
77 }
78
79private:
80 SkCanvas* fTarget;
81};
82
83static void test_clipVisitor(skiatest::Reporter* reporter, SkCanvas* canvas) {
84 SkISize size = canvas->getDeviceSize();
rmistry@google.comd6176b02012-08-23 18:14:13 +000085
reed@google.com90c07ea2012-04-13 13:50:27 +000086 SkBitmap bm;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000087 bm.setConfig(SkImageInfo::MakeN32Premul(size.width(), size.height()));
reed@google.com90c07ea2012-04-13 13:50:27 +000088 SkCanvas c(bm);
89
90 Canvas2CanvasClipVisitor visitor(&c);
91 canvas->replayClips(&visitor);
92
93 REPORTER_ASSERT(reporter, c.getTotalClip() == canvas->getTotalClip());
94}
95
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000096static const int kWidth = 2;
97static const int kHeight = 2;
reed@google.com7c202932011-12-14 18:48:05 +000098
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000099// Format strings that describe the test context. The %s token is where
100// the name of the test step is inserted. The context is required for
101// disambiguating the error in the case of failures that are reported in
102// functions that are called multiple times in different contexts (test
103// cases and test steps).
104static const char* const kDefaultAssertMessageFormat = "%s";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000105static const char* const kCanvasDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000106 "Drawing test step %s with SkCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000107static const char* const kPictureDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000108 "Drawing test step %s with SkPicture";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000109static const char* const kPictureSecondDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000110 "Duplicate draw of test step %s with SkPicture";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000111static const char* const kDeferredDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000112 "Drawing test step %s with SkDeferredCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000113static const char* const kProxyDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000114 "Drawing test step %s with SkProxyCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000115static const char* const kNWayDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000116 "Drawing test step %s with SkNWayCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000117static const char* const kDeferredPreFlushAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000118 "test step %s, SkDeferredCanvas state consistency before flush";
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000119static const char* const kDeferredPostFlushPlaybackAssertMessageFormat =
120 "test step %s, SkDeferredCanvas playback canvas state consistency after flush";
junov@chromium.orgfb103892012-09-20 19:35:43 +0000121static const char* const kDeferredPostSilentFlushPlaybackAssertMessageFormat =
122 "test step %s, SkDeferredCanvas playback canvas state consistency after silent flush";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000123static const char* const kPictureResourceReuseMessageFormat =
124 "test step %s, SkPicture duplicate flattened object test";
125static const char* const kProxyStateAssertMessageFormat =
126 "test step %s, SkProxyCanvas state consistency";
127static const char* const kProxyIndirectStateAssertMessageFormat =
128 "test step %s, SkProxyCanvas indirect canvas state consistency";
129static const char* const kNWayStateAssertMessageFormat =
130 "test step %s, SkNWayCanvas state consistency";
131static const char* const kNWayIndirect1StateAssertMessageFormat =
132 "test step %s, SkNWayCanvas indirect canvas 1 state consistency";
133static const char* const kNWayIndirect2StateAssertMessageFormat =
134 "test step %s, SkNWayCanvas indirect canvas 2 state consistency";
edisonn@google.com77909122012-10-18 15:58:23 +0000135static const char* const kPdfAssertMessageFormat =
136 "PDF sanity check failed %s";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000137
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000138static void createBitmap(SkBitmap* bm, SkColor color) {
139 bm->allocN32Pixels(kWidth, kHeight);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000140 bm->eraseColor(color);
141}
142
reed@google.com28183b42014-02-04 15:34:10 +0000143static SkSurface* createSurface(SkColor color) {
144 SkSurface* surface = SkSurface::NewRasterPMColor(kWidth, kHeight);
145 surface->getCanvas()->clear(color);
146 return surface;
147}
148
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000149class CanvasTestStep;
150static SkTDArray<CanvasTestStep*>& testStepArray() {
151 static SkTDArray<CanvasTestStep*> theTests;
152 return theTests;
153}
154
155class CanvasTestStep {
156public:
edisonn@google.com77909122012-10-18 15:58:23 +0000157 CanvasTestStep(bool fEnablePdfTesting = true) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000158 *testStepArray().append() = this;
159 fAssertMessageFormat = kDefaultAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000160 this->fEnablePdfTesting = fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000161 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000162 virtual ~CanvasTestStep() { }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000163
164 virtual void draw(SkCanvas*, skiatest::Reporter*) = 0;
165 virtual const char* name() const = 0;
166
167 const char* assertMessage() {
168 fAssertMessage.printf(fAssertMessageFormat, name());
169 return fAssertMessage.c_str();
170 }
171
172 void setAssertMessageFormat(const char* format) {
173 fAssertMessageFormat = format;
174 }
175
edisonn@google.com77909122012-10-18 15:58:23 +0000176 bool enablePdfTesting() { return fEnablePdfTesting; }
177
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000178private:
179 SkString fAssertMessage;
180 const char* fAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000181 bool fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000182};
183
184///////////////////////////////////////////////////////////////////////////////
185// Constants used by test steps
186
rmistry@google.comd6176b02012-08-23 18:14:13 +0000187const SkRect kTestRect =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000188 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
189 SkIntToScalar(2), SkIntToScalar(1));
190static SkMatrix testMatrix() {
191 SkMatrix matrix;
192 matrix.reset();
193 matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
194 return matrix;
195}
196const SkMatrix kTestMatrix = testMatrix();
197static SkPath testPath() {
198 SkPath path;
199 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
200 SkIntToScalar(2), SkIntToScalar(1)));
201 return path;
202}
203const SkPath kTestPath = testPath();
204static SkRegion testRegion() {
205 SkRegion region;
206 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
207 region.setRect(rect);
208 return region;
209}
210const SkIRect kTestIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
211const SkRegion kTestRegion = testRegion();
212const SkColor kTestColor = 0x01020304;
213const SkPaint kTestPaint;
214const SkPoint kTestPoints[3] = {
215 {SkIntToScalar(0), SkIntToScalar(0)},
216 {SkIntToScalar(2), SkIntToScalar(1)},
217 {SkIntToScalar(0), SkIntToScalar(2)}
218};
219const size_t kTestPointCount = 3;
220static SkBitmap testBitmap() {
221 SkBitmap bitmap;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000222 createBitmap(&bitmap, 0x05060708);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000223 return bitmap;
224}
225SkBitmap kTestBitmap; // cannot be created during static init
226SkString kTestText("Hello World");
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000227SkPoint kTestPoints2[] = {
228 { SkIntToScalar(0), SkIntToScalar(1) },
229 { SkIntToScalar(1), SkIntToScalar(1) },
230 { SkIntToScalar(2), SkIntToScalar(1) },
231 { SkIntToScalar(3), SkIntToScalar(1) },
232 { SkIntToScalar(4), SkIntToScalar(1) },
233 { SkIntToScalar(5), SkIntToScalar(1) },
234 { SkIntToScalar(6), SkIntToScalar(1) },
235 { SkIntToScalar(7), SkIntToScalar(1) },
236 { SkIntToScalar(8), SkIntToScalar(1) },
237 { SkIntToScalar(9), SkIntToScalar(1) },
238 { SkIntToScalar(10), SkIntToScalar(1) },
239};
rmistry@google.comd6176b02012-08-23 18:14:13 +0000240
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000241
242///////////////////////////////////////////////////////////////////////////////
243// Macros for defining test steps
244
245#define TEST_STEP(NAME, FUNCTION) \
246class NAME##_TestStep : public CanvasTestStep{ \
247public: \
248 virtual void draw(SkCanvas* canvas, skiatest::Reporter* reporter) { \
249 FUNCTION (canvas, reporter, this); \
250 } \
251 virtual const char* name() const {return #NAME ;} \
252}; \
253static NAME##_TestStep NAME##_TestStepInstance;
254
edisonn@google.com77909122012-10-18 15:58:23 +0000255#define TEST_STEP_NO_PDF(NAME, FUNCTION) \
256class NAME##_TestStep : public CanvasTestStep{ \
257public: \
258 NAME##_TestStep() : CanvasTestStep(false) {} \
259 virtual void draw(SkCanvas* canvas, skiatest::Reporter* reporter) { \
260 FUNCTION (canvas, reporter, this); \
261 } \
262 virtual const char* name() const {return #NAME ;} \
263}; \
264static NAME##_TestStep NAME##_TestStepInstance;
265
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000266#define SIMPLE_TEST_STEP(NAME, CALL) \
267static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter*, \
268 CanvasTestStep*) { \
269 canvas-> CALL ; \
270} \
271TEST_STEP(NAME, NAME##TestStep )
272
273#define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
274static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter* reporter, \
275 CanvasTestStep* testStep) { \
276 REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \
277 testStep->assertMessage()); \
278} \
279TEST_STEP(NAME, NAME##TestStep )
280
281
282///////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000283// Basic test steps for most virtual methods in SkCanvas that draw or affect
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000284// the state of the canvas.
285
junov@chromium.orga907ac32012-02-24 21:54:07 +0000286SIMPLE_TEST_STEP_WITH_ASSERT(Translate,
287 translate(SkIntToScalar(1), SkIntToScalar(2)));
288SIMPLE_TEST_STEP_WITH_ASSERT(Scale,
289 scale(SkIntToScalar(1), SkIntToScalar(2)));
290SIMPLE_TEST_STEP_WITH_ASSERT(Rotate, rotate(SkIntToScalar(1)));
291SIMPLE_TEST_STEP_WITH_ASSERT(Skew,
292 skew(SkIntToScalar(1), SkIntToScalar(2)));
293SIMPLE_TEST_STEP_WITH_ASSERT(Concat, concat(kTestMatrix));
294SIMPLE_TEST_STEP(SetMatrix, setMatrix(kTestMatrix));
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000295SIMPLE_TEST_STEP(ClipRect, clipRect(kTestRect));
296SIMPLE_TEST_STEP(ClipPath, clipPath(kTestPath));
297SIMPLE_TEST_STEP(ClipRegion,
junov@chromium.orga907ac32012-02-24 21:54:07 +0000298 clipRegion(kTestRegion, SkRegion::kReplace_Op));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000299SIMPLE_TEST_STEP(Clear, clear(kTestColor));
300SIMPLE_TEST_STEP(DrawPaint, drawPaint(kTestPaint));
301SIMPLE_TEST_STEP(DrawPointsPoints, drawPoints(SkCanvas::kPoints_PointMode,
302 kTestPointCount, kTestPoints, kTestPaint));
303SIMPLE_TEST_STEP(DrawPointsLiness, drawPoints(SkCanvas::kLines_PointMode,
304 kTestPointCount, kTestPoints, kTestPaint));
305SIMPLE_TEST_STEP(DrawPointsPolygon, drawPoints(SkCanvas::kPolygon_PointMode,
306 kTestPointCount, kTestPoints, kTestPaint));
307SIMPLE_TEST_STEP(DrawRect, drawRect(kTestRect, kTestPaint));
308SIMPLE_TEST_STEP(DrawPath, drawPath(kTestPath, kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000309SIMPLE_TEST_STEP(DrawBitmap, drawBitmap(kTestBitmap, 0, 0));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000310SIMPLE_TEST_STEP(DrawBitmapPaint, drawBitmap(kTestBitmap, 0, 0, &kTestPaint));
311SIMPLE_TEST_STEP(DrawBitmapRect, drawBitmapRect(kTestBitmap, NULL, kTestRect,
312 NULL));
313SIMPLE_TEST_STEP(DrawBitmapRectSrcRect, drawBitmapRect(kTestBitmap,
314 &kTestIRect, kTestRect, NULL));
315SIMPLE_TEST_STEP(DrawBitmapRectPaint, drawBitmapRect(kTestBitmap, NULL,
316 kTestRect, &kTestPaint));
317SIMPLE_TEST_STEP(DrawBitmapMatrix, drawBitmapMatrix(kTestBitmap, kTestMatrix,
318 NULL));
319SIMPLE_TEST_STEP(DrawBitmapMatrixPaint, drawBitmapMatrix(kTestBitmap,
320 kTestMatrix, &kTestPaint));
321SIMPLE_TEST_STEP(DrawBitmapNine, drawBitmapNine(kTestBitmap, kTestIRect,
322 kTestRect, NULL));
323SIMPLE_TEST_STEP(DrawBitmapNinePaint, drawBitmapNine(kTestBitmap, kTestIRect,
324 kTestRect, &kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000325SIMPLE_TEST_STEP(DrawSprite, drawSprite(kTestBitmap, 0, 0, NULL));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000326SIMPLE_TEST_STEP(DrawSpritePaint, drawSprite(kTestBitmap, 0, 0, &kTestPaint));
327SIMPLE_TEST_STEP(DrawText, drawText(kTestText.c_str(), kTestText.size(),
328 0, 1, kTestPaint));
329SIMPLE_TEST_STEP(DrawPosText, drawPosText(kTestText.c_str(),
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000330 kTestText.size(), kTestPoints2, kTestPaint));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000331SIMPLE_TEST_STEP(DrawTextOnPath, drawTextOnPath(kTestText.c_str(),
332 kTestText.size(), kTestPath, NULL, kTestPaint));
333SIMPLE_TEST_STEP(DrawTextOnPathMatrix, drawTextOnPath(kTestText.c_str(),
334 kTestText.size(), kTestPath, &kTestMatrix, kTestPaint));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000335SIMPLE_TEST_STEP(DrawData, drawData(kTestText.c_str(), kTestText.size()));
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000336SIMPLE_TEST_STEP(BeginGroup, beginCommentGroup(kTestText.c_str()));
337SIMPLE_TEST_STEP(AddComment, addComment(kTestText.c_str(), kTestText.c_str()));
338SIMPLE_TEST_STEP(EndGroup, endCommentGroup());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000339
340///////////////////////////////////////////////////////////////////////////////
341// Complex test steps
342
rmistry@google.comd6176b02012-08-23 18:14:13 +0000343// Save/restore calls cannot be in isolated simple test steps because the test
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000344// cases that use SkPicture require that save and restore calls be balanced.
rmistry@google.comd6176b02012-08-23 18:14:13 +0000345static void SaveMatrixStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000346 skiatest::Reporter* reporter,
347 CanvasTestStep* testStep) {
348 int saveCount = canvas->getSaveCount();
349 canvas->save(SkCanvas::kMatrix_SaveFlag);
350 canvas->clipRegion(kTestRegion);
351 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
352 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000353 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000354 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000355 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000356 testStep->assertMessage());
357 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() == kTestRegion,
358 testStep->assertMessage());
359}
360TEST_STEP(SaveMatrix, SaveMatrixStep);
361
rmistry@google.comd6176b02012-08-23 18:14:13 +0000362static void SaveClipStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000363 skiatest::Reporter* reporter,
364 CanvasTestStep* testStep) {
365 int saveCount = canvas->getSaveCount();
366 canvas->save(SkCanvas::kClip_SaveFlag);
367 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
368 canvas->clipRegion(kTestRegion);
369 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000370 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000371 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000372 REPORTER_ASSERT_MESSAGE(reporter, !canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000373 testStep->assertMessage());
374 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion,
375 testStep->assertMessage());
376}
377TEST_STEP(SaveClip, SaveClipStep);
378
rmistry@google.comd6176b02012-08-23 18:14:13 +0000379static void SaveMatrixClipStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000380 skiatest::Reporter* reporter,
381 CanvasTestStep* testStep) {
382 int saveCount = canvas->getSaveCount();
383 canvas->save(SkCanvas::kMatrixClip_SaveFlag);
384 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
385 canvas->clipRegion(kTestRegion);
386 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000387 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000388 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000389 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000390 testStep->assertMessage());
391 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion,
392 testStep->assertMessage());
393}
394TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
395
rmistry@google.comd6176b02012-08-23 18:14:13 +0000396static void SaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000397 skiatest::Reporter* reporter,
398 CanvasTestStep* testStep) {
399 int saveCount = canvas->getSaveCount();
400 canvas->saveLayer(NULL, NULL);
401 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000402 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000403 testStep->assertMessage());
404}
405TEST_STEP(SaveLayer, SaveLayerStep);
406
rmistry@google.comd6176b02012-08-23 18:14:13 +0000407static void BoundedSaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000408 skiatest::Reporter* reporter,
409 CanvasTestStep* testStep) {
410 int saveCount = canvas->getSaveCount();
411 canvas->saveLayer(&kTestRect, NULL);
412 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000413 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000414 testStep->assertMessage());
415}
416TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
417
rmistry@google.comd6176b02012-08-23 18:14:13 +0000418static void PaintSaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000419 skiatest::Reporter* reporter,
420 CanvasTestStep* testStep) {
421 int saveCount = canvas->getSaveCount();
422 canvas->saveLayer(NULL, &kTestPaint);
423 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000424 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000425 testStep->assertMessage());
426}
427TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
428
rmistry@google.comd6176b02012-08-23 18:14:13 +0000429static void TwoClipOpsStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000430 skiatest::Reporter*,
431 CanvasTestStep*) {
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000432 // This test exercises a functionality in SkPicture that leads to the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000433 // recording of restore offset placeholders. This test will trigger an
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000434 // assertion at playback time if the placeholders are not properly
435 // filled when the recording ends.
436 canvas->clipRect(kTestRect);
437 canvas->clipRegion(kTestRegion);
438}
439TEST_STEP(TwoClipOps, TwoClipOpsStep);
440
epoger@google.com94fa43c2012-04-11 17:51:01 +0000441// exercise fix for http://code.google.com/p/skia/issues/detail?id=560
442// ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
rmistry@google.comd6176b02012-08-23 18:14:13 +0000443static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000444 skiatest::Reporter*,
445 CanvasTestStep*) {
epoger@google.com94fa43c2012-04-11 17:51:01 +0000446 SkPaint paint;
447 paint.setStrokeWidth(SkIntToScalar(1));
448 paint.setStyle(SkPaint::kStroke_Style);
449
450 SkPath path;
451 SkPoint pt1 = { 0, 0 };
452 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
453 SkPoint pt3 = { SkIntToScalar(1), 0 };
454 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
455 path.moveTo(pt1);
456 path.lineTo(pt2);
457 path.lineTo(pt3);
458 path.lineTo(pt4);
459
460 canvas->drawPath(path, paint);
461}
462TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
463
rmistry@google.comd6176b02012-08-23 18:14:13 +0000464static void DrawVerticesShaderTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000465 skiatest::Reporter*,
466 CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000467 SkPoint pts[4];
468 pts[0].set(0, 0);
469 pts[1].set(SkIntToScalar(kWidth), 0);
470 pts[2].set(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
471 pts[3].set(0, SkIntToScalar(kHeight));
472 SkPaint paint;
473 SkShader* shader = SkShader::CreateBitmapShader(kTestBitmap,
474 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
475 paint.setShader(shader)->unref();
476 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
477 NULL, NULL, NULL, 0, paint);
478}
edisonn@google.com77909122012-10-18 15:58:23 +0000479// NYI: issue 240.
480TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000481
rmistry@google.comd6176b02012-08-23 18:14:13 +0000482static void DrawPictureTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000483 skiatest::Reporter*,
484 CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000485 SkPicture* testPicture = SkNEW_ARGS(SkPicture, ());
486 SkAutoUnref aup(testPicture);
487 SkCanvas* testCanvas = testPicture->beginRecording(kWidth, kHeight);
488 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
489 testCanvas->clipRect(kTestRect);
490 testCanvas->drawRect(kTestRect, kTestPaint);
491 canvas->drawPicture(*testPicture);
492}
493TEST_STEP(DrawPicture, DrawPictureTestStep);
494
rmistry@google.comd6176b02012-08-23 18:14:13 +0000495static void SaveRestoreTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000496 skiatest::Reporter* reporter,
497 CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000498 int baseSaveCount = canvas->getSaveCount();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000499 int n = canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000500 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount == n, testStep->assertMessage());
501 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000502 testStep->assertMessage());
503 canvas->save();
504 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000505 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000506 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000507 canvas->restoreToCount(baseSaveCount + 1);
508 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000509 testStep->assertMessage());
510
511 // should this pin to 1, or be a no-op, or crash?
512 canvas->restoreToCount(0);
513 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
514 testStep->assertMessage());
515}
516TEST_STEP(SaveRestore, SaveRestoreTestStep);
517
rmistry@google.comd6176b02012-08-23 18:14:13 +0000518static void DrawLayerTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000519 skiatest::Reporter* reporter,
520 CanvasTestStep* testStep) {
521 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
522 testStep->assertMessage());
523 canvas->save();
524 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
525 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000526 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000527
reed@google.com7c202932011-12-14 18:48:05 +0000528 const SkRect* bounds = NULL; // null means include entire bounds
529 const SkPaint* paint = NULL;
530
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000531 canvas->saveLayer(bounds, paint);
532 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
533 testStep->assertMessage());
534 canvas->restore();
535 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
536 testStep->assertMessage());
reed@google.com7c202932011-12-14 18:48:05 +0000537
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000538 canvas->saveLayer(bounds, paint);
539 canvas->saveLayer(bounds, paint);
540 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
541 testStep->assertMessage());
542 canvas->restore();
543 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
544 testStep->assertMessage());
545 canvas->restore();
reed@google.com7c202932011-12-14 18:48:05 +0000546 // now layer count should be 0
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000547 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
548 testStep->assertMessage());
549}
550TEST_STEP(DrawLayer, DrawLayerTestStep);
reed@google.com3b3e8952012-08-16 20:53:31 +0000551
552static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000553 skiatest::Reporter*,
554 CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000555 // This test step challenges the TestDeferredCanvasStateConsistency
556 // test cases because the opaque paint can trigger an optimization
557 // that discards previously recorded commands. The challenge is to maintain
558 // correct clip and matrix stack state.
559 canvas->resetMatrix();
560 canvas->rotate(SkIntToScalar(30));
561 canvas->save();
562 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
563 canvas->save();
564 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
565 SkPaint paint;
566 paint.setColor(0xFFFFFFFF);
567 canvas->drawPaint(paint);
568 canvas->restore();
569 canvas->restore();
570}
571TEST_STEP(NestedSaveRestoreWithSolidPaint, \
572 NestedSaveRestoreWithSolidPaintTestStep);
573
574static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000575 skiatest::Reporter*,
576 CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000577 // This test step challenges the TestDeferredCanvasStateConsistency
578 // test case because the canvas flush on a deferred canvas will
579 // reset the recording session. The challenge is to maintain correct
580 // clip and matrix stack state on the playback canvas.
581 canvas->resetMatrix();
582 canvas->rotate(SkIntToScalar(30));
583 canvas->save();
584 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
585 canvas->save();
586 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
587 canvas->drawRect(kTestRect,kTestPaint);
588 canvas->flush();
589 canvas->restore();
590 canvas->restore();
591}
592TEST_STEP(NestedSaveRestoreWithFlush, \
593 NestedSaveRestoreWithFlushTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000594
595static void AssertCanvasStatesEqual(skiatest::Reporter* reporter,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000596 const SkCanvas* canvas1,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000597 const SkCanvas* canvas2,
598 CanvasTestStep* testStep) {
599 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
600 canvas2->getDeviceSize(), testStep->assertMessage());
601 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
602 canvas2->getSaveCount(), testStep->assertMessage());
603 REPORTER_ASSERT_MESSAGE(reporter, canvas1->isDrawingToLayer() ==
604 canvas2->isDrawingToLayer(), testStep->assertMessage());
reed@google.com3b3e8952012-08-16 20:53:31 +0000605
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000606 SkRect bounds1, bounds2;
607 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.com3b3e8952012-08-16 20:53:31 +0000608 canvas1->getClipBounds(&bounds1) == canvas2->getClipBounds(&bounds2),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000609 testStep->assertMessage());
610 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
reed@google.com3b3e8952012-08-16 20:53:31 +0000611 testStep->assertMessage());
612
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000613 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
614 canvas2->getDrawFilter(), testStep->assertMessage());
615 SkIRect deviceBounds1, deviceBounds2;
616 REPORTER_ASSERT_MESSAGE(reporter,
617 canvas1->getClipDeviceBounds(&deviceBounds1) ==
618 canvas2->getClipDeviceBounds(&deviceBounds2),
619 testStep->assertMessage());
620 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2,
621 testStep->assertMessage());
622 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getBounder() ==
623 canvas2->getBounder(), testStep->assertMessage());
624 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
625 canvas2->getTotalMatrix(), testStep->assertMessage());
626 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getClipType() ==
627 canvas2->getClipType(), testStep->assertMessage());
628 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalClip() ==
629 canvas2->getTotalClip(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000630
631 // The following test code is commented out because the test fails when
rmistry@google.comd6176b02012-08-23 18:14:13 +0000632 // the canvas is an SkPictureRecord or SkDeferredCanvas
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000633 // Issue: http://code.google.com/p/skia/issues/detail?id=498
634 // Also, creating a LayerIter on an SkProxyCanvas crashes
635 // Issue: http://code.google.com/p/skia/issues/detail?id=499
636 /*
637 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
638 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
639 while (!layerIter1.done() && !layerIter2.done()) {
640 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
641 layerIter2.matrix(), testStep->assertMessage());
642 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
643 layerIter2.clip(), testStep->assertMessage());
644 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
645 layerIter2.paint(), testStep->assertMessage());
646 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
647 layerIter2.x(), testStep->assertMessage());
648 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
649 layerIter2.y(), testStep->assertMessage());
650 layerIter1.next();
651 layerIter2.next();
652 }
653 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
654 testStep->assertMessage());
655 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
656 testStep->assertMessage());
657 */
658}
659
660// The following class groups static functions that need to access
661// the privates members of SkPictureRecord
662class SkPictureTester {
663private:
reed@google.come2589ae2012-07-10 19:38:01 +0000664 static int EQ(const SkFlatData* a, const SkFlatData* b) {
665 return *a == *b;
666 }
667
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000668 static void AssertFlattenedObjectsEqual(
669 SkPictureRecord* referenceRecord,
670 SkPictureRecord* testRecord,
671 skiatest::Reporter* reporter,
672 CanvasTestStep* testStep) {
673
674 REPORTER_ASSERT_MESSAGE(reporter,
djsollen@google.comc9ab9872012-08-29 18:52:07 +0000675 referenceRecord->fBitmapHeap->count() ==
676 testRecord->fBitmapHeap->count(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000677 REPORTER_ASSERT_MESSAGE(reporter,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000678 referenceRecord->fPaints.count() ==
679 testRecord->fPaints.count(), testStep->assertMessage());
680 for (int i = 0; i < referenceRecord->fPaints.count(); ++i) {
681 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.come2589ae2012-07-10 19:38:01 +0000682 EQ(referenceRecord->fPaints[i], testRecord->fPaints[i]),
683 testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000684 }
685 REPORTER_ASSERT_MESSAGE(reporter,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000686 !referenceRecord->fPathHeap ==
687 !testRecord->fPathHeap,
688 testStep->assertMessage());
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000689 // The following tests are commented out because they currently
690 // fail. Issue: http://code.google.com/p/skia/issues/detail?id=507
691 /*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000692 if (referenceRecord->fPathHeap) {
693 REPORTER_ASSERT_MESSAGE(reporter,
694 referenceRecord->fPathHeap->count() ==
695 testRecord->fPathHeap->count(),
696 testStep->assertMessage());
697 for (int i = 0; i < referenceRecord->fPathHeap->count(); ++i) {
698 REPORTER_ASSERT_MESSAGE(reporter,
699 (*referenceRecord->fPathHeap)[i] ==
700 (*testRecord->fPathHeap)[i], testStep->assertMessage());
701 }
702 }
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000703 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000704
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000705 }
706
707public:
708
rmistry@google.comd6176b02012-08-23 18:14:13 +0000709 static void TestPictureFlattenedObjectReuse(skiatest::Reporter* reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000710 CanvasTestStep* testStep,
711 uint32_t recordFlags) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000712 // Verify that when a test step is executed twice, no extra resources
713 // are flattened during the second execution
714 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
715 SkPicture referencePicture;
716 SkCanvas* referenceCanvas = referencePicture.beginRecording(kWidth,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000717 kHeight, recordFlags);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000718 testStep->draw(referenceCanvas, reporter);
719 SkPicture testPicture;
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000720 SkCanvas* testCanvas = testPicture.beginRecording(kWidth,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000721 kHeight, recordFlags);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000722 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000723 testStep->setAssertMessageFormat(kPictureSecondDrawAssertMessageFormat);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000724 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000725
726 SkPictureRecord* referenceRecord = static_cast<SkPictureRecord*>(
727 referenceCanvas);
728 SkPictureRecord* testRecord = static_cast<SkPictureRecord*>(
729 testCanvas);
730 testStep->setAssertMessageFormat(kPictureResourceReuseMessageFormat);
731 AssertFlattenedObjectsEqual(referenceRecord, testRecord,
junov@chromium.org76b9c4b2012-02-22 21:24:41 +0000732 reporter, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000733 }
734};
735
edisonn@google.com77909122012-10-18 15:58:23 +0000736static void TestPdfDevice(skiatest::Reporter* reporter,
737 CanvasTestStep* testStep) {
738 SkISize pageSize = SkISize::Make(kWidth, kHeight);
739 SkPDFDevice device(pageSize, pageSize, SkMatrix::I());
740 SkCanvas canvas(&device);
741 testStep->setAssertMessageFormat(kPdfAssertMessageFormat);
742 testStep->draw(&canvas, reporter);
743 SkPDFDocument doc;
744 doc.appendPage(&device);
745 SkDynamicMemoryWStream stream;
746 doc.emitPDF(&stream);
747}
748
junov@chromium.org88e29142012-08-07 16:48:22 +0000749// The following class groups static functions that need to access
750// the privates members of SkDeferredCanvas
751class SkDeferredCanvasTester {
752public:
753 static void TestDeferredCanvasStateConsistency(
754 skiatest::Reporter* reporter,
755 CanvasTestStep* testStep,
junov@chromium.orgfb103892012-09-20 19:35:43 +0000756 const SkCanvas& referenceCanvas, bool silent) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000757
reed@google.com28183b42014-02-04 15:34:10 +0000758 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
759 SkAutoTUnref<SkDeferredCanvas> deferredCanvas(SkDeferredCanvas::Create(surface.get()));
760
junov@chromium.org88e29142012-08-07 16:48:22 +0000761 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
junov@chromium.org66070a52013-05-28 17:39:08 +0000762 testStep->draw(deferredCanvas, reporter);
junov@chromium.org88e29142012-08-07 16:48:22 +0000763 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
junov@chromium.org66070a52013-05-28 17:39:08 +0000764 AssertCanvasStatesEqual(reporter, deferredCanvas, &referenceCanvas,
junov@chromium.org88e29142012-08-07 16:48:22 +0000765 testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000766
junov@chromium.orgfb103892012-09-20 19:35:43 +0000767 if (silent) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000768 deferredCanvas->silentFlush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000769 } else {
junov@chromium.org66070a52013-05-28 17:39:08 +0000770 deferredCanvas->flush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000771 }
772
skia.committer@gmail.com4c5ea442012-09-21 02:01:01 +0000773 testStep->setAssertMessageFormat(
junov@chromium.orgfb103892012-09-20 19:35:43 +0000774 silent ? kDeferredPostSilentFlushPlaybackAssertMessageFormat :
junov@chromium.org88e29142012-08-07 16:48:22 +0000775 kDeferredPostFlushPlaybackAssertMessageFormat);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000776 AssertCanvasStatesEqual(reporter,
junov@chromium.org66070a52013-05-28 17:39:08 +0000777 deferredCanvas->immediateCanvas(),
junov@chromium.org88e29142012-08-07 16:48:22 +0000778 &referenceCanvas, testStep);
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000779
junov@chromium.org88e29142012-08-07 16:48:22 +0000780 // Verified that deferred canvas state is not affected by flushing
781 // pending draw operations
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000782
junov@chromium.org88e29142012-08-07 16:48:22 +0000783 // The following test code is commented out because it currently fails.
784 // Issue: http://code.google.com/p/skia/issues/detail?id=496
785 /*
786 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
787 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
788 testStep);
789 */
790 }
791};
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000792
caryclark@google.com42639cd2012-06-06 12:03:39 +0000793// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000794static void TestProxyCanvasStateConsistency(
795 skiatest::Reporter* reporter,
796 CanvasTestStep* testStep,
797 const SkCanvas& referenceCanvas) {
798
799 SkBitmap indirectStore;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000800 createBitmap(&indirectStore, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000801 SkBitmapDevice indirectDevice(indirectStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000802 SkCanvas indirectCanvas(&indirectDevice);
803 SkProxyCanvas proxyCanvas(&indirectCanvas);
804 testStep->setAssertMessageFormat(kProxyDrawAssertMessageFormat);
805 testStep->draw(&proxyCanvas, reporter);
806 // Verify that the SkProxyCanvas reports consitent state
807 testStep->setAssertMessageFormat(kProxyStateAssertMessageFormat);
808 AssertCanvasStatesEqual(reporter, &proxyCanvas, &referenceCanvas,
809 testStep);
810 // Verify that the indirect canvas reports consitent state
811 testStep->setAssertMessageFormat(kProxyIndirectStateAssertMessageFormat);
812 AssertCanvasStatesEqual(reporter, &indirectCanvas, &referenceCanvas,
813 testStep);
814}
815
caryclark@google.com42639cd2012-06-06 12:03:39 +0000816// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000817static void TestNWayCanvasStateConsistency(
818 skiatest::Reporter* reporter,
819 CanvasTestStep* testStep,
820 const SkCanvas& referenceCanvas) {
821
822 SkBitmap indirectStore1;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000823 createBitmap(&indirectStore1, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000824 SkBitmapDevice indirectDevice1(indirectStore1);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000825 SkCanvas indirectCanvas1(&indirectDevice1);
826
827 SkBitmap indirectStore2;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000828 createBitmap(&indirectStore2, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000829 SkBitmapDevice indirectDevice2(indirectStore2);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000830 SkCanvas indirectCanvas2(&indirectDevice2);
831
djsollen@google.comf0a062b2012-05-01 16:50:25 +0000832 SkISize canvasSize = referenceCanvas.getDeviceSize();
833 SkNWayCanvas nWayCanvas(canvasSize.width(), canvasSize.height());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000834 nWayCanvas.addCanvas(&indirectCanvas1);
835 nWayCanvas.addCanvas(&indirectCanvas2);
836
837 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
838 testStep->draw(&nWayCanvas, reporter);
839 // Verify that the SkProxyCanvas reports consitent state
840 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
841 AssertCanvasStatesEqual(reporter, &nWayCanvas, &referenceCanvas,
842 testStep);
843 // Verify that the indirect canvases report consitent state
844 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
845 AssertCanvasStatesEqual(reporter, &indirectCanvas1, &referenceCanvas,
846 testStep);
847 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
848 AssertCanvasStatesEqual(reporter, &indirectCanvas2, &referenceCanvas,
849 testStep);
850}
851
852/*
853 * This sub-test verifies that the test step passes when executed
854 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
855 * that the all canvas derivatives report the same state as an SkCanvas
856 * after having executed the test step.
857 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000858static void TestOverrideStateConsistency(skiatest::Reporter* reporter,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000859 CanvasTestStep* testStep) {
860 SkBitmap referenceStore;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000861 createBitmap(&referenceStore, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000862 SkBitmapDevice referenceDevice(referenceStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000863 SkCanvas referenceCanvas(&referenceDevice);
864 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
865 testStep->draw(&referenceCanvas, reporter);
866
junov@chromium.orgfb103892012-09-20 19:35:43 +0000867 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, false);
868
869 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, true);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000870
caryclark@google.com42639cd2012-06-06 12:03:39 +0000871 // The following test code is disabled because SkProxyCanvas is
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000872 // missing a lot of virtual overrides on get* methods, which are used
873 // to verify canvas state.
874 // Issue: http://code.google.com/p/skia/issues/detail?id=500
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000875
caryclark@google.com42639cd2012-06-06 12:03:39 +0000876 if (false) { // avoid bit rot, suppress warning
877 TestProxyCanvasStateConsistency(reporter, testStep, referenceCanvas);
878 }
879
880 // The following test code is disabled because SkNWayCanvas does not
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000881 // report correct clipping and device bounds information
882 // Issue: http://code.google.com/p/skia/issues/detail?id=501
caryclark@google.com42639cd2012-06-06 12:03:39 +0000883
884 if (false) { // avoid bit rot, suppress warning
885 TestNWayCanvasStateConsistency(reporter, testStep, referenceCanvas);
886 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000887
caryclark@google.com42639cd2012-06-06 12:03:39 +0000888 if (false) { // avoid bit rot, suppress warning
889 test_clipVisitor(reporter, &referenceCanvas);
890 }
reed@google.com7c202932011-12-14 18:48:05 +0000891}
reed@google.com37f3ae02011-11-28 16:06:04 +0000892
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000893DEF_TEST(Canvas, reporter) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000894 // Init global here because bitmap pixels cannot be alocated during
895 // static initialization
896 kTestBitmap = testBitmap();
reed@google.com37f3ae02011-11-28 16:06:04 +0000897
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000898 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
899 TestOverrideStateConsistency(reporter, testStepArray()[testStep]);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000900 SkPictureTester::TestPictureFlattenedObjectReuse(reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000901 testStepArray()[testStep], 0);
edisonn@google.com77909122012-10-18 15:58:23 +0000902 if (testStepArray()[testStep]->enablePdfTesting()) {
903 TestPdfDevice(reporter, testStepArray()[testStep]);
904 }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000905 }
junov@chromium.orgcd62ecf2012-08-02 17:43:25 +0000906
907 // Explicitly call reset(), so we don't leak the pixels (since kTestBitmap is a global)
908 kTestBitmap.reset();
reed@google.com37f3ae02011-11-28 16:06:04 +0000909}