blob: 6229e849fb2bb5eea17ef6f33949ab8ee62a5e14 [file] [log] [blame]
reed@google.com37f3ae02011-11-28 16:06:04 +00001/*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +00002 * Copyright 2012 Google Inc.
reed@google.com37f3ae02011-11-28 16:06:04 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +00007
8/* Description:
9 * This test defines a series of elementatry test steps that perform
10 * a single or a small group of canvas API calls. Each test step is
11 * used in several test cases that verify that different types of SkCanvas
12 * flavors and derivatives pass it and yield consistent behavior. The
13 * test cases analyse results that are queryable through the API. They do
14 * not look at rendering results.
15 *
16 * Adding test stepss:
17 * The general pattern for creating a new test step is to write a test
18 * function of the form:
19 *
rmistry@google.comd6176b02012-08-23 18:14:13 +000020 * static void MyTestStepFunction(SkCanvas* canvas,
tomhudsoncb3bd182016-05-18 07:24:16 -070021 * const TestData& d,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000022 * skiatest::Reporter* reporter,
23 * CanvasTestStep* testStep)
24 * {
25 * canvas->someCanvasAPImethod();
26 * (...)
27 * REPORTER_ASSERT_MESSAGE(reporter, (...), \
28 * testStep->assertMessage());
29 * }
30 *
31 * The definition of the test step function should be followed by an
32 * invocation of the TEST_STEP macro, which generates a class and
33 * instance for the test step:
34 *
35 * TEST_STEP(MyTestStep, MyTestStepFunction)
36 *
37 * There are also short hand macros for defining simple test steps
38 * in a single line of code. A simple test step is a one that is made
39 * of a single canvas API call.
40 *
41 * SIMPLE_TEST_STEP(MytestStep, someCanvasAPIMethod());
42 *
43 * There is another macro called SIMPLE_TEST_STEP_WITH_ASSERT that
44 * works the same way as SIMPLE_TEST_STEP, and additionally verifies
45 * that the invoked method returns a non-zero value.
46 */
reed@google.com37f3ae02011-11-28 16:06:04 +000047#include "SkBitmap.h"
48#include "SkCanvas.h"
reed687fa1c2015-04-07 08:00:56 -070049#include "SkClipStack.h"
halcanary3d32d502015-03-01 06:55:20 -080050#include "SkDocument.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000051#include "SkMatrix.h"
52#include "SkNWayCanvas.h"
53#include "SkPaint.h"
fmalitaf433bb22015-08-17 08:05:13 -070054#include "SkPaintFilterCanvas.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000055#include "SkPath.h"
56#include "SkPicture.h"
57#include "SkPictureRecord.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000058#include "SkPictureRecorder.h"
reed1e7f5e72016-04-27 07:49:17 -070059#include "SkRasterClip.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000060#include "SkRect.h"
61#include "SkRegion.h"
62#include "SkShader.h"
63#include "SkStream.h"
reed@google.com28183b42014-02-04 15:34:10 +000064#include "SkSurface.h"
scroggo565901d2015-12-10 10:44:13 -080065#include "SkTemplates.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000066#include "SkTDArray.h"
67#include "Test.h"
reed@google.com37f3ae02011-11-28 16:06:04 +000068
Mike Reed918e1442017-01-23 11:39:45 -050069DEF_TEST(canvas_clipbounds, reporter) {
70 SkCanvas canvas(10, 10);
71 SkIRect irect;
72 SkRect rect;
73
74 irect = canvas.getDeviceClipBounds();
75 REPORTER_ASSERT(reporter, irect == SkIRect::MakeWH(10, 10));
76 // local bounds are always too big today -- can we trim them?
77 rect = canvas.getLocalClipBounds();
78 REPORTER_ASSERT(reporter, rect.contains(SkRect::MakeWH(10, 10)));
79
80 canvas.clipRect(SkRect::MakeEmpty());
81
82 irect = canvas.getDeviceClipBounds();
83 REPORTER_ASSERT(reporter, irect == SkIRect::MakeEmpty());
84 rect = canvas.getLocalClipBounds();
85 REPORTER_ASSERT(reporter, rect == SkRect::MakeEmpty());
86}
87
piotaixrf05f5a72014-10-03 13:26:55 -070088static const int kWidth = 2, kHeight = 2;
89
90static void createBitmap(SkBitmap* bm, SkColor color) {
91 bm->allocN32Pixels(kWidth, kHeight);
92 bm->eraseColor(color);
93}
94
piotaixrf05f5a72014-10-03 13:26:55 -070095///////////////////////////////////////////////////////////////////////////////
96// Constants used by test steps
97const SkPoint kTestPoints[] = {
98 {SkIntToScalar(0), SkIntToScalar(0)},
99 {SkIntToScalar(2), SkIntToScalar(1)},
100 {SkIntToScalar(0), SkIntToScalar(2)}
101};
102const SkPoint kTestPoints2[] = {
103 { SkIntToScalar(0), SkIntToScalar(1) },
104 { SkIntToScalar(1), SkIntToScalar(1) },
105 { SkIntToScalar(2), SkIntToScalar(1) },
106 { SkIntToScalar(3), SkIntToScalar(1) },
107 { SkIntToScalar(4), SkIntToScalar(1) },
108 { SkIntToScalar(5), SkIntToScalar(1) },
109 { SkIntToScalar(6), SkIntToScalar(1) },
110 { SkIntToScalar(7), SkIntToScalar(1) },
111 { SkIntToScalar(8), SkIntToScalar(1) },
112 { SkIntToScalar(9), SkIntToScalar(1) },
113 { SkIntToScalar(10), SkIntToScalar(1) }
114};
115
116struct TestData {
117public:
118 TestData()
119 : fRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
120 SkIntToScalar(2), SkIntToScalar(1)))
121 , fMatrix(TestMatrix())
122 , fPath(TestPath())
123 , fNearlyZeroLengthPath(TestNearlyZeroLengthPath())
124 , fIRect(SkIRect::MakeXYWH(0, 0, 2, 1))
125 , fRegion(TestRegion())
126 , fColor(0x01020304)
127 , fPoints(kTestPoints)
128 , fPointCount(3)
129 , fWidth(2)
130 , fHeight(2)
131 , fText("Hello World")
132 , fPoints2(kTestPoints2)
133 , fBitmap(TestBitmap())
134 { }
135
136 SkRect fRect;
tfarina567ff2f2015-04-27 07:01:44 -0700137 SkMatrix fMatrix;
piotaixrf05f5a72014-10-03 13:26:55 -0700138 SkPath fPath;
139 SkPath fNearlyZeroLengthPath;
140 SkIRect fIRect;
141 SkRegion fRegion;
142 SkColor fColor;
143 SkPaint fPaint;
144 const SkPoint* fPoints;
145 size_t fPointCount;
146 int fWidth;
147 int fHeight;
148 SkString fText;
149 const SkPoint* fPoints2;
150 SkBitmap fBitmap;
151
152private:
153 static SkMatrix TestMatrix() {
154 SkMatrix matrix;
155 matrix.reset();
156 matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
157
158 return matrix;
159 }
160 static SkPath TestPath() {
161 SkPath path;
162 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
163 SkIntToScalar(2), SkIntToScalar(1)));
164 return path;
165 }
166 static SkPath TestNearlyZeroLengthPath() {
167 SkPath path;
168 SkPoint pt1 = { 0, 0 };
169 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
170 SkPoint pt3 = { SkIntToScalar(1), 0 };
171 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
172 path.moveTo(pt1);
173 path.lineTo(pt2);
174 path.lineTo(pt3);
175 path.lineTo(pt4);
176 return path;
177 }
178 static SkRegion TestRegion() {
179 SkRegion region;
180 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
181 region.setRect(rect);
182 return region;
183 }
184 static SkBitmap TestBitmap() {
185 SkBitmap bitmap;
186 createBitmap(&bitmap, 0x05060708);
187 return bitmap;
188 }
189};
190
reed@google.com90c07ea2012-04-13 13:50:27 +0000191class Canvas2CanvasClipVisitor : public SkCanvas::ClipVisitor {
192public:
193 Canvas2CanvasClipVisitor(SkCanvas* target) : fTarget(target) {}
194
Mike Reedc1f77742016-12-09 09:00:50 -0500195 void clipRect(const SkRect& r, SkClipOp op, bool aa) override {
reed@google.com90c07ea2012-04-13 13:50:27 +0000196 fTarget->clipRect(r, op, aa);
197 }
Mike Reedc1f77742016-12-09 09:00:50 -0500198 void clipRRect(const SkRRect& r, SkClipOp op, bool aa) override {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000199 fTarget->clipRRect(r, op, aa);
200 }
Mike Reedc1f77742016-12-09 09:00:50 -0500201 void clipPath(const SkPath& p, SkClipOp op, bool aa) override {
reed@google.com90c07ea2012-04-13 13:50:27 +0000202 fTarget->clipPath(p, op, aa);
203 }
204
205private:
206 SkCanvas* fTarget;
207};
208
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000209// Format strings that describe the test context. The %s token is where
210// the name of the test step is inserted. The context is required for
211// disambiguating the error in the case of failures that are reported in
212// functions that are called multiple times in different contexts (test
213// cases and test steps).
214static const char* const kDefaultAssertMessageFormat = "%s";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000215static const char* const kCanvasDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000216 "Drawing test step %s with SkCanvas";
edisonn@google.com77909122012-10-18 15:58:23 +0000217static const char* const kPdfAssertMessageFormat =
218 "PDF sanity check failed %s";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000219
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000220class CanvasTestStep;
221static SkTDArray<CanvasTestStep*>& testStepArray() {
222 static SkTDArray<CanvasTestStep*> theTests;
223 return theTests;
224}
225
226class CanvasTestStep {
227public:
edisonn@google.com77909122012-10-18 15:58:23 +0000228 CanvasTestStep(bool fEnablePdfTesting = true) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000229 *testStepArray().append() = this;
230 fAssertMessageFormat = kDefaultAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000231 this->fEnablePdfTesting = fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000232 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000233 virtual ~CanvasTestStep() { }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000234
piotaixrf05f5a72014-10-03 13:26:55 -0700235 virtual void draw(SkCanvas*, const TestData&, skiatest::Reporter*) = 0;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000236 virtual const char* name() const = 0;
237
238 const char* assertMessage() {
239 fAssertMessage.printf(fAssertMessageFormat, name());
240 return fAssertMessage.c_str();
241 }
242
243 void setAssertMessageFormat(const char* format) {
244 fAssertMessageFormat = format;
245 }
246
edisonn@google.com77909122012-10-18 15:58:23 +0000247 bool enablePdfTesting() { return fEnablePdfTesting; }
248
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000249private:
250 SkString fAssertMessage;
251 const char* fAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000252 bool fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000253};
254
255///////////////////////////////////////////////////////////////////////////////
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000256// Macros for defining test steps
257
258#define TEST_STEP(NAME, FUNCTION) \
259class NAME##_TestStep : public CanvasTestStep{ \
260public: \
piotaixrf05f5a72014-10-03 13:26:55 -0700261 virtual void draw(SkCanvas* canvas, const TestData& d, \
262 skiatest::Reporter* reporter) { \
263 FUNCTION (canvas, d, reporter, this); \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000264 } \
265 virtual const char* name() const {return #NAME ;} \
266}; \
267static NAME##_TestStep NAME##_TestStepInstance;
268
piotaixrf05f5a72014-10-03 13:26:55 -0700269#define TEST_STEP_NO_PDF(NAME, FUNCTION) \
edisonn@google.com77909122012-10-18 15:58:23 +0000270class NAME##_TestStep : public CanvasTestStep{ \
271public: \
272 NAME##_TestStep() : CanvasTestStep(false) {} \
piotaixrf05f5a72014-10-03 13:26:55 -0700273 virtual void draw(SkCanvas* canvas, const TestData& d, \
274 skiatest::Reporter* reporter) { \
275 FUNCTION (canvas, d, reporter, this); \
edisonn@google.com77909122012-10-18 15:58:23 +0000276 } \
277 virtual const char* name() const {return #NAME ;} \
278}; \
279static NAME##_TestStep NAME##_TestStepInstance;
280
piotaixrf05f5a72014-10-03 13:26:55 -0700281#define SIMPLE_TEST_STEP(NAME, CALL) \
282static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \
283 skiatest::Reporter*, CanvasTestStep*) { \
284 canvas-> CALL ; \
285} \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000286TEST_STEP(NAME, NAME##TestStep )
287
288#define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
piotaixrf05f5a72014-10-03 13:26:55 -0700289static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \
290 skiatest::Reporter*, CanvasTestStep* testStep) { \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000291 REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \
292 testStep->assertMessage()); \
293} \
294TEST_STEP(NAME, NAME##TestStep )
295
296
297///////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000298// Basic test steps for most virtual methods in SkCanvas that draw or affect
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000299// the state of the canvas.
300
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000301SIMPLE_TEST_STEP(Translate, translate(SkIntToScalar(1), SkIntToScalar(2)));
302SIMPLE_TEST_STEP(Scale, scale(SkIntToScalar(1), SkIntToScalar(2)));
303SIMPLE_TEST_STEP(Rotate, rotate(SkIntToScalar(1)));
304SIMPLE_TEST_STEP(Skew, skew(SkIntToScalar(1), SkIntToScalar(2)));
piotaixrf05f5a72014-10-03 13:26:55 -0700305SIMPLE_TEST_STEP(Concat, concat(d.fMatrix));
306SIMPLE_TEST_STEP(SetMatrix, setMatrix(d.fMatrix));
307SIMPLE_TEST_STEP(ClipRect, clipRect(d.fRect));
308SIMPLE_TEST_STEP(ClipPath, clipPath(d.fPath));
Mike Reedc1f77742016-12-09 09:00:50 -0500309SIMPLE_TEST_STEP(ClipRegion, clipRegion(d.fRegion, kReplace_SkClipOp));
piotaixrf05f5a72014-10-03 13:26:55 -0700310SIMPLE_TEST_STEP(Clear, clear(d.fColor));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000311
312///////////////////////////////////////////////////////////////////////////////
313// Complex test steps
314
piotaixrf05f5a72014-10-03 13:26:55 -0700315static void SaveMatrixClipStep(SkCanvas* canvas, const TestData& d,
316 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000317 int saveCount = canvas->getSaveCount();
Florin Malita5f6102d2014-06-30 10:13:28 -0400318 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000319 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
piotaixrf05f5a72014-10-03 13:26:55 -0700320 canvas->clipRegion(d.fRegion);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000321 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000322 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000323 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000324 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000325 testStep->assertMessage());
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000326// REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion, testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000327}
328TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
329
piotaixrf05f5a72014-10-03 13:26:55 -0700330static void SaveLayerStep(SkCanvas* canvas, const TestData& d,
331 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000332 int saveCount = canvas->getSaveCount();
halcanary96fcdcc2015-08-27 07:41:13 -0700333 canvas->saveLayer(nullptr, nullptr);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000334 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000335 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000336 testStep->assertMessage());
337}
338TEST_STEP(SaveLayer, SaveLayerStep);
339
piotaixrf05f5a72014-10-03 13:26:55 -0700340static void BoundedSaveLayerStep(SkCanvas* canvas, const TestData& d,
341 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000342 int saveCount = canvas->getSaveCount();
halcanary96fcdcc2015-08-27 07:41:13 -0700343 canvas->saveLayer(&d.fRect, nullptr);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000344 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000345 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000346 testStep->assertMessage());
347}
348TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
349
piotaixrf05f5a72014-10-03 13:26:55 -0700350static void PaintSaveLayerStep(SkCanvas* canvas, const TestData& d,
351 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000352 int saveCount = canvas->getSaveCount();
halcanary96fcdcc2015-08-27 07:41:13 -0700353 canvas->saveLayer(nullptr, &d.fPaint);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000354 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000355 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000356 testStep->assertMessage());
357}
358TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
359
piotaixrf05f5a72014-10-03 13:26:55 -0700360static void TwoClipOpsStep(SkCanvas* canvas, const TestData& d,
361 skiatest::Reporter*, CanvasTestStep*) {
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000362 // This test exercises a functionality in SkPicture that leads to the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000363 // recording of restore offset placeholders. This test will trigger an
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000364 // assertion at playback time if the placeholders are not properly
365 // filled when the recording ends.
piotaixrf05f5a72014-10-03 13:26:55 -0700366 canvas->clipRect(d.fRect);
367 canvas->clipRegion(d.fRegion);
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000368}
369TEST_STEP(TwoClipOps, TwoClipOpsStep);
370
epoger@google.com94fa43c2012-04-11 17:51:01 +0000371// exercise fix for http://code.google.com/p/skia/issues/detail?id=560
372// ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
piotaixrf05f5a72014-10-03 13:26:55 -0700373static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas, const TestData& d,
374 skiatest::Reporter*, CanvasTestStep*) {
epoger@google.com94fa43c2012-04-11 17:51:01 +0000375 SkPaint paint;
376 paint.setStrokeWidth(SkIntToScalar(1));
377 paint.setStyle(SkPaint::kStroke_Style);
378
piotaixrf05f5a72014-10-03 13:26:55 -0700379 canvas->drawPath(d.fNearlyZeroLengthPath, paint);
epoger@google.com94fa43c2012-04-11 17:51:01 +0000380}
381TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
382
piotaixrf05f5a72014-10-03 13:26:55 -0700383static void DrawVerticesShaderTestStep(SkCanvas* canvas, const TestData& d,
384 skiatest::Reporter*, CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000385 SkPoint pts[4];
386 pts[0].set(0, 0);
piotaixrf05f5a72014-10-03 13:26:55 -0700387 pts[1].set(SkIntToScalar(d.fWidth), 0);
388 pts[2].set(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight));
389 pts[3].set(0, SkIntToScalar(d.fHeight));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000390 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -0700391 paint.setShader(SkShader::MakeBitmapShader(d.fBitmap, SkShader::kClamp_TileMode,
392 SkShader::kClamp_TileMode));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000393 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
Mike Reed7d954ad2016-10-28 15:42:34 -0400394 nullptr, SkBlendMode::kModulate, nullptr, 0, paint);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000395}
edisonn@google.com77909122012-10-18 15:58:23 +0000396// NYI: issue 240.
397TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000398
piotaixrf05f5a72014-10-03 13:26:55 -0700399static void DrawPictureTestStep(SkCanvas* canvas, const TestData& d,
400 skiatest::Reporter*, CanvasTestStep*) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000401 SkPictureRecorder recorder;
piotaixrf05f5a72014-10-03 13:26:55 -0700402 SkCanvas* testCanvas = recorder.beginRecording(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight),
halcanary96fcdcc2015-08-27 07:41:13 -0700403 nullptr, 0);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000404 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
piotaixrf05f5a72014-10-03 13:26:55 -0700405 testCanvas->clipRect(d.fRect);
406 testCanvas->drawRect(d.fRect, d.fPaint);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000407
reedca2622b2016-03-18 07:25:55 -0700408 canvas->drawPicture(recorder.finishRecordingAsPicture());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000409}
410TEST_STEP(DrawPicture, DrawPictureTestStep);
411
piotaixrf05f5a72014-10-03 13:26:55 -0700412static void SaveRestoreTestStep(SkCanvas* canvas, const TestData& d,
413 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000414 int baseSaveCount = canvas->getSaveCount();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000415 int n = canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000416 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount == n, testStep->assertMessage());
417 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000418 testStep->assertMessage());
419 canvas->save();
420 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000421 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000422 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000423 canvas->restoreToCount(baseSaveCount + 1);
424 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000425 testStep->assertMessage());
426
427 // should this pin to 1, or be a no-op, or crash?
428 canvas->restoreToCount(0);
429 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
430 testStep->assertMessage());
431}
432TEST_STEP(SaveRestore, SaveRestoreTestStep);
433
piotaixrf05f5a72014-10-03 13:26:55 -0700434static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas, const TestData& d,
435 skiatest::Reporter*, CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000436 // This test step challenges the TestDeferredCanvasStateConsistency
437 // test cases because the opaque paint can trigger an optimization
438 // that discards previously recorded commands. The challenge is to maintain
439 // correct clip and matrix stack state.
440 canvas->resetMatrix();
441 canvas->rotate(SkIntToScalar(30));
442 canvas->save();
443 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
444 canvas->save();
445 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
446 SkPaint paint;
447 paint.setColor(0xFFFFFFFF);
448 canvas->drawPaint(paint);
449 canvas->restore();
450 canvas->restore();
451}
452TEST_STEP(NestedSaveRestoreWithSolidPaint, \
453 NestedSaveRestoreWithSolidPaintTestStep);
454
piotaixrf05f5a72014-10-03 13:26:55 -0700455static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas, const TestData& d,
456 skiatest::Reporter*, CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000457 // This test step challenges the TestDeferredCanvasStateConsistency
458 // test case because the canvas flush on a deferred canvas will
459 // reset the recording session. The challenge is to maintain correct
460 // clip and matrix stack state on the playback canvas.
461 canvas->resetMatrix();
462 canvas->rotate(SkIntToScalar(30));
463 canvas->save();
464 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
465 canvas->save();
466 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
piotaixrf05f5a72014-10-03 13:26:55 -0700467 canvas->drawRect(d.fRect,d.fPaint);
reed@google.com3b3e8952012-08-16 20:53:31 +0000468 canvas->flush();
469 canvas->restore();
470 canvas->restore();
471}
piotaixrf05f5a72014-10-03 13:26:55 -0700472TEST_STEP(NestedSaveRestoreWithFlush, NestedSaveRestoreWithFlushTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000473
tomhudsoncb3bd182016-05-18 07:24:16 -0700474static void DescribeTopLayerTestStep(SkCanvas* canvas,
475 const TestData& d,
476 skiatest::Reporter* reporter,
477 CanvasTestStep* testStep) {
478 SkMatrix m;
479 SkIRect r;
480 // NOTE: adjustToTopLayer() does *not* reduce the clip size, even if the canvas
481 // is smaller than 10x10!
482
483 canvas->temporary_internal_describeTopLayer(&m, &r);
484 REPORTER_ASSERT_MESSAGE(reporter, m.isIdentity(), testStep->assertMessage());
485 REPORTER_ASSERT_MESSAGE(reporter, r == SkIRect::MakeXYWH(0, 0, 2, 2),
486 testStep->assertMessage());
487
488 // Putting a full-canvas layer on it should make no change to the results.
489 SkRect layerBounds = SkRect::MakeXYWH(0.f, 0.f, 10.f, 10.f);
490 canvas->saveLayer(layerBounds, nullptr);
491 canvas->temporary_internal_describeTopLayer(&m, &r);
492 REPORTER_ASSERT_MESSAGE(reporter, m.isIdentity(), testStep->assertMessage());
493 REPORTER_ASSERT_MESSAGE(reporter, r == SkIRect::MakeXYWH(0, 0, 2, 2),
494 testStep->assertMessage());
495 canvas->restore();
496
497 // Adding a translated layer translates the results.
498 // Default canvas is only 2x2, so can't offset our layer by very much at all;
499 // saveLayer() aborts if the bounds don't intersect.
500 layerBounds = SkRect::MakeXYWH(1.f, 1.f, 6.f, 6.f);
501 canvas->saveLayer(layerBounds, nullptr);
502 canvas->temporary_internal_describeTopLayer(&m, &r);
503 REPORTER_ASSERT_MESSAGE(reporter, m == SkMatrix::MakeTrans(-1.f, -1.f),
504 testStep->assertMessage());
505 REPORTER_ASSERT_MESSAGE(reporter, r == SkIRect::MakeXYWH(0, 0, 1, 1),
506 testStep->assertMessage());
507 canvas->restore();
508
509}
510TEST_STEP(DescribeTopLayer, DescribeTopLayerTestStep);
511
512
reed3aafe112016-08-18 12:45:34 -0700513static void TestPdfDevice(skiatest::Reporter* reporter, const TestData& d, CanvasTestStep* step) {
halcanary3d32d502015-03-01 06:55:20 -0800514 SkDynamicMemoryWStream outStream;
halcanary4b656662016-04-27 07:45:18 -0700515 sk_sp<SkDocument> doc(SkDocument::MakePDF(&outStream));
halcanary8ee06f22015-08-11 10:30:12 -0700516 REPORTER_ASSERT(reporter, doc);
halcanary2ccdb632015-08-11 13:35:12 -0700517 if (!doc) {
518 return;
519 }
halcanary3d32d502015-03-01 06:55:20 -0800520 SkCanvas* canvas = doc->beginPage(SkIntToScalar(d.fWidth),
521 SkIntToScalar(d.fHeight));
522 REPORTER_ASSERT(reporter, canvas);
reed3aafe112016-08-18 12:45:34 -0700523 step->setAssertMessageFormat(kPdfAssertMessageFormat);
524 step->draw(canvas, d, reporter);
edisonn@google.com77909122012-10-18 15:58:23 +0000525}
526
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000527/*
528 * This sub-test verifies that the test step passes when executed
529 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
530 * that the all canvas derivatives report the same state as an SkCanvas
531 * after having executed the test step.
532 */
piotaixrf05f5a72014-10-03 13:26:55 -0700533static void TestOverrideStateConsistency(skiatest::Reporter* reporter, const TestData& d,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000534 CanvasTestStep* testStep) {
535 SkBitmap referenceStore;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000536 createBitmap(&referenceStore, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700537 SkCanvas referenceCanvas(referenceStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000538 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700539 testStep->draw(&referenceCanvas, d, reporter);
reed@google.com7c202932011-12-14 18:48:05 +0000540}
reed@google.com37f3ae02011-11-28 16:06:04 +0000541
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000542static void test_newraster(skiatest::Reporter* reporter) {
543 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
reed3054be12014-12-10 07:24:28 -0800544 const size_t minRowBytes = info.minRowBytes();
545 const size_t size = info.getSafeSize(minRowBytes);
scroggo565901d2015-12-10 10:44:13 -0800546 SkAutoTMalloc<SkPMColor> storage(size);
547 SkPMColor* baseAddr = storage.get();
reed3054be12014-12-10 07:24:28 -0800548 sk_bzero(baseAddr, size);
549
Mike Reed5df49342016-11-12 08:06:55 -0600550 std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000551 REPORTER_ASSERT(reporter, canvas);
552
reed6ceeebd2016-03-09 14:26:26 -0800553 SkPixmap pmap;
554 const SkPMColor* addr = canvas->peekPixels(&pmap) ? pmap.addr32() : nullptr;
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000555 REPORTER_ASSERT(reporter, addr);
reed6ceeebd2016-03-09 14:26:26 -0800556 REPORTER_ASSERT(reporter, info == pmap.info());
557 REPORTER_ASSERT(reporter, minRowBytes == pmap.rowBytes());
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000558 for (int y = 0; y < info.height(); ++y) {
559 for (int x = 0; x < info.width(); ++x) {
560 REPORTER_ASSERT(reporter, 0 == addr[x]);
561 }
reed6ceeebd2016-03-09 14:26:26 -0800562 addr = (const SkPMColor*)((const char*)addr + pmap.rowBytes());
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000563 }
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000564
565 // now try a deliberately bad info
reede5ea5002014-09-03 11:54:58 -0700566 info = info.makeWH(-1, info.height());
Mike Reed5df49342016-11-12 08:06:55 -0600567 REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes));
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000568
569 // too big
reede5ea5002014-09-03 11:54:58 -0700570 info = info.makeWH(1 << 30, 1 << 30);
Mike Reed5df49342016-11-12 08:06:55 -0600571 REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes));
skia.committer@gmail.com0e530752014-02-28 03:02:05 +0000572
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000573 // not a valid pixel type
reede5ea5002014-09-03 11:54:58 -0700574 info = SkImageInfo::Make(10, 10, kUnknown_SkColorType, info.alphaType());
Mike Reed5df49342016-11-12 08:06:55 -0600575 REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes));
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000576
577 // We should succeed with a zero-sized valid info
578 info = SkImageInfo::MakeN32Premul(0, 0);
Mike Reed5df49342016-11-12 08:06:55 -0600579 canvas = SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000580 REPORTER_ASSERT(reporter, canvas);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000581}
582
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000583DEF_TEST(Canvas, reporter) {
piotaixrf05f5a72014-10-03 13:26:55 -0700584 TestData d;
reed@google.com37f3ae02011-11-28 16:06:04 +0000585
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000586 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
piotaixrf05f5a72014-10-03 13:26:55 -0700587 TestOverrideStateConsistency(reporter, d, testStepArray()[testStep]);
edisonn@google.com77909122012-10-18 15:58:23 +0000588 if (testStepArray()[testStep]->enablePdfTesting()) {
piotaixrf05f5a72014-10-03 13:26:55 -0700589 TestPdfDevice(reporter, d, testStepArray()[testStep]);
edisonn@google.com77909122012-10-18 15:58:23 +0000590 }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000591 }
junov@chromium.orgcd62ecf2012-08-02 17:43:25 +0000592
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000593 test_newraster(reporter);
reed@google.com37f3ae02011-11-28 16:06:04 +0000594}
reedf0090cb2014-11-26 08:55:51 -0800595
596DEF_TEST(Canvas_SaveState, reporter) {
597 SkCanvas canvas(10, 10);
598 REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
599
600 int n = canvas.save();
601 REPORTER_ASSERT(reporter, 1 == n);
602 REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
603
halcanary96fcdcc2015-08-27 07:41:13 -0700604 n = canvas.saveLayer(nullptr, nullptr);
reedf0090cb2014-11-26 08:55:51 -0800605 REPORTER_ASSERT(reporter, 2 == n);
606 REPORTER_ASSERT(reporter, 3 == canvas.getSaveCount());
halcanary9d524f22016-03-29 09:03:52 -0700607
reedf0090cb2014-11-26 08:55:51 -0800608 canvas.restore();
609 REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
610 canvas.restore();
611 REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
612}
reedc1b11f12015-03-13 08:48:26 -0700613
614DEF_TEST(Canvas_ClipEmptyPath, reporter) {
615 SkCanvas canvas(10, 10);
616 canvas.save();
617 SkPath path;
618 canvas.clipPath(path);
619 canvas.restore();
620 canvas.save();
621 path.moveTo(5, 5);
622 canvas.clipPath(path);
623 canvas.restore();
624 canvas.save();
625 path.moveTo(7, 7);
626 canvas.clipPath(path); // should not assert here
627 canvas.restore();
628}
fmalitaf433bb22015-08-17 08:05:13 -0700629
vjiaoblacke5de1302016-07-13 14:05:28 -0700630#define SHADOW_TEST_CANVAS_CONST 10
vjiaoblack95302da2016-07-21 10:25:54 -0700631#ifdef SK_EXPERIMENTAL_SHADOWING
vjiaoblacke5de1302016-07-13 14:05:28 -0700632class SkShadowTestCanvas : public SkPaintFilterCanvas {
633public:
634
635 SkShadowTestCanvas(int x, int y, skiatest::Reporter* reporter)
636 : INHERITED(x,y)
637 , fReporter(reporter) {}
638
639 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type type) const {
640 REPORTER_ASSERT(this->fReporter, this->getZ() == SHADOW_TEST_CANVAS_CONST);
641
642 return true;
643 }
644
645 void testUpdateDepth(skiatest::Reporter *reporter) {
646 // set some depths (with picture enabled), then check them as they get set
647
648 REPORTER_ASSERT(reporter, this->getZ() == 0);
649 this->translateZ(-10);
650 REPORTER_ASSERT(reporter, this->getZ() == -10);
651
652 this->save();
653 this->translateZ(20);
654 REPORTER_ASSERT(reporter, this->getZ() == 10);
655
656 this->restore();
657 REPORTER_ASSERT(reporter, this->getZ() == -10);
658
659 this->translateZ(13.14f);
660 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(this->getZ(), 3.14f));
661 }
662
663private:
664 skiatest::Reporter* fReporter;
665
666 typedef SkPaintFilterCanvas INHERITED;
667};
vjiaoblack95302da2016-07-21 10:25:54 -0700668#endif
vjiaoblacke5de1302016-07-13 14:05:28 -0700669
fmalitaf433bb22015-08-17 08:05:13 -0700670namespace {
671
672class MockFilterCanvas : public SkPaintFilterCanvas {
673public:
674 MockFilterCanvas(SkCanvas* canvas) : INHERITED(canvas) { }
675
676protected:
fmalita32cdc322016-01-12 07:21:11 -0800677 bool onFilter(SkTCopyOnFirstWrite<SkPaint>*, Type) const override { return true; }
fmalitaf433bb22015-08-17 08:05:13 -0700678
679private:
680 typedef SkPaintFilterCanvas INHERITED;
681};
682
683} // anonymous namespace
684
685// SkPaintFilterCanvas should inherit the initial target canvas state.
686DEF_TEST(PaintFilterCanvas_ConsistentState, reporter) {
687 SkCanvas canvas(100, 100);
688 canvas.clipRect(SkRect::MakeXYWH(12.7f, 12.7f, 75, 75));
689 canvas.scale(0.5f, 0.75f);
690
fmalitaf433bb22015-08-17 08:05:13 -0700691 MockFilterCanvas filterCanvas(&canvas);
692 REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix());
Mike Reed918e1442017-01-23 11:39:45 -0500693 REPORTER_ASSERT(reporter, canvas.getLocalClipBounds() == filterCanvas.getLocalClipBounds());
fmalitaf433bb22015-08-17 08:05:13 -0700694
695 filterCanvas.clipRect(SkRect::MakeXYWH(30.5f, 30.7f, 100, 100));
696 filterCanvas.scale(0.75f, 0.5f);
697 REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix());
Mike Reed918e1442017-01-23 11:39:45 -0500698 REPORTER_ASSERT(reporter, filterCanvas.getLocalClipBounds().contains(canvas.getLocalClipBounds()));
vjiaoblacke5de1302016-07-13 14:05:28 -0700699
vjiaoblack95302da2016-07-21 10:25:54 -0700700#ifdef SK_EXPERIMENTAL_SHADOWING
vjiaoblacke5de1302016-07-13 14:05:28 -0700701 SkShadowTestCanvas* tCanvas = new SkShadowTestCanvas(100,100, reporter);
702 tCanvas->testUpdateDepth(reporter);
703 delete(tCanvas);
704
705 SkPictureRecorder recorder;
706 SkShadowTestCanvas *tSCanvas = new SkShadowTestCanvas(100, 100, reporter);
707 SkCanvas *tPCanvas = recorder.beginRecording(SkRect::MakeIWH(100, 100));
708
709 tPCanvas->translateZ(SHADOW_TEST_CANVAS_CONST);
710 sk_sp<SkPicture> pic = recorder.finishRecordingAsPicture();
711 tSCanvas->drawPicture(pic);
712
713 delete(tSCanvas);
vjiaoblack95302da2016-07-21 10:25:54 -0700714#endif
fmalitaf433bb22015-08-17 08:05:13 -0700715}
reedbabc3de2016-07-08 08:43:27 -0700716
717///////////////////////////////////////////////////////////////////////////////////////////////////
718
719#include "SkDeferredCanvas.h"
720#include "SkDumpCanvas.h"
721
722DEF_TEST(DeferredCanvas, r) {
723 SkDebugfDumper dumper;
724 SkDumpCanvas dumpC(&dumper);
725
726 SkDeferredCanvas canvas(&dumpC);
727
728 SkPaint paint;
729// paint.setShader(SkShader::MakeColorShader(SK_ColorRED));
730
731 canvas.save();
732 canvas.clipRect(SkRect::MakeWH(55, 55));
733 canvas.translate(10, 20);
734 canvas.drawRect(SkRect::MakeWH(50, 50), paint);
735 canvas.restore();
736}
737
Mike Reed584ca892016-11-15 11:52:55 -0500738///////////////////////////////////////////////////////////////////////////////////////////////////
739
740#include "SkCanvasStack.h"
741#include "SkNWayCanvas.h"
742
743// Subclass that takes a bool*, which it updates in its construct (true) and destructor (false)
744// to allow the caller to know how long the object is alive.
745class LifeLineCanvas : public SkCanvas {
746 bool* fLifeLine;
747public:
748 LifeLineCanvas(int w, int h, bool* lifeline) : SkCanvas(w, h), fLifeLine(lifeline) {
749 *fLifeLine = true;
750 }
751 ~LifeLineCanvas() {
752 *fLifeLine = false;
753 }
754};
755
756// Check that NWayCanvas does NOT try to manage the lifetime of its sub-canvases
757DEF_TEST(NWayCanvas, r) {
758 const int w = 10;
759 const int h = 10;
760 bool life[2];
761 {
762 LifeLineCanvas c0(w, h, &life[0]);
763 REPORTER_ASSERT(r, life[0]);
764 }
765 REPORTER_ASSERT(r, !life[0]);
766
767
768 std::unique_ptr<SkCanvas> c0 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[0]));
769 std::unique_ptr<SkCanvas> c1 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[1]));
770 REPORTER_ASSERT(r, life[0]);
771 REPORTER_ASSERT(r, life[1]);
772
773 {
774 SkNWayCanvas nway(w, h);
775 nway.addCanvas(c0.get());
776 nway.addCanvas(c1.get());
777 REPORTER_ASSERT(r, life[0]);
778 REPORTER_ASSERT(r, life[1]);
779 }
780 // Now assert that the death of the nway has NOT also killed the sub-canvases
781 REPORTER_ASSERT(r, life[0]);
782 REPORTER_ASSERT(r, life[1]);
783}
784
785// Check that CanvasStack DOES manage the lifetime of its sub-canvases
786DEF_TEST(CanvasStack, r) {
787 const int w = 10;
788 const int h = 10;
789 bool life[2];
790 std::unique_ptr<SkCanvas> c0 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[0]));
791 std::unique_ptr<SkCanvas> c1 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[1]));
792 REPORTER_ASSERT(r, life[0]);
793 REPORTER_ASSERT(r, life[1]);
794
795 {
796 SkCanvasStack stack(w, h);
797 stack.pushCanvas(std::move(c0), {0,0});
798 stack.pushCanvas(std::move(c1), {0,0});
799 REPORTER_ASSERT(r, life[0]);
800 REPORTER_ASSERT(r, life[1]);
801 }
802 // Now assert that the death of the canvasstack has also killed the sub-canvases
803 REPORTER_ASSERT(r, !life[0]);
804 REPORTER_ASSERT(r, !life[1]);
805}