blob: b3a63f1cbec7c65ba01aea21f6528e78282e2bcc [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();
commit-bot@chromium.org8c2ee592014-03-07 18:42:15 +0000199static SkPath test_path() {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000200 SkPath path;
201 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
202 SkIntToScalar(2), SkIntToScalar(1)));
203 return path;
204}
commit-bot@chromium.org8c2ee592014-03-07 18:42:15 +0000205const SkPath kTestPath = test_path();
206static SkPath test_nearly_zero_length_path() {
207 SkPath path;
208 SkPoint pt1 = { 0, 0 };
209 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
210 SkPoint pt3 = { SkIntToScalar(1), 0 };
211 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
212 path.moveTo(pt1);
213 path.lineTo(pt2);
214 path.lineTo(pt3);
215 path.lineTo(pt4);
216 return path;
217}
218const SkPath kNearlyZeroLengthPath = test_nearly_zero_length_path();
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000219static SkRegion testRegion() {
220 SkRegion region;
221 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
222 region.setRect(rect);
223 return region;
224}
225const SkIRect kTestIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
226const SkRegion kTestRegion = testRegion();
227const SkColor kTestColor = 0x01020304;
228const SkPaint kTestPaint;
229const SkPoint kTestPoints[3] = {
230 {SkIntToScalar(0), SkIntToScalar(0)},
231 {SkIntToScalar(2), SkIntToScalar(1)},
232 {SkIntToScalar(0), SkIntToScalar(2)}
233};
234const size_t kTestPointCount = 3;
235static SkBitmap testBitmap() {
236 SkBitmap bitmap;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000237 createBitmap(&bitmap, 0x05060708);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000238 return bitmap;
239}
240SkBitmap kTestBitmap; // cannot be created during static init
241SkString kTestText("Hello World");
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000242SkPoint kTestPoints2[] = {
243 { SkIntToScalar(0), SkIntToScalar(1) },
244 { SkIntToScalar(1), SkIntToScalar(1) },
245 { SkIntToScalar(2), SkIntToScalar(1) },
246 { SkIntToScalar(3), SkIntToScalar(1) },
247 { SkIntToScalar(4), SkIntToScalar(1) },
248 { SkIntToScalar(5), SkIntToScalar(1) },
249 { SkIntToScalar(6), SkIntToScalar(1) },
250 { SkIntToScalar(7), SkIntToScalar(1) },
251 { SkIntToScalar(8), SkIntToScalar(1) },
252 { SkIntToScalar(9), SkIntToScalar(1) },
253 { SkIntToScalar(10), SkIntToScalar(1) },
254};
rmistry@google.comd6176b02012-08-23 18:14:13 +0000255
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000256
257///////////////////////////////////////////////////////////////////////////////
258// Macros for defining test steps
259
260#define TEST_STEP(NAME, FUNCTION) \
261class NAME##_TestStep : public CanvasTestStep{ \
262public: \
263 virtual void draw(SkCanvas* canvas, skiatest::Reporter* reporter) { \
264 FUNCTION (canvas, reporter, this); \
265 } \
266 virtual const char* name() const {return #NAME ;} \
267}; \
268static NAME##_TestStep NAME##_TestStepInstance;
269
edisonn@google.com77909122012-10-18 15:58:23 +0000270#define TEST_STEP_NO_PDF(NAME, FUNCTION) \
271class NAME##_TestStep : public CanvasTestStep{ \
272public: \
273 NAME##_TestStep() : CanvasTestStep(false) {} \
274 virtual void draw(SkCanvas* canvas, skiatest::Reporter* reporter) { \
275 FUNCTION (canvas, reporter, this); \
276 } \
277 virtual const char* name() const {return #NAME ;} \
278}; \
279static NAME##_TestStep NAME##_TestStepInstance;
280
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000281#define SIMPLE_TEST_STEP(NAME, CALL) \
282static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter*, \
283 CanvasTestStep*) { \
284 canvas-> CALL ; \
285} \
286TEST_STEP(NAME, NAME##TestStep )
287
288#define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
289static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter* reporter, \
290 CanvasTestStep* testStep) { \
291 REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \
292 testStep->assertMessage()); \
293} \
294TEST_STEP(NAME, NAME##TestStep )
295
296
297///////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000298// Basic test steps for most virtual methods in SkCanvas that draw or affect
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000299// the state of the canvas.
300
junov@chromium.orga907ac32012-02-24 21:54:07 +0000301SIMPLE_TEST_STEP_WITH_ASSERT(Translate,
302 translate(SkIntToScalar(1), SkIntToScalar(2)));
303SIMPLE_TEST_STEP_WITH_ASSERT(Scale,
304 scale(SkIntToScalar(1), SkIntToScalar(2)));
305SIMPLE_TEST_STEP_WITH_ASSERT(Rotate, rotate(SkIntToScalar(1)));
306SIMPLE_TEST_STEP_WITH_ASSERT(Skew,
307 skew(SkIntToScalar(1), SkIntToScalar(2)));
308SIMPLE_TEST_STEP_WITH_ASSERT(Concat, concat(kTestMatrix));
309SIMPLE_TEST_STEP(SetMatrix, setMatrix(kTestMatrix));
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000310SIMPLE_TEST_STEP(ClipRect, clipRect(kTestRect));
311SIMPLE_TEST_STEP(ClipPath, clipPath(kTestPath));
312SIMPLE_TEST_STEP(ClipRegion,
junov@chromium.orga907ac32012-02-24 21:54:07 +0000313 clipRegion(kTestRegion, SkRegion::kReplace_Op));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000314SIMPLE_TEST_STEP(Clear, clear(kTestColor));
315SIMPLE_TEST_STEP(DrawPaint, drawPaint(kTestPaint));
316SIMPLE_TEST_STEP(DrawPointsPoints, drawPoints(SkCanvas::kPoints_PointMode,
317 kTestPointCount, kTestPoints, kTestPaint));
318SIMPLE_TEST_STEP(DrawPointsLiness, drawPoints(SkCanvas::kLines_PointMode,
319 kTestPointCount, kTestPoints, kTestPaint));
320SIMPLE_TEST_STEP(DrawPointsPolygon, drawPoints(SkCanvas::kPolygon_PointMode,
321 kTestPointCount, kTestPoints, kTestPaint));
322SIMPLE_TEST_STEP(DrawRect, drawRect(kTestRect, kTestPaint));
323SIMPLE_TEST_STEP(DrawPath, drawPath(kTestPath, kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000324SIMPLE_TEST_STEP(DrawBitmap, drawBitmap(kTestBitmap, 0, 0));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000325SIMPLE_TEST_STEP(DrawBitmapPaint, drawBitmap(kTestBitmap, 0, 0, &kTestPaint));
326SIMPLE_TEST_STEP(DrawBitmapRect, drawBitmapRect(kTestBitmap, NULL, kTestRect,
327 NULL));
328SIMPLE_TEST_STEP(DrawBitmapRectSrcRect, drawBitmapRect(kTestBitmap,
329 &kTestIRect, kTestRect, NULL));
330SIMPLE_TEST_STEP(DrawBitmapRectPaint, drawBitmapRect(kTestBitmap, NULL,
331 kTestRect, &kTestPaint));
332SIMPLE_TEST_STEP(DrawBitmapMatrix, drawBitmapMatrix(kTestBitmap, kTestMatrix,
333 NULL));
334SIMPLE_TEST_STEP(DrawBitmapMatrixPaint, drawBitmapMatrix(kTestBitmap,
335 kTestMatrix, &kTestPaint));
336SIMPLE_TEST_STEP(DrawBitmapNine, drawBitmapNine(kTestBitmap, kTestIRect,
337 kTestRect, NULL));
338SIMPLE_TEST_STEP(DrawBitmapNinePaint, drawBitmapNine(kTestBitmap, kTestIRect,
339 kTestRect, &kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000340SIMPLE_TEST_STEP(DrawSprite, drawSprite(kTestBitmap, 0, 0, NULL));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000341SIMPLE_TEST_STEP(DrawSpritePaint, drawSprite(kTestBitmap, 0, 0, &kTestPaint));
342SIMPLE_TEST_STEP(DrawText, drawText(kTestText.c_str(), kTestText.size(),
343 0, 1, kTestPaint));
344SIMPLE_TEST_STEP(DrawPosText, drawPosText(kTestText.c_str(),
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000345 kTestText.size(), kTestPoints2, kTestPaint));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000346SIMPLE_TEST_STEP(DrawTextOnPath, drawTextOnPath(kTestText.c_str(),
347 kTestText.size(), kTestPath, NULL, kTestPaint));
348SIMPLE_TEST_STEP(DrawTextOnPathMatrix, drawTextOnPath(kTestText.c_str(),
349 kTestText.size(), kTestPath, &kTestMatrix, kTestPaint));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000350SIMPLE_TEST_STEP(DrawData, drawData(kTestText.c_str(), kTestText.size()));
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000351SIMPLE_TEST_STEP(BeginGroup, beginCommentGroup(kTestText.c_str()));
352SIMPLE_TEST_STEP(AddComment, addComment(kTestText.c_str(), kTestText.c_str()));
353SIMPLE_TEST_STEP(EndGroup, endCommentGroup());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000354
355///////////////////////////////////////////////////////////////////////////////
356// Complex test steps
357
rmistry@google.comd6176b02012-08-23 18:14:13 +0000358// Save/restore calls cannot be in isolated simple test steps because the test
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000359// cases that use SkPicture require that save and restore calls be balanced.
rmistry@google.comd6176b02012-08-23 18:14:13 +0000360static void SaveMatrixStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000361 skiatest::Reporter* reporter,
362 CanvasTestStep* testStep) {
363 int saveCount = canvas->getSaveCount();
364 canvas->save(SkCanvas::kMatrix_SaveFlag);
365 canvas->clipRegion(kTestRegion);
366 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
367 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000368 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000369 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000370 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000371 testStep->assertMessage());
372 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() == kTestRegion,
373 testStep->assertMessage());
374}
375TEST_STEP(SaveMatrix, SaveMatrixStep);
376
rmistry@google.comd6176b02012-08-23 18:14:13 +0000377static void SaveClipStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000378 skiatest::Reporter* reporter,
379 CanvasTestStep* testStep) {
380 int saveCount = canvas->getSaveCount();
381 canvas->save(SkCanvas::kClip_SaveFlag);
382 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
383 canvas->clipRegion(kTestRegion);
384 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000385 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000386 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000387 REPORTER_ASSERT_MESSAGE(reporter, !canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000388 testStep->assertMessage());
389 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion,
390 testStep->assertMessage());
391}
392TEST_STEP(SaveClip, SaveClipStep);
393
rmistry@google.comd6176b02012-08-23 18:14:13 +0000394static void SaveMatrixClipStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000395 skiatest::Reporter* reporter,
396 CanvasTestStep* testStep) {
397 int saveCount = canvas->getSaveCount();
398 canvas->save(SkCanvas::kMatrixClip_SaveFlag);
399 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
400 canvas->clipRegion(kTestRegion);
401 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000402 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000403 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000404 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000405 testStep->assertMessage());
406 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion,
407 testStep->assertMessage());
408}
409TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
410
rmistry@google.comd6176b02012-08-23 18:14:13 +0000411static void SaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000412 skiatest::Reporter* reporter,
413 CanvasTestStep* testStep) {
414 int saveCount = canvas->getSaveCount();
415 canvas->saveLayer(NULL, NULL);
416 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000417 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000418 testStep->assertMessage());
419}
420TEST_STEP(SaveLayer, SaveLayerStep);
421
rmistry@google.comd6176b02012-08-23 18:14:13 +0000422static void BoundedSaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000423 skiatest::Reporter* reporter,
424 CanvasTestStep* testStep) {
425 int saveCount = canvas->getSaveCount();
426 canvas->saveLayer(&kTestRect, NULL);
427 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000428 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000429 testStep->assertMessage());
430}
431TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
432
rmistry@google.comd6176b02012-08-23 18:14:13 +0000433static void PaintSaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000434 skiatest::Reporter* reporter,
435 CanvasTestStep* testStep) {
436 int saveCount = canvas->getSaveCount();
437 canvas->saveLayer(NULL, &kTestPaint);
438 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000439 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000440 testStep->assertMessage());
441}
442TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
443
rmistry@google.comd6176b02012-08-23 18:14:13 +0000444static void TwoClipOpsStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000445 skiatest::Reporter*,
446 CanvasTestStep*) {
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000447 // This test exercises a functionality in SkPicture that leads to the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000448 // recording of restore offset placeholders. This test will trigger an
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000449 // assertion at playback time if the placeholders are not properly
450 // filled when the recording ends.
451 canvas->clipRect(kTestRect);
452 canvas->clipRegion(kTestRegion);
453}
454TEST_STEP(TwoClipOps, TwoClipOpsStep);
455
epoger@google.com94fa43c2012-04-11 17:51:01 +0000456// exercise fix for http://code.google.com/p/skia/issues/detail?id=560
457// ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
rmistry@google.comd6176b02012-08-23 18:14:13 +0000458static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000459 skiatest::Reporter*,
460 CanvasTestStep*) {
epoger@google.com94fa43c2012-04-11 17:51:01 +0000461 SkPaint paint;
462 paint.setStrokeWidth(SkIntToScalar(1));
463 paint.setStyle(SkPaint::kStroke_Style);
464
commit-bot@chromium.org8c2ee592014-03-07 18:42:15 +0000465 canvas->drawPath(kNearlyZeroLengthPath, paint);
epoger@google.com94fa43c2012-04-11 17:51:01 +0000466}
467TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
468
rmistry@google.comd6176b02012-08-23 18:14:13 +0000469static void DrawVerticesShaderTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000470 skiatest::Reporter*,
471 CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000472 SkPoint pts[4];
473 pts[0].set(0, 0);
474 pts[1].set(SkIntToScalar(kWidth), 0);
475 pts[2].set(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
476 pts[3].set(0, SkIntToScalar(kHeight));
477 SkPaint paint;
478 SkShader* shader = SkShader::CreateBitmapShader(kTestBitmap,
479 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
480 paint.setShader(shader)->unref();
481 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
482 NULL, NULL, NULL, 0, paint);
483}
edisonn@google.com77909122012-10-18 15:58:23 +0000484// NYI: issue 240.
485TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000486
rmistry@google.comd6176b02012-08-23 18:14:13 +0000487static void DrawPictureTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000488 skiatest::Reporter*,
489 CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000490 SkPicture* testPicture = SkNEW_ARGS(SkPicture, ());
491 SkAutoUnref aup(testPicture);
492 SkCanvas* testCanvas = testPicture->beginRecording(kWidth, kHeight);
493 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
494 testCanvas->clipRect(kTestRect);
495 testCanvas->drawRect(kTestRect, kTestPaint);
496 canvas->drawPicture(*testPicture);
497}
498TEST_STEP(DrawPicture, DrawPictureTestStep);
499
rmistry@google.comd6176b02012-08-23 18:14:13 +0000500static void SaveRestoreTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000501 skiatest::Reporter* reporter,
502 CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000503 int baseSaveCount = canvas->getSaveCount();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000504 int n = canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000505 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount == n, testStep->assertMessage());
506 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000507 testStep->assertMessage());
508 canvas->save();
509 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000510 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000511 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000512 canvas->restoreToCount(baseSaveCount + 1);
513 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000514 testStep->assertMessage());
515
516 // should this pin to 1, or be a no-op, or crash?
517 canvas->restoreToCount(0);
518 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
519 testStep->assertMessage());
520}
521TEST_STEP(SaveRestore, SaveRestoreTestStep);
522
rmistry@google.comd6176b02012-08-23 18:14:13 +0000523static void DrawLayerTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000524 skiatest::Reporter* reporter,
525 CanvasTestStep* testStep) {
526 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
527 testStep->assertMessage());
528 canvas->save();
529 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
530 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000531 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000532
reed@google.com7c202932011-12-14 18:48:05 +0000533 const SkRect* bounds = NULL; // null means include entire bounds
534 const SkPaint* paint = NULL;
535
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000536 canvas->saveLayer(bounds, paint);
537 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
538 testStep->assertMessage());
539 canvas->restore();
540 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
541 testStep->assertMessage());
reed@google.com7c202932011-12-14 18:48:05 +0000542
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000543 canvas->saveLayer(bounds, paint);
544 canvas->saveLayer(bounds, paint);
545 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
546 testStep->assertMessage());
547 canvas->restore();
548 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
549 testStep->assertMessage());
550 canvas->restore();
reed@google.com7c202932011-12-14 18:48:05 +0000551 // now layer count should be 0
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000552 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
553 testStep->assertMessage());
554}
555TEST_STEP(DrawLayer, DrawLayerTestStep);
reed@google.com3b3e8952012-08-16 20:53:31 +0000556
557static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000558 skiatest::Reporter*,
559 CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000560 // This test step challenges the TestDeferredCanvasStateConsistency
561 // test cases because the opaque paint can trigger an optimization
562 // that discards previously recorded commands. The challenge is to maintain
563 // correct clip and matrix stack state.
564 canvas->resetMatrix();
565 canvas->rotate(SkIntToScalar(30));
566 canvas->save();
567 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
568 canvas->save();
569 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
570 SkPaint paint;
571 paint.setColor(0xFFFFFFFF);
572 canvas->drawPaint(paint);
573 canvas->restore();
574 canvas->restore();
575}
576TEST_STEP(NestedSaveRestoreWithSolidPaint, \
577 NestedSaveRestoreWithSolidPaintTestStep);
578
579static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000580 skiatest::Reporter*,
581 CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000582 // This test step challenges the TestDeferredCanvasStateConsistency
583 // test case because the canvas flush on a deferred canvas will
584 // reset the recording session. The challenge is to maintain correct
585 // clip and matrix stack state on the playback canvas.
586 canvas->resetMatrix();
587 canvas->rotate(SkIntToScalar(30));
588 canvas->save();
589 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
590 canvas->save();
591 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
592 canvas->drawRect(kTestRect,kTestPaint);
593 canvas->flush();
594 canvas->restore();
595 canvas->restore();
596}
597TEST_STEP(NestedSaveRestoreWithFlush, \
598 NestedSaveRestoreWithFlushTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000599
600static void AssertCanvasStatesEqual(skiatest::Reporter* reporter,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000601 const SkCanvas* canvas1,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000602 const SkCanvas* canvas2,
603 CanvasTestStep* testStep) {
604 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
605 canvas2->getDeviceSize(), testStep->assertMessage());
606 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
607 canvas2->getSaveCount(), testStep->assertMessage());
608 REPORTER_ASSERT_MESSAGE(reporter, canvas1->isDrawingToLayer() ==
609 canvas2->isDrawingToLayer(), testStep->assertMessage());
reed@google.com3b3e8952012-08-16 20:53:31 +0000610
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000611 SkRect bounds1, bounds2;
612 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.com3b3e8952012-08-16 20:53:31 +0000613 canvas1->getClipBounds(&bounds1) == canvas2->getClipBounds(&bounds2),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000614 testStep->assertMessage());
615 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
reed@google.com3b3e8952012-08-16 20:53:31 +0000616 testStep->assertMessage());
617
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000618 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
619 canvas2->getDrawFilter(), testStep->assertMessage());
620 SkIRect deviceBounds1, deviceBounds2;
621 REPORTER_ASSERT_MESSAGE(reporter,
622 canvas1->getClipDeviceBounds(&deviceBounds1) ==
623 canvas2->getClipDeviceBounds(&deviceBounds2),
624 testStep->assertMessage());
625 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2,
626 testStep->assertMessage());
627 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getBounder() ==
628 canvas2->getBounder(), testStep->assertMessage());
629 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
630 canvas2->getTotalMatrix(), testStep->assertMessage());
631 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getClipType() ==
632 canvas2->getClipType(), testStep->assertMessage());
633 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalClip() ==
634 canvas2->getTotalClip(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000635
636 // The following test code is commented out because the test fails when
rmistry@google.comd6176b02012-08-23 18:14:13 +0000637 // the canvas is an SkPictureRecord or SkDeferredCanvas
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000638 // Issue: http://code.google.com/p/skia/issues/detail?id=498
639 // Also, creating a LayerIter on an SkProxyCanvas crashes
640 // Issue: http://code.google.com/p/skia/issues/detail?id=499
641 /*
642 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
643 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
644 while (!layerIter1.done() && !layerIter2.done()) {
645 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
646 layerIter2.matrix(), testStep->assertMessage());
647 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
648 layerIter2.clip(), testStep->assertMessage());
649 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
650 layerIter2.paint(), testStep->assertMessage());
651 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
652 layerIter2.x(), testStep->assertMessage());
653 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
654 layerIter2.y(), testStep->assertMessage());
655 layerIter1.next();
656 layerIter2.next();
657 }
658 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
659 testStep->assertMessage());
660 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
661 testStep->assertMessage());
662 */
663}
664
665// The following class groups static functions that need to access
666// the privates members of SkPictureRecord
667class SkPictureTester {
668private:
reed@google.come2589ae2012-07-10 19:38:01 +0000669 static int EQ(const SkFlatData* a, const SkFlatData* b) {
670 return *a == *b;
671 }
672
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000673 static void AssertFlattenedObjectsEqual(
674 SkPictureRecord* referenceRecord,
675 SkPictureRecord* testRecord,
676 skiatest::Reporter* reporter,
677 CanvasTestStep* testStep) {
678
679 REPORTER_ASSERT_MESSAGE(reporter,
djsollen@google.comc9ab9872012-08-29 18:52:07 +0000680 referenceRecord->fBitmapHeap->count() ==
681 testRecord->fBitmapHeap->count(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000682 REPORTER_ASSERT_MESSAGE(reporter,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000683 referenceRecord->fPaints.count() ==
684 testRecord->fPaints.count(), testStep->assertMessage());
685 for (int i = 0; i < referenceRecord->fPaints.count(); ++i) {
686 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.come2589ae2012-07-10 19:38:01 +0000687 EQ(referenceRecord->fPaints[i], testRecord->fPaints[i]),
688 testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000689 }
690 REPORTER_ASSERT_MESSAGE(reporter,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000691 !referenceRecord->fPathHeap ==
692 !testRecord->fPathHeap,
693 testStep->assertMessage());
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000694 // The following tests are commented out because they currently
695 // fail. Issue: http://code.google.com/p/skia/issues/detail?id=507
696 /*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000697 if (referenceRecord->fPathHeap) {
698 REPORTER_ASSERT_MESSAGE(reporter,
699 referenceRecord->fPathHeap->count() ==
700 testRecord->fPathHeap->count(),
701 testStep->assertMessage());
702 for (int i = 0; i < referenceRecord->fPathHeap->count(); ++i) {
703 REPORTER_ASSERT_MESSAGE(reporter,
704 (*referenceRecord->fPathHeap)[i] ==
705 (*testRecord->fPathHeap)[i], testStep->assertMessage());
706 }
707 }
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000708 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000709
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000710 }
711
712public:
713
rmistry@google.comd6176b02012-08-23 18:14:13 +0000714 static void TestPictureFlattenedObjectReuse(skiatest::Reporter* reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000715 CanvasTestStep* testStep,
716 uint32_t recordFlags) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000717 // Verify that when a test step is executed twice, no extra resources
718 // are flattened during the second execution
719 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
720 SkPicture referencePicture;
721 SkCanvas* referenceCanvas = referencePicture.beginRecording(kWidth,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000722 kHeight, recordFlags);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000723 testStep->draw(referenceCanvas, reporter);
724 SkPicture testPicture;
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000725 SkCanvas* testCanvas = testPicture.beginRecording(kWidth,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000726 kHeight, recordFlags);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000727 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000728 testStep->setAssertMessageFormat(kPictureSecondDrawAssertMessageFormat);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000729 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000730
731 SkPictureRecord* referenceRecord = static_cast<SkPictureRecord*>(
732 referenceCanvas);
733 SkPictureRecord* testRecord = static_cast<SkPictureRecord*>(
734 testCanvas);
735 testStep->setAssertMessageFormat(kPictureResourceReuseMessageFormat);
736 AssertFlattenedObjectsEqual(referenceRecord, testRecord,
junov@chromium.org76b9c4b2012-02-22 21:24:41 +0000737 reporter, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000738 }
739};
740
edisonn@google.com77909122012-10-18 15:58:23 +0000741static void TestPdfDevice(skiatest::Reporter* reporter,
742 CanvasTestStep* testStep) {
743 SkISize pageSize = SkISize::Make(kWidth, kHeight);
744 SkPDFDevice device(pageSize, pageSize, SkMatrix::I());
745 SkCanvas canvas(&device);
746 testStep->setAssertMessageFormat(kPdfAssertMessageFormat);
747 testStep->draw(&canvas, reporter);
748 SkPDFDocument doc;
749 doc.appendPage(&device);
750 SkDynamicMemoryWStream stream;
751 doc.emitPDF(&stream);
752}
753
junov@chromium.org88e29142012-08-07 16:48:22 +0000754// The following class groups static functions that need to access
755// the privates members of SkDeferredCanvas
756class SkDeferredCanvasTester {
757public:
758 static void TestDeferredCanvasStateConsistency(
759 skiatest::Reporter* reporter,
760 CanvasTestStep* testStep,
junov@chromium.orgfb103892012-09-20 19:35:43 +0000761 const SkCanvas& referenceCanvas, bool silent) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000762
reed@google.com28183b42014-02-04 15:34:10 +0000763 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
764 SkAutoTUnref<SkDeferredCanvas> deferredCanvas(SkDeferredCanvas::Create(surface.get()));
765
junov@chromium.org88e29142012-08-07 16:48:22 +0000766 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
junov@chromium.org66070a52013-05-28 17:39:08 +0000767 testStep->draw(deferredCanvas, reporter);
junov@chromium.org88e29142012-08-07 16:48:22 +0000768 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
junov@chromium.org66070a52013-05-28 17:39:08 +0000769 AssertCanvasStatesEqual(reporter, deferredCanvas, &referenceCanvas,
junov@chromium.org88e29142012-08-07 16:48:22 +0000770 testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000771
junov@chromium.orgfb103892012-09-20 19:35:43 +0000772 if (silent) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000773 deferredCanvas->silentFlush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000774 } else {
junov@chromium.org66070a52013-05-28 17:39:08 +0000775 deferredCanvas->flush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000776 }
777
skia.committer@gmail.com4c5ea442012-09-21 02:01:01 +0000778 testStep->setAssertMessageFormat(
junov@chromium.orgfb103892012-09-20 19:35:43 +0000779 silent ? kDeferredPostSilentFlushPlaybackAssertMessageFormat :
junov@chromium.org88e29142012-08-07 16:48:22 +0000780 kDeferredPostFlushPlaybackAssertMessageFormat);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000781 AssertCanvasStatesEqual(reporter,
junov@chromium.org66070a52013-05-28 17:39:08 +0000782 deferredCanvas->immediateCanvas(),
junov@chromium.org88e29142012-08-07 16:48:22 +0000783 &referenceCanvas, testStep);
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000784
junov@chromium.org88e29142012-08-07 16:48:22 +0000785 // Verified that deferred canvas state is not affected by flushing
786 // pending draw operations
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000787
junov@chromium.org88e29142012-08-07 16:48:22 +0000788 // The following test code is commented out because it currently fails.
789 // Issue: http://code.google.com/p/skia/issues/detail?id=496
790 /*
791 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
792 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
793 testStep);
794 */
795 }
796};
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000797
caryclark@google.com42639cd2012-06-06 12:03:39 +0000798// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000799static void TestProxyCanvasStateConsistency(
800 skiatest::Reporter* reporter,
801 CanvasTestStep* testStep,
802 const SkCanvas& referenceCanvas) {
803
804 SkBitmap indirectStore;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000805 createBitmap(&indirectStore, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000806 SkBitmapDevice indirectDevice(indirectStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000807 SkCanvas indirectCanvas(&indirectDevice);
808 SkProxyCanvas proxyCanvas(&indirectCanvas);
809 testStep->setAssertMessageFormat(kProxyDrawAssertMessageFormat);
810 testStep->draw(&proxyCanvas, reporter);
811 // Verify that the SkProxyCanvas reports consitent state
812 testStep->setAssertMessageFormat(kProxyStateAssertMessageFormat);
813 AssertCanvasStatesEqual(reporter, &proxyCanvas, &referenceCanvas,
814 testStep);
815 // Verify that the indirect canvas reports consitent state
816 testStep->setAssertMessageFormat(kProxyIndirectStateAssertMessageFormat);
817 AssertCanvasStatesEqual(reporter, &indirectCanvas, &referenceCanvas,
818 testStep);
819}
820
caryclark@google.com42639cd2012-06-06 12:03:39 +0000821// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000822static void TestNWayCanvasStateConsistency(
823 skiatest::Reporter* reporter,
824 CanvasTestStep* testStep,
825 const SkCanvas& referenceCanvas) {
826
827 SkBitmap indirectStore1;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000828 createBitmap(&indirectStore1, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000829 SkBitmapDevice indirectDevice1(indirectStore1);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000830 SkCanvas indirectCanvas1(&indirectDevice1);
831
832 SkBitmap indirectStore2;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000833 createBitmap(&indirectStore2, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000834 SkBitmapDevice indirectDevice2(indirectStore2);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000835 SkCanvas indirectCanvas2(&indirectDevice2);
836
djsollen@google.comf0a062b2012-05-01 16:50:25 +0000837 SkISize canvasSize = referenceCanvas.getDeviceSize();
838 SkNWayCanvas nWayCanvas(canvasSize.width(), canvasSize.height());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000839 nWayCanvas.addCanvas(&indirectCanvas1);
840 nWayCanvas.addCanvas(&indirectCanvas2);
841
842 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
843 testStep->draw(&nWayCanvas, reporter);
844 // Verify that the SkProxyCanvas reports consitent state
845 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
846 AssertCanvasStatesEqual(reporter, &nWayCanvas, &referenceCanvas,
847 testStep);
848 // Verify that the indirect canvases report consitent state
849 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
850 AssertCanvasStatesEqual(reporter, &indirectCanvas1, &referenceCanvas,
851 testStep);
852 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
853 AssertCanvasStatesEqual(reporter, &indirectCanvas2, &referenceCanvas,
854 testStep);
855}
856
857/*
858 * This sub-test verifies that the test step passes when executed
859 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
860 * that the all canvas derivatives report the same state as an SkCanvas
861 * after having executed the test step.
862 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000863static void TestOverrideStateConsistency(skiatest::Reporter* reporter,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000864 CanvasTestStep* testStep) {
865 SkBitmap referenceStore;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000866 createBitmap(&referenceStore, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000867 SkBitmapDevice referenceDevice(referenceStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000868 SkCanvas referenceCanvas(&referenceDevice);
869 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
870 testStep->draw(&referenceCanvas, reporter);
871
junov@chromium.orgfb103892012-09-20 19:35:43 +0000872 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, false);
873
874 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, true);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000875
caryclark@google.com42639cd2012-06-06 12:03:39 +0000876 // The following test code is disabled because SkProxyCanvas is
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000877 // missing a lot of virtual overrides on get* methods, which are used
878 // to verify canvas state.
879 // Issue: http://code.google.com/p/skia/issues/detail?id=500
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000880
caryclark@google.com42639cd2012-06-06 12:03:39 +0000881 if (false) { // avoid bit rot, suppress warning
882 TestProxyCanvasStateConsistency(reporter, testStep, referenceCanvas);
883 }
884
885 // The following test code is disabled because SkNWayCanvas does not
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000886 // report correct clipping and device bounds information
887 // Issue: http://code.google.com/p/skia/issues/detail?id=501
caryclark@google.com42639cd2012-06-06 12:03:39 +0000888
889 if (false) { // avoid bit rot, suppress warning
890 TestNWayCanvasStateConsistency(reporter, testStep, referenceCanvas);
891 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000892
caryclark@google.com42639cd2012-06-06 12:03:39 +0000893 if (false) { // avoid bit rot, suppress warning
894 test_clipVisitor(reporter, &referenceCanvas);
895 }
reed@google.com7c202932011-12-14 18:48:05 +0000896}
reed@google.com37f3ae02011-11-28 16:06:04 +0000897
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000898static void test_newraster(skiatest::Reporter* reporter) {
899 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
900 SkCanvas* canvas = SkCanvas::NewRaster(info);
901 REPORTER_ASSERT(reporter, canvas);
902
903 SkImageInfo info2;
904 size_t rowBytes;
905 const SkPMColor* addr = (const SkPMColor*)canvas->peekPixels(&info2, &rowBytes);
906 REPORTER_ASSERT(reporter, addr);
907 REPORTER_ASSERT(reporter, info == info2);
908 for (int y = 0; y < info.height(); ++y) {
909 for (int x = 0; x < info.width(); ++x) {
910 REPORTER_ASSERT(reporter, 0 == addr[x]);
911 }
912 addr = (const SkPMColor*)((const char*)addr + rowBytes);
913 }
914 SkDELETE(canvas);
915
916 // now try a deliberately bad info
917 info.fWidth = -1;
918 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRaster(info));
919
920 // too big
921 info.fWidth = 1 << 30;
922 info.fHeight = 1 << 30;
923 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRaster(info));
skia.committer@gmail.com0e530752014-02-28 03:02:05 +0000924
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000925 // not a valid pixel type
926 info.fWidth = info.fHeight = 10;
927 info.fColorType = kUnknown_SkColorType;
928 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRaster(info));
929
930 // We should succeed with a zero-sized valid info
931 info = SkImageInfo::MakeN32Premul(0, 0);
932 canvas = SkCanvas::NewRaster(info);
933 REPORTER_ASSERT(reporter, canvas);
934 SkDELETE(canvas);
935}
936
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000937DEF_TEST(Canvas, reporter) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000938 // Init global here because bitmap pixels cannot be alocated during
939 // static initialization
940 kTestBitmap = testBitmap();
reed@google.com37f3ae02011-11-28 16:06:04 +0000941
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000942 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
943 TestOverrideStateConsistency(reporter, testStepArray()[testStep]);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000944 SkPictureTester::TestPictureFlattenedObjectReuse(reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000945 testStepArray()[testStep], 0);
edisonn@google.com77909122012-10-18 15:58:23 +0000946 if (testStepArray()[testStep]->enablePdfTesting()) {
947 TestPdfDevice(reporter, testStepArray()[testStep]);
948 }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000949 }
junov@chromium.orgcd62ecf2012-08-02 17:43:25 +0000950
951 // Explicitly call reset(), so we don't leak the pixels (since kTestBitmap is a global)
952 kTestBitmap.reset();
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000953
954 test_newraster(reporter);
reed@google.com37f3ae02011-11-28 16:06:04 +0000955}