blob: 744eafcf807b1c2beb47f82a5090b69faa17e5dc [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"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000066#include "TestClassDef.h"
reed@google.com37f3ae02011-11-28 16:06:04 +000067
reed@google.com90c07ea2012-04-13 13:50:27 +000068class Canvas2CanvasClipVisitor : public SkCanvas::ClipVisitor {
69public:
70 Canvas2CanvasClipVisitor(SkCanvas* target) : fTarget(target) {}
71
72 virtual void clipRect(const SkRect& r, SkRegion::Op op, bool aa) {
73 fTarget->clipRect(r, op, aa);
74 }
75 virtual void clipPath(const SkPath& p, SkRegion::Op op, bool aa) {
76 fTarget->clipPath(p, op, aa);
77 }
78
79private:
80 SkCanvas* fTarget;
81};
82
83static void test_clipVisitor(skiatest::Reporter* reporter, SkCanvas* canvas) {
84 SkISize size = canvas->getDeviceSize();
rmistry@google.comd6176b02012-08-23 18:14:13 +000085
reed@google.com90c07ea2012-04-13 13:50:27 +000086 SkBitmap bm;
87 bm.setConfig(SkBitmap::kARGB_8888_Config, size.width(), size.height());
88 SkCanvas c(bm);
89
90 Canvas2CanvasClipVisitor visitor(&c);
91 canvas->replayClips(&visitor);
92
93 REPORTER_ASSERT(reporter, c.getTotalClip() == canvas->getTotalClip());
94}
95
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000096static const int kWidth = 2;
97static const int kHeight = 2;
reed@google.com7c202932011-12-14 18:48:05 +000098
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000099// Format strings that describe the test context. The %s token is where
100// the name of the test step is inserted. The context is required for
101// disambiguating the error in the case of failures that are reported in
102// functions that are called multiple times in different contexts (test
103// cases and test steps).
104static const char* const kDefaultAssertMessageFormat = "%s";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000105static const char* const kCanvasDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000106 "Drawing test step %s with SkCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000107static const char* const kPictureDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000108 "Drawing test step %s with SkPicture";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000109static const char* const kPictureSecondDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000110 "Duplicate draw of test step %s with SkPicture";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000111static const char* const kDeferredDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000112 "Drawing test step %s with SkDeferredCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000113static const char* const kProxyDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000114 "Drawing test step %s with SkProxyCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000115static const char* const kNWayDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000116 "Drawing test step %s with SkNWayCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000117static const char* const kDeferredPreFlushAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000118 "test step %s, SkDeferredCanvas state consistency before flush";
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000119static const char* const kDeferredPostFlushPlaybackAssertMessageFormat =
120 "test step %s, SkDeferredCanvas playback canvas state consistency after flush";
junov@chromium.orgfb103892012-09-20 19:35:43 +0000121static const char* const kDeferredPostSilentFlushPlaybackAssertMessageFormat =
122 "test step %s, SkDeferredCanvas playback canvas state consistency after silent flush";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000123static const char* const kPictureResourceReuseMessageFormat =
124 "test step %s, SkPicture duplicate flattened object test";
125static const char* const kProxyStateAssertMessageFormat =
126 "test step %s, SkProxyCanvas state consistency";
127static const char* const kProxyIndirectStateAssertMessageFormat =
128 "test step %s, SkProxyCanvas indirect canvas state consistency";
129static const char* const kNWayStateAssertMessageFormat =
130 "test step %s, SkNWayCanvas state consistency";
131static const char* const kNWayIndirect1StateAssertMessageFormat =
132 "test step %s, SkNWayCanvas indirect canvas 1 state consistency";
133static const char* const kNWayIndirect2StateAssertMessageFormat =
134 "test step %s, SkNWayCanvas indirect canvas 2 state consistency";
edisonn@google.com77909122012-10-18 15:58:23 +0000135static const char* const kPdfAssertMessageFormat =
136 "PDF sanity check failed %s";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000137
138static void createBitmap(SkBitmap* bm, SkBitmap::Config config, SkColor color) {
139 bm->setConfig(config, kWidth, kHeight);
140 bm->allocPixels();
141 bm->eraseColor(color);
142}
143
144class CanvasTestStep;
145static SkTDArray<CanvasTestStep*>& testStepArray() {
146 static SkTDArray<CanvasTestStep*> theTests;
147 return theTests;
148}
149
150class CanvasTestStep {
151public:
edisonn@google.com77909122012-10-18 15:58:23 +0000152 CanvasTestStep(bool fEnablePdfTesting = true) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000153 *testStepArray().append() = this;
154 fAssertMessageFormat = kDefaultAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000155 this->fEnablePdfTesting = fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000156 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000157 virtual ~CanvasTestStep() { }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000158
159 virtual void draw(SkCanvas*, skiatest::Reporter*) = 0;
160 virtual const char* name() const = 0;
161
162 const char* assertMessage() {
163 fAssertMessage.printf(fAssertMessageFormat, name());
164 return fAssertMessage.c_str();
165 }
166
167 void setAssertMessageFormat(const char* format) {
168 fAssertMessageFormat = format;
169 }
170
edisonn@google.com77909122012-10-18 15:58:23 +0000171 bool enablePdfTesting() { return fEnablePdfTesting; }
172
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000173private:
174 SkString fAssertMessage;
175 const char* fAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000176 bool fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000177};
178
179///////////////////////////////////////////////////////////////////////////////
180// Constants used by test steps
181
rmistry@google.comd6176b02012-08-23 18:14:13 +0000182const SkRect kTestRect =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000183 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
184 SkIntToScalar(2), SkIntToScalar(1));
185static SkMatrix testMatrix() {
186 SkMatrix matrix;
187 matrix.reset();
188 matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
189 return matrix;
190}
191const SkMatrix kTestMatrix = testMatrix();
192static SkPath testPath() {
193 SkPath path;
194 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
195 SkIntToScalar(2), SkIntToScalar(1)));
196 return path;
197}
198const SkPath kTestPath = testPath();
199static SkRegion testRegion() {
200 SkRegion region;
201 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
202 region.setRect(rect);
203 return region;
204}
205const SkIRect kTestIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
206const SkRegion kTestRegion = testRegion();
207const SkColor kTestColor = 0x01020304;
208const SkPaint kTestPaint;
209const SkPoint kTestPoints[3] = {
210 {SkIntToScalar(0), SkIntToScalar(0)},
211 {SkIntToScalar(2), SkIntToScalar(1)},
212 {SkIntToScalar(0), SkIntToScalar(2)}
213};
214const size_t kTestPointCount = 3;
215static SkBitmap testBitmap() {
216 SkBitmap bitmap;
217 createBitmap(&bitmap, SkBitmap::kARGB_8888_Config, 0x05060708);
218 return bitmap;
219}
220SkBitmap kTestBitmap; // cannot be created during static init
221SkString kTestText("Hello World");
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000222SkPoint kTestPoints2[] = {
223 { SkIntToScalar(0), SkIntToScalar(1) },
224 { SkIntToScalar(1), SkIntToScalar(1) },
225 { SkIntToScalar(2), SkIntToScalar(1) },
226 { SkIntToScalar(3), SkIntToScalar(1) },
227 { SkIntToScalar(4), SkIntToScalar(1) },
228 { SkIntToScalar(5), SkIntToScalar(1) },
229 { SkIntToScalar(6), SkIntToScalar(1) },
230 { SkIntToScalar(7), SkIntToScalar(1) },
231 { SkIntToScalar(8), SkIntToScalar(1) },
232 { SkIntToScalar(9), SkIntToScalar(1) },
233 { SkIntToScalar(10), SkIntToScalar(1) },
234};
rmistry@google.comd6176b02012-08-23 18:14:13 +0000235
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000236
237///////////////////////////////////////////////////////////////////////////////
238// Macros for defining test steps
239
240#define TEST_STEP(NAME, FUNCTION) \
241class NAME##_TestStep : public CanvasTestStep{ \
242public: \
243 virtual void draw(SkCanvas* canvas, skiatest::Reporter* reporter) { \
244 FUNCTION (canvas, reporter, this); \
245 } \
246 virtual const char* name() const {return #NAME ;} \
247}; \
248static NAME##_TestStep NAME##_TestStepInstance;
249
edisonn@google.com77909122012-10-18 15:58:23 +0000250#define TEST_STEP_NO_PDF(NAME, FUNCTION) \
251class NAME##_TestStep : public CanvasTestStep{ \
252public: \
253 NAME##_TestStep() : CanvasTestStep(false) {} \
254 virtual void draw(SkCanvas* canvas, skiatest::Reporter* reporter) { \
255 FUNCTION (canvas, reporter, this); \
256 } \
257 virtual const char* name() const {return #NAME ;} \
258}; \
259static NAME##_TestStep NAME##_TestStepInstance;
260
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000261#define SIMPLE_TEST_STEP(NAME, CALL) \
262static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter*, \
263 CanvasTestStep*) { \
264 canvas-> CALL ; \
265} \
266TEST_STEP(NAME, NAME##TestStep )
267
268#define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
269static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter* reporter, \
270 CanvasTestStep* testStep) { \
271 REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \
272 testStep->assertMessage()); \
273} \
274TEST_STEP(NAME, NAME##TestStep )
275
276
277///////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000278// Basic test steps for most virtual methods in SkCanvas that draw or affect
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000279// the state of the canvas.
280
junov@chromium.orga907ac32012-02-24 21:54:07 +0000281SIMPLE_TEST_STEP_WITH_ASSERT(Translate,
282 translate(SkIntToScalar(1), SkIntToScalar(2)));
283SIMPLE_TEST_STEP_WITH_ASSERT(Scale,
284 scale(SkIntToScalar(1), SkIntToScalar(2)));
285SIMPLE_TEST_STEP_WITH_ASSERT(Rotate, rotate(SkIntToScalar(1)));
286SIMPLE_TEST_STEP_WITH_ASSERT(Skew,
287 skew(SkIntToScalar(1), SkIntToScalar(2)));
288SIMPLE_TEST_STEP_WITH_ASSERT(Concat, concat(kTestMatrix));
289SIMPLE_TEST_STEP(SetMatrix, setMatrix(kTestMatrix));
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000290SIMPLE_TEST_STEP(ClipRect, clipRect(kTestRect));
291SIMPLE_TEST_STEP(ClipPath, clipPath(kTestPath));
292SIMPLE_TEST_STEP(ClipRegion,
junov@chromium.orga907ac32012-02-24 21:54:07 +0000293 clipRegion(kTestRegion, SkRegion::kReplace_Op));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000294SIMPLE_TEST_STEP(Clear, clear(kTestColor));
295SIMPLE_TEST_STEP(DrawPaint, drawPaint(kTestPaint));
296SIMPLE_TEST_STEP(DrawPointsPoints, drawPoints(SkCanvas::kPoints_PointMode,
297 kTestPointCount, kTestPoints, kTestPaint));
298SIMPLE_TEST_STEP(DrawPointsLiness, drawPoints(SkCanvas::kLines_PointMode,
299 kTestPointCount, kTestPoints, kTestPaint));
300SIMPLE_TEST_STEP(DrawPointsPolygon, drawPoints(SkCanvas::kPolygon_PointMode,
301 kTestPointCount, kTestPoints, kTestPaint));
302SIMPLE_TEST_STEP(DrawRect, drawRect(kTestRect, kTestPaint));
303SIMPLE_TEST_STEP(DrawPath, drawPath(kTestPath, kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000304SIMPLE_TEST_STEP(DrawBitmap, drawBitmap(kTestBitmap, 0, 0));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000305SIMPLE_TEST_STEP(DrawBitmapPaint, drawBitmap(kTestBitmap, 0, 0, &kTestPaint));
306SIMPLE_TEST_STEP(DrawBitmapRect, drawBitmapRect(kTestBitmap, NULL, kTestRect,
307 NULL));
308SIMPLE_TEST_STEP(DrawBitmapRectSrcRect, drawBitmapRect(kTestBitmap,
309 &kTestIRect, kTestRect, NULL));
310SIMPLE_TEST_STEP(DrawBitmapRectPaint, drawBitmapRect(kTestBitmap, NULL,
311 kTestRect, &kTestPaint));
312SIMPLE_TEST_STEP(DrawBitmapMatrix, drawBitmapMatrix(kTestBitmap, kTestMatrix,
313 NULL));
314SIMPLE_TEST_STEP(DrawBitmapMatrixPaint, drawBitmapMatrix(kTestBitmap,
315 kTestMatrix, &kTestPaint));
316SIMPLE_TEST_STEP(DrawBitmapNine, drawBitmapNine(kTestBitmap, kTestIRect,
317 kTestRect, NULL));
318SIMPLE_TEST_STEP(DrawBitmapNinePaint, drawBitmapNine(kTestBitmap, kTestIRect,
319 kTestRect, &kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000320SIMPLE_TEST_STEP(DrawSprite, drawSprite(kTestBitmap, 0, 0, NULL));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000321SIMPLE_TEST_STEP(DrawSpritePaint, drawSprite(kTestBitmap, 0, 0, &kTestPaint));
322SIMPLE_TEST_STEP(DrawText, drawText(kTestText.c_str(), kTestText.size(),
323 0, 1, kTestPaint));
324SIMPLE_TEST_STEP(DrawPosText, drawPosText(kTestText.c_str(),
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000325 kTestText.size(), kTestPoints2, kTestPaint));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000326SIMPLE_TEST_STEP(DrawTextOnPath, drawTextOnPath(kTestText.c_str(),
327 kTestText.size(), kTestPath, NULL, kTestPaint));
328SIMPLE_TEST_STEP(DrawTextOnPathMatrix, drawTextOnPath(kTestText.c_str(),
329 kTestText.size(), kTestPath, &kTestMatrix, kTestPaint));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000330SIMPLE_TEST_STEP(DrawData, drawData(kTestText.c_str(), kTestText.size()));
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000331SIMPLE_TEST_STEP(BeginGroup, beginCommentGroup(kTestText.c_str()));
332SIMPLE_TEST_STEP(AddComment, addComment(kTestText.c_str(), kTestText.c_str()));
333SIMPLE_TEST_STEP(EndGroup, endCommentGroup());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000334
335///////////////////////////////////////////////////////////////////////////////
336// Complex test steps
337
rmistry@google.comd6176b02012-08-23 18:14:13 +0000338// Save/restore calls cannot be in isolated simple test steps because the test
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000339// cases that use SkPicture require that save and restore calls be balanced.
rmistry@google.comd6176b02012-08-23 18:14:13 +0000340static void SaveMatrixStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000341 skiatest::Reporter* reporter,
342 CanvasTestStep* testStep) {
343 int saveCount = canvas->getSaveCount();
344 canvas->save(SkCanvas::kMatrix_SaveFlag);
345 canvas->clipRegion(kTestRegion);
346 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
347 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000348 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000349 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000350 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000351 testStep->assertMessage());
352 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() == kTestRegion,
353 testStep->assertMessage());
354}
355TEST_STEP(SaveMatrix, SaveMatrixStep);
356
rmistry@google.comd6176b02012-08-23 18:14:13 +0000357static void SaveClipStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000358 skiatest::Reporter* reporter,
359 CanvasTestStep* testStep) {
360 int saveCount = canvas->getSaveCount();
361 canvas->save(SkCanvas::kClip_SaveFlag);
362 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
363 canvas->clipRegion(kTestRegion);
364 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000365 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000366 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000367 REPORTER_ASSERT_MESSAGE(reporter, !canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000368 testStep->assertMessage());
369 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion,
370 testStep->assertMessage());
371}
372TEST_STEP(SaveClip, SaveClipStep);
373
rmistry@google.comd6176b02012-08-23 18:14:13 +0000374static void SaveMatrixClipStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000375 skiatest::Reporter* reporter,
376 CanvasTestStep* testStep) {
377 int saveCount = canvas->getSaveCount();
378 canvas->save(SkCanvas::kMatrixClip_SaveFlag);
379 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
380 canvas->clipRegion(kTestRegion);
381 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000382 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000383 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000384 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000385 testStep->assertMessage());
386 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion,
387 testStep->assertMessage());
388}
389TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
390
rmistry@google.comd6176b02012-08-23 18:14:13 +0000391static void SaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000392 skiatest::Reporter* reporter,
393 CanvasTestStep* testStep) {
394 int saveCount = canvas->getSaveCount();
395 canvas->saveLayer(NULL, NULL);
396 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000397 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000398 testStep->assertMessage());
399}
400TEST_STEP(SaveLayer, SaveLayerStep);
401
rmistry@google.comd6176b02012-08-23 18:14:13 +0000402static void BoundedSaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000403 skiatest::Reporter* reporter,
404 CanvasTestStep* testStep) {
405 int saveCount = canvas->getSaveCount();
406 canvas->saveLayer(&kTestRect, NULL);
407 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000408 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000409 testStep->assertMessage());
410}
411TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
412
rmistry@google.comd6176b02012-08-23 18:14:13 +0000413static void PaintSaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000414 skiatest::Reporter* reporter,
415 CanvasTestStep* testStep) {
416 int saveCount = canvas->getSaveCount();
417 canvas->saveLayer(NULL, &kTestPaint);
418 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000419 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000420 testStep->assertMessage());
421}
422TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
423
rmistry@google.comd6176b02012-08-23 18:14:13 +0000424static void TwoClipOpsStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000425 skiatest::Reporter*,
426 CanvasTestStep*) {
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000427 // This test exercises a functionality in SkPicture that leads to the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000428 // recording of restore offset placeholders. This test will trigger an
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000429 // assertion at playback time if the placeholders are not properly
430 // filled when the recording ends.
431 canvas->clipRect(kTestRect);
432 canvas->clipRegion(kTestRegion);
433}
434TEST_STEP(TwoClipOps, TwoClipOpsStep);
435
epoger@google.com94fa43c2012-04-11 17:51:01 +0000436// exercise fix for http://code.google.com/p/skia/issues/detail?id=560
437// ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
rmistry@google.comd6176b02012-08-23 18:14:13 +0000438static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000439 skiatest::Reporter*,
440 CanvasTestStep*) {
epoger@google.com94fa43c2012-04-11 17:51:01 +0000441 SkPaint paint;
442 paint.setStrokeWidth(SkIntToScalar(1));
443 paint.setStyle(SkPaint::kStroke_Style);
444
445 SkPath path;
446 SkPoint pt1 = { 0, 0 };
447 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
448 SkPoint pt3 = { SkIntToScalar(1), 0 };
449 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
450 path.moveTo(pt1);
451 path.lineTo(pt2);
452 path.lineTo(pt3);
453 path.lineTo(pt4);
454
455 canvas->drawPath(path, paint);
456}
457TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
458
rmistry@google.comd6176b02012-08-23 18:14:13 +0000459static void DrawVerticesShaderTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000460 skiatest::Reporter*,
461 CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000462 SkPoint pts[4];
463 pts[0].set(0, 0);
464 pts[1].set(SkIntToScalar(kWidth), 0);
465 pts[2].set(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
466 pts[3].set(0, SkIntToScalar(kHeight));
467 SkPaint paint;
468 SkShader* shader = SkShader::CreateBitmapShader(kTestBitmap,
469 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
470 paint.setShader(shader)->unref();
471 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
472 NULL, NULL, NULL, 0, paint);
473}
edisonn@google.com77909122012-10-18 15:58:23 +0000474// NYI: issue 240.
475TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000476
rmistry@google.comd6176b02012-08-23 18:14:13 +0000477static void DrawPictureTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000478 skiatest::Reporter*,
479 CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000480 SkPicture* testPicture = SkNEW_ARGS(SkPicture, ());
481 SkAutoUnref aup(testPicture);
482 SkCanvas* testCanvas = testPicture->beginRecording(kWidth, kHeight);
483 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
484 testCanvas->clipRect(kTestRect);
485 testCanvas->drawRect(kTestRect, kTestPaint);
486 canvas->drawPicture(*testPicture);
487}
488TEST_STEP(DrawPicture, DrawPictureTestStep);
489
rmistry@google.comd6176b02012-08-23 18:14:13 +0000490static void SaveRestoreTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000491 skiatest::Reporter* reporter,
492 CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000493 int baseSaveCount = canvas->getSaveCount();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000494 int n = canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000495 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount == n, testStep->assertMessage());
496 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000497 testStep->assertMessage());
498 canvas->save();
499 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000500 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000501 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000502 canvas->restoreToCount(baseSaveCount + 1);
503 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000504 testStep->assertMessage());
505
506 // should this pin to 1, or be a no-op, or crash?
507 canvas->restoreToCount(0);
508 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
509 testStep->assertMessage());
510}
511TEST_STEP(SaveRestore, SaveRestoreTestStep);
512
rmistry@google.comd6176b02012-08-23 18:14:13 +0000513static void DrawLayerTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000514 skiatest::Reporter* reporter,
515 CanvasTestStep* testStep) {
516 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
517 testStep->assertMessage());
518 canvas->save();
519 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
520 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000521 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000522
reed@google.com7c202932011-12-14 18:48:05 +0000523 const SkRect* bounds = NULL; // null means include entire bounds
524 const SkPaint* paint = NULL;
525
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000526 canvas->saveLayer(bounds, paint);
527 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
528 testStep->assertMessage());
529 canvas->restore();
530 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
531 testStep->assertMessage());
reed@google.com7c202932011-12-14 18:48:05 +0000532
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000533 canvas->saveLayer(bounds, paint);
534 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());
540 canvas->restore();
reed@google.com7c202932011-12-14 18:48:05 +0000541 // now layer count should be 0
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000542 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
543 testStep->assertMessage());
544}
545TEST_STEP(DrawLayer, DrawLayerTestStep);
reed@google.com3b3e8952012-08-16 20:53:31 +0000546
547static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000548 skiatest::Reporter*,
549 CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000550 // This test step challenges the TestDeferredCanvasStateConsistency
551 // test cases because the opaque paint can trigger an optimization
552 // that discards previously recorded commands. The challenge is to maintain
553 // correct clip and matrix stack state.
554 canvas->resetMatrix();
555 canvas->rotate(SkIntToScalar(30));
556 canvas->save();
557 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
558 canvas->save();
559 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
560 SkPaint paint;
561 paint.setColor(0xFFFFFFFF);
562 canvas->drawPaint(paint);
563 canvas->restore();
564 canvas->restore();
565}
566TEST_STEP(NestedSaveRestoreWithSolidPaint, \
567 NestedSaveRestoreWithSolidPaintTestStep);
568
569static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000570 skiatest::Reporter*,
571 CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000572 // This test step challenges the TestDeferredCanvasStateConsistency
573 // test case because the canvas flush on a deferred canvas will
574 // reset the recording session. The challenge is to maintain correct
575 // clip and matrix stack state on the playback canvas.
576 canvas->resetMatrix();
577 canvas->rotate(SkIntToScalar(30));
578 canvas->save();
579 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
580 canvas->save();
581 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
582 canvas->drawRect(kTestRect,kTestPaint);
583 canvas->flush();
584 canvas->restore();
585 canvas->restore();
586}
587TEST_STEP(NestedSaveRestoreWithFlush, \
588 NestedSaveRestoreWithFlushTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000589
590static void AssertCanvasStatesEqual(skiatest::Reporter* reporter,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000591 const SkCanvas* canvas1,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000592 const SkCanvas* canvas2,
593 CanvasTestStep* testStep) {
594 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
595 canvas2->getDeviceSize(), testStep->assertMessage());
596 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
597 canvas2->getSaveCount(), testStep->assertMessage());
598 REPORTER_ASSERT_MESSAGE(reporter, canvas1->isDrawingToLayer() ==
599 canvas2->isDrawingToLayer(), testStep->assertMessage());
reed@google.com3b3e8952012-08-16 20:53:31 +0000600
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000601 SkRect bounds1, bounds2;
602 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.com3b3e8952012-08-16 20:53:31 +0000603 canvas1->getClipBounds(&bounds1) == canvas2->getClipBounds(&bounds2),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000604 testStep->assertMessage());
605 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
reed@google.com3b3e8952012-08-16 20:53:31 +0000606 testStep->assertMessage());
607
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000608 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
609 canvas2->getDrawFilter(), testStep->assertMessage());
610 SkIRect deviceBounds1, deviceBounds2;
611 REPORTER_ASSERT_MESSAGE(reporter,
612 canvas1->getClipDeviceBounds(&deviceBounds1) ==
613 canvas2->getClipDeviceBounds(&deviceBounds2),
614 testStep->assertMessage());
615 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2,
616 testStep->assertMessage());
617 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getBounder() ==
618 canvas2->getBounder(), testStep->assertMessage());
619 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
620 canvas2->getTotalMatrix(), testStep->assertMessage());
621 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getClipType() ==
622 canvas2->getClipType(), testStep->assertMessage());
623 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalClip() ==
624 canvas2->getTotalClip(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000625
626 // The following test code is commented out because the test fails when
rmistry@google.comd6176b02012-08-23 18:14:13 +0000627 // the canvas is an SkPictureRecord or SkDeferredCanvas
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000628 // Issue: http://code.google.com/p/skia/issues/detail?id=498
629 // Also, creating a LayerIter on an SkProxyCanvas crashes
630 // Issue: http://code.google.com/p/skia/issues/detail?id=499
631 /*
632 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
633 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
634 while (!layerIter1.done() && !layerIter2.done()) {
635 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
636 layerIter2.matrix(), testStep->assertMessage());
637 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
638 layerIter2.clip(), testStep->assertMessage());
639 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
640 layerIter2.paint(), testStep->assertMessage());
641 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
642 layerIter2.x(), testStep->assertMessage());
643 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
644 layerIter2.y(), testStep->assertMessage());
645 layerIter1.next();
646 layerIter2.next();
647 }
648 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
649 testStep->assertMessage());
650 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
651 testStep->assertMessage());
652 */
653}
654
655// The following class groups static functions that need to access
656// the privates members of SkPictureRecord
657class SkPictureTester {
658private:
reed@google.come2589ae2012-07-10 19:38:01 +0000659 static int EQ(const SkFlatData* a, const SkFlatData* b) {
660 return *a == *b;
661 }
662
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000663 static void AssertFlattenedObjectsEqual(
664 SkPictureRecord* referenceRecord,
665 SkPictureRecord* testRecord,
666 skiatest::Reporter* reporter,
667 CanvasTestStep* testStep) {
668
669 REPORTER_ASSERT_MESSAGE(reporter,
djsollen@google.comc9ab9872012-08-29 18:52:07 +0000670 referenceRecord->fBitmapHeap->count() ==
671 testRecord->fBitmapHeap->count(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000672 REPORTER_ASSERT_MESSAGE(reporter,
673 referenceRecord->fMatrices.count() ==
674 testRecord->fMatrices.count(), testStep->assertMessage());
675 for (int i = 0; i < referenceRecord->fMatrices.count(); ++i) {
676 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.come2589ae2012-07-10 19:38:01 +0000677 EQ(referenceRecord->fMatrices[i], testRecord->fMatrices[i]),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000678 testStep->assertMessage());
679 }
680 REPORTER_ASSERT_MESSAGE(reporter,
681 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,
689 referenceRecord->fRegions.count() ==
690 testRecord->fRegions.count(), testStep->assertMessage());
691 for (int i = 0; i < referenceRecord->fRegions.count(); ++i) {
692 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.come2589ae2012-07-10 19:38:01 +0000693 EQ(referenceRecord->fRegions[i], testRecord->fRegions[i]),
694 testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000695 }
696 REPORTER_ASSERT_MESSAGE(reporter,
697 !referenceRecord->fPathHeap ==
698 !testRecord->fPathHeap,
699 testStep->assertMessage());
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000700 // The following tests are commented out because they currently
701 // fail. Issue: http://code.google.com/p/skia/issues/detail?id=507
702 /*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000703 if (referenceRecord->fPathHeap) {
704 REPORTER_ASSERT_MESSAGE(reporter,
705 referenceRecord->fPathHeap->count() ==
706 testRecord->fPathHeap->count(),
707 testStep->assertMessage());
708 for (int i = 0; i < referenceRecord->fPathHeap->count(); ++i) {
709 REPORTER_ASSERT_MESSAGE(reporter,
710 (*referenceRecord->fPathHeap)[i] ==
711 (*testRecord->fPathHeap)[i], testStep->assertMessage());
712 }
713 }
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000714 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000715
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000716 }
717
718public:
719
rmistry@google.comd6176b02012-08-23 18:14:13 +0000720 static void TestPictureFlattenedObjectReuse(skiatest::Reporter* reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000721 CanvasTestStep* testStep,
722 uint32_t recordFlags) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000723 // Verify that when a test step is executed twice, no extra resources
724 // are flattened during the second execution
725 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
726 SkPicture referencePicture;
727 SkCanvas* referenceCanvas = referencePicture.beginRecording(kWidth,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000728 kHeight, recordFlags);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000729 testStep->draw(referenceCanvas, reporter);
730 SkPicture testPicture;
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000731 SkCanvas* testCanvas = testPicture.beginRecording(kWidth,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000732 kHeight, recordFlags);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000733 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000734 testStep->setAssertMessageFormat(kPictureSecondDrawAssertMessageFormat);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000735 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000736
737 SkPictureRecord* referenceRecord = static_cast<SkPictureRecord*>(
738 referenceCanvas);
739 SkPictureRecord* testRecord = static_cast<SkPictureRecord*>(
740 testCanvas);
741 testStep->setAssertMessageFormat(kPictureResourceReuseMessageFormat);
742 AssertFlattenedObjectsEqual(referenceRecord, testRecord,
junov@chromium.org76b9c4b2012-02-22 21:24:41 +0000743 reporter, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000744 }
745};
746
edisonn@google.com77909122012-10-18 15:58:23 +0000747static void TestPdfDevice(skiatest::Reporter* reporter,
748 CanvasTestStep* testStep) {
749 SkISize pageSize = SkISize::Make(kWidth, kHeight);
750 SkPDFDevice device(pageSize, pageSize, SkMatrix::I());
751 SkCanvas canvas(&device);
752 testStep->setAssertMessageFormat(kPdfAssertMessageFormat);
753 testStep->draw(&canvas, reporter);
754 SkPDFDocument doc;
755 doc.appendPage(&device);
756 SkDynamicMemoryWStream stream;
757 doc.emitPDF(&stream);
758}
759
junov@chromium.org88e29142012-08-07 16:48:22 +0000760// The following class groups static functions that need to access
761// the privates members of SkDeferredCanvas
762class SkDeferredCanvasTester {
763public:
764 static void TestDeferredCanvasStateConsistency(
765 skiatest::Reporter* reporter,
766 CanvasTestStep* testStep,
junov@chromium.orgfb103892012-09-20 19:35:43 +0000767 const SkCanvas& referenceCanvas, bool silent) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000768
junov@chromium.org88e29142012-08-07 16:48:22 +0000769 SkBitmap deferredStore;
770 createBitmap(&deferredStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000771 SkBitmapDevice deferredDevice(deferredStore);
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +0000772 SkAutoTUnref<SkDeferredCanvas> deferredCanvas(SkDeferredCanvas::Create(&deferredDevice));
junov@chromium.org88e29142012-08-07 16:48:22 +0000773 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
junov@chromium.org66070a52013-05-28 17:39:08 +0000774 testStep->draw(deferredCanvas, reporter);
junov@chromium.org88e29142012-08-07 16:48:22 +0000775 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
junov@chromium.org66070a52013-05-28 17:39:08 +0000776 AssertCanvasStatesEqual(reporter, deferredCanvas, &referenceCanvas,
junov@chromium.org88e29142012-08-07 16:48:22 +0000777 testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000778
junov@chromium.orgfb103892012-09-20 19:35:43 +0000779 if (silent) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000780 deferredCanvas->silentFlush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000781 } else {
junov@chromium.org66070a52013-05-28 17:39:08 +0000782 deferredCanvas->flush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000783 }
784
skia.committer@gmail.com4c5ea442012-09-21 02:01:01 +0000785 testStep->setAssertMessageFormat(
junov@chromium.orgfb103892012-09-20 19:35:43 +0000786 silent ? kDeferredPostSilentFlushPlaybackAssertMessageFormat :
junov@chromium.org88e29142012-08-07 16:48:22 +0000787 kDeferredPostFlushPlaybackAssertMessageFormat);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000788 AssertCanvasStatesEqual(reporter,
junov@chromium.org66070a52013-05-28 17:39:08 +0000789 deferredCanvas->immediateCanvas(),
junov@chromium.org88e29142012-08-07 16:48:22 +0000790 &referenceCanvas, testStep);
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000791
junov@chromium.org88e29142012-08-07 16:48:22 +0000792 // Verified that deferred canvas state is not affected by flushing
793 // pending draw operations
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000794
junov@chromium.org88e29142012-08-07 16:48:22 +0000795 // The following test code is commented out because it currently fails.
796 // Issue: http://code.google.com/p/skia/issues/detail?id=496
797 /*
798 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
799 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
800 testStep);
801 */
802 }
803};
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000804
caryclark@google.com42639cd2012-06-06 12:03:39 +0000805// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000806static void TestProxyCanvasStateConsistency(
807 skiatest::Reporter* reporter,
808 CanvasTestStep* testStep,
809 const SkCanvas& referenceCanvas) {
810
811 SkBitmap indirectStore;
812 createBitmap(&indirectStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000813 SkBitmapDevice indirectDevice(indirectStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000814 SkCanvas indirectCanvas(&indirectDevice);
815 SkProxyCanvas proxyCanvas(&indirectCanvas);
816 testStep->setAssertMessageFormat(kProxyDrawAssertMessageFormat);
817 testStep->draw(&proxyCanvas, reporter);
818 // Verify that the SkProxyCanvas reports consitent state
819 testStep->setAssertMessageFormat(kProxyStateAssertMessageFormat);
820 AssertCanvasStatesEqual(reporter, &proxyCanvas, &referenceCanvas,
821 testStep);
822 // Verify that the indirect canvas reports consitent state
823 testStep->setAssertMessageFormat(kProxyIndirectStateAssertMessageFormat);
824 AssertCanvasStatesEqual(reporter, &indirectCanvas, &referenceCanvas,
825 testStep);
826}
827
caryclark@google.com42639cd2012-06-06 12:03:39 +0000828// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000829static void TestNWayCanvasStateConsistency(
830 skiatest::Reporter* reporter,
831 CanvasTestStep* testStep,
832 const SkCanvas& referenceCanvas) {
833
834 SkBitmap indirectStore1;
835 createBitmap(&indirectStore1, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000836 SkBitmapDevice indirectDevice1(indirectStore1);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000837 SkCanvas indirectCanvas1(&indirectDevice1);
838
839 SkBitmap indirectStore2;
840 createBitmap(&indirectStore2, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000841 SkBitmapDevice indirectDevice2(indirectStore2);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000842 SkCanvas indirectCanvas2(&indirectDevice2);
843
djsollen@google.comf0a062b2012-05-01 16:50:25 +0000844 SkISize canvasSize = referenceCanvas.getDeviceSize();
845 SkNWayCanvas nWayCanvas(canvasSize.width(), canvasSize.height());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000846 nWayCanvas.addCanvas(&indirectCanvas1);
847 nWayCanvas.addCanvas(&indirectCanvas2);
848
849 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
850 testStep->draw(&nWayCanvas, reporter);
851 // Verify that the SkProxyCanvas reports consitent state
852 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
853 AssertCanvasStatesEqual(reporter, &nWayCanvas, &referenceCanvas,
854 testStep);
855 // Verify that the indirect canvases report consitent state
856 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
857 AssertCanvasStatesEqual(reporter, &indirectCanvas1, &referenceCanvas,
858 testStep);
859 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
860 AssertCanvasStatesEqual(reporter, &indirectCanvas2, &referenceCanvas,
861 testStep);
862}
863
864/*
865 * This sub-test verifies that the test step passes when executed
866 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
867 * that the all canvas derivatives report the same state as an SkCanvas
868 * after having executed the test step.
869 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000870static void TestOverrideStateConsistency(skiatest::Reporter* reporter,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000871 CanvasTestStep* testStep) {
872 SkBitmap referenceStore;
873 createBitmap(&referenceStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000874 SkBitmapDevice referenceDevice(referenceStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000875 SkCanvas referenceCanvas(&referenceDevice);
876 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
877 testStep->draw(&referenceCanvas, reporter);
878
junov@chromium.orgfb103892012-09-20 19:35:43 +0000879 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, false);
880
881 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, true);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000882
caryclark@google.com42639cd2012-06-06 12:03:39 +0000883 // The following test code is disabled because SkProxyCanvas is
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000884 // missing a lot of virtual overrides on get* methods, which are used
885 // to verify canvas state.
886 // Issue: http://code.google.com/p/skia/issues/detail?id=500
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000887
caryclark@google.com42639cd2012-06-06 12:03:39 +0000888 if (false) { // avoid bit rot, suppress warning
889 TestProxyCanvasStateConsistency(reporter, testStep, referenceCanvas);
890 }
891
892 // The following test code is disabled because SkNWayCanvas does not
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000893 // report correct clipping and device bounds information
894 // Issue: http://code.google.com/p/skia/issues/detail?id=501
caryclark@google.com42639cd2012-06-06 12:03:39 +0000895
896 if (false) { // avoid bit rot, suppress warning
897 TestNWayCanvasStateConsistency(reporter, testStep, referenceCanvas);
898 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000899
caryclark@google.com42639cd2012-06-06 12:03:39 +0000900 if (false) { // avoid bit rot, suppress warning
901 test_clipVisitor(reporter, &referenceCanvas);
902 }
reed@google.com7c202932011-12-14 18:48:05 +0000903}
reed@google.com37f3ae02011-11-28 16:06:04 +0000904
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000905DEF_TEST(Canvas, reporter) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000906 // Init global here because bitmap pixels cannot be alocated during
907 // static initialization
908 kTestBitmap = testBitmap();
reed@google.com37f3ae02011-11-28 16:06:04 +0000909
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000910 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
911 TestOverrideStateConsistency(reporter, testStepArray()[testStep]);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000912 SkPictureTester::TestPictureFlattenedObjectReuse(reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000913 testStepArray()[testStep], 0);
edisonn@google.com77909122012-10-18 15:58:23 +0000914 if (testStepArray()[testStep]->enablePdfTesting()) {
915 TestPdfDevice(reporter, testStepArray()[testStep]);
916 }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000917 }
junov@chromium.orgcd62ecf2012-08-02 17:43:25 +0000918
919 // Explicitly call reset(), so we don't leak the pixels (since kTestBitmap is a global)
920 kTestBitmap.reset();
reed@google.com37f3ae02011-11-28 16:06:04 +0000921}