blob: 5f3d79ceeb43bc069d1c981bccdf311f49f1d52f [file] [log] [blame]
reed@google.com37f3ae02011-11-28 16:06:04 +00001
2/*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +00003 * Copyright 2012 Google Inc.
reed@google.com37f3ae02011-11-28 16:06:04 +00004 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +00008
9/* Description:
10 * This test defines a series of elementatry test steps that perform
11 * a single or a small group of canvas API calls. Each test step is
12 * used in several test cases that verify that different types of SkCanvas
13 * flavors and derivatives pass it and yield consistent behavior. The
14 * test cases analyse results that are queryable through the API. They do
15 * not look at rendering results.
16 *
17 * Adding test stepss:
18 * The general pattern for creating a new test step is to write a test
19 * function of the form:
20 *
rmistry@google.comd6176b02012-08-23 18:14:13 +000021 * static void MyTestStepFunction(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000022 * skiatest::Reporter* reporter,
23 * CanvasTestStep* testStep)
24 * {
25 * canvas->someCanvasAPImethod();
26 * (...)
27 * REPORTER_ASSERT_MESSAGE(reporter, (...), \
28 * testStep->assertMessage());
29 * }
30 *
31 * The definition of the test step function should be followed by an
32 * invocation of the TEST_STEP macro, which generates a class and
33 * instance for the test step:
34 *
35 * TEST_STEP(MyTestStep, MyTestStepFunction)
36 *
37 * There are also short hand macros for defining simple test steps
38 * in a single line of code. A simple test step is a one that is made
39 * of a single canvas API call.
40 *
41 * SIMPLE_TEST_STEP(MytestStep, someCanvasAPIMethod());
42 *
43 * There is another macro called SIMPLE_TEST_STEP_WITH_ASSERT that
44 * works the same way as SIMPLE_TEST_STEP, and additionally verifies
45 * that the invoked method returns a non-zero value.
46 */
reed@google.com37f3ae02011-11-28 16:06:04 +000047#include "SkBitmap.h"
48#include "SkCanvas.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000049#include "SkDeferredCanvas.h"
50#include "SkDevice.h"
51#include "SkMatrix.h"
52#include "SkNWayCanvas.h"
edisonn@google.com77909122012-10-18 15:58:23 +000053#include "SkPDFDevice.h"
54#include "SkPDFDocument.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000055#include "SkPaint.h"
56#include "SkPath.h"
57#include "SkPicture.h"
58#include "SkPictureRecord.h"
59#include "SkProxyCanvas.h"
60#include "SkRect.h"
61#include "SkRegion.h"
62#include "SkShader.h"
63#include "SkStream.h"
reed@google.com28183b42014-02-04 15:34:10 +000064#include "SkSurface.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000065#include "SkTDArray.h"
66#include "Test.h"
reed@google.com37f3ae02011-11-28 16:06:04 +000067
reed@google.com90c07ea2012-04-13 13:50:27 +000068class Canvas2CanvasClipVisitor : public SkCanvas::ClipVisitor {
69public:
70 Canvas2CanvasClipVisitor(SkCanvas* target) : fTarget(target) {}
71
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000072 virtual void clipRect(const SkRect& r, SkRegion::Op op, bool aa) SK_OVERRIDE {
reed@google.com90c07ea2012-04-13 13:50:27 +000073 fTarget->clipRect(r, op, aa);
74 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000075 virtual void clipRRect(const SkRRect& r, SkRegion::Op op, bool aa) SK_OVERRIDE {
76 fTarget->clipRRect(r, op, aa);
77 }
78 virtual void clipPath(const SkPath& p, SkRegion::Op op, bool aa) SK_OVERRIDE {
reed@google.com90c07ea2012-04-13 13:50:27 +000079 fTarget->clipPath(p, op, aa);
80 }
81
82private:
83 SkCanvas* fTarget;
84};
85
86static void test_clipVisitor(skiatest::Reporter* reporter, SkCanvas* canvas) {
87 SkISize size = canvas->getDeviceSize();
rmistry@google.comd6176b02012-08-23 18:14:13 +000088
reed@google.com90c07ea2012-04-13 13:50:27 +000089 SkBitmap bm;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000090 bm.setConfig(SkImageInfo::MakeN32Premul(size.width(), size.height()));
reed@google.com90c07ea2012-04-13 13:50:27 +000091 SkCanvas c(bm);
92
93 Canvas2CanvasClipVisitor visitor(&c);
94 canvas->replayClips(&visitor);
95
96 REPORTER_ASSERT(reporter, c.getTotalClip() == canvas->getTotalClip());
97}
98
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000099static const int kWidth = 2;
100static const int kHeight = 2;
reed@google.com7c202932011-12-14 18:48:05 +0000101
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000102// Format strings that describe the test context. The %s token is where
103// the name of the test step is inserted. The context is required for
104// disambiguating the error in the case of failures that are reported in
105// functions that are called multiple times in different contexts (test
106// cases and test steps).
107static const char* const kDefaultAssertMessageFormat = "%s";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000108static const char* const kCanvasDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000109 "Drawing test step %s with SkCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000110static const char* const kPictureDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000111 "Drawing test step %s with SkPicture";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000112static const char* const kPictureSecondDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000113 "Duplicate draw of test step %s with 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 kDeferredPreFlushAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000121 "test step %s, SkDeferredCanvas state consistency before flush";
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000122static const char* const kDeferredPostFlushPlaybackAssertMessageFormat =
123 "test step %s, SkDeferredCanvas playback canvas state consistency after flush";
junov@chromium.orgfb103892012-09-20 19:35:43 +0000124static const char* const kDeferredPostSilentFlushPlaybackAssertMessageFormat =
125 "test step %s, SkDeferredCanvas playback canvas state consistency after silent flush";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000126static const char* const kPictureResourceReuseMessageFormat =
127 "test step %s, SkPicture duplicate flattened object test";
128static const char* const kProxyStateAssertMessageFormat =
129 "test step %s, SkProxyCanvas state consistency";
130static const char* const kProxyIndirectStateAssertMessageFormat =
131 "test step %s, SkProxyCanvas indirect canvas state consistency";
132static const char* const kNWayStateAssertMessageFormat =
133 "test step %s, SkNWayCanvas state consistency";
134static const char* const kNWayIndirect1StateAssertMessageFormat =
135 "test step %s, SkNWayCanvas indirect canvas 1 state consistency";
136static const char* const kNWayIndirect2StateAssertMessageFormat =
137 "test step %s, SkNWayCanvas indirect canvas 2 state consistency";
edisonn@google.com77909122012-10-18 15:58:23 +0000138static const char* const kPdfAssertMessageFormat =
139 "PDF sanity check failed %s";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000140
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000141static void createBitmap(SkBitmap* bm, SkColor color) {
142 bm->allocN32Pixels(kWidth, kHeight);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000143 bm->eraseColor(color);
144}
145
reed@google.com28183b42014-02-04 15:34:10 +0000146static SkSurface* createSurface(SkColor color) {
147 SkSurface* surface = SkSurface::NewRasterPMColor(kWidth, kHeight);
148 surface->getCanvas()->clear(color);
149 return surface;
150}
151
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000152class CanvasTestStep;
153static SkTDArray<CanvasTestStep*>& testStepArray() {
154 static SkTDArray<CanvasTestStep*> theTests;
155 return theTests;
156}
157
158class CanvasTestStep {
159public:
edisonn@google.com77909122012-10-18 15:58:23 +0000160 CanvasTestStep(bool fEnablePdfTesting = true) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000161 *testStepArray().append() = this;
162 fAssertMessageFormat = kDefaultAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000163 this->fEnablePdfTesting = fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000164 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000165 virtual ~CanvasTestStep() { }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000166
167 virtual void draw(SkCanvas*, skiatest::Reporter*) = 0;
168 virtual const char* name() const = 0;
169
170 const char* assertMessage() {
171 fAssertMessage.printf(fAssertMessageFormat, name());
172 return fAssertMessage.c_str();
173 }
174
175 void setAssertMessageFormat(const char* format) {
176 fAssertMessageFormat = format;
177 }
178
edisonn@google.com77909122012-10-18 15:58:23 +0000179 bool enablePdfTesting() { return fEnablePdfTesting; }
180
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000181private:
182 SkString fAssertMessage;
183 const char* fAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000184 bool fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000185};
186
187///////////////////////////////////////////////////////////////////////////////
188// Constants used by test steps
189
rmistry@google.comd6176b02012-08-23 18:14:13 +0000190const SkRect kTestRect =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000191 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
192 SkIntToScalar(2), SkIntToScalar(1));
193static SkMatrix testMatrix() {
194 SkMatrix matrix;
195 matrix.reset();
196 matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
197 return matrix;
198}
199const SkMatrix kTestMatrix = testMatrix();
200static SkPath testPath() {
201 SkPath path;
202 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
203 SkIntToScalar(2), SkIntToScalar(1)));
204 return path;
205}
206const SkPath kTestPath = testPath();
207static SkRegion testRegion() {
208 SkRegion region;
209 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
210 region.setRect(rect);
211 return region;
212}
213const SkIRect kTestIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
214const SkRegion kTestRegion = testRegion();
215const SkColor kTestColor = 0x01020304;
216const SkPaint kTestPaint;
217const SkPoint kTestPoints[3] = {
218 {SkIntToScalar(0), SkIntToScalar(0)},
219 {SkIntToScalar(2), SkIntToScalar(1)},
220 {SkIntToScalar(0), SkIntToScalar(2)}
221};
222const size_t kTestPointCount = 3;
223static SkBitmap testBitmap() {
224 SkBitmap bitmap;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000225 createBitmap(&bitmap, 0x05060708);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000226 return bitmap;
227}
228SkBitmap kTestBitmap; // cannot be created during static init
229SkString kTestText("Hello World");
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000230SkPoint kTestPoints2[] = {
231 { SkIntToScalar(0), SkIntToScalar(1) },
232 { SkIntToScalar(1), SkIntToScalar(1) },
233 { SkIntToScalar(2), SkIntToScalar(1) },
234 { SkIntToScalar(3), SkIntToScalar(1) },
235 { SkIntToScalar(4), SkIntToScalar(1) },
236 { SkIntToScalar(5), SkIntToScalar(1) },
237 { SkIntToScalar(6), SkIntToScalar(1) },
238 { SkIntToScalar(7), SkIntToScalar(1) },
239 { SkIntToScalar(8), SkIntToScalar(1) },
240 { SkIntToScalar(9), SkIntToScalar(1) },
241 { SkIntToScalar(10), SkIntToScalar(1) },
242};
rmistry@google.comd6176b02012-08-23 18:14:13 +0000243
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000244
245///////////////////////////////////////////////////////////////////////////////
246// Macros for defining test steps
247
248#define TEST_STEP(NAME, FUNCTION) \
249class NAME##_TestStep : public CanvasTestStep{ \
250public: \
251 virtual void draw(SkCanvas* canvas, skiatest::Reporter* reporter) { \
252 FUNCTION (canvas, reporter, this); \
253 } \
254 virtual const char* name() const {return #NAME ;} \
255}; \
256static NAME##_TestStep NAME##_TestStepInstance;
257
edisonn@google.com77909122012-10-18 15:58:23 +0000258#define TEST_STEP_NO_PDF(NAME, FUNCTION) \
259class NAME##_TestStep : public CanvasTestStep{ \
260public: \
261 NAME##_TestStep() : CanvasTestStep(false) {} \
262 virtual void draw(SkCanvas* canvas, skiatest::Reporter* reporter) { \
263 FUNCTION (canvas, reporter, this); \
264 } \
265 virtual const char* name() const {return #NAME ;} \
266}; \
267static NAME##_TestStep NAME##_TestStepInstance;
268
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000269#define SIMPLE_TEST_STEP(NAME, CALL) \
270static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter*, \
271 CanvasTestStep*) { \
272 canvas-> CALL ; \
273} \
274TEST_STEP(NAME, NAME##TestStep )
275
276#define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
277static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter* reporter, \
278 CanvasTestStep* testStep) { \
279 REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \
280 testStep->assertMessage()); \
281} \
282TEST_STEP(NAME, NAME##TestStep )
283
284
285///////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000286// Basic test steps for most virtual methods in SkCanvas that draw or affect
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000287// the state of the canvas.
288
junov@chromium.orga907ac32012-02-24 21:54:07 +0000289SIMPLE_TEST_STEP_WITH_ASSERT(Translate,
290 translate(SkIntToScalar(1), SkIntToScalar(2)));
291SIMPLE_TEST_STEP_WITH_ASSERT(Scale,
292 scale(SkIntToScalar(1), SkIntToScalar(2)));
293SIMPLE_TEST_STEP_WITH_ASSERT(Rotate, rotate(SkIntToScalar(1)));
294SIMPLE_TEST_STEP_WITH_ASSERT(Skew,
295 skew(SkIntToScalar(1), SkIntToScalar(2)));
296SIMPLE_TEST_STEP_WITH_ASSERT(Concat, concat(kTestMatrix));
297SIMPLE_TEST_STEP(SetMatrix, setMatrix(kTestMatrix));
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000298SIMPLE_TEST_STEP(ClipRect, clipRect(kTestRect));
299SIMPLE_TEST_STEP(ClipPath, clipPath(kTestPath));
300SIMPLE_TEST_STEP(ClipRegion,
junov@chromium.orga907ac32012-02-24 21:54:07 +0000301 clipRegion(kTestRegion, SkRegion::kReplace_Op));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000302SIMPLE_TEST_STEP(Clear, clear(kTestColor));
303SIMPLE_TEST_STEP(DrawPaint, drawPaint(kTestPaint));
304SIMPLE_TEST_STEP(DrawPointsPoints, drawPoints(SkCanvas::kPoints_PointMode,
305 kTestPointCount, kTestPoints, kTestPaint));
306SIMPLE_TEST_STEP(DrawPointsLiness, drawPoints(SkCanvas::kLines_PointMode,
307 kTestPointCount, kTestPoints, kTestPaint));
308SIMPLE_TEST_STEP(DrawPointsPolygon, drawPoints(SkCanvas::kPolygon_PointMode,
309 kTestPointCount, kTestPoints, kTestPaint));
310SIMPLE_TEST_STEP(DrawRect, drawRect(kTestRect, kTestPaint));
311SIMPLE_TEST_STEP(DrawPath, drawPath(kTestPath, kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000312SIMPLE_TEST_STEP(DrawBitmap, drawBitmap(kTestBitmap, 0, 0));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000313SIMPLE_TEST_STEP(DrawBitmapPaint, drawBitmap(kTestBitmap, 0, 0, &kTestPaint));
314SIMPLE_TEST_STEP(DrawBitmapRect, drawBitmapRect(kTestBitmap, NULL, kTestRect,
315 NULL));
316SIMPLE_TEST_STEP(DrawBitmapRectSrcRect, drawBitmapRect(kTestBitmap,
317 &kTestIRect, kTestRect, NULL));
318SIMPLE_TEST_STEP(DrawBitmapRectPaint, drawBitmapRect(kTestBitmap, NULL,
319 kTestRect, &kTestPaint));
320SIMPLE_TEST_STEP(DrawBitmapMatrix, drawBitmapMatrix(kTestBitmap, kTestMatrix,
321 NULL));
322SIMPLE_TEST_STEP(DrawBitmapMatrixPaint, drawBitmapMatrix(kTestBitmap,
323 kTestMatrix, &kTestPaint));
324SIMPLE_TEST_STEP(DrawBitmapNine, drawBitmapNine(kTestBitmap, kTestIRect,
325 kTestRect, NULL));
326SIMPLE_TEST_STEP(DrawBitmapNinePaint, drawBitmapNine(kTestBitmap, kTestIRect,
327 kTestRect, &kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000328SIMPLE_TEST_STEP(DrawSprite, drawSprite(kTestBitmap, 0, 0, NULL));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000329SIMPLE_TEST_STEP(DrawSpritePaint, drawSprite(kTestBitmap, 0, 0, &kTestPaint));
330SIMPLE_TEST_STEP(DrawText, drawText(kTestText.c_str(), kTestText.size(),
331 0, 1, kTestPaint));
332SIMPLE_TEST_STEP(DrawPosText, drawPosText(kTestText.c_str(),
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000333 kTestText.size(), kTestPoints2, kTestPaint));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000334SIMPLE_TEST_STEP(DrawTextOnPath, drawTextOnPath(kTestText.c_str(),
335 kTestText.size(), kTestPath, NULL, kTestPaint));
336SIMPLE_TEST_STEP(DrawTextOnPathMatrix, drawTextOnPath(kTestText.c_str(),
337 kTestText.size(), kTestPath, &kTestMatrix, kTestPaint));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000338SIMPLE_TEST_STEP(DrawData, drawData(kTestText.c_str(), kTestText.size()));
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000339SIMPLE_TEST_STEP(BeginGroup, beginCommentGroup(kTestText.c_str()));
340SIMPLE_TEST_STEP(AddComment, addComment(kTestText.c_str(), kTestText.c_str()));
341SIMPLE_TEST_STEP(EndGroup, endCommentGroup());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000342
343///////////////////////////////////////////////////////////////////////////////
344// Complex test steps
345
rmistry@google.comd6176b02012-08-23 18:14:13 +0000346// Save/restore calls cannot be in isolated simple test steps because the test
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000347// cases that use SkPicture require that save and restore calls be balanced.
rmistry@google.comd6176b02012-08-23 18:14:13 +0000348static void SaveMatrixStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000349 skiatest::Reporter* reporter,
350 CanvasTestStep* testStep) {
351 int saveCount = canvas->getSaveCount();
352 canvas->save(SkCanvas::kMatrix_SaveFlag);
353 canvas->clipRegion(kTestRegion);
354 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
355 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000356 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000357 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000358 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000359 testStep->assertMessage());
360 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() == kTestRegion,
361 testStep->assertMessage());
362}
363TEST_STEP(SaveMatrix, SaveMatrixStep);
364
rmistry@google.comd6176b02012-08-23 18:14:13 +0000365static void SaveClipStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000366 skiatest::Reporter* reporter,
367 CanvasTestStep* testStep) {
368 int saveCount = canvas->getSaveCount();
369 canvas->save(SkCanvas::kClip_SaveFlag);
370 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
371 canvas->clipRegion(kTestRegion);
372 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000373 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000374 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000375 REPORTER_ASSERT_MESSAGE(reporter, !canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000376 testStep->assertMessage());
377 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion,
378 testStep->assertMessage());
379}
380TEST_STEP(SaveClip, SaveClipStep);
381
rmistry@google.comd6176b02012-08-23 18:14:13 +0000382static void SaveMatrixClipStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000383 skiatest::Reporter* reporter,
384 CanvasTestStep* testStep) {
385 int saveCount = canvas->getSaveCount();
386 canvas->save(SkCanvas::kMatrixClip_SaveFlag);
387 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
388 canvas->clipRegion(kTestRegion);
389 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000390 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000391 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000392 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000393 testStep->assertMessage());
394 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion,
395 testStep->assertMessage());
396}
397TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
398
rmistry@google.comd6176b02012-08-23 18:14:13 +0000399static void SaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000400 skiatest::Reporter* reporter,
401 CanvasTestStep* testStep) {
402 int saveCount = canvas->getSaveCount();
403 canvas->saveLayer(NULL, NULL);
404 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000405 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000406 testStep->assertMessage());
407}
408TEST_STEP(SaveLayer, SaveLayerStep);
409
rmistry@google.comd6176b02012-08-23 18:14:13 +0000410static void BoundedSaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000411 skiatest::Reporter* reporter,
412 CanvasTestStep* testStep) {
413 int saveCount = canvas->getSaveCount();
414 canvas->saveLayer(&kTestRect, NULL);
415 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000416 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000417 testStep->assertMessage());
418}
419TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
420
rmistry@google.comd6176b02012-08-23 18:14:13 +0000421static void PaintSaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000422 skiatest::Reporter* reporter,
423 CanvasTestStep* testStep) {
424 int saveCount = canvas->getSaveCount();
425 canvas->saveLayer(NULL, &kTestPaint);
426 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000427 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000428 testStep->assertMessage());
429}
430TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
431
rmistry@google.comd6176b02012-08-23 18:14:13 +0000432static void TwoClipOpsStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000433 skiatest::Reporter*,
434 CanvasTestStep*) {
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000435 // This test exercises a functionality in SkPicture that leads to the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000436 // recording of restore offset placeholders. This test will trigger an
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000437 // assertion at playback time if the placeholders are not properly
438 // filled when the recording ends.
439 canvas->clipRect(kTestRect);
440 canvas->clipRegion(kTestRegion);
441}
442TEST_STEP(TwoClipOps, TwoClipOpsStep);
443
epoger@google.com94fa43c2012-04-11 17:51:01 +0000444// exercise fix for http://code.google.com/p/skia/issues/detail?id=560
445// ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
rmistry@google.comd6176b02012-08-23 18:14:13 +0000446static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000447 skiatest::Reporter*,
448 CanvasTestStep*) {
epoger@google.com94fa43c2012-04-11 17:51:01 +0000449 SkPaint paint;
450 paint.setStrokeWidth(SkIntToScalar(1));
451 paint.setStyle(SkPaint::kStroke_Style);
452
453 SkPath path;
454 SkPoint pt1 = { 0, 0 };
455 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
456 SkPoint pt3 = { SkIntToScalar(1), 0 };
457 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
458 path.moveTo(pt1);
459 path.lineTo(pt2);
460 path.lineTo(pt3);
461 path.lineTo(pt4);
462
463 canvas->drawPath(path, paint);
464}
465TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
466
rmistry@google.comd6176b02012-08-23 18:14:13 +0000467static void DrawVerticesShaderTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000468 skiatest::Reporter*,
469 CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000470 SkPoint pts[4];
471 pts[0].set(0, 0);
472 pts[1].set(SkIntToScalar(kWidth), 0);
473 pts[2].set(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
474 pts[3].set(0, SkIntToScalar(kHeight));
475 SkPaint paint;
476 SkShader* shader = SkShader::CreateBitmapShader(kTestBitmap,
477 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
478 paint.setShader(shader)->unref();
479 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
480 NULL, NULL, NULL, 0, paint);
481}
edisonn@google.com77909122012-10-18 15:58:23 +0000482// NYI: issue 240.
483TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000484
rmistry@google.comd6176b02012-08-23 18:14:13 +0000485static void DrawPictureTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000486 skiatest::Reporter*,
487 CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000488 SkPicture* testPicture = SkNEW_ARGS(SkPicture, ());
489 SkAutoUnref aup(testPicture);
490 SkCanvas* testCanvas = testPicture->beginRecording(kWidth, kHeight);
491 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
492 testCanvas->clipRect(kTestRect);
493 testCanvas->drawRect(kTestRect, kTestPaint);
494 canvas->drawPicture(*testPicture);
495}
496TEST_STEP(DrawPicture, DrawPictureTestStep);
497
rmistry@google.comd6176b02012-08-23 18:14:13 +0000498static void SaveRestoreTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000499 skiatest::Reporter* reporter,
500 CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000501 int baseSaveCount = canvas->getSaveCount();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000502 int n = canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000503 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount == n, testStep->assertMessage());
504 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000505 testStep->assertMessage());
506 canvas->save();
507 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000508 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000509 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000510 canvas->restoreToCount(baseSaveCount + 1);
511 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000512 testStep->assertMessage());
513
514 // should this pin to 1, or be a no-op, or crash?
515 canvas->restoreToCount(0);
516 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
517 testStep->assertMessage());
518}
519TEST_STEP(SaveRestore, SaveRestoreTestStep);
520
rmistry@google.comd6176b02012-08-23 18:14:13 +0000521static void DrawLayerTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000522 skiatest::Reporter* reporter,
523 CanvasTestStep* testStep) {
524 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
525 testStep->assertMessage());
526 canvas->save();
527 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
528 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000529 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000530
reed@google.com7c202932011-12-14 18:48:05 +0000531 const SkRect* bounds = NULL; // null means include entire bounds
532 const SkPaint* paint = NULL;
533
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000534 canvas->saveLayer(bounds, paint);
535 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
536 testStep->assertMessage());
537 canvas->restore();
538 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
539 testStep->assertMessage());
reed@google.com7c202932011-12-14 18:48:05 +0000540
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000541 canvas->saveLayer(bounds, paint);
542 canvas->saveLayer(bounds, paint);
543 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
544 testStep->assertMessage());
545 canvas->restore();
546 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
547 testStep->assertMessage());
548 canvas->restore();
reed@google.com7c202932011-12-14 18:48:05 +0000549 // now layer count should be 0
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000550 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
551 testStep->assertMessage());
552}
553TEST_STEP(DrawLayer, DrawLayerTestStep);
reed@google.com3b3e8952012-08-16 20:53:31 +0000554
555static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000556 skiatest::Reporter*,
557 CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000558 // This test step challenges the TestDeferredCanvasStateConsistency
559 // test cases because the opaque paint can trigger an optimization
560 // that discards previously recorded commands. The challenge is to maintain
561 // correct clip and matrix stack state.
562 canvas->resetMatrix();
563 canvas->rotate(SkIntToScalar(30));
564 canvas->save();
565 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
566 canvas->save();
567 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
568 SkPaint paint;
569 paint.setColor(0xFFFFFFFF);
570 canvas->drawPaint(paint);
571 canvas->restore();
572 canvas->restore();
573}
574TEST_STEP(NestedSaveRestoreWithSolidPaint, \
575 NestedSaveRestoreWithSolidPaintTestStep);
576
577static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000578 skiatest::Reporter*,
579 CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000580 // This test step challenges the TestDeferredCanvasStateConsistency
581 // test case because the canvas flush on a deferred canvas will
582 // reset the recording session. The challenge is to maintain correct
583 // clip and matrix stack state on the playback canvas.
584 canvas->resetMatrix();
585 canvas->rotate(SkIntToScalar(30));
586 canvas->save();
587 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
588 canvas->save();
589 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
590 canvas->drawRect(kTestRect,kTestPaint);
591 canvas->flush();
592 canvas->restore();
593 canvas->restore();
594}
595TEST_STEP(NestedSaveRestoreWithFlush, \
596 NestedSaveRestoreWithFlushTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000597
598static void AssertCanvasStatesEqual(skiatest::Reporter* reporter,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000599 const SkCanvas* canvas1,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000600 const SkCanvas* canvas2,
601 CanvasTestStep* testStep) {
602 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
603 canvas2->getDeviceSize(), testStep->assertMessage());
604 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
605 canvas2->getSaveCount(), testStep->assertMessage());
606 REPORTER_ASSERT_MESSAGE(reporter, canvas1->isDrawingToLayer() ==
607 canvas2->isDrawingToLayer(), testStep->assertMessage());
reed@google.com3b3e8952012-08-16 20:53:31 +0000608
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000609 SkRect bounds1, bounds2;
610 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.com3b3e8952012-08-16 20:53:31 +0000611 canvas1->getClipBounds(&bounds1) == canvas2->getClipBounds(&bounds2),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000612 testStep->assertMessage());
613 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
reed@google.com3b3e8952012-08-16 20:53:31 +0000614 testStep->assertMessage());
615
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000616 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
617 canvas2->getDrawFilter(), testStep->assertMessage());
618 SkIRect deviceBounds1, deviceBounds2;
619 REPORTER_ASSERT_MESSAGE(reporter,
620 canvas1->getClipDeviceBounds(&deviceBounds1) ==
621 canvas2->getClipDeviceBounds(&deviceBounds2),
622 testStep->assertMessage());
623 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2,
624 testStep->assertMessage());
625 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getBounder() ==
626 canvas2->getBounder(), testStep->assertMessage());
627 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
628 canvas2->getTotalMatrix(), testStep->assertMessage());
629 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getClipType() ==
630 canvas2->getClipType(), testStep->assertMessage());
631 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalClip() ==
632 canvas2->getTotalClip(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000633
634 // The following test code is commented out because the test fails when
rmistry@google.comd6176b02012-08-23 18:14:13 +0000635 // the canvas is an SkPictureRecord or SkDeferredCanvas
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000636 // Issue: http://code.google.com/p/skia/issues/detail?id=498
637 // Also, creating a LayerIter on an SkProxyCanvas crashes
638 // Issue: http://code.google.com/p/skia/issues/detail?id=499
639 /*
640 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
641 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
642 while (!layerIter1.done() && !layerIter2.done()) {
643 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
644 layerIter2.matrix(), testStep->assertMessage());
645 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
646 layerIter2.clip(), testStep->assertMessage());
647 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
648 layerIter2.paint(), testStep->assertMessage());
649 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
650 layerIter2.x(), testStep->assertMessage());
651 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
652 layerIter2.y(), testStep->assertMessage());
653 layerIter1.next();
654 layerIter2.next();
655 }
656 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
657 testStep->assertMessage());
658 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
659 testStep->assertMessage());
660 */
661}
662
663// The following class groups static functions that need to access
664// the privates members of SkPictureRecord
665class SkPictureTester {
666private:
reed@google.come2589ae2012-07-10 19:38:01 +0000667 static int EQ(const SkFlatData* a, const SkFlatData* b) {
668 return *a == *b;
669 }
670
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000671 static void AssertFlattenedObjectsEqual(
672 SkPictureRecord* referenceRecord,
673 SkPictureRecord* testRecord,
674 skiatest::Reporter* reporter,
675 CanvasTestStep* testStep) {
676
677 REPORTER_ASSERT_MESSAGE(reporter,
djsollen@google.comc9ab9872012-08-29 18:52:07 +0000678 referenceRecord->fBitmapHeap->count() ==
679 testRecord->fBitmapHeap->count(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000680 REPORTER_ASSERT_MESSAGE(reporter,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000681 referenceRecord->fPaints.count() ==
682 testRecord->fPaints.count(), testStep->assertMessage());
683 for (int i = 0; i < referenceRecord->fPaints.count(); ++i) {
684 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.come2589ae2012-07-10 19:38:01 +0000685 EQ(referenceRecord->fPaints[i], testRecord->fPaints[i]),
686 testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000687 }
688 REPORTER_ASSERT_MESSAGE(reporter,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000689 !referenceRecord->fPathHeap ==
690 !testRecord->fPathHeap,
691 testStep->assertMessage());
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000692 // The following tests are commented out because they currently
693 // fail. Issue: http://code.google.com/p/skia/issues/detail?id=507
694 /*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000695 if (referenceRecord->fPathHeap) {
696 REPORTER_ASSERT_MESSAGE(reporter,
697 referenceRecord->fPathHeap->count() ==
698 testRecord->fPathHeap->count(),
699 testStep->assertMessage());
700 for (int i = 0; i < referenceRecord->fPathHeap->count(); ++i) {
701 REPORTER_ASSERT_MESSAGE(reporter,
702 (*referenceRecord->fPathHeap)[i] ==
703 (*testRecord->fPathHeap)[i], testStep->assertMessage());
704 }
705 }
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000706 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000707
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000708 }
709
710public:
711
rmistry@google.comd6176b02012-08-23 18:14:13 +0000712 static void TestPictureFlattenedObjectReuse(skiatest::Reporter* reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000713 CanvasTestStep* testStep,
714 uint32_t recordFlags) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000715 // Verify that when a test step is executed twice, no extra resources
716 // are flattened during the second execution
717 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
718 SkPicture referencePicture;
719 SkCanvas* referenceCanvas = referencePicture.beginRecording(kWidth,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000720 kHeight, recordFlags);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000721 testStep->draw(referenceCanvas, reporter);
722 SkPicture testPicture;
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000723 SkCanvas* testCanvas = testPicture.beginRecording(kWidth,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000724 kHeight, recordFlags);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000725 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000726 testStep->setAssertMessageFormat(kPictureSecondDrawAssertMessageFormat);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000727 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000728
729 SkPictureRecord* referenceRecord = static_cast<SkPictureRecord*>(
730 referenceCanvas);
731 SkPictureRecord* testRecord = static_cast<SkPictureRecord*>(
732 testCanvas);
733 testStep->setAssertMessageFormat(kPictureResourceReuseMessageFormat);
734 AssertFlattenedObjectsEqual(referenceRecord, testRecord,
junov@chromium.org76b9c4b2012-02-22 21:24:41 +0000735 reporter, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000736 }
737};
738
edisonn@google.com77909122012-10-18 15:58:23 +0000739static void TestPdfDevice(skiatest::Reporter* reporter,
740 CanvasTestStep* testStep) {
741 SkISize pageSize = SkISize::Make(kWidth, kHeight);
742 SkPDFDevice device(pageSize, pageSize, SkMatrix::I());
743 SkCanvas canvas(&device);
744 testStep->setAssertMessageFormat(kPdfAssertMessageFormat);
745 testStep->draw(&canvas, reporter);
746 SkPDFDocument doc;
747 doc.appendPage(&device);
748 SkDynamicMemoryWStream stream;
749 doc.emitPDF(&stream);
750}
751
junov@chromium.org88e29142012-08-07 16:48:22 +0000752// The following class groups static functions that need to access
753// the privates members of SkDeferredCanvas
754class SkDeferredCanvasTester {
755public:
756 static void TestDeferredCanvasStateConsistency(
757 skiatest::Reporter* reporter,
758 CanvasTestStep* testStep,
junov@chromium.orgfb103892012-09-20 19:35:43 +0000759 const SkCanvas& referenceCanvas, bool silent) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000760
reed@google.com28183b42014-02-04 15:34:10 +0000761 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
762 SkAutoTUnref<SkDeferredCanvas> deferredCanvas(SkDeferredCanvas::Create(surface.get()));
763
junov@chromium.org88e29142012-08-07 16:48:22 +0000764 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
junov@chromium.org66070a52013-05-28 17:39:08 +0000765 testStep->draw(deferredCanvas, reporter);
junov@chromium.org88e29142012-08-07 16:48:22 +0000766 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
junov@chromium.org66070a52013-05-28 17:39:08 +0000767 AssertCanvasStatesEqual(reporter, deferredCanvas, &referenceCanvas,
junov@chromium.org88e29142012-08-07 16:48:22 +0000768 testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000769
junov@chromium.orgfb103892012-09-20 19:35:43 +0000770 if (silent) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000771 deferredCanvas->silentFlush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000772 } else {
junov@chromium.org66070a52013-05-28 17:39:08 +0000773 deferredCanvas->flush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000774 }
775
skia.committer@gmail.com4c5ea442012-09-21 02:01:01 +0000776 testStep->setAssertMessageFormat(
junov@chromium.orgfb103892012-09-20 19:35:43 +0000777 silent ? kDeferredPostSilentFlushPlaybackAssertMessageFormat :
junov@chromium.org88e29142012-08-07 16:48:22 +0000778 kDeferredPostFlushPlaybackAssertMessageFormat);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000779 AssertCanvasStatesEqual(reporter,
junov@chromium.org66070a52013-05-28 17:39:08 +0000780 deferredCanvas->immediateCanvas(),
junov@chromium.org88e29142012-08-07 16:48:22 +0000781 &referenceCanvas, testStep);
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000782
junov@chromium.org88e29142012-08-07 16:48:22 +0000783 // Verified that deferred canvas state is not affected by flushing
784 // pending draw operations
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000785
junov@chromium.org88e29142012-08-07 16:48:22 +0000786 // The following test code is commented out because it currently fails.
787 // Issue: http://code.google.com/p/skia/issues/detail?id=496
788 /*
789 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
790 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
791 testStep);
792 */
793 }
794};
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000795
caryclark@google.com42639cd2012-06-06 12:03:39 +0000796// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000797static void TestProxyCanvasStateConsistency(
798 skiatest::Reporter* reporter,
799 CanvasTestStep* testStep,
800 const SkCanvas& referenceCanvas) {
801
802 SkBitmap indirectStore;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000803 createBitmap(&indirectStore, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000804 SkBitmapDevice indirectDevice(indirectStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000805 SkCanvas indirectCanvas(&indirectDevice);
806 SkProxyCanvas proxyCanvas(&indirectCanvas);
807 testStep->setAssertMessageFormat(kProxyDrawAssertMessageFormat);
808 testStep->draw(&proxyCanvas, reporter);
809 // Verify that the SkProxyCanvas reports consitent state
810 testStep->setAssertMessageFormat(kProxyStateAssertMessageFormat);
811 AssertCanvasStatesEqual(reporter, &proxyCanvas, &referenceCanvas,
812 testStep);
813 // Verify that the indirect canvas reports consitent state
814 testStep->setAssertMessageFormat(kProxyIndirectStateAssertMessageFormat);
815 AssertCanvasStatesEqual(reporter, &indirectCanvas, &referenceCanvas,
816 testStep);
817}
818
caryclark@google.com42639cd2012-06-06 12:03:39 +0000819// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000820static void TestNWayCanvasStateConsistency(
821 skiatest::Reporter* reporter,
822 CanvasTestStep* testStep,
823 const SkCanvas& referenceCanvas) {
824
825 SkBitmap indirectStore1;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000826 createBitmap(&indirectStore1, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000827 SkBitmapDevice indirectDevice1(indirectStore1);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000828 SkCanvas indirectCanvas1(&indirectDevice1);
829
830 SkBitmap indirectStore2;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000831 createBitmap(&indirectStore2, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000832 SkBitmapDevice indirectDevice2(indirectStore2);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000833 SkCanvas indirectCanvas2(&indirectDevice2);
834
djsollen@google.comf0a062b2012-05-01 16:50:25 +0000835 SkISize canvasSize = referenceCanvas.getDeviceSize();
836 SkNWayCanvas nWayCanvas(canvasSize.width(), canvasSize.height());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000837 nWayCanvas.addCanvas(&indirectCanvas1);
838 nWayCanvas.addCanvas(&indirectCanvas2);
839
840 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
841 testStep->draw(&nWayCanvas, reporter);
842 // Verify that the SkProxyCanvas reports consitent state
843 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
844 AssertCanvasStatesEqual(reporter, &nWayCanvas, &referenceCanvas,
845 testStep);
846 // Verify that the indirect canvases report consitent state
847 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
848 AssertCanvasStatesEqual(reporter, &indirectCanvas1, &referenceCanvas,
849 testStep);
850 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
851 AssertCanvasStatesEqual(reporter, &indirectCanvas2, &referenceCanvas,
852 testStep);
853}
854
855/*
856 * This sub-test verifies that the test step passes when executed
857 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
858 * that the all canvas derivatives report the same state as an SkCanvas
859 * after having executed the test step.
860 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000861static void TestOverrideStateConsistency(skiatest::Reporter* reporter,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000862 CanvasTestStep* testStep) {
863 SkBitmap referenceStore;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000864 createBitmap(&referenceStore, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000865 SkBitmapDevice referenceDevice(referenceStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000866 SkCanvas referenceCanvas(&referenceDevice);
867 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
868 testStep->draw(&referenceCanvas, reporter);
869
junov@chromium.orgfb103892012-09-20 19:35:43 +0000870 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, false);
871
872 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, true);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000873
caryclark@google.com42639cd2012-06-06 12:03:39 +0000874 // The following test code is disabled because SkProxyCanvas is
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000875 // missing a lot of virtual overrides on get* methods, which are used
876 // to verify canvas state.
877 // Issue: http://code.google.com/p/skia/issues/detail?id=500
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000878
caryclark@google.com42639cd2012-06-06 12:03:39 +0000879 if (false) { // avoid bit rot, suppress warning
880 TestProxyCanvasStateConsistency(reporter, testStep, referenceCanvas);
881 }
882
883 // The following test code is disabled because SkNWayCanvas does not
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000884 // report correct clipping and device bounds information
885 // Issue: http://code.google.com/p/skia/issues/detail?id=501
caryclark@google.com42639cd2012-06-06 12:03:39 +0000886
887 if (false) { // avoid bit rot, suppress warning
888 TestNWayCanvasStateConsistency(reporter, testStep, referenceCanvas);
889 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000890
caryclark@google.com42639cd2012-06-06 12:03:39 +0000891 if (false) { // avoid bit rot, suppress warning
892 test_clipVisitor(reporter, &referenceCanvas);
893 }
reed@google.com7c202932011-12-14 18:48:05 +0000894}
reed@google.com37f3ae02011-11-28 16:06:04 +0000895
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000896DEF_TEST(Canvas, reporter) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000897 // Init global here because bitmap pixels cannot be alocated during
898 // static initialization
899 kTestBitmap = testBitmap();
reed@google.com37f3ae02011-11-28 16:06:04 +0000900
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000901 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
902 TestOverrideStateConsistency(reporter, testStepArray()[testStep]);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000903 SkPictureTester::TestPictureFlattenedObjectReuse(reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000904 testStepArray()[testStep], 0);
edisonn@google.com77909122012-10-18 15:58:23 +0000905 if (testStepArray()[testStep]->enablePdfTesting()) {
906 TestPdfDevice(reporter, testStepArray()[testStep]);
907 }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000908 }
junov@chromium.orgcd62ecf2012-08-02 17:43:25 +0000909
910 // Explicitly call reset(), so we don't leak the pixels (since kTestBitmap is a global)
911 kTestBitmap.reset();
reed@google.com37f3ae02011-11-28 16:06:04 +0000912}