blob: bf30dd9c559798e7aafdb4ee8581dda2acf459f0 [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 * (...)
Brian Salomon1c80e992018-01-29 09:50:47 -050027 * REPORTER_ASSERT(reporter, (...), \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000028 * 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 */
Mike Reed2a65cc02017-03-22 10:01:53 -040047
reed@google.com37f3ae02011-11-28 16:06:04 +000048#include "SkBitmap.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040049#include "SkBlendMode.h"
reed@google.com37f3ae02011-11-28 16:06:04 +000050#include "SkCanvas.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040051#include "SkCanvasStack.h"
52#include "SkClipOp.h"
53#include "SkClipOpPriv.h"
54#include "SkColor.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040055#include "SkImageFilter.h"
56#include "SkImageInfo.h"
57#include "SkMalloc.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000058#include "SkMatrix.h"
59#include "SkNWayCanvas.h"
Hal Canary23564b92018-09-07 14:33:14 -040060#include "SkPDFDocument.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000061#include "SkPaint.h"
fmalitaf433bb22015-08-17 08:05:13 -070062#include "SkPaintFilterCanvas.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000063#include "SkPath.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000064#include "SkPictureRecorder.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040065#include "SkPixmap.h"
66#include "SkPoint.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000067#include "SkRect.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040068#include "SkRefCnt.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000069#include "SkRegion.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040070#include "SkScalar.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000071#include "SkShader.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040072#include "SkSize.h"
Xianzhu Wang3b428cb2018-01-22 09:51:13 -080073#include "SkSpecialImage.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000074#include "SkStream.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040075#include "SkString.h"
reed@google.com28183b42014-02-04 15:34:10 +000076#include "SkSurface.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000077#include "SkTDArray.h"
Xianzhu Wang3b428cb2018-01-22 09:51:13 -080078#include "SkTemplates.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040079#include "SkTypes.h"
Mike Reed887cdf12017-04-03 11:11:09 -040080#include "SkVertices.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000081#include "Test.h"
reed@google.com37f3ae02011-11-28 16:06:04 +000082
Ben Wagnerb607a8f2018-03-12 13:46:21 -040083#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
84#include "SkColorData.h"
85#include "SkColorSpace.h"
86#endif
87
88#include <memory>
89#include <utility>
90
91class SkColorSpaceXformer;
92class SkReadBuffer;
93template <typename T> class SkTCopyOnFirstWrite;
94
Mike Reed918e1442017-01-23 11:39:45 -050095DEF_TEST(canvas_clipbounds, reporter) {
96 SkCanvas canvas(10, 10);
Mike Reed6f4a9b22017-01-24 09:13:40 -050097 SkIRect irect, irect2;
98 SkRect rect, rect2;
Mike Reed918e1442017-01-23 11:39:45 -050099
100 irect = canvas.getDeviceClipBounds();
101 REPORTER_ASSERT(reporter, irect == SkIRect::MakeWH(10, 10));
Mike Reed6f4a9b22017-01-24 09:13:40 -0500102 REPORTER_ASSERT(reporter, canvas.getDeviceClipBounds(&irect2));
103 REPORTER_ASSERT(reporter, irect == irect2);
104
Mike Reed918e1442017-01-23 11:39:45 -0500105 // local bounds are always too big today -- can we trim them?
106 rect = canvas.getLocalClipBounds();
107 REPORTER_ASSERT(reporter, rect.contains(SkRect::MakeWH(10, 10)));
Mike Reed6f4a9b22017-01-24 09:13:40 -0500108 REPORTER_ASSERT(reporter, canvas.getLocalClipBounds(&rect2));
109 REPORTER_ASSERT(reporter, rect == rect2);
Mike Reed918e1442017-01-23 11:39:45 -0500110
111 canvas.clipRect(SkRect::MakeEmpty());
112
113 irect = canvas.getDeviceClipBounds();
114 REPORTER_ASSERT(reporter, irect == SkIRect::MakeEmpty());
Mike Reed6f4a9b22017-01-24 09:13:40 -0500115 REPORTER_ASSERT(reporter, !canvas.getDeviceClipBounds(&irect2));
116 REPORTER_ASSERT(reporter, irect == irect2);
117
Mike Reed918e1442017-01-23 11:39:45 -0500118 rect = canvas.getLocalClipBounds();
119 REPORTER_ASSERT(reporter, rect == SkRect::MakeEmpty());
Mike Reed6f4a9b22017-01-24 09:13:40 -0500120 REPORTER_ASSERT(reporter, !canvas.getLocalClipBounds(&rect2));
121 REPORTER_ASSERT(reporter, rect == rect2);
Mike Reed566e53c2017-03-10 10:49:45 -0500122
123 // Test for wacky sizes that we (historically) have guarded against
124 {
125 SkCanvas c(-10, -20);
126 REPORTER_ASSERT(reporter, c.getBaseLayerSize() == SkISize::MakeEmpty());
Mike Reeddc9f0db2017-03-10 13:54:16 -0500127
128 SkPictureRecorder().beginRecording({ 5, 5, 4, 4 });
Mike Reed566e53c2017-03-10 10:49:45 -0500129 }
Mike Reed918e1442017-01-23 11:39:45 -0500130}
131
Robert Phillips09dfc472017-09-13 15:25:47 -0400132// Will call proc with multiple styles of canvas (recording, raster, pdf)
Mike Reed02be3c12017-03-23 12:34:15 -0400133template <typename F> static void multi_canvas_driver(int w, int h, F proc) {
134 proc(SkPictureRecorder().beginRecording(SkRect::MakeIWH(w, h)));
135
136 SkNullWStream stream;
Hal Canary23564b92018-09-07 14:33:14 -0400137 if (auto doc = SkPDF::MakeDocument(&stream)) {
Hal Canary4125b612018-03-20 14:17:00 -0400138 proc(doc->beginPage(SkIntToScalar(w), SkIntToScalar(h)));
139 }
Mike Reed02be3c12017-03-23 12:34:15 -0400140
141 proc(SkSurface::MakeRasterN32Premul(w, h, nullptr)->getCanvas());
142}
143
Mike Reed2a65cc02017-03-22 10:01:53 -0400144const SkIRect gBaseRestrictedR = { 0, 0, 10, 10 };
145
146static void test_restriction(skiatest::Reporter* reporter, SkCanvas* canvas) {
147 REPORTER_ASSERT(reporter, canvas->getDeviceClipBounds() == gBaseRestrictedR);
148
149 const SkIRect restrictionR = { 2, 2, 8, 8 };
150 canvas->androidFramework_setDeviceClipRestriction(restrictionR);
151 REPORTER_ASSERT(reporter, canvas->getDeviceClipBounds() == restrictionR);
152
153 const SkIRect clipR = { 4, 4, 6, 6 };
154 canvas->clipRect(SkRect::Make(clipR), SkClipOp::kIntersect);
155 REPORTER_ASSERT(reporter, canvas->getDeviceClipBounds() == clipR);
156
Mike Reed14113bc2017-05-10 14:13:20 -0400157#ifdef SK_SUPPORT_DEPRECATED_CLIPOPS
Mike Reed2a65cc02017-03-22 10:01:53 -0400158 // now test that expanding clipops can't exceed the restriction
159 const SkClipOp expanders[] = {
160 SkClipOp::kUnion_deprecated,
161 SkClipOp::kXOR_deprecated,
162 SkClipOp::kReverseDifference_deprecated,
163 SkClipOp::kReplace_deprecated,
164 };
165
166 const SkRect expandR = { 0, 0, 5, 9 };
167 SkASSERT(!SkRect::Make(restrictionR).contains(expandR));
168
169 for (SkClipOp op : expanders) {
170 canvas->save();
171 canvas->clipRect(expandR, op);
172 REPORTER_ASSERT(reporter, gBaseRestrictedR.contains(canvas->getDeviceClipBounds()));
173 canvas->restore();
174 }
Mike Reed14113bc2017-05-10 14:13:20 -0400175#endif
Mike Reed2a65cc02017-03-22 10:01:53 -0400176}
177
178/**
179 * Clip restriction logic exists in the canvas itself, and in various kinds of devices.
180 *
181 * This test explicitly tries to exercise that variety:
182 * - picture : empty device but exercises canvas itself
183 * - pdf : uses SkClipStack in its device (as does SVG and GPU)
184 * - raster : uses SkRasterClip in its device
185 */
186DEF_TEST(canvas_clip_restriction, reporter) {
Mike Reed02be3c12017-03-23 12:34:15 -0400187 multi_canvas_driver(gBaseRestrictedR.width(), gBaseRestrictedR.height(),
188 [reporter](SkCanvas* canvas) { test_restriction(reporter, canvas); });
189}
Mike Reed2a65cc02017-03-22 10:01:53 -0400190
Mike Reed02be3c12017-03-23 12:34:15 -0400191DEF_TEST(canvas_empty_clip, reporter) {
192 multi_canvas_driver(50, 50, [reporter](SkCanvas* canvas) {
193 canvas->save();
194 canvas->clipRect({0, 0, 20, 40 });
195 REPORTER_ASSERT(reporter, !canvas->isClipEmpty());
196 canvas->clipRect({30, 0, 50, 40 });
197 REPORTER_ASSERT(reporter, canvas->isClipEmpty());
198 });
Mike Reed2a65cc02017-03-22 10:01:53 -0400199}
200
piotaixrf05f5a72014-10-03 13:26:55 -0700201static const int kWidth = 2, kHeight = 2;
202
203static void createBitmap(SkBitmap* bm, SkColor color) {
204 bm->allocN32Pixels(kWidth, kHeight);
205 bm->eraseColor(color);
206}
207
piotaixrf05f5a72014-10-03 13:26:55 -0700208///////////////////////////////////////////////////////////////////////////////
209// Constants used by test steps
210const SkPoint kTestPoints[] = {
211 {SkIntToScalar(0), SkIntToScalar(0)},
212 {SkIntToScalar(2), SkIntToScalar(1)},
213 {SkIntToScalar(0), SkIntToScalar(2)}
214};
215const SkPoint kTestPoints2[] = {
216 { SkIntToScalar(0), SkIntToScalar(1) },
217 { SkIntToScalar(1), SkIntToScalar(1) },
218 { SkIntToScalar(2), SkIntToScalar(1) },
219 { SkIntToScalar(3), SkIntToScalar(1) },
220 { SkIntToScalar(4), SkIntToScalar(1) },
221 { SkIntToScalar(5), SkIntToScalar(1) },
222 { SkIntToScalar(6), SkIntToScalar(1) },
223 { SkIntToScalar(7), SkIntToScalar(1) },
224 { SkIntToScalar(8), SkIntToScalar(1) },
225 { SkIntToScalar(9), SkIntToScalar(1) },
226 { SkIntToScalar(10), SkIntToScalar(1) }
227};
228
229struct TestData {
230public:
231 TestData()
232 : fRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
233 SkIntToScalar(2), SkIntToScalar(1)))
234 , fMatrix(TestMatrix())
235 , fPath(TestPath())
236 , fNearlyZeroLengthPath(TestNearlyZeroLengthPath())
237 , fIRect(SkIRect::MakeXYWH(0, 0, 2, 1))
238 , fRegion(TestRegion())
239 , fColor(0x01020304)
240 , fPoints(kTestPoints)
241 , fPointCount(3)
242 , fWidth(2)
243 , fHeight(2)
244 , fText("Hello World")
245 , fPoints2(kTestPoints2)
246 , fBitmap(TestBitmap())
247 { }
248
249 SkRect fRect;
tfarina567ff2f2015-04-27 07:01:44 -0700250 SkMatrix fMatrix;
piotaixrf05f5a72014-10-03 13:26:55 -0700251 SkPath fPath;
252 SkPath fNearlyZeroLengthPath;
253 SkIRect fIRect;
254 SkRegion fRegion;
255 SkColor fColor;
256 SkPaint fPaint;
257 const SkPoint* fPoints;
258 size_t fPointCount;
259 int fWidth;
260 int fHeight;
261 SkString fText;
262 const SkPoint* fPoints2;
263 SkBitmap fBitmap;
264
265private:
266 static SkMatrix TestMatrix() {
267 SkMatrix matrix;
268 matrix.reset();
269 matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
270
271 return matrix;
272 }
273 static SkPath TestPath() {
274 SkPath path;
275 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
276 SkIntToScalar(2), SkIntToScalar(1)));
277 return path;
278 }
279 static SkPath TestNearlyZeroLengthPath() {
280 SkPath path;
281 SkPoint pt1 = { 0, 0 };
282 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
283 SkPoint pt3 = { SkIntToScalar(1), 0 };
284 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
285 path.moveTo(pt1);
286 path.lineTo(pt2);
287 path.lineTo(pt3);
288 path.lineTo(pt4);
289 return path;
290 }
291 static SkRegion TestRegion() {
292 SkRegion region;
293 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
294 region.setRect(rect);
295 return region;
296 }
297 static SkBitmap TestBitmap() {
298 SkBitmap bitmap;
299 createBitmap(&bitmap, 0x05060708);
300 return bitmap;
301 }
302};
303
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000304// Format strings that describe the test context. The %s token is where
305// the name of the test step is inserted. The context is required for
306// disambiguating the error in the case of failures that are reported in
307// functions that are called multiple times in different contexts (test
308// cases and test steps).
309static const char* const kDefaultAssertMessageFormat = "%s";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000310static const char* const kCanvasDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000311 "Drawing test step %s with SkCanvas";
edisonn@google.com77909122012-10-18 15:58:23 +0000312static const char* const kPdfAssertMessageFormat =
313 "PDF sanity check failed %s";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000314
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000315class CanvasTestStep;
316static SkTDArray<CanvasTestStep*>& testStepArray() {
317 static SkTDArray<CanvasTestStep*> theTests;
318 return theTests;
319}
320
321class CanvasTestStep {
322public:
edisonn@google.com77909122012-10-18 15:58:23 +0000323 CanvasTestStep(bool fEnablePdfTesting = true) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000324 *testStepArray().append() = this;
325 fAssertMessageFormat = kDefaultAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000326 this->fEnablePdfTesting = fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000327 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000328 virtual ~CanvasTestStep() { }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000329
piotaixrf05f5a72014-10-03 13:26:55 -0700330 virtual void draw(SkCanvas*, const TestData&, skiatest::Reporter*) = 0;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000331 virtual const char* name() const = 0;
332
333 const char* assertMessage() {
334 fAssertMessage.printf(fAssertMessageFormat, name());
335 return fAssertMessage.c_str();
336 }
337
338 void setAssertMessageFormat(const char* format) {
339 fAssertMessageFormat = format;
340 }
341
edisonn@google.com77909122012-10-18 15:58:23 +0000342 bool enablePdfTesting() { return fEnablePdfTesting; }
343
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000344private:
345 SkString fAssertMessage;
346 const char* fAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000347 bool fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000348};
349
350///////////////////////////////////////////////////////////////////////////////
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000351// Macros for defining test steps
352
353#define TEST_STEP(NAME, FUNCTION) \
354class NAME##_TestStep : public CanvasTestStep{ \
355public: \
piotaixrf05f5a72014-10-03 13:26:55 -0700356 virtual void draw(SkCanvas* canvas, const TestData& d, \
357 skiatest::Reporter* reporter) { \
358 FUNCTION (canvas, d, reporter, this); \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000359 } \
360 virtual const char* name() const {return #NAME ;} \
361}; \
362static NAME##_TestStep NAME##_TestStepInstance;
363
piotaixrf05f5a72014-10-03 13:26:55 -0700364#define TEST_STEP_NO_PDF(NAME, FUNCTION) \
edisonn@google.com77909122012-10-18 15:58:23 +0000365class NAME##_TestStep : public CanvasTestStep{ \
366public: \
367 NAME##_TestStep() : CanvasTestStep(false) {} \
piotaixrf05f5a72014-10-03 13:26:55 -0700368 virtual void draw(SkCanvas* canvas, const TestData& d, \
369 skiatest::Reporter* reporter) { \
370 FUNCTION (canvas, d, reporter, this); \
edisonn@google.com77909122012-10-18 15:58:23 +0000371 } \
372 virtual const char* name() const {return #NAME ;} \
373}; \
374static NAME##_TestStep NAME##_TestStepInstance;
375
piotaixrf05f5a72014-10-03 13:26:55 -0700376#define SIMPLE_TEST_STEP(NAME, CALL) \
377static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \
378 skiatest::Reporter*, CanvasTestStep*) { \
379 canvas-> CALL ; \
380} \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000381TEST_STEP(NAME, NAME##TestStep )
382
Brian Salomon1c80e992018-01-29 09:50:47 -0500383#define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
384 static void NAME##TestStep(SkCanvas* canvas, const TestData& d, skiatest::Reporter*, \
385 CanvasTestStep* testStep) { \
386 REPORTER_ASSERT(reporter, canvas->CALL, testStep->assertMessage()); \
387 } \
388 TEST_STEP(NAME, NAME##TestStep)
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000389
390///////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000391// Basic test steps for most virtual methods in SkCanvas that draw or affect
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000392// the state of the canvas.
393
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000394SIMPLE_TEST_STEP(Translate, translate(SkIntToScalar(1), SkIntToScalar(2)));
395SIMPLE_TEST_STEP(Scale, scale(SkIntToScalar(1), SkIntToScalar(2)));
396SIMPLE_TEST_STEP(Rotate, rotate(SkIntToScalar(1)));
397SIMPLE_TEST_STEP(Skew, skew(SkIntToScalar(1), SkIntToScalar(2)));
piotaixrf05f5a72014-10-03 13:26:55 -0700398SIMPLE_TEST_STEP(Concat, concat(d.fMatrix));
399SIMPLE_TEST_STEP(SetMatrix, setMatrix(d.fMatrix));
400SIMPLE_TEST_STEP(ClipRect, clipRect(d.fRect));
401SIMPLE_TEST_STEP(ClipPath, clipPath(d.fPath));
Mike Reedc1f77742016-12-09 09:00:50 -0500402SIMPLE_TEST_STEP(ClipRegion, clipRegion(d.fRegion, kReplace_SkClipOp));
piotaixrf05f5a72014-10-03 13:26:55 -0700403SIMPLE_TEST_STEP(Clear, clear(d.fColor));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000404
405///////////////////////////////////////////////////////////////////////////////
406// Complex test steps
407
piotaixrf05f5a72014-10-03 13:26:55 -0700408static void SaveMatrixClipStep(SkCanvas* canvas, const TestData& d,
409 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000410 int saveCount = canvas->getSaveCount();
Florin Malita5f6102d2014-06-30 10:13:28 -0400411 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000412 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
piotaixrf05f5a72014-10-03 13:26:55 -0700413 canvas->clipRegion(d.fRegion);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000414 canvas->restore();
Brian Salomon1c80e992018-01-29 09:50:47 -0500415 REPORTER_ASSERT(reporter, canvas->getSaveCount() == saveCount, testStep->assertMessage());
416 REPORTER_ASSERT(reporter, canvas->getTotalMatrix().isIdentity(), testStep->assertMessage());
417 // REPORTER_ASSERT(reporter, canvas->getTotalClip() != kTestRegion,
418 // testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000419}
420TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
421
piotaixrf05f5a72014-10-03 13:26:55 -0700422static void SaveLayerStep(SkCanvas* canvas, const TestData& d,
423 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000424 int saveCount = canvas->getSaveCount();
halcanary96fcdcc2015-08-27 07:41:13 -0700425 canvas->saveLayer(nullptr, nullptr);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000426 canvas->restore();
Brian Salomon1c80e992018-01-29 09:50:47 -0500427 REPORTER_ASSERT(reporter, canvas->getSaveCount() == saveCount, testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000428}
429TEST_STEP(SaveLayer, SaveLayerStep);
430
piotaixrf05f5a72014-10-03 13:26:55 -0700431static void BoundedSaveLayerStep(SkCanvas* canvas, const TestData& d,
432 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000433 int saveCount = canvas->getSaveCount();
halcanary96fcdcc2015-08-27 07:41:13 -0700434 canvas->saveLayer(&d.fRect, nullptr);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000435 canvas->restore();
Brian Salomon1c80e992018-01-29 09:50:47 -0500436 REPORTER_ASSERT(reporter, canvas->getSaveCount() == saveCount, testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000437}
438TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
439
piotaixrf05f5a72014-10-03 13:26:55 -0700440static void PaintSaveLayerStep(SkCanvas* canvas, const TestData& d,
441 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000442 int saveCount = canvas->getSaveCount();
halcanary96fcdcc2015-08-27 07:41:13 -0700443 canvas->saveLayer(nullptr, &d.fPaint);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000444 canvas->restore();
Brian Salomon1c80e992018-01-29 09:50:47 -0500445 REPORTER_ASSERT(reporter, canvas->getSaveCount() == saveCount, testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000446}
447TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
448
piotaixrf05f5a72014-10-03 13:26:55 -0700449static void TwoClipOpsStep(SkCanvas* canvas, const TestData& d,
450 skiatest::Reporter*, CanvasTestStep*) {
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000451 // This test exercises a functionality in SkPicture that leads to the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000452 // recording of restore offset placeholders. This test will trigger an
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000453 // assertion at playback time if the placeholders are not properly
454 // filled when the recording ends.
piotaixrf05f5a72014-10-03 13:26:55 -0700455 canvas->clipRect(d.fRect);
456 canvas->clipRegion(d.fRegion);
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000457}
458TEST_STEP(TwoClipOps, TwoClipOpsStep);
459
epoger@google.com94fa43c2012-04-11 17:51:01 +0000460// exercise fix for http://code.google.com/p/skia/issues/detail?id=560
461// ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
piotaixrf05f5a72014-10-03 13:26:55 -0700462static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas, const TestData& d,
463 skiatest::Reporter*, CanvasTestStep*) {
epoger@google.com94fa43c2012-04-11 17:51:01 +0000464 SkPaint paint;
465 paint.setStrokeWidth(SkIntToScalar(1));
466 paint.setStyle(SkPaint::kStroke_Style);
467
piotaixrf05f5a72014-10-03 13:26:55 -0700468 canvas->drawPath(d.fNearlyZeroLengthPath, paint);
epoger@google.com94fa43c2012-04-11 17:51:01 +0000469}
470TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
471
piotaixrf05f5a72014-10-03 13:26:55 -0700472static void DrawVerticesShaderTestStep(SkCanvas* canvas, const TestData& d,
473 skiatest::Reporter*, CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000474 SkPoint pts[4];
475 pts[0].set(0, 0);
piotaixrf05f5a72014-10-03 13:26:55 -0700476 pts[1].set(SkIntToScalar(d.fWidth), 0);
477 pts[2].set(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight));
478 pts[3].set(0, SkIntToScalar(d.fHeight));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000479 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -0700480 paint.setShader(SkShader::MakeBitmapShader(d.fBitmap, SkShader::kClamp_TileMode,
481 SkShader::kClamp_TileMode));
Mike Reed887cdf12017-04-03 11:11:09 -0400482 canvas->drawVertices(SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, 4, pts, pts,
483 nullptr),
484 SkBlendMode::kModulate, paint);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000485}
edisonn@google.com77909122012-10-18 15:58:23 +0000486// NYI: issue 240.
487TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000488
piotaixrf05f5a72014-10-03 13:26:55 -0700489static void DrawPictureTestStep(SkCanvas* canvas, const TestData& d,
490 skiatest::Reporter*, CanvasTestStep*) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000491 SkPictureRecorder recorder;
piotaixrf05f5a72014-10-03 13:26:55 -0700492 SkCanvas* testCanvas = recorder.beginRecording(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight),
halcanary96fcdcc2015-08-27 07:41:13 -0700493 nullptr, 0);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000494 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
piotaixrf05f5a72014-10-03 13:26:55 -0700495 testCanvas->clipRect(d.fRect);
496 testCanvas->drawRect(d.fRect, d.fPaint);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000497
reedca2622b2016-03-18 07:25:55 -0700498 canvas->drawPicture(recorder.finishRecordingAsPicture());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000499}
500TEST_STEP(DrawPicture, DrawPictureTestStep);
501
piotaixrf05f5a72014-10-03 13:26:55 -0700502static void SaveRestoreTestStep(SkCanvas* canvas, const TestData& d,
503 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000504 int baseSaveCount = canvas->getSaveCount();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000505 int n = canvas->save();
Brian Salomon1c80e992018-01-29 09:50:47 -0500506 REPORTER_ASSERT(reporter, baseSaveCount == n, testStep->assertMessage());
507 REPORTER_ASSERT(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
508 testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000509 canvas->save();
510 canvas->save();
Brian Salomon1c80e992018-01-29 09:50:47 -0500511 REPORTER_ASSERT(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
512 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000513 canvas->restoreToCount(baseSaveCount + 1);
Brian Salomon1c80e992018-01-29 09:50:47 -0500514 REPORTER_ASSERT(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
515 testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000516
517 // should this pin to 1, or be a no-op, or crash?
518 canvas->restoreToCount(0);
Brian Salomon1c80e992018-01-29 09:50:47 -0500519 REPORTER_ASSERT(reporter, 1 == canvas->getSaveCount(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000520}
521TEST_STEP(SaveRestore, SaveRestoreTestStep);
522
piotaixrf05f5a72014-10-03 13:26:55 -0700523static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas, const TestData& d,
524 skiatest::Reporter*, CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000525 // This test step challenges the TestDeferredCanvasStateConsistency
526 // test cases because the opaque paint can trigger an optimization
527 // that discards previously recorded commands. The challenge is to maintain
528 // correct clip and matrix stack state.
529 canvas->resetMatrix();
530 canvas->rotate(SkIntToScalar(30));
531 canvas->save();
532 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
533 canvas->save();
534 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
535 SkPaint paint;
536 paint.setColor(0xFFFFFFFF);
537 canvas->drawPaint(paint);
538 canvas->restore();
539 canvas->restore();
540}
541TEST_STEP(NestedSaveRestoreWithSolidPaint, \
542 NestedSaveRestoreWithSolidPaintTestStep);
543
piotaixrf05f5a72014-10-03 13:26:55 -0700544static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas, const TestData& d,
545 skiatest::Reporter*, CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000546 // This test step challenges the TestDeferredCanvasStateConsistency
547 // test case because the canvas flush on a deferred canvas will
548 // reset the recording session. The challenge is to maintain correct
549 // clip and matrix stack state on the playback canvas.
550 canvas->resetMatrix();
551 canvas->rotate(SkIntToScalar(30));
552 canvas->save();
553 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
554 canvas->save();
555 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
piotaixrf05f5a72014-10-03 13:26:55 -0700556 canvas->drawRect(d.fRect,d.fPaint);
reed@google.com3b3e8952012-08-16 20:53:31 +0000557 canvas->flush();
558 canvas->restore();
559 canvas->restore();
560}
piotaixrf05f5a72014-10-03 13:26:55 -0700561TEST_STEP(NestedSaveRestoreWithFlush, NestedSaveRestoreWithFlushTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000562
reed3aafe112016-08-18 12:45:34 -0700563static void TestPdfDevice(skiatest::Reporter* reporter, const TestData& d, CanvasTestStep* step) {
halcanary3d32d502015-03-01 06:55:20 -0800564 SkDynamicMemoryWStream outStream;
Hal Canary23564b92018-09-07 14:33:14 -0400565 sk_sp<SkDocument> doc(SkPDF::MakeDocument(&outStream));
halcanary2ccdb632015-08-11 13:35:12 -0700566 if (!doc) {
Hal Canary4125b612018-03-20 14:17:00 -0400567 INFOF(reporter, "PDF disabled; TestPdfDevice test skipped.");
halcanary2ccdb632015-08-11 13:35:12 -0700568 return;
569 }
halcanary3d32d502015-03-01 06:55:20 -0800570 SkCanvas* canvas = doc->beginPage(SkIntToScalar(d.fWidth),
571 SkIntToScalar(d.fHeight));
572 REPORTER_ASSERT(reporter, canvas);
reed3aafe112016-08-18 12:45:34 -0700573 step->setAssertMessageFormat(kPdfAssertMessageFormat);
574 step->draw(canvas, d, reporter);
edisonn@google.com77909122012-10-18 15:58:23 +0000575}
576
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000577/*
578 * This sub-test verifies that the test step passes when executed
579 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
580 * that the all canvas derivatives report the same state as an SkCanvas
581 * after having executed the test step.
582 */
piotaixrf05f5a72014-10-03 13:26:55 -0700583static void TestOverrideStateConsistency(skiatest::Reporter* reporter, const TestData& d,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000584 CanvasTestStep* testStep) {
585 SkBitmap referenceStore;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000586 createBitmap(&referenceStore, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700587 SkCanvas referenceCanvas(referenceStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000588 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700589 testStep->draw(&referenceCanvas, d, reporter);
reed@google.com7c202932011-12-14 18:48:05 +0000590}
reed@google.com37f3ae02011-11-28 16:06:04 +0000591
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000592static void test_newraster(skiatest::Reporter* reporter) {
593 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
reed3054be12014-12-10 07:24:28 -0800594 const size_t minRowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400595 const size_t size = info.computeByteSize(minRowBytes);
scroggo565901d2015-12-10 10:44:13 -0800596 SkAutoTMalloc<SkPMColor> storage(size);
597 SkPMColor* baseAddr = storage.get();
reed3054be12014-12-10 07:24:28 -0800598 sk_bzero(baseAddr, size);
599
Mike Reed5df49342016-11-12 08:06:55 -0600600 std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000601 REPORTER_ASSERT(reporter, canvas);
602
reed6ceeebd2016-03-09 14:26:26 -0800603 SkPixmap pmap;
604 const SkPMColor* addr = canvas->peekPixels(&pmap) ? pmap.addr32() : nullptr;
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000605 REPORTER_ASSERT(reporter, addr);
reed6ceeebd2016-03-09 14:26:26 -0800606 REPORTER_ASSERT(reporter, info == pmap.info());
607 REPORTER_ASSERT(reporter, minRowBytes == pmap.rowBytes());
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000608 for (int y = 0; y < info.height(); ++y) {
609 for (int x = 0; x < info.width(); ++x) {
610 REPORTER_ASSERT(reporter, 0 == addr[x]);
611 }
reed6ceeebd2016-03-09 14:26:26 -0800612 addr = (const SkPMColor*)((const char*)addr + pmap.rowBytes());
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000613 }
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000614
615 // now try a deliberately bad info
reede5ea5002014-09-03 11:54:58 -0700616 info = info.makeWH(-1, info.height());
Mike Reed5df49342016-11-12 08:06:55 -0600617 REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes));
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000618
619 // too big
reede5ea5002014-09-03 11:54:58 -0700620 info = info.makeWH(1 << 30, 1 << 30);
Mike Reed5df49342016-11-12 08:06:55 -0600621 REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes));
skia.committer@gmail.com0e530752014-02-28 03:02:05 +0000622
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000623 // not a valid pixel type
reede5ea5002014-09-03 11:54:58 -0700624 info = SkImageInfo::Make(10, 10, kUnknown_SkColorType, info.alphaType());
Mike Reed5df49342016-11-12 08:06:55 -0600625 REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes));
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000626
Brian Osman431ac562018-10-10 13:20:38 -0400627 // We should not succeed with a zero-sized valid info
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000628 info = SkImageInfo::MakeN32Premul(0, 0);
Mike Reed5df49342016-11-12 08:06:55 -0600629 canvas = SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes);
Brian Osman431ac562018-10-10 13:20:38 -0400630 REPORTER_ASSERT(reporter, nullptr == canvas);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000631}
632
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000633DEF_TEST(Canvas, reporter) {
piotaixrf05f5a72014-10-03 13:26:55 -0700634 TestData d;
reed@google.com37f3ae02011-11-28 16:06:04 +0000635
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000636 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
piotaixrf05f5a72014-10-03 13:26:55 -0700637 TestOverrideStateConsistency(reporter, d, testStepArray()[testStep]);
edisonn@google.com77909122012-10-18 15:58:23 +0000638 if (testStepArray()[testStep]->enablePdfTesting()) {
piotaixrf05f5a72014-10-03 13:26:55 -0700639 TestPdfDevice(reporter, d, testStepArray()[testStep]);
edisonn@google.com77909122012-10-18 15:58:23 +0000640 }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000641 }
junov@chromium.orgcd62ecf2012-08-02 17:43:25 +0000642
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000643 test_newraster(reporter);
reed@google.com37f3ae02011-11-28 16:06:04 +0000644}
reedf0090cb2014-11-26 08:55:51 -0800645
646DEF_TEST(Canvas_SaveState, reporter) {
647 SkCanvas canvas(10, 10);
648 REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
649
650 int n = canvas.save();
651 REPORTER_ASSERT(reporter, 1 == n);
652 REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
653
halcanary96fcdcc2015-08-27 07:41:13 -0700654 n = canvas.saveLayer(nullptr, nullptr);
reedf0090cb2014-11-26 08:55:51 -0800655 REPORTER_ASSERT(reporter, 2 == n);
656 REPORTER_ASSERT(reporter, 3 == canvas.getSaveCount());
halcanary9d524f22016-03-29 09:03:52 -0700657
reedf0090cb2014-11-26 08:55:51 -0800658 canvas.restore();
659 REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
660 canvas.restore();
661 REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
662}
reedc1b11f12015-03-13 08:48:26 -0700663
664DEF_TEST(Canvas_ClipEmptyPath, reporter) {
665 SkCanvas canvas(10, 10);
666 canvas.save();
667 SkPath path;
668 canvas.clipPath(path);
669 canvas.restore();
670 canvas.save();
671 path.moveTo(5, 5);
672 canvas.clipPath(path);
673 canvas.restore();
674 canvas.save();
675 path.moveTo(7, 7);
676 canvas.clipPath(path); // should not assert here
677 canvas.restore();
678}
fmalitaf433bb22015-08-17 08:05:13 -0700679
680namespace {
681
682class MockFilterCanvas : public SkPaintFilterCanvas {
683public:
684 MockFilterCanvas(SkCanvas* canvas) : INHERITED(canvas) { }
685
686protected:
fmalita32cdc322016-01-12 07:21:11 -0800687 bool onFilter(SkTCopyOnFirstWrite<SkPaint>*, Type) const override { return true; }
fmalitaf433bb22015-08-17 08:05:13 -0700688
689private:
690 typedef SkPaintFilterCanvas INHERITED;
691};
692
693} // anonymous namespace
694
695// SkPaintFilterCanvas should inherit the initial target canvas state.
696DEF_TEST(PaintFilterCanvas_ConsistentState, reporter) {
697 SkCanvas canvas(100, 100);
698 canvas.clipRect(SkRect::MakeXYWH(12.7f, 12.7f, 75, 75));
699 canvas.scale(0.5f, 0.75f);
700
fmalitaf433bb22015-08-17 08:05:13 -0700701 MockFilterCanvas filterCanvas(&canvas);
702 REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix());
Mike Reed918e1442017-01-23 11:39:45 -0500703 REPORTER_ASSERT(reporter, canvas.getLocalClipBounds() == filterCanvas.getLocalClipBounds());
fmalitaf433bb22015-08-17 08:05:13 -0700704
705 filterCanvas.clipRect(SkRect::MakeXYWH(30.5f, 30.7f, 100, 100));
706 filterCanvas.scale(0.75f, 0.5f);
707 REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix());
Mike Reed918e1442017-01-23 11:39:45 -0500708 REPORTER_ASSERT(reporter, filterCanvas.getLocalClipBounds().contains(canvas.getLocalClipBounds()));
fmalitaf433bb22015-08-17 08:05:13 -0700709}
reedbabc3de2016-07-08 08:43:27 -0700710
711///////////////////////////////////////////////////////////////////////////////////////////////////
712
Mike Reed584ca892016-11-15 11:52:55 -0500713// Subclass that takes a bool*, which it updates in its construct (true) and destructor (false)
714// to allow the caller to know how long the object is alive.
715class LifeLineCanvas : public SkCanvas {
716 bool* fLifeLine;
717public:
718 LifeLineCanvas(int w, int h, bool* lifeline) : SkCanvas(w, h), fLifeLine(lifeline) {
719 *fLifeLine = true;
720 }
721 ~LifeLineCanvas() {
722 *fLifeLine = false;
723 }
724};
725
726// Check that NWayCanvas does NOT try to manage the lifetime of its sub-canvases
727DEF_TEST(NWayCanvas, r) {
728 const int w = 10;
729 const int h = 10;
730 bool life[2];
731 {
732 LifeLineCanvas c0(w, h, &life[0]);
733 REPORTER_ASSERT(r, life[0]);
734 }
735 REPORTER_ASSERT(r, !life[0]);
736
737
738 std::unique_ptr<SkCanvas> c0 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[0]));
739 std::unique_ptr<SkCanvas> c1 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[1]));
740 REPORTER_ASSERT(r, life[0]);
741 REPORTER_ASSERT(r, life[1]);
742
743 {
744 SkNWayCanvas nway(w, h);
745 nway.addCanvas(c0.get());
746 nway.addCanvas(c1.get());
747 REPORTER_ASSERT(r, life[0]);
748 REPORTER_ASSERT(r, life[1]);
749 }
750 // Now assert that the death of the nway has NOT also killed the sub-canvases
751 REPORTER_ASSERT(r, life[0]);
752 REPORTER_ASSERT(r, life[1]);
753}
754
755// Check that CanvasStack DOES manage the lifetime of its sub-canvases
756DEF_TEST(CanvasStack, r) {
757 const int w = 10;
758 const int h = 10;
759 bool life[2];
760 std::unique_ptr<SkCanvas> c0 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[0]));
761 std::unique_ptr<SkCanvas> c1 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[1]));
762 REPORTER_ASSERT(r, life[0]);
763 REPORTER_ASSERT(r, life[1]);
764
765 {
766 SkCanvasStack stack(w, h);
767 stack.pushCanvas(std::move(c0), {0,0});
768 stack.pushCanvas(std::move(c1), {0,0});
769 REPORTER_ASSERT(r, life[0]);
770 REPORTER_ASSERT(r, life[1]);
771 }
772 // Now assert that the death of the canvasstack has also killed the sub-canvases
773 REPORTER_ASSERT(r, !life[0]);
774 REPORTER_ASSERT(r, !life[1]);
775}
Mike Reedcd667b62017-03-02 15:21:11 -0500776
Mike Reed3b4c22d2017-03-02 20:07:46 -0500777static void test_cliptype(SkCanvas* canvas, skiatest::Reporter* r) {
Mike Reedcd667b62017-03-02 15:21:11 -0500778 REPORTER_ASSERT(r, !canvas->isClipEmpty());
779 REPORTER_ASSERT(r, canvas->isClipRect());
780
781 canvas->save();
782 canvas->clipRect({0, 0, 0, 0});
783 REPORTER_ASSERT(r, canvas->isClipEmpty());
784 REPORTER_ASSERT(r, !canvas->isClipRect());
785 canvas->restore();
786
787 canvas->save();
788 canvas->clipRect({2, 2, 6, 6});
789 REPORTER_ASSERT(r, !canvas->isClipEmpty());
790 REPORTER_ASSERT(r, canvas->isClipRect());
791 canvas->restore();
792
793 canvas->save();
794 canvas->clipRect({2, 2, 6, 6}, SkClipOp::kDifference); // punch a hole in the clip
795 REPORTER_ASSERT(r, !canvas->isClipEmpty());
796 REPORTER_ASSERT(r, !canvas->isClipRect());
797 canvas->restore();
798
799 REPORTER_ASSERT(r, !canvas->isClipEmpty());
800 REPORTER_ASSERT(r, canvas->isClipRect());
801}
Mike Reed3b4c22d2017-03-02 20:07:46 -0500802
803DEF_TEST(CanvasClipType, r) {
804 // test rasterclip backend
805 test_cliptype(SkSurface::MakeRasterN32Premul(10, 10)->getCanvas(), r);
806
807 // test clipstack backend
808 SkDynamicMemoryWStream stream;
Hal Canary23564b92018-09-07 14:33:14 -0400809 if (auto doc = SkPDF::MakeDocument(&stream)) {
Hal Canary4125b612018-03-20 14:17:00 -0400810 test_cliptype(doc->beginPage(100, 100), r);
811 }
Mike Reed3b4c22d2017-03-02 20:07:46 -0500812}
Matt Sarett31f99ce2017-04-11 08:46:01 -0400813
814#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
815DEF_TEST(Canvas_LegacyColorBehavior, r) {
816 sk_sp<SkColorSpace> cs = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
817 SkColorSpace::kAdobeRGB_Gamut);
818
819 // Make a Adobe RGB bitmap.
820 SkBitmap bitmap;
821 bitmap.allocPixels(SkImageInfo::MakeN32(1, 1, kOpaque_SkAlphaType, cs));
822 bitmap.eraseColor(0xFF000000);
823
824 // Wrap it in a legacy canvas. Test that the canvas behaves like a legacy canvas.
825 SkCanvas canvas(bitmap, SkCanvas::ColorBehavior::kLegacy);
826 REPORTER_ASSERT(r, !canvas.imageInfo().colorSpace());
827 SkPaint p;
828 p.setColor(SK_ColorRED);
829 canvas.drawIRect(SkIRect::MakeWH(1, 1), p);
830 REPORTER_ASSERT(r, SK_ColorRED == SkSwizzle_BGRA_to_PMColor(*bitmap.getAddr32(0, 0)));
831}
832#endif
Xianzhu Wang3b428cb2018-01-22 09:51:13 -0800833
834namespace {
835
836class ZeroBoundsImageFilter : public SkImageFilter {
837public:
838 static sk_sp<SkImageFilter> Make() { return sk_sp<SkImageFilter>(new ZeroBoundsImageFilter); }
839
Xianzhu Wang3b428cb2018-01-22 09:51:13 -0800840protected:
841 sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage*, const Context&, SkIPoint*) const override {
842 return nullptr;
843 }
844 sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override { return nullptr; }
Robert Phillips12078432018-05-17 11:17:39 -0400845 SkIRect onFilterNodeBounds(const SkIRect&, const SkMatrix&,
846 MapDirection, const SkIRect* inputRect) const override {
Xianzhu Wang3b428cb2018-01-22 09:51:13 -0800847 return SkIRect::MakeEmpty();
848 }
849
850private:
Mike Klein4fee3232018-10-18 17:27:16 -0400851 SK_FLATTENABLE_HOOKS(ZeroBoundsImageFilter)
852
Xianzhu Wang3b428cb2018-01-22 09:51:13 -0800853 ZeroBoundsImageFilter() : INHERITED(nullptr, 0, nullptr) {}
854
855 typedef SkImageFilter INHERITED;
856};
857
858sk_sp<SkFlattenable> ZeroBoundsImageFilter::CreateProc(SkReadBuffer& buffer) {
859 SkDEBUGFAIL("Should never get here");
860 return nullptr;
861}
862
Xianzhu Wang3b428cb2018-01-22 09:51:13 -0800863} // anonymous namespace
864
865DEF_TEST(Canvas_SaveLayerWithNullBoundsAndZeroBoundsImageFilter, r) {
866 SkCanvas canvas(10, 10);
867 SkPaint p;
868 p.setImageFilter(ZeroBoundsImageFilter::Make());
869 // This should not fail any assert.
870 canvas.saveLayer(nullptr, &p);
871 REPORTER_ASSERT(r, canvas.getDeviceClipBounds().isEmpty());
872 canvas.restore();
873}
Mike Reed490aa592018-04-13 15:34:16 -0400874
875#include "SkPaintImageFilter.h"
876
877// Test that we don't crash/assert when building a canvas with degenerate coordintes
878// (esp. big ones, that might invoke tiling).
879DEF_TEST(Canvas_degenerate_dimension, reporter) {
880 // Need a paint that will sneak us past the quickReject in SkCanvas, so we can test the
881 // raster code further downstream.
882 SkPaint paint;
883 paint.setImageFilter(SkPaintImageFilter::Make(SkPaint(), nullptr));
884 REPORTER_ASSERT(reporter, !paint.canComputeFastBounds());
885
886 const int big = 100 * 1024; // big enough to definitely trigger tiling
887 const SkISize sizes[] {SkISize{0, big}, {big, 0}, {0, 0}};
888 for (SkISize size : sizes) {
889 SkBitmap bm;
890 bm.setInfo(SkImageInfo::MakeN32Premul(size.width(), size.height()));
891 SkCanvas canvas(bm);
892 canvas.drawRect({0, 0, 100, 90*1024}, paint);
893 }
894}
895
Robert Phillips3d0e8502018-04-20 10:27:27 -0400896#include "SkBlurImageFilter.h"
897
898DEF_TEST(Canvas_ClippedOutImageFilter, reporter) {
899 SkCanvas canvas(100, 100);
900
901 SkPaint p;
902 p.setColor(SK_ColorGREEN);
903 p.setImageFilter(SkBlurImageFilter::Make(3.0f, 3.0f, nullptr, nullptr));
904
905 SkRect blurredRect = SkRect::MakeXYWH(60, 10, 30, 30);
906
907 SkMatrix invM;
908 invM.setRotate(-45);
909 invM.mapRect(&blurredRect);
910
911 const SkRect clipRect = SkRect::MakeXYWH(0, 50, 50, 50);
912
913 canvas.clipRect(clipRect);
914
915 canvas.rotate(45);
916 const SkMatrix preCTM = canvas.getTotalMatrix();
917 canvas.drawRect(blurredRect, p);
918 const SkMatrix postCTM = canvas.getTotalMatrix();
919 REPORTER_ASSERT(reporter, preCTM == postCTM);
920}
921