blob: 2ec25e4bab023b699107b57cf92f0957c2c26e31 [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"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000064#include "SkTDArray.h"
65#include "Test.h"
reed@google.com37f3ae02011-11-28 16:06:04 +000066
piotaixrf05f5a72014-10-03 13:26:55 -070067static const int kWidth = 2, kHeight = 2;
68
69static void createBitmap(SkBitmap* bm, SkColor color) {
70 bm->allocN32Pixels(kWidth, kHeight);
71 bm->eraseColor(color);
72}
73
piotaixrf05f5a72014-10-03 13:26:55 -070074///////////////////////////////////////////////////////////////////////////////
75// Constants used by test steps
76const SkPoint kTestPoints[] = {
77 {SkIntToScalar(0), SkIntToScalar(0)},
78 {SkIntToScalar(2), SkIntToScalar(1)},
79 {SkIntToScalar(0), SkIntToScalar(2)}
80};
81const SkPoint kTestPoints2[] = {
82 { SkIntToScalar(0), SkIntToScalar(1) },
83 { SkIntToScalar(1), SkIntToScalar(1) },
84 { SkIntToScalar(2), SkIntToScalar(1) },
85 { SkIntToScalar(3), SkIntToScalar(1) },
86 { SkIntToScalar(4), SkIntToScalar(1) },
87 { SkIntToScalar(5), SkIntToScalar(1) },
88 { SkIntToScalar(6), SkIntToScalar(1) },
89 { SkIntToScalar(7), SkIntToScalar(1) },
90 { SkIntToScalar(8), SkIntToScalar(1) },
91 { SkIntToScalar(9), SkIntToScalar(1) },
92 { SkIntToScalar(10), SkIntToScalar(1) }
93};
94
95struct TestData {
96public:
97 TestData()
98 : fRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
99 SkIntToScalar(2), SkIntToScalar(1)))
100 , fMatrix(TestMatrix())
101 , fPath(TestPath())
102 , fNearlyZeroLengthPath(TestNearlyZeroLengthPath())
103 , fIRect(SkIRect::MakeXYWH(0, 0, 2, 1))
104 , fRegion(TestRegion())
105 , fColor(0x01020304)
106 , fPoints(kTestPoints)
107 , fPointCount(3)
108 , fWidth(2)
109 , fHeight(2)
110 , fText("Hello World")
111 , fPoints2(kTestPoints2)
112 , fBitmap(TestBitmap())
113 { }
114
115 SkRect fRect;
tfarina567ff2f2015-04-27 07:01:44 -0700116 SkMatrix fMatrix;
piotaixrf05f5a72014-10-03 13:26:55 -0700117 SkPath fPath;
118 SkPath fNearlyZeroLengthPath;
119 SkIRect fIRect;
120 SkRegion fRegion;
121 SkColor fColor;
122 SkPaint fPaint;
123 const SkPoint* fPoints;
124 size_t fPointCount;
125 int fWidth;
126 int fHeight;
127 SkString fText;
128 const SkPoint* fPoints2;
129 SkBitmap fBitmap;
130
131private:
132 static SkMatrix TestMatrix() {
133 SkMatrix matrix;
134 matrix.reset();
135 matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
136
137 return matrix;
138 }
139 static SkPath TestPath() {
140 SkPath path;
141 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
142 SkIntToScalar(2), SkIntToScalar(1)));
143 return path;
144 }
145 static SkPath TestNearlyZeroLengthPath() {
146 SkPath path;
147 SkPoint pt1 = { 0, 0 };
148 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
149 SkPoint pt3 = { SkIntToScalar(1), 0 };
150 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
151 path.moveTo(pt1);
152 path.lineTo(pt2);
153 path.lineTo(pt3);
154 path.lineTo(pt4);
155 return path;
156 }
157 static SkRegion TestRegion() {
158 SkRegion region;
159 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
160 region.setRect(rect);
161 return region;
162 }
163 static SkBitmap TestBitmap() {
164 SkBitmap bitmap;
165 createBitmap(&bitmap, 0x05060708);
166 return bitmap;
167 }
168};
169
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000170static bool equal_clips(const SkCanvas& a, const SkCanvas& b) {
171 if (a.isClipEmpty()) {
172 return b.isClipEmpty();
173 }
174 if (!a.isClipRect()) {
175 // this is liberally true, since we don't expose a way to know this exactly (for non-rects)
176 return !b.isClipRect();
177 }
178 SkIRect ar, br;
179 a.getClipDeviceBounds(&ar);
180 b.getClipDeviceBounds(&br);
181 return ar == br;
182}
183
reed@google.com90c07ea2012-04-13 13:50:27 +0000184class Canvas2CanvasClipVisitor : public SkCanvas::ClipVisitor {
185public:
186 Canvas2CanvasClipVisitor(SkCanvas* target) : fTarget(target) {}
187
mtklein36352bf2015-03-25 18:17:31 -0700188 void clipRect(const SkRect& r, SkRegion::Op op, bool aa) override {
reed@google.com90c07ea2012-04-13 13:50:27 +0000189 fTarget->clipRect(r, op, aa);
190 }
mtklein36352bf2015-03-25 18:17:31 -0700191 void clipRRect(const SkRRect& r, SkRegion::Op op, bool aa) override {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000192 fTarget->clipRRect(r, op, aa);
193 }
mtklein36352bf2015-03-25 18:17:31 -0700194 void clipPath(const SkPath& p, SkRegion::Op op, bool aa) override {
reed@google.com90c07ea2012-04-13 13:50:27 +0000195 fTarget->clipPath(p, op, aa);
196 }
197
198private:
199 SkCanvas* fTarget;
200};
201
202static void test_clipVisitor(skiatest::Reporter* reporter, SkCanvas* canvas) {
203 SkISize size = canvas->getDeviceSize();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000204
reed@google.com90c07ea2012-04-13 13:50:27 +0000205 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000206 bm.setInfo(SkImageInfo::MakeN32Premul(size.width(), size.height()));
reed@google.com90c07ea2012-04-13 13:50:27 +0000207 SkCanvas c(bm);
208
209 Canvas2CanvasClipVisitor visitor(&c);
210 canvas->replayClips(&visitor);
211
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000212 REPORTER_ASSERT(reporter, equal_clips(c, *canvas));
reed@google.com90c07ea2012-04-13 13:50:27 +0000213}
214
reed687fa1c2015-04-07 08:00:56 -0700215static void test_clipstack(skiatest::Reporter* reporter) {
216 // The clipstack is refcounted, and needs to be able to out-live the canvas if a client has
217 // ref'd it.
halcanary96fcdcc2015-08-27 07:41:13 -0700218 const SkClipStack* cs = nullptr;
reed687fa1c2015-04-07 08:00:56 -0700219 {
220 SkCanvas canvas(10, 10);
221 cs = SkRef(canvas.getClipStack());
222 }
223 REPORTER_ASSERT(reporter, cs->unique());
224 cs->unref();
225}
226
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000227// Format strings that describe the test context. The %s token is where
228// the name of the test step is inserted. The context is required for
229// disambiguating the error in the case of failures that are reported in
230// functions that are called multiple times in different contexts (test
231// cases and test steps).
232static const char* const kDefaultAssertMessageFormat = "%s";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000233static const char* const kCanvasDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000234 "Drawing test step %s with SkCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000235static const char* const kNWayDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000236 "Drawing test step %s with SkNWayCanvas";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000237static const char* const kNWayStateAssertMessageFormat =
238 "test step %s, SkNWayCanvas state consistency";
239static const char* const kNWayIndirect1StateAssertMessageFormat =
240 "test step %s, SkNWayCanvas indirect canvas 1 state consistency";
241static const char* const kNWayIndirect2StateAssertMessageFormat =
242 "test step %s, SkNWayCanvas indirect canvas 2 state consistency";
edisonn@google.com77909122012-10-18 15:58:23 +0000243static const char* const kPdfAssertMessageFormat =
244 "PDF sanity check failed %s";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000245
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000246class CanvasTestStep;
247static SkTDArray<CanvasTestStep*>& testStepArray() {
248 static SkTDArray<CanvasTestStep*> theTests;
249 return theTests;
250}
251
252class CanvasTestStep {
253public:
edisonn@google.com77909122012-10-18 15:58:23 +0000254 CanvasTestStep(bool fEnablePdfTesting = true) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000255 *testStepArray().append() = this;
256 fAssertMessageFormat = kDefaultAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000257 this->fEnablePdfTesting = fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000258 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000259 virtual ~CanvasTestStep() { }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000260
piotaixrf05f5a72014-10-03 13:26:55 -0700261 virtual void draw(SkCanvas*, const TestData&, skiatest::Reporter*) = 0;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000262 virtual const char* name() const = 0;
263
264 const char* assertMessage() {
265 fAssertMessage.printf(fAssertMessageFormat, name());
266 return fAssertMessage.c_str();
267 }
268
269 void setAssertMessageFormat(const char* format) {
270 fAssertMessageFormat = format;
271 }
272
edisonn@google.com77909122012-10-18 15:58:23 +0000273 bool enablePdfTesting() { return fEnablePdfTesting; }
274
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000275private:
276 SkString fAssertMessage;
277 const char* fAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000278 bool fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000279};
280
281///////////////////////////////////////////////////////////////////////////////
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000282// Macros for defining test steps
283
284#define TEST_STEP(NAME, FUNCTION) \
285class NAME##_TestStep : public CanvasTestStep{ \
286public: \
piotaixrf05f5a72014-10-03 13:26:55 -0700287 virtual void draw(SkCanvas* canvas, const TestData& d, \
288 skiatest::Reporter* reporter) { \
289 FUNCTION (canvas, d, reporter, this); \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000290 } \
291 virtual const char* name() const {return #NAME ;} \
292}; \
293static NAME##_TestStep NAME##_TestStepInstance;
294
piotaixrf05f5a72014-10-03 13:26:55 -0700295#define TEST_STEP_NO_PDF(NAME, FUNCTION) \
edisonn@google.com77909122012-10-18 15:58:23 +0000296class NAME##_TestStep : public CanvasTestStep{ \
297public: \
298 NAME##_TestStep() : CanvasTestStep(false) {} \
piotaixrf05f5a72014-10-03 13:26:55 -0700299 virtual void draw(SkCanvas* canvas, const TestData& d, \
300 skiatest::Reporter* reporter) { \
301 FUNCTION (canvas, d, reporter, this); \
edisonn@google.com77909122012-10-18 15:58:23 +0000302 } \
303 virtual const char* name() const {return #NAME ;} \
304}; \
305static NAME##_TestStep NAME##_TestStepInstance;
306
piotaixrf05f5a72014-10-03 13:26:55 -0700307#define SIMPLE_TEST_STEP(NAME, CALL) \
308static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \
309 skiatest::Reporter*, CanvasTestStep*) { \
310 canvas-> CALL ; \
311} \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000312TEST_STEP(NAME, NAME##TestStep )
313
314#define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
piotaixrf05f5a72014-10-03 13:26:55 -0700315static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \
316 skiatest::Reporter*, CanvasTestStep* testStep) { \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000317 REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \
318 testStep->assertMessage()); \
319} \
320TEST_STEP(NAME, NAME##TestStep )
321
322
323///////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000324// Basic test steps for most virtual methods in SkCanvas that draw or affect
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000325// the state of the canvas.
326
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000327SIMPLE_TEST_STEP(Translate, translate(SkIntToScalar(1), SkIntToScalar(2)));
328SIMPLE_TEST_STEP(Scale, scale(SkIntToScalar(1), SkIntToScalar(2)));
329SIMPLE_TEST_STEP(Rotate, rotate(SkIntToScalar(1)));
330SIMPLE_TEST_STEP(Skew, skew(SkIntToScalar(1), SkIntToScalar(2)));
piotaixrf05f5a72014-10-03 13:26:55 -0700331SIMPLE_TEST_STEP(Concat, concat(d.fMatrix));
332SIMPLE_TEST_STEP(SetMatrix, setMatrix(d.fMatrix));
333SIMPLE_TEST_STEP(ClipRect, clipRect(d.fRect));
334SIMPLE_TEST_STEP(ClipPath, clipPath(d.fPath));
335SIMPLE_TEST_STEP(ClipRegion, clipRegion(d.fRegion, SkRegion::kReplace_Op));
336SIMPLE_TEST_STEP(Clear, clear(d.fColor));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000337
338///////////////////////////////////////////////////////////////////////////////
339// Complex test steps
340
piotaixrf05f5a72014-10-03 13:26:55 -0700341static void SaveMatrixClipStep(SkCanvas* canvas, const TestData& d,
342 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000343 int saveCount = canvas->getSaveCount();
Florin Malita5f6102d2014-06-30 10:13:28 -0400344 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000345 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
piotaixrf05f5a72014-10-03 13:26:55 -0700346 canvas->clipRegion(d.fRegion);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000347 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000348 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000349 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000350 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000351 testStep->assertMessage());
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000352// REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion, testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000353}
354TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
355
piotaixrf05f5a72014-10-03 13:26:55 -0700356static void SaveLayerStep(SkCanvas* canvas, const TestData& d,
357 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000358 int saveCount = canvas->getSaveCount();
halcanary96fcdcc2015-08-27 07:41:13 -0700359 canvas->saveLayer(nullptr, nullptr);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000360 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000361 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000362 testStep->assertMessage());
363}
364TEST_STEP(SaveLayer, SaveLayerStep);
365
piotaixrf05f5a72014-10-03 13:26:55 -0700366static void BoundedSaveLayerStep(SkCanvas* canvas, const TestData& d,
367 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000368 int saveCount = canvas->getSaveCount();
halcanary96fcdcc2015-08-27 07:41:13 -0700369 canvas->saveLayer(&d.fRect, nullptr);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000370 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000371 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000372 testStep->assertMessage());
373}
374TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
375
piotaixrf05f5a72014-10-03 13:26:55 -0700376static void PaintSaveLayerStep(SkCanvas* canvas, const TestData& d,
377 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000378 int saveCount = canvas->getSaveCount();
halcanary96fcdcc2015-08-27 07:41:13 -0700379 canvas->saveLayer(nullptr, &d.fPaint);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000380 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000381 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000382 testStep->assertMessage());
383}
384TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
385
piotaixrf05f5a72014-10-03 13:26:55 -0700386static void TwoClipOpsStep(SkCanvas* canvas, const TestData& d,
387 skiatest::Reporter*, CanvasTestStep*) {
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000388 // This test exercises a functionality in SkPicture that leads to the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000389 // recording of restore offset placeholders. This test will trigger an
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000390 // assertion at playback time if the placeholders are not properly
391 // filled when the recording ends.
piotaixrf05f5a72014-10-03 13:26:55 -0700392 canvas->clipRect(d.fRect);
393 canvas->clipRegion(d.fRegion);
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000394}
395TEST_STEP(TwoClipOps, TwoClipOpsStep);
396
epoger@google.com94fa43c2012-04-11 17:51:01 +0000397// exercise fix for http://code.google.com/p/skia/issues/detail?id=560
398// ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
piotaixrf05f5a72014-10-03 13:26:55 -0700399static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas, const TestData& d,
400 skiatest::Reporter*, CanvasTestStep*) {
epoger@google.com94fa43c2012-04-11 17:51:01 +0000401 SkPaint paint;
402 paint.setStrokeWidth(SkIntToScalar(1));
403 paint.setStyle(SkPaint::kStroke_Style);
404
piotaixrf05f5a72014-10-03 13:26:55 -0700405 canvas->drawPath(d.fNearlyZeroLengthPath, paint);
epoger@google.com94fa43c2012-04-11 17:51:01 +0000406}
407TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
408
piotaixrf05f5a72014-10-03 13:26:55 -0700409static void DrawVerticesShaderTestStep(SkCanvas* canvas, const TestData& d,
410 skiatest::Reporter*, CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000411 SkPoint pts[4];
412 pts[0].set(0, 0);
piotaixrf05f5a72014-10-03 13:26:55 -0700413 pts[1].set(SkIntToScalar(d.fWidth), 0);
414 pts[2].set(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight));
415 pts[3].set(0, SkIntToScalar(d.fHeight));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000416 SkPaint paint;
piotaixrf05f5a72014-10-03 13:26:55 -0700417 SkShader* shader = SkShader::CreateBitmapShader(d.fBitmap,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000418 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
419 paint.setShader(shader)->unref();
420 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
halcanary96fcdcc2015-08-27 07:41:13 -0700421 nullptr, nullptr, nullptr, 0, paint);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000422}
edisonn@google.com77909122012-10-18 15:58:23 +0000423// NYI: issue 240.
424TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000425
piotaixrf05f5a72014-10-03 13:26:55 -0700426static void DrawPictureTestStep(SkCanvas* canvas, const TestData& d,
427 skiatest::Reporter*, CanvasTestStep*) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000428 SkPictureRecorder recorder;
piotaixrf05f5a72014-10-03 13:26:55 -0700429 SkCanvas* testCanvas = recorder.beginRecording(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight),
halcanary96fcdcc2015-08-27 07:41:13 -0700430 nullptr, 0);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000431 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
piotaixrf05f5a72014-10-03 13:26:55 -0700432 testCanvas->clipRect(d.fRect);
433 testCanvas->drawRect(d.fRect, d.fPaint);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000434 SkAutoTUnref<SkPicture> testPicture(recorder.endRecording());
435
robertphillips9b14f262014-06-04 05:40:44 -0700436 canvas->drawPicture(testPicture);
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
piotaixrf05f5a72014-10-03 13:26:55 -0700502static void AssertCanvasStatesEqual(skiatest::Reporter* reporter, const TestData& d,
503 const SkCanvas* canvas1, const SkCanvas* canvas2,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000504 CanvasTestStep* testStep) {
505 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
506 canvas2->getDeviceSize(), testStep->assertMessage());
507 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
508 canvas2->getSaveCount(), testStep->assertMessage());
reed@google.com3b3e8952012-08-16 20:53:31 +0000509
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000510 SkRect bounds1, bounds2;
511 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.com3b3e8952012-08-16 20:53:31 +0000512 canvas1->getClipBounds(&bounds1) == canvas2->getClipBounds(&bounds2),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000513 testStep->assertMessage());
514 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
reed@google.com3b3e8952012-08-16 20:53:31 +0000515 testStep->assertMessage());
516
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000517 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
518 canvas2->getDrawFilter(), testStep->assertMessage());
519 SkIRect deviceBounds1, deviceBounds2;
520 REPORTER_ASSERT_MESSAGE(reporter,
521 canvas1->getClipDeviceBounds(&deviceBounds1) ==
522 canvas2->getClipDeviceBounds(&deviceBounds2),
523 testStep->assertMessage());
reed868074b2014-06-03 10:53:59 -0700524 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2, testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000525 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
526 canvas2->getTotalMatrix(), testStep->assertMessage());
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000527 REPORTER_ASSERT_MESSAGE(reporter, equal_clips(*canvas1, *canvas2), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000528
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000529 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
530 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
531 while (!layerIter1.done() && !layerIter2.done()) {
532 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
533 layerIter2.matrix(), testStep->assertMessage());
534 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
535 layerIter2.clip(), testStep->assertMessage());
536 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
537 layerIter2.paint(), testStep->assertMessage());
538 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
539 layerIter2.x(), testStep->assertMessage());
540 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
541 layerIter2.y(), testStep->assertMessage());
542 layerIter1.next();
543 layerIter2.next();
544 }
545 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
546 testStep->assertMessage());
547 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
548 testStep->assertMessage());
piotaixr76993ed2014-10-27 15:31:34 -0700549
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000550}
551
edisonn@google.com77909122012-10-18 15:58:23 +0000552static void TestPdfDevice(skiatest::Reporter* reporter,
piotaixrf05f5a72014-10-03 13:26:55 -0700553 const TestData& d,
edisonn@google.com77909122012-10-18 15:58:23 +0000554 CanvasTestStep* testStep) {
halcanary3d32d502015-03-01 06:55:20 -0800555 SkDynamicMemoryWStream outStream;
556 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&outStream));
halcanary8ee06f22015-08-11 10:30:12 -0700557#if SK_SUPPORT_PDF
558 REPORTER_ASSERT(reporter, doc);
halcanary2ccdb632015-08-11 13:35:12 -0700559#else
560 REPORTER_ASSERT(reporter, !doc);
561#endif // SK_SUPPORT_PDF
562 if (!doc) {
563 return;
564 }
halcanary3d32d502015-03-01 06:55:20 -0800565 SkCanvas* canvas = doc->beginPage(SkIntToScalar(d.fWidth),
566 SkIntToScalar(d.fHeight));
567 REPORTER_ASSERT(reporter, canvas);
edisonn@google.com77909122012-10-18 15:58:23 +0000568 testStep->setAssertMessageFormat(kPdfAssertMessageFormat);
halcanary3d32d502015-03-01 06:55:20 -0800569 testStep->draw(canvas, d, reporter);
570
571 REPORTER_ASSERT(reporter, doc->close());
edisonn@google.com77909122012-10-18 15:58:23 +0000572}
573
caryclark@google.com42639cd2012-06-06 12:03:39 +0000574// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000575static void TestNWayCanvasStateConsistency(
576 skiatest::Reporter* reporter,
piotaixrf05f5a72014-10-03 13:26:55 -0700577 const TestData& d,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000578 CanvasTestStep* testStep,
579 const SkCanvas& referenceCanvas) {
580
581 SkBitmap indirectStore1;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000582 createBitmap(&indirectStore1, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700583 SkCanvas indirectCanvas1(indirectStore1);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000584
585 SkBitmap indirectStore2;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000586 createBitmap(&indirectStore2, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700587 SkCanvas indirectCanvas2(indirectStore2);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000588
djsollen@google.comf0a062b2012-05-01 16:50:25 +0000589 SkISize canvasSize = referenceCanvas.getDeviceSize();
590 SkNWayCanvas nWayCanvas(canvasSize.width(), canvasSize.height());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000591 nWayCanvas.addCanvas(&indirectCanvas1);
592 nWayCanvas.addCanvas(&indirectCanvas2);
593
594 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700595 testStep->draw(&nWayCanvas, d, reporter);
scroggo648238c2015-01-29 11:58:51 -0800596 // Verify that the SkNWayCanvas reports consitent state
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000597 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700598 AssertCanvasStatesEqual(reporter, d, &nWayCanvas, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000599 // Verify that the indirect canvases report consitent state
600 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700601 AssertCanvasStatesEqual(reporter, d, &indirectCanvas1, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000602 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700603 AssertCanvasStatesEqual(reporter, d, &indirectCanvas2, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000604}
605
606/*
607 * This sub-test verifies that the test step passes when executed
608 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
609 * that the all canvas derivatives report the same state as an SkCanvas
610 * after having executed the test step.
611 */
piotaixrf05f5a72014-10-03 13:26:55 -0700612static void TestOverrideStateConsistency(skiatest::Reporter* reporter, const TestData& d,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000613 CanvasTestStep* testStep) {
614 SkBitmap referenceStore;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000615 createBitmap(&referenceStore, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700616 SkCanvas referenceCanvas(referenceStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000617 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700618 testStep->draw(&referenceCanvas, d, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000619
caryclark@google.com42639cd2012-06-06 12:03:39 +0000620 // The following test code is disabled because SkNWayCanvas does not
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000621 // report correct clipping and device bounds information
622 // Issue: http://code.google.com/p/skia/issues/detail?id=501
caryclark@google.com42639cd2012-06-06 12:03:39 +0000623
624 if (false) { // avoid bit rot, suppress warning
piotaixrf05f5a72014-10-03 13:26:55 -0700625 TestNWayCanvasStateConsistency(reporter, d, testStep, referenceCanvas);
caryclark@google.com42639cd2012-06-06 12:03:39 +0000626 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000627
caryclark@google.com42639cd2012-06-06 12:03:39 +0000628 if (false) { // avoid bit rot, suppress warning
629 test_clipVisitor(reporter, &referenceCanvas);
630 }
reed687fa1c2015-04-07 08:00:56 -0700631 test_clipstack(reporter);
reed@google.com7c202932011-12-14 18:48:05 +0000632}
reed@google.com37f3ae02011-11-28 16:06:04 +0000633
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000634static void test_newraster(skiatest::Reporter* reporter) {
635 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
reed3054be12014-12-10 07:24:28 -0800636 const size_t minRowBytes = info.minRowBytes();
637 const size_t size = info.getSafeSize(minRowBytes);
638 SkAutoMalloc storage(size);
639 SkPMColor* baseAddr = static_cast<SkPMColor*>(storage.get());
640 sk_bzero(baseAddr, size);
641
642 SkCanvas* canvas = SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000643 REPORTER_ASSERT(reporter, canvas);
644
645 SkImageInfo info2;
646 size_t rowBytes;
647 const SkPMColor* addr = (const SkPMColor*)canvas->peekPixels(&info2, &rowBytes);
648 REPORTER_ASSERT(reporter, addr);
649 REPORTER_ASSERT(reporter, info == info2);
reed3054be12014-12-10 07:24:28 -0800650 REPORTER_ASSERT(reporter, minRowBytes == rowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000651 for (int y = 0; y < info.height(); ++y) {
652 for (int x = 0; x < info.width(); ++x) {
653 REPORTER_ASSERT(reporter, 0 == addr[x]);
654 }
655 addr = (const SkPMColor*)((const char*)addr + rowBytes);
656 }
halcanary385fe4d2015-08-26 13:07:48 -0700657 delete canvas;
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000658
659 // now try a deliberately bad info
reede5ea5002014-09-03 11:54:58 -0700660 info = info.makeWH(-1, info.height());
halcanary96fcdcc2015-08-27 07:41:13 -0700661 REPORTER_ASSERT(reporter, nullptr == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000662
663 // too big
reede5ea5002014-09-03 11:54:58 -0700664 info = info.makeWH(1 << 30, 1 << 30);
halcanary96fcdcc2015-08-27 07:41:13 -0700665 REPORTER_ASSERT(reporter, nullptr == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
skia.committer@gmail.com0e530752014-02-28 03:02:05 +0000666
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000667 // not a valid pixel type
reede5ea5002014-09-03 11:54:58 -0700668 info = SkImageInfo::Make(10, 10, kUnknown_SkColorType, info.alphaType());
halcanary96fcdcc2015-08-27 07:41:13 -0700669 REPORTER_ASSERT(reporter, nullptr == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000670
671 // We should succeed with a zero-sized valid info
672 info = SkImageInfo::MakeN32Premul(0, 0);
reed3054be12014-12-10 07:24:28 -0800673 canvas = SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000674 REPORTER_ASSERT(reporter, canvas);
halcanary385fe4d2015-08-26 13:07:48 -0700675 delete canvas;
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000676}
677
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000678DEF_TEST(Canvas, reporter) {
piotaixrf05f5a72014-10-03 13:26:55 -0700679 TestData d;
reed@google.com37f3ae02011-11-28 16:06:04 +0000680
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000681 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
piotaixrf05f5a72014-10-03 13:26:55 -0700682 TestOverrideStateConsistency(reporter, d, testStepArray()[testStep]);
edisonn@google.com77909122012-10-18 15:58:23 +0000683 if (testStepArray()[testStep]->enablePdfTesting()) {
piotaixrf05f5a72014-10-03 13:26:55 -0700684 TestPdfDevice(reporter, d, testStepArray()[testStep]);
edisonn@google.com77909122012-10-18 15:58:23 +0000685 }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000686 }
junov@chromium.orgcd62ecf2012-08-02 17:43:25 +0000687
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000688 test_newraster(reporter);
reed@google.com37f3ae02011-11-28 16:06:04 +0000689}
reedf0090cb2014-11-26 08:55:51 -0800690
691DEF_TEST(Canvas_SaveState, reporter) {
692 SkCanvas canvas(10, 10);
693 REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
694
695 int n = canvas.save();
696 REPORTER_ASSERT(reporter, 1 == n);
697 REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
698
halcanary96fcdcc2015-08-27 07:41:13 -0700699 n = canvas.saveLayer(nullptr, nullptr);
reedf0090cb2014-11-26 08:55:51 -0800700 REPORTER_ASSERT(reporter, 2 == n);
701 REPORTER_ASSERT(reporter, 3 == canvas.getSaveCount());
702
703 canvas.restore();
704 REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
705 canvas.restore();
706 REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
707}
reedc1b11f12015-03-13 08:48:26 -0700708
709DEF_TEST(Canvas_ClipEmptyPath, reporter) {
710 SkCanvas canvas(10, 10);
711 canvas.save();
712 SkPath path;
713 canvas.clipPath(path);
714 canvas.restore();
715 canvas.save();
716 path.moveTo(5, 5);
717 canvas.clipPath(path);
718 canvas.restore();
719 canvas.save();
720 path.moveTo(7, 7);
721 canvas.clipPath(path); // should not assert here
722 canvas.restore();
723}
fmalitaf433bb22015-08-17 08:05:13 -0700724
725namespace {
726
727class MockFilterCanvas : public SkPaintFilterCanvas {
728public:
729 MockFilterCanvas(SkCanvas* canvas) : INHERITED(canvas) { }
730
731protected:
732 void onFilterPaint(SkPaint *paint, Type type) const override { }
733
734private:
735 typedef SkPaintFilterCanvas INHERITED;
736};
737
738} // anonymous namespace
739
740// SkPaintFilterCanvas should inherit the initial target canvas state.
741DEF_TEST(PaintFilterCanvas_ConsistentState, reporter) {
742 SkCanvas canvas(100, 100);
743 canvas.clipRect(SkRect::MakeXYWH(12.7f, 12.7f, 75, 75));
744 canvas.scale(0.5f, 0.75f);
745
746 SkRect clip1, clip2;
747
748 MockFilterCanvas filterCanvas(&canvas);
749 REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix());
750 REPORTER_ASSERT(reporter, canvas.getClipBounds(&clip1) == filterCanvas.getClipBounds(&clip2));
751 REPORTER_ASSERT(reporter, clip1 == clip2);
752
753 filterCanvas.clipRect(SkRect::MakeXYWH(30.5f, 30.7f, 100, 100));
754 filterCanvas.scale(0.75f, 0.5f);
755 REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix());
756 REPORTER_ASSERT(reporter, canvas.getClipBounds(&clip1) == filterCanvas.getClipBounds(&clip2));
757 REPORTER_ASSERT(reporter, clip1 == clip2);
758}