blob: 8c64d58e0d6f043d8e57070f867068926c46a7bd [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"
64#include "SkTDArray.h"
65#include "Test.h"
reed@google.com37f3ae02011-11-28 16:06:04 +000066
reed@google.com90c07ea2012-04-13 13:50:27 +000067class Canvas2CanvasClipVisitor : public SkCanvas::ClipVisitor {
68public:
69 Canvas2CanvasClipVisitor(SkCanvas* target) : fTarget(target) {}
70
71 virtual void clipRect(const SkRect& r, SkRegion::Op op, bool aa) {
72 fTarget->clipRect(r, op, aa);
73 }
74 virtual void clipPath(const SkPath& p, SkRegion::Op op, bool aa) {
75 fTarget->clipPath(p, op, aa);
76 }
77
78private:
79 SkCanvas* fTarget;
80};
81
82static void test_clipVisitor(skiatest::Reporter* reporter, SkCanvas* canvas) {
83 SkISize size = canvas->getDeviceSize();
rmistry@google.comd6176b02012-08-23 18:14:13 +000084
reed@google.com90c07ea2012-04-13 13:50:27 +000085 SkBitmap bm;
86 bm.setConfig(SkBitmap::kARGB_8888_Config, size.width(), size.height());
87 SkCanvas c(bm);
88
89 Canvas2CanvasClipVisitor visitor(&c);
90 canvas->replayClips(&visitor);
91
92 REPORTER_ASSERT(reporter, c.getTotalClip() == canvas->getTotalClip());
93}
94
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000095static const int kWidth = 2;
96static const int kHeight = 2;
97// Maximum stream length for picture serialization
rmistry@google.comd6176b02012-08-23 18:14:13 +000098static const size_t kMaxPictureBufferSize = 1024;
reed@google.com7c202932011-12-14 18:48:05 +000099
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000100// Format strings that describe the test context. The %s token is where
101// the name of the test step is inserted. The context is required for
102// disambiguating the error in the case of failures that are reported in
103// functions that are called multiple times in different contexts (test
104// cases and test steps).
105static const char* const kDefaultAssertMessageFormat = "%s";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000106static const char* const kCanvasDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000107 "Drawing test step %s with SkCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000108static const char* const kPictureDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000109 "Drawing test step %s with SkPicture";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000110static const char* const kPictureSecondDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000111 "Duplicate draw of test step %s with SkPicture";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000112static const char* const kPictureReDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000113 "Playing back test step %s from an SkPicture to another SkPicture";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000114static const char* const kDeferredDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000115 "Drawing test step %s with SkDeferredCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000116static const char* const kProxyDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000117 "Drawing test step %s with SkProxyCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000118static const char* const kNWayDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000119 "Drawing test step %s with SkNWayCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000120static const char* const kRoundTripAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000121 "test step %s, SkPicture consistency after round trip";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000122static const char* const kPictureRecoringAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000123 "test step %s, SkPicture state consistency after recording";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000124static const char* const kPicturePlaybackAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000125 "test step %s, SkPicture state consistency in playback canvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000126static const char* const kDeferredPreFlushAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000127 "test step %s, SkDeferredCanvas state consistency before flush";
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000128static const char* const kDeferredPostFlushPlaybackAssertMessageFormat =
129 "test step %s, SkDeferredCanvas playback canvas state consistency after flush";
junov@chromium.orgfb103892012-09-20 19:35:43 +0000130static const char* const kDeferredPostSilentFlushPlaybackAssertMessageFormat =
131 "test step %s, SkDeferredCanvas playback canvas state consistency after silent flush";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000132static const char* const kDeferredPostFlushAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000133 "test step %s, SkDeferredCanvas state consistency after flush";
134static const char* const kPictureResourceReuseMessageFormat =
135 "test step %s, SkPicture duplicate flattened object test";
136static const char* const kProxyStateAssertMessageFormat =
137 "test step %s, SkProxyCanvas state consistency";
138static const char* const kProxyIndirectStateAssertMessageFormat =
139 "test step %s, SkProxyCanvas indirect canvas state consistency";
140static const char* const kNWayStateAssertMessageFormat =
141 "test step %s, SkNWayCanvas state consistency";
142static const char* const kNWayIndirect1StateAssertMessageFormat =
143 "test step %s, SkNWayCanvas indirect canvas 1 state consistency";
144static const char* const kNWayIndirect2StateAssertMessageFormat =
145 "test step %s, SkNWayCanvas indirect canvas 2 state consistency";
edisonn@google.com77909122012-10-18 15:58:23 +0000146static const char* const kPdfAssertMessageFormat =
147 "PDF sanity check failed %s";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000148
149static void createBitmap(SkBitmap* bm, SkBitmap::Config config, SkColor color) {
150 bm->setConfig(config, kWidth, kHeight);
151 bm->allocPixels();
152 bm->eraseColor(color);
153}
154
155class CanvasTestStep;
156static SkTDArray<CanvasTestStep*>& testStepArray() {
157 static SkTDArray<CanvasTestStep*> theTests;
158 return theTests;
159}
160
161class CanvasTestStep {
162public:
edisonn@google.com77909122012-10-18 15:58:23 +0000163 CanvasTestStep(bool fEnablePdfTesting = true) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000164 *testStepArray().append() = this;
165 fAssertMessageFormat = kDefaultAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000166 this->fEnablePdfTesting = fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000167 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000168 virtual ~CanvasTestStep() { }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000169
170 virtual void draw(SkCanvas*, skiatest::Reporter*) = 0;
171 virtual const char* name() const = 0;
172
173 const char* assertMessage() {
174 fAssertMessage.printf(fAssertMessageFormat, name());
175 return fAssertMessage.c_str();
176 }
177
178 void setAssertMessageFormat(const char* format) {
179 fAssertMessageFormat = format;
180 }
181
edisonn@google.com77909122012-10-18 15:58:23 +0000182 bool enablePdfTesting() { return fEnablePdfTesting; }
183
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000184private:
185 SkString fAssertMessage;
186 const char* fAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000187 bool fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000188};
189
190///////////////////////////////////////////////////////////////////////////////
191// Constants used by test steps
192
rmistry@google.comd6176b02012-08-23 18:14:13 +0000193const SkRect kTestRect =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000194 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
195 SkIntToScalar(2), SkIntToScalar(1));
196static SkMatrix testMatrix() {
197 SkMatrix matrix;
198 matrix.reset();
199 matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
200 return matrix;
201}
202const SkMatrix kTestMatrix = testMatrix();
203static SkPath testPath() {
204 SkPath path;
205 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
206 SkIntToScalar(2), SkIntToScalar(1)));
207 return path;
208}
209const SkPath kTestPath = testPath();
210static SkRegion testRegion() {
211 SkRegion region;
212 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
213 region.setRect(rect);
214 return region;
215}
216const SkIRect kTestIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
217const SkRegion kTestRegion = testRegion();
218const SkColor kTestColor = 0x01020304;
219const SkPaint kTestPaint;
220const SkPoint kTestPoints[3] = {
221 {SkIntToScalar(0), SkIntToScalar(0)},
222 {SkIntToScalar(2), SkIntToScalar(1)},
223 {SkIntToScalar(0), SkIntToScalar(2)}
224};
225const size_t kTestPointCount = 3;
226static SkBitmap testBitmap() {
227 SkBitmap bitmap;
228 createBitmap(&bitmap, SkBitmap::kARGB_8888_Config, 0x05060708);
229 return bitmap;
230}
231SkBitmap kTestBitmap; // cannot be created during static init
232SkString kTestText("Hello World");
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000233SkPoint kTestPoints2[] = {
234 { SkIntToScalar(0), SkIntToScalar(1) },
235 { SkIntToScalar(1), SkIntToScalar(1) },
236 { SkIntToScalar(2), SkIntToScalar(1) },
237 { SkIntToScalar(3), SkIntToScalar(1) },
238 { SkIntToScalar(4), SkIntToScalar(1) },
239 { SkIntToScalar(5), SkIntToScalar(1) },
240 { SkIntToScalar(6), SkIntToScalar(1) },
241 { SkIntToScalar(7), SkIntToScalar(1) },
242 { SkIntToScalar(8), SkIntToScalar(1) },
243 { SkIntToScalar(9), SkIntToScalar(1) },
244 { SkIntToScalar(10), SkIntToScalar(1) },
245};
rmistry@google.comd6176b02012-08-23 18:14:13 +0000246
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000247
248///////////////////////////////////////////////////////////////////////////////
249// Macros for defining test steps
250
251#define TEST_STEP(NAME, FUNCTION) \
252class NAME##_TestStep : public CanvasTestStep{ \
253public: \
254 virtual void draw(SkCanvas* canvas, skiatest::Reporter* reporter) { \
255 FUNCTION (canvas, reporter, this); \
256 } \
257 virtual const char* name() const {return #NAME ;} \
258}; \
259static NAME##_TestStep NAME##_TestStepInstance;
260
edisonn@google.com77909122012-10-18 15:58:23 +0000261#define TEST_STEP_NO_PDF(NAME, FUNCTION) \
262class NAME##_TestStep : public CanvasTestStep{ \
263public: \
264 NAME##_TestStep() : CanvasTestStep(false) {} \
265 virtual void draw(SkCanvas* canvas, skiatest::Reporter* reporter) { \
266 FUNCTION (canvas, reporter, this); \
267 } \
268 virtual const char* name() const {return #NAME ;} \
269}; \
270static NAME##_TestStep NAME##_TestStepInstance;
271
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000272#define SIMPLE_TEST_STEP(NAME, CALL) \
273static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter*, \
274 CanvasTestStep*) { \
275 canvas-> CALL ; \
276} \
277TEST_STEP(NAME, NAME##TestStep )
278
279#define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
280static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter* reporter, \
281 CanvasTestStep* testStep) { \
282 REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \
283 testStep->assertMessage()); \
284} \
285TEST_STEP(NAME, NAME##TestStep )
286
287
288///////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000289// Basic test steps for most virtual methods in SkCanvas that draw or affect
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000290// the state of the canvas.
291
junov@chromium.orga907ac32012-02-24 21:54:07 +0000292SIMPLE_TEST_STEP_WITH_ASSERT(Translate,
293 translate(SkIntToScalar(1), SkIntToScalar(2)));
294SIMPLE_TEST_STEP_WITH_ASSERT(Scale,
295 scale(SkIntToScalar(1), SkIntToScalar(2)));
296SIMPLE_TEST_STEP_WITH_ASSERT(Rotate, rotate(SkIntToScalar(1)));
297SIMPLE_TEST_STEP_WITH_ASSERT(Skew,
298 skew(SkIntToScalar(1), SkIntToScalar(2)));
299SIMPLE_TEST_STEP_WITH_ASSERT(Concat, concat(kTestMatrix));
300SIMPLE_TEST_STEP(SetMatrix, setMatrix(kTestMatrix));
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000301SIMPLE_TEST_STEP(ClipRect, clipRect(kTestRect));
302SIMPLE_TEST_STEP(ClipPath, clipPath(kTestPath));
303SIMPLE_TEST_STEP(ClipRegion,
junov@chromium.orga907ac32012-02-24 21:54:07 +0000304 clipRegion(kTestRegion, SkRegion::kReplace_Op));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000305SIMPLE_TEST_STEP(Clear, clear(kTestColor));
306SIMPLE_TEST_STEP(DrawPaint, drawPaint(kTestPaint));
307SIMPLE_TEST_STEP(DrawPointsPoints, drawPoints(SkCanvas::kPoints_PointMode,
308 kTestPointCount, kTestPoints, kTestPaint));
309SIMPLE_TEST_STEP(DrawPointsLiness, drawPoints(SkCanvas::kLines_PointMode,
310 kTestPointCount, kTestPoints, kTestPaint));
311SIMPLE_TEST_STEP(DrawPointsPolygon, drawPoints(SkCanvas::kPolygon_PointMode,
312 kTestPointCount, kTestPoints, kTestPaint));
313SIMPLE_TEST_STEP(DrawRect, drawRect(kTestRect, kTestPaint));
314SIMPLE_TEST_STEP(DrawPath, drawPath(kTestPath, kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000315SIMPLE_TEST_STEP(DrawBitmap, drawBitmap(kTestBitmap, 0, 0));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000316SIMPLE_TEST_STEP(DrawBitmapPaint, drawBitmap(kTestBitmap, 0, 0, &kTestPaint));
317SIMPLE_TEST_STEP(DrawBitmapRect, drawBitmapRect(kTestBitmap, NULL, kTestRect,
318 NULL));
319SIMPLE_TEST_STEP(DrawBitmapRectSrcRect, drawBitmapRect(kTestBitmap,
320 &kTestIRect, kTestRect, NULL));
321SIMPLE_TEST_STEP(DrawBitmapRectPaint, drawBitmapRect(kTestBitmap, NULL,
322 kTestRect, &kTestPaint));
323SIMPLE_TEST_STEP(DrawBitmapMatrix, drawBitmapMatrix(kTestBitmap, kTestMatrix,
324 NULL));
325SIMPLE_TEST_STEP(DrawBitmapMatrixPaint, drawBitmapMatrix(kTestBitmap,
326 kTestMatrix, &kTestPaint));
327SIMPLE_TEST_STEP(DrawBitmapNine, drawBitmapNine(kTestBitmap, kTestIRect,
328 kTestRect, NULL));
329SIMPLE_TEST_STEP(DrawBitmapNinePaint, drawBitmapNine(kTestBitmap, kTestIRect,
330 kTestRect, &kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000331SIMPLE_TEST_STEP(DrawSprite, drawSprite(kTestBitmap, 0, 0, NULL));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000332SIMPLE_TEST_STEP(DrawSpritePaint, drawSprite(kTestBitmap, 0, 0, &kTestPaint));
333SIMPLE_TEST_STEP(DrawText, drawText(kTestText.c_str(), kTestText.size(),
334 0, 1, kTestPaint));
335SIMPLE_TEST_STEP(DrawPosText, drawPosText(kTestText.c_str(),
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000336 kTestText.size(), kTestPoints2, kTestPaint));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000337SIMPLE_TEST_STEP(DrawTextOnPath, drawTextOnPath(kTestText.c_str(),
338 kTestText.size(), kTestPath, NULL, kTestPaint));
339SIMPLE_TEST_STEP(DrawTextOnPathMatrix, drawTextOnPath(kTestText.c_str(),
340 kTestText.size(), kTestPath, &kTestMatrix, kTestPaint));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000341SIMPLE_TEST_STEP(DrawData, drawData(kTestText.c_str(), kTestText.size()));
342
343///////////////////////////////////////////////////////////////////////////////
344// Complex test steps
345
rmistry@google.comd6176b02012-08-23 18:14:13 +0000346// Save/restore calls cannot be in isolated simple test steps because the test
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000347// cases that use SkPicture require that save and restore calls be balanced.
rmistry@google.comd6176b02012-08-23 18:14:13 +0000348static void SaveMatrixStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000349 skiatest::Reporter* reporter,
350 CanvasTestStep* testStep) {
351 int saveCount = canvas->getSaveCount();
352 canvas->save(SkCanvas::kMatrix_SaveFlag);
353 canvas->clipRegion(kTestRegion);
354 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
355 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000356 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000357 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000358 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000359 testStep->assertMessage());
360 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() == kTestRegion,
361 testStep->assertMessage());
362}
363TEST_STEP(SaveMatrix, SaveMatrixStep);
364
rmistry@google.comd6176b02012-08-23 18:14:13 +0000365static void SaveClipStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000366 skiatest::Reporter* reporter,
367 CanvasTestStep* testStep) {
368 int saveCount = canvas->getSaveCount();
369 canvas->save(SkCanvas::kClip_SaveFlag);
370 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
371 canvas->clipRegion(kTestRegion);
372 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000373 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000374 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000375 REPORTER_ASSERT_MESSAGE(reporter, !canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000376 testStep->assertMessage());
377 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion,
378 testStep->assertMessage());
379}
380TEST_STEP(SaveClip, SaveClipStep);
381
rmistry@google.comd6176b02012-08-23 18:14:13 +0000382static void SaveMatrixClipStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000383 skiatest::Reporter* reporter,
384 CanvasTestStep* testStep) {
385 int saveCount = canvas->getSaveCount();
386 canvas->save(SkCanvas::kMatrixClip_SaveFlag);
387 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
388 canvas->clipRegion(kTestRegion);
389 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000390 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000391 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000392 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000393 testStep->assertMessage());
394 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion,
395 testStep->assertMessage());
396}
397TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
398
rmistry@google.comd6176b02012-08-23 18:14:13 +0000399static void SaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000400 skiatest::Reporter* reporter,
401 CanvasTestStep* testStep) {
402 int saveCount = canvas->getSaveCount();
403 canvas->saveLayer(NULL, NULL);
404 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000405 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000406 testStep->assertMessage());
407}
408TEST_STEP(SaveLayer, SaveLayerStep);
409
rmistry@google.comd6176b02012-08-23 18:14:13 +0000410static void BoundedSaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000411 skiatest::Reporter* reporter,
412 CanvasTestStep* testStep) {
413 int saveCount = canvas->getSaveCount();
414 canvas->saveLayer(&kTestRect, NULL);
415 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000416 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000417 testStep->assertMessage());
418}
419TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
420
rmistry@google.comd6176b02012-08-23 18:14:13 +0000421static void PaintSaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000422 skiatest::Reporter* reporter,
423 CanvasTestStep* testStep) {
424 int saveCount = canvas->getSaveCount();
425 canvas->saveLayer(NULL, &kTestPaint);
426 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000427 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000428 testStep->assertMessage());
429}
430TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
431
rmistry@google.comd6176b02012-08-23 18:14:13 +0000432static void TwoClipOpsStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000433 skiatest::Reporter* reporter,
434 CanvasTestStep* testStep) {
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000435 // This test exercises a functionality in SkPicture that leads to the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000436 // recording of restore offset placeholders. This test will trigger an
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000437 // assertion at playback time if the placeholders are not properly
438 // filled when the recording ends.
439 canvas->clipRect(kTestRect);
440 canvas->clipRegion(kTestRegion);
441}
442TEST_STEP(TwoClipOps, TwoClipOpsStep);
443
epoger@google.com94fa43c2012-04-11 17:51:01 +0000444// exercise fix for http://code.google.com/p/skia/issues/detail?id=560
445// ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
rmistry@google.comd6176b02012-08-23 18:14:13 +0000446static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas,
epoger@google.com94fa43c2012-04-11 17:51:01 +0000447 skiatest::Reporter* reporter,
448 CanvasTestStep* testStep) {
449 SkPaint paint;
450 paint.setStrokeWidth(SkIntToScalar(1));
451 paint.setStyle(SkPaint::kStroke_Style);
452
453 SkPath path;
454 SkPoint pt1 = { 0, 0 };
455 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
456 SkPoint pt3 = { SkIntToScalar(1), 0 };
457 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
458 path.moveTo(pt1);
459 path.lineTo(pt2);
460 path.lineTo(pt3);
461 path.lineTo(pt4);
462
463 canvas->drawPath(path, paint);
464}
465TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
466
rmistry@google.comd6176b02012-08-23 18:14:13 +0000467static void DrawVerticesShaderTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000468 skiatest::Reporter* reporter,
469 CanvasTestStep* testStep) {
470 SkPoint pts[4];
471 pts[0].set(0, 0);
472 pts[1].set(SkIntToScalar(kWidth), 0);
473 pts[2].set(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
474 pts[3].set(0, SkIntToScalar(kHeight));
475 SkPaint paint;
476 SkShader* shader = SkShader::CreateBitmapShader(kTestBitmap,
477 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
478 paint.setShader(shader)->unref();
479 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
480 NULL, NULL, NULL, 0, paint);
481}
edisonn@google.com77909122012-10-18 15:58:23 +0000482// NYI: issue 240.
483TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000484
rmistry@google.comd6176b02012-08-23 18:14:13 +0000485static void DrawPictureTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000486 skiatest::Reporter* reporter,
487 CanvasTestStep* testStep) {
488 SkPicture* testPicture = SkNEW_ARGS(SkPicture, ());
489 SkAutoUnref aup(testPicture);
490 SkCanvas* testCanvas = testPicture->beginRecording(kWidth, kHeight);
491 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
492 testCanvas->clipRect(kTestRect);
493 testCanvas->drawRect(kTestRect, kTestPaint);
494 canvas->drawPicture(*testPicture);
495}
496TEST_STEP(DrawPicture, DrawPictureTestStep);
497
rmistry@google.comd6176b02012-08-23 18:14:13 +0000498static void SaveRestoreTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000499 skiatest::Reporter* reporter,
500 CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000501 int baseSaveCount = canvas->getSaveCount();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000502 int n = canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000503 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount == n, testStep->assertMessage());
504 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000505 testStep->assertMessage());
506 canvas->save();
507 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000508 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000509 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000510 canvas->restoreToCount(baseSaveCount + 1);
511 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000512 testStep->assertMessage());
513
514 // should this pin to 1, or be a no-op, or crash?
515 canvas->restoreToCount(0);
516 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
517 testStep->assertMessage());
518}
519TEST_STEP(SaveRestore, SaveRestoreTestStep);
520
rmistry@google.comd6176b02012-08-23 18:14:13 +0000521static void DrawLayerTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000522 skiatest::Reporter* reporter,
523 CanvasTestStep* testStep) {
524 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
525 testStep->assertMessage());
526 canvas->save();
527 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
528 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000529 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000530
reed@google.com7c202932011-12-14 18:48:05 +0000531 const SkRect* bounds = NULL; // null means include entire bounds
532 const SkPaint* paint = NULL;
533
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000534 canvas->saveLayer(bounds, paint);
535 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
536 testStep->assertMessage());
537 canvas->restore();
538 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
539 testStep->assertMessage());
reed@google.com7c202932011-12-14 18:48:05 +0000540
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000541 canvas->saveLayer(bounds, paint);
542 canvas->saveLayer(bounds, paint);
543 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
544 testStep->assertMessage());
545 canvas->restore();
546 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
547 testStep->assertMessage());
548 canvas->restore();
reed@google.com7c202932011-12-14 18:48:05 +0000549 // now layer count should be 0
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000550 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
551 testStep->assertMessage());
552}
553TEST_STEP(DrawLayer, DrawLayerTestStep);
reed@google.com3b3e8952012-08-16 20:53:31 +0000554
555static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas,
556 skiatest::Reporter* reporter,
557 CanvasTestStep* testStep) {
558 // This test step challenges the TestDeferredCanvasStateConsistency
559 // test cases because the opaque paint can trigger an optimization
560 // that discards previously recorded commands. The challenge is to maintain
561 // correct clip and matrix stack state.
562 canvas->resetMatrix();
563 canvas->rotate(SkIntToScalar(30));
564 canvas->save();
565 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
566 canvas->save();
567 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
568 SkPaint paint;
569 paint.setColor(0xFFFFFFFF);
570 canvas->drawPaint(paint);
571 canvas->restore();
572 canvas->restore();
573}
574TEST_STEP(NestedSaveRestoreWithSolidPaint, \
575 NestedSaveRestoreWithSolidPaintTestStep);
576
577static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas,
578 skiatest::Reporter* reporter,
579 CanvasTestStep* testStep) {
580 // This test step challenges the TestDeferredCanvasStateConsistency
581 // test case because the canvas flush on a deferred canvas will
582 // reset the recording session. The challenge is to maintain correct
583 // clip and matrix stack state on the playback canvas.
584 canvas->resetMatrix();
585 canvas->rotate(SkIntToScalar(30));
586 canvas->save();
587 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
588 canvas->save();
589 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
590 canvas->drawRect(kTestRect,kTestPaint);
591 canvas->flush();
592 canvas->restore();
593 canvas->restore();
594}
595TEST_STEP(NestedSaveRestoreWithFlush, \
596 NestedSaveRestoreWithFlushTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000597
598static void AssertCanvasStatesEqual(skiatest::Reporter* reporter,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000599 const SkCanvas* canvas1,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000600 const SkCanvas* canvas2,
601 CanvasTestStep* testStep) {
602 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
603 canvas2->getDeviceSize(), testStep->assertMessage());
604 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
605 canvas2->getSaveCount(), testStep->assertMessage());
606 REPORTER_ASSERT_MESSAGE(reporter, canvas1->isDrawingToLayer() ==
607 canvas2->isDrawingToLayer(), testStep->assertMessage());
reed@google.com3b3e8952012-08-16 20:53:31 +0000608
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000609 SkRect bounds1, bounds2;
610 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.com3b3e8952012-08-16 20:53:31 +0000611 canvas1->getClipBounds(&bounds1) == canvas2->getClipBounds(&bounds2),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000612 testStep->assertMessage());
613 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
reed@google.com3b3e8952012-08-16 20:53:31 +0000614 testStep->assertMessage());
615
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000616 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
617 canvas2->getDrawFilter(), testStep->assertMessage());
618 SkIRect deviceBounds1, deviceBounds2;
619 REPORTER_ASSERT_MESSAGE(reporter,
620 canvas1->getClipDeviceBounds(&deviceBounds1) ==
621 canvas2->getClipDeviceBounds(&deviceBounds2),
622 testStep->assertMessage());
623 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2,
624 testStep->assertMessage());
625 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getBounder() ==
626 canvas2->getBounder(), testStep->assertMessage());
627 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
628 canvas2->getTotalMatrix(), testStep->assertMessage());
629 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getClipType() ==
630 canvas2->getClipType(), testStep->assertMessage());
631 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalClip() ==
632 canvas2->getTotalClip(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000633
634 // The following test code is commented out because the test fails when
rmistry@google.comd6176b02012-08-23 18:14:13 +0000635 // the canvas is an SkPictureRecord or SkDeferredCanvas
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000636 // Issue: http://code.google.com/p/skia/issues/detail?id=498
637 // Also, creating a LayerIter on an SkProxyCanvas crashes
638 // Issue: http://code.google.com/p/skia/issues/detail?id=499
639 /*
640 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
641 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
642 while (!layerIter1.done() && !layerIter2.done()) {
643 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
644 layerIter2.matrix(), testStep->assertMessage());
645 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
646 layerIter2.clip(), testStep->assertMessage());
647 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
648 layerIter2.paint(), testStep->assertMessage());
649 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
650 layerIter2.x(), testStep->assertMessage());
651 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
652 layerIter2.y(), testStep->assertMessage());
653 layerIter1.next();
654 layerIter2.next();
655 }
656 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
657 testStep->assertMessage());
658 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
659 testStep->assertMessage());
660 */
661}
662
663// The following class groups static functions that need to access
664// the privates members of SkPictureRecord
665class SkPictureTester {
666private:
reed@google.come2589ae2012-07-10 19:38:01 +0000667 static int EQ(const SkFlatData* a, const SkFlatData* b) {
668 return *a == *b;
669 }
670
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000671 static void AssertFlattenedObjectsEqual(
672 SkPictureRecord* referenceRecord,
673 SkPictureRecord* testRecord,
674 skiatest::Reporter* reporter,
675 CanvasTestStep* testStep) {
676
677 REPORTER_ASSERT_MESSAGE(reporter,
djsollen@google.comc9ab9872012-08-29 18:52:07 +0000678 referenceRecord->fBitmapHeap->count() ==
679 testRecord->fBitmapHeap->count(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000680 REPORTER_ASSERT_MESSAGE(reporter,
681 referenceRecord->fMatrices.count() ==
682 testRecord->fMatrices.count(), testStep->assertMessage());
683 for (int i = 0; i < referenceRecord->fMatrices.count(); ++i) {
684 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.come2589ae2012-07-10 19:38:01 +0000685 EQ(referenceRecord->fMatrices[i], testRecord->fMatrices[i]),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000686 testStep->assertMessage());
687 }
688 REPORTER_ASSERT_MESSAGE(reporter,
689 referenceRecord->fPaints.count() ==
690 testRecord->fPaints.count(), testStep->assertMessage());
691 for (int i = 0; i < referenceRecord->fPaints.count(); ++i) {
692 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.come2589ae2012-07-10 19:38:01 +0000693 EQ(referenceRecord->fPaints[i], testRecord->fPaints[i]),
694 testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000695 }
696 REPORTER_ASSERT_MESSAGE(reporter,
697 referenceRecord->fRegions.count() ==
698 testRecord->fRegions.count(), testStep->assertMessage());
699 for (int i = 0; i < referenceRecord->fRegions.count(); ++i) {
700 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.come2589ae2012-07-10 19:38:01 +0000701 EQ(referenceRecord->fRegions[i], testRecord->fRegions[i]),
702 testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000703 }
704 REPORTER_ASSERT_MESSAGE(reporter,
705 !referenceRecord->fPathHeap ==
706 !testRecord->fPathHeap,
707 testStep->assertMessage());
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000708 // The following tests are commented out because they currently
709 // fail. Issue: http://code.google.com/p/skia/issues/detail?id=507
710 /*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000711 if (referenceRecord->fPathHeap) {
712 REPORTER_ASSERT_MESSAGE(reporter,
713 referenceRecord->fPathHeap->count() ==
714 testRecord->fPathHeap->count(),
715 testStep->assertMessage());
716 for (int i = 0; i < referenceRecord->fPathHeap->count(); ++i) {
717 REPORTER_ASSERT_MESSAGE(reporter,
718 (*referenceRecord->fPathHeap)[i] ==
719 (*testRecord->fPathHeap)[i], testStep->assertMessage());
720 }
721 }
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000722 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000723
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000724 }
725
726public:
727
rmistry@google.comd6176b02012-08-23 18:14:13 +0000728 static void TestPictureFlattenedObjectReuse(skiatest::Reporter* reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000729 CanvasTestStep* testStep,
730 uint32_t recordFlags) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000731 // Verify that when a test step is executed twice, no extra resources
732 // are flattened during the second execution
733 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
734 SkPicture referencePicture;
735 SkCanvas* referenceCanvas = referencePicture.beginRecording(kWidth,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000736 kHeight, recordFlags);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000737 testStep->draw(referenceCanvas, reporter);
738 SkPicture testPicture;
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000739 SkCanvas* testCanvas = testPicture.beginRecording(kWidth,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000740 kHeight, recordFlags);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000741 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000742 testStep->setAssertMessageFormat(kPictureSecondDrawAssertMessageFormat);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000743 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000744
745 SkPictureRecord* referenceRecord = static_cast<SkPictureRecord*>(
746 referenceCanvas);
747 SkPictureRecord* testRecord = static_cast<SkPictureRecord*>(
748 testCanvas);
749 testStep->setAssertMessageFormat(kPictureResourceReuseMessageFormat);
750 AssertFlattenedObjectsEqual(referenceRecord, testRecord,
junov@chromium.org76b9c4b2012-02-22 21:24:41 +0000751 reporter, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000752 }
753};
754
edisonn@google.com77909122012-10-18 15:58:23 +0000755static void TestPdfDevice(skiatest::Reporter* reporter,
756 CanvasTestStep* testStep) {
757 SkISize pageSize = SkISize::Make(kWidth, kHeight);
758 SkPDFDevice device(pageSize, pageSize, SkMatrix::I());
759 SkCanvas canvas(&device);
760 testStep->setAssertMessageFormat(kPdfAssertMessageFormat);
761 testStep->draw(&canvas, reporter);
762 SkPDFDocument doc;
763 doc.appendPage(&device);
764 SkDynamicMemoryWStream stream;
765 doc.emitPDF(&stream);
766}
767
junov@chromium.org88e29142012-08-07 16:48:22 +0000768// The following class groups static functions that need to access
769// the privates members of SkDeferredCanvas
770class SkDeferredCanvasTester {
771public:
772 static void TestDeferredCanvasStateConsistency(
773 skiatest::Reporter* reporter,
774 CanvasTestStep* testStep,
junov@chromium.orgfb103892012-09-20 19:35:43 +0000775 const SkCanvas& referenceCanvas, bool silent) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000776
junov@chromium.org88e29142012-08-07 16:48:22 +0000777 SkBitmap deferredStore;
778 createBitmap(&deferredStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
779 SkDevice deferredDevice(deferredStore);
780 SkDeferredCanvas deferredCanvas(&deferredDevice);
781 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
782 testStep->draw(&deferredCanvas, reporter);
783 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
784 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
785 testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000786
junov@chromium.orgfb103892012-09-20 19:35:43 +0000787 if (silent) {
788 deferredCanvas.silentFlush();
789 } else {
790 deferredCanvas.flush();
791 }
792
skia.committer@gmail.com4c5ea442012-09-21 02:01:01 +0000793 testStep->setAssertMessageFormat(
junov@chromium.orgfb103892012-09-20 19:35:43 +0000794 silent ? kDeferredPostSilentFlushPlaybackAssertMessageFormat :
junov@chromium.org88e29142012-08-07 16:48:22 +0000795 kDeferredPostFlushPlaybackAssertMessageFormat);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000796 AssertCanvasStatesEqual(reporter,
junov@chromium.org88e29142012-08-07 16:48:22 +0000797 deferredCanvas.immediateCanvas(),
798 &referenceCanvas, testStep);
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000799
junov@chromium.org88e29142012-08-07 16:48:22 +0000800 // Verified that deferred canvas state is not affected by flushing
801 // pending draw operations
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000802
junov@chromium.org88e29142012-08-07 16:48:22 +0000803 // The following test code is commented out because it currently fails.
804 // Issue: http://code.google.com/p/skia/issues/detail?id=496
805 /*
806 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
807 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
808 testStep);
809 */
810 }
811};
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000812
caryclark@google.com42639cd2012-06-06 12:03:39 +0000813// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000814static void TestProxyCanvasStateConsistency(
815 skiatest::Reporter* reporter,
816 CanvasTestStep* testStep,
817 const SkCanvas& referenceCanvas) {
818
819 SkBitmap indirectStore;
820 createBitmap(&indirectStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
821 SkDevice indirectDevice(indirectStore);
822 SkCanvas indirectCanvas(&indirectDevice);
823 SkProxyCanvas proxyCanvas(&indirectCanvas);
824 testStep->setAssertMessageFormat(kProxyDrawAssertMessageFormat);
825 testStep->draw(&proxyCanvas, reporter);
826 // Verify that the SkProxyCanvas reports consitent state
827 testStep->setAssertMessageFormat(kProxyStateAssertMessageFormat);
828 AssertCanvasStatesEqual(reporter, &proxyCanvas, &referenceCanvas,
829 testStep);
830 // Verify that the indirect canvas reports consitent state
831 testStep->setAssertMessageFormat(kProxyIndirectStateAssertMessageFormat);
832 AssertCanvasStatesEqual(reporter, &indirectCanvas, &referenceCanvas,
833 testStep);
834}
835
caryclark@google.com42639cd2012-06-06 12:03:39 +0000836// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000837static void TestNWayCanvasStateConsistency(
838 skiatest::Reporter* reporter,
839 CanvasTestStep* testStep,
840 const SkCanvas& referenceCanvas) {
841
842 SkBitmap indirectStore1;
843 createBitmap(&indirectStore1, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
844 SkDevice indirectDevice1(indirectStore1);
845 SkCanvas indirectCanvas1(&indirectDevice1);
846
847 SkBitmap indirectStore2;
848 createBitmap(&indirectStore2, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
849 SkDevice indirectDevice2(indirectStore2);
850 SkCanvas indirectCanvas2(&indirectDevice2);
851
djsollen@google.comf0a062b2012-05-01 16:50:25 +0000852 SkISize canvasSize = referenceCanvas.getDeviceSize();
853 SkNWayCanvas nWayCanvas(canvasSize.width(), canvasSize.height());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000854 nWayCanvas.addCanvas(&indirectCanvas1);
855 nWayCanvas.addCanvas(&indirectCanvas2);
856
857 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
858 testStep->draw(&nWayCanvas, reporter);
859 // Verify that the SkProxyCanvas reports consitent state
860 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
861 AssertCanvasStatesEqual(reporter, &nWayCanvas, &referenceCanvas,
862 testStep);
863 // Verify that the indirect canvases report consitent state
864 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
865 AssertCanvasStatesEqual(reporter, &indirectCanvas1, &referenceCanvas,
866 testStep);
867 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
868 AssertCanvasStatesEqual(reporter, &indirectCanvas2, &referenceCanvas,
869 testStep);
870}
871
872/*
873 * This sub-test verifies that the test step passes when executed
874 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
875 * that the all canvas derivatives report the same state as an SkCanvas
876 * after having executed the test step.
877 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000878static void TestOverrideStateConsistency(skiatest::Reporter* reporter,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000879 CanvasTestStep* testStep) {
880 SkBitmap referenceStore;
881 createBitmap(&referenceStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
882 SkDevice referenceDevice(referenceStore);
883 SkCanvas referenceCanvas(&referenceDevice);
884 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
885 testStep->draw(&referenceCanvas, reporter);
886
junov@chromium.orgfb103892012-09-20 19:35:43 +0000887 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, false);
888
889 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, true);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000890
caryclark@google.com42639cd2012-06-06 12:03:39 +0000891 // The following test code is disabled because SkProxyCanvas is
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000892 // missing a lot of virtual overrides on get* methods, which are used
893 // to verify canvas state.
894 // Issue: http://code.google.com/p/skia/issues/detail?id=500
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000895
caryclark@google.com42639cd2012-06-06 12:03:39 +0000896 if (false) { // avoid bit rot, suppress warning
897 TestProxyCanvasStateConsistency(reporter, testStep, referenceCanvas);
898 }
899
900 // The following test code is disabled because SkNWayCanvas does not
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000901 // report correct clipping and device bounds information
902 // Issue: http://code.google.com/p/skia/issues/detail?id=501
caryclark@google.com42639cd2012-06-06 12:03:39 +0000903
904 if (false) { // avoid bit rot, suppress warning
905 TestNWayCanvasStateConsistency(reporter, testStep, referenceCanvas);
906 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000907
caryclark@google.com42639cd2012-06-06 12:03:39 +0000908 if (false) { // avoid bit rot, suppress warning
909 test_clipVisitor(reporter, &referenceCanvas);
910 }
reed@google.com7c202932011-12-14 18:48:05 +0000911}
reed@google.com37f3ae02011-11-28 16:06:04 +0000912
913static void TestCanvas(skiatest::Reporter* reporter) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000914 // Init global here because bitmap pixels cannot be alocated during
915 // static initialization
916 kTestBitmap = testBitmap();
reed@google.com37f3ae02011-11-28 16:06:04 +0000917
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000918 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
919 TestOverrideStateConsistency(reporter, testStepArray()[testStep]);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000920 SkPictureTester::TestPictureFlattenedObjectReuse(reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000921 testStepArray()[testStep], 0);
edisonn@google.com77909122012-10-18 15:58:23 +0000922 if (testStepArray()[testStep]->enablePdfTesting()) {
923 TestPdfDevice(reporter, testStepArray()[testStep]);
924 }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000925 }
junov@chromium.orgcd62ecf2012-08-02 17:43:25 +0000926
927 // Explicitly call reset(), so we don't leak the pixels (since kTestBitmap is a global)
928 kTestBitmap.reset();
reed@google.com37f3ae02011-11-28 16:06:04 +0000929}
930
931#include "TestClassDef.h"
932DEFINE_TESTCLASS("Canvas", TestCanvasClass, TestCanvas)