blob: 3cefcbd4de8cdbc98e5ec09707a0c8a29ad58f61 [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"
robertphillips@google.com770963f2014-04-18 18:04:41 +000058#include "SkPictureRecorder.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000059#include "SkProxyCanvas.h"
60#include "SkRect.h"
61#include "SkRegion.h"
62#include "SkShader.h"
63#include "SkStream.h"
reed@google.com28183b42014-02-04 15:34:10 +000064#include "SkSurface.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000065#include "SkTDArray.h"
66#include "Test.h"
reed@google.com37f3ae02011-11-28 16:06:04 +000067
piotaixrf05f5a72014-10-03 13:26:55 -070068static const int kWidth = 2, kHeight = 2;
69
70static void createBitmap(SkBitmap* bm, SkColor color) {
71 bm->allocN32Pixels(kWidth, kHeight);
72 bm->eraseColor(color);
73}
74
75static SkSurface* createSurface(SkColor color) {
76 SkSurface* surface = SkSurface::NewRasterPMColor(kWidth, kHeight);
77 surface->getCanvas()->clear(color);
78 return surface;
79}
80
81///////////////////////////////////////////////////////////////////////////////
82// Constants used by test steps
83const SkPoint kTestPoints[] = {
84 {SkIntToScalar(0), SkIntToScalar(0)},
85 {SkIntToScalar(2), SkIntToScalar(1)},
86 {SkIntToScalar(0), SkIntToScalar(2)}
87};
88const SkPoint kTestPoints2[] = {
89 { SkIntToScalar(0), SkIntToScalar(1) },
90 { SkIntToScalar(1), SkIntToScalar(1) },
91 { SkIntToScalar(2), SkIntToScalar(1) },
92 { SkIntToScalar(3), SkIntToScalar(1) },
93 { SkIntToScalar(4), SkIntToScalar(1) },
94 { SkIntToScalar(5), SkIntToScalar(1) },
95 { SkIntToScalar(6), SkIntToScalar(1) },
96 { SkIntToScalar(7), SkIntToScalar(1) },
97 { SkIntToScalar(8), SkIntToScalar(1) },
98 { SkIntToScalar(9), SkIntToScalar(1) },
99 { SkIntToScalar(10), SkIntToScalar(1) }
100};
101
102struct TestData {
103public:
104 TestData()
105 : fRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
106 SkIntToScalar(2), SkIntToScalar(1)))
107 , fMatrix(TestMatrix())
108 , fPath(TestPath())
109 , fNearlyZeroLengthPath(TestNearlyZeroLengthPath())
110 , fIRect(SkIRect::MakeXYWH(0, 0, 2, 1))
111 , fRegion(TestRegion())
112 , fColor(0x01020304)
113 , fPoints(kTestPoints)
114 , fPointCount(3)
115 , fWidth(2)
116 , fHeight(2)
117 , fText("Hello World")
118 , fPoints2(kTestPoints2)
119 , fBitmap(TestBitmap())
120 { }
121
122 SkRect fRect;
123 SkMatrix fMatrix;;
124 SkPath fPath;
125 SkPath fNearlyZeroLengthPath;
126 SkIRect fIRect;
127 SkRegion fRegion;
128 SkColor fColor;
129 SkPaint fPaint;
130 const SkPoint* fPoints;
131 size_t fPointCount;
132 int fWidth;
133 int fHeight;
134 SkString fText;
135 const SkPoint* fPoints2;
136 SkBitmap fBitmap;
137
138private:
139 static SkMatrix TestMatrix() {
140 SkMatrix matrix;
141 matrix.reset();
142 matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
143
144 return matrix;
145 }
146 static SkPath TestPath() {
147 SkPath path;
148 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
149 SkIntToScalar(2), SkIntToScalar(1)));
150 return path;
151 }
152 static SkPath TestNearlyZeroLengthPath() {
153 SkPath path;
154 SkPoint pt1 = { 0, 0 };
155 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
156 SkPoint pt3 = { SkIntToScalar(1), 0 };
157 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
158 path.moveTo(pt1);
159 path.lineTo(pt2);
160 path.lineTo(pt3);
161 path.lineTo(pt4);
162 return path;
163 }
164 static SkRegion TestRegion() {
165 SkRegion region;
166 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
167 region.setRect(rect);
168 return region;
169 }
170 static SkBitmap TestBitmap() {
171 SkBitmap bitmap;
172 createBitmap(&bitmap, 0x05060708);
173 return bitmap;
174 }
175};
176
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000177static bool equal_clips(const SkCanvas& a, const SkCanvas& b) {
178 if (a.isClipEmpty()) {
179 return b.isClipEmpty();
180 }
181 if (!a.isClipRect()) {
182 // this is liberally true, since we don't expose a way to know this exactly (for non-rects)
183 return !b.isClipRect();
184 }
185 SkIRect ar, br;
186 a.getClipDeviceBounds(&ar);
187 b.getClipDeviceBounds(&br);
188 return ar == br;
189}
190
reed@google.com90c07ea2012-04-13 13:50:27 +0000191class Canvas2CanvasClipVisitor : public SkCanvas::ClipVisitor {
192public:
193 Canvas2CanvasClipVisitor(SkCanvas* target) : fTarget(target) {}
194
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000195 virtual void clipRect(const SkRect& r, SkRegion::Op op, bool aa) SK_OVERRIDE {
reed@google.com90c07ea2012-04-13 13:50:27 +0000196 fTarget->clipRect(r, op, aa);
197 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000198 virtual void clipRRect(const SkRRect& r, SkRegion::Op op, bool aa) SK_OVERRIDE {
199 fTarget->clipRRect(r, op, aa);
200 }
201 virtual void clipPath(const SkPath& p, SkRegion::Op op, bool aa) SK_OVERRIDE {
reed@google.com90c07ea2012-04-13 13:50:27 +0000202 fTarget->clipPath(p, op, aa);
203 }
204
205private:
206 SkCanvas* fTarget;
207};
208
209static void test_clipVisitor(skiatest::Reporter* reporter, SkCanvas* canvas) {
210 SkISize size = canvas->getDeviceSize();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000211
reed@google.com90c07ea2012-04-13 13:50:27 +0000212 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000213 bm.setInfo(SkImageInfo::MakeN32Premul(size.width(), size.height()));
reed@google.com90c07ea2012-04-13 13:50:27 +0000214 SkCanvas c(bm);
215
216 Canvas2CanvasClipVisitor visitor(&c);
217 canvas->replayClips(&visitor);
218
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000219 REPORTER_ASSERT(reporter, equal_clips(c, *canvas));
reed@google.com90c07ea2012-04-13 13:50:27 +0000220}
221
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000222// Format strings that describe the test context. The %s token is where
223// the name of the test step is inserted. The context is required for
224// disambiguating the error in the case of failures that are reported in
225// functions that are called multiple times in different contexts (test
226// cases and test steps).
227static const char* const kDefaultAssertMessageFormat = "%s";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000228static const char* const kCanvasDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000229 "Drawing test step %s with SkCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000230static const char* const kDeferredDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000231 "Drawing test step %s with SkDeferredCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000232static const char* const kProxyDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000233 "Drawing test step %s with SkProxyCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000234static const char* const kNWayDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000235 "Drawing test step %s with SkNWayCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000236static const char* const kDeferredPreFlushAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000237 "test step %s, SkDeferredCanvas state consistency before flush";
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000238static const char* const kDeferredPostFlushPlaybackAssertMessageFormat =
239 "test step %s, SkDeferredCanvas playback canvas state consistency after flush";
junov@chromium.orgfb103892012-09-20 19:35:43 +0000240static const char* const kDeferredPostSilentFlushPlaybackAssertMessageFormat =
241 "test step %s, SkDeferredCanvas playback canvas state consistency after silent flush";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000242static const char* const kProxyStateAssertMessageFormat =
243 "test step %s, SkProxyCanvas state consistency";
244static const char* const kProxyIndirectStateAssertMessageFormat =
245 "test step %s, SkProxyCanvas indirect canvas state consistency";
246static const char* const kNWayStateAssertMessageFormat =
247 "test step %s, SkNWayCanvas state consistency";
248static const char* const kNWayIndirect1StateAssertMessageFormat =
249 "test step %s, SkNWayCanvas indirect canvas 1 state consistency";
250static const char* const kNWayIndirect2StateAssertMessageFormat =
251 "test step %s, SkNWayCanvas indirect canvas 2 state consistency";
edisonn@google.com77909122012-10-18 15:58:23 +0000252static const char* const kPdfAssertMessageFormat =
253 "PDF sanity check failed %s";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000254
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000255class CanvasTestStep;
256static SkTDArray<CanvasTestStep*>& testStepArray() {
257 static SkTDArray<CanvasTestStep*> theTests;
258 return theTests;
259}
260
261class CanvasTestStep {
262public:
edisonn@google.com77909122012-10-18 15:58:23 +0000263 CanvasTestStep(bool fEnablePdfTesting = true) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000264 *testStepArray().append() = this;
265 fAssertMessageFormat = kDefaultAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000266 this->fEnablePdfTesting = fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000267 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000268 virtual ~CanvasTestStep() { }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000269
piotaixrf05f5a72014-10-03 13:26:55 -0700270 virtual void draw(SkCanvas*, const TestData&, skiatest::Reporter*) = 0;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000271 virtual const char* name() const = 0;
272
273 const char* assertMessage() {
274 fAssertMessage.printf(fAssertMessageFormat, name());
275 return fAssertMessage.c_str();
276 }
277
278 void setAssertMessageFormat(const char* format) {
279 fAssertMessageFormat = format;
280 }
281
edisonn@google.com77909122012-10-18 15:58:23 +0000282 bool enablePdfTesting() { return fEnablePdfTesting; }
283
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000284private:
285 SkString fAssertMessage;
286 const char* fAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000287 bool fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000288};
289
290///////////////////////////////////////////////////////////////////////////////
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000291// Macros for defining test steps
292
293#define TEST_STEP(NAME, FUNCTION) \
294class NAME##_TestStep : public CanvasTestStep{ \
295public: \
piotaixrf05f5a72014-10-03 13:26:55 -0700296 virtual void draw(SkCanvas* canvas, const TestData& d, \
297 skiatest::Reporter* reporter) { \
298 FUNCTION (canvas, d, reporter, this); \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000299 } \
300 virtual const char* name() const {return #NAME ;} \
301}; \
302static NAME##_TestStep NAME##_TestStepInstance;
303
piotaixrf05f5a72014-10-03 13:26:55 -0700304#define TEST_STEP_NO_PDF(NAME, FUNCTION) \
edisonn@google.com77909122012-10-18 15:58:23 +0000305class NAME##_TestStep : public CanvasTestStep{ \
306public: \
307 NAME##_TestStep() : CanvasTestStep(false) {} \
piotaixrf05f5a72014-10-03 13:26:55 -0700308 virtual void draw(SkCanvas* canvas, const TestData& d, \
309 skiatest::Reporter* reporter) { \
310 FUNCTION (canvas, d, reporter, this); \
edisonn@google.com77909122012-10-18 15:58:23 +0000311 } \
312 virtual const char* name() const {return #NAME ;} \
313}; \
314static NAME##_TestStep NAME##_TestStepInstance;
315
piotaixrf05f5a72014-10-03 13:26:55 -0700316#define SIMPLE_TEST_STEP(NAME, CALL) \
317static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \
318 skiatest::Reporter*, CanvasTestStep*) { \
319 canvas-> CALL ; \
320} \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000321TEST_STEP(NAME, NAME##TestStep )
322
323#define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
piotaixrf05f5a72014-10-03 13:26:55 -0700324static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \
325 skiatest::Reporter*, CanvasTestStep* testStep) { \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000326 REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \
327 testStep->assertMessage()); \
328} \
329TEST_STEP(NAME, NAME##TestStep )
330
331
332///////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000333// Basic test steps for most virtual methods in SkCanvas that draw or affect
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000334// the state of the canvas.
335
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000336SIMPLE_TEST_STEP(Translate, translate(SkIntToScalar(1), SkIntToScalar(2)));
337SIMPLE_TEST_STEP(Scale, scale(SkIntToScalar(1), SkIntToScalar(2)));
338SIMPLE_TEST_STEP(Rotate, rotate(SkIntToScalar(1)));
339SIMPLE_TEST_STEP(Skew, skew(SkIntToScalar(1), SkIntToScalar(2)));
piotaixrf05f5a72014-10-03 13:26:55 -0700340SIMPLE_TEST_STEP(Concat, concat(d.fMatrix));
341SIMPLE_TEST_STEP(SetMatrix, setMatrix(d.fMatrix));
342SIMPLE_TEST_STEP(ClipRect, clipRect(d.fRect));
343SIMPLE_TEST_STEP(ClipPath, clipPath(d.fPath));
344SIMPLE_TEST_STEP(ClipRegion, clipRegion(d.fRegion, SkRegion::kReplace_Op));
345SIMPLE_TEST_STEP(Clear, clear(d.fColor));
346SIMPLE_TEST_STEP(DrawPaint, drawPaint(d.fPaint));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000347SIMPLE_TEST_STEP(DrawPointsPoints, drawPoints(SkCanvas::kPoints_PointMode,
piotaixrf05f5a72014-10-03 13:26:55 -0700348 d.fPointCount, d.fPoints, d.fPaint));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000349SIMPLE_TEST_STEP(DrawPointsLiness, drawPoints(SkCanvas::kLines_PointMode,
piotaixrf05f5a72014-10-03 13:26:55 -0700350 d.fPointCount, d.fPoints, d.fPaint));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000351SIMPLE_TEST_STEP(DrawPointsPolygon, drawPoints(SkCanvas::kPolygon_PointMode,
piotaixrf05f5a72014-10-03 13:26:55 -0700352 d.fPointCount, d.fPoints, d.fPaint));
353SIMPLE_TEST_STEP(DrawRect, drawRect(d.fRect, d.fPaint));
354SIMPLE_TEST_STEP(DrawPath, drawPath(d.fPath, d.fPaint));
355SIMPLE_TEST_STEP(DrawBitmap, drawBitmap(d.fBitmap, 0, 0));
356SIMPLE_TEST_STEP(DrawBitmapPaint, drawBitmap(d.fBitmap, 0, 0, &d.fPaint));
357SIMPLE_TEST_STEP(DrawBitmapRect, drawBitmapRect(d.fBitmap, NULL, d.fRect, NULL));
358SIMPLE_TEST_STEP(DrawBitmapRectSrcRect, drawBitmapRect(d.fBitmap, &d.fIRect, d.fRect, NULL));
359SIMPLE_TEST_STEP(DrawBitmapRectPaint, drawBitmapRect(d.fBitmap, NULL, d.fRect, &d.fPaint));
360SIMPLE_TEST_STEP(DrawBitmapMatrix, drawBitmapMatrix(d.fBitmap, d.fMatrix, NULL));
361SIMPLE_TEST_STEP(DrawBitmapMatrixPaint, drawBitmapMatrix(d.fBitmap, d.fMatrix, &d.fPaint));
362SIMPLE_TEST_STEP(DrawBitmapNine, drawBitmapNine(d.fBitmap, d.fIRect, d.fRect, NULL));
363SIMPLE_TEST_STEP(DrawBitmapNinePaint, drawBitmapNine(d.fBitmap, d.fIRect, d.fRect, &d.fPaint));
364SIMPLE_TEST_STEP(DrawSprite, drawSprite(d.fBitmap, 0, 0, NULL));
365SIMPLE_TEST_STEP(DrawSpritePaint, drawSprite(d.fBitmap, 0, 0, &d.fPaint));
366SIMPLE_TEST_STEP(DrawText, drawText(d.fText.c_str(), d.fText.size(), 0, 1, d.fPaint));
367SIMPLE_TEST_STEP(DrawPosText, drawPosText(d.fText.c_str(), d.fText.size(), d.fPoints2, d.fPaint));
368SIMPLE_TEST_STEP(DrawTextOnPath, drawTextOnPath(d.fText.c_str(), d.fText.size(),
369 d.fPath, NULL, d.fPaint));
370SIMPLE_TEST_STEP(DrawTextOnPathMatrix, drawTextOnPath(d.fText.c_str(), d.fText.size(), d.fPath,
371 &d.fMatrix, d.fPaint));
372SIMPLE_TEST_STEP(DrawData, drawData(d.fText.c_str(), d.fText.size()));
373SIMPLE_TEST_STEP(BeginGroup, beginCommentGroup(d.fText.c_str()));
374SIMPLE_TEST_STEP(AddComment, addComment(d.fText.c_str(), d.fText.c_str()));
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000375SIMPLE_TEST_STEP(EndGroup, endCommentGroup());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000376
377///////////////////////////////////////////////////////////////////////////////
378// Complex test steps
379
piotaixrf05f5a72014-10-03 13:26:55 -0700380static void SaveMatrixClipStep(SkCanvas* canvas, const TestData& d,
381 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000382 int saveCount = canvas->getSaveCount();
Florin Malita5f6102d2014-06-30 10:13:28 -0400383 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000384 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
piotaixrf05f5a72014-10-03 13:26:55 -0700385 canvas->clipRegion(d.fRegion);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000386 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000387 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000388 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000389 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000390 testStep->assertMessage());
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000391// REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion, testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000392}
393TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
394
piotaixrf05f5a72014-10-03 13:26:55 -0700395static void SaveLayerStep(SkCanvas* canvas, const TestData& d,
396 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000397 int saveCount = canvas->getSaveCount();
398 canvas->saveLayer(NULL, NULL);
399 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000400 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000401 testStep->assertMessage());
402}
403TEST_STEP(SaveLayer, SaveLayerStep);
404
piotaixrf05f5a72014-10-03 13:26:55 -0700405static void BoundedSaveLayerStep(SkCanvas* canvas, const TestData& d,
406 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000407 int saveCount = canvas->getSaveCount();
piotaixrf05f5a72014-10-03 13:26:55 -0700408 canvas->saveLayer(&d.fRect, NULL);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000409 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000410 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000411 testStep->assertMessage());
412}
413TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
414
piotaixrf05f5a72014-10-03 13:26:55 -0700415static void PaintSaveLayerStep(SkCanvas* canvas, const TestData& d,
416 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000417 int saveCount = canvas->getSaveCount();
piotaixrf05f5a72014-10-03 13:26:55 -0700418 canvas->saveLayer(NULL, &d.fPaint);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000419 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000420 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000421 testStep->assertMessage());
422}
423TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
424
piotaixrf05f5a72014-10-03 13:26:55 -0700425static void TwoClipOpsStep(SkCanvas* canvas, const TestData& d,
426 skiatest::Reporter*, CanvasTestStep*) {
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000427 // This test exercises a functionality in SkPicture that leads to the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000428 // recording of restore offset placeholders. This test will trigger an
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000429 // assertion at playback time if the placeholders are not properly
430 // filled when the recording ends.
piotaixrf05f5a72014-10-03 13:26:55 -0700431 canvas->clipRect(d.fRect);
432 canvas->clipRegion(d.fRegion);
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000433}
434TEST_STEP(TwoClipOps, TwoClipOpsStep);
435
epoger@google.com94fa43c2012-04-11 17:51:01 +0000436// exercise fix for http://code.google.com/p/skia/issues/detail?id=560
437// ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
piotaixrf05f5a72014-10-03 13:26:55 -0700438static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas, const TestData& d,
439 skiatest::Reporter*, CanvasTestStep*) {
epoger@google.com94fa43c2012-04-11 17:51:01 +0000440 SkPaint paint;
441 paint.setStrokeWidth(SkIntToScalar(1));
442 paint.setStyle(SkPaint::kStroke_Style);
443
piotaixrf05f5a72014-10-03 13:26:55 -0700444 canvas->drawPath(d.fNearlyZeroLengthPath, paint);
epoger@google.com94fa43c2012-04-11 17:51:01 +0000445}
446TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
447
piotaixrf05f5a72014-10-03 13:26:55 -0700448static void DrawVerticesShaderTestStep(SkCanvas* canvas, const TestData& d,
449 skiatest::Reporter*, CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000450 SkPoint pts[4];
451 pts[0].set(0, 0);
piotaixrf05f5a72014-10-03 13:26:55 -0700452 pts[1].set(SkIntToScalar(d.fWidth), 0);
453 pts[2].set(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight));
454 pts[3].set(0, SkIntToScalar(d.fHeight));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000455 SkPaint paint;
piotaixrf05f5a72014-10-03 13:26:55 -0700456 SkShader* shader = SkShader::CreateBitmapShader(d.fBitmap,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000457 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
458 paint.setShader(shader)->unref();
459 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
460 NULL, NULL, NULL, 0, paint);
461}
edisonn@google.com77909122012-10-18 15:58:23 +0000462// NYI: issue 240.
463TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000464
piotaixrf05f5a72014-10-03 13:26:55 -0700465static void DrawPictureTestStep(SkCanvas* canvas, const TestData& d,
466 skiatest::Reporter*, CanvasTestStep*) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000467 SkPictureRecorder recorder;
piotaixrf05f5a72014-10-03 13:26:55 -0700468 SkCanvas* testCanvas = recorder.beginRecording(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight),
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700469 NULL, 0);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000470 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
piotaixrf05f5a72014-10-03 13:26:55 -0700471 testCanvas->clipRect(d.fRect);
472 testCanvas->drawRect(d.fRect, d.fPaint);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000473 SkAutoTUnref<SkPicture> testPicture(recorder.endRecording());
474
robertphillips9b14f262014-06-04 05:40:44 -0700475 canvas->drawPicture(testPicture);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000476}
477TEST_STEP(DrawPicture, DrawPictureTestStep);
478
piotaixrf05f5a72014-10-03 13:26:55 -0700479static void SaveRestoreTestStep(SkCanvas* canvas, const TestData& d,
480 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000481 int baseSaveCount = canvas->getSaveCount();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000482 int n = canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000483 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount == n, testStep->assertMessage());
484 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000485 testStep->assertMessage());
486 canvas->save();
487 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000488 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000489 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000490 canvas->restoreToCount(baseSaveCount + 1);
491 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000492 testStep->assertMessage());
493
494 // should this pin to 1, or be a no-op, or crash?
495 canvas->restoreToCount(0);
496 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
497 testStep->assertMessage());
498}
499TEST_STEP(SaveRestore, SaveRestoreTestStep);
500
piotaixrf05f5a72014-10-03 13:26:55 -0700501static void DrawLayerTestStep(SkCanvas* canvas, const TestData& d,
502 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000503 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
504 testStep->assertMessage());
505 canvas->save();
506 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
507 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000508 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000509
reed@google.com7c202932011-12-14 18:48:05 +0000510 const SkRect* bounds = NULL; // null means include entire bounds
511 const SkPaint* paint = NULL;
512
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000513 canvas->saveLayer(bounds, paint);
514 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
515 testStep->assertMessage());
516 canvas->restore();
517 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
518 testStep->assertMessage());
reed@google.com7c202932011-12-14 18:48:05 +0000519
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000520 canvas->saveLayer(bounds, paint);
521 canvas->saveLayer(bounds, paint);
522 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
523 testStep->assertMessage());
524 canvas->restore();
525 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
526 testStep->assertMessage());
527 canvas->restore();
reed@google.com7c202932011-12-14 18:48:05 +0000528 // now layer count should be 0
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000529 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
530 testStep->assertMessage());
531}
532TEST_STEP(DrawLayer, DrawLayerTestStep);
reed@google.com3b3e8952012-08-16 20:53:31 +0000533
piotaixrf05f5a72014-10-03 13:26:55 -0700534static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas, const TestData& d,
535 skiatest::Reporter*, CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000536 // This test step challenges the TestDeferredCanvasStateConsistency
537 // test cases because the opaque paint can trigger an optimization
538 // that discards previously recorded commands. The challenge is to maintain
539 // correct clip and matrix stack state.
540 canvas->resetMatrix();
541 canvas->rotate(SkIntToScalar(30));
542 canvas->save();
543 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
544 canvas->save();
545 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
546 SkPaint paint;
547 paint.setColor(0xFFFFFFFF);
548 canvas->drawPaint(paint);
549 canvas->restore();
550 canvas->restore();
551}
552TEST_STEP(NestedSaveRestoreWithSolidPaint, \
553 NestedSaveRestoreWithSolidPaintTestStep);
554
piotaixrf05f5a72014-10-03 13:26:55 -0700555static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas, const TestData& d,
556 skiatest::Reporter*, CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000557 // This test step challenges the TestDeferredCanvasStateConsistency
558 // test case because the canvas flush on a deferred canvas will
559 // reset the recording session. The challenge is to maintain correct
560 // clip and matrix stack state on the playback canvas.
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));
piotaixrf05f5a72014-10-03 13:26:55 -0700567 canvas->drawRect(d.fRect,d.fPaint);
reed@google.com3b3e8952012-08-16 20:53:31 +0000568 canvas->flush();
569 canvas->restore();
570 canvas->restore();
571}
piotaixrf05f5a72014-10-03 13:26:55 -0700572TEST_STEP(NestedSaveRestoreWithFlush, NestedSaveRestoreWithFlushTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000573
piotaixrf05f5a72014-10-03 13:26:55 -0700574static void AssertCanvasStatesEqual(skiatest::Reporter* reporter, const TestData& d,
575 const SkCanvas* canvas1, const SkCanvas* canvas2,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000576 CanvasTestStep* testStep) {
577 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
578 canvas2->getDeviceSize(), testStep->assertMessage());
579 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
580 canvas2->getSaveCount(), testStep->assertMessage());
581 REPORTER_ASSERT_MESSAGE(reporter, canvas1->isDrawingToLayer() ==
582 canvas2->isDrawingToLayer(), testStep->assertMessage());
reed@google.com3b3e8952012-08-16 20:53:31 +0000583
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000584 SkRect bounds1, bounds2;
585 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.com3b3e8952012-08-16 20:53:31 +0000586 canvas1->getClipBounds(&bounds1) == canvas2->getClipBounds(&bounds2),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000587 testStep->assertMessage());
588 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
reed@google.com3b3e8952012-08-16 20:53:31 +0000589 testStep->assertMessage());
590
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000591 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
592 canvas2->getDrawFilter(), testStep->assertMessage());
593 SkIRect deviceBounds1, deviceBounds2;
594 REPORTER_ASSERT_MESSAGE(reporter,
595 canvas1->getClipDeviceBounds(&deviceBounds1) ==
596 canvas2->getClipDeviceBounds(&deviceBounds2),
597 testStep->assertMessage());
reed868074b2014-06-03 10:53:59 -0700598 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2, testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000599 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
600 canvas2->getTotalMatrix(), testStep->assertMessage());
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000601 REPORTER_ASSERT_MESSAGE(reporter, equal_clips(*canvas1, *canvas2), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000602
603 // The following test code is commented out because the test fails when
rmistry@google.comd6176b02012-08-23 18:14:13 +0000604 // the canvas is an SkPictureRecord or SkDeferredCanvas
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000605 // Issue: http://code.google.com/p/skia/issues/detail?id=498
606 // Also, creating a LayerIter on an SkProxyCanvas crashes
607 // Issue: http://code.google.com/p/skia/issues/detail?id=499
608 /*
609 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
610 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
611 while (!layerIter1.done() && !layerIter2.done()) {
612 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
613 layerIter2.matrix(), testStep->assertMessage());
614 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
615 layerIter2.clip(), testStep->assertMessage());
616 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
617 layerIter2.paint(), testStep->assertMessage());
618 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
619 layerIter2.x(), testStep->assertMessage());
620 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
621 layerIter2.y(), testStep->assertMessage());
622 layerIter1.next();
623 layerIter2.next();
624 }
625 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
626 testStep->assertMessage());
627 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
628 testStep->assertMessage());
629 */
630}
631
632// The following class groups static functions that need to access
633// the privates members of SkPictureRecord
634class SkPictureTester {
635private:
reed@google.come2589ae2012-07-10 19:38:01 +0000636 static int EQ(const SkFlatData* a, const SkFlatData* b) {
637 return *a == *b;
638 }
639
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000640 static void AssertFlattenedObjectsEqual(
641 SkPictureRecord* referenceRecord,
642 SkPictureRecord* testRecord,
643 skiatest::Reporter* reporter,
644 CanvasTestStep* testStep) {
645
646 REPORTER_ASSERT_MESSAGE(reporter,
djsollen@google.comc9ab9872012-08-29 18:52:07 +0000647 referenceRecord->fBitmapHeap->count() ==
648 testRecord->fBitmapHeap->count(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000649 REPORTER_ASSERT_MESSAGE(reporter,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000650 referenceRecord->fPaints.count() ==
651 testRecord->fPaints.count(), testStep->assertMessage());
652 for (int i = 0; i < referenceRecord->fPaints.count(); ++i) {
653 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.come2589ae2012-07-10 19:38:01 +0000654 EQ(referenceRecord->fPaints[i], testRecord->fPaints[i]),
655 testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000656 }
657 REPORTER_ASSERT_MESSAGE(reporter,
robertphillips0bdbea72014-06-11 11:37:55 -0700658 !referenceRecord->fPathHeap == !testRecord->fPathHeap,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000659 testStep->assertMessage());
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000660 // The following tests are commented out because they currently
661 // fail. Issue: http://code.google.com/p/skia/issues/detail?id=507
662 /*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000663 if (referenceRecord->fPathHeap) {
664 REPORTER_ASSERT_MESSAGE(reporter,
665 referenceRecord->fPathHeap->count() ==
666 testRecord->fPathHeap->count(),
667 testStep->assertMessage());
668 for (int i = 0; i < referenceRecord->fPathHeap->count(); ++i) {
669 REPORTER_ASSERT_MESSAGE(reporter,
670 (*referenceRecord->fPathHeap)[i] ==
671 (*testRecord->fPathHeap)[i], testStep->assertMessage());
672 }
673 }
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000674 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000675
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000676 }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000677};
678
edisonn@google.com77909122012-10-18 15:58:23 +0000679static void TestPdfDevice(skiatest::Reporter* reporter,
piotaixrf05f5a72014-10-03 13:26:55 -0700680 const TestData& d,
edisonn@google.com77909122012-10-18 15:58:23 +0000681 CanvasTestStep* testStep) {
piotaixrf05f5a72014-10-03 13:26:55 -0700682 SkISize pageSize = SkISize::Make(d.fWidth, d.fHeight);
edisonn@google.com77909122012-10-18 15:58:23 +0000683 SkPDFDevice device(pageSize, pageSize, SkMatrix::I());
684 SkCanvas canvas(&device);
685 testStep->setAssertMessageFormat(kPdfAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700686 testStep->draw(&canvas, d, reporter);
edisonn@google.com77909122012-10-18 15:58:23 +0000687 SkPDFDocument doc;
688 doc.appendPage(&device);
689 SkDynamicMemoryWStream stream;
690 doc.emitPDF(&stream);
691}
692
junov@chromium.org88e29142012-08-07 16:48:22 +0000693// The following class groups static functions that need to access
694// the privates members of SkDeferredCanvas
695class SkDeferredCanvasTester {
696public:
697 static void TestDeferredCanvasStateConsistency(
698 skiatest::Reporter* reporter,
piotaixrf05f5a72014-10-03 13:26:55 -0700699 const TestData& d,
junov@chromium.org88e29142012-08-07 16:48:22 +0000700 CanvasTestStep* testStep,
junov@chromium.orgfb103892012-09-20 19:35:43 +0000701 const SkCanvas& referenceCanvas, bool silent) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000702
reed@google.com28183b42014-02-04 15:34:10 +0000703 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
704 SkAutoTUnref<SkDeferredCanvas> deferredCanvas(SkDeferredCanvas::Create(surface.get()));
705
junov@chromium.org88e29142012-08-07 16:48:22 +0000706 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700707 testStep->draw(deferredCanvas, d, reporter);
junov@chromium.org88e29142012-08-07 16:48:22 +0000708 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700709 AssertCanvasStatesEqual(reporter, d, deferredCanvas, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000710
junov@chromium.orgfb103892012-09-20 19:35:43 +0000711 if (silent) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000712 deferredCanvas->silentFlush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000713 } else {
junov@chromium.org66070a52013-05-28 17:39:08 +0000714 deferredCanvas->flush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000715 }
716
skia.committer@gmail.com4c5ea442012-09-21 02:01:01 +0000717 testStep->setAssertMessageFormat(
junov@chromium.orgfb103892012-09-20 19:35:43 +0000718 silent ? kDeferredPostSilentFlushPlaybackAssertMessageFormat :
junov@chromium.org88e29142012-08-07 16:48:22 +0000719 kDeferredPostFlushPlaybackAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700720 AssertCanvasStatesEqual(reporter, d, deferredCanvas->immediateCanvas(),
721 &referenceCanvas, testStep);
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000722
junov@chromium.org88e29142012-08-07 16:48:22 +0000723 // Verified that deferred canvas state is not affected by flushing
724 // pending draw operations
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000725
junov@chromium.org88e29142012-08-07 16:48:22 +0000726 // The following test code is commented out because it currently fails.
727 // Issue: http://code.google.com/p/skia/issues/detail?id=496
728 /*
729 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
730 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
731 testStep);
732 */
733 }
734};
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000735
caryclark@google.com42639cd2012-06-06 12:03:39 +0000736// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000737static void TestProxyCanvasStateConsistency(
738 skiatest::Reporter* reporter,
piotaixrf05f5a72014-10-03 13:26:55 -0700739 const TestData& d,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000740 CanvasTestStep* testStep,
741 const SkCanvas& referenceCanvas) {
742
743 SkBitmap indirectStore;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000744 createBitmap(&indirectStore, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700745 SkCanvas indirectCanvas(indirectStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000746 SkProxyCanvas proxyCanvas(&indirectCanvas);
747 testStep->setAssertMessageFormat(kProxyDrawAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700748 testStep->draw(&proxyCanvas, d, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000749 // Verify that the SkProxyCanvas reports consitent state
750 testStep->setAssertMessageFormat(kProxyStateAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700751 AssertCanvasStatesEqual(reporter, d, &proxyCanvas, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000752 // Verify that the indirect canvas reports consitent state
753 testStep->setAssertMessageFormat(kProxyIndirectStateAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700754 AssertCanvasStatesEqual(reporter, d, &indirectCanvas, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000755}
756
caryclark@google.com42639cd2012-06-06 12:03:39 +0000757// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000758static void TestNWayCanvasStateConsistency(
759 skiatest::Reporter* reporter,
piotaixrf05f5a72014-10-03 13:26:55 -0700760 const TestData& d,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000761 CanvasTestStep* testStep,
762 const SkCanvas& referenceCanvas) {
763
764 SkBitmap indirectStore1;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000765 createBitmap(&indirectStore1, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700766 SkCanvas indirectCanvas1(indirectStore1);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000767
768 SkBitmap indirectStore2;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000769 createBitmap(&indirectStore2, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700770 SkCanvas indirectCanvas2(indirectStore2);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000771
djsollen@google.comf0a062b2012-05-01 16:50:25 +0000772 SkISize canvasSize = referenceCanvas.getDeviceSize();
773 SkNWayCanvas nWayCanvas(canvasSize.width(), canvasSize.height());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000774 nWayCanvas.addCanvas(&indirectCanvas1);
775 nWayCanvas.addCanvas(&indirectCanvas2);
776
777 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700778 testStep->draw(&nWayCanvas, d, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000779 // Verify that the SkProxyCanvas reports consitent state
780 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700781 AssertCanvasStatesEqual(reporter, d, &nWayCanvas, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000782 // Verify that the indirect canvases report consitent state
783 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700784 AssertCanvasStatesEqual(reporter, d, &indirectCanvas1, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000785 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700786 AssertCanvasStatesEqual(reporter, d, &indirectCanvas2, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000787}
788
789/*
790 * This sub-test verifies that the test step passes when executed
791 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
792 * that the all canvas derivatives report the same state as an SkCanvas
793 * after having executed the test step.
794 */
piotaixrf05f5a72014-10-03 13:26:55 -0700795static void TestOverrideStateConsistency(skiatest::Reporter* reporter, const TestData& d,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000796 CanvasTestStep* testStep) {
797 SkBitmap referenceStore;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000798 createBitmap(&referenceStore, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700799 SkCanvas referenceCanvas(referenceStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000800 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700801 testStep->draw(&referenceCanvas, d, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000802
piotaixrf05f5a72014-10-03 13:26:55 -0700803 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, d, testStep, referenceCanvas, false);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000804
piotaixrf05f5a72014-10-03 13:26:55 -0700805 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, d, testStep, referenceCanvas, true);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000806
caryclark@google.com42639cd2012-06-06 12:03:39 +0000807 // The following test code is disabled because SkProxyCanvas is
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000808 // missing a lot of virtual overrides on get* methods, which are used
809 // to verify canvas state.
810 // Issue: http://code.google.com/p/skia/issues/detail?id=500
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000811
caryclark@google.com42639cd2012-06-06 12:03:39 +0000812 if (false) { // avoid bit rot, suppress warning
piotaixrf05f5a72014-10-03 13:26:55 -0700813 TestProxyCanvasStateConsistency(reporter, d, testStep, referenceCanvas);
caryclark@google.com42639cd2012-06-06 12:03:39 +0000814 }
815
816 // The following test code is disabled because SkNWayCanvas does not
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000817 // report correct clipping and device bounds information
818 // Issue: http://code.google.com/p/skia/issues/detail?id=501
caryclark@google.com42639cd2012-06-06 12:03:39 +0000819
820 if (false) { // avoid bit rot, suppress warning
piotaixrf05f5a72014-10-03 13:26:55 -0700821 TestNWayCanvasStateConsistency(reporter, d, testStep, referenceCanvas);
caryclark@google.com42639cd2012-06-06 12:03:39 +0000822 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000823
caryclark@google.com42639cd2012-06-06 12:03:39 +0000824 if (false) { // avoid bit rot, suppress warning
825 test_clipVisitor(reporter, &referenceCanvas);
826 }
reed@google.com7c202932011-12-14 18:48:05 +0000827}
reed@google.com37f3ae02011-11-28 16:06:04 +0000828
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000829static void test_newraster(skiatest::Reporter* reporter) {
830 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
831 SkCanvas* canvas = SkCanvas::NewRaster(info);
832 REPORTER_ASSERT(reporter, canvas);
833
834 SkImageInfo info2;
835 size_t rowBytes;
836 const SkPMColor* addr = (const SkPMColor*)canvas->peekPixels(&info2, &rowBytes);
837 REPORTER_ASSERT(reporter, addr);
838 REPORTER_ASSERT(reporter, info == info2);
839 for (int y = 0; y < info.height(); ++y) {
840 for (int x = 0; x < info.width(); ++x) {
841 REPORTER_ASSERT(reporter, 0 == addr[x]);
842 }
843 addr = (const SkPMColor*)((const char*)addr + rowBytes);
844 }
845 SkDELETE(canvas);
846
847 // now try a deliberately bad info
reede5ea5002014-09-03 11:54:58 -0700848 info = info.makeWH(-1, info.height());
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000849 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRaster(info));
850
851 // too big
reede5ea5002014-09-03 11:54:58 -0700852 info = info.makeWH(1 << 30, 1 << 30);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000853 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRaster(info));
skia.committer@gmail.com0e530752014-02-28 03:02:05 +0000854
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000855 // not a valid pixel type
reede5ea5002014-09-03 11:54:58 -0700856 info = SkImageInfo::Make(10, 10, kUnknown_SkColorType, info.alphaType());
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000857 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRaster(info));
858
859 // We should succeed with a zero-sized valid info
860 info = SkImageInfo::MakeN32Premul(0, 0);
861 canvas = SkCanvas::NewRaster(info);
862 REPORTER_ASSERT(reporter, canvas);
863 SkDELETE(canvas);
864}
865
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000866DEF_TEST(Canvas, reporter) {
piotaixrf05f5a72014-10-03 13:26:55 -0700867 TestData d;
reed@google.com37f3ae02011-11-28 16:06:04 +0000868
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000869 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
piotaixrf05f5a72014-10-03 13:26:55 -0700870 TestOverrideStateConsistency(reporter, d, testStepArray()[testStep]);
edisonn@google.com77909122012-10-18 15:58:23 +0000871 if (testStepArray()[testStep]->enablePdfTesting()) {
piotaixrf05f5a72014-10-03 13:26:55 -0700872 TestPdfDevice(reporter, d, testStepArray()[testStep]);
edisonn@google.com77909122012-10-18 15:58:23 +0000873 }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000874 }
junov@chromium.orgcd62ecf2012-08-02 17:43:25 +0000875
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000876 test_newraster(reporter);
reed@google.com37f3ae02011-11-28 16:06:04 +0000877}