blob: 2390c7455f58351a2d93d464ea65d8ffac120cfa [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 "SkDevice.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"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000059#include "SkRect.h"
60#include "SkRegion.h"
61#include "SkShader.h"
62#include "SkStream.h"
reed@google.com28183b42014-02-04 15:34:10 +000063#include "SkSurface.h"
scroggo565901d2015-12-10 10:44:13 -080064#include "SkTemplates.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
piotaixrf05f5a72014-10-03 13:26:55 -070075///////////////////////////////////////////////////////////////////////////////
76// Constants used by test steps
77const SkPoint kTestPoints[] = {
78 {SkIntToScalar(0), SkIntToScalar(0)},
79 {SkIntToScalar(2), SkIntToScalar(1)},
80 {SkIntToScalar(0), SkIntToScalar(2)}
81};
82const SkPoint kTestPoints2[] = {
83 { SkIntToScalar(0), SkIntToScalar(1) },
84 { SkIntToScalar(1), SkIntToScalar(1) },
85 { SkIntToScalar(2), SkIntToScalar(1) },
86 { SkIntToScalar(3), SkIntToScalar(1) },
87 { SkIntToScalar(4), SkIntToScalar(1) },
88 { SkIntToScalar(5), SkIntToScalar(1) },
89 { SkIntToScalar(6), SkIntToScalar(1) },
90 { SkIntToScalar(7), SkIntToScalar(1) },
91 { SkIntToScalar(8), SkIntToScalar(1) },
92 { SkIntToScalar(9), SkIntToScalar(1) },
93 { SkIntToScalar(10), SkIntToScalar(1) }
94};
95
96struct TestData {
97public:
98 TestData()
99 : fRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
100 SkIntToScalar(2), SkIntToScalar(1)))
101 , fMatrix(TestMatrix())
102 , fPath(TestPath())
103 , fNearlyZeroLengthPath(TestNearlyZeroLengthPath())
104 , fIRect(SkIRect::MakeXYWH(0, 0, 2, 1))
105 , fRegion(TestRegion())
106 , fColor(0x01020304)
107 , fPoints(kTestPoints)
108 , fPointCount(3)
109 , fWidth(2)
110 , fHeight(2)
111 , fText("Hello World")
112 , fPoints2(kTestPoints2)
113 , fBitmap(TestBitmap())
114 { }
115
116 SkRect fRect;
tfarina567ff2f2015-04-27 07:01:44 -0700117 SkMatrix fMatrix;
piotaixrf05f5a72014-10-03 13:26:55 -0700118 SkPath fPath;
119 SkPath fNearlyZeroLengthPath;
120 SkIRect fIRect;
121 SkRegion fRegion;
122 SkColor fColor;
123 SkPaint fPaint;
124 const SkPoint* fPoints;
125 size_t fPointCount;
126 int fWidth;
127 int fHeight;
128 SkString fText;
129 const SkPoint* fPoints2;
130 SkBitmap fBitmap;
131
132private:
133 static SkMatrix TestMatrix() {
134 SkMatrix matrix;
135 matrix.reset();
136 matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
137
138 return matrix;
139 }
140 static SkPath TestPath() {
141 SkPath path;
142 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
143 SkIntToScalar(2), SkIntToScalar(1)));
144 return path;
145 }
146 static SkPath TestNearlyZeroLengthPath() {
147 SkPath path;
148 SkPoint pt1 = { 0, 0 };
149 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
150 SkPoint pt3 = { SkIntToScalar(1), 0 };
151 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
152 path.moveTo(pt1);
153 path.lineTo(pt2);
154 path.lineTo(pt3);
155 path.lineTo(pt4);
156 return path;
157 }
158 static SkRegion TestRegion() {
159 SkRegion region;
160 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
161 region.setRect(rect);
162 return region;
163 }
164 static SkBitmap TestBitmap() {
165 SkBitmap bitmap;
166 createBitmap(&bitmap, 0x05060708);
167 return bitmap;
168 }
169};
170
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000171static bool equal_clips(const SkCanvas& a, const SkCanvas& b) {
172 if (a.isClipEmpty()) {
173 return b.isClipEmpty();
174 }
175 if (!a.isClipRect()) {
176 // this is liberally true, since we don't expose a way to know this exactly (for non-rects)
177 return !b.isClipRect();
178 }
179 SkIRect ar, br;
180 a.getClipDeviceBounds(&ar);
181 b.getClipDeviceBounds(&br);
182 return ar == br;
183}
184
reed@google.com90c07ea2012-04-13 13:50:27 +0000185class Canvas2CanvasClipVisitor : public SkCanvas::ClipVisitor {
186public:
187 Canvas2CanvasClipVisitor(SkCanvas* target) : fTarget(target) {}
188
mtklein36352bf2015-03-25 18:17:31 -0700189 void clipRect(const SkRect& r, SkRegion::Op op, bool aa) override {
reed@google.com90c07ea2012-04-13 13:50:27 +0000190 fTarget->clipRect(r, op, aa);
191 }
mtklein36352bf2015-03-25 18:17:31 -0700192 void clipRRect(const SkRRect& r, SkRegion::Op op, bool aa) override {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000193 fTarget->clipRRect(r, op, aa);
194 }
mtklein36352bf2015-03-25 18:17:31 -0700195 void clipPath(const SkPath& p, SkRegion::Op op, bool aa) override {
reed@google.com90c07ea2012-04-13 13:50:27 +0000196 fTarget->clipPath(p, op, aa);
197 }
198
199private:
200 SkCanvas* fTarget;
201};
202
203static void test_clipVisitor(skiatest::Reporter* reporter, SkCanvas* canvas) {
204 SkISize size = canvas->getDeviceSize();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000205
reed@google.com90c07ea2012-04-13 13:50:27 +0000206 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000207 bm.setInfo(SkImageInfo::MakeN32Premul(size.width(), size.height()));
reed@google.com90c07ea2012-04-13 13:50:27 +0000208 SkCanvas c(bm);
209
210 Canvas2CanvasClipVisitor visitor(&c);
211 canvas->replayClips(&visitor);
212
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000213 REPORTER_ASSERT(reporter, equal_clips(c, *canvas));
reed@google.com90c07ea2012-04-13 13:50:27 +0000214}
215
reed687fa1c2015-04-07 08:00:56 -0700216static void test_clipstack(skiatest::Reporter* reporter) {
217 // The clipstack is refcounted, and needs to be able to out-live the canvas if a client has
218 // ref'd it.
halcanary96fcdcc2015-08-27 07:41:13 -0700219 const SkClipStack* cs = nullptr;
reed687fa1c2015-04-07 08:00:56 -0700220 {
221 SkCanvas canvas(10, 10);
222 cs = SkRef(canvas.getClipStack());
223 }
224 REPORTER_ASSERT(reporter, cs->unique());
225 cs->unref();
226}
227
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000228// Format strings that describe the test context. The %s token is where
229// the name of the test step is inserted. The context is required for
230// disambiguating the error in the case of failures that are reported in
231// functions that are called multiple times in different contexts (test
232// cases and test steps).
233static const char* const kDefaultAssertMessageFormat = "%s";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000234static const char* const kCanvasDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000235 "Drawing test step %s with SkCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000236static const char* const kNWayDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000237 "Drawing test step %s with SkNWayCanvas";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000238static const char* const kNWayStateAssertMessageFormat =
239 "test step %s, SkNWayCanvas state consistency";
240static const char* const kNWayIndirect1StateAssertMessageFormat =
241 "test step %s, SkNWayCanvas indirect canvas 1 state consistency";
242static const char* const kNWayIndirect2StateAssertMessageFormat =
243 "test step %s, SkNWayCanvas indirect canvas 2 state consistency";
edisonn@google.com77909122012-10-18 15:58:23 +0000244static const char* const kPdfAssertMessageFormat =
245 "PDF sanity check failed %s";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000246
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000247class CanvasTestStep;
248static SkTDArray<CanvasTestStep*>& testStepArray() {
249 static SkTDArray<CanvasTestStep*> theTests;
250 return theTests;
251}
252
253class CanvasTestStep {
254public:
edisonn@google.com77909122012-10-18 15:58:23 +0000255 CanvasTestStep(bool fEnablePdfTesting = true) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000256 *testStepArray().append() = this;
257 fAssertMessageFormat = kDefaultAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000258 this->fEnablePdfTesting = fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000259 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000260 virtual ~CanvasTestStep() { }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000261
piotaixrf05f5a72014-10-03 13:26:55 -0700262 virtual void draw(SkCanvas*, const TestData&, skiatest::Reporter*) = 0;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000263 virtual const char* name() const = 0;
264
265 const char* assertMessage() {
266 fAssertMessage.printf(fAssertMessageFormat, name());
267 return fAssertMessage.c_str();
268 }
269
270 void setAssertMessageFormat(const char* format) {
271 fAssertMessageFormat = format;
272 }
273
edisonn@google.com77909122012-10-18 15:58:23 +0000274 bool enablePdfTesting() { return fEnablePdfTesting; }
275
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000276private:
277 SkString fAssertMessage;
278 const char* fAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000279 bool fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000280};
281
282///////////////////////////////////////////////////////////////////////////////
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000283// Macros for defining test steps
284
285#define TEST_STEP(NAME, FUNCTION) \
286class NAME##_TestStep : public CanvasTestStep{ \
287public: \
piotaixrf05f5a72014-10-03 13:26:55 -0700288 virtual void draw(SkCanvas* canvas, const TestData& d, \
289 skiatest::Reporter* reporter) { \
290 FUNCTION (canvas, d, reporter, this); \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000291 } \
292 virtual const char* name() const {return #NAME ;} \
293}; \
294static NAME##_TestStep NAME##_TestStepInstance;
295
piotaixrf05f5a72014-10-03 13:26:55 -0700296#define TEST_STEP_NO_PDF(NAME, FUNCTION) \
edisonn@google.com77909122012-10-18 15:58:23 +0000297class NAME##_TestStep : public CanvasTestStep{ \
298public: \
299 NAME##_TestStep() : CanvasTestStep(false) {} \
piotaixrf05f5a72014-10-03 13:26:55 -0700300 virtual void draw(SkCanvas* canvas, const TestData& d, \
301 skiatest::Reporter* reporter) { \
302 FUNCTION (canvas, d, reporter, this); \
edisonn@google.com77909122012-10-18 15:58:23 +0000303 } \
304 virtual const char* name() const {return #NAME ;} \
305}; \
306static NAME##_TestStep NAME##_TestStepInstance;
307
piotaixrf05f5a72014-10-03 13:26:55 -0700308#define SIMPLE_TEST_STEP(NAME, CALL) \
309static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \
310 skiatest::Reporter*, CanvasTestStep*) { \
311 canvas-> CALL ; \
312} \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000313TEST_STEP(NAME, NAME##TestStep )
314
315#define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
piotaixrf05f5a72014-10-03 13:26:55 -0700316static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \
317 skiatest::Reporter*, CanvasTestStep* testStep) { \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000318 REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \
319 testStep->assertMessage()); \
320} \
321TEST_STEP(NAME, NAME##TestStep )
322
323
324///////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000325// Basic test steps for most virtual methods in SkCanvas that draw or affect
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000326// the state of the canvas.
327
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000328SIMPLE_TEST_STEP(Translate, translate(SkIntToScalar(1), SkIntToScalar(2)));
329SIMPLE_TEST_STEP(Scale, scale(SkIntToScalar(1), SkIntToScalar(2)));
330SIMPLE_TEST_STEP(Rotate, rotate(SkIntToScalar(1)));
331SIMPLE_TEST_STEP(Skew, skew(SkIntToScalar(1), SkIntToScalar(2)));
piotaixrf05f5a72014-10-03 13:26:55 -0700332SIMPLE_TEST_STEP(Concat, concat(d.fMatrix));
333SIMPLE_TEST_STEP(SetMatrix, setMatrix(d.fMatrix));
334SIMPLE_TEST_STEP(ClipRect, clipRect(d.fRect));
335SIMPLE_TEST_STEP(ClipPath, clipPath(d.fPath));
336SIMPLE_TEST_STEP(ClipRegion, clipRegion(d.fRegion, SkRegion::kReplace_Op));
337SIMPLE_TEST_STEP(Clear, clear(d.fColor));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000338
339///////////////////////////////////////////////////////////////////////////////
340// Complex test steps
341
piotaixrf05f5a72014-10-03 13:26:55 -0700342static void SaveMatrixClipStep(SkCanvas* canvas, const TestData& d,
343 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000344 int saveCount = canvas->getSaveCount();
Florin Malita5f6102d2014-06-30 10:13:28 -0400345 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000346 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
piotaixrf05f5a72014-10-03 13:26:55 -0700347 canvas->clipRegion(d.fRegion);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000348 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000349 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000350 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000351 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000352 testStep->assertMessage());
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000353// REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion, testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000354}
355TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
356
piotaixrf05f5a72014-10-03 13:26:55 -0700357static void SaveLayerStep(SkCanvas* canvas, const TestData& d,
358 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000359 int saveCount = canvas->getSaveCount();
halcanary96fcdcc2015-08-27 07:41:13 -0700360 canvas->saveLayer(nullptr, nullptr);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000361 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000362 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000363 testStep->assertMessage());
364}
365TEST_STEP(SaveLayer, SaveLayerStep);
366
piotaixrf05f5a72014-10-03 13:26:55 -0700367static void BoundedSaveLayerStep(SkCanvas* canvas, const TestData& d,
368 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000369 int saveCount = canvas->getSaveCount();
halcanary96fcdcc2015-08-27 07:41:13 -0700370 canvas->saveLayer(&d.fRect, nullptr);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000371 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000372 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000373 testStep->assertMessage());
374}
375TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
376
piotaixrf05f5a72014-10-03 13:26:55 -0700377static void PaintSaveLayerStep(SkCanvas* canvas, const TestData& d,
378 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000379 int saveCount = canvas->getSaveCount();
halcanary96fcdcc2015-08-27 07:41:13 -0700380 canvas->saveLayer(nullptr, &d.fPaint);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000381 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000382 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000383 testStep->assertMessage());
384}
385TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
386
piotaixrf05f5a72014-10-03 13:26:55 -0700387static void TwoClipOpsStep(SkCanvas* canvas, const TestData& d,
388 skiatest::Reporter*, CanvasTestStep*) {
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000389 // This test exercises a functionality in SkPicture that leads to the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000390 // recording of restore offset placeholders. This test will trigger an
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000391 // assertion at playback time if the placeholders are not properly
392 // filled when the recording ends.
piotaixrf05f5a72014-10-03 13:26:55 -0700393 canvas->clipRect(d.fRect);
394 canvas->clipRegion(d.fRegion);
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000395}
396TEST_STEP(TwoClipOps, TwoClipOpsStep);
397
epoger@google.com94fa43c2012-04-11 17:51:01 +0000398// exercise fix for http://code.google.com/p/skia/issues/detail?id=560
399// ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
piotaixrf05f5a72014-10-03 13:26:55 -0700400static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas, const TestData& d,
401 skiatest::Reporter*, CanvasTestStep*) {
epoger@google.com94fa43c2012-04-11 17:51:01 +0000402 SkPaint paint;
403 paint.setStrokeWidth(SkIntToScalar(1));
404 paint.setStyle(SkPaint::kStroke_Style);
405
piotaixrf05f5a72014-10-03 13:26:55 -0700406 canvas->drawPath(d.fNearlyZeroLengthPath, paint);
epoger@google.com94fa43c2012-04-11 17:51:01 +0000407}
408TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
409
piotaixrf05f5a72014-10-03 13:26:55 -0700410static void DrawVerticesShaderTestStep(SkCanvas* canvas, const TestData& d,
411 skiatest::Reporter*, CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000412 SkPoint pts[4];
413 pts[0].set(0, 0);
piotaixrf05f5a72014-10-03 13:26:55 -0700414 pts[1].set(SkIntToScalar(d.fWidth), 0);
415 pts[2].set(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight));
416 pts[3].set(0, SkIntToScalar(d.fHeight));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000417 SkPaint paint;
piotaixrf05f5a72014-10-03 13:26:55 -0700418 SkShader* shader = SkShader::CreateBitmapShader(d.fBitmap,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000419 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
420 paint.setShader(shader)->unref();
421 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 SkAutoTUnref<SkPicture> testPicture(recorder.endRecording());
436
robertphillips9b14f262014-06-04 05:40:44 -0700437 canvas->drawPicture(testPicture);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000438}
439TEST_STEP(DrawPicture, DrawPictureTestStep);
440
piotaixrf05f5a72014-10-03 13:26:55 -0700441static void SaveRestoreTestStep(SkCanvas* canvas, const TestData& d,
442 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000443 int baseSaveCount = canvas->getSaveCount();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000444 int n = canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000445 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount == n, testStep->assertMessage());
446 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000447 testStep->assertMessage());
448 canvas->save();
449 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000450 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000451 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000452 canvas->restoreToCount(baseSaveCount + 1);
453 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000454 testStep->assertMessage());
455
456 // should this pin to 1, or be a no-op, or crash?
457 canvas->restoreToCount(0);
458 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
459 testStep->assertMessage());
460}
461TEST_STEP(SaveRestore, SaveRestoreTestStep);
462
piotaixrf05f5a72014-10-03 13:26:55 -0700463static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas, const TestData& d,
464 skiatest::Reporter*, CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000465 // This test step challenges the TestDeferredCanvasStateConsistency
466 // test cases because the opaque paint can trigger an optimization
467 // that discards previously recorded commands. The challenge is to maintain
468 // correct clip and matrix stack state.
469 canvas->resetMatrix();
470 canvas->rotate(SkIntToScalar(30));
471 canvas->save();
472 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
473 canvas->save();
474 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
475 SkPaint paint;
476 paint.setColor(0xFFFFFFFF);
477 canvas->drawPaint(paint);
478 canvas->restore();
479 canvas->restore();
480}
481TEST_STEP(NestedSaveRestoreWithSolidPaint, \
482 NestedSaveRestoreWithSolidPaintTestStep);
483
piotaixrf05f5a72014-10-03 13:26:55 -0700484static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas, const TestData& d,
485 skiatest::Reporter*, CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000486 // This test step challenges the TestDeferredCanvasStateConsistency
487 // test case because the canvas flush on a deferred canvas will
488 // reset the recording session. The challenge is to maintain correct
489 // clip and matrix stack state on the playback canvas.
490 canvas->resetMatrix();
491 canvas->rotate(SkIntToScalar(30));
492 canvas->save();
493 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
494 canvas->save();
495 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
piotaixrf05f5a72014-10-03 13:26:55 -0700496 canvas->drawRect(d.fRect,d.fPaint);
reed@google.com3b3e8952012-08-16 20:53:31 +0000497 canvas->flush();
498 canvas->restore();
499 canvas->restore();
500}
piotaixrf05f5a72014-10-03 13:26:55 -0700501TEST_STEP(NestedSaveRestoreWithFlush, NestedSaveRestoreWithFlushTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000502
piotaixrf05f5a72014-10-03 13:26:55 -0700503static void AssertCanvasStatesEqual(skiatest::Reporter* reporter, const TestData& d,
504 const SkCanvas* canvas1, const SkCanvas* canvas2,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000505 CanvasTestStep* testStep) {
506 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
507 canvas2->getDeviceSize(), testStep->assertMessage());
508 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
509 canvas2->getSaveCount(), testStep->assertMessage());
reed@google.com3b3e8952012-08-16 20:53:31 +0000510
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000511 SkRect bounds1, bounds2;
512 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.com3b3e8952012-08-16 20:53:31 +0000513 canvas1->getClipBounds(&bounds1) == canvas2->getClipBounds(&bounds2),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000514 testStep->assertMessage());
515 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
reed@google.com3b3e8952012-08-16 20:53:31 +0000516 testStep->assertMessage());
517
benjaminwagner1c2729c2016-01-22 09:45:14 -0800518#ifdef SK_SUPPORT_LEGACY_DRAWFILTER
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000519 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
520 canvas2->getDrawFilter(), testStep->assertMessage());
benjaminwagner1c2729c2016-01-22 09:45:14 -0800521#endif
522
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000523 SkIRect deviceBounds1, deviceBounds2;
524 REPORTER_ASSERT_MESSAGE(reporter,
525 canvas1->getClipDeviceBounds(&deviceBounds1) ==
526 canvas2->getClipDeviceBounds(&deviceBounds2),
527 testStep->assertMessage());
reed868074b2014-06-03 10:53:59 -0700528 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2, testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000529 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
530 canvas2->getTotalMatrix(), testStep->assertMessage());
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000531 REPORTER_ASSERT_MESSAGE(reporter, equal_clips(*canvas1, *canvas2), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000532
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000533 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
534 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
535 while (!layerIter1.done() && !layerIter2.done()) {
536 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
537 layerIter2.matrix(), testStep->assertMessage());
538 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
539 layerIter2.clip(), testStep->assertMessage());
540 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
541 layerIter2.paint(), testStep->assertMessage());
542 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
543 layerIter2.x(), testStep->assertMessage());
544 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
545 layerIter2.y(), testStep->assertMessage());
546 layerIter1.next();
547 layerIter2.next();
548 }
549 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
550 testStep->assertMessage());
551 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
552 testStep->assertMessage());
piotaixr76993ed2014-10-27 15:31:34 -0700553
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000554}
555
edisonn@google.com77909122012-10-18 15:58:23 +0000556static void TestPdfDevice(skiatest::Reporter* reporter,
piotaixrf05f5a72014-10-03 13:26:55 -0700557 const TestData& d,
edisonn@google.com77909122012-10-18 15:58:23 +0000558 CanvasTestStep* testStep) {
halcanary3d32d502015-03-01 06:55:20 -0800559 SkDynamicMemoryWStream outStream;
560 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&outStream));
halcanary8ee06f22015-08-11 10:30:12 -0700561#if SK_SUPPORT_PDF
562 REPORTER_ASSERT(reporter, doc);
halcanary2ccdb632015-08-11 13:35:12 -0700563#else
564 REPORTER_ASSERT(reporter, !doc);
565#endif // SK_SUPPORT_PDF
566 if (!doc) {
567 return;
568 }
halcanary3d32d502015-03-01 06:55:20 -0800569 SkCanvas* canvas = doc->beginPage(SkIntToScalar(d.fWidth),
570 SkIntToScalar(d.fHeight));
571 REPORTER_ASSERT(reporter, canvas);
edisonn@google.com77909122012-10-18 15:58:23 +0000572 testStep->setAssertMessageFormat(kPdfAssertMessageFormat);
halcanary3d32d502015-03-01 06:55:20 -0800573 testStep->draw(canvas, d, reporter);
574
575 REPORTER_ASSERT(reporter, doc->close());
edisonn@google.com77909122012-10-18 15:58:23 +0000576}
577
caryclark@google.com42639cd2012-06-06 12:03:39 +0000578// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000579static void TestNWayCanvasStateConsistency(
580 skiatest::Reporter* reporter,
piotaixrf05f5a72014-10-03 13:26:55 -0700581 const TestData& d,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000582 CanvasTestStep* testStep,
583 const SkCanvas& referenceCanvas) {
584
585 SkBitmap indirectStore1;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000586 createBitmap(&indirectStore1, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700587 SkCanvas indirectCanvas1(indirectStore1);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000588
589 SkBitmap indirectStore2;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000590 createBitmap(&indirectStore2, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700591 SkCanvas indirectCanvas2(indirectStore2);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000592
djsollen@google.comf0a062b2012-05-01 16:50:25 +0000593 SkISize canvasSize = referenceCanvas.getDeviceSize();
594 SkNWayCanvas nWayCanvas(canvasSize.width(), canvasSize.height());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000595 nWayCanvas.addCanvas(&indirectCanvas1);
596 nWayCanvas.addCanvas(&indirectCanvas2);
597
598 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700599 testStep->draw(&nWayCanvas, d, reporter);
scroggo648238c2015-01-29 11:58:51 -0800600 // Verify that the SkNWayCanvas reports consitent state
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000601 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700602 AssertCanvasStatesEqual(reporter, d, &nWayCanvas, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000603 // Verify that the indirect canvases report consitent state
604 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700605 AssertCanvasStatesEqual(reporter, d, &indirectCanvas1, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000606 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700607 AssertCanvasStatesEqual(reporter, d, &indirectCanvas2, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000608}
609
610/*
611 * This sub-test verifies that the test step passes when executed
612 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
613 * that the all canvas derivatives report the same state as an SkCanvas
614 * after having executed the test step.
615 */
piotaixrf05f5a72014-10-03 13:26:55 -0700616static void TestOverrideStateConsistency(skiatest::Reporter* reporter, const TestData& d,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000617 CanvasTestStep* testStep) {
618 SkBitmap referenceStore;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000619 createBitmap(&referenceStore, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700620 SkCanvas referenceCanvas(referenceStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000621 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700622 testStep->draw(&referenceCanvas, d, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000623
caryclark@google.com42639cd2012-06-06 12:03:39 +0000624 // The following test code is disabled because SkNWayCanvas does not
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000625 // report correct clipping and device bounds information
626 // Issue: http://code.google.com/p/skia/issues/detail?id=501
caryclark@google.com42639cd2012-06-06 12:03:39 +0000627
628 if (false) { // avoid bit rot, suppress warning
piotaixrf05f5a72014-10-03 13:26:55 -0700629 TestNWayCanvasStateConsistency(reporter, d, testStep, referenceCanvas);
caryclark@google.com42639cd2012-06-06 12:03:39 +0000630 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000631
caryclark@google.com42639cd2012-06-06 12:03:39 +0000632 if (false) { // avoid bit rot, suppress warning
633 test_clipVisitor(reporter, &referenceCanvas);
634 }
reed687fa1c2015-04-07 08:00:56 -0700635 test_clipstack(reporter);
reed@google.com7c202932011-12-14 18:48:05 +0000636}
reed@google.com37f3ae02011-11-28 16:06:04 +0000637
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000638static void test_newraster(skiatest::Reporter* reporter) {
639 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
reed3054be12014-12-10 07:24:28 -0800640 const size_t minRowBytes = info.minRowBytes();
641 const size_t size = info.getSafeSize(minRowBytes);
scroggo565901d2015-12-10 10:44:13 -0800642 SkAutoTMalloc<SkPMColor> storage(size);
643 SkPMColor* baseAddr = storage.get();
reed3054be12014-12-10 07:24:28 -0800644 sk_bzero(baseAddr, size);
645
646 SkCanvas* canvas = SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000647 REPORTER_ASSERT(reporter, canvas);
648
649 SkImageInfo info2;
650 size_t rowBytes;
651 const SkPMColor* addr = (const SkPMColor*)canvas->peekPixels(&info2, &rowBytes);
652 REPORTER_ASSERT(reporter, addr);
653 REPORTER_ASSERT(reporter, info == info2);
reed3054be12014-12-10 07:24:28 -0800654 REPORTER_ASSERT(reporter, minRowBytes == rowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000655 for (int y = 0; y < info.height(); ++y) {
656 for (int x = 0; x < info.width(); ++x) {
657 REPORTER_ASSERT(reporter, 0 == addr[x]);
658 }
659 addr = (const SkPMColor*)((const char*)addr + rowBytes);
660 }
halcanary385fe4d2015-08-26 13:07:48 -0700661 delete canvas;
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000662
663 // now try a deliberately bad info
reede5ea5002014-09-03 11:54:58 -0700664 info = info.makeWH(-1, info.height());
halcanary96fcdcc2015-08-27 07:41:13 -0700665 REPORTER_ASSERT(reporter, nullptr == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000666
667 // too big
reede5ea5002014-09-03 11:54:58 -0700668 info = info.makeWH(1 << 30, 1 << 30);
halcanary96fcdcc2015-08-27 07:41:13 -0700669 REPORTER_ASSERT(reporter, nullptr == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
skia.committer@gmail.com0e530752014-02-28 03:02:05 +0000670
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000671 // not a valid pixel type
reede5ea5002014-09-03 11:54:58 -0700672 info = SkImageInfo::Make(10, 10, kUnknown_SkColorType, info.alphaType());
halcanary96fcdcc2015-08-27 07:41:13 -0700673 REPORTER_ASSERT(reporter, nullptr == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000674
675 // We should succeed with a zero-sized valid info
676 info = SkImageInfo::MakeN32Premul(0, 0);
reed3054be12014-12-10 07:24:28 -0800677 canvas = SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000678 REPORTER_ASSERT(reporter, canvas);
halcanary385fe4d2015-08-26 13:07:48 -0700679 delete canvas;
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000680}
681
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000682DEF_TEST(Canvas, reporter) {
piotaixrf05f5a72014-10-03 13:26:55 -0700683 TestData d;
reed@google.com37f3ae02011-11-28 16:06:04 +0000684
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000685 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
piotaixrf05f5a72014-10-03 13:26:55 -0700686 TestOverrideStateConsistency(reporter, d, testStepArray()[testStep]);
edisonn@google.com77909122012-10-18 15:58:23 +0000687 if (testStepArray()[testStep]->enablePdfTesting()) {
piotaixrf05f5a72014-10-03 13:26:55 -0700688 TestPdfDevice(reporter, d, testStepArray()[testStep]);
edisonn@google.com77909122012-10-18 15:58:23 +0000689 }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000690 }
junov@chromium.orgcd62ecf2012-08-02 17:43:25 +0000691
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000692 test_newraster(reporter);
reed@google.com37f3ae02011-11-28 16:06:04 +0000693}
reedf0090cb2014-11-26 08:55:51 -0800694
695DEF_TEST(Canvas_SaveState, reporter) {
696 SkCanvas canvas(10, 10);
697 REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
698
699 int n = canvas.save();
700 REPORTER_ASSERT(reporter, 1 == n);
701 REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
702
halcanary96fcdcc2015-08-27 07:41:13 -0700703 n = canvas.saveLayer(nullptr, nullptr);
reedf0090cb2014-11-26 08:55:51 -0800704 REPORTER_ASSERT(reporter, 2 == n);
705 REPORTER_ASSERT(reporter, 3 == canvas.getSaveCount());
706
707 canvas.restore();
708 REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
709 canvas.restore();
710 REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
711}
reedc1b11f12015-03-13 08:48:26 -0700712
713DEF_TEST(Canvas_ClipEmptyPath, reporter) {
714 SkCanvas canvas(10, 10);
715 canvas.save();
716 SkPath path;
717 canvas.clipPath(path);
718 canvas.restore();
719 canvas.save();
720 path.moveTo(5, 5);
721 canvas.clipPath(path);
722 canvas.restore();
723 canvas.save();
724 path.moveTo(7, 7);
725 canvas.clipPath(path); // should not assert here
726 canvas.restore();
727}
fmalitaf433bb22015-08-17 08:05:13 -0700728
729namespace {
730
731class MockFilterCanvas : public SkPaintFilterCanvas {
732public:
733 MockFilterCanvas(SkCanvas* canvas) : INHERITED(canvas) { }
734
735protected:
fmalita32cdc322016-01-12 07:21:11 -0800736 bool onFilter(SkTCopyOnFirstWrite<SkPaint>*, Type) const override { return true; }
fmalitaf433bb22015-08-17 08:05:13 -0700737
738private:
739 typedef SkPaintFilterCanvas INHERITED;
740};
741
742} // anonymous namespace
743
744// SkPaintFilterCanvas should inherit the initial target canvas state.
745DEF_TEST(PaintFilterCanvas_ConsistentState, reporter) {
746 SkCanvas canvas(100, 100);
747 canvas.clipRect(SkRect::MakeXYWH(12.7f, 12.7f, 75, 75));
748 canvas.scale(0.5f, 0.75f);
749
750 SkRect clip1, clip2;
751
752 MockFilterCanvas filterCanvas(&canvas);
753 REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix());
754 REPORTER_ASSERT(reporter, canvas.getClipBounds(&clip1) == filterCanvas.getClipBounds(&clip2));
755 REPORTER_ASSERT(reporter, clip1 == clip2);
756
757 filterCanvas.clipRect(SkRect::MakeXYWH(30.5f, 30.7f, 100, 100));
758 filterCanvas.scale(0.75f, 0.5f);
759 REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix());
760 REPORTER_ASSERT(reporter, canvas.getClipBounds(&clip1) == filterCanvas.getClipBounds(&clip2));
761 REPORTER_ASSERT(reporter, clip1 == clip2);
762}