blob: 1a1c20aadc39b897694b72635b325a72c343cff8 [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"
halcanary3d32d502015-03-01 06:55:20 -080055#include "SkDocument.h"
Cary Clark4dc5a452018-05-21 11:56:57 -040056#include "SkFlattenablePriv.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040057#include "SkImageFilter.h"
58#include "SkImageInfo.h"
59#include "SkMalloc.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000060#include "SkMatrix.h"
61#include "SkNWayCanvas.h"
62#include "SkPaint.h"
fmalitaf433bb22015-08-17 08:05:13 -070063#include "SkPaintFilterCanvas.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000064#include "SkPath.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000065#include "SkPictureRecorder.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040066#include "SkPixmap.h"
67#include "SkPoint.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000068#include "SkRect.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040069#include "SkRefCnt.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000070#include "SkRegion.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040071#include "SkScalar.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000072#include "SkShader.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040073#include "SkSize.h"
Xianzhu Wang3b428cb2018-01-22 09:51:13 -080074#include "SkSpecialImage.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000075#include "SkStream.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040076#include "SkString.h"
reed@google.com28183b42014-02-04 15:34:10 +000077#include "SkSurface.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000078#include "SkTDArray.h"
Xianzhu Wang3b428cb2018-01-22 09:51:13 -080079#include "SkTemplates.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040080#include "SkTypes.h"
Mike Reed887cdf12017-04-03 11:11:09 -040081#include "SkVertices.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000082#include "Test.h"
reed@google.com37f3ae02011-11-28 16:06:04 +000083
Ben Wagnerb607a8f2018-03-12 13:46:21 -040084#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
85#include "SkColorData.h"
86#include "SkColorSpace.h"
87#endif
88
89#include <memory>
90#include <utility>
91
92class SkColorSpaceXformer;
93class SkReadBuffer;
94template <typename T> class SkTCopyOnFirstWrite;
95
Mike Reed918e1442017-01-23 11:39:45 -050096DEF_TEST(canvas_clipbounds, reporter) {
97 SkCanvas canvas(10, 10);
Mike Reed6f4a9b22017-01-24 09:13:40 -050098 SkIRect irect, irect2;
99 SkRect rect, rect2;
Mike Reed918e1442017-01-23 11:39:45 -0500100
101 irect = canvas.getDeviceClipBounds();
102 REPORTER_ASSERT(reporter, irect == SkIRect::MakeWH(10, 10));
Mike Reed6f4a9b22017-01-24 09:13:40 -0500103 REPORTER_ASSERT(reporter, canvas.getDeviceClipBounds(&irect2));
104 REPORTER_ASSERT(reporter, irect == irect2);
105
Mike Reed918e1442017-01-23 11:39:45 -0500106 // local bounds are always too big today -- can we trim them?
107 rect = canvas.getLocalClipBounds();
108 REPORTER_ASSERT(reporter, rect.contains(SkRect::MakeWH(10, 10)));
Mike Reed6f4a9b22017-01-24 09:13:40 -0500109 REPORTER_ASSERT(reporter, canvas.getLocalClipBounds(&rect2));
110 REPORTER_ASSERT(reporter, rect == rect2);
Mike Reed918e1442017-01-23 11:39:45 -0500111
112 canvas.clipRect(SkRect::MakeEmpty());
113
114 irect = canvas.getDeviceClipBounds();
115 REPORTER_ASSERT(reporter, irect == SkIRect::MakeEmpty());
Mike Reed6f4a9b22017-01-24 09:13:40 -0500116 REPORTER_ASSERT(reporter, !canvas.getDeviceClipBounds(&irect2));
117 REPORTER_ASSERT(reporter, irect == irect2);
118
Mike Reed918e1442017-01-23 11:39:45 -0500119 rect = canvas.getLocalClipBounds();
120 REPORTER_ASSERT(reporter, rect == SkRect::MakeEmpty());
Mike Reed6f4a9b22017-01-24 09:13:40 -0500121 REPORTER_ASSERT(reporter, !canvas.getLocalClipBounds(&rect2));
122 REPORTER_ASSERT(reporter, rect == rect2);
Mike Reed566e53c2017-03-10 10:49:45 -0500123
124 // Test for wacky sizes that we (historically) have guarded against
125 {
126 SkCanvas c(-10, -20);
127 REPORTER_ASSERT(reporter, c.getBaseLayerSize() == SkISize::MakeEmpty());
Mike Reeddc9f0db2017-03-10 13:54:16 -0500128
129 SkPictureRecorder().beginRecording({ 5, 5, 4, 4 });
Mike Reed566e53c2017-03-10 10:49:45 -0500130 }
Mike Reed918e1442017-01-23 11:39:45 -0500131}
132
Robert Phillips09dfc472017-09-13 15:25:47 -0400133// Will call proc with multiple styles of canvas (recording, raster, pdf)
Mike Reed02be3c12017-03-23 12:34:15 -0400134template <typename F> static void multi_canvas_driver(int w, int h, F proc) {
135 proc(SkPictureRecorder().beginRecording(SkRect::MakeIWH(w, h)));
136
137 SkNullWStream stream;
Hal Canary4125b612018-03-20 14:17:00 -0400138 if (auto doc = SkDocument::MakePDF(&stream)) {
139 proc(doc->beginPage(SkIntToScalar(w), SkIntToScalar(h)));
140 }
Mike Reed02be3c12017-03-23 12:34:15 -0400141
142 proc(SkSurface::MakeRasterN32Premul(w, h, nullptr)->getCanvas());
143}
144
Mike Reed2a65cc02017-03-22 10:01:53 -0400145const SkIRect gBaseRestrictedR = { 0, 0, 10, 10 };
146
147static void test_restriction(skiatest::Reporter* reporter, SkCanvas* canvas) {
148 REPORTER_ASSERT(reporter, canvas->getDeviceClipBounds() == gBaseRestrictedR);
149
150 const SkIRect restrictionR = { 2, 2, 8, 8 };
151 canvas->androidFramework_setDeviceClipRestriction(restrictionR);
152 REPORTER_ASSERT(reporter, canvas->getDeviceClipBounds() == restrictionR);
153
154 const SkIRect clipR = { 4, 4, 6, 6 };
155 canvas->clipRect(SkRect::Make(clipR), SkClipOp::kIntersect);
156 REPORTER_ASSERT(reporter, canvas->getDeviceClipBounds() == clipR);
157
Mike Reed14113bc2017-05-10 14:13:20 -0400158#ifdef SK_SUPPORT_DEPRECATED_CLIPOPS
Mike Reed2a65cc02017-03-22 10:01:53 -0400159 // now test that expanding clipops can't exceed the restriction
160 const SkClipOp expanders[] = {
161 SkClipOp::kUnion_deprecated,
162 SkClipOp::kXOR_deprecated,
163 SkClipOp::kReverseDifference_deprecated,
164 SkClipOp::kReplace_deprecated,
165 };
166
167 const SkRect expandR = { 0, 0, 5, 9 };
168 SkASSERT(!SkRect::Make(restrictionR).contains(expandR));
169
170 for (SkClipOp op : expanders) {
171 canvas->save();
172 canvas->clipRect(expandR, op);
173 REPORTER_ASSERT(reporter, gBaseRestrictedR.contains(canvas->getDeviceClipBounds()));
174 canvas->restore();
175 }
Mike Reed14113bc2017-05-10 14:13:20 -0400176#endif
Mike Reed2a65cc02017-03-22 10:01:53 -0400177}
178
179/**
180 * Clip restriction logic exists in the canvas itself, and in various kinds of devices.
181 *
182 * This test explicitly tries to exercise that variety:
183 * - picture : empty device but exercises canvas itself
184 * - pdf : uses SkClipStack in its device (as does SVG and GPU)
185 * - raster : uses SkRasterClip in its device
186 */
187DEF_TEST(canvas_clip_restriction, reporter) {
Mike Reed02be3c12017-03-23 12:34:15 -0400188 multi_canvas_driver(gBaseRestrictedR.width(), gBaseRestrictedR.height(),
189 [reporter](SkCanvas* canvas) { test_restriction(reporter, canvas); });
190}
Mike Reed2a65cc02017-03-22 10:01:53 -0400191
Mike Reed02be3c12017-03-23 12:34:15 -0400192DEF_TEST(canvas_empty_clip, reporter) {
193 multi_canvas_driver(50, 50, [reporter](SkCanvas* canvas) {
194 canvas->save();
195 canvas->clipRect({0, 0, 20, 40 });
196 REPORTER_ASSERT(reporter, !canvas->isClipEmpty());
197 canvas->clipRect({30, 0, 50, 40 });
198 REPORTER_ASSERT(reporter, canvas->isClipEmpty());
199 });
Mike Reed2a65cc02017-03-22 10:01:53 -0400200}
201
piotaixrf05f5a72014-10-03 13:26:55 -0700202static const int kWidth = 2, kHeight = 2;
203
204static void createBitmap(SkBitmap* bm, SkColor color) {
205 bm->allocN32Pixels(kWidth, kHeight);
206 bm->eraseColor(color);
207}
208
piotaixrf05f5a72014-10-03 13:26:55 -0700209///////////////////////////////////////////////////////////////////////////////
210// Constants used by test steps
211const SkPoint kTestPoints[] = {
212 {SkIntToScalar(0), SkIntToScalar(0)},
213 {SkIntToScalar(2), SkIntToScalar(1)},
214 {SkIntToScalar(0), SkIntToScalar(2)}
215};
216const SkPoint kTestPoints2[] = {
217 { SkIntToScalar(0), SkIntToScalar(1) },
218 { SkIntToScalar(1), SkIntToScalar(1) },
219 { SkIntToScalar(2), SkIntToScalar(1) },
220 { SkIntToScalar(3), SkIntToScalar(1) },
221 { SkIntToScalar(4), SkIntToScalar(1) },
222 { SkIntToScalar(5), SkIntToScalar(1) },
223 { SkIntToScalar(6), SkIntToScalar(1) },
224 { SkIntToScalar(7), SkIntToScalar(1) },
225 { SkIntToScalar(8), SkIntToScalar(1) },
226 { SkIntToScalar(9), SkIntToScalar(1) },
227 { SkIntToScalar(10), SkIntToScalar(1) }
228};
229
230struct TestData {
231public:
232 TestData()
233 : fRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
234 SkIntToScalar(2), SkIntToScalar(1)))
235 , fMatrix(TestMatrix())
236 , fPath(TestPath())
237 , fNearlyZeroLengthPath(TestNearlyZeroLengthPath())
238 , fIRect(SkIRect::MakeXYWH(0, 0, 2, 1))
239 , fRegion(TestRegion())
240 , fColor(0x01020304)
241 , fPoints(kTestPoints)
242 , fPointCount(3)
243 , fWidth(2)
244 , fHeight(2)
245 , fText("Hello World")
246 , fPoints2(kTestPoints2)
247 , fBitmap(TestBitmap())
248 { }
249
250 SkRect fRect;
tfarina567ff2f2015-04-27 07:01:44 -0700251 SkMatrix fMatrix;
piotaixrf05f5a72014-10-03 13:26:55 -0700252 SkPath fPath;
253 SkPath fNearlyZeroLengthPath;
254 SkIRect fIRect;
255 SkRegion fRegion;
256 SkColor fColor;
257 SkPaint fPaint;
258 const SkPoint* fPoints;
259 size_t fPointCount;
260 int fWidth;
261 int fHeight;
262 SkString fText;
263 const SkPoint* fPoints2;
264 SkBitmap fBitmap;
265
266private:
267 static SkMatrix TestMatrix() {
268 SkMatrix matrix;
269 matrix.reset();
270 matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
271
272 return matrix;
273 }
274 static SkPath TestPath() {
275 SkPath path;
276 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
277 SkIntToScalar(2), SkIntToScalar(1)));
278 return path;
279 }
280 static SkPath TestNearlyZeroLengthPath() {
281 SkPath path;
282 SkPoint pt1 = { 0, 0 };
283 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
284 SkPoint pt3 = { SkIntToScalar(1), 0 };
285 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
286 path.moveTo(pt1);
287 path.lineTo(pt2);
288 path.lineTo(pt3);
289 path.lineTo(pt4);
290 return path;
291 }
292 static SkRegion TestRegion() {
293 SkRegion region;
294 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
295 region.setRect(rect);
296 return region;
297 }
298 static SkBitmap TestBitmap() {
299 SkBitmap bitmap;
300 createBitmap(&bitmap, 0x05060708);
301 return bitmap;
302 }
303};
304
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000305// Format strings that describe the test context. The %s token is where
306// the name of the test step is inserted. The context is required for
307// disambiguating the error in the case of failures that are reported in
308// functions that are called multiple times in different contexts (test
309// cases and test steps).
310static const char* const kDefaultAssertMessageFormat = "%s";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000311static const char* const kCanvasDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000312 "Drawing test step %s with SkCanvas";
edisonn@google.com77909122012-10-18 15:58:23 +0000313static const char* const kPdfAssertMessageFormat =
314 "PDF sanity check failed %s";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000315
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000316class CanvasTestStep;
317static SkTDArray<CanvasTestStep*>& testStepArray() {
318 static SkTDArray<CanvasTestStep*> theTests;
319 return theTests;
320}
321
322class CanvasTestStep {
323public:
edisonn@google.com77909122012-10-18 15:58:23 +0000324 CanvasTestStep(bool fEnablePdfTesting = true) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000325 *testStepArray().append() = this;
326 fAssertMessageFormat = kDefaultAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000327 this->fEnablePdfTesting = fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000328 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000329 virtual ~CanvasTestStep() { }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000330
piotaixrf05f5a72014-10-03 13:26:55 -0700331 virtual void draw(SkCanvas*, const TestData&, skiatest::Reporter*) = 0;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000332 virtual const char* name() const = 0;
333
334 const char* assertMessage() {
335 fAssertMessage.printf(fAssertMessageFormat, name());
336 return fAssertMessage.c_str();
337 }
338
339 void setAssertMessageFormat(const char* format) {
340 fAssertMessageFormat = format;
341 }
342
edisonn@google.com77909122012-10-18 15:58:23 +0000343 bool enablePdfTesting() { return fEnablePdfTesting; }
344
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000345private:
346 SkString fAssertMessage;
347 const char* fAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000348 bool fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000349};
350
351///////////////////////////////////////////////////////////////////////////////
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000352// Macros for defining test steps
353
354#define TEST_STEP(NAME, FUNCTION) \
355class NAME##_TestStep : public CanvasTestStep{ \
356public: \
piotaixrf05f5a72014-10-03 13:26:55 -0700357 virtual void draw(SkCanvas* canvas, const TestData& d, \
358 skiatest::Reporter* reporter) { \
359 FUNCTION (canvas, d, reporter, this); \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000360 } \
361 virtual const char* name() const {return #NAME ;} \
362}; \
363static NAME##_TestStep NAME##_TestStepInstance;
364
piotaixrf05f5a72014-10-03 13:26:55 -0700365#define TEST_STEP_NO_PDF(NAME, FUNCTION) \
edisonn@google.com77909122012-10-18 15:58:23 +0000366class NAME##_TestStep : public CanvasTestStep{ \
367public: \
368 NAME##_TestStep() : CanvasTestStep(false) {} \
piotaixrf05f5a72014-10-03 13:26:55 -0700369 virtual void draw(SkCanvas* canvas, const TestData& d, \
370 skiatest::Reporter* reporter) { \
371 FUNCTION (canvas, d, reporter, this); \
edisonn@google.com77909122012-10-18 15:58:23 +0000372 } \
373 virtual const char* name() const {return #NAME ;} \
374}; \
375static NAME##_TestStep NAME##_TestStepInstance;
376
piotaixrf05f5a72014-10-03 13:26:55 -0700377#define SIMPLE_TEST_STEP(NAME, CALL) \
378static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \
379 skiatest::Reporter*, CanvasTestStep*) { \
380 canvas-> CALL ; \
381} \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000382TEST_STEP(NAME, NAME##TestStep )
383
Brian Salomon1c80e992018-01-29 09:50:47 -0500384#define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
385 static void NAME##TestStep(SkCanvas* canvas, const TestData& d, skiatest::Reporter*, \
386 CanvasTestStep* testStep) { \
387 REPORTER_ASSERT(reporter, canvas->CALL, testStep->assertMessage()); \
388 } \
389 TEST_STEP(NAME, NAME##TestStep)
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000390
391///////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000392// Basic test steps for most virtual methods in SkCanvas that draw or affect
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000393// the state of the canvas.
394
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000395SIMPLE_TEST_STEP(Translate, translate(SkIntToScalar(1), SkIntToScalar(2)));
396SIMPLE_TEST_STEP(Scale, scale(SkIntToScalar(1), SkIntToScalar(2)));
397SIMPLE_TEST_STEP(Rotate, rotate(SkIntToScalar(1)));
398SIMPLE_TEST_STEP(Skew, skew(SkIntToScalar(1), SkIntToScalar(2)));
piotaixrf05f5a72014-10-03 13:26:55 -0700399SIMPLE_TEST_STEP(Concat, concat(d.fMatrix));
400SIMPLE_TEST_STEP(SetMatrix, setMatrix(d.fMatrix));
401SIMPLE_TEST_STEP(ClipRect, clipRect(d.fRect));
402SIMPLE_TEST_STEP(ClipPath, clipPath(d.fPath));
Mike Reedc1f77742016-12-09 09:00:50 -0500403SIMPLE_TEST_STEP(ClipRegion, clipRegion(d.fRegion, kReplace_SkClipOp));
piotaixrf05f5a72014-10-03 13:26:55 -0700404SIMPLE_TEST_STEP(Clear, clear(d.fColor));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000405
406///////////////////////////////////////////////////////////////////////////////
407// Complex test steps
408
piotaixrf05f5a72014-10-03 13:26:55 -0700409static void SaveMatrixClipStep(SkCanvas* canvas, const TestData& d,
410 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000411 int saveCount = canvas->getSaveCount();
Florin Malita5f6102d2014-06-30 10:13:28 -0400412 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000413 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
piotaixrf05f5a72014-10-03 13:26:55 -0700414 canvas->clipRegion(d.fRegion);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000415 canvas->restore();
Brian Salomon1c80e992018-01-29 09:50:47 -0500416 REPORTER_ASSERT(reporter, canvas->getSaveCount() == saveCount, testStep->assertMessage());
417 REPORTER_ASSERT(reporter, canvas->getTotalMatrix().isIdentity(), testStep->assertMessage());
418 // REPORTER_ASSERT(reporter, canvas->getTotalClip() != kTestRegion,
419 // testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000420}
421TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
422
piotaixrf05f5a72014-10-03 13:26:55 -0700423static void SaveLayerStep(SkCanvas* canvas, const TestData& d,
424 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000425 int saveCount = canvas->getSaveCount();
halcanary96fcdcc2015-08-27 07:41:13 -0700426 canvas->saveLayer(nullptr, nullptr);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000427 canvas->restore();
Brian Salomon1c80e992018-01-29 09:50:47 -0500428 REPORTER_ASSERT(reporter, canvas->getSaveCount() == saveCount, testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000429}
430TEST_STEP(SaveLayer, SaveLayerStep);
431
piotaixrf05f5a72014-10-03 13:26:55 -0700432static void BoundedSaveLayerStep(SkCanvas* canvas, const TestData& d,
433 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000434 int saveCount = canvas->getSaveCount();
halcanary96fcdcc2015-08-27 07:41:13 -0700435 canvas->saveLayer(&d.fRect, nullptr);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000436 canvas->restore();
Brian Salomon1c80e992018-01-29 09:50:47 -0500437 REPORTER_ASSERT(reporter, canvas->getSaveCount() == saveCount, testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000438}
439TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
440
piotaixrf05f5a72014-10-03 13:26:55 -0700441static void PaintSaveLayerStep(SkCanvas* canvas, const TestData& d,
442 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000443 int saveCount = canvas->getSaveCount();
halcanary96fcdcc2015-08-27 07:41:13 -0700444 canvas->saveLayer(nullptr, &d.fPaint);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000445 canvas->restore();
Brian Salomon1c80e992018-01-29 09:50:47 -0500446 REPORTER_ASSERT(reporter, canvas->getSaveCount() == saveCount, testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000447}
448TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
449
piotaixrf05f5a72014-10-03 13:26:55 -0700450static void TwoClipOpsStep(SkCanvas* canvas, const TestData& d,
451 skiatest::Reporter*, CanvasTestStep*) {
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000452 // This test exercises a functionality in SkPicture that leads to the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000453 // recording of restore offset placeholders. This test will trigger an
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000454 // assertion at playback time if the placeholders are not properly
455 // filled when the recording ends.
piotaixrf05f5a72014-10-03 13:26:55 -0700456 canvas->clipRect(d.fRect);
457 canvas->clipRegion(d.fRegion);
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000458}
459TEST_STEP(TwoClipOps, TwoClipOpsStep);
460
epoger@google.com94fa43c2012-04-11 17:51:01 +0000461// exercise fix for http://code.google.com/p/skia/issues/detail?id=560
462// ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
piotaixrf05f5a72014-10-03 13:26:55 -0700463static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas, const TestData& d,
464 skiatest::Reporter*, CanvasTestStep*) {
epoger@google.com94fa43c2012-04-11 17:51:01 +0000465 SkPaint paint;
466 paint.setStrokeWidth(SkIntToScalar(1));
467 paint.setStyle(SkPaint::kStroke_Style);
468
piotaixrf05f5a72014-10-03 13:26:55 -0700469 canvas->drawPath(d.fNearlyZeroLengthPath, paint);
epoger@google.com94fa43c2012-04-11 17:51:01 +0000470}
471TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
472
piotaixrf05f5a72014-10-03 13:26:55 -0700473static void DrawVerticesShaderTestStep(SkCanvas* canvas, const TestData& d,
474 skiatest::Reporter*, CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000475 SkPoint pts[4];
476 pts[0].set(0, 0);
piotaixrf05f5a72014-10-03 13:26:55 -0700477 pts[1].set(SkIntToScalar(d.fWidth), 0);
478 pts[2].set(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight));
479 pts[3].set(0, SkIntToScalar(d.fHeight));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000480 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -0700481 paint.setShader(SkShader::MakeBitmapShader(d.fBitmap, SkShader::kClamp_TileMode,
482 SkShader::kClamp_TileMode));
Mike Reed887cdf12017-04-03 11:11:09 -0400483 canvas->drawVertices(SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, 4, pts, pts,
484 nullptr),
485 SkBlendMode::kModulate, paint);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000486}
edisonn@google.com77909122012-10-18 15:58:23 +0000487// NYI: issue 240.
488TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000489
piotaixrf05f5a72014-10-03 13:26:55 -0700490static void DrawPictureTestStep(SkCanvas* canvas, const TestData& d,
491 skiatest::Reporter*, CanvasTestStep*) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000492 SkPictureRecorder recorder;
piotaixrf05f5a72014-10-03 13:26:55 -0700493 SkCanvas* testCanvas = recorder.beginRecording(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight),
halcanary96fcdcc2015-08-27 07:41:13 -0700494 nullptr, 0);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000495 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
piotaixrf05f5a72014-10-03 13:26:55 -0700496 testCanvas->clipRect(d.fRect);
497 testCanvas->drawRect(d.fRect, d.fPaint);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000498
reedca2622b2016-03-18 07:25:55 -0700499 canvas->drawPicture(recorder.finishRecordingAsPicture());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000500}
501TEST_STEP(DrawPicture, DrawPictureTestStep);
502
piotaixrf05f5a72014-10-03 13:26:55 -0700503static void SaveRestoreTestStep(SkCanvas* canvas, const TestData& d,
504 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000505 int baseSaveCount = canvas->getSaveCount();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000506 int n = canvas->save();
Brian Salomon1c80e992018-01-29 09:50:47 -0500507 REPORTER_ASSERT(reporter, baseSaveCount == n, testStep->assertMessage());
508 REPORTER_ASSERT(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
509 testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000510 canvas->save();
511 canvas->save();
Brian Salomon1c80e992018-01-29 09:50:47 -0500512 REPORTER_ASSERT(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
513 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000514 canvas->restoreToCount(baseSaveCount + 1);
Brian Salomon1c80e992018-01-29 09:50:47 -0500515 REPORTER_ASSERT(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
516 testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000517
518 // should this pin to 1, or be a no-op, or crash?
519 canvas->restoreToCount(0);
Brian Salomon1c80e992018-01-29 09:50:47 -0500520 REPORTER_ASSERT(reporter, 1 == canvas->getSaveCount(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000521}
522TEST_STEP(SaveRestore, SaveRestoreTestStep);
523
piotaixrf05f5a72014-10-03 13:26:55 -0700524static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas, const TestData& d,
525 skiatest::Reporter*, CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000526 // This test step challenges the TestDeferredCanvasStateConsistency
527 // test cases because the opaque paint can trigger an optimization
528 // that discards previously recorded commands. The challenge is to maintain
529 // correct clip and matrix stack state.
530 canvas->resetMatrix();
531 canvas->rotate(SkIntToScalar(30));
532 canvas->save();
533 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
534 canvas->save();
535 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
536 SkPaint paint;
537 paint.setColor(0xFFFFFFFF);
538 canvas->drawPaint(paint);
539 canvas->restore();
540 canvas->restore();
541}
542TEST_STEP(NestedSaveRestoreWithSolidPaint, \
543 NestedSaveRestoreWithSolidPaintTestStep);
544
piotaixrf05f5a72014-10-03 13:26:55 -0700545static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas, const TestData& d,
546 skiatest::Reporter*, CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000547 // This test step challenges the TestDeferredCanvasStateConsistency
548 // test case because the canvas flush on a deferred canvas will
549 // reset the recording session. The challenge is to maintain correct
550 // clip and matrix stack state on the playback canvas.
551 canvas->resetMatrix();
552 canvas->rotate(SkIntToScalar(30));
553 canvas->save();
554 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
555 canvas->save();
556 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
piotaixrf05f5a72014-10-03 13:26:55 -0700557 canvas->drawRect(d.fRect,d.fPaint);
reed@google.com3b3e8952012-08-16 20:53:31 +0000558 canvas->flush();
559 canvas->restore();
560 canvas->restore();
561}
piotaixrf05f5a72014-10-03 13:26:55 -0700562TEST_STEP(NestedSaveRestoreWithFlush, NestedSaveRestoreWithFlushTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000563
reed3aafe112016-08-18 12:45:34 -0700564static void TestPdfDevice(skiatest::Reporter* reporter, const TestData& d, CanvasTestStep* step) {
halcanary3d32d502015-03-01 06:55:20 -0800565 SkDynamicMemoryWStream outStream;
halcanary4b656662016-04-27 07:45:18 -0700566 sk_sp<SkDocument> doc(SkDocument::MakePDF(&outStream));
halcanary2ccdb632015-08-11 13:35:12 -0700567 if (!doc) {
Hal Canary4125b612018-03-20 14:17:00 -0400568 INFOF(reporter, "PDF disabled; TestPdfDevice test skipped.");
halcanary2ccdb632015-08-11 13:35:12 -0700569 return;
570 }
halcanary3d32d502015-03-01 06:55:20 -0800571 SkCanvas* canvas = doc->beginPage(SkIntToScalar(d.fWidth),
572 SkIntToScalar(d.fHeight));
573 REPORTER_ASSERT(reporter, canvas);
reed3aafe112016-08-18 12:45:34 -0700574 step->setAssertMessageFormat(kPdfAssertMessageFormat);
575 step->draw(canvas, d, reporter);
edisonn@google.com77909122012-10-18 15:58:23 +0000576}
577
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000578/*
579 * This sub-test verifies that the test step passes when executed
580 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
581 * that the all canvas derivatives report the same state as an SkCanvas
582 * after having executed the test step.
583 */
piotaixrf05f5a72014-10-03 13:26:55 -0700584static void TestOverrideStateConsistency(skiatest::Reporter* reporter, const TestData& d,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000585 CanvasTestStep* testStep) {
586 SkBitmap referenceStore;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000587 createBitmap(&referenceStore, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700588 SkCanvas referenceCanvas(referenceStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000589 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700590 testStep->draw(&referenceCanvas, d, reporter);
reed@google.com7c202932011-12-14 18:48:05 +0000591}
reed@google.com37f3ae02011-11-28 16:06:04 +0000592
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000593static void test_newraster(skiatest::Reporter* reporter) {
594 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
reed3054be12014-12-10 07:24:28 -0800595 const size_t minRowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400596 const size_t size = info.computeByteSize(minRowBytes);
scroggo565901d2015-12-10 10:44:13 -0800597 SkAutoTMalloc<SkPMColor> storage(size);
598 SkPMColor* baseAddr = storage.get();
reed3054be12014-12-10 07:24:28 -0800599 sk_bzero(baseAddr, size);
600
Mike Reed5df49342016-11-12 08:06:55 -0600601 std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000602 REPORTER_ASSERT(reporter, canvas);
603
reed6ceeebd2016-03-09 14:26:26 -0800604 SkPixmap pmap;
605 const SkPMColor* addr = canvas->peekPixels(&pmap) ? pmap.addr32() : nullptr;
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000606 REPORTER_ASSERT(reporter, addr);
reed6ceeebd2016-03-09 14:26:26 -0800607 REPORTER_ASSERT(reporter, info == pmap.info());
608 REPORTER_ASSERT(reporter, minRowBytes == pmap.rowBytes());
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000609 for (int y = 0; y < info.height(); ++y) {
610 for (int x = 0; x < info.width(); ++x) {
611 REPORTER_ASSERT(reporter, 0 == addr[x]);
612 }
reed6ceeebd2016-03-09 14:26:26 -0800613 addr = (const SkPMColor*)((const char*)addr + pmap.rowBytes());
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000614 }
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000615
616 // now try a deliberately bad info
reede5ea5002014-09-03 11:54:58 -0700617 info = info.makeWH(-1, info.height());
Mike Reed5df49342016-11-12 08:06:55 -0600618 REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes));
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000619
620 // too big
reede5ea5002014-09-03 11:54:58 -0700621 info = info.makeWH(1 << 30, 1 << 30);
Mike Reed5df49342016-11-12 08:06:55 -0600622 REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes));
skia.committer@gmail.com0e530752014-02-28 03:02:05 +0000623
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000624 // not a valid pixel type
reede5ea5002014-09-03 11:54:58 -0700625 info = SkImageInfo::Make(10, 10, kUnknown_SkColorType, info.alphaType());
Mike Reed5df49342016-11-12 08:06:55 -0600626 REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes));
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000627
628 // We should succeed with a zero-sized valid info
629 info = SkImageInfo::MakeN32Premul(0, 0);
Mike Reed5df49342016-11-12 08:06:55 -0600630 canvas = SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000631 REPORTER_ASSERT(reporter, canvas);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000632}
633
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000634DEF_TEST(Canvas, reporter) {
piotaixrf05f5a72014-10-03 13:26:55 -0700635 TestData d;
reed@google.com37f3ae02011-11-28 16:06:04 +0000636
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000637 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
piotaixrf05f5a72014-10-03 13:26:55 -0700638 TestOverrideStateConsistency(reporter, d, testStepArray()[testStep]);
edisonn@google.com77909122012-10-18 15:58:23 +0000639 if (testStepArray()[testStep]->enablePdfTesting()) {
piotaixrf05f5a72014-10-03 13:26:55 -0700640 TestPdfDevice(reporter, d, testStepArray()[testStep]);
edisonn@google.com77909122012-10-18 15:58:23 +0000641 }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000642 }
junov@chromium.orgcd62ecf2012-08-02 17:43:25 +0000643
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000644 test_newraster(reporter);
reed@google.com37f3ae02011-11-28 16:06:04 +0000645}
reedf0090cb2014-11-26 08:55:51 -0800646
647DEF_TEST(Canvas_SaveState, reporter) {
648 SkCanvas canvas(10, 10);
649 REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
650
651 int n = canvas.save();
652 REPORTER_ASSERT(reporter, 1 == n);
653 REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
654
halcanary96fcdcc2015-08-27 07:41:13 -0700655 n = canvas.saveLayer(nullptr, nullptr);
reedf0090cb2014-11-26 08:55:51 -0800656 REPORTER_ASSERT(reporter, 2 == n);
657 REPORTER_ASSERT(reporter, 3 == canvas.getSaveCount());
halcanary9d524f22016-03-29 09:03:52 -0700658
reedf0090cb2014-11-26 08:55:51 -0800659 canvas.restore();
660 REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
661 canvas.restore();
662 REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
663}
reedc1b11f12015-03-13 08:48:26 -0700664
665DEF_TEST(Canvas_ClipEmptyPath, reporter) {
666 SkCanvas canvas(10, 10);
667 canvas.save();
668 SkPath path;
669 canvas.clipPath(path);
670 canvas.restore();
671 canvas.save();
672 path.moveTo(5, 5);
673 canvas.clipPath(path);
674 canvas.restore();
675 canvas.save();
676 path.moveTo(7, 7);
677 canvas.clipPath(path); // should not assert here
678 canvas.restore();
679}
fmalitaf433bb22015-08-17 08:05:13 -0700680
681namespace {
682
683class MockFilterCanvas : public SkPaintFilterCanvas {
684public:
685 MockFilterCanvas(SkCanvas* canvas) : INHERITED(canvas) { }
686
687protected:
fmalita32cdc322016-01-12 07:21:11 -0800688 bool onFilter(SkTCopyOnFirstWrite<SkPaint>*, Type) const override { return true; }
fmalitaf433bb22015-08-17 08:05:13 -0700689
690private:
691 typedef SkPaintFilterCanvas INHERITED;
692};
693
694} // anonymous namespace
695
696// SkPaintFilterCanvas should inherit the initial target canvas state.
697DEF_TEST(PaintFilterCanvas_ConsistentState, reporter) {
698 SkCanvas canvas(100, 100);
699 canvas.clipRect(SkRect::MakeXYWH(12.7f, 12.7f, 75, 75));
700 canvas.scale(0.5f, 0.75f);
701
fmalitaf433bb22015-08-17 08:05:13 -0700702 MockFilterCanvas filterCanvas(&canvas);
703 REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix());
Mike Reed918e1442017-01-23 11:39:45 -0500704 REPORTER_ASSERT(reporter, canvas.getLocalClipBounds() == filterCanvas.getLocalClipBounds());
fmalitaf433bb22015-08-17 08:05:13 -0700705
706 filterCanvas.clipRect(SkRect::MakeXYWH(30.5f, 30.7f, 100, 100));
707 filterCanvas.scale(0.75f, 0.5f);
708 REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix());
Mike Reed918e1442017-01-23 11:39:45 -0500709 REPORTER_ASSERT(reporter, filterCanvas.getLocalClipBounds().contains(canvas.getLocalClipBounds()));
fmalitaf433bb22015-08-17 08:05:13 -0700710}
reedbabc3de2016-07-08 08:43:27 -0700711
712///////////////////////////////////////////////////////////////////////////////////////////////////
713
Mike Reed584ca892016-11-15 11:52:55 -0500714// Subclass that takes a bool*, which it updates in its construct (true) and destructor (false)
715// to allow the caller to know how long the object is alive.
716class LifeLineCanvas : public SkCanvas {
717 bool* fLifeLine;
718public:
719 LifeLineCanvas(int w, int h, bool* lifeline) : SkCanvas(w, h), fLifeLine(lifeline) {
720 *fLifeLine = true;
721 }
722 ~LifeLineCanvas() {
723 *fLifeLine = false;
724 }
725};
726
727// Check that NWayCanvas does NOT try to manage the lifetime of its sub-canvases
728DEF_TEST(NWayCanvas, r) {
729 const int w = 10;
730 const int h = 10;
731 bool life[2];
732 {
733 LifeLineCanvas c0(w, h, &life[0]);
734 REPORTER_ASSERT(r, life[0]);
735 }
736 REPORTER_ASSERT(r, !life[0]);
737
738
739 std::unique_ptr<SkCanvas> c0 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[0]));
740 std::unique_ptr<SkCanvas> c1 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[1]));
741 REPORTER_ASSERT(r, life[0]);
742 REPORTER_ASSERT(r, life[1]);
743
744 {
745 SkNWayCanvas nway(w, h);
746 nway.addCanvas(c0.get());
747 nway.addCanvas(c1.get());
748 REPORTER_ASSERT(r, life[0]);
749 REPORTER_ASSERT(r, life[1]);
750 }
751 // Now assert that the death of the nway has NOT also killed the sub-canvases
752 REPORTER_ASSERT(r, life[0]);
753 REPORTER_ASSERT(r, life[1]);
754}
755
756// Check that CanvasStack DOES manage the lifetime of its sub-canvases
757DEF_TEST(CanvasStack, r) {
758 const int w = 10;
759 const int h = 10;
760 bool life[2];
761 std::unique_ptr<SkCanvas> c0 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[0]));
762 std::unique_ptr<SkCanvas> c1 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[1]));
763 REPORTER_ASSERT(r, life[0]);
764 REPORTER_ASSERT(r, life[1]);
765
766 {
767 SkCanvasStack stack(w, h);
768 stack.pushCanvas(std::move(c0), {0,0});
769 stack.pushCanvas(std::move(c1), {0,0});
770 REPORTER_ASSERT(r, life[0]);
771 REPORTER_ASSERT(r, life[1]);
772 }
773 // Now assert that the death of the canvasstack has also killed the sub-canvases
774 REPORTER_ASSERT(r, !life[0]);
775 REPORTER_ASSERT(r, !life[1]);
776}
Mike Reedcd667b62017-03-02 15:21:11 -0500777
Mike Reed3b4c22d2017-03-02 20:07:46 -0500778static void test_cliptype(SkCanvas* canvas, skiatest::Reporter* r) {
Mike Reedcd667b62017-03-02 15:21:11 -0500779 REPORTER_ASSERT(r, !canvas->isClipEmpty());
780 REPORTER_ASSERT(r, canvas->isClipRect());
781
782 canvas->save();
783 canvas->clipRect({0, 0, 0, 0});
784 REPORTER_ASSERT(r, canvas->isClipEmpty());
785 REPORTER_ASSERT(r, !canvas->isClipRect());
786 canvas->restore();
787
788 canvas->save();
789 canvas->clipRect({2, 2, 6, 6});
790 REPORTER_ASSERT(r, !canvas->isClipEmpty());
791 REPORTER_ASSERT(r, canvas->isClipRect());
792 canvas->restore();
793
794 canvas->save();
795 canvas->clipRect({2, 2, 6, 6}, SkClipOp::kDifference); // punch a hole in the clip
796 REPORTER_ASSERT(r, !canvas->isClipEmpty());
797 REPORTER_ASSERT(r, !canvas->isClipRect());
798 canvas->restore();
799
800 REPORTER_ASSERT(r, !canvas->isClipEmpty());
801 REPORTER_ASSERT(r, canvas->isClipRect());
802}
Mike Reed3b4c22d2017-03-02 20:07:46 -0500803
804DEF_TEST(CanvasClipType, r) {
805 // test rasterclip backend
806 test_cliptype(SkSurface::MakeRasterN32Premul(10, 10)->getCanvas(), r);
807
808 // test clipstack backend
809 SkDynamicMemoryWStream stream;
Hal Canary4125b612018-03-20 14:17:00 -0400810 if (auto doc = SkDocument::MakePDF(&stream)) {
811 test_cliptype(doc->beginPage(100, 100), r);
812 }
Mike Reed3b4c22d2017-03-02 20:07:46 -0500813}
Matt Sarett31f99ce2017-04-11 08:46:01 -0400814
815#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
816DEF_TEST(Canvas_LegacyColorBehavior, r) {
817 sk_sp<SkColorSpace> cs = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
818 SkColorSpace::kAdobeRGB_Gamut);
819
820 // Make a Adobe RGB bitmap.
821 SkBitmap bitmap;
822 bitmap.allocPixels(SkImageInfo::MakeN32(1, 1, kOpaque_SkAlphaType, cs));
823 bitmap.eraseColor(0xFF000000);
824
825 // Wrap it in a legacy canvas. Test that the canvas behaves like a legacy canvas.
826 SkCanvas canvas(bitmap, SkCanvas::ColorBehavior::kLegacy);
827 REPORTER_ASSERT(r, !canvas.imageInfo().colorSpace());
828 SkPaint p;
829 p.setColor(SK_ColorRED);
830 canvas.drawIRect(SkIRect::MakeWH(1, 1), p);
831 REPORTER_ASSERT(r, SK_ColorRED == SkSwizzle_BGRA_to_PMColor(*bitmap.getAddr32(0, 0)));
832}
833#endif
Xianzhu Wang3b428cb2018-01-22 09:51:13 -0800834
835namespace {
836
837class ZeroBoundsImageFilter : public SkImageFilter {
838public:
839 static sk_sp<SkImageFilter> Make() { return sk_sp<SkImageFilter>(new ZeroBoundsImageFilter); }
840
Xianzhu Wang3b428cb2018-01-22 09:51:13 -0800841 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(ZeroBoundsImageFilter)
842
843protected:
844 sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage*, const Context&, SkIPoint*) const override {
845 return nullptr;
846 }
847 sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override { return nullptr; }
Robert Phillips12078432018-05-17 11:17:39 -0400848 SkIRect onFilterNodeBounds(const SkIRect&, const SkMatrix&,
849 MapDirection, const SkIRect* inputRect) const override {
Xianzhu Wang3b428cb2018-01-22 09:51:13 -0800850 return SkIRect::MakeEmpty();
851 }
852
853private:
854 ZeroBoundsImageFilter() : INHERITED(nullptr, 0, nullptr) {}
855
856 typedef SkImageFilter INHERITED;
857};
858
859sk_sp<SkFlattenable> ZeroBoundsImageFilter::CreateProc(SkReadBuffer& buffer) {
860 SkDEBUGFAIL("Should never get here");
861 return nullptr;
862}
863
Xianzhu Wang3b428cb2018-01-22 09:51:13 -0800864} // anonymous namespace
865
866DEF_TEST(Canvas_SaveLayerWithNullBoundsAndZeroBoundsImageFilter, r) {
867 SkCanvas canvas(10, 10);
868 SkPaint p;
869 p.setImageFilter(ZeroBoundsImageFilter::Make());
870 // This should not fail any assert.
871 canvas.saveLayer(nullptr, &p);
872 REPORTER_ASSERT(r, canvas.getDeviceClipBounds().isEmpty());
873 canvas.restore();
874}
Mike Reed490aa592018-04-13 15:34:16 -0400875
876#include "SkPaintImageFilter.h"
877
878// Test that we don't crash/assert when building a canvas with degenerate coordintes
879// (esp. big ones, that might invoke tiling).
880DEF_TEST(Canvas_degenerate_dimension, reporter) {
881 // Need a paint that will sneak us past the quickReject in SkCanvas, so we can test the
882 // raster code further downstream.
883 SkPaint paint;
884 paint.setImageFilter(SkPaintImageFilter::Make(SkPaint(), nullptr));
885 REPORTER_ASSERT(reporter, !paint.canComputeFastBounds());
886
887 const int big = 100 * 1024; // big enough to definitely trigger tiling
888 const SkISize sizes[] {SkISize{0, big}, {big, 0}, {0, 0}};
889 for (SkISize size : sizes) {
890 SkBitmap bm;
891 bm.setInfo(SkImageInfo::MakeN32Premul(size.width(), size.height()));
892 SkCanvas canvas(bm);
893 canvas.drawRect({0, 0, 100, 90*1024}, paint);
894 }
895}
896
Robert Phillips3d0e8502018-04-20 10:27:27 -0400897#include "SkBlurImageFilter.h"
898
899DEF_TEST(Canvas_ClippedOutImageFilter, reporter) {
900 SkCanvas canvas(100, 100);
901
902 SkPaint p;
903 p.setColor(SK_ColorGREEN);
904 p.setImageFilter(SkBlurImageFilter::Make(3.0f, 3.0f, nullptr, nullptr));
905
906 SkRect blurredRect = SkRect::MakeXYWH(60, 10, 30, 30);
907
908 SkMatrix invM;
909 invM.setRotate(-45);
910 invM.mapRect(&blurredRect);
911
912 const SkRect clipRect = SkRect::MakeXYWH(0, 50, 50, 50);
913
914 canvas.clipRect(clipRect);
915
916 canvas.rotate(45);
917 const SkMatrix preCTM = canvas.getTotalMatrix();
918 canvas.drawRect(blurredRect, p);
919 const SkMatrix postCTM = canvas.getTotalMatrix();
920 REPORTER_ASSERT(reporter, preCTM == postCTM);
921}
922