blob: 98cfbc158dcae6b959f19b36b7cd60c9fbcc612e [file] [log] [blame]
reed@google.com37f3ae02011-11-28 16:06:04 +00001/*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +00002 * Copyright 2012 Google Inc.
reed@google.com37f3ae02011-11-28 16:06:04 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +00007
8/* Description:
9 * This test defines a series of elementatry test steps that perform
10 * a single or a small group of canvas API calls. Each test step is
11 * used in several test cases that verify that different types of SkCanvas
12 * flavors and derivatives pass it and yield consistent behavior. The
13 * test cases analyse results that are queryable through the API. They do
14 * not look at rendering results.
15 *
16 * Adding test stepss:
17 * The general pattern for creating a new test step is to write a test
18 * function of the form:
19 *
rmistry@google.comd6176b02012-08-23 18:14:13 +000020 * static void MyTestStepFunction(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000021 * skiatest::Reporter* reporter,
22 * CanvasTestStep* testStep)
23 * {
24 * canvas->someCanvasAPImethod();
25 * (...)
26 * REPORTER_ASSERT_MESSAGE(reporter, (...), \
27 * testStep->assertMessage());
28 * }
29 *
30 * The definition of the test step function should be followed by an
31 * invocation of the TEST_STEP macro, which generates a class and
32 * instance for the test step:
33 *
34 * TEST_STEP(MyTestStep, MyTestStepFunction)
35 *
36 * There are also short hand macros for defining simple test steps
37 * in a single line of code. A simple test step is a one that is made
38 * of a single canvas API call.
39 *
40 * SIMPLE_TEST_STEP(MytestStep, someCanvasAPIMethod());
41 *
42 * There is another macro called SIMPLE_TEST_STEP_WITH_ASSERT that
43 * works the same way as SIMPLE_TEST_STEP, and additionally verifies
44 * that the invoked method returns a non-zero value.
45 */
reed@google.com37f3ae02011-11-28 16:06:04 +000046#include "SkBitmap.h"
47#include "SkCanvas.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000048#include "SkDeferredCanvas.h"
49#include "SkDevice.h"
halcanary3d32d502015-03-01 06:55:20 -080050#include "SkDocument.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000051#include "SkMatrix.h"
52#include "SkNWayCanvas.h"
53#include "SkPaint.h"
54#include "SkPath.h"
55#include "SkPicture.h"
56#include "SkPictureRecord.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000057#include "SkPictureRecorder.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000058#include "SkRect.h"
59#include "SkRegion.h"
60#include "SkShader.h"
61#include "SkStream.h"
reed@google.com28183b42014-02-04 15:34:10 +000062#include "SkSurface.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000063#include "SkTDArray.h"
64#include "Test.h"
reed@google.com37f3ae02011-11-28 16:06:04 +000065
piotaixrf05f5a72014-10-03 13:26:55 -070066static const int kWidth = 2, kHeight = 2;
67
68static void createBitmap(SkBitmap* bm, SkColor color) {
69 bm->allocN32Pixels(kWidth, kHeight);
70 bm->eraseColor(color);
71}
72
73static SkSurface* createSurface(SkColor color) {
reed3054be12014-12-10 07:24:28 -080074 SkSurface* surface = SkSurface::NewRasterN32Premul(kWidth, kHeight);
piotaixrf05f5a72014-10-03 13:26:55 -070075 surface->getCanvas()->clear(color);
76 return surface;
77}
78
79///////////////////////////////////////////////////////////////////////////////
80// Constants used by test steps
81const SkPoint kTestPoints[] = {
82 {SkIntToScalar(0), SkIntToScalar(0)},
83 {SkIntToScalar(2), SkIntToScalar(1)},
84 {SkIntToScalar(0), SkIntToScalar(2)}
85};
86const SkPoint kTestPoints2[] = {
87 { SkIntToScalar(0), SkIntToScalar(1) },
88 { SkIntToScalar(1), SkIntToScalar(1) },
89 { SkIntToScalar(2), SkIntToScalar(1) },
90 { SkIntToScalar(3), SkIntToScalar(1) },
91 { SkIntToScalar(4), SkIntToScalar(1) },
92 { SkIntToScalar(5), SkIntToScalar(1) },
93 { SkIntToScalar(6), SkIntToScalar(1) },
94 { SkIntToScalar(7), SkIntToScalar(1) },
95 { SkIntToScalar(8), SkIntToScalar(1) },
96 { SkIntToScalar(9), SkIntToScalar(1) },
97 { SkIntToScalar(10), SkIntToScalar(1) }
98};
99
100struct TestData {
101public:
102 TestData()
103 : fRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
104 SkIntToScalar(2), SkIntToScalar(1)))
105 , fMatrix(TestMatrix())
106 , fPath(TestPath())
107 , fNearlyZeroLengthPath(TestNearlyZeroLengthPath())
108 , fIRect(SkIRect::MakeXYWH(0, 0, 2, 1))
109 , fRegion(TestRegion())
110 , fColor(0x01020304)
111 , fPoints(kTestPoints)
112 , fPointCount(3)
113 , fWidth(2)
114 , fHeight(2)
115 , fText("Hello World")
116 , fPoints2(kTestPoints2)
117 , fBitmap(TestBitmap())
118 { }
119
120 SkRect fRect;
121 SkMatrix fMatrix;;
122 SkPath fPath;
123 SkPath fNearlyZeroLengthPath;
124 SkIRect fIRect;
125 SkRegion fRegion;
126 SkColor fColor;
127 SkPaint fPaint;
128 const SkPoint* fPoints;
129 size_t fPointCount;
130 int fWidth;
131 int fHeight;
132 SkString fText;
133 const SkPoint* fPoints2;
134 SkBitmap fBitmap;
135
136private:
137 static SkMatrix TestMatrix() {
138 SkMatrix matrix;
139 matrix.reset();
140 matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
141
142 return matrix;
143 }
144 static SkPath TestPath() {
145 SkPath path;
146 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
147 SkIntToScalar(2), SkIntToScalar(1)));
148 return path;
149 }
150 static SkPath TestNearlyZeroLengthPath() {
151 SkPath path;
152 SkPoint pt1 = { 0, 0 };
153 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
154 SkPoint pt3 = { SkIntToScalar(1), 0 };
155 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
156 path.moveTo(pt1);
157 path.lineTo(pt2);
158 path.lineTo(pt3);
159 path.lineTo(pt4);
160 return path;
161 }
162 static SkRegion TestRegion() {
163 SkRegion region;
164 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
165 region.setRect(rect);
166 return region;
167 }
168 static SkBitmap TestBitmap() {
169 SkBitmap bitmap;
170 createBitmap(&bitmap, 0x05060708);
171 return bitmap;
172 }
173};
174
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000175static bool equal_clips(const SkCanvas& a, const SkCanvas& b) {
176 if (a.isClipEmpty()) {
177 return b.isClipEmpty();
178 }
179 if (!a.isClipRect()) {
180 // this is liberally true, since we don't expose a way to know this exactly (for non-rects)
181 return !b.isClipRect();
182 }
183 SkIRect ar, br;
184 a.getClipDeviceBounds(&ar);
185 b.getClipDeviceBounds(&br);
186 return ar == br;
187}
188
reed@google.com90c07ea2012-04-13 13:50:27 +0000189class Canvas2CanvasClipVisitor : public SkCanvas::ClipVisitor {
190public:
191 Canvas2CanvasClipVisitor(SkCanvas* target) : fTarget(target) {}
192
mtklein72c9faa2015-01-09 10:06:39 -0800193 void clipRect(const SkRect& r, SkRegion::Op op, bool aa) SK_OVERRIDE {
reed@google.com90c07ea2012-04-13 13:50:27 +0000194 fTarget->clipRect(r, op, aa);
195 }
mtklein72c9faa2015-01-09 10:06:39 -0800196 void clipRRect(const SkRRect& r, SkRegion::Op op, bool aa) SK_OVERRIDE {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000197 fTarget->clipRRect(r, op, aa);
198 }
mtklein72c9faa2015-01-09 10:06:39 -0800199 void clipPath(const SkPath& p, SkRegion::Op op, bool aa) SK_OVERRIDE {
reed@google.com90c07ea2012-04-13 13:50:27 +0000200 fTarget->clipPath(p, op, aa);
201 }
202
203private:
204 SkCanvas* fTarget;
205};
206
207static void test_clipVisitor(skiatest::Reporter* reporter, SkCanvas* canvas) {
208 SkISize size = canvas->getDeviceSize();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000209
reed@google.com90c07ea2012-04-13 13:50:27 +0000210 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000211 bm.setInfo(SkImageInfo::MakeN32Premul(size.width(), size.height()));
reed@google.com90c07ea2012-04-13 13:50:27 +0000212 SkCanvas c(bm);
213
214 Canvas2CanvasClipVisitor visitor(&c);
215 canvas->replayClips(&visitor);
216
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000217 REPORTER_ASSERT(reporter, equal_clips(c, *canvas));
reed@google.com90c07ea2012-04-13 13:50:27 +0000218}
219
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000220// Format strings that describe the test context. The %s token is where
221// the name of the test step is inserted. The context is required for
222// disambiguating the error in the case of failures that are reported in
223// functions that are called multiple times in different contexts (test
224// cases and test steps).
225static const char* const kDefaultAssertMessageFormat = "%s";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000226static const char* const kCanvasDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000227 "Drawing test step %s with SkCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000228static const char* const kDeferredDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000229 "Drawing test step %s with SkDeferredCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000230static const char* const kNWayDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000231 "Drawing test step %s with SkNWayCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000232static const char* const kDeferredPreFlushAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000233 "test step %s, SkDeferredCanvas state consistency before flush";
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000234static const char* const kDeferredPostFlushPlaybackAssertMessageFormat =
235 "test step %s, SkDeferredCanvas playback canvas state consistency after flush";
junov@chromium.orgfb103892012-09-20 19:35:43 +0000236static const char* const kDeferredPostSilentFlushPlaybackAssertMessageFormat =
237 "test step %s, SkDeferredCanvas playback canvas state consistency after silent flush";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000238static const char* const kNWayStateAssertMessageFormat =
239 "test step %s, SkNWayCanvas state consistency";
240static const char* const kNWayIndirect1StateAssertMessageFormat =
241 "test step %s, SkNWayCanvas indirect canvas 1 state consistency";
242static const char* const kNWayIndirect2StateAssertMessageFormat =
243 "test step %s, SkNWayCanvas indirect canvas 2 state consistency";
edisonn@google.com77909122012-10-18 15:58:23 +0000244static const char* const kPdfAssertMessageFormat =
245 "PDF sanity check failed %s";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000246
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000247class CanvasTestStep;
248static SkTDArray<CanvasTestStep*>& testStepArray() {
249 static SkTDArray<CanvasTestStep*> theTests;
250 return theTests;
251}
252
253class CanvasTestStep {
254public:
edisonn@google.com77909122012-10-18 15:58:23 +0000255 CanvasTestStep(bool fEnablePdfTesting = true) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000256 *testStepArray().append() = this;
257 fAssertMessageFormat = kDefaultAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000258 this->fEnablePdfTesting = fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000259 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000260 virtual ~CanvasTestStep() { }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000261
piotaixrf05f5a72014-10-03 13:26:55 -0700262 virtual void draw(SkCanvas*, const TestData&, skiatest::Reporter*) = 0;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000263 virtual const char* name() const = 0;
264
265 const char* assertMessage() {
266 fAssertMessage.printf(fAssertMessageFormat, name());
267 return fAssertMessage.c_str();
268 }
269
270 void setAssertMessageFormat(const char* format) {
271 fAssertMessageFormat = format;
272 }
273
edisonn@google.com77909122012-10-18 15:58:23 +0000274 bool enablePdfTesting() { return fEnablePdfTesting; }
275
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000276private:
277 SkString fAssertMessage;
278 const char* fAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000279 bool fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000280};
281
282///////////////////////////////////////////////////////////////////////////////
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000283// Macros for defining test steps
284
285#define TEST_STEP(NAME, FUNCTION) \
286class NAME##_TestStep : public CanvasTestStep{ \
287public: \
piotaixrf05f5a72014-10-03 13:26:55 -0700288 virtual void draw(SkCanvas* canvas, const TestData& d, \
289 skiatest::Reporter* reporter) { \
290 FUNCTION (canvas, d, reporter, this); \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000291 } \
292 virtual const char* name() const {return #NAME ;} \
293}; \
294static NAME##_TestStep NAME##_TestStepInstance;
295
piotaixrf05f5a72014-10-03 13:26:55 -0700296#define TEST_STEP_NO_PDF(NAME, FUNCTION) \
edisonn@google.com77909122012-10-18 15:58:23 +0000297class NAME##_TestStep : public CanvasTestStep{ \
298public: \
299 NAME##_TestStep() : CanvasTestStep(false) {} \
piotaixrf05f5a72014-10-03 13:26:55 -0700300 virtual void draw(SkCanvas* canvas, const TestData& d, \
301 skiatest::Reporter* reporter) { \
302 FUNCTION (canvas, d, reporter, this); \
edisonn@google.com77909122012-10-18 15:58:23 +0000303 } \
304 virtual const char* name() const {return #NAME ;} \
305}; \
306static NAME##_TestStep NAME##_TestStepInstance;
307
piotaixrf05f5a72014-10-03 13:26:55 -0700308#define SIMPLE_TEST_STEP(NAME, CALL) \
309static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \
310 skiatest::Reporter*, CanvasTestStep*) { \
311 canvas-> CALL ; \
312} \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000313TEST_STEP(NAME, NAME##TestStep )
314
315#define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
piotaixrf05f5a72014-10-03 13:26:55 -0700316static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \
317 skiatest::Reporter*, CanvasTestStep* testStep) { \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000318 REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \
319 testStep->assertMessage()); \
320} \
321TEST_STEP(NAME, NAME##TestStep )
322
323
324///////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000325// Basic test steps for most virtual methods in SkCanvas that draw or affect
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000326// the state of the canvas.
327
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000328SIMPLE_TEST_STEP(Translate, translate(SkIntToScalar(1), SkIntToScalar(2)));
329SIMPLE_TEST_STEP(Scale, scale(SkIntToScalar(1), SkIntToScalar(2)));
330SIMPLE_TEST_STEP(Rotate, rotate(SkIntToScalar(1)));
331SIMPLE_TEST_STEP(Skew, skew(SkIntToScalar(1), SkIntToScalar(2)));
piotaixrf05f5a72014-10-03 13:26:55 -0700332SIMPLE_TEST_STEP(Concat, concat(d.fMatrix));
333SIMPLE_TEST_STEP(SetMatrix, setMatrix(d.fMatrix));
334SIMPLE_TEST_STEP(ClipRect, clipRect(d.fRect));
335SIMPLE_TEST_STEP(ClipPath, clipPath(d.fPath));
336SIMPLE_TEST_STEP(ClipRegion, clipRegion(d.fRegion, SkRegion::kReplace_Op));
337SIMPLE_TEST_STEP(Clear, clear(d.fColor));
piotaixrf05f5a72014-10-03 13:26:55 -0700338SIMPLE_TEST_STEP(BeginGroup, beginCommentGroup(d.fText.c_str()));
339SIMPLE_TEST_STEP(AddComment, addComment(d.fText.c_str(), d.fText.c_str()));
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000340SIMPLE_TEST_STEP(EndGroup, endCommentGroup());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000341
342///////////////////////////////////////////////////////////////////////////////
343// Complex test steps
344
piotaixrf05f5a72014-10-03 13:26:55 -0700345static void SaveMatrixClipStep(SkCanvas* canvas, const TestData& d,
346 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000347 int saveCount = canvas->getSaveCount();
Florin Malita5f6102d2014-06-30 10:13:28 -0400348 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000349 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
piotaixrf05f5a72014-10-03 13:26:55 -0700350 canvas->clipRegion(d.fRegion);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000351 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000352 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000353 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000354 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000355 testStep->assertMessage());
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000356// REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion, testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000357}
358TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
359
piotaixrf05f5a72014-10-03 13:26:55 -0700360static void SaveLayerStep(SkCanvas* canvas, const TestData& d,
361 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000362 int saveCount = canvas->getSaveCount();
363 canvas->saveLayer(NULL, NULL);
364 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000365 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000366 testStep->assertMessage());
367}
368TEST_STEP(SaveLayer, SaveLayerStep);
369
piotaixrf05f5a72014-10-03 13:26:55 -0700370static void BoundedSaveLayerStep(SkCanvas* canvas, const TestData& d,
371 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000372 int saveCount = canvas->getSaveCount();
piotaixrf05f5a72014-10-03 13:26:55 -0700373 canvas->saveLayer(&d.fRect, NULL);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000374 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000375 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000376 testStep->assertMessage());
377}
378TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
379
piotaixrf05f5a72014-10-03 13:26:55 -0700380static void PaintSaveLayerStep(SkCanvas* canvas, const TestData& d,
381 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000382 int saveCount = canvas->getSaveCount();
piotaixrf05f5a72014-10-03 13:26:55 -0700383 canvas->saveLayer(NULL, &d.fPaint);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000384 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000385 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000386 testStep->assertMessage());
387}
388TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
389
piotaixrf05f5a72014-10-03 13:26:55 -0700390static void TwoClipOpsStep(SkCanvas* canvas, const TestData& d,
391 skiatest::Reporter*, CanvasTestStep*) {
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000392 // This test exercises a functionality in SkPicture that leads to the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000393 // recording of restore offset placeholders. This test will trigger an
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000394 // assertion at playback time if the placeholders are not properly
395 // filled when the recording ends.
piotaixrf05f5a72014-10-03 13:26:55 -0700396 canvas->clipRect(d.fRect);
397 canvas->clipRegion(d.fRegion);
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000398}
399TEST_STEP(TwoClipOps, TwoClipOpsStep);
400
epoger@google.com94fa43c2012-04-11 17:51:01 +0000401// exercise fix for http://code.google.com/p/skia/issues/detail?id=560
402// ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
piotaixrf05f5a72014-10-03 13:26:55 -0700403static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas, const TestData& d,
404 skiatest::Reporter*, CanvasTestStep*) {
epoger@google.com94fa43c2012-04-11 17:51:01 +0000405 SkPaint paint;
406 paint.setStrokeWidth(SkIntToScalar(1));
407 paint.setStyle(SkPaint::kStroke_Style);
408
piotaixrf05f5a72014-10-03 13:26:55 -0700409 canvas->drawPath(d.fNearlyZeroLengthPath, paint);
epoger@google.com94fa43c2012-04-11 17:51:01 +0000410}
411TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
412
piotaixrf05f5a72014-10-03 13:26:55 -0700413static void DrawVerticesShaderTestStep(SkCanvas* canvas, const TestData& d,
414 skiatest::Reporter*, CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000415 SkPoint pts[4];
416 pts[0].set(0, 0);
piotaixrf05f5a72014-10-03 13:26:55 -0700417 pts[1].set(SkIntToScalar(d.fWidth), 0);
418 pts[2].set(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight));
419 pts[3].set(0, SkIntToScalar(d.fHeight));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000420 SkPaint paint;
piotaixrf05f5a72014-10-03 13:26:55 -0700421 SkShader* shader = SkShader::CreateBitmapShader(d.fBitmap,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000422 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
423 paint.setShader(shader)->unref();
424 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
425 NULL, NULL, NULL, 0, paint);
426}
edisonn@google.com77909122012-10-18 15:58:23 +0000427// NYI: issue 240.
428TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000429
piotaixrf05f5a72014-10-03 13:26:55 -0700430static void DrawPictureTestStep(SkCanvas* canvas, const TestData& d,
431 skiatest::Reporter*, CanvasTestStep*) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000432 SkPictureRecorder recorder;
piotaixrf05f5a72014-10-03 13:26:55 -0700433 SkCanvas* testCanvas = recorder.beginRecording(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight),
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700434 NULL, 0);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000435 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
piotaixrf05f5a72014-10-03 13:26:55 -0700436 testCanvas->clipRect(d.fRect);
437 testCanvas->drawRect(d.fRect, d.fPaint);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000438 SkAutoTUnref<SkPicture> testPicture(recorder.endRecording());
439
robertphillips9b14f262014-06-04 05:40:44 -0700440 canvas->drawPicture(testPicture);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000441}
442TEST_STEP(DrawPicture, DrawPictureTestStep);
443
piotaixrf05f5a72014-10-03 13:26:55 -0700444static void SaveRestoreTestStep(SkCanvas* canvas, const TestData& d,
445 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000446 int baseSaveCount = canvas->getSaveCount();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000447 int n = canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000448 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount == n, testStep->assertMessage());
449 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000450 testStep->assertMessage());
451 canvas->save();
452 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000453 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000454 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000455 canvas->restoreToCount(baseSaveCount + 1);
456 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000457 testStep->assertMessage());
458
459 // should this pin to 1, or be a no-op, or crash?
460 canvas->restoreToCount(0);
461 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
462 testStep->assertMessage());
463}
464TEST_STEP(SaveRestore, SaveRestoreTestStep);
465
piotaixrf05f5a72014-10-03 13:26:55 -0700466static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas, const TestData& d,
467 skiatest::Reporter*, CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000468 // This test step challenges the TestDeferredCanvasStateConsistency
469 // test cases because the opaque paint can trigger an optimization
470 // that discards previously recorded commands. The challenge is to maintain
471 // correct clip and matrix stack state.
472 canvas->resetMatrix();
473 canvas->rotate(SkIntToScalar(30));
474 canvas->save();
475 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
476 canvas->save();
477 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
478 SkPaint paint;
479 paint.setColor(0xFFFFFFFF);
480 canvas->drawPaint(paint);
481 canvas->restore();
482 canvas->restore();
483}
484TEST_STEP(NestedSaveRestoreWithSolidPaint, \
485 NestedSaveRestoreWithSolidPaintTestStep);
486
piotaixrf05f5a72014-10-03 13:26:55 -0700487static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas, const TestData& d,
488 skiatest::Reporter*, CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000489 // This test step challenges the TestDeferredCanvasStateConsistency
490 // test case because the canvas flush on a deferred canvas will
491 // reset the recording session. The challenge is to maintain correct
492 // clip and matrix stack state on the playback canvas.
493 canvas->resetMatrix();
494 canvas->rotate(SkIntToScalar(30));
495 canvas->save();
496 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
497 canvas->save();
498 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
piotaixrf05f5a72014-10-03 13:26:55 -0700499 canvas->drawRect(d.fRect,d.fPaint);
reed@google.com3b3e8952012-08-16 20:53:31 +0000500 canvas->flush();
501 canvas->restore();
502 canvas->restore();
503}
piotaixrf05f5a72014-10-03 13:26:55 -0700504TEST_STEP(NestedSaveRestoreWithFlush, NestedSaveRestoreWithFlushTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000505
piotaixrf05f5a72014-10-03 13:26:55 -0700506static void AssertCanvasStatesEqual(skiatest::Reporter* reporter, const TestData& d,
507 const SkCanvas* canvas1, const SkCanvas* canvas2,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000508 CanvasTestStep* testStep) {
509 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
510 canvas2->getDeviceSize(), testStep->assertMessage());
511 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
512 canvas2->getSaveCount(), testStep->assertMessage());
reed@google.com3b3e8952012-08-16 20:53:31 +0000513
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000514 SkRect bounds1, bounds2;
515 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.com3b3e8952012-08-16 20:53:31 +0000516 canvas1->getClipBounds(&bounds1) == canvas2->getClipBounds(&bounds2),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000517 testStep->assertMessage());
518 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
reed@google.com3b3e8952012-08-16 20:53:31 +0000519 testStep->assertMessage());
520
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000521 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
522 canvas2->getDrawFilter(), testStep->assertMessage());
523 SkIRect deviceBounds1, deviceBounds2;
524 REPORTER_ASSERT_MESSAGE(reporter,
525 canvas1->getClipDeviceBounds(&deviceBounds1) ==
526 canvas2->getClipDeviceBounds(&deviceBounds2),
527 testStep->assertMessage());
reed868074b2014-06-03 10:53:59 -0700528 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2, testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000529 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
530 canvas2->getTotalMatrix(), testStep->assertMessage());
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000531 REPORTER_ASSERT_MESSAGE(reporter, equal_clips(*canvas1, *canvas2), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000532
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000533 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
534 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
535 while (!layerIter1.done() && !layerIter2.done()) {
536 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
537 layerIter2.matrix(), testStep->assertMessage());
538 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
539 layerIter2.clip(), testStep->assertMessage());
540 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
541 layerIter2.paint(), testStep->assertMessage());
542 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
543 layerIter2.x(), testStep->assertMessage());
544 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
545 layerIter2.y(), testStep->assertMessage());
546 layerIter1.next();
547 layerIter2.next();
548 }
549 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
550 testStep->assertMessage());
551 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
552 testStep->assertMessage());
piotaixr76993ed2014-10-27 15:31:34 -0700553
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000554}
555
edisonn@google.com77909122012-10-18 15:58:23 +0000556static void TestPdfDevice(skiatest::Reporter* reporter,
piotaixrf05f5a72014-10-03 13:26:55 -0700557 const TestData& d,
edisonn@google.com77909122012-10-18 15:58:23 +0000558 CanvasTestStep* testStep) {
halcanary3d32d502015-03-01 06:55:20 -0800559 SkDynamicMemoryWStream outStream;
560 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&outStream));
561 SkCanvas* canvas = doc->beginPage(SkIntToScalar(d.fWidth),
562 SkIntToScalar(d.fHeight));
563 REPORTER_ASSERT(reporter, canvas);
edisonn@google.com77909122012-10-18 15:58:23 +0000564 testStep->setAssertMessageFormat(kPdfAssertMessageFormat);
halcanary3d32d502015-03-01 06:55:20 -0800565 testStep->draw(canvas, d, reporter);
566
567 REPORTER_ASSERT(reporter, doc->close());
edisonn@google.com77909122012-10-18 15:58:23 +0000568}
569
junov@chromium.org88e29142012-08-07 16:48:22 +0000570// The following class groups static functions that need to access
571// the privates members of SkDeferredCanvas
572class SkDeferredCanvasTester {
573public:
574 static void TestDeferredCanvasStateConsistency(
575 skiatest::Reporter* reporter,
piotaixrf05f5a72014-10-03 13:26:55 -0700576 const TestData& d,
junov@chromium.org88e29142012-08-07 16:48:22 +0000577 CanvasTestStep* testStep,
junov@chromium.orgfb103892012-09-20 19:35:43 +0000578 const SkCanvas& referenceCanvas, bool silent) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000579
reed@google.com28183b42014-02-04 15:34:10 +0000580 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
581 SkAutoTUnref<SkDeferredCanvas> deferredCanvas(SkDeferredCanvas::Create(surface.get()));
582
junov@chromium.org88e29142012-08-07 16:48:22 +0000583 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700584 testStep->draw(deferredCanvas, d, reporter);
junov@chromium.org88e29142012-08-07 16:48:22 +0000585 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700586 AssertCanvasStatesEqual(reporter, d, deferredCanvas, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000587
junov@chromium.orgfb103892012-09-20 19:35:43 +0000588 if (silent) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000589 deferredCanvas->silentFlush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000590 } else {
junov@chromium.org66070a52013-05-28 17:39:08 +0000591 deferredCanvas->flush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000592 }
593
skia.committer@gmail.com4c5ea442012-09-21 02:01:01 +0000594 testStep->setAssertMessageFormat(
junov@chromium.orgfb103892012-09-20 19:35:43 +0000595 silent ? kDeferredPostSilentFlushPlaybackAssertMessageFormat :
junov@chromium.org88e29142012-08-07 16:48:22 +0000596 kDeferredPostFlushPlaybackAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700597 AssertCanvasStatesEqual(reporter, d, deferredCanvas->immediateCanvas(),
598 &referenceCanvas, testStep);
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000599
junov@chromium.org88e29142012-08-07 16:48:22 +0000600 // Verified that deferred canvas state is not affected by flushing
601 // pending draw operations
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000602
junov@chromium.org88e29142012-08-07 16:48:22 +0000603 // The following test code is commented out because it currently fails.
604 // Issue: http://code.google.com/p/skia/issues/detail?id=496
605 /*
606 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
607 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
608 testStep);
609 */
610 }
611};
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000612
caryclark@google.com42639cd2012-06-06 12:03:39 +0000613// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000614static void TestNWayCanvasStateConsistency(
615 skiatest::Reporter* reporter,
piotaixrf05f5a72014-10-03 13:26:55 -0700616 const TestData& d,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000617 CanvasTestStep* testStep,
618 const SkCanvas& referenceCanvas) {
619
620 SkBitmap indirectStore1;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000621 createBitmap(&indirectStore1, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700622 SkCanvas indirectCanvas1(indirectStore1);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000623
624 SkBitmap indirectStore2;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000625 createBitmap(&indirectStore2, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700626 SkCanvas indirectCanvas2(indirectStore2);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000627
djsollen@google.comf0a062b2012-05-01 16:50:25 +0000628 SkISize canvasSize = referenceCanvas.getDeviceSize();
629 SkNWayCanvas nWayCanvas(canvasSize.width(), canvasSize.height());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000630 nWayCanvas.addCanvas(&indirectCanvas1);
631 nWayCanvas.addCanvas(&indirectCanvas2);
632
633 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700634 testStep->draw(&nWayCanvas, d, reporter);
scroggo648238c2015-01-29 11:58:51 -0800635 // Verify that the SkNWayCanvas reports consitent state
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000636 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700637 AssertCanvasStatesEqual(reporter, d, &nWayCanvas, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000638 // Verify that the indirect canvases report consitent state
639 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700640 AssertCanvasStatesEqual(reporter, d, &indirectCanvas1, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000641 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700642 AssertCanvasStatesEqual(reporter, d, &indirectCanvas2, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000643}
644
645/*
646 * This sub-test verifies that the test step passes when executed
647 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
648 * that the all canvas derivatives report the same state as an SkCanvas
649 * after having executed the test step.
650 */
piotaixrf05f5a72014-10-03 13:26:55 -0700651static void TestOverrideStateConsistency(skiatest::Reporter* reporter, const TestData& d,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000652 CanvasTestStep* testStep) {
653 SkBitmap referenceStore;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000654 createBitmap(&referenceStore, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700655 SkCanvas referenceCanvas(referenceStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000656 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700657 testStep->draw(&referenceCanvas, d, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000658
piotaixrf05f5a72014-10-03 13:26:55 -0700659 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, d, testStep, referenceCanvas, false);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000660
piotaixrf05f5a72014-10-03 13:26:55 -0700661 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, d, testStep, referenceCanvas, true);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000662
caryclark@google.com42639cd2012-06-06 12:03:39 +0000663 // The following test code is disabled because SkNWayCanvas does not
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000664 // report correct clipping and device bounds information
665 // Issue: http://code.google.com/p/skia/issues/detail?id=501
caryclark@google.com42639cd2012-06-06 12:03:39 +0000666
667 if (false) { // avoid bit rot, suppress warning
piotaixrf05f5a72014-10-03 13:26:55 -0700668 TestNWayCanvasStateConsistency(reporter, d, testStep, referenceCanvas);
caryclark@google.com42639cd2012-06-06 12:03:39 +0000669 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000670
caryclark@google.com42639cd2012-06-06 12:03:39 +0000671 if (false) { // avoid bit rot, suppress warning
672 test_clipVisitor(reporter, &referenceCanvas);
673 }
reed@google.com7c202932011-12-14 18:48:05 +0000674}
reed@google.com37f3ae02011-11-28 16:06:04 +0000675
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000676static void test_newraster(skiatest::Reporter* reporter) {
677 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
reed3054be12014-12-10 07:24:28 -0800678 const size_t minRowBytes = info.minRowBytes();
679 const size_t size = info.getSafeSize(minRowBytes);
680 SkAutoMalloc storage(size);
681 SkPMColor* baseAddr = static_cast<SkPMColor*>(storage.get());
682 sk_bzero(baseAddr, size);
683
684 SkCanvas* canvas = SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000685 REPORTER_ASSERT(reporter, canvas);
686
687 SkImageInfo info2;
688 size_t rowBytes;
689 const SkPMColor* addr = (const SkPMColor*)canvas->peekPixels(&info2, &rowBytes);
690 REPORTER_ASSERT(reporter, addr);
691 REPORTER_ASSERT(reporter, info == info2);
reed3054be12014-12-10 07:24:28 -0800692 REPORTER_ASSERT(reporter, minRowBytes == rowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000693 for (int y = 0; y < info.height(); ++y) {
694 for (int x = 0; x < info.width(); ++x) {
695 REPORTER_ASSERT(reporter, 0 == addr[x]);
696 }
697 addr = (const SkPMColor*)((const char*)addr + rowBytes);
698 }
699 SkDELETE(canvas);
700
701 // now try a deliberately bad info
reede5ea5002014-09-03 11:54:58 -0700702 info = info.makeWH(-1, info.height());
reed3054be12014-12-10 07:24:28 -0800703 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000704
705 // too big
reede5ea5002014-09-03 11:54:58 -0700706 info = info.makeWH(1 << 30, 1 << 30);
reed3054be12014-12-10 07:24:28 -0800707 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
skia.committer@gmail.com0e530752014-02-28 03:02:05 +0000708
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000709 // not a valid pixel type
reede5ea5002014-09-03 11:54:58 -0700710 info = SkImageInfo::Make(10, 10, kUnknown_SkColorType, info.alphaType());
reed3054be12014-12-10 07:24:28 -0800711 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000712
713 // We should succeed with a zero-sized valid info
714 info = SkImageInfo::MakeN32Premul(0, 0);
reed3054be12014-12-10 07:24:28 -0800715 canvas = SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000716 REPORTER_ASSERT(reporter, canvas);
717 SkDELETE(canvas);
718}
719
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000720DEF_TEST(Canvas, reporter) {
piotaixrf05f5a72014-10-03 13:26:55 -0700721 TestData d;
reed@google.com37f3ae02011-11-28 16:06:04 +0000722
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000723 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
piotaixrf05f5a72014-10-03 13:26:55 -0700724 TestOverrideStateConsistency(reporter, d, testStepArray()[testStep]);
edisonn@google.com77909122012-10-18 15:58:23 +0000725 if (testStepArray()[testStep]->enablePdfTesting()) {
piotaixrf05f5a72014-10-03 13:26:55 -0700726 TestPdfDevice(reporter, d, testStepArray()[testStep]);
edisonn@google.com77909122012-10-18 15:58:23 +0000727 }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000728 }
junov@chromium.orgcd62ecf2012-08-02 17:43:25 +0000729
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000730 test_newraster(reporter);
reed@google.com37f3ae02011-11-28 16:06:04 +0000731}
reedf0090cb2014-11-26 08:55:51 -0800732
733DEF_TEST(Canvas_SaveState, reporter) {
734 SkCanvas canvas(10, 10);
735 REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
736
737 int n = canvas.save();
738 REPORTER_ASSERT(reporter, 1 == n);
739 REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
740
741 n = canvas.saveLayer(NULL, NULL);
742 REPORTER_ASSERT(reporter, 2 == n);
743 REPORTER_ASSERT(reporter, 3 == canvas.getSaveCount());
744
745 canvas.restore();
746 REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
747 canvas.restore();
748 REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
749}