blob: 6d9fa1a671f8a3e239efac954e1d3b0fdc34f1c4 [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;
reed@google.com7c202932011-12-14 18:48:05 +000097
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000098// Format strings that describe the test context. The %s token is where
99// the name of the test step is inserted. The context is required for
100// disambiguating the error in the case of failures that are reported in
101// functions that are called multiple times in different contexts (test
102// cases and test steps).
103static const char* const kDefaultAssertMessageFormat = "%s";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000104static const char* const kCanvasDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000105 "Drawing test step %s with SkCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000106static const char* const kPictureDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000107 "Drawing test step %s with SkPicture";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000108static const char* const kPictureSecondDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000109 "Duplicate draw of test step %s with SkPicture";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000110static const char* const kDeferredDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000111 "Drawing test step %s with SkDeferredCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000112static const char* const kProxyDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000113 "Drawing test step %s with SkProxyCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000114static const char* const kNWayDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000115 "Drawing test step %s with SkNWayCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000116static const char* const kDeferredPreFlushAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000117 "test step %s, SkDeferredCanvas state consistency before flush";
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000118static const char* const kDeferredPostFlushPlaybackAssertMessageFormat =
119 "test step %s, SkDeferredCanvas playback canvas state consistency after flush";
junov@chromium.orgfb103892012-09-20 19:35:43 +0000120static const char* const kDeferredPostSilentFlushPlaybackAssertMessageFormat =
121 "test step %s, SkDeferredCanvas playback canvas state consistency after silent flush";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000122static const char* const kPictureResourceReuseMessageFormat =
123 "test step %s, SkPicture duplicate flattened object test";
124static const char* const kProxyStateAssertMessageFormat =
125 "test step %s, SkProxyCanvas state consistency";
126static const char* const kProxyIndirectStateAssertMessageFormat =
127 "test step %s, SkProxyCanvas indirect canvas state consistency";
128static const char* const kNWayStateAssertMessageFormat =
129 "test step %s, SkNWayCanvas state consistency";
130static const char* const kNWayIndirect1StateAssertMessageFormat =
131 "test step %s, SkNWayCanvas indirect canvas 1 state consistency";
132static const char* const kNWayIndirect2StateAssertMessageFormat =
133 "test step %s, SkNWayCanvas indirect canvas 2 state consistency";
edisonn@google.com77909122012-10-18 15:58:23 +0000134static const char* const kPdfAssertMessageFormat =
135 "PDF sanity check failed %s";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000136
137static void createBitmap(SkBitmap* bm, SkBitmap::Config config, SkColor color) {
138 bm->setConfig(config, kWidth, kHeight);
139 bm->allocPixels();
140 bm->eraseColor(color);
141}
142
143class CanvasTestStep;
144static SkTDArray<CanvasTestStep*>& testStepArray() {
145 static SkTDArray<CanvasTestStep*> theTests;
146 return theTests;
147}
148
149class CanvasTestStep {
150public:
edisonn@google.com77909122012-10-18 15:58:23 +0000151 CanvasTestStep(bool fEnablePdfTesting = true) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000152 *testStepArray().append() = this;
153 fAssertMessageFormat = kDefaultAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000154 this->fEnablePdfTesting = fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000155 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000156 virtual ~CanvasTestStep() { }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000157
158 virtual void draw(SkCanvas*, skiatest::Reporter*) = 0;
159 virtual const char* name() const = 0;
160
161 const char* assertMessage() {
162 fAssertMessage.printf(fAssertMessageFormat, name());
163 return fAssertMessage.c_str();
164 }
165
166 void setAssertMessageFormat(const char* format) {
167 fAssertMessageFormat = format;
168 }
169
edisonn@google.com77909122012-10-18 15:58:23 +0000170 bool enablePdfTesting() { return fEnablePdfTesting; }
171
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000172private:
173 SkString fAssertMessage;
174 const char* fAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000175 bool fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000176};
177
178///////////////////////////////////////////////////////////////////////////////
179// Constants used by test steps
180
rmistry@google.comd6176b02012-08-23 18:14:13 +0000181const SkRect kTestRect =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000182 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
183 SkIntToScalar(2), SkIntToScalar(1));
184static SkMatrix testMatrix() {
185 SkMatrix matrix;
186 matrix.reset();
187 matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
188 return matrix;
189}
190const SkMatrix kTestMatrix = testMatrix();
191static SkPath testPath() {
192 SkPath path;
193 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
194 SkIntToScalar(2), SkIntToScalar(1)));
195 return path;
196}
197const SkPath kTestPath = testPath();
198static SkRegion testRegion() {
199 SkRegion region;
200 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
201 region.setRect(rect);
202 return region;
203}
204const SkIRect kTestIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
205const SkRegion kTestRegion = testRegion();
206const SkColor kTestColor = 0x01020304;
207const SkPaint kTestPaint;
208const SkPoint kTestPoints[3] = {
209 {SkIntToScalar(0), SkIntToScalar(0)},
210 {SkIntToScalar(2), SkIntToScalar(1)},
211 {SkIntToScalar(0), SkIntToScalar(2)}
212};
213const size_t kTestPointCount = 3;
214static SkBitmap testBitmap() {
215 SkBitmap bitmap;
216 createBitmap(&bitmap, SkBitmap::kARGB_8888_Config, 0x05060708);
217 return bitmap;
218}
219SkBitmap kTestBitmap; // cannot be created during static init
220SkString kTestText("Hello World");
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000221SkPoint kTestPoints2[] = {
222 { SkIntToScalar(0), SkIntToScalar(1) },
223 { SkIntToScalar(1), SkIntToScalar(1) },
224 { SkIntToScalar(2), SkIntToScalar(1) },
225 { SkIntToScalar(3), SkIntToScalar(1) },
226 { SkIntToScalar(4), SkIntToScalar(1) },
227 { SkIntToScalar(5), SkIntToScalar(1) },
228 { SkIntToScalar(6), SkIntToScalar(1) },
229 { SkIntToScalar(7), SkIntToScalar(1) },
230 { SkIntToScalar(8), SkIntToScalar(1) },
231 { SkIntToScalar(9), SkIntToScalar(1) },
232 { SkIntToScalar(10), SkIntToScalar(1) },
233};
rmistry@google.comd6176b02012-08-23 18:14:13 +0000234
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000235
236///////////////////////////////////////////////////////////////////////////////
237// Macros for defining test steps
238
239#define TEST_STEP(NAME, FUNCTION) \
240class NAME##_TestStep : public CanvasTestStep{ \
241public: \
242 virtual void draw(SkCanvas* canvas, skiatest::Reporter* reporter) { \
243 FUNCTION (canvas, reporter, this); \
244 } \
245 virtual const char* name() const {return #NAME ;} \
246}; \
247static NAME##_TestStep NAME##_TestStepInstance;
248
edisonn@google.com77909122012-10-18 15:58:23 +0000249#define TEST_STEP_NO_PDF(NAME, FUNCTION) \
250class NAME##_TestStep : public CanvasTestStep{ \
251public: \
252 NAME##_TestStep() : CanvasTestStep(false) {} \
253 virtual void draw(SkCanvas* canvas, skiatest::Reporter* reporter) { \
254 FUNCTION (canvas, reporter, this); \
255 } \
256 virtual const char* name() const {return #NAME ;} \
257}; \
258static NAME##_TestStep NAME##_TestStepInstance;
259
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000260#define SIMPLE_TEST_STEP(NAME, CALL) \
261static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter*, \
262 CanvasTestStep*) { \
263 canvas-> CALL ; \
264} \
265TEST_STEP(NAME, NAME##TestStep )
266
267#define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
268static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter* reporter, \
269 CanvasTestStep* testStep) { \
270 REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \
271 testStep->assertMessage()); \
272} \
273TEST_STEP(NAME, NAME##TestStep )
274
275
276///////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000277// Basic test steps for most virtual methods in SkCanvas that draw or affect
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000278// the state of the canvas.
279
junov@chromium.orga907ac32012-02-24 21:54:07 +0000280SIMPLE_TEST_STEP_WITH_ASSERT(Translate,
281 translate(SkIntToScalar(1), SkIntToScalar(2)));
282SIMPLE_TEST_STEP_WITH_ASSERT(Scale,
283 scale(SkIntToScalar(1), SkIntToScalar(2)));
284SIMPLE_TEST_STEP_WITH_ASSERT(Rotate, rotate(SkIntToScalar(1)));
285SIMPLE_TEST_STEP_WITH_ASSERT(Skew,
286 skew(SkIntToScalar(1), SkIntToScalar(2)));
287SIMPLE_TEST_STEP_WITH_ASSERT(Concat, concat(kTestMatrix));
288SIMPLE_TEST_STEP(SetMatrix, setMatrix(kTestMatrix));
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000289SIMPLE_TEST_STEP(ClipRect, clipRect(kTestRect));
290SIMPLE_TEST_STEP(ClipPath, clipPath(kTestPath));
291SIMPLE_TEST_STEP(ClipRegion,
junov@chromium.orga907ac32012-02-24 21:54:07 +0000292 clipRegion(kTestRegion, SkRegion::kReplace_Op));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000293SIMPLE_TEST_STEP(Clear, clear(kTestColor));
294SIMPLE_TEST_STEP(DrawPaint, drawPaint(kTestPaint));
295SIMPLE_TEST_STEP(DrawPointsPoints, drawPoints(SkCanvas::kPoints_PointMode,
296 kTestPointCount, kTestPoints, kTestPaint));
297SIMPLE_TEST_STEP(DrawPointsLiness, drawPoints(SkCanvas::kLines_PointMode,
298 kTestPointCount, kTestPoints, kTestPaint));
299SIMPLE_TEST_STEP(DrawPointsPolygon, drawPoints(SkCanvas::kPolygon_PointMode,
300 kTestPointCount, kTestPoints, kTestPaint));
301SIMPLE_TEST_STEP(DrawRect, drawRect(kTestRect, kTestPaint));
302SIMPLE_TEST_STEP(DrawPath, drawPath(kTestPath, kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000303SIMPLE_TEST_STEP(DrawBitmap, drawBitmap(kTestBitmap, 0, 0));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000304SIMPLE_TEST_STEP(DrawBitmapPaint, drawBitmap(kTestBitmap, 0, 0, &kTestPaint));
305SIMPLE_TEST_STEP(DrawBitmapRect, drawBitmapRect(kTestBitmap, NULL, kTestRect,
306 NULL));
307SIMPLE_TEST_STEP(DrawBitmapRectSrcRect, drawBitmapRect(kTestBitmap,
308 &kTestIRect, kTestRect, NULL));
309SIMPLE_TEST_STEP(DrawBitmapRectPaint, drawBitmapRect(kTestBitmap, NULL,
310 kTestRect, &kTestPaint));
311SIMPLE_TEST_STEP(DrawBitmapMatrix, drawBitmapMatrix(kTestBitmap, kTestMatrix,
312 NULL));
313SIMPLE_TEST_STEP(DrawBitmapMatrixPaint, drawBitmapMatrix(kTestBitmap,
314 kTestMatrix, &kTestPaint));
315SIMPLE_TEST_STEP(DrawBitmapNine, drawBitmapNine(kTestBitmap, kTestIRect,
316 kTestRect, NULL));
317SIMPLE_TEST_STEP(DrawBitmapNinePaint, drawBitmapNine(kTestBitmap, kTestIRect,
318 kTestRect, &kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000319SIMPLE_TEST_STEP(DrawSprite, drawSprite(kTestBitmap, 0, 0, NULL));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000320SIMPLE_TEST_STEP(DrawSpritePaint, drawSprite(kTestBitmap, 0, 0, &kTestPaint));
321SIMPLE_TEST_STEP(DrawText, drawText(kTestText.c_str(), kTestText.size(),
322 0, 1, kTestPaint));
323SIMPLE_TEST_STEP(DrawPosText, drawPosText(kTestText.c_str(),
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000324 kTestText.size(), kTestPoints2, kTestPaint));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000325SIMPLE_TEST_STEP(DrawTextOnPath, drawTextOnPath(kTestText.c_str(),
326 kTestText.size(), kTestPath, NULL, kTestPaint));
327SIMPLE_TEST_STEP(DrawTextOnPathMatrix, drawTextOnPath(kTestText.c_str(),
328 kTestText.size(), kTestPath, &kTestMatrix, kTestPaint));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000329SIMPLE_TEST_STEP(DrawData, drawData(kTestText.c_str(), kTestText.size()));
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000330SIMPLE_TEST_STEP(BeginGroup, beginCommentGroup(kTestText.c_str()));
331SIMPLE_TEST_STEP(AddComment, addComment(kTestText.c_str(), kTestText.c_str()));
332SIMPLE_TEST_STEP(EndGroup, endCommentGroup());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000333
334///////////////////////////////////////////////////////////////////////////////
335// Complex test steps
336
rmistry@google.comd6176b02012-08-23 18:14:13 +0000337// Save/restore calls cannot be in isolated simple test steps because the test
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000338// cases that use SkPicture require that save and restore calls be balanced.
rmistry@google.comd6176b02012-08-23 18:14:13 +0000339static void SaveMatrixStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000340 skiatest::Reporter* reporter,
341 CanvasTestStep* testStep) {
342 int saveCount = canvas->getSaveCount();
343 canvas->save(SkCanvas::kMatrix_SaveFlag);
344 canvas->clipRegion(kTestRegion);
345 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
346 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000347 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000348 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000349 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000350 testStep->assertMessage());
351 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() == kTestRegion,
352 testStep->assertMessage());
353}
354TEST_STEP(SaveMatrix, SaveMatrixStep);
355
rmistry@google.comd6176b02012-08-23 18:14:13 +0000356static void SaveClipStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000357 skiatest::Reporter* reporter,
358 CanvasTestStep* testStep) {
359 int saveCount = canvas->getSaveCount();
360 canvas->save(SkCanvas::kClip_SaveFlag);
361 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
362 canvas->clipRegion(kTestRegion);
363 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000364 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000365 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000366 REPORTER_ASSERT_MESSAGE(reporter, !canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000367 testStep->assertMessage());
368 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion,
369 testStep->assertMessage());
370}
371TEST_STEP(SaveClip, SaveClipStep);
372
rmistry@google.comd6176b02012-08-23 18:14:13 +0000373static void SaveMatrixClipStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000374 skiatest::Reporter* reporter,
375 CanvasTestStep* testStep) {
376 int saveCount = canvas->getSaveCount();
377 canvas->save(SkCanvas::kMatrixClip_SaveFlag);
378 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
379 canvas->clipRegion(kTestRegion);
380 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000381 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000382 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000383 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000384 testStep->assertMessage());
385 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion,
386 testStep->assertMessage());
387}
388TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
389
rmistry@google.comd6176b02012-08-23 18:14:13 +0000390static void SaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000391 skiatest::Reporter* reporter,
392 CanvasTestStep* testStep) {
393 int saveCount = canvas->getSaveCount();
394 canvas->saveLayer(NULL, NULL);
395 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000396 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000397 testStep->assertMessage());
398}
399TEST_STEP(SaveLayer, SaveLayerStep);
400
rmistry@google.comd6176b02012-08-23 18:14:13 +0000401static void BoundedSaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000402 skiatest::Reporter* reporter,
403 CanvasTestStep* testStep) {
404 int saveCount = canvas->getSaveCount();
405 canvas->saveLayer(&kTestRect, NULL);
406 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000407 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000408 testStep->assertMessage());
409}
410TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
411
rmistry@google.comd6176b02012-08-23 18:14:13 +0000412static void PaintSaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000413 skiatest::Reporter* reporter,
414 CanvasTestStep* testStep) {
415 int saveCount = canvas->getSaveCount();
416 canvas->saveLayer(NULL, &kTestPaint);
417 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000418 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000419 testStep->assertMessage());
420}
421TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
422
rmistry@google.comd6176b02012-08-23 18:14:13 +0000423static void TwoClipOpsStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000424 skiatest::Reporter*,
425 CanvasTestStep*) {
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000426 // This test exercises a functionality in SkPicture that leads to the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000427 // recording of restore offset placeholders. This test will trigger an
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000428 // assertion at playback time if the placeholders are not properly
429 // filled when the recording ends.
430 canvas->clipRect(kTestRect);
431 canvas->clipRegion(kTestRegion);
432}
433TEST_STEP(TwoClipOps, TwoClipOpsStep);
434
epoger@google.com94fa43c2012-04-11 17:51:01 +0000435// exercise fix for http://code.google.com/p/skia/issues/detail?id=560
436// ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
rmistry@google.comd6176b02012-08-23 18:14:13 +0000437static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000438 skiatest::Reporter*,
439 CanvasTestStep*) {
epoger@google.com94fa43c2012-04-11 17:51:01 +0000440 SkPaint paint;
441 paint.setStrokeWidth(SkIntToScalar(1));
442 paint.setStyle(SkPaint::kStroke_Style);
443
444 SkPath path;
445 SkPoint pt1 = { 0, 0 };
446 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
447 SkPoint pt3 = { SkIntToScalar(1), 0 };
448 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
449 path.moveTo(pt1);
450 path.lineTo(pt2);
451 path.lineTo(pt3);
452 path.lineTo(pt4);
453
454 canvas->drawPath(path, paint);
455}
456TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
457
rmistry@google.comd6176b02012-08-23 18:14:13 +0000458static void DrawVerticesShaderTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000459 skiatest::Reporter*,
460 CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000461 SkPoint pts[4];
462 pts[0].set(0, 0);
463 pts[1].set(SkIntToScalar(kWidth), 0);
464 pts[2].set(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
465 pts[3].set(0, SkIntToScalar(kHeight));
466 SkPaint paint;
467 SkShader* shader = SkShader::CreateBitmapShader(kTestBitmap,
468 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
469 paint.setShader(shader)->unref();
470 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
471 NULL, NULL, NULL, 0, paint);
472}
edisonn@google.com77909122012-10-18 15:58:23 +0000473// NYI: issue 240.
474TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000475
rmistry@google.comd6176b02012-08-23 18:14:13 +0000476static void DrawPictureTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000477 skiatest::Reporter*,
478 CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000479 SkPicture* testPicture = SkNEW_ARGS(SkPicture, ());
480 SkAutoUnref aup(testPicture);
481 SkCanvas* testCanvas = testPicture->beginRecording(kWidth, kHeight);
482 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
483 testCanvas->clipRect(kTestRect);
484 testCanvas->drawRect(kTestRect, kTestPaint);
485 canvas->drawPicture(*testPicture);
486}
487TEST_STEP(DrawPicture, DrawPictureTestStep);
488
rmistry@google.comd6176b02012-08-23 18:14:13 +0000489static void SaveRestoreTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000490 skiatest::Reporter* reporter,
491 CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000492 int baseSaveCount = canvas->getSaveCount();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000493 int n = canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000494 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount == n, testStep->assertMessage());
495 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000496 testStep->assertMessage());
497 canvas->save();
498 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000499 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000500 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000501 canvas->restoreToCount(baseSaveCount + 1);
502 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000503 testStep->assertMessage());
504
505 // should this pin to 1, or be a no-op, or crash?
506 canvas->restoreToCount(0);
507 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
508 testStep->assertMessage());
509}
510TEST_STEP(SaveRestore, SaveRestoreTestStep);
511
rmistry@google.comd6176b02012-08-23 18:14:13 +0000512static void DrawLayerTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000513 skiatest::Reporter* reporter,
514 CanvasTestStep* testStep) {
515 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
516 testStep->assertMessage());
517 canvas->save();
518 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
519 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000520 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000521
reed@google.com7c202932011-12-14 18:48:05 +0000522 const SkRect* bounds = NULL; // null means include entire bounds
523 const SkPaint* paint = NULL;
524
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000525 canvas->saveLayer(bounds, paint);
526 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
527 testStep->assertMessage());
528 canvas->restore();
529 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
530 testStep->assertMessage());
reed@google.com7c202932011-12-14 18:48:05 +0000531
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000532 canvas->saveLayer(bounds, paint);
533 canvas->saveLayer(bounds, paint);
534 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
535 testStep->assertMessage());
536 canvas->restore();
537 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
538 testStep->assertMessage());
539 canvas->restore();
reed@google.com7c202932011-12-14 18:48:05 +0000540 // now layer count should be 0
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000541 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
542 testStep->assertMessage());
543}
544TEST_STEP(DrawLayer, DrawLayerTestStep);
reed@google.com3b3e8952012-08-16 20:53:31 +0000545
546static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000547 skiatest::Reporter*,
548 CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000549 // This test step challenges the TestDeferredCanvasStateConsistency
550 // test cases because the opaque paint can trigger an optimization
551 // that discards previously recorded commands. The challenge is to maintain
552 // correct clip and matrix stack state.
553 canvas->resetMatrix();
554 canvas->rotate(SkIntToScalar(30));
555 canvas->save();
556 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
557 canvas->save();
558 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
559 SkPaint paint;
560 paint.setColor(0xFFFFFFFF);
561 canvas->drawPaint(paint);
562 canvas->restore();
563 canvas->restore();
564}
565TEST_STEP(NestedSaveRestoreWithSolidPaint, \
566 NestedSaveRestoreWithSolidPaintTestStep);
567
568static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000569 skiatest::Reporter*,
570 CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000571 // This test step challenges the TestDeferredCanvasStateConsistency
572 // test case because the canvas flush on a deferred canvas will
573 // reset the recording session. The challenge is to maintain correct
574 // clip and matrix stack state on the playback canvas.
575 canvas->resetMatrix();
576 canvas->rotate(SkIntToScalar(30));
577 canvas->save();
578 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
579 canvas->save();
580 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
581 canvas->drawRect(kTestRect,kTestPaint);
582 canvas->flush();
583 canvas->restore();
584 canvas->restore();
585}
586TEST_STEP(NestedSaveRestoreWithFlush, \
587 NestedSaveRestoreWithFlushTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000588
589static void AssertCanvasStatesEqual(skiatest::Reporter* reporter,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000590 const SkCanvas* canvas1,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000591 const SkCanvas* canvas2,
592 CanvasTestStep* testStep) {
593 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
594 canvas2->getDeviceSize(), testStep->assertMessage());
595 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
596 canvas2->getSaveCount(), testStep->assertMessage());
597 REPORTER_ASSERT_MESSAGE(reporter, canvas1->isDrawingToLayer() ==
598 canvas2->isDrawingToLayer(), testStep->assertMessage());
reed@google.com3b3e8952012-08-16 20:53:31 +0000599
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000600 SkRect bounds1, bounds2;
601 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.com3b3e8952012-08-16 20:53:31 +0000602 canvas1->getClipBounds(&bounds1) == canvas2->getClipBounds(&bounds2),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000603 testStep->assertMessage());
604 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
reed@google.com3b3e8952012-08-16 20:53:31 +0000605 testStep->assertMessage());
606
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000607 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
608 canvas2->getDrawFilter(), testStep->assertMessage());
609 SkIRect deviceBounds1, deviceBounds2;
610 REPORTER_ASSERT_MESSAGE(reporter,
611 canvas1->getClipDeviceBounds(&deviceBounds1) ==
612 canvas2->getClipDeviceBounds(&deviceBounds2),
613 testStep->assertMessage());
614 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2,
615 testStep->assertMessage());
616 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getBounder() ==
617 canvas2->getBounder(), testStep->assertMessage());
618 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
619 canvas2->getTotalMatrix(), testStep->assertMessage());
620 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getClipType() ==
621 canvas2->getClipType(), testStep->assertMessage());
622 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalClip() ==
623 canvas2->getTotalClip(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000624
625 // The following test code is commented out because the test fails when
rmistry@google.comd6176b02012-08-23 18:14:13 +0000626 // the canvas is an SkPictureRecord or SkDeferredCanvas
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000627 // Issue: http://code.google.com/p/skia/issues/detail?id=498
628 // Also, creating a LayerIter on an SkProxyCanvas crashes
629 // Issue: http://code.google.com/p/skia/issues/detail?id=499
630 /*
631 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
632 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
633 while (!layerIter1.done() && !layerIter2.done()) {
634 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
635 layerIter2.matrix(), testStep->assertMessage());
636 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
637 layerIter2.clip(), testStep->assertMessage());
638 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
639 layerIter2.paint(), testStep->assertMessage());
640 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
641 layerIter2.x(), testStep->assertMessage());
642 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
643 layerIter2.y(), testStep->assertMessage());
644 layerIter1.next();
645 layerIter2.next();
646 }
647 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
648 testStep->assertMessage());
649 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
650 testStep->assertMessage());
651 */
652}
653
654// The following class groups static functions that need to access
655// the privates members of SkPictureRecord
656class SkPictureTester {
657private:
reed@google.come2589ae2012-07-10 19:38:01 +0000658 static int EQ(const SkFlatData* a, const SkFlatData* b) {
659 return *a == *b;
660 }
661
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000662 static void AssertFlattenedObjectsEqual(
663 SkPictureRecord* referenceRecord,
664 SkPictureRecord* testRecord,
665 skiatest::Reporter* reporter,
666 CanvasTestStep* testStep) {
667
668 REPORTER_ASSERT_MESSAGE(reporter,
djsollen@google.comc9ab9872012-08-29 18:52:07 +0000669 referenceRecord->fBitmapHeap->count() ==
670 testRecord->fBitmapHeap->count(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000671 REPORTER_ASSERT_MESSAGE(reporter,
672 referenceRecord->fMatrices.count() ==
673 testRecord->fMatrices.count(), testStep->assertMessage());
674 for (int i = 0; i < referenceRecord->fMatrices.count(); ++i) {
675 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.come2589ae2012-07-10 19:38:01 +0000676 EQ(referenceRecord->fMatrices[i], testRecord->fMatrices[i]),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000677 testStep->assertMessage());
678 }
679 REPORTER_ASSERT_MESSAGE(reporter,
680 referenceRecord->fPaints.count() ==
681 testRecord->fPaints.count(), testStep->assertMessage());
682 for (int i = 0; i < referenceRecord->fPaints.count(); ++i) {
683 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.come2589ae2012-07-10 19:38:01 +0000684 EQ(referenceRecord->fPaints[i], testRecord->fPaints[i]),
685 testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000686 }
687 REPORTER_ASSERT_MESSAGE(reporter,
688 referenceRecord->fRegions.count() ==
689 testRecord->fRegions.count(), testStep->assertMessage());
690 for (int i = 0; i < referenceRecord->fRegions.count(); ++i) {
691 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.come2589ae2012-07-10 19:38:01 +0000692 EQ(referenceRecord->fRegions[i], testRecord->fRegions[i]),
693 testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000694 }
695 REPORTER_ASSERT_MESSAGE(reporter,
696 !referenceRecord->fPathHeap ==
697 !testRecord->fPathHeap,
698 testStep->assertMessage());
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000699 // The following tests are commented out because they currently
700 // fail. Issue: http://code.google.com/p/skia/issues/detail?id=507
701 /*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000702 if (referenceRecord->fPathHeap) {
703 REPORTER_ASSERT_MESSAGE(reporter,
704 referenceRecord->fPathHeap->count() ==
705 testRecord->fPathHeap->count(),
706 testStep->assertMessage());
707 for (int i = 0; i < referenceRecord->fPathHeap->count(); ++i) {
708 REPORTER_ASSERT_MESSAGE(reporter,
709 (*referenceRecord->fPathHeap)[i] ==
710 (*testRecord->fPathHeap)[i], testStep->assertMessage());
711 }
712 }
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000713 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000714
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000715 }
716
717public:
718
rmistry@google.comd6176b02012-08-23 18:14:13 +0000719 static void TestPictureFlattenedObjectReuse(skiatest::Reporter* reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000720 CanvasTestStep* testStep,
721 uint32_t recordFlags) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000722 // Verify that when a test step is executed twice, no extra resources
723 // are flattened during the second execution
724 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
725 SkPicture referencePicture;
726 SkCanvas* referenceCanvas = referencePicture.beginRecording(kWidth,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000727 kHeight, recordFlags);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000728 testStep->draw(referenceCanvas, reporter);
729 SkPicture testPicture;
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000730 SkCanvas* testCanvas = testPicture.beginRecording(kWidth,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000731 kHeight, recordFlags);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000732 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000733 testStep->setAssertMessageFormat(kPictureSecondDrawAssertMessageFormat);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000734 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000735
736 SkPictureRecord* referenceRecord = static_cast<SkPictureRecord*>(
737 referenceCanvas);
738 SkPictureRecord* testRecord = static_cast<SkPictureRecord*>(
739 testCanvas);
740 testStep->setAssertMessageFormat(kPictureResourceReuseMessageFormat);
741 AssertFlattenedObjectsEqual(referenceRecord, testRecord,
junov@chromium.org76b9c4b2012-02-22 21:24:41 +0000742 reporter, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000743 }
744};
745
edisonn@google.com77909122012-10-18 15:58:23 +0000746static void TestPdfDevice(skiatest::Reporter* reporter,
747 CanvasTestStep* testStep) {
748 SkISize pageSize = SkISize::Make(kWidth, kHeight);
749 SkPDFDevice device(pageSize, pageSize, SkMatrix::I());
750 SkCanvas canvas(&device);
751 testStep->setAssertMessageFormat(kPdfAssertMessageFormat);
752 testStep->draw(&canvas, reporter);
753 SkPDFDocument doc;
754 doc.appendPage(&device);
755 SkDynamicMemoryWStream stream;
756 doc.emitPDF(&stream);
757}
758
junov@chromium.org88e29142012-08-07 16:48:22 +0000759// The following class groups static functions that need to access
760// the privates members of SkDeferredCanvas
761class SkDeferredCanvasTester {
762public:
763 static void TestDeferredCanvasStateConsistency(
764 skiatest::Reporter* reporter,
765 CanvasTestStep* testStep,
junov@chromium.orgfb103892012-09-20 19:35:43 +0000766 const SkCanvas& referenceCanvas, bool silent) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000767
junov@chromium.org88e29142012-08-07 16:48:22 +0000768 SkBitmap deferredStore;
769 createBitmap(&deferredStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000770 SkBitmapDevice deferredDevice(deferredStore);
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +0000771 SkAutoTUnref<SkDeferredCanvas> deferredCanvas(SkDeferredCanvas::Create(&deferredDevice));
junov@chromium.org88e29142012-08-07 16:48:22 +0000772 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
junov@chromium.org66070a52013-05-28 17:39:08 +0000773 testStep->draw(deferredCanvas, reporter);
junov@chromium.org88e29142012-08-07 16:48:22 +0000774 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
junov@chromium.org66070a52013-05-28 17:39:08 +0000775 AssertCanvasStatesEqual(reporter, deferredCanvas, &referenceCanvas,
junov@chromium.org88e29142012-08-07 16:48:22 +0000776 testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000777
junov@chromium.orgfb103892012-09-20 19:35:43 +0000778 if (silent) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000779 deferredCanvas->silentFlush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000780 } else {
junov@chromium.org66070a52013-05-28 17:39:08 +0000781 deferredCanvas->flush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000782 }
783
skia.committer@gmail.com4c5ea442012-09-21 02:01:01 +0000784 testStep->setAssertMessageFormat(
junov@chromium.orgfb103892012-09-20 19:35:43 +0000785 silent ? kDeferredPostSilentFlushPlaybackAssertMessageFormat :
junov@chromium.org88e29142012-08-07 16:48:22 +0000786 kDeferredPostFlushPlaybackAssertMessageFormat);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000787 AssertCanvasStatesEqual(reporter,
junov@chromium.org66070a52013-05-28 17:39:08 +0000788 deferredCanvas->immediateCanvas(),
junov@chromium.org88e29142012-08-07 16:48:22 +0000789 &referenceCanvas, testStep);
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000790
junov@chromium.org88e29142012-08-07 16:48:22 +0000791 // Verified that deferred canvas state is not affected by flushing
792 // pending draw operations
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000793
junov@chromium.org88e29142012-08-07 16:48:22 +0000794 // The following test code is commented out because it currently fails.
795 // Issue: http://code.google.com/p/skia/issues/detail?id=496
796 /*
797 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
798 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
799 testStep);
800 */
801 }
802};
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000803
caryclark@google.com42639cd2012-06-06 12:03:39 +0000804// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000805static void TestProxyCanvasStateConsistency(
806 skiatest::Reporter* reporter,
807 CanvasTestStep* testStep,
808 const SkCanvas& referenceCanvas) {
809
810 SkBitmap indirectStore;
811 createBitmap(&indirectStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000812 SkBitmapDevice indirectDevice(indirectStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000813 SkCanvas indirectCanvas(&indirectDevice);
814 SkProxyCanvas proxyCanvas(&indirectCanvas);
815 testStep->setAssertMessageFormat(kProxyDrawAssertMessageFormat);
816 testStep->draw(&proxyCanvas, reporter);
817 // Verify that the SkProxyCanvas reports consitent state
818 testStep->setAssertMessageFormat(kProxyStateAssertMessageFormat);
819 AssertCanvasStatesEqual(reporter, &proxyCanvas, &referenceCanvas,
820 testStep);
821 // Verify that the indirect canvas reports consitent state
822 testStep->setAssertMessageFormat(kProxyIndirectStateAssertMessageFormat);
823 AssertCanvasStatesEqual(reporter, &indirectCanvas, &referenceCanvas,
824 testStep);
825}
826
caryclark@google.com42639cd2012-06-06 12:03:39 +0000827// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000828static void TestNWayCanvasStateConsistency(
829 skiatest::Reporter* reporter,
830 CanvasTestStep* testStep,
831 const SkCanvas& referenceCanvas) {
832
833 SkBitmap indirectStore1;
834 createBitmap(&indirectStore1, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000835 SkBitmapDevice indirectDevice1(indirectStore1);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000836 SkCanvas indirectCanvas1(&indirectDevice1);
837
838 SkBitmap indirectStore2;
839 createBitmap(&indirectStore2, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000840 SkBitmapDevice indirectDevice2(indirectStore2);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000841 SkCanvas indirectCanvas2(&indirectDevice2);
842
djsollen@google.comf0a062b2012-05-01 16:50:25 +0000843 SkISize canvasSize = referenceCanvas.getDeviceSize();
844 SkNWayCanvas nWayCanvas(canvasSize.width(), canvasSize.height());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000845 nWayCanvas.addCanvas(&indirectCanvas1);
846 nWayCanvas.addCanvas(&indirectCanvas2);
847
848 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
849 testStep->draw(&nWayCanvas, reporter);
850 // Verify that the SkProxyCanvas reports consitent state
851 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
852 AssertCanvasStatesEqual(reporter, &nWayCanvas, &referenceCanvas,
853 testStep);
854 // Verify that the indirect canvases report consitent state
855 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
856 AssertCanvasStatesEqual(reporter, &indirectCanvas1, &referenceCanvas,
857 testStep);
858 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
859 AssertCanvasStatesEqual(reporter, &indirectCanvas2, &referenceCanvas,
860 testStep);
861}
862
863/*
864 * This sub-test verifies that the test step passes when executed
865 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
866 * that the all canvas derivatives report the same state as an SkCanvas
867 * after having executed the test step.
868 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000869static void TestOverrideStateConsistency(skiatest::Reporter* reporter,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000870 CanvasTestStep* testStep) {
871 SkBitmap referenceStore;
872 createBitmap(&referenceStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000873 SkBitmapDevice referenceDevice(referenceStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000874 SkCanvas referenceCanvas(&referenceDevice);
875 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
876 testStep->draw(&referenceCanvas, reporter);
877
junov@chromium.orgfb103892012-09-20 19:35:43 +0000878 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, false);
879
880 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, true);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000881
caryclark@google.com42639cd2012-06-06 12:03:39 +0000882 // The following test code is disabled because SkProxyCanvas is
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000883 // missing a lot of virtual overrides on get* methods, which are used
884 // to verify canvas state.
885 // Issue: http://code.google.com/p/skia/issues/detail?id=500
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000886
caryclark@google.com42639cd2012-06-06 12:03:39 +0000887 if (false) { // avoid bit rot, suppress warning
888 TestProxyCanvasStateConsistency(reporter, testStep, referenceCanvas);
889 }
890
891 // The following test code is disabled because SkNWayCanvas does not
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000892 // report correct clipping and device bounds information
893 // Issue: http://code.google.com/p/skia/issues/detail?id=501
caryclark@google.com42639cd2012-06-06 12:03:39 +0000894
895 if (false) { // avoid bit rot, suppress warning
896 TestNWayCanvasStateConsistency(reporter, testStep, referenceCanvas);
897 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000898
caryclark@google.com42639cd2012-06-06 12:03:39 +0000899 if (false) { // avoid bit rot, suppress warning
900 test_clipVisitor(reporter, &referenceCanvas);
901 }
reed@google.com7c202932011-12-14 18:48:05 +0000902}
reed@google.com37f3ae02011-11-28 16:06:04 +0000903
904static void TestCanvas(skiatest::Reporter* reporter) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000905 // Init global here because bitmap pixels cannot be alocated during
906 // static initialization
907 kTestBitmap = testBitmap();
reed@google.com37f3ae02011-11-28 16:06:04 +0000908
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000909 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
910 TestOverrideStateConsistency(reporter, testStepArray()[testStep]);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000911 SkPictureTester::TestPictureFlattenedObjectReuse(reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000912 testStepArray()[testStep], 0);
edisonn@google.com77909122012-10-18 15:58:23 +0000913 if (testStepArray()[testStep]->enablePdfTesting()) {
914 TestPdfDevice(reporter, testStepArray()[testStep]);
915 }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000916 }
junov@chromium.orgcd62ecf2012-08-02 17:43:25 +0000917
918 // Explicitly call reset(), so we don't leak the pixels (since kTestBitmap is a global)
919 kTestBitmap.reset();
reed@google.com37f3ae02011-11-28 16:06:04 +0000920}
921
922#include "TestClassDef.h"
923DEFINE_TESTCLASS("Canvas", TestCanvasClass, TestCanvas)