blob: 18c683dbc12eecbc7d13a79087ec95c35a4a7ab9 [file] [log] [blame]
reed@google.com37f3ae02011-11-28 16:06:04 +00001/*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +00002 * Copyright 2012 Google Inc.
reed@google.com37f3ae02011-11-28 16:06:04 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +00007
8/* Description:
9 * This test defines a series of elementatry test steps that perform
10 * a single or a small group of canvas API calls. Each test step is
11 * used in several test cases that verify that different types of SkCanvas
12 * flavors and derivatives pass it and yield consistent behavior. The
13 * test cases analyse results that are queryable through the API. They do
14 * not look at rendering results.
15 *
16 * Adding test stepss:
17 * The general pattern for creating a new test step is to write a test
18 * function of the form:
19 *
rmistry@google.comd6176b02012-08-23 18:14:13 +000020 * static void MyTestStepFunction(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000021 * skiatest::Reporter* reporter,
22 * CanvasTestStep* testStep)
23 * {
24 * canvas->someCanvasAPImethod();
25 * (...)
26 * REPORTER_ASSERT_MESSAGE(reporter, (...), \
27 * testStep->assertMessage());
28 * }
29 *
30 * The definition of the test step function should be followed by an
31 * invocation of the TEST_STEP macro, which generates a class and
32 * instance for the test step:
33 *
34 * TEST_STEP(MyTestStep, MyTestStepFunction)
35 *
36 * There are also short hand macros for defining simple test steps
37 * in a single line of code. A simple test step is a one that is made
38 * of a single canvas API call.
39 *
40 * SIMPLE_TEST_STEP(MytestStep, someCanvasAPIMethod());
41 *
42 * There is another macro called SIMPLE_TEST_STEP_WITH_ASSERT that
43 * works the same way as SIMPLE_TEST_STEP, and additionally verifies
44 * that the invoked method returns a non-zero value.
45 */
reed@google.com37f3ae02011-11-28 16:06:04 +000046#include "SkBitmap.h"
47#include "SkCanvas.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000048#include "SkDeferredCanvas.h"
49#include "SkDevice.h"
50#include "SkMatrix.h"
51#include "SkNWayCanvas.h"
edisonn@google.com77909122012-10-18 15:58:23 +000052#include "SkPDFDevice.h"
53#include "SkPDFDocument.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000054#include "SkPaint.h"
55#include "SkPath.h"
56#include "SkPicture.h"
57#include "SkPictureRecord.h"
58#include "SkProxyCanvas.h"
59#include "SkRect.h"
60#include "SkRegion.h"
61#include "SkShader.h"
62#include "SkStream.h"
reed@google.com28183b42014-02-04 15:34:10 +000063#include "SkSurface.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000064#include "SkTDArray.h"
65#include "Test.h"
reed@google.com37f3ae02011-11-28 16:06:04 +000066
reed@google.com90c07ea2012-04-13 13:50:27 +000067class Canvas2CanvasClipVisitor : public SkCanvas::ClipVisitor {
68public:
69 Canvas2CanvasClipVisitor(SkCanvas* target) : fTarget(target) {}
70
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000071 virtual void clipRect(const SkRect& r, SkRegion::Op op, bool aa) SK_OVERRIDE {
reed@google.com90c07ea2012-04-13 13:50:27 +000072 fTarget->clipRect(r, op, aa);
73 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000074 virtual void clipRRect(const SkRRect& r, SkRegion::Op op, bool aa) SK_OVERRIDE {
75 fTarget->clipRRect(r, op, aa);
76 }
77 virtual void clipPath(const SkPath& p, SkRegion::Op op, bool aa) SK_OVERRIDE {
reed@google.com90c07ea2012-04-13 13:50:27 +000078 fTarget->clipPath(p, op, aa);
79 }
80
81private:
82 SkCanvas* fTarget;
83};
84
85static void test_clipVisitor(skiatest::Reporter* reporter, SkCanvas* canvas) {
86 SkISize size = canvas->getDeviceSize();
rmistry@google.comd6176b02012-08-23 18:14:13 +000087
reed@google.com90c07ea2012-04-13 13:50:27 +000088 SkBitmap bm;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000089 bm.setConfig(SkImageInfo::MakeN32Premul(size.width(), size.height()));
reed@google.com90c07ea2012-04-13 13:50:27 +000090 SkCanvas c(bm);
91
92 Canvas2CanvasClipVisitor visitor(&c);
93 canvas->replayClips(&visitor);
94
95 REPORTER_ASSERT(reporter, c.getTotalClip() == canvas->getTotalClip());
96}
97
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000098static const int kWidth = 2;
99static const int kHeight = 2;
reed@google.com7c202932011-12-14 18:48:05 +0000100
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000101// Format strings that describe the test context. The %s token is where
102// the name of the test step is inserted. The context is required for
103// disambiguating the error in the case of failures that are reported in
104// functions that are called multiple times in different contexts (test
105// cases and test steps).
106static const char* const kDefaultAssertMessageFormat = "%s";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000107static const char* const kCanvasDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000108 "Drawing test step %s with SkCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000109static const char* const kPictureDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000110 "Drawing test step %s with SkPicture";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000111static const char* const kPictureSecondDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000112 "Duplicate draw of test step %s with SkPicture";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000113static const char* const kDeferredDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000114 "Drawing test step %s with SkDeferredCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000115static const char* const kProxyDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000116 "Drawing test step %s with SkProxyCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000117static const char* const kNWayDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000118 "Drawing test step %s with SkNWayCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000119static const char* const kDeferredPreFlushAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000120 "test step %s, SkDeferredCanvas state consistency before flush";
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000121static const char* const kDeferredPostFlushPlaybackAssertMessageFormat =
122 "test step %s, SkDeferredCanvas playback canvas state consistency after flush";
junov@chromium.orgfb103892012-09-20 19:35:43 +0000123static const char* const kDeferredPostSilentFlushPlaybackAssertMessageFormat =
124 "test step %s, SkDeferredCanvas playback canvas state consistency after silent flush";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000125static const char* const kPictureResourceReuseMessageFormat =
126 "test step %s, SkPicture duplicate flattened object test";
127static const char* const kProxyStateAssertMessageFormat =
128 "test step %s, SkProxyCanvas state consistency";
129static const char* const kProxyIndirectStateAssertMessageFormat =
130 "test step %s, SkProxyCanvas indirect canvas state consistency";
131static const char* const kNWayStateAssertMessageFormat =
132 "test step %s, SkNWayCanvas state consistency";
133static const char* const kNWayIndirect1StateAssertMessageFormat =
134 "test step %s, SkNWayCanvas indirect canvas 1 state consistency";
135static const char* const kNWayIndirect2StateAssertMessageFormat =
136 "test step %s, SkNWayCanvas indirect canvas 2 state consistency";
edisonn@google.com77909122012-10-18 15:58:23 +0000137static const char* const kPdfAssertMessageFormat =
138 "PDF sanity check failed %s";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000139
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000140static void createBitmap(SkBitmap* bm, SkColor color) {
141 bm->allocN32Pixels(kWidth, kHeight);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000142 bm->eraseColor(color);
143}
144
reed@google.com28183b42014-02-04 15:34:10 +0000145static SkSurface* createSurface(SkColor color) {
146 SkSurface* surface = SkSurface::NewRasterPMColor(kWidth, kHeight);
147 surface->getCanvas()->clear(color);
148 return surface;
149}
150
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000151class CanvasTestStep;
152static SkTDArray<CanvasTestStep*>& testStepArray() {
153 static SkTDArray<CanvasTestStep*> theTests;
154 return theTests;
155}
156
157class CanvasTestStep {
158public:
edisonn@google.com77909122012-10-18 15:58:23 +0000159 CanvasTestStep(bool fEnablePdfTesting = true) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000160 *testStepArray().append() = this;
161 fAssertMessageFormat = kDefaultAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000162 this->fEnablePdfTesting = fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000163 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000164 virtual ~CanvasTestStep() { }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000165
166 virtual void draw(SkCanvas*, skiatest::Reporter*) = 0;
167 virtual const char* name() const = 0;
168
169 const char* assertMessage() {
170 fAssertMessage.printf(fAssertMessageFormat, name());
171 return fAssertMessage.c_str();
172 }
173
174 void setAssertMessageFormat(const char* format) {
175 fAssertMessageFormat = format;
176 }
177
edisonn@google.com77909122012-10-18 15:58:23 +0000178 bool enablePdfTesting() { return fEnablePdfTesting; }
179
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000180private:
181 SkString fAssertMessage;
182 const char* fAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000183 bool fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000184};
185
186///////////////////////////////////////////////////////////////////////////////
187// Constants used by test steps
188
rmistry@google.comd6176b02012-08-23 18:14:13 +0000189const SkRect kTestRect =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000190 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
191 SkIntToScalar(2), SkIntToScalar(1));
192static SkMatrix testMatrix() {
193 SkMatrix matrix;
194 matrix.reset();
195 matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
196 return matrix;
197}
198const SkMatrix kTestMatrix = testMatrix();
199static SkPath testPath() {
200 SkPath path;
201 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
202 SkIntToScalar(2), SkIntToScalar(1)));
203 return path;
204}
205const SkPath kTestPath = testPath();
206static SkRegion testRegion() {
207 SkRegion region;
208 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
209 region.setRect(rect);
210 return region;
211}
212const SkIRect kTestIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
213const SkRegion kTestRegion = testRegion();
214const SkColor kTestColor = 0x01020304;
215const SkPaint kTestPaint;
216const SkPoint kTestPoints[3] = {
217 {SkIntToScalar(0), SkIntToScalar(0)},
218 {SkIntToScalar(2), SkIntToScalar(1)},
219 {SkIntToScalar(0), SkIntToScalar(2)}
220};
221const size_t kTestPointCount = 3;
222static SkBitmap testBitmap() {
223 SkBitmap bitmap;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000224 createBitmap(&bitmap, 0x05060708);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000225 return bitmap;
226}
227SkBitmap kTestBitmap; // cannot be created during static init
228SkString kTestText("Hello World");
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000229SkPoint kTestPoints2[] = {
230 { SkIntToScalar(0), SkIntToScalar(1) },
231 { SkIntToScalar(1), SkIntToScalar(1) },
232 { SkIntToScalar(2), SkIntToScalar(1) },
233 { SkIntToScalar(3), SkIntToScalar(1) },
234 { SkIntToScalar(4), SkIntToScalar(1) },
235 { SkIntToScalar(5), SkIntToScalar(1) },
236 { SkIntToScalar(6), SkIntToScalar(1) },
237 { SkIntToScalar(7), SkIntToScalar(1) },
238 { SkIntToScalar(8), SkIntToScalar(1) },
239 { SkIntToScalar(9), SkIntToScalar(1) },
240 { SkIntToScalar(10), SkIntToScalar(1) },
241};
rmistry@google.comd6176b02012-08-23 18:14:13 +0000242
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000243
244///////////////////////////////////////////////////////////////////////////////
245// Macros for defining test steps
246
247#define TEST_STEP(NAME, FUNCTION) \
248class NAME##_TestStep : public CanvasTestStep{ \
249public: \
250 virtual void draw(SkCanvas* canvas, skiatest::Reporter* reporter) { \
251 FUNCTION (canvas, reporter, this); \
252 } \
253 virtual const char* name() const {return #NAME ;} \
254}; \
255static NAME##_TestStep NAME##_TestStepInstance;
256
edisonn@google.com77909122012-10-18 15:58:23 +0000257#define TEST_STEP_NO_PDF(NAME, FUNCTION) \
258class NAME##_TestStep : public CanvasTestStep{ \
259public: \
260 NAME##_TestStep() : CanvasTestStep(false) {} \
261 virtual void draw(SkCanvas* canvas, skiatest::Reporter* reporter) { \
262 FUNCTION (canvas, reporter, this); \
263 } \
264 virtual const char* name() const {return #NAME ;} \
265}; \
266static NAME##_TestStep NAME##_TestStepInstance;
267
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000268#define SIMPLE_TEST_STEP(NAME, CALL) \
269static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter*, \
270 CanvasTestStep*) { \
271 canvas-> CALL ; \
272} \
273TEST_STEP(NAME, NAME##TestStep )
274
275#define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
276static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter* reporter, \
277 CanvasTestStep* testStep) { \
278 REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \
279 testStep->assertMessage()); \
280} \
281TEST_STEP(NAME, NAME##TestStep )
282
283
284///////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000285// Basic test steps for most virtual methods in SkCanvas that draw or affect
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000286// the state of the canvas.
287
junov@chromium.orga907ac32012-02-24 21:54:07 +0000288SIMPLE_TEST_STEP_WITH_ASSERT(Translate,
289 translate(SkIntToScalar(1), SkIntToScalar(2)));
290SIMPLE_TEST_STEP_WITH_ASSERT(Scale,
291 scale(SkIntToScalar(1), SkIntToScalar(2)));
292SIMPLE_TEST_STEP_WITH_ASSERT(Rotate, rotate(SkIntToScalar(1)));
293SIMPLE_TEST_STEP_WITH_ASSERT(Skew,
294 skew(SkIntToScalar(1), SkIntToScalar(2)));
295SIMPLE_TEST_STEP_WITH_ASSERT(Concat, concat(kTestMatrix));
296SIMPLE_TEST_STEP(SetMatrix, setMatrix(kTestMatrix));
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000297SIMPLE_TEST_STEP(ClipRect, clipRect(kTestRect));
298SIMPLE_TEST_STEP(ClipPath, clipPath(kTestPath));
299SIMPLE_TEST_STEP(ClipRegion,
junov@chromium.orga907ac32012-02-24 21:54:07 +0000300 clipRegion(kTestRegion, SkRegion::kReplace_Op));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000301SIMPLE_TEST_STEP(Clear, clear(kTestColor));
302SIMPLE_TEST_STEP(DrawPaint, drawPaint(kTestPaint));
303SIMPLE_TEST_STEP(DrawPointsPoints, drawPoints(SkCanvas::kPoints_PointMode,
304 kTestPointCount, kTestPoints, kTestPaint));
305SIMPLE_TEST_STEP(DrawPointsLiness, drawPoints(SkCanvas::kLines_PointMode,
306 kTestPointCount, kTestPoints, kTestPaint));
307SIMPLE_TEST_STEP(DrawPointsPolygon, drawPoints(SkCanvas::kPolygon_PointMode,
308 kTestPointCount, kTestPoints, kTestPaint));
309SIMPLE_TEST_STEP(DrawRect, drawRect(kTestRect, kTestPaint));
310SIMPLE_TEST_STEP(DrawPath, drawPath(kTestPath, kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000311SIMPLE_TEST_STEP(DrawBitmap, drawBitmap(kTestBitmap, 0, 0));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000312SIMPLE_TEST_STEP(DrawBitmapPaint, drawBitmap(kTestBitmap, 0, 0, &kTestPaint));
313SIMPLE_TEST_STEP(DrawBitmapRect, drawBitmapRect(kTestBitmap, NULL, kTestRect,
314 NULL));
315SIMPLE_TEST_STEP(DrawBitmapRectSrcRect, drawBitmapRect(kTestBitmap,
316 &kTestIRect, kTestRect, NULL));
317SIMPLE_TEST_STEP(DrawBitmapRectPaint, drawBitmapRect(kTestBitmap, NULL,
318 kTestRect, &kTestPaint));
319SIMPLE_TEST_STEP(DrawBitmapMatrix, drawBitmapMatrix(kTestBitmap, kTestMatrix,
320 NULL));
321SIMPLE_TEST_STEP(DrawBitmapMatrixPaint, drawBitmapMatrix(kTestBitmap,
322 kTestMatrix, &kTestPaint));
323SIMPLE_TEST_STEP(DrawBitmapNine, drawBitmapNine(kTestBitmap, kTestIRect,
324 kTestRect, NULL));
325SIMPLE_TEST_STEP(DrawBitmapNinePaint, drawBitmapNine(kTestBitmap, kTestIRect,
326 kTestRect, &kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000327SIMPLE_TEST_STEP(DrawSprite, drawSprite(kTestBitmap, 0, 0, NULL));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000328SIMPLE_TEST_STEP(DrawSpritePaint, drawSprite(kTestBitmap, 0, 0, &kTestPaint));
329SIMPLE_TEST_STEP(DrawText, drawText(kTestText.c_str(), kTestText.size(),
330 0, 1, kTestPaint));
331SIMPLE_TEST_STEP(DrawPosText, drawPosText(kTestText.c_str(),
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000332 kTestText.size(), kTestPoints2, kTestPaint));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000333SIMPLE_TEST_STEP(DrawTextOnPath, drawTextOnPath(kTestText.c_str(),
334 kTestText.size(), kTestPath, NULL, kTestPaint));
335SIMPLE_TEST_STEP(DrawTextOnPathMatrix, drawTextOnPath(kTestText.c_str(),
336 kTestText.size(), kTestPath, &kTestMatrix, kTestPaint));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000337SIMPLE_TEST_STEP(DrawData, drawData(kTestText.c_str(), kTestText.size()));
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000338SIMPLE_TEST_STEP(BeginGroup, beginCommentGroup(kTestText.c_str()));
339SIMPLE_TEST_STEP(AddComment, addComment(kTestText.c_str(), kTestText.c_str()));
340SIMPLE_TEST_STEP(EndGroup, endCommentGroup());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000341
342///////////////////////////////////////////////////////////////////////////////
343// Complex test steps
344
rmistry@google.comd6176b02012-08-23 18:14:13 +0000345// Save/restore calls cannot be in isolated simple test steps because the test
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000346// cases that use SkPicture require that save and restore calls be balanced.
rmistry@google.comd6176b02012-08-23 18:14:13 +0000347static void SaveMatrixStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000348 skiatest::Reporter* reporter,
349 CanvasTestStep* testStep) {
350 int saveCount = canvas->getSaveCount();
351 canvas->save(SkCanvas::kMatrix_SaveFlag);
352 canvas->clipRegion(kTestRegion);
353 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
354 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000355 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000356 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000357 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000358 testStep->assertMessage());
359 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() == kTestRegion,
360 testStep->assertMessage());
361}
362TEST_STEP(SaveMatrix, SaveMatrixStep);
363
rmistry@google.comd6176b02012-08-23 18:14:13 +0000364static void SaveClipStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000365 skiatest::Reporter* reporter,
366 CanvasTestStep* testStep) {
367 int saveCount = canvas->getSaveCount();
368 canvas->save(SkCanvas::kClip_SaveFlag);
369 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
370 canvas->clipRegion(kTestRegion);
371 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000372 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000373 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000374 REPORTER_ASSERT_MESSAGE(reporter, !canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000375 testStep->assertMessage());
376 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion,
377 testStep->assertMessage());
378}
379TEST_STEP(SaveClip, SaveClipStep);
380
rmistry@google.comd6176b02012-08-23 18:14:13 +0000381static void SaveMatrixClipStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000382 skiatest::Reporter* reporter,
383 CanvasTestStep* testStep) {
384 int saveCount = canvas->getSaveCount();
385 canvas->save(SkCanvas::kMatrixClip_SaveFlag);
386 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
387 canvas->clipRegion(kTestRegion);
388 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000389 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000390 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000391 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000392 testStep->assertMessage());
393 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion,
394 testStep->assertMessage());
395}
396TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
397
rmistry@google.comd6176b02012-08-23 18:14:13 +0000398static void SaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000399 skiatest::Reporter* reporter,
400 CanvasTestStep* testStep) {
401 int saveCount = canvas->getSaveCount();
402 canvas->saveLayer(NULL, NULL);
403 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000404 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000405 testStep->assertMessage());
406}
407TEST_STEP(SaveLayer, SaveLayerStep);
408
rmistry@google.comd6176b02012-08-23 18:14:13 +0000409static void BoundedSaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000410 skiatest::Reporter* reporter,
411 CanvasTestStep* testStep) {
412 int saveCount = canvas->getSaveCount();
413 canvas->saveLayer(&kTestRect, NULL);
414 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000415 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000416 testStep->assertMessage());
417}
418TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
419
rmistry@google.comd6176b02012-08-23 18:14:13 +0000420static void PaintSaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000421 skiatest::Reporter* reporter,
422 CanvasTestStep* testStep) {
423 int saveCount = canvas->getSaveCount();
424 canvas->saveLayer(NULL, &kTestPaint);
425 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000426 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000427 testStep->assertMessage());
428}
429TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
430
rmistry@google.comd6176b02012-08-23 18:14:13 +0000431static void TwoClipOpsStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000432 skiatest::Reporter*,
433 CanvasTestStep*) {
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000434 // This test exercises a functionality in SkPicture that leads to the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000435 // recording of restore offset placeholders. This test will trigger an
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000436 // assertion at playback time if the placeholders are not properly
437 // filled when the recording ends.
438 canvas->clipRect(kTestRect);
439 canvas->clipRegion(kTestRegion);
440}
441TEST_STEP(TwoClipOps, TwoClipOpsStep);
442
epoger@google.com94fa43c2012-04-11 17:51:01 +0000443// exercise fix for http://code.google.com/p/skia/issues/detail?id=560
444// ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
rmistry@google.comd6176b02012-08-23 18:14:13 +0000445static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000446 skiatest::Reporter*,
447 CanvasTestStep*) {
epoger@google.com94fa43c2012-04-11 17:51:01 +0000448 SkPaint paint;
449 paint.setStrokeWidth(SkIntToScalar(1));
450 paint.setStyle(SkPaint::kStroke_Style);
451
452 SkPath path;
453 SkPoint pt1 = { 0, 0 };
454 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
455 SkPoint pt3 = { SkIntToScalar(1), 0 };
456 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
457 path.moveTo(pt1);
458 path.lineTo(pt2);
459 path.lineTo(pt3);
460 path.lineTo(pt4);
461
462 canvas->drawPath(path, paint);
463}
464TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
465
rmistry@google.comd6176b02012-08-23 18:14:13 +0000466static void DrawVerticesShaderTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000467 skiatest::Reporter*,
468 CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000469 SkPoint pts[4];
470 pts[0].set(0, 0);
471 pts[1].set(SkIntToScalar(kWidth), 0);
472 pts[2].set(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
473 pts[3].set(0, SkIntToScalar(kHeight));
474 SkPaint paint;
475 SkShader* shader = SkShader::CreateBitmapShader(kTestBitmap,
476 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
477 paint.setShader(shader)->unref();
478 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
479 NULL, NULL, NULL, 0, paint);
480}
edisonn@google.com77909122012-10-18 15:58:23 +0000481// NYI: issue 240.
482TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000483
rmistry@google.comd6176b02012-08-23 18:14:13 +0000484static void DrawPictureTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000485 skiatest::Reporter*,
486 CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000487 SkPicture* testPicture = SkNEW_ARGS(SkPicture, ());
488 SkAutoUnref aup(testPicture);
489 SkCanvas* testCanvas = testPicture->beginRecording(kWidth, kHeight);
490 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
491 testCanvas->clipRect(kTestRect);
492 testCanvas->drawRect(kTestRect, kTestPaint);
493 canvas->drawPicture(*testPicture);
494}
495TEST_STEP(DrawPicture, DrawPictureTestStep);
496
rmistry@google.comd6176b02012-08-23 18:14:13 +0000497static void SaveRestoreTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000498 skiatest::Reporter* reporter,
499 CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000500 int baseSaveCount = canvas->getSaveCount();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000501 int n = canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000502 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount == n, testStep->assertMessage());
503 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000504 testStep->assertMessage());
505 canvas->save();
506 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000507 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000508 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000509 canvas->restoreToCount(baseSaveCount + 1);
510 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000511 testStep->assertMessage());
512
513 // should this pin to 1, or be a no-op, or crash?
514 canvas->restoreToCount(0);
515 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
516 testStep->assertMessage());
517}
518TEST_STEP(SaveRestore, SaveRestoreTestStep);
519
rmistry@google.comd6176b02012-08-23 18:14:13 +0000520static void DrawLayerTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000521 skiatest::Reporter* reporter,
522 CanvasTestStep* testStep) {
523 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
524 testStep->assertMessage());
525 canvas->save();
526 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
527 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000528 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000529
reed@google.com7c202932011-12-14 18:48:05 +0000530 const SkRect* bounds = NULL; // null means include entire bounds
531 const SkPaint* paint = NULL;
532
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000533 canvas->saveLayer(bounds, paint);
534 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
535 testStep->assertMessage());
536 canvas->restore();
537 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
538 testStep->assertMessage());
reed@google.com7c202932011-12-14 18:48:05 +0000539
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000540 canvas->saveLayer(bounds, paint);
541 canvas->saveLayer(bounds, paint);
542 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
543 testStep->assertMessage());
544 canvas->restore();
545 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
546 testStep->assertMessage());
547 canvas->restore();
reed@google.com7c202932011-12-14 18:48:05 +0000548 // now layer count should be 0
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000549 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
550 testStep->assertMessage());
551}
552TEST_STEP(DrawLayer, DrawLayerTestStep);
reed@google.com3b3e8952012-08-16 20:53:31 +0000553
554static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000555 skiatest::Reporter*,
556 CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000557 // This test step challenges the TestDeferredCanvasStateConsistency
558 // test cases because the opaque paint can trigger an optimization
559 // that discards previously recorded commands. The challenge is to maintain
560 // correct clip and matrix stack state.
561 canvas->resetMatrix();
562 canvas->rotate(SkIntToScalar(30));
563 canvas->save();
564 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
565 canvas->save();
566 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
567 SkPaint paint;
568 paint.setColor(0xFFFFFFFF);
569 canvas->drawPaint(paint);
570 canvas->restore();
571 canvas->restore();
572}
573TEST_STEP(NestedSaveRestoreWithSolidPaint, \
574 NestedSaveRestoreWithSolidPaintTestStep);
575
576static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000577 skiatest::Reporter*,
578 CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000579 // This test step challenges the TestDeferredCanvasStateConsistency
580 // test case because the canvas flush on a deferred canvas will
581 // reset the recording session. The challenge is to maintain correct
582 // clip and matrix stack state on the playback canvas.
583 canvas->resetMatrix();
584 canvas->rotate(SkIntToScalar(30));
585 canvas->save();
586 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
587 canvas->save();
588 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
589 canvas->drawRect(kTestRect,kTestPaint);
590 canvas->flush();
591 canvas->restore();
592 canvas->restore();
593}
594TEST_STEP(NestedSaveRestoreWithFlush, \
595 NestedSaveRestoreWithFlushTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000596
597static void AssertCanvasStatesEqual(skiatest::Reporter* reporter,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000598 const SkCanvas* canvas1,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000599 const SkCanvas* canvas2,
600 CanvasTestStep* testStep) {
601 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
602 canvas2->getDeviceSize(), testStep->assertMessage());
603 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
604 canvas2->getSaveCount(), testStep->assertMessage());
605 REPORTER_ASSERT_MESSAGE(reporter, canvas1->isDrawingToLayer() ==
606 canvas2->isDrawingToLayer(), testStep->assertMessage());
reed@google.com3b3e8952012-08-16 20:53:31 +0000607
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000608 SkRect bounds1, bounds2;
609 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.com3b3e8952012-08-16 20:53:31 +0000610 canvas1->getClipBounds(&bounds1) == canvas2->getClipBounds(&bounds2),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000611 testStep->assertMessage());
612 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
reed@google.com3b3e8952012-08-16 20:53:31 +0000613 testStep->assertMessage());
614
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000615 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
616 canvas2->getDrawFilter(), testStep->assertMessage());
617 SkIRect deviceBounds1, deviceBounds2;
618 REPORTER_ASSERT_MESSAGE(reporter,
619 canvas1->getClipDeviceBounds(&deviceBounds1) ==
620 canvas2->getClipDeviceBounds(&deviceBounds2),
621 testStep->assertMessage());
622 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2,
623 testStep->assertMessage());
624 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getBounder() ==
625 canvas2->getBounder(), testStep->assertMessage());
626 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
627 canvas2->getTotalMatrix(), testStep->assertMessage());
628 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getClipType() ==
629 canvas2->getClipType(), testStep->assertMessage());
630 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalClip() ==
631 canvas2->getTotalClip(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000632
633 // The following test code is commented out because the test fails when
rmistry@google.comd6176b02012-08-23 18:14:13 +0000634 // the canvas is an SkPictureRecord or SkDeferredCanvas
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000635 // Issue: http://code.google.com/p/skia/issues/detail?id=498
636 // Also, creating a LayerIter on an SkProxyCanvas crashes
637 // Issue: http://code.google.com/p/skia/issues/detail?id=499
638 /*
639 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
640 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
641 while (!layerIter1.done() && !layerIter2.done()) {
642 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
643 layerIter2.matrix(), testStep->assertMessage());
644 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
645 layerIter2.clip(), testStep->assertMessage());
646 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
647 layerIter2.paint(), testStep->assertMessage());
648 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
649 layerIter2.x(), testStep->assertMessage());
650 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
651 layerIter2.y(), testStep->assertMessage());
652 layerIter1.next();
653 layerIter2.next();
654 }
655 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
656 testStep->assertMessage());
657 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
658 testStep->assertMessage());
659 */
660}
661
662// The following class groups static functions that need to access
663// the privates members of SkPictureRecord
664class SkPictureTester {
665private:
reed@google.come2589ae2012-07-10 19:38:01 +0000666 static int EQ(const SkFlatData* a, const SkFlatData* b) {
667 return *a == *b;
668 }
669
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000670 static void AssertFlattenedObjectsEqual(
671 SkPictureRecord* referenceRecord,
672 SkPictureRecord* testRecord,
673 skiatest::Reporter* reporter,
674 CanvasTestStep* testStep) {
675
676 REPORTER_ASSERT_MESSAGE(reporter,
djsollen@google.comc9ab9872012-08-29 18:52:07 +0000677 referenceRecord->fBitmapHeap->count() ==
678 testRecord->fBitmapHeap->count(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000679 REPORTER_ASSERT_MESSAGE(reporter,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000680 referenceRecord->fPaints.count() ==
681 testRecord->fPaints.count(), testStep->assertMessage());
682 for (int i = 0; i < referenceRecord->fPaints.count(); ++i) {
683 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.come2589ae2012-07-10 19:38:01 +0000684 EQ(referenceRecord->fPaints[i], testRecord->fPaints[i]),
685 testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000686 }
687 REPORTER_ASSERT_MESSAGE(reporter,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000688 !referenceRecord->fPathHeap ==
689 !testRecord->fPathHeap,
690 testStep->assertMessage());
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000691 // The following tests are commented out because they currently
692 // fail. Issue: http://code.google.com/p/skia/issues/detail?id=507
693 /*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000694 if (referenceRecord->fPathHeap) {
695 REPORTER_ASSERT_MESSAGE(reporter,
696 referenceRecord->fPathHeap->count() ==
697 testRecord->fPathHeap->count(),
698 testStep->assertMessage());
699 for (int i = 0; i < referenceRecord->fPathHeap->count(); ++i) {
700 REPORTER_ASSERT_MESSAGE(reporter,
701 (*referenceRecord->fPathHeap)[i] ==
702 (*testRecord->fPathHeap)[i], testStep->assertMessage());
703 }
704 }
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000705 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000706
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000707 }
708
709public:
710
rmistry@google.comd6176b02012-08-23 18:14:13 +0000711 static void TestPictureFlattenedObjectReuse(skiatest::Reporter* reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000712 CanvasTestStep* testStep,
713 uint32_t recordFlags) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000714 // Verify that when a test step is executed twice, no extra resources
715 // are flattened during the second execution
716 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
717 SkPicture referencePicture;
718 SkCanvas* referenceCanvas = referencePicture.beginRecording(kWidth,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000719 kHeight, recordFlags);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000720 testStep->draw(referenceCanvas, reporter);
721 SkPicture testPicture;
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000722 SkCanvas* testCanvas = testPicture.beginRecording(kWidth,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000723 kHeight, recordFlags);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000724 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000725 testStep->setAssertMessageFormat(kPictureSecondDrawAssertMessageFormat);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000726 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000727
728 SkPictureRecord* referenceRecord = static_cast<SkPictureRecord*>(
729 referenceCanvas);
730 SkPictureRecord* testRecord = static_cast<SkPictureRecord*>(
731 testCanvas);
732 testStep->setAssertMessageFormat(kPictureResourceReuseMessageFormat);
733 AssertFlattenedObjectsEqual(referenceRecord, testRecord,
junov@chromium.org76b9c4b2012-02-22 21:24:41 +0000734 reporter, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000735 }
736};
737
edisonn@google.com77909122012-10-18 15:58:23 +0000738static void TestPdfDevice(skiatest::Reporter* reporter,
739 CanvasTestStep* testStep) {
740 SkISize pageSize = SkISize::Make(kWidth, kHeight);
741 SkPDFDevice device(pageSize, pageSize, SkMatrix::I());
742 SkCanvas canvas(&device);
743 testStep->setAssertMessageFormat(kPdfAssertMessageFormat);
744 testStep->draw(&canvas, reporter);
745 SkPDFDocument doc;
746 doc.appendPage(&device);
747 SkDynamicMemoryWStream stream;
748 doc.emitPDF(&stream);
749}
750
junov@chromium.org88e29142012-08-07 16:48:22 +0000751// The following class groups static functions that need to access
752// the privates members of SkDeferredCanvas
753class SkDeferredCanvasTester {
754public:
755 static void TestDeferredCanvasStateConsistency(
756 skiatest::Reporter* reporter,
757 CanvasTestStep* testStep,
junov@chromium.orgfb103892012-09-20 19:35:43 +0000758 const SkCanvas& referenceCanvas, bool silent) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000759
reed@google.com28183b42014-02-04 15:34:10 +0000760 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
761 SkAutoTUnref<SkDeferredCanvas> deferredCanvas(SkDeferredCanvas::Create(surface.get()));
762
junov@chromium.org88e29142012-08-07 16:48:22 +0000763 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
junov@chromium.org66070a52013-05-28 17:39:08 +0000764 testStep->draw(deferredCanvas, reporter);
junov@chromium.org88e29142012-08-07 16:48:22 +0000765 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
junov@chromium.org66070a52013-05-28 17:39:08 +0000766 AssertCanvasStatesEqual(reporter, deferredCanvas, &referenceCanvas,
junov@chromium.org88e29142012-08-07 16:48:22 +0000767 testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000768
junov@chromium.orgfb103892012-09-20 19:35:43 +0000769 if (silent) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000770 deferredCanvas->silentFlush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000771 } else {
junov@chromium.org66070a52013-05-28 17:39:08 +0000772 deferredCanvas->flush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000773 }
774
skia.committer@gmail.com4c5ea442012-09-21 02:01:01 +0000775 testStep->setAssertMessageFormat(
junov@chromium.orgfb103892012-09-20 19:35:43 +0000776 silent ? kDeferredPostSilentFlushPlaybackAssertMessageFormat :
junov@chromium.org88e29142012-08-07 16:48:22 +0000777 kDeferredPostFlushPlaybackAssertMessageFormat);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000778 AssertCanvasStatesEqual(reporter,
junov@chromium.org66070a52013-05-28 17:39:08 +0000779 deferredCanvas->immediateCanvas(),
junov@chromium.org88e29142012-08-07 16:48:22 +0000780 &referenceCanvas, testStep);
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000781
junov@chromium.org88e29142012-08-07 16:48:22 +0000782 // Verified that deferred canvas state is not affected by flushing
783 // pending draw operations
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000784
junov@chromium.org88e29142012-08-07 16:48:22 +0000785 // The following test code is commented out because it currently fails.
786 // Issue: http://code.google.com/p/skia/issues/detail?id=496
787 /*
788 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
789 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
790 testStep);
791 */
792 }
793};
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000794
caryclark@google.com42639cd2012-06-06 12:03:39 +0000795// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000796static void TestProxyCanvasStateConsistency(
797 skiatest::Reporter* reporter,
798 CanvasTestStep* testStep,
799 const SkCanvas& referenceCanvas) {
800
801 SkBitmap indirectStore;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000802 createBitmap(&indirectStore, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000803 SkBitmapDevice indirectDevice(indirectStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000804 SkCanvas indirectCanvas(&indirectDevice);
805 SkProxyCanvas proxyCanvas(&indirectCanvas);
806 testStep->setAssertMessageFormat(kProxyDrawAssertMessageFormat);
807 testStep->draw(&proxyCanvas, reporter);
808 // Verify that the SkProxyCanvas reports consitent state
809 testStep->setAssertMessageFormat(kProxyStateAssertMessageFormat);
810 AssertCanvasStatesEqual(reporter, &proxyCanvas, &referenceCanvas,
811 testStep);
812 // Verify that the indirect canvas reports consitent state
813 testStep->setAssertMessageFormat(kProxyIndirectStateAssertMessageFormat);
814 AssertCanvasStatesEqual(reporter, &indirectCanvas, &referenceCanvas,
815 testStep);
816}
817
caryclark@google.com42639cd2012-06-06 12:03:39 +0000818// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000819static void TestNWayCanvasStateConsistency(
820 skiatest::Reporter* reporter,
821 CanvasTestStep* testStep,
822 const SkCanvas& referenceCanvas) {
823
824 SkBitmap indirectStore1;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000825 createBitmap(&indirectStore1, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000826 SkBitmapDevice indirectDevice1(indirectStore1);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000827 SkCanvas indirectCanvas1(&indirectDevice1);
828
829 SkBitmap indirectStore2;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000830 createBitmap(&indirectStore2, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000831 SkBitmapDevice indirectDevice2(indirectStore2);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000832 SkCanvas indirectCanvas2(&indirectDevice2);
833
djsollen@google.comf0a062b2012-05-01 16:50:25 +0000834 SkISize canvasSize = referenceCanvas.getDeviceSize();
835 SkNWayCanvas nWayCanvas(canvasSize.width(), canvasSize.height());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000836 nWayCanvas.addCanvas(&indirectCanvas1);
837 nWayCanvas.addCanvas(&indirectCanvas2);
838
839 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
840 testStep->draw(&nWayCanvas, reporter);
841 // Verify that the SkProxyCanvas reports consitent state
842 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
843 AssertCanvasStatesEqual(reporter, &nWayCanvas, &referenceCanvas,
844 testStep);
845 // Verify that the indirect canvases report consitent state
846 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
847 AssertCanvasStatesEqual(reporter, &indirectCanvas1, &referenceCanvas,
848 testStep);
849 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
850 AssertCanvasStatesEqual(reporter, &indirectCanvas2, &referenceCanvas,
851 testStep);
852}
853
854/*
855 * This sub-test verifies that the test step passes when executed
856 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
857 * that the all canvas derivatives report the same state as an SkCanvas
858 * after having executed the test step.
859 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000860static void TestOverrideStateConsistency(skiatest::Reporter* reporter,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000861 CanvasTestStep* testStep) {
862 SkBitmap referenceStore;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000863 createBitmap(&referenceStore, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000864 SkBitmapDevice referenceDevice(referenceStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000865 SkCanvas referenceCanvas(&referenceDevice);
866 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
867 testStep->draw(&referenceCanvas, reporter);
868
junov@chromium.orgfb103892012-09-20 19:35:43 +0000869 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, false);
870
871 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, true);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000872
caryclark@google.com42639cd2012-06-06 12:03:39 +0000873 // The following test code is disabled because SkProxyCanvas is
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000874 // missing a lot of virtual overrides on get* methods, which are used
875 // to verify canvas state.
876 // Issue: http://code.google.com/p/skia/issues/detail?id=500
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000877
caryclark@google.com42639cd2012-06-06 12:03:39 +0000878 if (false) { // avoid bit rot, suppress warning
879 TestProxyCanvasStateConsistency(reporter, testStep, referenceCanvas);
880 }
881
882 // The following test code is disabled because SkNWayCanvas does not
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000883 // report correct clipping and device bounds information
884 // Issue: http://code.google.com/p/skia/issues/detail?id=501
caryclark@google.com42639cd2012-06-06 12:03:39 +0000885
886 if (false) { // avoid bit rot, suppress warning
887 TestNWayCanvasStateConsistency(reporter, testStep, referenceCanvas);
888 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000889
caryclark@google.com42639cd2012-06-06 12:03:39 +0000890 if (false) { // avoid bit rot, suppress warning
891 test_clipVisitor(reporter, &referenceCanvas);
892 }
reed@google.com7c202932011-12-14 18:48:05 +0000893}
reed@google.com37f3ae02011-11-28 16:06:04 +0000894
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000895static void test_newraster(skiatest::Reporter* reporter) {
896 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
897 SkCanvas* canvas = SkCanvas::NewRaster(info);
898 REPORTER_ASSERT(reporter, canvas);
899
900 SkImageInfo info2;
901 size_t rowBytes;
902 const SkPMColor* addr = (const SkPMColor*)canvas->peekPixels(&info2, &rowBytes);
903 REPORTER_ASSERT(reporter, addr);
904 REPORTER_ASSERT(reporter, info == info2);
905 for (int y = 0; y < info.height(); ++y) {
906 for (int x = 0; x < info.width(); ++x) {
907 REPORTER_ASSERT(reporter, 0 == addr[x]);
908 }
909 addr = (const SkPMColor*)((const char*)addr + rowBytes);
910 }
911 SkDELETE(canvas);
912
913 // now try a deliberately bad info
914 info.fWidth = -1;
915 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRaster(info));
916
917 // too big
918 info.fWidth = 1 << 30;
919 info.fHeight = 1 << 30;
920 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRaster(info));
921
922 // not a valid pixel type
923 info.fWidth = info.fHeight = 10;
924 info.fColorType = kUnknown_SkColorType;
925 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRaster(info));
926
927 // We should succeed with a zero-sized valid info
928 info = SkImageInfo::MakeN32Premul(0, 0);
929 canvas = SkCanvas::NewRaster(info);
930 REPORTER_ASSERT(reporter, canvas);
931 SkDELETE(canvas);
932}
933
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000934DEF_TEST(Canvas, reporter) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000935 // Init global here because bitmap pixels cannot be alocated during
936 // static initialization
937 kTestBitmap = testBitmap();
reed@google.com37f3ae02011-11-28 16:06:04 +0000938
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000939 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
940 TestOverrideStateConsistency(reporter, testStepArray()[testStep]);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000941 SkPictureTester::TestPictureFlattenedObjectReuse(reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000942 testStepArray()[testStep], 0);
edisonn@google.com77909122012-10-18 15:58:23 +0000943 if (testStepArray()[testStep]->enablePdfTesting()) {
944 TestPdfDevice(reporter, testStepArray()[testStep]);
945 }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000946 }
junov@chromium.orgcd62ecf2012-08-02 17:43:25 +0000947
948 // Explicitly call reset(), so we don't leak the pixels (since kTestBitmap is a global)
949 kTestBitmap.reset();
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000950
951 test_newraster(reporter);
reed@google.com37f3ae02011-11-28 16:06:04 +0000952}