blob: 70c2c04ffabf511239239fe8fe3cf91ab78582af [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,
tomhudsoncb3bd182016-05-18 07:24:16 -070021 * const TestData& d,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000022 * skiatest::Reporter* reporter,
23 * CanvasTestStep* testStep)
24 * {
25 * canvas->someCanvasAPImethod();
26 * (...)
27 * REPORTER_ASSERT_MESSAGE(reporter, (...), \
28 * testStep->assertMessage());
29 * }
30 *
31 * The definition of the test step function should be followed by an
32 * invocation of the TEST_STEP macro, which generates a class and
33 * instance for the test step:
34 *
35 * TEST_STEP(MyTestStep, MyTestStepFunction)
36 *
37 * There are also short hand macros for defining simple test steps
38 * in a single line of code. A simple test step is a one that is made
39 * of a single canvas API call.
40 *
41 * SIMPLE_TEST_STEP(MytestStep, someCanvasAPIMethod());
42 *
43 * There is another macro called SIMPLE_TEST_STEP_WITH_ASSERT that
44 * works the same way as SIMPLE_TEST_STEP, and additionally verifies
45 * that the invoked method returns a non-zero value.
46 */
reed@google.com37f3ae02011-11-28 16:06:04 +000047#include "SkBitmap.h"
48#include "SkCanvas.h"
reed687fa1c2015-04-07 08:00:56 -070049#include "SkClipStack.h"
halcanary3d32d502015-03-01 06:55:20 -080050#include "SkDocument.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000051#include "SkMatrix.h"
52#include "SkNWayCanvas.h"
53#include "SkPaint.h"
fmalitaf433bb22015-08-17 08:05:13 -070054#include "SkPaintFilterCanvas.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000055#include "SkPath.h"
56#include "SkPicture.h"
57#include "SkPictureRecord.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000058#include "SkPictureRecorder.h"
reed1e7f5e72016-04-27 07:49:17 -070059#include "SkRasterClip.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"
scroggo565901d2015-12-10 10:44:13 -080065#include "SkTemplates.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000066#include "SkTDArray.h"
67#include "Test.h"
reed@google.com37f3ae02011-11-28 16:06:04 +000068
piotaixrf05f5a72014-10-03 13:26:55 -070069static const int kWidth = 2, kHeight = 2;
70
71static void createBitmap(SkBitmap* bm, SkColor color) {
72 bm->allocN32Pixels(kWidth, kHeight);
73 bm->eraseColor(color);
74}
75
piotaixrf05f5a72014-10-03 13:26:55 -070076///////////////////////////////////////////////////////////////////////////////
77// Constants used by test steps
78const SkPoint kTestPoints[] = {
79 {SkIntToScalar(0), SkIntToScalar(0)},
80 {SkIntToScalar(2), SkIntToScalar(1)},
81 {SkIntToScalar(0), SkIntToScalar(2)}
82};
83const SkPoint kTestPoints2[] = {
84 { SkIntToScalar(0), SkIntToScalar(1) },
85 { SkIntToScalar(1), SkIntToScalar(1) },
86 { SkIntToScalar(2), SkIntToScalar(1) },
87 { SkIntToScalar(3), SkIntToScalar(1) },
88 { SkIntToScalar(4), SkIntToScalar(1) },
89 { SkIntToScalar(5), SkIntToScalar(1) },
90 { SkIntToScalar(6), SkIntToScalar(1) },
91 { SkIntToScalar(7), SkIntToScalar(1) },
92 { SkIntToScalar(8), SkIntToScalar(1) },
93 { SkIntToScalar(9), SkIntToScalar(1) },
94 { SkIntToScalar(10), SkIntToScalar(1) }
95};
96
97struct TestData {
98public:
99 TestData()
100 : fRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
101 SkIntToScalar(2), SkIntToScalar(1)))
102 , fMatrix(TestMatrix())
103 , fPath(TestPath())
104 , fNearlyZeroLengthPath(TestNearlyZeroLengthPath())
105 , fIRect(SkIRect::MakeXYWH(0, 0, 2, 1))
106 , fRegion(TestRegion())
107 , fColor(0x01020304)
108 , fPoints(kTestPoints)
109 , fPointCount(3)
110 , fWidth(2)
111 , fHeight(2)
112 , fText("Hello World")
113 , fPoints2(kTestPoints2)
114 , fBitmap(TestBitmap())
115 { }
116
117 SkRect fRect;
tfarina567ff2f2015-04-27 07:01:44 -0700118 SkMatrix fMatrix;
piotaixrf05f5a72014-10-03 13:26:55 -0700119 SkPath fPath;
120 SkPath fNearlyZeroLengthPath;
121 SkIRect fIRect;
122 SkRegion fRegion;
123 SkColor fColor;
124 SkPaint fPaint;
125 const SkPoint* fPoints;
126 size_t fPointCount;
127 int fWidth;
128 int fHeight;
129 SkString fText;
130 const SkPoint* fPoints2;
131 SkBitmap fBitmap;
132
133private:
134 static SkMatrix TestMatrix() {
135 SkMatrix matrix;
136 matrix.reset();
137 matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
138
139 return matrix;
140 }
141 static SkPath TestPath() {
142 SkPath path;
143 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
144 SkIntToScalar(2), SkIntToScalar(1)));
145 return path;
146 }
147 static SkPath TestNearlyZeroLengthPath() {
148 SkPath path;
149 SkPoint pt1 = { 0, 0 };
150 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
151 SkPoint pt3 = { SkIntToScalar(1), 0 };
152 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
153 path.moveTo(pt1);
154 path.lineTo(pt2);
155 path.lineTo(pt3);
156 path.lineTo(pt4);
157 return path;
158 }
159 static SkRegion TestRegion() {
160 SkRegion region;
161 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
162 region.setRect(rect);
163 return region;
164 }
165 static SkBitmap TestBitmap() {
166 SkBitmap bitmap;
167 createBitmap(&bitmap, 0x05060708);
168 return bitmap;
169 }
170};
171
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000172static bool equal_clips(const SkCanvas& a, const SkCanvas& b) {
173 if (a.isClipEmpty()) {
174 return b.isClipEmpty();
175 }
176 if (!a.isClipRect()) {
177 // this is liberally true, since we don't expose a way to know this exactly (for non-rects)
178 return !b.isClipRect();
179 }
180 SkIRect ar, br;
181 a.getClipDeviceBounds(&ar);
182 b.getClipDeviceBounds(&br);
183 return ar == br;
184}
185
reed@google.com90c07ea2012-04-13 13:50:27 +0000186class Canvas2CanvasClipVisitor : public SkCanvas::ClipVisitor {
187public:
188 Canvas2CanvasClipVisitor(SkCanvas* target) : fTarget(target) {}
189
mtklein36352bf2015-03-25 18:17:31 -0700190 void clipRect(const SkRect& r, SkRegion::Op op, bool aa) override {
reed@google.com90c07ea2012-04-13 13:50:27 +0000191 fTarget->clipRect(r, op, aa);
192 }
mtklein36352bf2015-03-25 18:17:31 -0700193 void clipRRect(const SkRRect& r, SkRegion::Op op, bool aa) override {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000194 fTarget->clipRRect(r, op, aa);
195 }
mtklein36352bf2015-03-25 18:17:31 -0700196 void clipPath(const SkPath& p, SkRegion::Op op, bool aa) override {
reed@google.com90c07ea2012-04-13 13:50:27 +0000197 fTarget->clipPath(p, op, aa);
198 }
199
200private:
201 SkCanvas* fTarget;
202};
203
204static void test_clipVisitor(skiatest::Reporter* reporter, SkCanvas* canvas) {
205 SkISize size = canvas->getDeviceSize();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000206
reed@google.com90c07ea2012-04-13 13:50:27 +0000207 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000208 bm.setInfo(SkImageInfo::MakeN32Premul(size.width(), size.height()));
reed@google.com90c07ea2012-04-13 13:50:27 +0000209 SkCanvas c(bm);
210
211 Canvas2CanvasClipVisitor visitor(&c);
212 canvas->replayClips(&visitor);
213
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000214 REPORTER_ASSERT(reporter, equal_clips(c, *canvas));
reed@google.com90c07ea2012-04-13 13:50:27 +0000215}
216
reed687fa1c2015-04-07 08:00:56 -0700217static void test_clipstack(skiatest::Reporter* reporter) {
218 // The clipstack is refcounted, and needs to be able to out-live the canvas if a client has
219 // ref'd it.
halcanary96fcdcc2015-08-27 07:41:13 -0700220 const SkClipStack* cs = nullptr;
reed687fa1c2015-04-07 08:00:56 -0700221 {
222 SkCanvas canvas(10, 10);
223 cs = SkRef(canvas.getClipStack());
224 }
225 REPORTER_ASSERT(reporter, cs->unique());
226 cs->unref();
227}
228
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000229// Format strings that describe the test context. The %s token is where
230// the name of the test step is inserted. The context is required for
231// disambiguating the error in the case of failures that are reported in
232// functions that are called multiple times in different contexts (test
233// cases and test steps).
234static const char* const kDefaultAssertMessageFormat = "%s";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000235static const char* const kCanvasDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000236 "Drawing test step %s with SkCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000237static const char* const kNWayDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000238 "Drawing test step %s with SkNWayCanvas";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000239static const char* const kNWayStateAssertMessageFormat =
240 "test step %s, SkNWayCanvas state consistency";
241static const char* const kNWayIndirect1StateAssertMessageFormat =
242 "test step %s, SkNWayCanvas indirect canvas 1 state consistency";
243static const char* const kNWayIndirect2StateAssertMessageFormat =
244 "test step %s, SkNWayCanvas indirect canvas 2 state consistency";
edisonn@google.com77909122012-10-18 15:58:23 +0000245static const char* const kPdfAssertMessageFormat =
246 "PDF sanity check failed %s";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000247
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000248class CanvasTestStep;
249static SkTDArray<CanvasTestStep*>& testStepArray() {
250 static SkTDArray<CanvasTestStep*> theTests;
251 return theTests;
252}
253
254class CanvasTestStep {
255public:
edisonn@google.com77909122012-10-18 15:58:23 +0000256 CanvasTestStep(bool fEnablePdfTesting = true) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000257 *testStepArray().append() = this;
258 fAssertMessageFormat = kDefaultAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000259 this->fEnablePdfTesting = fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000260 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000261 virtual ~CanvasTestStep() { }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000262
piotaixrf05f5a72014-10-03 13:26:55 -0700263 virtual void draw(SkCanvas*, const TestData&, skiatest::Reporter*) = 0;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000264 virtual const char* name() const = 0;
265
266 const char* assertMessage() {
267 fAssertMessage.printf(fAssertMessageFormat, name());
268 return fAssertMessage.c_str();
269 }
270
271 void setAssertMessageFormat(const char* format) {
272 fAssertMessageFormat = format;
273 }
274
edisonn@google.com77909122012-10-18 15:58:23 +0000275 bool enablePdfTesting() { return fEnablePdfTesting; }
276
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000277private:
278 SkString fAssertMessage;
279 const char* fAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000280 bool fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000281};
282
283///////////////////////////////////////////////////////////////////////////////
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000284// Macros for defining test steps
285
286#define TEST_STEP(NAME, FUNCTION) \
287class NAME##_TestStep : public CanvasTestStep{ \
288public: \
piotaixrf05f5a72014-10-03 13:26:55 -0700289 virtual void draw(SkCanvas* canvas, const TestData& d, \
290 skiatest::Reporter* reporter) { \
291 FUNCTION (canvas, d, reporter, this); \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000292 } \
293 virtual const char* name() const {return #NAME ;} \
294}; \
295static NAME##_TestStep NAME##_TestStepInstance;
296
piotaixrf05f5a72014-10-03 13:26:55 -0700297#define TEST_STEP_NO_PDF(NAME, FUNCTION) \
edisonn@google.com77909122012-10-18 15:58:23 +0000298class NAME##_TestStep : public CanvasTestStep{ \
299public: \
300 NAME##_TestStep() : CanvasTestStep(false) {} \
piotaixrf05f5a72014-10-03 13:26:55 -0700301 virtual void draw(SkCanvas* canvas, const TestData& d, \
302 skiatest::Reporter* reporter) { \
303 FUNCTION (canvas, d, reporter, this); \
edisonn@google.com77909122012-10-18 15:58:23 +0000304 } \
305 virtual const char* name() const {return #NAME ;} \
306}; \
307static NAME##_TestStep NAME##_TestStepInstance;
308
piotaixrf05f5a72014-10-03 13:26:55 -0700309#define SIMPLE_TEST_STEP(NAME, CALL) \
310static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \
311 skiatest::Reporter*, CanvasTestStep*) { \
312 canvas-> CALL ; \
313} \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000314TEST_STEP(NAME, NAME##TestStep )
315
316#define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
piotaixrf05f5a72014-10-03 13:26:55 -0700317static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \
318 skiatest::Reporter*, CanvasTestStep* testStep) { \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000319 REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \
320 testStep->assertMessage()); \
321} \
322TEST_STEP(NAME, NAME##TestStep )
323
324
325///////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000326// Basic test steps for most virtual methods in SkCanvas that draw or affect
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000327// the state of the canvas.
328
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000329SIMPLE_TEST_STEP(Translate, translate(SkIntToScalar(1), SkIntToScalar(2)));
330SIMPLE_TEST_STEP(Scale, scale(SkIntToScalar(1), SkIntToScalar(2)));
331SIMPLE_TEST_STEP(Rotate, rotate(SkIntToScalar(1)));
332SIMPLE_TEST_STEP(Skew, skew(SkIntToScalar(1), SkIntToScalar(2)));
piotaixrf05f5a72014-10-03 13:26:55 -0700333SIMPLE_TEST_STEP(Concat, concat(d.fMatrix));
334SIMPLE_TEST_STEP(SetMatrix, setMatrix(d.fMatrix));
335SIMPLE_TEST_STEP(ClipRect, clipRect(d.fRect));
336SIMPLE_TEST_STEP(ClipPath, clipPath(d.fPath));
337SIMPLE_TEST_STEP(ClipRegion, clipRegion(d.fRegion, SkRegion::kReplace_Op));
338SIMPLE_TEST_STEP(Clear, clear(d.fColor));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000339
340///////////////////////////////////////////////////////////////////////////////
341// Complex test steps
342
piotaixrf05f5a72014-10-03 13:26:55 -0700343static void SaveMatrixClipStep(SkCanvas* canvas, const TestData& d,
344 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000345 int saveCount = canvas->getSaveCount();
Florin Malita5f6102d2014-06-30 10:13:28 -0400346 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000347 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
piotaixrf05f5a72014-10-03 13:26:55 -0700348 canvas->clipRegion(d.fRegion);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000349 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000350 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000351 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000352 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000353 testStep->assertMessage());
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000354// REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion, testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000355}
356TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
357
piotaixrf05f5a72014-10-03 13:26:55 -0700358static void SaveLayerStep(SkCanvas* canvas, const TestData& d,
359 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000360 int saveCount = canvas->getSaveCount();
halcanary96fcdcc2015-08-27 07:41:13 -0700361 canvas->saveLayer(nullptr, nullptr);
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());
365}
366TEST_STEP(SaveLayer, SaveLayerStep);
367
piotaixrf05f5a72014-10-03 13:26:55 -0700368static void BoundedSaveLayerStep(SkCanvas* canvas, const TestData& d,
369 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000370 int saveCount = canvas->getSaveCount();
halcanary96fcdcc2015-08-27 07:41:13 -0700371 canvas->saveLayer(&d.fRect, nullptr);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000372 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000373 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000374 testStep->assertMessage());
375}
376TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
377
piotaixrf05f5a72014-10-03 13:26:55 -0700378static void PaintSaveLayerStep(SkCanvas* canvas, const TestData& d,
379 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000380 int saveCount = canvas->getSaveCount();
halcanary96fcdcc2015-08-27 07:41:13 -0700381 canvas->saveLayer(nullptr, &d.fPaint);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000382 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000383 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000384 testStep->assertMessage());
385}
386TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
387
piotaixrf05f5a72014-10-03 13:26:55 -0700388static void TwoClipOpsStep(SkCanvas* canvas, const TestData& d,
389 skiatest::Reporter*, CanvasTestStep*) {
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000390 // This test exercises a functionality in SkPicture that leads to the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000391 // recording of restore offset placeholders. This test will trigger an
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000392 // assertion at playback time if the placeholders are not properly
393 // filled when the recording ends.
piotaixrf05f5a72014-10-03 13:26:55 -0700394 canvas->clipRect(d.fRect);
395 canvas->clipRegion(d.fRegion);
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000396}
397TEST_STEP(TwoClipOps, TwoClipOpsStep);
398
epoger@google.com94fa43c2012-04-11 17:51:01 +0000399// exercise fix for http://code.google.com/p/skia/issues/detail?id=560
400// ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
piotaixrf05f5a72014-10-03 13:26:55 -0700401static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas, const TestData& d,
402 skiatest::Reporter*, CanvasTestStep*) {
epoger@google.com94fa43c2012-04-11 17:51:01 +0000403 SkPaint paint;
404 paint.setStrokeWidth(SkIntToScalar(1));
405 paint.setStyle(SkPaint::kStroke_Style);
406
piotaixrf05f5a72014-10-03 13:26:55 -0700407 canvas->drawPath(d.fNearlyZeroLengthPath, paint);
epoger@google.com94fa43c2012-04-11 17:51:01 +0000408}
409TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
410
piotaixrf05f5a72014-10-03 13:26:55 -0700411static void DrawVerticesShaderTestStep(SkCanvas* canvas, const TestData& d,
412 skiatest::Reporter*, CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000413 SkPoint pts[4];
414 pts[0].set(0, 0);
piotaixrf05f5a72014-10-03 13:26:55 -0700415 pts[1].set(SkIntToScalar(d.fWidth), 0);
416 pts[2].set(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight));
417 pts[3].set(0, SkIntToScalar(d.fHeight));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000418 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -0700419 paint.setShader(SkShader::MakeBitmapShader(d.fBitmap, SkShader::kClamp_TileMode,
420 SkShader::kClamp_TileMode));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000421 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
halcanary96fcdcc2015-08-27 07:41:13 -0700422 nullptr, nullptr, nullptr, 0, paint);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000423}
edisonn@google.com77909122012-10-18 15:58:23 +0000424// NYI: issue 240.
425TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000426
piotaixrf05f5a72014-10-03 13:26:55 -0700427static void DrawPictureTestStep(SkCanvas* canvas, const TestData& d,
428 skiatest::Reporter*, CanvasTestStep*) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000429 SkPictureRecorder recorder;
piotaixrf05f5a72014-10-03 13:26:55 -0700430 SkCanvas* testCanvas = recorder.beginRecording(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight),
halcanary96fcdcc2015-08-27 07:41:13 -0700431 nullptr, 0);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000432 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
piotaixrf05f5a72014-10-03 13:26:55 -0700433 testCanvas->clipRect(d.fRect);
434 testCanvas->drawRect(d.fRect, d.fPaint);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000435
reedca2622b2016-03-18 07:25:55 -0700436 canvas->drawPicture(recorder.finishRecordingAsPicture());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000437}
438TEST_STEP(DrawPicture, DrawPictureTestStep);
439
piotaixrf05f5a72014-10-03 13:26:55 -0700440static void SaveRestoreTestStep(SkCanvas* canvas, const TestData& d,
441 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000442 int baseSaveCount = canvas->getSaveCount();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000443 int n = canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000444 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount == n, testStep->assertMessage());
445 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000446 testStep->assertMessage());
447 canvas->save();
448 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000449 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000450 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000451 canvas->restoreToCount(baseSaveCount + 1);
452 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000453 testStep->assertMessage());
454
455 // should this pin to 1, or be a no-op, or crash?
456 canvas->restoreToCount(0);
457 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
458 testStep->assertMessage());
459}
460TEST_STEP(SaveRestore, SaveRestoreTestStep);
461
piotaixrf05f5a72014-10-03 13:26:55 -0700462static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas, const TestData& d,
463 skiatest::Reporter*, CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000464 // This test step challenges the TestDeferredCanvasStateConsistency
465 // test cases because the opaque paint can trigger an optimization
466 // that discards previously recorded commands. The challenge is to maintain
467 // correct clip and matrix stack state.
468 canvas->resetMatrix();
469 canvas->rotate(SkIntToScalar(30));
470 canvas->save();
471 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
472 canvas->save();
473 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
474 SkPaint paint;
475 paint.setColor(0xFFFFFFFF);
476 canvas->drawPaint(paint);
477 canvas->restore();
478 canvas->restore();
479}
480TEST_STEP(NestedSaveRestoreWithSolidPaint, \
481 NestedSaveRestoreWithSolidPaintTestStep);
482
piotaixrf05f5a72014-10-03 13:26:55 -0700483static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas, const TestData& d,
484 skiatest::Reporter*, CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000485 // This test step challenges the TestDeferredCanvasStateConsistency
486 // test case because the canvas flush on a deferred canvas will
487 // reset the recording session. The challenge is to maintain correct
488 // clip and matrix stack state on the playback canvas.
489 canvas->resetMatrix();
490 canvas->rotate(SkIntToScalar(30));
491 canvas->save();
492 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
493 canvas->save();
494 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
piotaixrf05f5a72014-10-03 13:26:55 -0700495 canvas->drawRect(d.fRect,d.fPaint);
reed@google.com3b3e8952012-08-16 20:53:31 +0000496 canvas->flush();
497 canvas->restore();
498 canvas->restore();
499}
piotaixrf05f5a72014-10-03 13:26:55 -0700500TEST_STEP(NestedSaveRestoreWithFlush, NestedSaveRestoreWithFlushTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000501
tomhudsoncb3bd182016-05-18 07:24:16 -0700502static void DescribeTopLayerTestStep(SkCanvas* canvas,
503 const TestData& d,
504 skiatest::Reporter* reporter,
505 CanvasTestStep* testStep) {
506 SkMatrix m;
507 SkIRect r;
508 // NOTE: adjustToTopLayer() does *not* reduce the clip size, even if the canvas
509 // is smaller than 10x10!
510
511 canvas->temporary_internal_describeTopLayer(&m, &r);
512 REPORTER_ASSERT_MESSAGE(reporter, m.isIdentity(), testStep->assertMessage());
513 REPORTER_ASSERT_MESSAGE(reporter, r == SkIRect::MakeXYWH(0, 0, 2, 2),
514 testStep->assertMessage());
515
516 // Putting a full-canvas layer on it should make no change to the results.
517 SkRect layerBounds = SkRect::MakeXYWH(0.f, 0.f, 10.f, 10.f);
518 canvas->saveLayer(layerBounds, nullptr);
519 canvas->temporary_internal_describeTopLayer(&m, &r);
520 REPORTER_ASSERT_MESSAGE(reporter, m.isIdentity(), testStep->assertMessage());
521 REPORTER_ASSERT_MESSAGE(reporter, r == SkIRect::MakeXYWH(0, 0, 2, 2),
522 testStep->assertMessage());
523 canvas->restore();
524
525 // Adding a translated layer translates the results.
526 // Default canvas is only 2x2, so can't offset our layer by very much at all;
527 // saveLayer() aborts if the bounds don't intersect.
528 layerBounds = SkRect::MakeXYWH(1.f, 1.f, 6.f, 6.f);
529 canvas->saveLayer(layerBounds, nullptr);
530 canvas->temporary_internal_describeTopLayer(&m, &r);
531 REPORTER_ASSERT_MESSAGE(reporter, m == SkMatrix::MakeTrans(-1.f, -1.f),
532 testStep->assertMessage());
533 REPORTER_ASSERT_MESSAGE(reporter, r == SkIRect::MakeXYWH(0, 0, 1, 1),
534 testStep->assertMessage());
535 canvas->restore();
536
537}
538TEST_STEP(DescribeTopLayer, DescribeTopLayerTestStep);
539
540
robertphillipsda2cd8b2016-04-21 11:05:32 -0700541class CanvasTestingAccess {
542public:
543 static bool SameState(const SkCanvas* canvas1, const SkCanvas* canvas2) {
544 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
545 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
546 while (!layerIter1.done() && !layerIter2.done()) {
547 if (layerIter1.matrix() != layerIter2.matrix()) {
548 return false;
549 }
550 if (layerIter1.clip() != layerIter2.clip()) {
reed1e7f5e72016-04-27 07:49:17 -0700551 return false;
robertphillipsda2cd8b2016-04-21 11:05:32 -0700552 }
553 if (layerIter1.paint() != layerIter2.paint()) {
554 return false;
555 }
556 if (layerIter1.x() != layerIter2.x()) {
557 return false;
558 }
559 if (layerIter1.y() != layerIter2.y()) {
560 return false;
561 }
562 layerIter1.next();
563 layerIter2.next();
564 }
565 if (!layerIter1.done()) {
566 return false;
567 }
568 if (!layerIter2.done()) {
569 return false;
570 }
571 return true;
572 }
573};
574
piotaixrf05f5a72014-10-03 13:26:55 -0700575static void AssertCanvasStatesEqual(skiatest::Reporter* reporter, const TestData& d,
576 const SkCanvas* canvas1, const SkCanvas* canvas2,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000577 CanvasTestStep* testStep) {
578 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
579 canvas2->getDeviceSize(), testStep->assertMessage());
580 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
581 canvas2->getSaveCount(), testStep->assertMessage());
reed@google.com3b3e8952012-08-16 20:53:31 +0000582
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000583 SkRect bounds1, bounds2;
584 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.com3b3e8952012-08-16 20:53:31 +0000585 canvas1->getClipBounds(&bounds1) == canvas2->getClipBounds(&bounds2),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000586 testStep->assertMessage());
587 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
reed@google.com3b3e8952012-08-16 20:53:31 +0000588 testStep->assertMessage());
589
benjaminwagner1c2729c2016-01-22 09:45:14 -0800590#ifdef SK_SUPPORT_LEGACY_DRAWFILTER
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000591 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
592 canvas2->getDrawFilter(), testStep->assertMessage());
benjaminwagner1c2729c2016-01-22 09:45:14 -0800593#endif
594
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000595 SkIRect deviceBounds1, deviceBounds2;
596 REPORTER_ASSERT_MESSAGE(reporter,
597 canvas1->getClipDeviceBounds(&deviceBounds1) ==
598 canvas2->getClipDeviceBounds(&deviceBounds2),
599 testStep->assertMessage());
reed868074b2014-06-03 10:53:59 -0700600 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2, testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000601 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
602 canvas2->getTotalMatrix(), testStep->assertMessage());
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000603 REPORTER_ASSERT_MESSAGE(reporter, equal_clips(*canvas1, *canvas2), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000604
robertphillipsda2cd8b2016-04-21 11:05:32 -0700605 REPORTER_ASSERT_MESSAGE(reporter,
606 CanvasTestingAccess::SameState(canvas1, canvas2),
607 testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000608}
609
edisonn@google.com77909122012-10-18 15:58:23 +0000610static void TestPdfDevice(skiatest::Reporter* reporter,
piotaixrf05f5a72014-10-03 13:26:55 -0700611 const TestData& d,
edisonn@google.com77909122012-10-18 15:58:23 +0000612 CanvasTestStep* testStep) {
halcanary3d32d502015-03-01 06:55:20 -0800613 SkDynamicMemoryWStream outStream;
halcanary4b656662016-04-27 07:45:18 -0700614 sk_sp<SkDocument> doc(SkDocument::MakePDF(&outStream));
halcanary8ee06f22015-08-11 10:30:12 -0700615#if SK_SUPPORT_PDF
616 REPORTER_ASSERT(reporter, doc);
halcanary2ccdb632015-08-11 13:35:12 -0700617#else
618 REPORTER_ASSERT(reporter, !doc);
619#endif // SK_SUPPORT_PDF
620 if (!doc) {
621 return;
622 }
halcanary3d32d502015-03-01 06:55:20 -0800623 SkCanvas* canvas = doc->beginPage(SkIntToScalar(d.fWidth),
624 SkIntToScalar(d.fHeight));
625 REPORTER_ASSERT(reporter, canvas);
edisonn@google.com77909122012-10-18 15:58:23 +0000626 testStep->setAssertMessageFormat(kPdfAssertMessageFormat);
halcanary3d32d502015-03-01 06:55:20 -0800627 testStep->draw(canvas, d, reporter);
628
629 REPORTER_ASSERT(reporter, doc->close());
edisonn@google.com77909122012-10-18 15:58:23 +0000630}
631
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
caryclark@google.com42639cd2012-06-06 12:03:39 +0000678 // The following test code is disabled because SkNWayCanvas does not
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000679 // report correct clipping and device bounds information
680 // Issue: http://code.google.com/p/skia/issues/detail?id=501
caryclark@google.com42639cd2012-06-06 12:03:39 +0000681
682 if (false) { // avoid bit rot, suppress warning
piotaixrf05f5a72014-10-03 13:26:55 -0700683 TestNWayCanvasStateConsistency(reporter, d, testStep, referenceCanvas);
caryclark@google.com42639cd2012-06-06 12:03:39 +0000684 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000685
caryclark@google.com42639cd2012-06-06 12:03:39 +0000686 if (false) { // avoid bit rot, suppress warning
687 test_clipVisitor(reporter, &referenceCanvas);
688 }
reed687fa1c2015-04-07 08:00:56 -0700689 test_clipstack(reporter);
reed@google.com7c202932011-12-14 18:48:05 +0000690}
reed@google.com37f3ae02011-11-28 16:06:04 +0000691
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000692static void test_newraster(skiatest::Reporter* reporter) {
693 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
reed3054be12014-12-10 07:24:28 -0800694 const size_t minRowBytes = info.minRowBytes();
695 const size_t size = info.getSafeSize(minRowBytes);
scroggo565901d2015-12-10 10:44:13 -0800696 SkAutoTMalloc<SkPMColor> storage(size);
697 SkPMColor* baseAddr = storage.get();
reed3054be12014-12-10 07:24:28 -0800698 sk_bzero(baseAddr, size);
699
700 SkCanvas* canvas = SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000701 REPORTER_ASSERT(reporter, canvas);
702
reed6ceeebd2016-03-09 14:26:26 -0800703 SkPixmap pmap;
704 const SkPMColor* addr = canvas->peekPixels(&pmap) ? pmap.addr32() : nullptr;
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000705 REPORTER_ASSERT(reporter, addr);
reed6ceeebd2016-03-09 14:26:26 -0800706 REPORTER_ASSERT(reporter, info == pmap.info());
707 REPORTER_ASSERT(reporter, minRowBytes == pmap.rowBytes());
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000708 for (int y = 0; y < info.height(); ++y) {
709 for (int x = 0; x < info.width(); ++x) {
710 REPORTER_ASSERT(reporter, 0 == addr[x]);
711 }
reed6ceeebd2016-03-09 14:26:26 -0800712 addr = (const SkPMColor*)((const char*)addr + pmap.rowBytes());
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000713 }
halcanary385fe4d2015-08-26 13:07:48 -0700714 delete canvas;
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000715
716 // now try a deliberately bad info
reede5ea5002014-09-03 11:54:58 -0700717 info = info.makeWH(-1, info.height());
halcanary96fcdcc2015-08-27 07:41:13 -0700718 REPORTER_ASSERT(reporter, nullptr == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000719
720 // too big
reede5ea5002014-09-03 11:54:58 -0700721 info = info.makeWH(1 << 30, 1 << 30);
halcanary96fcdcc2015-08-27 07:41:13 -0700722 REPORTER_ASSERT(reporter, nullptr == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
skia.committer@gmail.com0e530752014-02-28 03:02:05 +0000723
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000724 // not a valid pixel type
reede5ea5002014-09-03 11:54:58 -0700725 info = SkImageInfo::Make(10, 10, kUnknown_SkColorType, info.alphaType());
halcanary96fcdcc2015-08-27 07:41:13 -0700726 REPORTER_ASSERT(reporter, nullptr == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000727
728 // We should succeed with a zero-sized valid info
729 info = SkImageInfo::MakeN32Premul(0, 0);
reed3054be12014-12-10 07:24:28 -0800730 canvas = SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000731 REPORTER_ASSERT(reporter, canvas);
halcanary385fe4d2015-08-26 13:07:48 -0700732 delete canvas;
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000733}
734
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000735DEF_TEST(Canvas, reporter) {
piotaixrf05f5a72014-10-03 13:26:55 -0700736 TestData d;
reed@google.com37f3ae02011-11-28 16:06:04 +0000737
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000738 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
piotaixrf05f5a72014-10-03 13:26:55 -0700739 TestOverrideStateConsistency(reporter, d, testStepArray()[testStep]);
edisonn@google.com77909122012-10-18 15:58:23 +0000740 if (testStepArray()[testStep]->enablePdfTesting()) {
piotaixrf05f5a72014-10-03 13:26:55 -0700741 TestPdfDevice(reporter, d, testStepArray()[testStep]);
edisonn@google.com77909122012-10-18 15:58:23 +0000742 }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000743 }
junov@chromium.orgcd62ecf2012-08-02 17:43:25 +0000744
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000745 test_newraster(reporter);
reed@google.com37f3ae02011-11-28 16:06:04 +0000746}
reedf0090cb2014-11-26 08:55:51 -0800747
748DEF_TEST(Canvas_SaveState, reporter) {
749 SkCanvas canvas(10, 10);
750 REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
751
752 int n = canvas.save();
753 REPORTER_ASSERT(reporter, 1 == n);
754 REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
755
halcanary96fcdcc2015-08-27 07:41:13 -0700756 n = canvas.saveLayer(nullptr, nullptr);
reedf0090cb2014-11-26 08:55:51 -0800757 REPORTER_ASSERT(reporter, 2 == n);
758 REPORTER_ASSERT(reporter, 3 == canvas.getSaveCount());
halcanary9d524f22016-03-29 09:03:52 -0700759
reedf0090cb2014-11-26 08:55:51 -0800760 canvas.restore();
761 REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
762 canvas.restore();
763 REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
764}
reedc1b11f12015-03-13 08:48:26 -0700765
766DEF_TEST(Canvas_ClipEmptyPath, reporter) {
767 SkCanvas canvas(10, 10);
768 canvas.save();
769 SkPath path;
770 canvas.clipPath(path);
771 canvas.restore();
772 canvas.save();
773 path.moveTo(5, 5);
774 canvas.clipPath(path);
775 canvas.restore();
776 canvas.save();
777 path.moveTo(7, 7);
778 canvas.clipPath(path); // should not assert here
779 canvas.restore();
780}
fmalitaf433bb22015-08-17 08:05:13 -0700781
782namespace {
783
784class MockFilterCanvas : public SkPaintFilterCanvas {
785public:
786 MockFilterCanvas(SkCanvas* canvas) : INHERITED(canvas) { }
787
788protected:
fmalita32cdc322016-01-12 07:21:11 -0800789 bool onFilter(SkTCopyOnFirstWrite<SkPaint>*, Type) const override { return true; }
fmalitaf433bb22015-08-17 08:05:13 -0700790
791private:
792 typedef SkPaintFilterCanvas INHERITED;
793};
794
795} // anonymous namespace
796
797// SkPaintFilterCanvas should inherit the initial target canvas state.
798DEF_TEST(PaintFilterCanvas_ConsistentState, reporter) {
799 SkCanvas canvas(100, 100);
800 canvas.clipRect(SkRect::MakeXYWH(12.7f, 12.7f, 75, 75));
801 canvas.scale(0.5f, 0.75f);
802
803 SkRect clip1, clip2;
804
805 MockFilterCanvas filterCanvas(&canvas);
806 REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix());
807 REPORTER_ASSERT(reporter, canvas.getClipBounds(&clip1) == filterCanvas.getClipBounds(&clip2));
808 REPORTER_ASSERT(reporter, clip1 == clip2);
809
810 filterCanvas.clipRect(SkRect::MakeXYWH(30.5f, 30.7f, 100, 100));
811 filterCanvas.scale(0.75f, 0.5f);
812 REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix());
813 REPORTER_ASSERT(reporter, canvas.getClipBounds(&clip1) == filterCanvas.getClipBounds(&clip2));
814 REPORTER_ASSERT(reporter, clip1 == clip2);
815}