blob: 26896fc4251ee1ccdbcca9b0e9fc3a5d8ef56708 [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()));
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000342SIMPLE_TEST_STEP(BeginGroup, beginCommentGroup(kTestText.c_str()));
343SIMPLE_TEST_STEP(AddComment, addComment(kTestText.c_str(), kTestText.c_str()));
344SIMPLE_TEST_STEP(EndGroup, endCommentGroup());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000345
346///////////////////////////////////////////////////////////////////////////////
347// Complex test steps
348
rmistry@google.comd6176b02012-08-23 18:14:13 +0000349// Save/restore calls cannot be in isolated simple test steps because the test
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000350// cases that use SkPicture require that save and restore calls be balanced.
rmistry@google.comd6176b02012-08-23 18:14:13 +0000351static void SaveMatrixStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000352 skiatest::Reporter* reporter,
353 CanvasTestStep* testStep) {
354 int saveCount = canvas->getSaveCount();
355 canvas->save(SkCanvas::kMatrix_SaveFlag);
356 canvas->clipRegion(kTestRegion);
357 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
358 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000359 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000360 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000361 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000362 testStep->assertMessage());
363 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() == kTestRegion,
364 testStep->assertMessage());
365}
366TEST_STEP(SaveMatrix, SaveMatrixStep);
367
rmistry@google.comd6176b02012-08-23 18:14:13 +0000368static void SaveClipStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000369 skiatest::Reporter* reporter,
370 CanvasTestStep* testStep) {
371 int saveCount = canvas->getSaveCount();
372 canvas->save(SkCanvas::kClip_SaveFlag);
373 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
374 canvas->clipRegion(kTestRegion);
375 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000376 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000377 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000378 REPORTER_ASSERT_MESSAGE(reporter, !canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000379 testStep->assertMessage());
380 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion,
381 testStep->assertMessage());
382}
383TEST_STEP(SaveClip, SaveClipStep);
384
rmistry@google.comd6176b02012-08-23 18:14:13 +0000385static void SaveMatrixClipStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000386 skiatest::Reporter* reporter,
387 CanvasTestStep* testStep) {
388 int saveCount = canvas->getSaveCount();
389 canvas->save(SkCanvas::kMatrixClip_SaveFlag);
390 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
391 canvas->clipRegion(kTestRegion);
392 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000393 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000394 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000395 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000396 testStep->assertMessage());
397 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion,
398 testStep->assertMessage());
399}
400TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
401
rmistry@google.comd6176b02012-08-23 18:14:13 +0000402static void SaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000403 skiatest::Reporter* reporter,
404 CanvasTestStep* testStep) {
405 int saveCount = canvas->getSaveCount();
406 canvas->saveLayer(NULL, NULL);
407 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000408 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000409 testStep->assertMessage());
410}
411TEST_STEP(SaveLayer, SaveLayerStep);
412
rmistry@google.comd6176b02012-08-23 18:14:13 +0000413static void BoundedSaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000414 skiatest::Reporter* reporter,
415 CanvasTestStep* testStep) {
416 int saveCount = canvas->getSaveCount();
417 canvas->saveLayer(&kTestRect, NULL);
418 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000419 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000420 testStep->assertMessage());
421}
422TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
423
rmistry@google.comd6176b02012-08-23 18:14:13 +0000424static void PaintSaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000425 skiatest::Reporter* reporter,
426 CanvasTestStep* testStep) {
427 int saveCount = canvas->getSaveCount();
428 canvas->saveLayer(NULL, &kTestPaint);
429 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000430 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000431 testStep->assertMessage());
432}
433TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
434
rmistry@google.comd6176b02012-08-23 18:14:13 +0000435static void TwoClipOpsStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000436 skiatest::Reporter*,
437 CanvasTestStep*) {
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000438 // This test exercises a functionality in SkPicture that leads to the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000439 // recording of restore offset placeholders. This test will trigger an
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000440 // assertion at playback time if the placeholders are not properly
441 // filled when the recording ends.
442 canvas->clipRect(kTestRect);
443 canvas->clipRegion(kTestRegion);
444}
445TEST_STEP(TwoClipOps, TwoClipOpsStep);
446
epoger@google.com94fa43c2012-04-11 17:51:01 +0000447// exercise fix for http://code.google.com/p/skia/issues/detail?id=560
448// ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
rmistry@google.comd6176b02012-08-23 18:14:13 +0000449static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000450 skiatest::Reporter*,
451 CanvasTestStep*) {
epoger@google.com94fa43c2012-04-11 17:51:01 +0000452 SkPaint paint;
453 paint.setStrokeWidth(SkIntToScalar(1));
454 paint.setStyle(SkPaint::kStroke_Style);
455
456 SkPath path;
457 SkPoint pt1 = { 0, 0 };
458 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
459 SkPoint pt3 = { SkIntToScalar(1), 0 };
460 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
461 path.moveTo(pt1);
462 path.lineTo(pt2);
463 path.lineTo(pt3);
464 path.lineTo(pt4);
465
466 canvas->drawPath(path, paint);
467}
468TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
469
rmistry@google.comd6176b02012-08-23 18:14:13 +0000470static void DrawVerticesShaderTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000471 skiatest::Reporter*,
472 CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000473 SkPoint pts[4];
474 pts[0].set(0, 0);
475 pts[1].set(SkIntToScalar(kWidth), 0);
476 pts[2].set(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
477 pts[3].set(0, SkIntToScalar(kHeight));
478 SkPaint paint;
479 SkShader* shader = SkShader::CreateBitmapShader(kTestBitmap,
480 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
481 paint.setShader(shader)->unref();
482 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
483 NULL, NULL, NULL, 0, paint);
484}
edisonn@google.com77909122012-10-18 15:58:23 +0000485// NYI: issue 240.
486TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000487
rmistry@google.comd6176b02012-08-23 18:14:13 +0000488static void DrawPictureTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000489 skiatest::Reporter*,
490 CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000491 SkPicture* testPicture = SkNEW_ARGS(SkPicture, ());
492 SkAutoUnref aup(testPicture);
493 SkCanvas* testCanvas = testPicture->beginRecording(kWidth, kHeight);
494 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
495 testCanvas->clipRect(kTestRect);
496 testCanvas->drawRect(kTestRect, kTestPaint);
497 canvas->drawPicture(*testPicture);
498}
499TEST_STEP(DrawPicture, DrawPictureTestStep);
500
rmistry@google.comd6176b02012-08-23 18:14:13 +0000501static void SaveRestoreTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000502 skiatest::Reporter* reporter,
503 CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000504 int baseSaveCount = canvas->getSaveCount();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000505 int n = canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000506 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount == n, testStep->assertMessage());
507 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000508 testStep->assertMessage());
509 canvas->save();
510 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000511 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000512 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000513 canvas->restoreToCount(baseSaveCount + 1);
514 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000515 testStep->assertMessage());
516
517 // should this pin to 1, or be a no-op, or crash?
518 canvas->restoreToCount(0);
519 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
520 testStep->assertMessage());
521}
522TEST_STEP(SaveRestore, SaveRestoreTestStep);
523
rmistry@google.comd6176b02012-08-23 18:14:13 +0000524static void DrawLayerTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000525 skiatest::Reporter* reporter,
526 CanvasTestStep* testStep) {
527 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
528 testStep->assertMessage());
529 canvas->save();
530 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
531 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000532 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000533
reed@google.com7c202932011-12-14 18:48:05 +0000534 const SkRect* bounds = NULL; // null means include entire bounds
535 const SkPaint* paint = NULL;
536
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000537 canvas->saveLayer(bounds, paint);
538 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
539 testStep->assertMessage());
540 canvas->restore();
541 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
542 testStep->assertMessage());
reed@google.com7c202932011-12-14 18:48:05 +0000543
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000544 canvas->saveLayer(bounds, paint);
545 canvas->saveLayer(bounds, paint);
546 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
547 testStep->assertMessage());
548 canvas->restore();
549 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
550 testStep->assertMessage());
551 canvas->restore();
reed@google.com7c202932011-12-14 18:48:05 +0000552 // now layer count should be 0
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000553 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
554 testStep->assertMessage());
555}
556TEST_STEP(DrawLayer, DrawLayerTestStep);
reed@google.com3b3e8952012-08-16 20:53:31 +0000557
558static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000559 skiatest::Reporter*,
560 CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000561 // This test step challenges the TestDeferredCanvasStateConsistency
562 // test cases because the opaque paint can trigger an optimization
563 // that discards previously recorded commands. The challenge is to maintain
564 // correct clip and matrix stack state.
565 canvas->resetMatrix();
566 canvas->rotate(SkIntToScalar(30));
567 canvas->save();
568 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
569 canvas->save();
570 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
571 SkPaint paint;
572 paint.setColor(0xFFFFFFFF);
573 canvas->drawPaint(paint);
574 canvas->restore();
575 canvas->restore();
576}
577TEST_STEP(NestedSaveRestoreWithSolidPaint, \
578 NestedSaveRestoreWithSolidPaintTestStep);
579
580static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000581 skiatest::Reporter*,
582 CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000583 // This test step challenges the TestDeferredCanvasStateConsistency
584 // test case because the canvas flush on a deferred canvas will
585 // reset the recording session. The challenge is to maintain correct
586 // clip and matrix stack state on the playback canvas.
587 canvas->resetMatrix();
588 canvas->rotate(SkIntToScalar(30));
589 canvas->save();
590 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
591 canvas->save();
592 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
593 canvas->drawRect(kTestRect,kTestPaint);
594 canvas->flush();
595 canvas->restore();
596 canvas->restore();
597}
598TEST_STEP(NestedSaveRestoreWithFlush, \
599 NestedSaveRestoreWithFlushTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000600
601static void AssertCanvasStatesEqual(skiatest::Reporter* reporter,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000602 const SkCanvas* canvas1,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000603 const SkCanvas* canvas2,
604 CanvasTestStep* testStep) {
605 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
606 canvas2->getDeviceSize(), testStep->assertMessage());
607 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
608 canvas2->getSaveCount(), testStep->assertMessage());
609 REPORTER_ASSERT_MESSAGE(reporter, canvas1->isDrawingToLayer() ==
610 canvas2->isDrawingToLayer(), testStep->assertMessage());
reed@google.com3b3e8952012-08-16 20:53:31 +0000611
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000612 SkRect bounds1, bounds2;
613 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.com3b3e8952012-08-16 20:53:31 +0000614 canvas1->getClipBounds(&bounds1) == canvas2->getClipBounds(&bounds2),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000615 testStep->assertMessage());
616 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
reed@google.com3b3e8952012-08-16 20:53:31 +0000617 testStep->assertMessage());
618
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000619 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
620 canvas2->getDrawFilter(), testStep->assertMessage());
621 SkIRect deviceBounds1, deviceBounds2;
622 REPORTER_ASSERT_MESSAGE(reporter,
623 canvas1->getClipDeviceBounds(&deviceBounds1) ==
624 canvas2->getClipDeviceBounds(&deviceBounds2),
625 testStep->assertMessage());
626 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2,
627 testStep->assertMessage());
628 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getBounder() ==
629 canvas2->getBounder(), testStep->assertMessage());
630 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
631 canvas2->getTotalMatrix(), testStep->assertMessage());
632 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getClipType() ==
633 canvas2->getClipType(), testStep->assertMessage());
634 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalClip() ==
635 canvas2->getTotalClip(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000636
637 // The following test code is commented out because the test fails when
rmistry@google.comd6176b02012-08-23 18:14:13 +0000638 // the canvas is an SkPictureRecord or SkDeferredCanvas
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000639 // Issue: http://code.google.com/p/skia/issues/detail?id=498
640 // Also, creating a LayerIter on an SkProxyCanvas crashes
641 // Issue: http://code.google.com/p/skia/issues/detail?id=499
642 /*
643 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
644 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
645 while (!layerIter1.done() && !layerIter2.done()) {
646 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
647 layerIter2.matrix(), testStep->assertMessage());
648 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
649 layerIter2.clip(), testStep->assertMessage());
650 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
651 layerIter2.paint(), testStep->assertMessage());
652 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
653 layerIter2.x(), testStep->assertMessage());
654 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
655 layerIter2.y(), testStep->assertMessage());
656 layerIter1.next();
657 layerIter2.next();
658 }
659 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
660 testStep->assertMessage());
661 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
662 testStep->assertMessage());
663 */
664}
665
666// The following class groups static functions that need to access
667// the privates members of SkPictureRecord
668class SkPictureTester {
669private:
reed@google.come2589ae2012-07-10 19:38:01 +0000670 static int EQ(const SkFlatData* a, const SkFlatData* b) {
671 return *a == *b;
672 }
673
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000674 static void AssertFlattenedObjectsEqual(
675 SkPictureRecord* referenceRecord,
676 SkPictureRecord* testRecord,
677 skiatest::Reporter* reporter,
678 CanvasTestStep* testStep) {
679
680 REPORTER_ASSERT_MESSAGE(reporter,
djsollen@google.comc9ab9872012-08-29 18:52:07 +0000681 referenceRecord->fBitmapHeap->count() ==
682 testRecord->fBitmapHeap->count(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000683 REPORTER_ASSERT_MESSAGE(reporter,
684 referenceRecord->fMatrices.count() ==
685 testRecord->fMatrices.count(), testStep->assertMessage());
686 for (int i = 0; i < referenceRecord->fMatrices.count(); ++i) {
687 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.come2589ae2012-07-10 19:38:01 +0000688 EQ(referenceRecord->fMatrices[i], testRecord->fMatrices[i]),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000689 testStep->assertMessage());
690 }
691 REPORTER_ASSERT_MESSAGE(reporter,
692 referenceRecord->fPaints.count() ==
693 testRecord->fPaints.count(), testStep->assertMessage());
694 for (int i = 0; i < referenceRecord->fPaints.count(); ++i) {
695 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.come2589ae2012-07-10 19:38:01 +0000696 EQ(referenceRecord->fPaints[i], testRecord->fPaints[i]),
697 testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000698 }
699 REPORTER_ASSERT_MESSAGE(reporter,
700 referenceRecord->fRegions.count() ==
701 testRecord->fRegions.count(), testStep->assertMessage());
702 for (int i = 0; i < referenceRecord->fRegions.count(); ++i) {
703 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.come2589ae2012-07-10 19:38:01 +0000704 EQ(referenceRecord->fRegions[i], testRecord->fRegions[i]),
705 testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000706 }
707 REPORTER_ASSERT_MESSAGE(reporter,
708 !referenceRecord->fPathHeap ==
709 !testRecord->fPathHeap,
710 testStep->assertMessage());
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000711 // The following tests are commented out because they currently
712 // fail. Issue: http://code.google.com/p/skia/issues/detail?id=507
713 /*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000714 if (referenceRecord->fPathHeap) {
715 REPORTER_ASSERT_MESSAGE(reporter,
716 referenceRecord->fPathHeap->count() ==
717 testRecord->fPathHeap->count(),
718 testStep->assertMessage());
719 for (int i = 0; i < referenceRecord->fPathHeap->count(); ++i) {
720 REPORTER_ASSERT_MESSAGE(reporter,
721 (*referenceRecord->fPathHeap)[i] ==
722 (*testRecord->fPathHeap)[i], testStep->assertMessage());
723 }
724 }
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000725 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000726
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000727 }
728
729public:
730
rmistry@google.comd6176b02012-08-23 18:14:13 +0000731 static void TestPictureFlattenedObjectReuse(skiatest::Reporter* reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000732 CanvasTestStep* testStep,
733 uint32_t recordFlags) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000734 // Verify that when a test step is executed twice, no extra resources
735 // are flattened during the second execution
736 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
737 SkPicture referencePicture;
738 SkCanvas* referenceCanvas = referencePicture.beginRecording(kWidth,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000739 kHeight, recordFlags);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000740 testStep->draw(referenceCanvas, reporter);
741 SkPicture testPicture;
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000742 SkCanvas* testCanvas = testPicture.beginRecording(kWidth,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000743 kHeight, recordFlags);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000744 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000745 testStep->setAssertMessageFormat(kPictureSecondDrawAssertMessageFormat);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000746 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000747
748 SkPictureRecord* referenceRecord = static_cast<SkPictureRecord*>(
749 referenceCanvas);
750 SkPictureRecord* testRecord = static_cast<SkPictureRecord*>(
751 testCanvas);
752 testStep->setAssertMessageFormat(kPictureResourceReuseMessageFormat);
753 AssertFlattenedObjectsEqual(referenceRecord, testRecord,
junov@chromium.org76b9c4b2012-02-22 21:24:41 +0000754 reporter, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000755 }
756};
757
edisonn@google.com77909122012-10-18 15:58:23 +0000758static void TestPdfDevice(skiatest::Reporter* reporter,
759 CanvasTestStep* testStep) {
760 SkISize pageSize = SkISize::Make(kWidth, kHeight);
761 SkPDFDevice device(pageSize, pageSize, SkMatrix::I());
762 SkCanvas canvas(&device);
763 testStep->setAssertMessageFormat(kPdfAssertMessageFormat);
764 testStep->draw(&canvas, reporter);
765 SkPDFDocument doc;
766 doc.appendPage(&device);
767 SkDynamicMemoryWStream stream;
768 doc.emitPDF(&stream);
769}
770
junov@chromium.org88e29142012-08-07 16:48:22 +0000771// The following class groups static functions that need to access
772// the privates members of SkDeferredCanvas
773class SkDeferredCanvasTester {
774public:
775 static void TestDeferredCanvasStateConsistency(
776 skiatest::Reporter* reporter,
777 CanvasTestStep* testStep,
junov@chromium.orgfb103892012-09-20 19:35:43 +0000778 const SkCanvas& referenceCanvas, bool silent) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000779
junov@chromium.org88e29142012-08-07 16:48:22 +0000780 SkBitmap deferredStore;
781 createBitmap(&deferredStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
robertphillips@google.com9b051a32013-08-20 20:06:40 +0000782 SkDevice deferredDevice(deferredStore);
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +0000783 SkAutoTUnref<SkDeferredCanvas> deferredCanvas(SkDeferredCanvas::Create(&deferredDevice));
junov@chromium.org88e29142012-08-07 16:48:22 +0000784 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
junov@chromium.org66070a52013-05-28 17:39:08 +0000785 testStep->draw(deferredCanvas, reporter);
junov@chromium.org88e29142012-08-07 16:48:22 +0000786 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
junov@chromium.org66070a52013-05-28 17:39:08 +0000787 AssertCanvasStatesEqual(reporter, deferredCanvas, &referenceCanvas,
junov@chromium.org88e29142012-08-07 16:48:22 +0000788 testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000789
junov@chromium.orgfb103892012-09-20 19:35:43 +0000790 if (silent) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000791 deferredCanvas->silentFlush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000792 } else {
junov@chromium.org66070a52013-05-28 17:39:08 +0000793 deferredCanvas->flush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000794 }
795
skia.committer@gmail.com4c5ea442012-09-21 02:01:01 +0000796 testStep->setAssertMessageFormat(
junov@chromium.orgfb103892012-09-20 19:35:43 +0000797 silent ? kDeferredPostSilentFlushPlaybackAssertMessageFormat :
junov@chromium.org88e29142012-08-07 16:48:22 +0000798 kDeferredPostFlushPlaybackAssertMessageFormat);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000799 AssertCanvasStatesEqual(reporter,
junov@chromium.org66070a52013-05-28 17:39:08 +0000800 deferredCanvas->immediateCanvas(),
junov@chromium.org88e29142012-08-07 16:48:22 +0000801 &referenceCanvas, testStep);
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000802
junov@chromium.org88e29142012-08-07 16:48:22 +0000803 // Verified that deferred canvas state is not affected by flushing
804 // pending draw operations
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000805
junov@chromium.org88e29142012-08-07 16:48:22 +0000806 // The following test code is commented out because it currently fails.
807 // Issue: http://code.google.com/p/skia/issues/detail?id=496
808 /*
809 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
810 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
811 testStep);
812 */
813 }
814};
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000815
caryclark@google.com42639cd2012-06-06 12:03:39 +0000816// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000817static void TestProxyCanvasStateConsistency(
818 skiatest::Reporter* reporter,
819 CanvasTestStep* testStep,
820 const SkCanvas& referenceCanvas) {
821
822 SkBitmap indirectStore;
823 createBitmap(&indirectStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
robertphillips@google.com9b051a32013-08-20 20:06:40 +0000824 SkDevice indirectDevice(indirectStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000825 SkCanvas indirectCanvas(&indirectDevice);
826 SkProxyCanvas proxyCanvas(&indirectCanvas);
827 testStep->setAssertMessageFormat(kProxyDrawAssertMessageFormat);
828 testStep->draw(&proxyCanvas, reporter);
829 // Verify that the SkProxyCanvas reports consitent state
830 testStep->setAssertMessageFormat(kProxyStateAssertMessageFormat);
831 AssertCanvasStatesEqual(reporter, &proxyCanvas, &referenceCanvas,
832 testStep);
833 // Verify that the indirect canvas reports consitent state
834 testStep->setAssertMessageFormat(kProxyIndirectStateAssertMessageFormat);
835 AssertCanvasStatesEqual(reporter, &indirectCanvas, &referenceCanvas,
836 testStep);
837}
838
caryclark@google.com42639cd2012-06-06 12:03:39 +0000839// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000840static void TestNWayCanvasStateConsistency(
841 skiatest::Reporter* reporter,
842 CanvasTestStep* testStep,
843 const SkCanvas& referenceCanvas) {
844
845 SkBitmap indirectStore1;
846 createBitmap(&indirectStore1, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
robertphillips@google.com9b051a32013-08-20 20:06:40 +0000847 SkDevice indirectDevice1(indirectStore1);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000848 SkCanvas indirectCanvas1(&indirectDevice1);
849
850 SkBitmap indirectStore2;
851 createBitmap(&indirectStore2, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
robertphillips@google.com9b051a32013-08-20 20:06:40 +0000852 SkDevice indirectDevice2(indirectStore2);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000853 SkCanvas indirectCanvas2(&indirectDevice2);
854
djsollen@google.comf0a062b2012-05-01 16:50:25 +0000855 SkISize canvasSize = referenceCanvas.getDeviceSize();
856 SkNWayCanvas nWayCanvas(canvasSize.width(), canvasSize.height());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000857 nWayCanvas.addCanvas(&indirectCanvas1);
858 nWayCanvas.addCanvas(&indirectCanvas2);
859
860 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
861 testStep->draw(&nWayCanvas, reporter);
862 // Verify that the SkProxyCanvas reports consitent state
863 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
864 AssertCanvasStatesEqual(reporter, &nWayCanvas, &referenceCanvas,
865 testStep);
866 // Verify that the indirect canvases report consitent state
867 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
868 AssertCanvasStatesEqual(reporter, &indirectCanvas1, &referenceCanvas,
869 testStep);
870 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
871 AssertCanvasStatesEqual(reporter, &indirectCanvas2, &referenceCanvas,
872 testStep);
873}
874
875/*
876 * This sub-test verifies that the test step passes when executed
877 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
878 * that the all canvas derivatives report the same state as an SkCanvas
879 * after having executed the test step.
880 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000881static void TestOverrideStateConsistency(skiatest::Reporter* reporter,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000882 CanvasTestStep* testStep) {
883 SkBitmap referenceStore;
884 createBitmap(&referenceStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
robertphillips@google.com9b051a32013-08-20 20:06:40 +0000885 SkDevice referenceDevice(referenceStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000886 SkCanvas referenceCanvas(&referenceDevice);
887 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
888 testStep->draw(&referenceCanvas, reporter);
889
junov@chromium.orgfb103892012-09-20 19:35:43 +0000890 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, false);
891
892 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, true);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000893
caryclark@google.com42639cd2012-06-06 12:03:39 +0000894 // The following test code is disabled because SkProxyCanvas is
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000895 // missing a lot of virtual overrides on get* methods, which are used
896 // to verify canvas state.
897 // Issue: http://code.google.com/p/skia/issues/detail?id=500
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000898
caryclark@google.com42639cd2012-06-06 12:03:39 +0000899 if (false) { // avoid bit rot, suppress warning
900 TestProxyCanvasStateConsistency(reporter, testStep, referenceCanvas);
901 }
902
903 // The following test code is disabled because SkNWayCanvas does not
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000904 // report correct clipping and device bounds information
905 // Issue: http://code.google.com/p/skia/issues/detail?id=501
caryclark@google.com42639cd2012-06-06 12:03:39 +0000906
907 if (false) { // avoid bit rot, suppress warning
908 TestNWayCanvasStateConsistency(reporter, testStep, referenceCanvas);
909 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000910
caryclark@google.com42639cd2012-06-06 12:03:39 +0000911 if (false) { // avoid bit rot, suppress warning
912 test_clipVisitor(reporter, &referenceCanvas);
913 }
reed@google.com7c202932011-12-14 18:48:05 +0000914}
reed@google.com37f3ae02011-11-28 16:06:04 +0000915
916static void TestCanvas(skiatest::Reporter* reporter) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000917 // Init global here because bitmap pixels cannot be alocated during
918 // static initialization
919 kTestBitmap = testBitmap();
reed@google.com37f3ae02011-11-28 16:06:04 +0000920
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000921 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
922 TestOverrideStateConsistency(reporter, testStepArray()[testStep]);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000923 SkPictureTester::TestPictureFlattenedObjectReuse(reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000924 testStepArray()[testStep], 0);
edisonn@google.com77909122012-10-18 15:58:23 +0000925 if (testStepArray()[testStep]->enablePdfTesting()) {
926 TestPdfDevice(reporter, testStepArray()[testStep]);
927 }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000928 }
junov@chromium.orgcd62ecf2012-08-02 17:43:25 +0000929
930 // Explicitly call reset(), so we don't leak the pixels (since kTestBitmap is a global)
931 kTestBitmap.reset();
reed@google.com37f3ae02011-11-28 16:06:04 +0000932}
933
934#include "TestClassDef.h"
935DEFINE_TESTCLASS("Canvas", TestCanvasClass, TestCanvas)