blob: 5ec0b8f646cc4d6adaa09d75e111064ebd69640d [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"
edisonn@google.com77909122012-10-18 15:58:23 +000052#include "SkPDFDevice.h"
53#include "SkPDFDocument.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000054#include "SkPaint.h"
55#include "SkPath.h"
56#include "SkPicture.h"
57#include "SkPictureRecord.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000058#include "SkPictureRecorder.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000059#include "SkProxyCanvas.h"
60#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
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +000068static bool equal_clips(const SkCanvas& a, const SkCanvas& b) {
69 if (a.isClipEmpty()) {
70 return b.isClipEmpty();
71 }
72 if (!a.isClipRect()) {
73 // this is liberally true, since we don't expose a way to know this exactly (for non-rects)
74 return !b.isClipRect();
75 }
76 SkIRect ar, br;
77 a.getClipDeviceBounds(&ar);
78 b.getClipDeviceBounds(&br);
79 return ar == br;
80}
81
reed@google.com90c07ea2012-04-13 13:50:27 +000082class Canvas2CanvasClipVisitor : public SkCanvas::ClipVisitor {
83public:
84 Canvas2CanvasClipVisitor(SkCanvas* target) : fTarget(target) {}
85
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000086 virtual void clipRect(const SkRect& r, SkRegion::Op op, bool aa) SK_OVERRIDE {
reed@google.com90c07ea2012-04-13 13:50:27 +000087 fTarget->clipRect(r, op, aa);
88 }
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000089 virtual void clipRRect(const SkRRect& r, SkRegion::Op op, bool aa) SK_OVERRIDE {
90 fTarget->clipRRect(r, op, aa);
91 }
92 virtual void clipPath(const SkPath& p, SkRegion::Op op, bool aa) SK_OVERRIDE {
reed@google.com90c07ea2012-04-13 13:50:27 +000093 fTarget->clipPath(p, op, aa);
94 }
95
96private:
97 SkCanvas* fTarget;
98};
99
100static void test_clipVisitor(skiatest::Reporter* reporter, SkCanvas* canvas) {
101 SkISize size = canvas->getDeviceSize();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000102
reed@google.com90c07ea2012-04-13 13:50:27 +0000103 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000104 bm.setInfo(SkImageInfo::MakeN32Premul(size.width(), size.height()));
reed@google.com90c07ea2012-04-13 13:50:27 +0000105 SkCanvas c(bm);
106
107 Canvas2CanvasClipVisitor visitor(&c);
108 canvas->replayClips(&visitor);
109
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000110 REPORTER_ASSERT(reporter, equal_clips(c, *canvas));
reed@google.com90c07ea2012-04-13 13:50:27 +0000111}
112
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000113static const int kWidth = 2;
114static const int kHeight = 2;
reed@google.com7c202932011-12-14 18:48:05 +0000115
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000116// Format strings that describe the test context. The %s token is where
117// the name of the test step is inserted. The context is required for
118// disambiguating the error in the case of failures that are reported in
119// functions that are called multiple times in different contexts (test
120// cases and test steps).
121static const char* const kDefaultAssertMessageFormat = "%s";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000122static const char* const kCanvasDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000123 "Drawing test step %s with SkCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000124static const char* const kDeferredDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000125 "Drawing test step %s with SkDeferredCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000126static const char* const kProxyDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000127 "Drawing test step %s with SkProxyCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000128static const char* const kNWayDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000129 "Drawing test step %s with SkNWayCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000130static const char* const kDeferredPreFlushAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000131 "test step %s, SkDeferredCanvas state consistency before flush";
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000132static const char* const kDeferredPostFlushPlaybackAssertMessageFormat =
133 "test step %s, SkDeferredCanvas playback canvas state consistency after flush";
junov@chromium.orgfb103892012-09-20 19:35:43 +0000134static const char* const kDeferredPostSilentFlushPlaybackAssertMessageFormat =
135 "test step %s, SkDeferredCanvas playback canvas state consistency after silent flush";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000136static const char* const kProxyStateAssertMessageFormat =
137 "test step %s, SkProxyCanvas state consistency";
138static const char* const kProxyIndirectStateAssertMessageFormat =
139 "test step %s, SkProxyCanvas indirect canvas state consistency";
140static const char* const kNWayStateAssertMessageFormat =
141 "test step %s, SkNWayCanvas state consistency";
142static const char* const kNWayIndirect1StateAssertMessageFormat =
143 "test step %s, SkNWayCanvas indirect canvas 1 state consistency";
144static const char* const kNWayIndirect2StateAssertMessageFormat =
145 "test step %s, SkNWayCanvas indirect canvas 2 state consistency";
edisonn@google.com77909122012-10-18 15:58:23 +0000146static const char* const kPdfAssertMessageFormat =
147 "PDF sanity check failed %s";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000148
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000149static void createBitmap(SkBitmap* bm, SkColor color) {
150 bm->allocN32Pixels(kWidth, kHeight);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000151 bm->eraseColor(color);
152}
153
reed@google.com28183b42014-02-04 15:34:10 +0000154static SkSurface* createSurface(SkColor color) {
155 SkSurface* surface = SkSurface::NewRasterPMColor(kWidth, kHeight);
156 surface->getCanvas()->clear(color);
157 return surface;
158}
159
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000160class CanvasTestStep;
161static SkTDArray<CanvasTestStep*>& testStepArray() {
162 static SkTDArray<CanvasTestStep*> theTests;
163 return theTests;
164}
165
166class CanvasTestStep {
167public:
edisonn@google.com77909122012-10-18 15:58:23 +0000168 CanvasTestStep(bool fEnablePdfTesting = true) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000169 *testStepArray().append() = this;
170 fAssertMessageFormat = kDefaultAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000171 this->fEnablePdfTesting = fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000172 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000173 virtual ~CanvasTestStep() { }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000174
175 virtual void draw(SkCanvas*, skiatest::Reporter*) = 0;
176 virtual const char* name() const = 0;
177
178 const char* assertMessage() {
179 fAssertMessage.printf(fAssertMessageFormat, name());
180 return fAssertMessage.c_str();
181 }
182
183 void setAssertMessageFormat(const char* format) {
184 fAssertMessageFormat = format;
185 }
186
edisonn@google.com77909122012-10-18 15:58:23 +0000187 bool enablePdfTesting() { return fEnablePdfTesting; }
188
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000189private:
190 SkString fAssertMessage;
191 const char* fAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000192 bool fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000193};
194
195///////////////////////////////////////////////////////////////////////////////
196// Constants used by test steps
197
rmistry@google.comd6176b02012-08-23 18:14:13 +0000198const SkRect kTestRect =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000199 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
200 SkIntToScalar(2), SkIntToScalar(1));
201static SkMatrix testMatrix() {
202 SkMatrix matrix;
203 matrix.reset();
204 matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
205 return matrix;
206}
207const SkMatrix kTestMatrix = testMatrix();
commit-bot@chromium.org8c2ee592014-03-07 18:42:15 +0000208static SkPath test_path() {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000209 SkPath path;
210 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
211 SkIntToScalar(2), SkIntToScalar(1)));
212 return path;
213}
commit-bot@chromium.org8c2ee592014-03-07 18:42:15 +0000214const SkPath kTestPath = test_path();
215static SkPath test_nearly_zero_length_path() {
216 SkPath path;
217 SkPoint pt1 = { 0, 0 };
218 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
219 SkPoint pt3 = { SkIntToScalar(1), 0 };
220 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
221 path.moveTo(pt1);
222 path.lineTo(pt2);
223 path.lineTo(pt3);
224 path.lineTo(pt4);
225 return path;
226}
227const SkPath kNearlyZeroLengthPath = test_nearly_zero_length_path();
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000228static SkRegion testRegion() {
229 SkRegion region;
230 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
231 region.setRect(rect);
232 return region;
233}
234const SkIRect kTestIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
235const SkRegion kTestRegion = testRegion();
236const SkColor kTestColor = 0x01020304;
237const SkPaint kTestPaint;
238const SkPoint kTestPoints[3] = {
239 {SkIntToScalar(0), SkIntToScalar(0)},
240 {SkIntToScalar(2), SkIntToScalar(1)},
241 {SkIntToScalar(0), SkIntToScalar(2)}
242};
243const size_t kTestPointCount = 3;
244static SkBitmap testBitmap() {
245 SkBitmap bitmap;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000246 createBitmap(&bitmap, 0x05060708);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000247 return bitmap;
248}
249SkBitmap kTestBitmap; // cannot be created during static init
250SkString kTestText("Hello World");
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000251SkPoint kTestPoints2[] = {
252 { SkIntToScalar(0), SkIntToScalar(1) },
253 { SkIntToScalar(1), SkIntToScalar(1) },
254 { SkIntToScalar(2), SkIntToScalar(1) },
255 { SkIntToScalar(3), SkIntToScalar(1) },
256 { SkIntToScalar(4), SkIntToScalar(1) },
257 { SkIntToScalar(5), SkIntToScalar(1) },
258 { SkIntToScalar(6), SkIntToScalar(1) },
259 { SkIntToScalar(7), SkIntToScalar(1) },
260 { SkIntToScalar(8), SkIntToScalar(1) },
261 { SkIntToScalar(9), SkIntToScalar(1) },
262 { SkIntToScalar(10), SkIntToScalar(1) },
263};
rmistry@google.comd6176b02012-08-23 18:14:13 +0000264
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000265
266///////////////////////////////////////////////////////////////////////////////
267// Macros for defining test steps
268
269#define TEST_STEP(NAME, FUNCTION) \
270class NAME##_TestStep : public CanvasTestStep{ \
271public: \
272 virtual void draw(SkCanvas* canvas, skiatest::Reporter* reporter) { \
273 FUNCTION (canvas, reporter, this); \
274 } \
275 virtual const char* name() const {return #NAME ;} \
276}; \
277static NAME##_TestStep NAME##_TestStepInstance;
278
edisonn@google.com77909122012-10-18 15:58:23 +0000279#define TEST_STEP_NO_PDF(NAME, FUNCTION) \
280class NAME##_TestStep : public CanvasTestStep{ \
281public: \
282 NAME##_TestStep() : CanvasTestStep(false) {} \
283 virtual void draw(SkCanvas* canvas, skiatest::Reporter* reporter) { \
284 FUNCTION (canvas, reporter, this); \
285 } \
286 virtual const char* name() const {return #NAME ;} \
287}; \
288static NAME##_TestStep NAME##_TestStepInstance;
289
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000290#define SIMPLE_TEST_STEP(NAME, CALL) \
291static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter*, \
292 CanvasTestStep*) { \
293 canvas-> CALL ; \
294} \
295TEST_STEP(NAME, NAME##TestStep )
296
297#define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
298static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter* reporter, \
299 CanvasTestStep* testStep) { \
300 REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \
301 testStep->assertMessage()); \
302} \
303TEST_STEP(NAME, NAME##TestStep )
304
305
306///////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000307// Basic test steps for most virtual methods in SkCanvas that draw or affect
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000308// the state of the canvas.
309
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000310SIMPLE_TEST_STEP(Translate, translate(SkIntToScalar(1), SkIntToScalar(2)));
311SIMPLE_TEST_STEP(Scale, scale(SkIntToScalar(1), SkIntToScalar(2)));
312SIMPLE_TEST_STEP(Rotate, rotate(SkIntToScalar(1)));
313SIMPLE_TEST_STEP(Skew, skew(SkIntToScalar(1), SkIntToScalar(2)));
314SIMPLE_TEST_STEP(Concat, concat(kTestMatrix));
junov@chromium.orga907ac32012-02-24 21:54:07 +0000315SIMPLE_TEST_STEP(SetMatrix, setMatrix(kTestMatrix));
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000316SIMPLE_TEST_STEP(ClipRect, clipRect(kTestRect));
317SIMPLE_TEST_STEP(ClipPath, clipPath(kTestPath));
318SIMPLE_TEST_STEP(ClipRegion,
junov@chromium.orga907ac32012-02-24 21:54:07 +0000319 clipRegion(kTestRegion, SkRegion::kReplace_Op));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000320SIMPLE_TEST_STEP(Clear, clear(kTestColor));
321SIMPLE_TEST_STEP(DrawPaint, drawPaint(kTestPaint));
322SIMPLE_TEST_STEP(DrawPointsPoints, drawPoints(SkCanvas::kPoints_PointMode,
323 kTestPointCount, kTestPoints, kTestPaint));
324SIMPLE_TEST_STEP(DrawPointsLiness, drawPoints(SkCanvas::kLines_PointMode,
325 kTestPointCount, kTestPoints, kTestPaint));
326SIMPLE_TEST_STEP(DrawPointsPolygon, drawPoints(SkCanvas::kPolygon_PointMode,
327 kTestPointCount, kTestPoints, kTestPaint));
328SIMPLE_TEST_STEP(DrawRect, drawRect(kTestRect, kTestPaint));
329SIMPLE_TEST_STEP(DrawPath, drawPath(kTestPath, kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000330SIMPLE_TEST_STEP(DrawBitmap, drawBitmap(kTestBitmap, 0, 0));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000331SIMPLE_TEST_STEP(DrawBitmapPaint, drawBitmap(kTestBitmap, 0, 0, &kTestPaint));
332SIMPLE_TEST_STEP(DrawBitmapRect, drawBitmapRect(kTestBitmap, NULL, kTestRect,
333 NULL));
334SIMPLE_TEST_STEP(DrawBitmapRectSrcRect, drawBitmapRect(kTestBitmap,
335 &kTestIRect, kTestRect, NULL));
336SIMPLE_TEST_STEP(DrawBitmapRectPaint, drawBitmapRect(kTestBitmap, NULL,
337 kTestRect, &kTestPaint));
338SIMPLE_TEST_STEP(DrawBitmapMatrix, drawBitmapMatrix(kTestBitmap, kTestMatrix,
339 NULL));
340SIMPLE_TEST_STEP(DrawBitmapMatrixPaint, drawBitmapMatrix(kTestBitmap,
341 kTestMatrix, &kTestPaint));
342SIMPLE_TEST_STEP(DrawBitmapNine, drawBitmapNine(kTestBitmap, kTestIRect,
343 kTestRect, NULL));
344SIMPLE_TEST_STEP(DrawBitmapNinePaint, drawBitmapNine(kTestBitmap, kTestIRect,
345 kTestRect, &kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000346SIMPLE_TEST_STEP(DrawSprite, drawSprite(kTestBitmap, 0, 0, NULL));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000347SIMPLE_TEST_STEP(DrawSpritePaint, drawSprite(kTestBitmap, 0, 0, &kTestPaint));
348SIMPLE_TEST_STEP(DrawText, drawText(kTestText.c_str(), kTestText.size(),
349 0, 1, kTestPaint));
350SIMPLE_TEST_STEP(DrawPosText, drawPosText(kTestText.c_str(),
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000351 kTestText.size(), kTestPoints2, kTestPaint));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000352SIMPLE_TEST_STEP(DrawTextOnPath, drawTextOnPath(kTestText.c_str(),
353 kTestText.size(), kTestPath, NULL, kTestPaint));
354SIMPLE_TEST_STEP(DrawTextOnPathMatrix, drawTextOnPath(kTestText.c_str(),
355 kTestText.size(), kTestPath, &kTestMatrix, kTestPaint));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000356SIMPLE_TEST_STEP(DrawData, drawData(kTestText.c_str(), kTestText.size()));
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000357SIMPLE_TEST_STEP(BeginGroup, beginCommentGroup(kTestText.c_str()));
358SIMPLE_TEST_STEP(AddComment, addComment(kTestText.c_str(), kTestText.c_str()));
359SIMPLE_TEST_STEP(EndGroup, endCommentGroup());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000360
361///////////////////////////////////////////////////////////////////////////////
362// Complex test steps
363
rmistry@google.comd6176b02012-08-23 18:14:13 +0000364static void SaveMatrixClipStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000365 skiatest::Reporter* reporter,
366 CanvasTestStep* testStep) {
367 int saveCount = canvas->getSaveCount();
Florin Malita5f6102d2014-06-30 10:13:28 -0400368 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000369 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
370 canvas->clipRegion(kTestRegion);
371 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000372 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000373 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000374 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000375 testStep->assertMessage());
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000376// REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion, testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000377}
378TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
379
rmistry@google.comd6176b02012-08-23 18:14:13 +0000380static void SaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000381 skiatest::Reporter* reporter,
382 CanvasTestStep* testStep) {
383 int saveCount = canvas->getSaveCount();
384 canvas->saveLayer(NULL, NULL);
385 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000386 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000387 testStep->assertMessage());
388}
389TEST_STEP(SaveLayer, SaveLayerStep);
390
rmistry@google.comd6176b02012-08-23 18:14:13 +0000391static void BoundedSaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000392 skiatest::Reporter* reporter,
393 CanvasTestStep* testStep) {
394 int saveCount = canvas->getSaveCount();
395 canvas->saveLayer(&kTestRect, NULL);
396 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000397 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000398 testStep->assertMessage());
399}
400TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
401
rmistry@google.comd6176b02012-08-23 18:14:13 +0000402static void PaintSaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000403 skiatest::Reporter* reporter,
404 CanvasTestStep* testStep) {
405 int saveCount = canvas->getSaveCount();
406 canvas->saveLayer(NULL, &kTestPaint);
407 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000408 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000409 testStep->assertMessage());
410}
411TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
412
rmistry@google.comd6176b02012-08-23 18:14:13 +0000413static void TwoClipOpsStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000414 skiatest::Reporter*,
415 CanvasTestStep*) {
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000416 // This test exercises a functionality in SkPicture that leads to the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000417 // recording of restore offset placeholders. This test will trigger an
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000418 // assertion at playback time if the placeholders are not properly
419 // filled when the recording ends.
420 canvas->clipRect(kTestRect);
421 canvas->clipRegion(kTestRegion);
422}
423TEST_STEP(TwoClipOps, TwoClipOpsStep);
424
epoger@google.com94fa43c2012-04-11 17:51:01 +0000425// exercise fix for http://code.google.com/p/skia/issues/detail?id=560
426// ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
rmistry@google.comd6176b02012-08-23 18:14:13 +0000427static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000428 skiatest::Reporter*,
429 CanvasTestStep*) {
epoger@google.com94fa43c2012-04-11 17:51:01 +0000430 SkPaint paint;
431 paint.setStrokeWidth(SkIntToScalar(1));
432 paint.setStyle(SkPaint::kStroke_Style);
433
commit-bot@chromium.org8c2ee592014-03-07 18:42:15 +0000434 canvas->drawPath(kNearlyZeroLengthPath, paint);
epoger@google.com94fa43c2012-04-11 17:51:01 +0000435}
436TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
437
rmistry@google.comd6176b02012-08-23 18:14:13 +0000438static void DrawVerticesShaderTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000439 skiatest::Reporter*,
440 CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000441 SkPoint pts[4];
442 pts[0].set(0, 0);
443 pts[1].set(SkIntToScalar(kWidth), 0);
444 pts[2].set(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
445 pts[3].set(0, SkIntToScalar(kHeight));
446 SkPaint paint;
447 SkShader* shader = SkShader::CreateBitmapShader(kTestBitmap,
448 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
449 paint.setShader(shader)->unref();
450 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
451 NULL, NULL, NULL, 0, paint);
452}
edisonn@google.com77909122012-10-18 15:58:23 +0000453// NYI: issue 240.
454TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000455
rmistry@google.comd6176b02012-08-23 18:14:13 +0000456static void DrawPictureTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000457 skiatest::Reporter*,
458 CanvasTestStep*) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000459 SkPictureRecorder recorder;
mtklein8e126562014-10-01 09:29:35 -0700460 SkCanvas* testCanvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntToScalar(kHeight),
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700461 NULL, 0);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000462 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
463 testCanvas->clipRect(kTestRect);
464 testCanvas->drawRect(kTestRect, kTestPaint);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000465 SkAutoTUnref<SkPicture> testPicture(recorder.endRecording());
466
robertphillips9b14f262014-06-04 05:40:44 -0700467 canvas->drawPicture(testPicture);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000468}
469TEST_STEP(DrawPicture, DrawPictureTestStep);
470
rmistry@google.comd6176b02012-08-23 18:14:13 +0000471static void SaveRestoreTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000472 skiatest::Reporter* reporter,
473 CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000474 int baseSaveCount = canvas->getSaveCount();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000475 int n = canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000476 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount == n, testStep->assertMessage());
477 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000478 testStep->assertMessage());
479 canvas->save();
480 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000481 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000482 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000483 canvas->restoreToCount(baseSaveCount + 1);
484 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000485 testStep->assertMessage());
486
487 // should this pin to 1, or be a no-op, or crash?
488 canvas->restoreToCount(0);
489 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
490 testStep->assertMessage());
491}
492TEST_STEP(SaveRestore, SaveRestoreTestStep);
493
rmistry@google.comd6176b02012-08-23 18:14:13 +0000494static void DrawLayerTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000495 skiatest::Reporter* reporter,
496 CanvasTestStep* testStep) {
497 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
498 testStep->assertMessage());
499 canvas->save();
500 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
501 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000502 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000503
reed@google.com7c202932011-12-14 18:48:05 +0000504 const SkRect* bounds = NULL; // null means include entire bounds
505 const SkPaint* paint = NULL;
506
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000507 canvas->saveLayer(bounds, paint);
508 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
509 testStep->assertMessage());
510 canvas->restore();
511 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
512 testStep->assertMessage());
reed@google.com7c202932011-12-14 18:48:05 +0000513
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000514 canvas->saveLayer(bounds, paint);
515 canvas->saveLayer(bounds, paint);
516 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
517 testStep->assertMessage());
518 canvas->restore();
519 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
520 testStep->assertMessage());
521 canvas->restore();
reed@google.com7c202932011-12-14 18:48:05 +0000522 // now layer count should be 0
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000523 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
524 testStep->assertMessage());
525}
526TEST_STEP(DrawLayer, DrawLayerTestStep);
reed@google.com3b3e8952012-08-16 20:53:31 +0000527
528static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000529 skiatest::Reporter*,
530 CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000531 // This test step challenges the TestDeferredCanvasStateConsistency
532 // test cases because the opaque paint can trigger an optimization
533 // that discards previously recorded commands. The challenge is to maintain
534 // correct clip and matrix stack state.
535 canvas->resetMatrix();
536 canvas->rotate(SkIntToScalar(30));
537 canvas->save();
538 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
539 canvas->save();
540 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
541 SkPaint paint;
542 paint.setColor(0xFFFFFFFF);
543 canvas->drawPaint(paint);
544 canvas->restore();
545 canvas->restore();
546}
547TEST_STEP(NestedSaveRestoreWithSolidPaint, \
548 NestedSaveRestoreWithSolidPaintTestStep);
549
550static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000551 skiatest::Reporter*,
552 CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000553 // This test step challenges the TestDeferredCanvasStateConsistency
554 // test case because the canvas flush on a deferred canvas will
555 // reset the recording session. The challenge is to maintain correct
556 // clip and matrix stack state on the playback canvas.
557 canvas->resetMatrix();
558 canvas->rotate(SkIntToScalar(30));
559 canvas->save();
560 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
561 canvas->save();
562 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
563 canvas->drawRect(kTestRect,kTestPaint);
564 canvas->flush();
565 canvas->restore();
566 canvas->restore();
567}
568TEST_STEP(NestedSaveRestoreWithFlush, \
569 NestedSaveRestoreWithFlushTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000570
571static void AssertCanvasStatesEqual(skiatest::Reporter* reporter,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000572 const SkCanvas* canvas1,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000573 const SkCanvas* canvas2,
574 CanvasTestStep* testStep) {
575 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
576 canvas2->getDeviceSize(), testStep->assertMessage());
577 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
578 canvas2->getSaveCount(), testStep->assertMessage());
579 REPORTER_ASSERT_MESSAGE(reporter, canvas1->isDrawingToLayer() ==
580 canvas2->isDrawingToLayer(), testStep->assertMessage());
reed@google.com3b3e8952012-08-16 20:53:31 +0000581
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000582 SkRect bounds1, bounds2;
583 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.com3b3e8952012-08-16 20:53:31 +0000584 canvas1->getClipBounds(&bounds1) == canvas2->getClipBounds(&bounds2),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000585 testStep->assertMessage());
586 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
reed@google.com3b3e8952012-08-16 20:53:31 +0000587 testStep->assertMessage());
588
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000589 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
590 canvas2->getDrawFilter(), testStep->assertMessage());
591 SkIRect deviceBounds1, deviceBounds2;
592 REPORTER_ASSERT_MESSAGE(reporter,
593 canvas1->getClipDeviceBounds(&deviceBounds1) ==
594 canvas2->getClipDeviceBounds(&deviceBounds2),
595 testStep->assertMessage());
reed868074b2014-06-03 10:53:59 -0700596 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2, testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000597 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
598 canvas2->getTotalMatrix(), testStep->assertMessage());
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000599 REPORTER_ASSERT_MESSAGE(reporter, equal_clips(*canvas1, *canvas2), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000600
601 // The following test code is commented out because the test fails when
rmistry@google.comd6176b02012-08-23 18:14:13 +0000602 // the canvas is an SkPictureRecord or SkDeferredCanvas
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000603 // Issue: http://code.google.com/p/skia/issues/detail?id=498
604 // Also, creating a LayerIter on an SkProxyCanvas crashes
605 // Issue: http://code.google.com/p/skia/issues/detail?id=499
606 /*
607 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
608 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
609 while (!layerIter1.done() && !layerIter2.done()) {
610 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
611 layerIter2.matrix(), testStep->assertMessage());
612 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
613 layerIter2.clip(), testStep->assertMessage());
614 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
615 layerIter2.paint(), testStep->assertMessage());
616 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
617 layerIter2.x(), testStep->assertMessage());
618 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
619 layerIter2.y(), testStep->assertMessage());
620 layerIter1.next();
621 layerIter2.next();
622 }
623 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
624 testStep->assertMessage());
625 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
626 testStep->assertMessage());
627 */
628}
629
630// The following class groups static functions that need to access
631// the privates members of SkPictureRecord
632class SkPictureTester {
633private:
reed@google.come2589ae2012-07-10 19:38:01 +0000634 static int EQ(const SkFlatData* a, const SkFlatData* b) {
635 return *a == *b;
636 }
637
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000638 static void AssertFlattenedObjectsEqual(
639 SkPictureRecord* referenceRecord,
640 SkPictureRecord* testRecord,
641 skiatest::Reporter* reporter,
642 CanvasTestStep* testStep) {
643
644 REPORTER_ASSERT_MESSAGE(reporter,
djsollen@google.comc9ab9872012-08-29 18:52:07 +0000645 referenceRecord->fBitmapHeap->count() ==
646 testRecord->fBitmapHeap->count(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000647 REPORTER_ASSERT_MESSAGE(reporter,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000648 referenceRecord->fPaints.count() ==
649 testRecord->fPaints.count(), testStep->assertMessage());
650 for (int i = 0; i < referenceRecord->fPaints.count(); ++i) {
651 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.come2589ae2012-07-10 19:38:01 +0000652 EQ(referenceRecord->fPaints[i], testRecord->fPaints[i]),
653 testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000654 }
655 REPORTER_ASSERT_MESSAGE(reporter,
robertphillips0bdbea72014-06-11 11:37:55 -0700656 !referenceRecord->fPathHeap == !testRecord->fPathHeap,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000657 testStep->assertMessage());
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000658 // The following tests are commented out because they currently
659 // fail. Issue: http://code.google.com/p/skia/issues/detail?id=507
660 /*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000661 if (referenceRecord->fPathHeap) {
662 REPORTER_ASSERT_MESSAGE(reporter,
663 referenceRecord->fPathHeap->count() ==
664 testRecord->fPathHeap->count(),
665 testStep->assertMessage());
666 for (int i = 0; i < referenceRecord->fPathHeap->count(); ++i) {
667 REPORTER_ASSERT_MESSAGE(reporter,
668 (*referenceRecord->fPathHeap)[i] ==
669 (*testRecord->fPathHeap)[i], testStep->assertMessage());
670 }
671 }
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000672 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000673
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000674 }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000675};
676
edisonn@google.com77909122012-10-18 15:58:23 +0000677static void TestPdfDevice(skiatest::Reporter* reporter,
678 CanvasTestStep* testStep) {
679 SkISize pageSize = SkISize::Make(kWidth, kHeight);
680 SkPDFDevice device(pageSize, pageSize, SkMatrix::I());
681 SkCanvas canvas(&device);
682 testStep->setAssertMessageFormat(kPdfAssertMessageFormat);
683 testStep->draw(&canvas, reporter);
684 SkPDFDocument doc;
685 doc.appendPage(&device);
686 SkDynamicMemoryWStream stream;
687 doc.emitPDF(&stream);
688}
689
junov@chromium.org88e29142012-08-07 16:48:22 +0000690// The following class groups static functions that need to access
691// the privates members of SkDeferredCanvas
692class SkDeferredCanvasTester {
693public:
694 static void TestDeferredCanvasStateConsistency(
695 skiatest::Reporter* reporter,
696 CanvasTestStep* testStep,
junov@chromium.orgfb103892012-09-20 19:35:43 +0000697 const SkCanvas& referenceCanvas, bool silent) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000698
reed@google.com28183b42014-02-04 15:34:10 +0000699 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
700 SkAutoTUnref<SkDeferredCanvas> deferredCanvas(SkDeferredCanvas::Create(surface.get()));
701
junov@chromium.org88e29142012-08-07 16:48:22 +0000702 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
junov@chromium.org66070a52013-05-28 17:39:08 +0000703 testStep->draw(deferredCanvas, reporter);
junov@chromium.org88e29142012-08-07 16:48:22 +0000704 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
junov@chromium.org66070a52013-05-28 17:39:08 +0000705 AssertCanvasStatesEqual(reporter, deferredCanvas, &referenceCanvas,
junov@chromium.org88e29142012-08-07 16:48:22 +0000706 testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000707
junov@chromium.orgfb103892012-09-20 19:35:43 +0000708 if (silent) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000709 deferredCanvas->silentFlush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000710 } else {
junov@chromium.org66070a52013-05-28 17:39:08 +0000711 deferredCanvas->flush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000712 }
713
skia.committer@gmail.com4c5ea442012-09-21 02:01:01 +0000714 testStep->setAssertMessageFormat(
junov@chromium.orgfb103892012-09-20 19:35:43 +0000715 silent ? kDeferredPostSilentFlushPlaybackAssertMessageFormat :
junov@chromium.org88e29142012-08-07 16:48:22 +0000716 kDeferredPostFlushPlaybackAssertMessageFormat);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000717 AssertCanvasStatesEqual(reporter,
junov@chromium.org66070a52013-05-28 17:39:08 +0000718 deferredCanvas->immediateCanvas(),
junov@chromium.org88e29142012-08-07 16:48:22 +0000719 &referenceCanvas, testStep);
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000720
junov@chromium.org88e29142012-08-07 16:48:22 +0000721 // Verified that deferred canvas state is not affected by flushing
722 // pending draw operations
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000723
junov@chromium.org88e29142012-08-07 16:48:22 +0000724 // The following test code is commented out because it currently fails.
725 // Issue: http://code.google.com/p/skia/issues/detail?id=496
726 /*
727 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
728 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
729 testStep);
730 */
731 }
732};
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000733
caryclark@google.com42639cd2012-06-06 12:03:39 +0000734// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000735static void TestProxyCanvasStateConsistency(
736 skiatest::Reporter* reporter,
737 CanvasTestStep* testStep,
738 const SkCanvas& referenceCanvas) {
739
740 SkBitmap indirectStore;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000741 createBitmap(&indirectStore, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700742 SkCanvas indirectCanvas(indirectStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000743 SkProxyCanvas proxyCanvas(&indirectCanvas);
744 testStep->setAssertMessageFormat(kProxyDrawAssertMessageFormat);
745 testStep->draw(&proxyCanvas, reporter);
746 // Verify that the SkProxyCanvas reports consitent state
747 testStep->setAssertMessageFormat(kProxyStateAssertMessageFormat);
748 AssertCanvasStatesEqual(reporter, &proxyCanvas, &referenceCanvas,
749 testStep);
750 // Verify that the indirect canvas reports consitent state
751 testStep->setAssertMessageFormat(kProxyIndirectStateAssertMessageFormat);
752 AssertCanvasStatesEqual(reporter, &indirectCanvas, &referenceCanvas,
753 testStep);
754}
755
caryclark@google.com42639cd2012-06-06 12:03:39 +0000756// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000757static void TestNWayCanvasStateConsistency(
758 skiatest::Reporter* reporter,
759 CanvasTestStep* testStep,
760 const SkCanvas& referenceCanvas) {
761
762 SkBitmap indirectStore1;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000763 createBitmap(&indirectStore1, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700764 SkCanvas indirectCanvas1(indirectStore1);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000765
766 SkBitmap indirectStore2;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000767 createBitmap(&indirectStore2, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700768 SkCanvas indirectCanvas2(indirectStore2);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000769
djsollen@google.comf0a062b2012-05-01 16:50:25 +0000770 SkISize canvasSize = referenceCanvas.getDeviceSize();
771 SkNWayCanvas nWayCanvas(canvasSize.width(), canvasSize.height());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000772 nWayCanvas.addCanvas(&indirectCanvas1);
773 nWayCanvas.addCanvas(&indirectCanvas2);
774
775 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
776 testStep->draw(&nWayCanvas, reporter);
777 // Verify that the SkProxyCanvas reports consitent state
778 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
779 AssertCanvasStatesEqual(reporter, &nWayCanvas, &referenceCanvas,
780 testStep);
781 // Verify that the indirect canvases report consitent state
782 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
783 AssertCanvasStatesEqual(reporter, &indirectCanvas1, &referenceCanvas,
784 testStep);
785 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
786 AssertCanvasStatesEqual(reporter, &indirectCanvas2, &referenceCanvas,
787 testStep);
788}
789
790/*
791 * This sub-test verifies that the test step passes when executed
792 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
793 * that the all canvas derivatives report the same state as an SkCanvas
794 * after having executed the test step.
795 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000796static void TestOverrideStateConsistency(skiatest::Reporter* reporter,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000797 CanvasTestStep* testStep) {
798 SkBitmap referenceStore;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000799 createBitmap(&referenceStore, 0xFFFFFFFF);
reed2a8ca932014-06-26 22:12:09 -0700800 SkCanvas referenceCanvas(referenceStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000801 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
802 testStep->draw(&referenceCanvas, reporter);
803
junov@chromium.orgfb103892012-09-20 19:35:43 +0000804 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, false);
805
806 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, true);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000807
caryclark@google.com42639cd2012-06-06 12:03:39 +0000808 // The following test code is disabled because SkProxyCanvas is
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000809 // missing a lot of virtual overrides on get* methods, which are used
810 // to verify canvas state.
811 // Issue: http://code.google.com/p/skia/issues/detail?id=500
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000812
caryclark@google.com42639cd2012-06-06 12:03:39 +0000813 if (false) { // avoid bit rot, suppress warning
814 TestProxyCanvasStateConsistency(reporter, testStep, referenceCanvas);
815 }
816
817 // The following test code is disabled because SkNWayCanvas does not
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000818 // report correct clipping and device bounds information
819 // Issue: http://code.google.com/p/skia/issues/detail?id=501
caryclark@google.com42639cd2012-06-06 12:03:39 +0000820
821 if (false) { // avoid bit rot, suppress warning
822 TestNWayCanvasStateConsistency(reporter, testStep, referenceCanvas);
823 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000824
caryclark@google.com42639cd2012-06-06 12:03:39 +0000825 if (false) { // avoid bit rot, suppress warning
826 test_clipVisitor(reporter, &referenceCanvas);
827 }
reed@google.com7c202932011-12-14 18:48:05 +0000828}
reed@google.com37f3ae02011-11-28 16:06:04 +0000829
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000830static void test_newraster(skiatest::Reporter* reporter) {
831 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
832 SkCanvas* canvas = SkCanvas::NewRaster(info);
833 REPORTER_ASSERT(reporter, canvas);
834
835 SkImageInfo info2;
836 size_t rowBytes;
837 const SkPMColor* addr = (const SkPMColor*)canvas->peekPixels(&info2, &rowBytes);
838 REPORTER_ASSERT(reporter, addr);
839 REPORTER_ASSERT(reporter, info == info2);
840 for (int y = 0; y < info.height(); ++y) {
841 for (int x = 0; x < info.width(); ++x) {
842 REPORTER_ASSERT(reporter, 0 == addr[x]);
843 }
844 addr = (const SkPMColor*)((const char*)addr + rowBytes);
845 }
846 SkDELETE(canvas);
847
848 // now try a deliberately bad info
reede5ea5002014-09-03 11:54:58 -0700849 info = info.makeWH(-1, info.height());
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000850 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRaster(info));
851
852 // too big
reede5ea5002014-09-03 11:54:58 -0700853 info = info.makeWH(1 << 30, 1 << 30);
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000854 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRaster(info));
skia.committer@gmail.com0e530752014-02-28 03:02:05 +0000855
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000856 // not a valid pixel type
reede5ea5002014-09-03 11:54:58 -0700857 info = SkImageInfo::Make(10, 10, kUnknown_SkColorType, info.alphaType());
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000858 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRaster(info));
859
860 // We should succeed with a zero-sized valid info
861 info = SkImageInfo::MakeN32Premul(0, 0);
862 canvas = SkCanvas::NewRaster(info);
863 REPORTER_ASSERT(reporter, canvas);
864 SkDELETE(canvas);
865}
866
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000867DEF_TEST(Canvas, reporter) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000868 // Init global here because bitmap pixels cannot be alocated during
869 // static initialization
870 kTestBitmap = testBitmap();
reed@google.com37f3ae02011-11-28 16:06:04 +0000871
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000872 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
873 TestOverrideStateConsistency(reporter, testStepArray()[testStep]);
edisonn@google.com77909122012-10-18 15:58:23 +0000874 if (testStepArray()[testStep]->enablePdfTesting()) {
875 TestPdfDevice(reporter, testStepArray()[testStep]);
876 }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000877 }
junov@chromium.orgcd62ecf2012-08-02 17:43:25 +0000878
879 // Explicitly call reset(), so we don't leak the pixels (since kTestBitmap is a global)
880 kTestBitmap.reset();
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000881
882 test_newraster(reporter);
reed@google.com37f3ae02011-11-28 16:06:04 +0000883}