blob: c405fcd52c59c7ce51edaa544c0b0e7d29d4d438 [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"
50#include "SkMatrix.h"
51#include "SkNWayCanvas.h"
halcanarya1f1ee92015-02-20 06:17:26 -080052#include "SkPDFCanon.h"
edisonn@google.com77909122012-10-18 15:58:23 +000053#include "SkPDFDevice.h"
54#include "SkPDFDocument.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000055#include "SkPaint.h"
56#include "SkPath.h"
57#include "SkPicture.h"
58#include "SkPictureRecord.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000059#include "SkPictureRecorder.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"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000065#include "SkTDArray.h"
66#include "Test.h"
reed@google.com37f3ae02011-11-28 16:06:04 +000067
piotaixrf05f5a72014-10-03 13:26:55 -070068static const int kWidth = 2, kHeight = 2;
69
70static void createBitmap(SkBitmap* bm, SkColor color) {
71 bm->allocN32Pixels(kWidth, kHeight);
72 bm->eraseColor(color);
73}
74
75static SkSurface* createSurface(SkColor color) {
reed3054be12014-12-10 07:24:28 -080076 SkSurface* surface = SkSurface::NewRasterN32Premul(kWidth, kHeight);
piotaixrf05f5a72014-10-03 13:26:55 -070077 surface->getCanvas()->clear(color);
78 return surface;
79}
80
81///////////////////////////////////////////////////////////////////////////////
82// Constants used by test steps
83const SkPoint kTestPoints[] = {
84 {SkIntToScalar(0), SkIntToScalar(0)},
85 {SkIntToScalar(2), SkIntToScalar(1)},
86 {SkIntToScalar(0), SkIntToScalar(2)}
87};
88const SkPoint kTestPoints2[] = {
89 { SkIntToScalar(0), SkIntToScalar(1) },
90 { SkIntToScalar(1), SkIntToScalar(1) },
91 { SkIntToScalar(2), SkIntToScalar(1) },
92 { SkIntToScalar(3), SkIntToScalar(1) },
93 { SkIntToScalar(4), SkIntToScalar(1) },
94 { SkIntToScalar(5), SkIntToScalar(1) },
95 { SkIntToScalar(6), SkIntToScalar(1) },
96 { SkIntToScalar(7), SkIntToScalar(1) },
97 { SkIntToScalar(8), SkIntToScalar(1) },
98 { SkIntToScalar(9), SkIntToScalar(1) },
99 { SkIntToScalar(10), SkIntToScalar(1) }
100};
101
102struct TestData {
103public:
104 TestData()
105 : fRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
106 SkIntToScalar(2), SkIntToScalar(1)))
107 , fMatrix(TestMatrix())
108 , fPath(TestPath())
109 , fNearlyZeroLengthPath(TestNearlyZeroLengthPath())
110 , fIRect(SkIRect::MakeXYWH(0, 0, 2, 1))
111 , fRegion(TestRegion())
112 , fColor(0x01020304)
113 , fPoints(kTestPoints)
114 , fPointCount(3)
115 , fWidth(2)
116 , fHeight(2)
117 , fText("Hello World")
118 , fPoints2(kTestPoints2)
119 , fBitmap(TestBitmap())
120 { }
121
122 SkRect fRect;
123 SkMatrix fMatrix;;
124 SkPath fPath;
125 SkPath fNearlyZeroLengthPath;
126 SkIRect fIRect;
127 SkRegion fRegion;
128 SkColor fColor;
129 SkPaint fPaint;
130 const SkPoint* fPoints;
131 size_t fPointCount;
132 int fWidth;
133 int fHeight;
134 SkString fText;
135 const SkPoint* fPoints2;
136 SkBitmap fBitmap;
137
138private:
139 static SkMatrix TestMatrix() {
140 SkMatrix matrix;
141 matrix.reset();
142 matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
143
144 return matrix;
145 }
146 static SkPath TestPath() {
147 SkPath path;
148 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
149 SkIntToScalar(2), SkIntToScalar(1)));
150 return path;
151 }
152 static SkPath TestNearlyZeroLengthPath() {
153 SkPath path;
154 SkPoint pt1 = { 0, 0 };
155 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
156 SkPoint pt3 = { SkIntToScalar(1), 0 };
157 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
158 path.moveTo(pt1);
159 path.lineTo(pt2);
160 path.lineTo(pt3);
161 path.lineTo(pt4);
162 return path;
163 }
164 static SkRegion TestRegion() {
165 SkRegion region;
166 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
167 region.setRect(rect);
168 return region;
169 }
170 static SkBitmap TestBitmap() {
171 SkBitmap bitmap;
172 createBitmap(&bitmap, 0x05060708);
173 return bitmap;
174 }
175};
176
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000177static bool equal_clips(const SkCanvas& a, const SkCanvas& b) {
178 if (a.isClipEmpty()) {
179 return b.isClipEmpty();
180 }
181 if (!a.isClipRect()) {
182 // this is liberally true, since we don't expose a way to know this exactly (for non-rects)
183 return !b.isClipRect();
184 }
185 SkIRect ar, br;
186 a.getClipDeviceBounds(&ar);
187 b.getClipDeviceBounds(&br);
188 return ar == br;
189}
190
reed@google.com90c07ea2012-04-13 13:50:27 +0000191class Canvas2CanvasClipVisitor : public SkCanvas::ClipVisitor {
192public:
193 Canvas2CanvasClipVisitor(SkCanvas* target) : fTarget(target) {}
194
mtklein72c9faa2015-01-09 10:06:39 -0800195 void clipRect(const SkRect& r, SkRegion::Op op, bool aa) SK_OVERRIDE {
reed@google.com90c07ea2012-04-13 13:50:27 +0000196 fTarget->clipRect(r, op, aa);
197 }
mtklein72c9faa2015-01-09 10:06:39 -0800198 void clipRRect(const SkRRect& r, SkRegion::Op op, bool aa) SK_OVERRIDE {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000199 fTarget->clipRRect(r, op, aa);
200 }
mtklein72c9faa2015-01-09 10:06:39 -0800201 void clipPath(const SkPath& p, SkRegion::Op op, bool aa) SK_OVERRIDE {
reed@google.com90c07ea2012-04-13 13:50:27 +0000202 fTarget->clipPath(p, op, aa);
203 }
204
205private:
206 SkCanvas* fTarget;
207};
208
209static void test_clipVisitor(skiatest::Reporter* reporter, SkCanvas* canvas) {
210 SkISize size = canvas->getDeviceSize();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000211
reed@google.com90c07ea2012-04-13 13:50:27 +0000212 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000213 bm.setInfo(SkImageInfo::MakeN32Premul(size.width(), size.height()));
reed@google.com90c07ea2012-04-13 13:50:27 +0000214 SkCanvas c(bm);
215
216 Canvas2CanvasClipVisitor visitor(&c);
217 canvas->replayClips(&visitor);
218
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000219 REPORTER_ASSERT(reporter, equal_clips(c, *canvas));
reed@google.com90c07ea2012-04-13 13:50:27 +0000220}
221
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000222// Format strings that describe the test context. The %s token is where
223// the name of the test step is inserted. The context is required for
224// disambiguating the error in the case of failures that are reported in
225// functions that are called multiple times in different contexts (test
226// cases and test steps).
227static const char* const kDefaultAssertMessageFormat = "%s";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000228static const char* const kCanvasDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000229 "Drawing test step %s with SkCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000230static const char* const kDeferredDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000231 "Drawing test step %s with SkDeferredCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000232static const char* const kNWayDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000233 "Drawing test step %s with SkNWayCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000234static const char* const kDeferredPreFlushAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000235 "test step %s, SkDeferredCanvas state consistency before flush";
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000236static const char* const kDeferredPostFlushPlaybackAssertMessageFormat =
237 "test step %s, SkDeferredCanvas playback canvas state consistency after flush";
junov@chromium.orgfb103892012-09-20 19:35:43 +0000238static const char* const kDeferredPostSilentFlushPlaybackAssertMessageFormat =
239 "test step %s, SkDeferredCanvas playback canvas state consistency after silent flush";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000240static const char* const kNWayStateAssertMessageFormat =
241 "test step %s, SkNWayCanvas state consistency";
242static const char* const kNWayIndirect1StateAssertMessageFormat =
243 "test step %s, SkNWayCanvas indirect canvas 1 state consistency";
244static const char* const kNWayIndirect2StateAssertMessageFormat =
245 "test step %s, SkNWayCanvas indirect canvas 2 state consistency";
edisonn@google.com77909122012-10-18 15:58:23 +0000246static const char* const kPdfAssertMessageFormat =
247 "PDF sanity check failed %s";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000248
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000249class CanvasTestStep;
250static SkTDArray<CanvasTestStep*>& testStepArray() {
251 static SkTDArray<CanvasTestStep*> theTests;
252 return theTests;
253}
254
255class CanvasTestStep {
256public:
edisonn@google.com77909122012-10-18 15:58:23 +0000257 CanvasTestStep(bool fEnablePdfTesting = true) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000258 *testStepArray().append() = this;
259 fAssertMessageFormat = kDefaultAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000260 this->fEnablePdfTesting = fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000261 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000262 virtual ~CanvasTestStep() { }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000263
piotaixrf05f5a72014-10-03 13:26:55 -0700264 virtual void draw(SkCanvas*, const TestData&, skiatest::Reporter*) = 0;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000265 virtual const char* name() const = 0;
266
267 const char* assertMessage() {
268 fAssertMessage.printf(fAssertMessageFormat, name());
269 return fAssertMessage.c_str();
270 }
271
272 void setAssertMessageFormat(const char* format) {
273 fAssertMessageFormat = format;
274 }
275
edisonn@google.com77909122012-10-18 15:58:23 +0000276 bool enablePdfTesting() { return fEnablePdfTesting; }
277
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000278private:
279 SkString fAssertMessage;
280 const char* fAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000281 bool fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000282};
283
284///////////////////////////////////////////////////////////////////////////////
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000285// Macros for defining test steps
286
287#define TEST_STEP(NAME, FUNCTION) \
288class NAME##_TestStep : public CanvasTestStep{ \
289public: \
piotaixrf05f5a72014-10-03 13:26:55 -0700290 virtual void draw(SkCanvas* canvas, const TestData& d, \
291 skiatest::Reporter* reporter) { \
292 FUNCTION (canvas, d, reporter, this); \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000293 } \
294 virtual const char* name() const {return #NAME ;} \
295}; \
296static NAME##_TestStep NAME##_TestStepInstance;
297
piotaixrf05f5a72014-10-03 13:26:55 -0700298#define TEST_STEP_NO_PDF(NAME, FUNCTION) \
edisonn@google.com77909122012-10-18 15:58:23 +0000299class NAME##_TestStep : public CanvasTestStep{ \
300public: \
301 NAME##_TestStep() : CanvasTestStep(false) {} \
piotaixrf05f5a72014-10-03 13:26:55 -0700302 virtual void draw(SkCanvas* canvas, const TestData& d, \
303 skiatest::Reporter* reporter) { \
304 FUNCTION (canvas, d, reporter, this); \
edisonn@google.com77909122012-10-18 15:58:23 +0000305 } \
306 virtual const char* name() const {return #NAME ;} \
307}; \
308static NAME##_TestStep NAME##_TestStepInstance;
309
piotaixrf05f5a72014-10-03 13:26:55 -0700310#define SIMPLE_TEST_STEP(NAME, CALL) \
311static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \
312 skiatest::Reporter*, CanvasTestStep*) { \
313 canvas-> CALL ; \
314} \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000315TEST_STEP(NAME, NAME##TestStep )
316
317#define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
piotaixrf05f5a72014-10-03 13:26:55 -0700318static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \
319 skiatest::Reporter*, CanvasTestStep* testStep) { \
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000320 REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \
321 testStep->assertMessage()); \
322} \
323TEST_STEP(NAME, NAME##TestStep )
324
325
326///////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000327// Basic test steps for most virtual methods in SkCanvas that draw or affect
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000328// the state of the canvas.
329
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000330SIMPLE_TEST_STEP(Translate, translate(SkIntToScalar(1), SkIntToScalar(2)));
331SIMPLE_TEST_STEP(Scale, scale(SkIntToScalar(1), SkIntToScalar(2)));
332SIMPLE_TEST_STEP(Rotate, rotate(SkIntToScalar(1)));
333SIMPLE_TEST_STEP(Skew, skew(SkIntToScalar(1), SkIntToScalar(2)));
piotaixrf05f5a72014-10-03 13:26:55 -0700334SIMPLE_TEST_STEP(Concat, concat(d.fMatrix));
335SIMPLE_TEST_STEP(SetMatrix, setMatrix(d.fMatrix));
336SIMPLE_TEST_STEP(ClipRect, clipRect(d.fRect));
337SIMPLE_TEST_STEP(ClipPath, clipPath(d.fPath));
338SIMPLE_TEST_STEP(ClipRegion, clipRegion(d.fRegion, SkRegion::kReplace_Op));
339SIMPLE_TEST_STEP(Clear, clear(d.fColor));
piotaixrf05f5a72014-10-03 13:26:55 -0700340SIMPLE_TEST_STEP(BeginGroup, beginCommentGroup(d.fText.c_str()));
341SIMPLE_TEST_STEP(AddComment, addComment(d.fText.c_str(), d.fText.c_str()));
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000342SIMPLE_TEST_STEP(EndGroup, endCommentGroup());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000343
344///////////////////////////////////////////////////////////////////////////////
345// Complex test steps
346
piotaixrf05f5a72014-10-03 13:26:55 -0700347static void SaveMatrixClipStep(SkCanvas* canvas, const TestData& d,
348 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000349 int saveCount = canvas->getSaveCount();
Florin Malita5f6102d2014-06-30 10:13:28 -0400350 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000351 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
piotaixrf05f5a72014-10-03 13:26:55 -0700352 canvas->clipRegion(d.fRegion);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000353 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000354 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000355 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000356 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000357 testStep->assertMessage());
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000358// REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion, testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000359}
360TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
361
piotaixrf05f5a72014-10-03 13:26:55 -0700362static void SaveLayerStep(SkCanvas* canvas, const TestData& d,
363 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000364 int saveCount = canvas->getSaveCount();
365 canvas->saveLayer(NULL, NULL);
366 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000367 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000368 testStep->assertMessage());
369}
370TEST_STEP(SaveLayer, SaveLayerStep);
371
piotaixrf05f5a72014-10-03 13:26:55 -0700372static void BoundedSaveLayerStep(SkCanvas* canvas, const TestData& d,
373 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000374 int saveCount = canvas->getSaveCount();
piotaixrf05f5a72014-10-03 13:26:55 -0700375 canvas->saveLayer(&d.fRect, NULL);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000376 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000377 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000378 testStep->assertMessage());
379}
380TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
381
piotaixrf05f5a72014-10-03 13:26:55 -0700382static void PaintSaveLayerStep(SkCanvas* canvas, const TestData& d,
383 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000384 int saveCount = canvas->getSaveCount();
piotaixrf05f5a72014-10-03 13:26:55 -0700385 canvas->saveLayer(NULL, &d.fPaint);
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000386 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000387 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000388 testStep->assertMessage());
389}
390TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
391
piotaixrf05f5a72014-10-03 13:26:55 -0700392static void TwoClipOpsStep(SkCanvas* canvas, const TestData& d,
393 skiatest::Reporter*, CanvasTestStep*) {
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000394 // This test exercises a functionality in SkPicture that leads to the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000395 // recording of restore offset placeholders. This test will trigger an
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000396 // assertion at playback time if the placeholders are not properly
397 // filled when the recording ends.
piotaixrf05f5a72014-10-03 13:26:55 -0700398 canvas->clipRect(d.fRect);
399 canvas->clipRegion(d.fRegion);
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000400}
401TEST_STEP(TwoClipOps, TwoClipOpsStep);
402
epoger@google.com94fa43c2012-04-11 17:51:01 +0000403// exercise fix for http://code.google.com/p/skia/issues/detail?id=560
404// ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
piotaixrf05f5a72014-10-03 13:26:55 -0700405static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas, const TestData& d,
406 skiatest::Reporter*, CanvasTestStep*) {
epoger@google.com94fa43c2012-04-11 17:51:01 +0000407 SkPaint paint;
408 paint.setStrokeWidth(SkIntToScalar(1));
409 paint.setStyle(SkPaint::kStroke_Style);
410
piotaixrf05f5a72014-10-03 13:26:55 -0700411 canvas->drawPath(d.fNearlyZeroLengthPath, paint);
epoger@google.com94fa43c2012-04-11 17:51:01 +0000412}
413TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
414
piotaixrf05f5a72014-10-03 13:26:55 -0700415static void DrawVerticesShaderTestStep(SkCanvas* canvas, const TestData& d,
416 skiatest::Reporter*, CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000417 SkPoint pts[4];
418 pts[0].set(0, 0);
piotaixrf05f5a72014-10-03 13:26:55 -0700419 pts[1].set(SkIntToScalar(d.fWidth), 0);
420 pts[2].set(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight));
421 pts[3].set(0, SkIntToScalar(d.fHeight));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000422 SkPaint paint;
piotaixrf05f5a72014-10-03 13:26:55 -0700423 SkShader* shader = SkShader::CreateBitmapShader(d.fBitmap,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000424 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
425 paint.setShader(shader)->unref();
426 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
427 NULL, NULL, NULL, 0, paint);
428}
edisonn@google.com77909122012-10-18 15:58:23 +0000429// NYI: issue 240.
430TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000431
piotaixrf05f5a72014-10-03 13:26:55 -0700432static void DrawPictureTestStep(SkCanvas* canvas, const TestData& d,
433 skiatest::Reporter*, CanvasTestStep*) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000434 SkPictureRecorder recorder;
piotaixrf05f5a72014-10-03 13:26:55 -0700435 SkCanvas* testCanvas = recorder.beginRecording(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight),
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700436 NULL, 0);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000437 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
piotaixrf05f5a72014-10-03 13:26:55 -0700438 testCanvas->clipRect(d.fRect);
439 testCanvas->drawRect(d.fRect, d.fPaint);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000440 SkAutoTUnref<SkPicture> testPicture(recorder.endRecording());
441
robertphillips9b14f262014-06-04 05:40:44 -0700442 canvas->drawPicture(testPicture);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000443}
444TEST_STEP(DrawPicture, DrawPictureTestStep);
445
piotaixrf05f5a72014-10-03 13:26:55 -0700446static void SaveRestoreTestStep(SkCanvas* canvas, const TestData& d,
447 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000448 int baseSaveCount = canvas->getSaveCount();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000449 int n = canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000450 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount == n, testStep->assertMessage());
451 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000452 testStep->assertMessage());
453 canvas->save();
454 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000455 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000456 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000457 canvas->restoreToCount(baseSaveCount + 1);
458 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000459 testStep->assertMessage());
460
461 // should this pin to 1, or be a no-op, or crash?
462 canvas->restoreToCount(0);
463 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
464 testStep->assertMessage());
465}
466TEST_STEP(SaveRestore, SaveRestoreTestStep);
467
piotaixrf05f5a72014-10-03 13:26:55 -0700468static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas, const TestData& d,
469 skiatest::Reporter*, CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000470 // This test step challenges the TestDeferredCanvasStateConsistency
471 // test cases because the opaque paint can trigger an optimization
472 // that discards previously recorded commands. The challenge is to maintain
473 // correct clip and matrix stack state.
474 canvas->resetMatrix();
475 canvas->rotate(SkIntToScalar(30));
476 canvas->save();
477 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
478 canvas->save();
479 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
480 SkPaint paint;
481 paint.setColor(0xFFFFFFFF);
482 canvas->drawPaint(paint);
483 canvas->restore();
484 canvas->restore();
485}
486TEST_STEP(NestedSaveRestoreWithSolidPaint, \
487 NestedSaveRestoreWithSolidPaintTestStep);
488
piotaixrf05f5a72014-10-03 13:26:55 -0700489static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas, const TestData& d,
490 skiatest::Reporter*, CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000491 // This test step challenges the TestDeferredCanvasStateConsistency
492 // test case because the canvas flush on a deferred canvas will
493 // reset the recording session. The challenge is to maintain correct
494 // clip and matrix stack state on the playback canvas.
495 canvas->resetMatrix();
496 canvas->rotate(SkIntToScalar(30));
497 canvas->save();
498 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
499 canvas->save();
500 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
piotaixrf05f5a72014-10-03 13:26:55 -0700501 canvas->drawRect(d.fRect,d.fPaint);
reed@google.com3b3e8952012-08-16 20:53:31 +0000502 canvas->flush();
503 canvas->restore();
504 canvas->restore();
505}
piotaixrf05f5a72014-10-03 13:26:55 -0700506TEST_STEP(NestedSaveRestoreWithFlush, NestedSaveRestoreWithFlushTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000507
piotaixrf05f5a72014-10-03 13:26:55 -0700508static void AssertCanvasStatesEqual(skiatest::Reporter* reporter, const TestData& d,
509 const SkCanvas* canvas1, const SkCanvas* canvas2,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000510 CanvasTestStep* testStep) {
511 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
512 canvas2->getDeviceSize(), testStep->assertMessage());
513 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
514 canvas2->getSaveCount(), testStep->assertMessage());
reed@google.com3b3e8952012-08-16 20:53:31 +0000515
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000516 SkRect bounds1, bounds2;
517 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.com3b3e8952012-08-16 20:53:31 +0000518 canvas1->getClipBounds(&bounds1) == canvas2->getClipBounds(&bounds2),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000519 testStep->assertMessage());
520 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
reed@google.com3b3e8952012-08-16 20:53:31 +0000521 testStep->assertMessage());
522
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000523 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
524 canvas2->getDrawFilter(), testStep->assertMessage());
525 SkIRect deviceBounds1, deviceBounds2;
526 REPORTER_ASSERT_MESSAGE(reporter,
527 canvas1->getClipDeviceBounds(&deviceBounds1) ==
528 canvas2->getClipDeviceBounds(&deviceBounds2),
529 testStep->assertMessage());
reed868074b2014-06-03 10:53:59 -0700530 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2, testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000531 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
532 canvas2->getTotalMatrix(), testStep->assertMessage());
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000533 REPORTER_ASSERT_MESSAGE(reporter, equal_clips(*canvas1, *canvas2), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000534
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000535 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
536 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
537 while (!layerIter1.done() && !layerIter2.done()) {
538 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
539 layerIter2.matrix(), testStep->assertMessage());
540 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
541 layerIter2.clip(), testStep->assertMessage());
542 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
543 layerIter2.paint(), testStep->assertMessage());
544 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
545 layerIter2.x(), testStep->assertMessage());
546 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
547 layerIter2.y(), testStep->assertMessage());
548 layerIter1.next();
549 layerIter2.next();
550 }
551 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
552 testStep->assertMessage());
553 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
554 testStep->assertMessage());
piotaixr76993ed2014-10-27 15:31:34 -0700555
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000556}
557
edisonn@google.com77909122012-10-18 15:58:23 +0000558static void TestPdfDevice(skiatest::Reporter* reporter,
piotaixrf05f5a72014-10-03 13:26:55 -0700559 const TestData& d,
edisonn@google.com77909122012-10-18 15:58:23 +0000560 CanvasTestStep* testStep) {
piotaixrf05f5a72014-10-03 13:26:55 -0700561 SkISize pageSize = SkISize::Make(d.fWidth, d.fHeight);
halcanarya1f1ee92015-02-20 06:17:26 -0800562 SkPDFCanon canon;
563 SkAutoTUnref<SkPDFDevice> pdfDevice(
564 SkPDFDevice::Create(pageSize, 72.0f, &canon));
565 SkCanvas canvas(pdfDevice.get());
edisonn@google.com77909122012-10-18 15:58:23 +0000566 testStep->setAssertMessageFormat(kPdfAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700567 testStep->draw(&canvas, d, reporter);
edisonn@google.com77909122012-10-18 15:58:23 +0000568 SkPDFDocument doc;
halcanarya1f1ee92015-02-20 06:17:26 -0800569 doc.appendPage(pdfDevice.get());
edisonn@google.com77909122012-10-18 15:58:23 +0000570 SkDynamicMemoryWStream stream;
571 doc.emitPDF(&stream);
572}
573
junov@chromium.org88e29142012-08-07 16:48:22 +0000574// The following class groups static functions that need to access
575// the privates members of SkDeferredCanvas
576class SkDeferredCanvasTester {
577public:
578 static void TestDeferredCanvasStateConsistency(
579 skiatest::Reporter* reporter,
piotaixrf05f5a72014-10-03 13:26:55 -0700580 const TestData& d,
junov@chromium.org88e29142012-08-07 16:48:22 +0000581 CanvasTestStep* testStep,
junov@chromium.orgfb103892012-09-20 19:35:43 +0000582 const SkCanvas& referenceCanvas, bool silent) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000583
reed@google.com28183b42014-02-04 15:34:10 +0000584 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
585 SkAutoTUnref<SkDeferredCanvas> deferredCanvas(SkDeferredCanvas::Create(surface.get()));
586
junov@chromium.org88e29142012-08-07 16:48:22 +0000587 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700588 testStep->draw(deferredCanvas, d, reporter);
junov@chromium.org88e29142012-08-07 16:48:22 +0000589 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700590 AssertCanvasStatesEqual(reporter, d, deferredCanvas, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000591
junov@chromium.orgfb103892012-09-20 19:35:43 +0000592 if (silent) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000593 deferredCanvas->silentFlush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000594 } else {
junov@chromium.org66070a52013-05-28 17:39:08 +0000595 deferredCanvas->flush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000596 }
597
skia.committer@gmail.com4c5ea442012-09-21 02:01:01 +0000598 testStep->setAssertMessageFormat(
junov@chromium.orgfb103892012-09-20 19:35:43 +0000599 silent ? kDeferredPostSilentFlushPlaybackAssertMessageFormat :
junov@chromium.org88e29142012-08-07 16:48:22 +0000600 kDeferredPostFlushPlaybackAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700601 AssertCanvasStatesEqual(reporter, d, deferredCanvas->immediateCanvas(),
602 &referenceCanvas, testStep);
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000603
junov@chromium.org88e29142012-08-07 16:48:22 +0000604 // Verified that deferred canvas state is not affected by flushing
605 // pending draw operations
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000606
junov@chromium.org88e29142012-08-07 16:48:22 +0000607 // The following test code is commented out because it currently fails.
608 // Issue: http://code.google.com/p/skia/issues/detail?id=496
609 /*
610 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
611 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
612 testStep);
613 */
614 }
615};
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000616
caryclark@google.com42639cd2012-06-06 12:03:39 +0000617// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000618static void TestNWayCanvasStateConsistency(
619 skiatest::Reporter* reporter,
piotaixrf05f5a72014-10-03 13:26:55 -0700620 const TestData& d,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000621 CanvasTestStep* testStep,
622 const SkCanvas& referenceCanvas) {
623
624 SkBitmap indirectStore1;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000625 createBitmap(&indirectStore1, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700626 SkCanvas indirectCanvas1(indirectStore1);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000627
628 SkBitmap indirectStore2;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000629 createBitmap(&indirectStore2, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700630 SkCanvas indirectCanvas2(indirectStore2);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000631
djsollen@google.comf0a062b2012-05-01 16:50:25 +0000632 SkISize canvasSize = referenceCanvas.getDeviceSize();
633 SkNWayCanvas nWayCanvas(canvasSize.width(), canvasSize.height());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000634 nWayCanvas.addCanvas(&indirectCanvas1);
635 nWayCanvas.addCanvas(&indirectCanvas2);
636
637 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700638 testStep->draw(&nWayCanvas, d, reporter);
scroggo648238c2015-01-29 11:58:51 -0800639 // Verify that the SkNWayCanvas reports consitent state
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000640 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700641 AssertCanvasStatesEqual(reporter, d, &nWayCanvas, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000642 // Verify that the indirect canvases report consitent state
643 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700644 AssertCanvasStatesEqual(reporter, d, &indirectCanvas1, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000645 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700646 AssertCanvasStatesEqual(reporter, d, &indirectCanvas2, &referenceCanvas, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000647}
648
649/*
650 * This sub-test verifies that the test step passes when executed
651 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
652 * that the all canvas derivatives report the same state as an SkCanvas
653 * after having executed the test step.
654 */
piotaixrf05f5a72014-10-03 13:26:55 -0700655static void TestOverrideStateConsistency(skiatest::Reporter* reporter, const TestData& d,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000656 CanvasTestStep* testStep) {
657 SkBitmap referenceStore;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000658 createBitmap(&referenceStore, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700659 SkCanvas referenceCanvas(referenceStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000660 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
piotaixrf05f5a72014-10-03 13:26:55 -0700661 testStep->draw(&referenceCanvas, d, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000662
piotaixrf05f5a72014-10-03 13:26:55 -0700663 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, d, testStep, referenceCanvas, false);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000664
piotaixrf05f5a72014-10-03 13:26:55 -0700665 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, d, testStep, referenceCanvas, true);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000666
caryclark@google.com42639cd2012-06-06 12:03:39 +0000667 // The following test code is disabled because SkNWayCanvas does not
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000668 // report correct clipping and device bounds information
669 // Issue: http://code.google.com/p/skia/issues/detail?id=501
caryclark@google.com42639cd2012-06-06 12:03:39 +0000670
671 if (false) { // avoid bit rot, suppress warning
piotaixrf05f5a72014-10-03 13:26:55 -0700672 TestNWayCanvasStateConsistency(reporter, d, testStep, referenceCanvas);
caryclark@google.com42639cd2012-06-06 12:03:39 +0000673 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000674
caryclark@google.com42639cd2012-06-06 12:03:39 +0000675 if (false) { // avoid bit rot, suppress warning
676 test_clipVisitor(reporter, &referenceCanvas);
677 }
reed@google.com7c202932011-12-14 18:48:05 +0000678}
reed@google.com37f3ae02011-11-28 16:06:04 +0000679
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000680static void test_newraster(skiatest::Reporter* reporter) {
681 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
reed3054be12014-12-10 07:24:28 -0800682 const size_t minRowBytes = info.minRowBytes();
683 const size_t size = info.getSafeSize(minRowBytes);
684 SkAutoMalloc storage(size);
685 SkPMColor* baseAddr = static_cast<SkPMColor*>(storage.get());
686 sk_bzero(baseAddr, size);
687
688 SkCanvas* canvas = SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000689 REPORTER_ASSERT(reporter, canvas);
690
691 SkImageInfo info2;
692 size_t rowBytes;
693 const SkPMColor* addr = (const SkPMColor*)canvas->peekPixels(&info2, &rowBytes);
694 REPORTER_ASSERT(reporter, addr);
695 REPORTER_ASSERT(reporter, info == info2);
reed3054be12014-12-10 07:24:28 -0800696 REPORTER_ASSERT(reporter, minRowBytes == rowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000697 for (int y = 0; y < info.height(); ++y) {
698 for (int x = 0; x < info.width(); ++x) {
699 REPORTER_ASSERT(reporter, 0 == addr[x]);
700 }
701 addr = (const SkPMColor*)((const char*)addr + rowBytes);
702 }
703 SkDELETE(canvas);
704
705 // now try a deliberately bad info
reede5ea5002014-09-03 11:54:58 -0700706 info = info.makeWH(-1, info.height());
reed3054be12014-12-10 07:24:28 -0800707 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000708
709 // too big
reede5ea5002014-09-03 11:54:58 -0700710 info = info.makeWH(1 << 30, 1 << 30);
reed3054be12014-12-10 07:24:28 -0800711 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
skia.committer@gmail.com0e530752014-02-28 03:02:05 +0000712
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000713 // not a valid pixel type
reede5ea5002014-09-03 11:54:58 -0700714 info = SkImageInfo::Make(10, 10, kUnknown_SkColorType, info.alphaType());
reed3054be12014-12-10 07:24:28 -0800715 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000716
717 // We should succeed with a zero-sized valid info
718 info = SkImageInfo::MakeN32Premul(0, 0);
reed3054be12014-12-10 07:24:28 -0800719 canvas = SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000720 REPORTER_ASSERT(reporter, canvas);
721 SkDELETE(canvas);
722}
723
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000724DEF_TEST(Canvas, reporter) {
piotaixrf05f5a72014-10-03 13:26:55 -0700725 TestData d;
reed@google.com37f3ae02011-11-28 16:06:04 +0000726
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000727 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
piotaixrf05f5a72014-10-03 13:26:55 -0700728 TestOverrideStateConsistency(reporter, d, testStepArray()[testStep]);
edisonn@google.com77909122012-10-18 15:58:23 +0000729 if (testStepArray()[testStep]->enablePdfTesting()) {
piotaixrf05f5a72014-10-03 13:26:55 -0700730 TestPdfDevice(reporter, d, testStepArray()[testStep]);
edisonn@google.com77909122012-10-18 15:58:23 +0000731 }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000732 }
junov@chromium.orgcd62ecf2012-08-02 17:43:25 +0000733
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000734 test_newraster(reporter);
reed@google.com37f3ae02011-11-28 16:06:04 +0000735}
reedf0090cb2014-11-26 08:55:51 -0800736
737DEF_TEST(Canvas_SaveState, reporter) {
738 SkCanvas canvas(10, 10);
739 REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
740
741 int n = canvas.save();
742 REPORTER_ASSERT(reporter, 1 == n);
743 REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
744
745 n = canvas.saveLayer(NULL, NULL);
746 REPORTER_ASSERT(reporter, 2 == n);
747 REPORTER_ASSERT(reporter, 3 == canvas.getSaveCount());
748
749 canvas.restore();
750 REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
751 canvas.restore();
752 REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
753}