blob: 5aa3d4662863efb96a4fd0d00d6e8e3b8b851ef5 [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"
reed687fa1c2015-04-07 08:00:56 -070048#include "SkClipStack.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000049#include "SkDeferredCanvas.h"
50#include "SkDevice.h"
halcanary3d32d502015-03-01 06:55:20 -080051#include "SkDocument.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000052#include "SkMatrix.h"
53#include "SkNWayCanvas.h"
54#include "SkPaint.h"
fmalitaf433bb22015-08-17 08:05:13 -070055#include "SkPaintFilterCanvas.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000056#include "SkPath.h"
57#include "SkPicture.h"
58#include "SkPictureRecord.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000059#include "SkPictureRecorder.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000060#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) {
reed3054be12014-12-10 07:24:28 -080076 SkSurface* surface = SkSurface::NewRasterN32Premul(kWidth, kHeight);
piotaixrf05f5a72014-10-03 13:26:55 -070077 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;
tfarina567ff2f2015-04-27 07:01:44 -0700123 SkMatrix fMatrix;
piotaixrf05f5a72014-10-03 13:26:55 -0700124 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
mtklein36352bf2015-03-25 18:17:31 -0700195 void clipRect(const SkRect& r, SkRegion::Op op, bool aa) override {
reed@google.com90c07ea2012-04-13 13:50:27 +0000196 fTarget->clipRect(r, op, aa);
197 }
mtklein36352bf2015-03-25 18:17:31 -0700198 void clipRRect(const SkRRect& r, SkRegion::Op op, bool aa) override {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000199 fTarget->clipRRect(r, op, aa);
200 }
mtklein36352bf2015-03-25 18:17:31 -0700201 void clipPath(const SkPath& p, SkRegion::Op op, bool aa) 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
reed687fa1c2015-04-07 08:00:56 -0700222static void test_clipstack(skiatest::Reporter* reporter) {
223 // The clipstack is refcounted, and needs to be able to out-live the canvas if a client has
224 // ref'd it.
225 const SkClipStack* cs = NULL;
226 {
227 SkCanvas canvas(10, 10);
228 cs = SkRef(canvas.getClipStack());
229 }
230 REPORTER_ASSERT(reporter, cs->unique());
231 cs->unref();
232}
233
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000234// Format strings that describe the test context. The %s token is where
235// the name of the test step is inserted. The context is required for
236// disambiguating the error in the case of failures that are reported in
237// functions that are called multiple times in different contexts (test
238// cases and test steps).
239static const char* const kDefaultAssertMessageFormat = "%s";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000240static const char* const kCanvasDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000241 "Drawing test step %s with SkCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000242static const char* const kDeferredDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000243 "Drawing test step %s with SkDeferredCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000244static const char* const kNWayDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000245 "Drawing test step %s with SkNWayCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000246static const char* const kDeferredPreFlushAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000247 "test step %s, SkDeferredCanvas state consistency before flush";
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000248static const char* const kDeferredPostFlushPlaybackAssertMessageFormat =
249 "test step %s, SkDeferredCanvas playback canvas state consistency after flush";
junov@chromium.orgfb103892012-09-20 19:35:43 +0000250static const char* const kDeferredPostSilentFlushPlaybackAssertMessageFormat =
251 "test step %s, SkDeferredCanvas playback canvas state consistency after silent flush";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000252static const char* const kNWayStateAssertMessageFormat =
253 "test step %s, SkNWayCanvas state consistency";
254static const char* const kNWayIndirect1StateAssertMessageFormat =
255 "test step %s, SkNWayCanvas indirect canvas 1 state consistency";
256static const char* const kNWayIndirect2StateAssertMessageFormat =
257 "test step %s, SkNWayCanvas indirect canvas 2 state consistency";
edisonn@google.com77909122012-10-18 15:58:23 +0000258static const char* const kPdfAssertMessageFormat =
259 "PDF sanity check failed %s";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000260
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000261class CanvasTestStep;
262static SkTDArray<CanvasTestStep*>& testStepArray() {
263 static SkTDArray<CanvasTestStep*> theTests;
264 return theTests;
265}
266
267class CanvasTestStep {
268public:
edisonn@google.com77909122012-10-18 15:58:23 +0000269 CanvasTestStep(bool fEnablePdfTesting = true) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000270 *testStepArray().append() = this;
271 fAssertMessageFormat = kDefaultAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000272 this->fEnablePdfTesting = fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000273 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000274 virtual ~CanvasTestStep() { }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000275
piotaixrf05f5a72014-10-03 13:26:55 -0700276 virtual void draw(SkCanvas*, const TestData&, skiatest::Reporter*) = 0;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000277 virtual const char* name() const = 0;
278
279 const char* assertMessage() {
280 fAssertMessage.printf(fAssertMessageFormat, name());
281 return fAssertMessage.c_str();
282 }
283
284 void setAssertMessageFormat(const char* format) {
285 fAssertMessageFormat = format;
286 }
287
edisonn@google.com77909122012-10-18 15:58:23 +0000288 bool enablePdfTesting() { return fEnablePdfTesting; }
289
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000290private:
291 SkString fAssertMessage;
292 const char* fAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000293 bool fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000294};
295
296///////////////////////////////////////////////////////////////////////////////
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000297// Macros for defining test steps
298
299#define TEST_STEP(NAME, FUNCTION) \
300class NAME##_TestStep : public CanvasTestStep{ \
301public: \
piotaixrf05f5a72014-10-03 13:26:55 -0700302 virtual void draw(SkCanvas* canvas, const TestData& d, \
303 skiatest::Reporter* reporter) { \
304 FUNCTION (canvas, d, reporter, this); \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000305 } \
306 virtual const char* name() const {return #NAME ;} \
307}; \
308static NAME##_TestStep NAME##_TestStepInstance;
309
piotaixrf05f5a72014-10-03 13:26:55 -0700310#define TEST_STEP_NO_PDF(NAME, FUNCTION) \
edisonn@google.com77909122012-10-18 15:58:23 +0000311class NAME##_TestStep : public CanvasTestStep{ \
312public: \
313 NAME##_TestStep() : CanvasTestStep(false) {} \
piotaixrf05f5a72014-10-03 13:26:55 -0700314 virtual void draw(SkCanvas* canvas, const TestData& d, \
315 skiatest::Reporter* reporter) { \
316 FUNCTION (canvas, d, reporter, this); \
edisonn@google.com77909122012-10-18 15:58:23 +0000317 } \
318 virtual const char* name() const {return #NAME ;} \
319}; \
320static NAME##_TestStep NAME##_TestStepInstance;
321
piotaixrf05f5a72014-10-03 13:26:55 -0700322#define SIMPLE_TEST_STEP(NAME, CALL) \
323static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \
324 skiatest::Reporter*, CanvasTestStep*) { \
325 canvas-> CALL ; \
326} \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000327TEST_STEP(NAME, NAME##TestStep )
328
329#define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
piotaixrf05f5a72014-10-03 13:26:55 -0700330static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \
331 skiatest::Reporter*, CanvasTestStep* testStep) { \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000332 REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \
333 testStep->assertMessage()); \
334} \
335TEST_STEP(NAME, NAME##TestStep )
336
337
338///////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000339// Basic test steps for most virtual methods in SkCanvas that draw or affect
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000340// the state of the canvas.
341
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000342SIMPLE_TEST_STEP(Translate, translate(SkIntToScalar(1), SkIntToScalar(2)));
343SIMPLE_TEST_STEP(Scale, scale(SkIntToScalar(1), SkIntToScalar(2)));
344SIMPLE_TEST_STEP(Rotate, rotate(SkIntToScalar(1)));
345SIMPLE_TEST_STEP(Skew, skew(SkIntToScalar(1), SkIntToScalar(2)));
piotaixrf05f5a72014-10-03 13:26:55 -0700346SIMPLE_TEST_STEP(Concat, concat(d.fMatrix));
347SIMPLE_TEST_STEP(SetMatrix, setMatrix(d.fMatrix));
348SIMPLE_TEST_STEP(ClipRect, clipRect(d.fRect));
349SIMPLE_TEST_STEP(ClipPath, clipPath(d.fPath));
350SIMPLE_TEST_STEP(ClipRegion, clipRegion(d.fRegion, SkRegion::kReplace_Op));
351SIMPLE_TEST_STEP(Clear, clear(d.fColor));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000352
353///////////////////////////////////////////////////////////////////////////////
354// Complex test steps
355
piotaixrf05f5a72014-10-03 13:26:55 -0700356static void SaveMatrixClipStep(SkCanvas* canvas, const TestData& d,
357 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000358 int saveCount = canvas->getSaveCount();
Florin Malita5f6102d2014-06-30 10:13:28 -0400359 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000360 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
piotaixrf05f5a72014-10-03 13:26:55 -0700361 canvas->clipRegion(d.fRegion);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000362 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000363 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000364 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000365 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000366 testStep->assertMessage());
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000367// REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion, testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000368}
369TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
370
piotaixrf05f5a72014-10-03 13:26:55 -0700371static void SaveLayerStep(SkCanvas* canvas, const TestData& d,
372 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000373 int saveCount = canvas->getSaveCount();
374 canvas->saveLayer(NULL, NULL);
375 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000376 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000377 testStep->assertMessage());
378}
379TEST_STEP(SaveLayer, SaveLayerStep);
380
piotaixrf05f5a72014-10-03 13:26:55 -0700381static void BoundedSaveLayerStep(SkCanvas* canvas, const TestData& d,
382 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000383 int saveCount = canvas->getSaveCount();
piotaixrf05f5a72014-10-03 13:26:55 -0700384 canvas->saveLayer(&d.fRect, NULL);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000385 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000386 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000387 testStep->assertMessage());
388}
389TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
390
piotaixrf05f5a72014-10-03 13:26:55 -0700391static void PaintSaveLayerStep(SkCanvas* canvas, const TestData& d,
392 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000393 int saveCount = canvas->getSaveCount();
piotaixrf05f5a72014-10-03 13:26:55 -0700394 canvas->saveLayer(NULL, &d.fPaint);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000395 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000396 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000397 testStep->assertMessage());
398}
399TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
400
piotaixrf05f5a72014-10-03 13:26:55 -0700401static void TwoClipOpsStep(SkCanvas* canvas, const TestData& d,
402 skiatest::Reporter*, CanvasTestStep*) {
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000403 // This test exercises a functionality in SkPicture that leads to the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000404 // recording of restore offset placeholders. This test will trigger an
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000405 // assertion at playback time if the placeholders are not properly
406 // filled when the recording ends.
piotaixrf05f5a72014-10-03 13:26:55 -0700407 canvas->clipRect(d.fRect);
408 canvas->clipRegion(d.fRegion);
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000409}
410TEST_STEP(TwoClipOps, TwoClipOpsStep);
411
epoger@google.com94fa43c2012-04-11 17:51:01 +0000412// exercise fix for http://code.google.com/p/skia/issues/detail?id=560
413// ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
piotaixrf05f5a72014-10-03 13:26:55 -0700414static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas, const TestData& d,
415 skiatest::Reporter*, CanvasTestStep*) {
epoger@google.com94fa43c2012-04-11 17:51:01 +0000416 SkPaint paint;
417 paint.setStrokeWidth(SkIntToScalar(1));
418 paint.setStyle(SkPaint::kStroke_Style);
419
piotaixrf05f5a72014-10-03 13:26:55 -0700420 canvas->drawPath(d.fNearlyZeroLengthPath, paint);
epoger@google.com94fa43c2012-04-11 17:51:01 +0000421}
422TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
423
piotaixrf05f5a72014-10-03 13:26:55 -0700424static void DrawVerticesShaderTestStep(SkCanvas* canvas, const TestData& d,
425 skiatest::Reporter*, CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000426 SkPoint pts[4];
427 pts[0].set(0, 0);
piotaixrf05f5a72014-10-03 13:26:55 -0700428 pts[1].set(SkIntToScalar(d.fWidth), 0);
429 pts[2].set(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight));
430 pts[3].set(0, SkIntToScalar(d.fHeight));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000431 SkPaint paint;
piotaixrf05f5a72014-10-03 13:26:55 -0700432 SkShader* shader = SkShader::CreateBitmapShader(d.fBitmap,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000433 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
434 paint.setShader(shader)->unref();
435 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
436 NULL, NULL, NULL, 0, paint);
437}
edisonn@google.com77909122012-10-18 15:58:23 +0000438// NYI: issue 240.
439TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000440
piotaixrf05f5a72014-10-03 13:26:55 -0700441static void DrawPictureTestStep(SkCanvas* canvas, const TestData& d,
442 skiatest::Reporter*, CanvasTestStep*) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000443 SkPictureRecorder recorder;
piotaixrf05f5a72014-10-03 13:26:55 -0700444 SkCanvas* testCanvas = recorder.beginRecording(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight),
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700445 NULL, 0);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000446 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
piotaixrf05f5a72014-10-03 13:26:55 -0700447 testCanvas->clipRect(d.fRect);
448 testCanvas->drawRect(d.fRect, d.fPaint);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000449 SkAutoTUnref<SkPicture> testPicture(recorder.endRecording());
450
robertphillips9b14f262014-06-04 05:40:44 -0700451 canvas->drawPicture(testPicture);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000452}
453TEST_STEP(DrawPicture, DrawPictureTestStep);
454
piotaixrf05f5a72014-10-03 13:26:55 -0700455static void SaveRestoreTestStep(SkCanvas* canvas, const TestData& d,
456 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000457 int baseSaveCount = canvas->getSaveCount();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000458 int n = canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000459 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount == n, testStep->assertMessage());
460 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000461 testStep->assertMessage());
462 canvas->save();
463 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000464 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000465 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000466 canvas->restoreToCount(baseSaveCount + 1);
467 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000468 testStep->assertMessage());
469
470 // should this pin to 1, or be a no-op, or crash?
471 canvas->restoreToCount(0);
472 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
473 testStep->assertMessage());
474}
475TEST_STEP(SaveRestore, SaveRestoreTestStep);
476
piotaixrf05f5a72014-10-03 13:26:55 -0700477static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas, const TestData& d,
478 skiatest::Reporter*, CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000479 // This test step challenges the TestDeferredCanvasStateConsistency
480 // test cases because the opaque paint can trigger an optimization
481 // that discards previously recorded commands. The challenge is to maintain
482 // correct clip and matrix stack state.
483 canvas->resetMatrix();
484 canvas->rotate(SkIntToScalar(30));
485 canvas->save();
486 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
487 canvas->save();
488 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
489 SkPaint paint;
490 paint.setColor(0xFFFFFFFF);
491 canvas->drawPaint(paint);
492 canvas->restore();
493 canvas->restore();
494}
495TEST_STEP(NestedSaveRestoreWithSolidPaint, \
496 NestedSaveRestoreWithSolidPaintTestStep);
497
piotaixrf05f5a72014-10-03 13:26:55 -0700498static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas, const TestData& d,
499 skiatest::Reporter*, CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000500 // This test step challenges the TestDeferredCanvasStateConsistency
501 // test case because the canvas flush on a deferred canvas will
502 // reset the recording session. The challenge is to maintain correct
503 // clip and matrix stack state on the playback canvas.
504 canvas->resetMatrix();
505 canvas->rotate(SkIntToScalar(30));
506 canvas->save();
507 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
508 canvas->save();
509 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
piotaixrf05f5a72014-10-03 13:26:55 -0700510 canvas->drawRect(d.fRect,d.fPaint);
reed@google.com3b3e8952012-08-16 20:53:31 +0000511 canvas->flush();
512 canvas->restore();
513 canvas->restore();
514}
piotaixrf05f5a72014-10-03 13:26:55 -0700515TEST_STEP(NestedSaveRestoreWithFlush, NestedSaveRestoreWithFlushTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000516
piotaixrf05f5a72014-10-03 13:26:55 -0700517static void AssertCanvasStatesEqual(skiatest::Reporter* reporter, const TestData& d,
518 const SkCanvas* canvas1, const SkCanvas* canvas2,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000519 CanvasTestStep* testStep) {
520 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
521 canvas2->getDeviceSize(), testStep->assertMessage());
522 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
523 canvas2->getSaveCount(), testStep->assertMessage());
reed@google.com3b3e8952012-08-16 20:53:31 +0000524
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000525 SkRect bounds1, bounds2;
526 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.com3b3e8952012-08-16 20:53:31 +0000527 canvas1->getClipBounds(&bounds1) == canvas2->getClipBounds(&bounds2),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000528 testStep->assertMessage());
529 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
reed@google.com3b3e8952012-08-16 20:53:31 +0000530 testStep->assertMessage());
531
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000532 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
533 canvas2->getDrawFilter(), testStep->assertMessage());
534 SkIRect deviceBounds1, deviceBounds2;
535 REPORTER_ASSERT_MESSAGE(reporter,
536 canvas1->getClipDeviceBounds(&deviceBounds1) ==
537 canvas2->getClipDeviceBounds(&deviceBounds2),
538 testStep->assertMessage());
reed868074b2014-06-03 10:53:59 -0700539 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2, testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000540 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
541 canvas2->getTotalMatrix(), testStep->assertMessage());
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000542 REPORTER_ASSERT_MESSAGE(reporter, equal_clips(*canvas1, *canvas2), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000543
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000544 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
545 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
546 while (!layerIter1.done() && !layerIter2.done()) {
547 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
548 layerIter2.matrix(), testStep->assertMessage());
549 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
550 layerIter2.clip(), testStep->assertMessage());
551 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
552 layerIter2.paint(), testStep->assertMessage());
553 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
554 layerIter2.x(), testStep->assertMessage());
555 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
556 layerIter2.y(), testStep->assertMessage());
557 layerIter1.next();
558 layerIter2.next();
559 }
560 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
561 testStep->assertMessage());
562 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
563 testStep->assertMessage());
piotaixr76993ed2014-10-27 15:31:34 -0700564
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000565}
566
edisonn@google.com77909122012-10-18 15:58:23 +0000567static void TestPdfDevice(skiatest::Reporter* reporter,
piotaixrf05f5a72014-10-03 13:26:55 -0700568 const TestData& d,
edisonn@google.com77909122012-10-18 15:58:23 +0000569 CanvasTestStep* testStep) {
halcanary3d32d502015-03-01 06:55:20 -0800570 SkDynamicMemoryWStream outStream;
571 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&outStream));
halcanary8ee06f22015-08-11 10:30:12 -0700572#if SK_SUPPORT_PDF
573 REPORTER_ASSERT(reporter, doc);
halcanary2ccdb632015-08-11 13:35:12 -0700574#else
575 REPORTER_ASSERT(reporter, !doc);
576#endif // SK_SUPPORT_PDF
577 if (!doc) {
578 return;
579 }
halcanary3d32d502015-03-01 06:55:20 -0800580 SkCanvas* canvas = doc->beginPage(SkIntToScalar(d.fWidth),
581 SkIntToScalar(d.fHeight));
582 REPORTER_ASSERT(reporter, canvas);
edisonn@google.com77909122012-10-18 15:58:23 +0000583 testStep->setAssertMessageFormat(kPdfAssertMessageFormat);
halcanary3d32d502015-03-01 06:55:20 -0800584 testStep->draw(canvas, d, reporter);
585
586 REPORTER_ASSERT(reporter, doc->close());
edisonn@google.com77909122012-10-18 15:58:23 +0000587}
588
junov@chromium.org88e29142012-08-07 16:48:22 +0000589// The following class groups static functions that need to access
590// the privates members of SkDeferredCanvas
591class SkDeferredCanvasTester {
592public:
593 static void TestDeferredCanvasStateConsistency(
594 skiatest::Reporter* reporter,
piotaixrf05f5a72014-10-03 13:26:55 -0700595 const TestData& d,
junov@chromium.org88e29142012-08-07 16:48:22 +0000596 CanvasTestStep* testStep,
junov@chromium.orgfb103892012-09-20 19:35:43 +0000597 const SkCanvas& referenceCanvas, bool silent) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000598
reed@google.com28183b42014-02-04 15:34:10 +0000599 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
600 SkAutoTUnref<SkDeferredCanvas> deferredCanvas(SkDeferredCanvas::Create(surface.get()));
601
junov@chromium.org88e29142012-08-07 16:48:22 +0000602 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700603 testStep->draw(deferredCanvas, d, reporter);
junov@chromium.org88e29142012-08-07 16:48:22 +0000604 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700605 AssertCanvasStatesEqual(reporter, d, deferredCanvas, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000606
junov@chromium.orgfb103892012-09-20 19:35:43 +0000607 if (silent) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000608 deferredCanvas->silentFlush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000609 } else {
junov@chromium.org66070a52013-05-28 17:39:08 +0000610 deferredCanvas->flush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000611 }
612
skia.committer@gmail.com4c5ea442012-09-21 02:01:01 +0000613 testStep->setAssertMessageFormat(
junov@chromium.orgfb103892012-09-20 19:35:43 +0000614 silent ? kDeferredPostSilentFlushPlaybackAssertMessageFormat :
junov@chromium.org88e29142012-08-07 16:48:22 +0000615 kDeferredPostFlushPlaybackAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700616 AssertCanvasStatesEqual(reporter, d, deferredCanvas->immediateCanvas(),
617 &referenceCanvas, testStep);
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000618
junov@chromium.org88e29142012-08-07 16:48:22 +0000619 // Verified that deferred canvas state is not affected by flushing
620 // pending draw operations
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000621
junov@chromium.org88e29142012-08-07 16:48:22 +0000622 // The following test code is commented out because it currently fails.
623 // Issue: http://code.google.com/p/skia/issues/detail?id=496
624 /*
625 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
626 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
627 testStep);
628 */
629 }
630};
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000631
caryclark@google.com42639cd2012-06-06 12:03:39 +0000632// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000633static void TestNWayCanvasStateConsistency(
634 skiatest::Reporter* reporter,
piotaixrf05f5a72014-10-03 13:26:55 -0700635 const TestData& d,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000636 CanvasTestStep* testStep,
637 const SkCanvas& referenceCanvas) {
638
639 SkBitmap indirectStore1;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000640 createBitmap(&indirectStore1, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700641 SkCanvas indirectCanvas1(indirectStore1);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000642
643 SkBitmap indirectStore2;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000644 createBitmap(&indirectStore2, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700645 SkCanvas indirectCanvas2(indirectStore2);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000646
djsollen@google.comf0a062b2012-05-01 16:50:25 +0000647 SkISize canvasSize = referenceCanvas.getDeviceSize();
648 SkNWayCanvas nWayCanvas(canvasSize.width(), canvasSize.height());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000649 nWayCanvas.addCanvas(&indirectCanvas1);
650 nWayCanvas.addCanvas(&indirectCanvas2);
651
652 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700653 testStep->draw(&nWayCanvas, d, reporter);
scroggo648238c2015-01-29 11:58:51 -0800654 // Verify that the SkNWayCanvas reports consitent state
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000655 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700656 AssertCanvasStatesEqual(reporter, d, &nWayCanvas, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000657 // Verify that the indirect canvases report consitent state
658 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700659 AssertCanvasStatesEqual(reporter, d, &indirectCanvas1, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000660 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700661 AssertCanvasStatesEqual(reporter, d, &indirectCanvas2, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000662}
663
664/*
665 * This sub-test verifies that the test step passes when executed
666 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
667 * that the all canvas derivatives report the same state as an SkCanvas
668 * after having executed the test step.
669 */
piotaixrf05f5a72014-10-03 13:26:55 -0700670static void TestOverrideStateConsistency(skiatest::Reporter* reporter, const TestData& d,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000671 CanvasTestStep* testStep) {
672 SkBitmap referenceStore;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000673 createBitmap(&referenceStore, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700674 SkCanvas referenceCanvas(referenceStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000675 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700676 testStep->draw(&referenceCanvas, d, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000677
piotaixrf05f5a72014-10-03 13:26:55 -0700678 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, d, testStep, referenceCanvas, false);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000679
piotaixrf05f5a72014-10-03 13:26:55 -0700680 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, d, testStep, referenceCanvas, true);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000681
caryclark@google.com42639cd2012-06-06 12:03:39 +0000682 // The following test code is disabled because SkNWayCanvas does not
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000683 // report correct clipping and device bounds information
684 // Issue: http://code.google.com/p/skia/issues/detail?id=501
caryclark@google.com42639cd2012-06-06 12:03:39 +0000685
686 if (false) { // avoid bit rot, suppress warning
piotaixrf05f5a72014-10-03 13:26:55 -0700687 TestNWayCanvasStateConsistency(reporter, d, testStep, referenceCanvas);
caryclark@google.com42639cd2012-06-06 12:03:39 +0000688 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000689
caryclark@google.com42639cd2012-06-06 12:03:39 +0000690 if (false) { // avoid bit rot, suppress warning
691 test_clipVisitor(reporter, &referenceCanvas);
692 }
reed687fa1c2015-04-07 08:00:56 -0700693 test_clipstack(reporter);
reed@google.com7c202932011-12-14 18:48:05 +0000694}
reed@google.com37f3ae02011-11-28 16:06:04 +0000695
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000696static void test_newraster(skiatest::Reporter* reporter) {
697 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
reed3054be12014-12-10 07:24:28 -0800698 const size_t minRowBytes = info.minRowBytes();
699 const size_t size = info.getSafeSize(minRowBytes);
700 SkAutoMalloc storage(size);
701 SkPMColor* baseAddr = static_cast<SkPMColor*>(storage.get());
702 sk_bzero(baseAddr, size);
703
704 SkCanvas* canvas = SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000705 REPORTER_ASSERT(reporter, canvas);
706
707 SkImageInfo info2;
708 size_t rowBytes;
709 const SkPMColor* addr = (const SkPMColor*)canvas->peekPixels(&info2, &rowBytes);
710 REPORTER_ASSERT(reporter, addr);
711 REPORTER_ASSERT(reporter, info == info2);
reed3054be12014-12-10 07:24:28 -0800712 REPORTER_ASSERT(reporter, minRowBytes == rowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000713 for (int y = 0; y < info.height(); ++y) {
714 for (int x = 0; x < info.width(); ++x) {
715 REPORTER_ASSERT(reporter, 0 == addr[x]);
716 }
717 addr = (const SkPMColor*)((const char*)addr + rowBytes);
718 }
719 SkDELETE(canvas);
720
721 // now try a deliberately bad info
reede5ea5002014-09-03 11:54:58 -0700722 info = info.makeWH(-1, info.height());
reed3054be12014-12-10 07:24:28 -0800723 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000724
725 // too big
reede5ea5002014-09-03 11:54:58 -0700726 info = info.makeWH(1 << 30, 1 << 30);
reed3054be12014-12-10 07:24:28 -0800727 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
skia.committer@gmail.com0e530752014-02-28 03:02:05 +0000728
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000729 // not a valid pixel type
reede5ea5002014-09-03 11:54:58 -0700730 info = SkImageInfo::Make(10, 10, kUnknown_SkColorType, info.alphaType());
reed3054be12014-12-10 07:24:28 -0800731 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000732
733 // We should succeed with a zero-sized valid info
734 info = SkImageInfo::MakeN32Premul(0, 0);
reed3054be12014-12-10 07:24:28 -0800735 canvas = SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000736 REPORTER_ASSERT(reporter, canvas);
737 SkDELETE(canvas);
738}
739
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000740DEF_TEST(Canvas, reporter) {
piotaixrf05f5a72014-10-03 13:26:55 -0700741 TestData d;
reed@google.com37f3ae02011-11-28 16:06:04 +0000742
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000743 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
piotaixrf05f5a72014-10-03 13:26:55 -0700744 TestOverrideStateConsistency(reporter, d, testStepArray()[testStep]);
edisonn@google.com77909122012-10-18 15:58:23 +0000745 if (testStepArray()[testStep]->enablePdfTesting()) {
piotaixrf05f5a72014-10-03 13:26:55 -0700746 TestPdfDevice(reporter, d, testStepArray()[testStep]);
edisonn@google.com77909122012-10-18 15:58:23 +0000747 }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000748 }
junov@chromium.orgcd62ecf2012-08-02 17:43:25 +0000749
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000750 test_newraster(reporter);
reed@google.com37f3ae02011-11-28 16:06:04 +0000751}
reedf0090cb2014-11-26 08:55:51 -0800752
753DEF_TEST(Canvas_SaveState, reporter) {
754 SkCanvas canvas(10, 10);
755 REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
756
757 int n = canvas.save();
758 REPORTER_ASSERT(reporter, 1 == n);
759 REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
760
761 n = canvas.saveLayer(NULL, NULL);
762 REPORTER_ASSERT(reporter, 2 == n);
763 REPORTER_ASSERT(reporter, 3 == canvas.getSaveCount());
764
765 canvas.restore();
766 REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
767 canvas.restore();
768 REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
769}
reedc1b11f12015-03-13 08:48:26 -0700770
771DEF_TEST(Canvas_ClipEmptyPath, reporter) {
772 SkCanvas canvas(10, 10);
773 canvas.save();
774 SkPath path;
775 canvas.clipPath(path);
776 canvas.restore();
777 canvas.save();
778 path.moveTo(5, 5);
779 canvas.clipPath(path);
780 canvas.restore();
781 canvas.save();
782 path.moveTo(7, 7);
783 canvas.clipPath(path); // should not assert here
784 canvas.restore();
785}
fmalitaf433bb22015-08-17 08:05:13 -0700786
787namespace {
788
789class MockFilterCanvas : public SkPaintFilterCanvas {
790public:
791 MockFilterCanvas(SkCanvas* canvas) : INHERITED(canvas) { }
792
793protected:
794 void onFilterPaint(SkPaint *paint, Type type) const override { }
795
796private:
797 typedef SkPaintFilterCanvas INHERITED;
798};
799
800} // anonymous namespace
801
802// SkPaintFilterCanvas should inherit the initial target canvas state.
803DEF_TEST(PaintFilterCanvas_ConsistentState, reporter) {
804 SkCanvas canvas(100, 100);
805 canvas.clipRect(SkRect::MakeXYWH(12.7f, 12.7f, 75, 75));
806 canvas.scale(0.5f, 0.75f);
807
808 SkRect clip1, clip2;
809
810 MockFilterCanvas filterCanvas(&canvas);
811 REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix());
812 REPORTER_ASSERT(reporter, canvas.getClipBounds(&clip1) == filterCanvas.getClipBounds(&clip2));
813 REPORTER_ASSERT(reporter, clip1 == clip2);
814
815 filterCanvas.clipRect(SkRect::MakeXYWH(30.5f, 30.7f, 100, 100));
816 filterCanvas.scale(0.75f, 0.5f);
817 REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix());
818 REPORTER_ASSERT(reporter, canvas.getClipBounds(&clip1) == filterCanvas.getClipBounds(&clip2));
819 REPORTER_ASSERT(reporter, clip1 == clip2);
820}