blob: e2ad2ed6b3244b3a027ef29296b64ff6d356b3c8 [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 kPictureDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000125 "Drawing test step %s with SkPicture";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000126static const char* const kPictureSecondDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000127 "Duplicate draw of test step %s with SkPicture";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000128static const char* const kDeferredDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000129 "Drawing test step %s with SkDeferredCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000130static const char* const kProxyDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000131 "Drawing test step %s with SkProxyCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000132static const char* const kNWayDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000133 "Drawing test step %s with SkNWayCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000134static const char* const kDeferredPreFlushAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000135 "test step %s, SkDeferredCanvas state consistency before flush";
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000136static const char* const kDeferredPostFlushPlaybackAssertMessageFormat =
137 "test step %s, SkDeferredCanvas playback canvas state consistency after flush";
junov@chromium.orgfb103892012-09-20 19:35:43 +0000138static const char* const kDeferredPostSilentFlushPlaybackAssertMessageFormat =
139 "test step %s, SkDeferredCanvas playback canvas state consistency after silent flush";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000140static const char* const kPictureResourceReuseMessageFormat =
141 "test step %s, SkPicture duplicate flattened object test";
142static const char* const kProxyStateAssertMessageFormat =
143 "test step %s, SkProxyCanvas state consistency";
144static const char* const kProxyIndirectStateAssertMessageFormat =
145 "test step %s, SkProxyCanvas indirect canvas state consistency";
146static const char* const kNWayStateAssertMessageFormat =
147 "test step %s, SkNWayCanvas state consistency";
148static const char* const kNWayIndirect1StateAssertMessageFormat =
149 "test step %s, SkNWayCanvas indirect canvas 1 state consistency";
150static const char* const kNWayIndirect2StateAssertMessageFormat =
151 "test step %s, SkNWayCanvas indirect canvas 2 state consistency";
edisonn@google.com77909122012-10-18 15:58:23 +0000152static const char* const kPdfAssertMessageFormat =
153 "PDF sanity check failed %s";
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000154
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000155static void createBitmap(SkBitmap* bm, SkColor color) {
156 bm->allocN32Pixels(kWidth, kHeight);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000157 bm->eraseColor(color);
158}
159
reed@google.com28183b42014-02-04 15:34:10 +0000160static SkSurface* createSurface(SkColor color) {
161 SkSurface* surface = SkSurface::NewRasterPMColor(kWidth, kHeight);
162 surface->getCanvas()->clear(color);
163 return surface;
164}
165
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000166class CanvasTestStep;
167static SkTDArray<CanvasTestStep*>& testStepArray() {
168 static SkTDArray<CanvasTestStep*> theTests;
169 return theTests;
170}
171
172class CanvasTestStep {
173public:
edisonn@google.com77909122012-10-18 15:58:23 +0000174 CanvasTestStep(bool fEnablePdfTesting = true) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000175 *testStepArray().append() = this;
176 fAssertMessageFormat = kDefaultAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000177 this->fEnablePdfTesting = fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000178 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000179 virtual ~CanvasTestStep() { }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000180
181 virtual void draw(SkCanvas*, skiatest::Reporter*) = 0;
182 virtual const char* name() const = 0;
183
184 const char* assertMessage() {
185 fAssertMessage.printf(fAssertMessageFormat, name());
186 return fAssertMessage.c_str();
187 }
188
189 void setAssertMessageFormat(const char* format) {
190 fAssertMessageFormat = format;
191 }
192
edisonn@google.com77909122012-10-18 15:58:23 +0000193 bool enablePdfTesting() { return fEnablePdfTesting; }
194
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000195private:
196 SkString fAssertMessage;
197 const char* fAssertMessageFormat;
edisonn@google.com77909122012-10-18 15:58:23 +0000198 bool fEnablePdfTesting;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000199};
200
201///////////////////////////////////////////////////////////////////////////////
202// Constants used by test steps
203
rmistry@google.comd6176b02012-08-23 18:14:13 +0000204const SkRect kTestRect =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000205 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
206 SkIntToScalar(2), SkIntToScalar(1));
207static SkMatrix testMatrix() {
208 SkMatrix matrix;
209 matrix.reset();
210 matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
211 return matrix;
212}
213const SkMatrix kTestMatrix = testMatrix();
commit-bot@chromium.org8c2ee592014-03-07 18:42:15 +0000214static SkPath test_path() {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000215 SkPath path;
216 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
217 SkIntToScalar(2), SkIntToScalar(1)));
218 return path;
219}
commit-bot@chromium.org8c2ee592014-03-07 18:42:15 +0000220const SkPath kTestPath = test_path();
221static SkPath test_nearly_zero_length_path() {
222 SkPath path;
223 SkPoint pt1 = { 0, 0 };
224 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
225 SkPoint pt3 = { SkIntToScalar(1), 0 };
226 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
227 path.moveTo(pt1);
228 path.lineTo(pt2);
229 path.lineTo(pt3);
230 path.lineTo(pt4);
231 return path;
232}
233const SkPath kNearlyZeroLengthPath = test_nearly_zero_length_path();
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000234static SkRegion testRegion() {
235 SkRegion region;
236 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
237 region.setRect(rect);
238 return region;
239}
240const SkIRect kTestIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
241const SkRegion kTestRegion = testRegion();
242const SkColor kTestColor = 0x01020304;
243const SkPaint kTestPaint;
244const SkPoint kTestPoints[3] = {
245 {SkIntToScalar(0), SkIntToScalar(0)},
246 {SkIntToScalar(2), SkIntToScalar(1)},
247 {SkIntToScalar(0), SkIntToScalar(2)}
248};
249const size_t kTestPointCount = 3;
250static SkBitmap testBitmap() {
251 SkBitmap bitmap;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000252 createBitmap(&bitmap, 0x05060708);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000253 return bitmap;
254}
255SkBitmap kTestBitmap; // cannot be created during static init
256SkString kTestText("Hello World");
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000257SkPoint kTestPoints2[] = {
258 { SkIntToScalar(0), SkIntToScalar(1) },
259 { SkIntToScalar(1), SkIntToScalar(1) },
260 { SkIntToScalar(2), SkIntToScalar(1) },
261 { SkIntToScalar(3), SkIntToScalar(1) },
262 { SkIntToScalar(4), SkIntToScalar(1) },
263 { SkIntToScalar(5), SkIntToScalar(1) },
264 { SkIntToScalar(6), SkIntToScalar(1) },
265 { SkIntToScalar(7), SkIntToScalar(1) },
266 { SkIntToScalar(8), SkIntToScalar(1) },
267 { SkIntToScalar(9), SkIntToScalar(1) },
268 { SkIntToScalar(10), SkIntToScalar(1) },
269};
rmistry@google.comd6176b02012-08-23 18:14:13 +0000270
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000271
272///////////////////////////////////////////////////////////////////////////////
273// Macros for defining test steps
274
275#define TEST_STEP(NAME, FUNCTION) \
276class NAME##_TestStep : public CanvasTestStep{ \
277public: \
278 virtual void draw(SkCanvas* canvas, skiatest::Reporter* reporter) { \
279 FUNCTION (canvas, reporter, this); \
280 } \
281 virtual const char* name() const {return #NAME ;} \
282}; \
283static NAME##_TestStep NAME##_TestStepInstance;
284
edisonn@google.com77909122012-10-18 15:58:23 +0000285#define TEST_STEP_NO_PDF(NAME, FUNCTION) \
286class NAME##_TestStep : public CanvasTestStep{ \
287public: \
288 NAME##_TestStep() : CanvasTestStep(false) {} \
289 virtual void draw(SkCanvas* canvas, skiatest::Reporter* reporter) { \
290 FUNCTION (canvas, reporter, this); \
291 } \
292 virtual const char* name() const {return #NAME ;} \
293}; \
294static NAME##_TestStep NAME##_TestStepInstance;
295
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000296#define SIMPLE_TEST_STEP(NAME, CALL) \
297static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter*, \
298 CanvasTestStep*) { \
299 canvas-> CALL ; \
300} \
301TEST_STEP(NAME, NAME##TestStep )
302
303#define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
304static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter* reporter, \
305 CanvasTestStep* testStep) { \
306 REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \
307 testStep->assertMessage()); \
308} \
309TEST_STEP(NAME, NAME##TestStep )
310
311
312///////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000313// Basic test steps for most virtual methods in SkCanvas that draw or affect
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000314// the state of the canvas.
315
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000316SIMPLE_TEST_STEP(Translate, translate(SkIntToScalar(1), SkIntToScalar(2)));
317SIMPLE_TEST_STEP(Scale, scale(SkIntToScalar(1), SkIntToScalar(2)));
318SIMPLE_TEST_STEP(Rotate, rotate(SkIntToScalar(1)));
319SIMPLE_TEST_STEP(Skew, skew(SkIntToScalar(1), SkIntToScalar(2)));
320SIMPLE_TEST_STEP(Concat, concat(kTestMatrix));
junov@chromium.orga907ac32012-02-24 21:54:07 +0000321SIMPLE_TEST_STEP(SetMatrix, setMatrix(kTestMatrix));
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000322SIMPLE_TEST_STEP(ClipRect, clipRect(kTestRect));
323SIMPLE_TEST_STEP(ClipPath, clipPath(kTestPath));
324SIMPLE_TEST_STEP(ClipRegion,
junov@chromium.orga907ac32012-02-24 21:54:07 +0000325 clipRegion(kTestRegion, SkRegion::kReplace_Op));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000326SIMPLE_TEST_STEP(Clear, clear(kTestColor));
327SIMPLE_TEST_STEP(DrawPaint, drawPaint(kTestPaint));
328SIMPLE_TEST_STEP(DrawPointsPoints, drawPoints(SkCanvas::kPoints_PointMode,
329 kTestPointCount, kTestPoints, kTestPaint));
330SIMPLE_TEST_STEP(DrawPointsLiness, drawPoints(SkCanvas::kLines_PointMode,
331 kTestPointCount, kTestPoints, kTestPaint));
332SIMPLE_TEST_STEP(DrawPointsPolygon, drawPoints(SkCanvas::kPolygon_PointMode,
333 kTestPointCount, kTestPoints, kTestPaint));
334SIMPLE_TEST_STEP(DrawRect, drawRect(kTestRect, kTestPaint));
335SIMPLE_TEST_STEP(DrawPath, drawPath(kTestPath, kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000336SIMPLE_TEST_STEP(DrawBitmap, drawBitmap(kTestBitmap, 0, 0));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000337SIMPLE_TEST_STEP(DrawBitmapPaint, drawBitmap(kTestBitmap, 0, 0, &kTestPaint));
338SIMPLE_TEST_STEP(DrawBitmapRect, drawBitmapRect(kTestBitmap, NULL, kTestRect,
339 NULL));
340SIMPLE_TEST_STEP(DrawBitmapRectSrcRect, drawBitmapRect(kTestBitmap,
341 &kTestIRect, kTestRect, NULL));
342SIMPLE_TEST_STEP(DrawBitmapRectPaint, drawBitmapRect(kTestBitmap, NULL,
343 kTestRect, &kTestPaint));
344SIMPLE_TEST_STEP(DrawBitmapMatrix, drawBitmapMatrix(kTestBitmap, kTestMatrix,
345 NULL));
346SIMPLE_TEST_STEP(DrawBitmapMatrixPaint, drawBitmapMatrix(kTestBitmap,
347 kTestMatrix, &kTestPaint));
348SIMPLE_TEST_STEP(DrawBitmapNine, drawBitmapNine(kTestBitmap, kTestIRect,
349 kTestRect, NULL));
350SIMPLE_TEST_STEP(DrawBitmapNinePaint, drawBitmapNine(kTestBitmap, kTestIRect,
351 kTestRect, &kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000352SIMPLE_TEST_STEP(DrawSprite, drawSprite(kTestBitmap, 0, 0, NULL));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000353SIMPLE_TEST_STEP(DrawSpritePaint, drawSprite(kTestBitmap, 0, 0, &kTestPaint));
354SIMPLE_TEST_STEP(DrawText, drawText(kTestText.c_str(), kTestText.size(),
355 0, 1, kTestPaint));
356SIMPLE_TEST_STEP(DrawPosText, drawPosText(kTestText.c_str(),
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000357 kTestText.size(), kTestPoints2, kTestPaint));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000358SIMPLE_TEST_STEP(DrawTextOnPath, drawTextOnPath(kTestText.c_str(),
359 kTestText.size(), kTestPath, NULL, kTestPaint));
360SIMPLE_TEST_STEP(DrawTextOnPathMatrix, drawTextOnPath(kTestText.c_str(),
361 kTestText.size(), kTestPath, &kTestMatrix, kTestPaint));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000362SIMPLE_TEST_STEP(DrawData, drawData(kTestText.c_str(), kTestText.size()));
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000363SIMPLE_TEST_STEP(BeginGroup, beginCommentGroup(kTestText.c_str()));
364SIMPLE_TEST_STEP(AddComment, addComment(kTestText.c_str(), kTestText.c_str()));
365SIMPLE_TEST_STEP(EndGroup, endCommentGroup());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000366
367///////////////////////////////////////////////////////////////////////////////
368// Complex test steps
369
rmistry@google.comd6176b02012-08-23 18:14:13 +0000370// Save/restore calls cannot be in isolated simple test steps because the test
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000371// cases that use SkPicture require that save and restore calls be balanced.
rmistry@google.comd6176b02012-08-23 18:14:13 +0000372static void SaveMatrixStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000373 skiatest::Reporter* reporter,
374 CanvasTestStep* testStep) {
375 int saveCount = canvas->getSaveCount();
376 canvas->save(SkCanvas::kMatrix_SaveFlag);
377 canvas->clipRegion(kTestRegion);
378 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
379 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000380 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000381 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000382 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000383 testStep->assertMessage());
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000384// REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() == kTestRegion, testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000385}
386TEST_STEP(SaveMatrix, SaveMatrixStep);
387
rmistry@google.comd6176b02012-08-23 18:14:13 +0000388static void SaveClipStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000389 skiatest::Reporter* reporter,
390 CanvasTestStep* testStep) {
391 int saveCount = canvas->getSaveCount();
392 canvas->save(SkCanvas::kClip_SaveFlag);
393 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
394 canvas->clipRegion(kTestRegion);
395 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000396 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000397 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000398 REPORTER_ASSERT_MESSAGE(reporter, !canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000399 testStep->assertMessage());
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000400// REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion, testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000401}
402TEST_STEP(SaveClip, SaveClipStep);
403
rmistry@google.comd6176b02012-08-23 18:14:13 +0000404static void SaveMatrixClipStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000405 skiatest::Reporter* reporter,
406 CanvasTestStep* testStep) {
407 int saveCount = canvas->getSaveCount();
408 canvas->save(SkCanvas::kMatrixClip_SaveFlag);
409 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
410 canvas->clipRegion(kTestRegion);
411 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000412 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000413 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000414 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000415 testStep->assertMessage());
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000416// REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion, testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000417}
418TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
419
rmistry@google.comd6176b02012-08-23 18:14:13 +0000420static void SaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000421 skiatest::Reporter* reporter,
422 CanvasTestStep* testStep) {
423 int saveCount = canvas->getSaveCount();
424 canvas->saveLayer(NULL, NULL);
425 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000426 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000427 testStep->assertMessage());
428}
429TEST_STEP(SaveLayer, SaveLayerStep);
430
rmistry@google.comd6176b02012-08-23 18:14:13 +0000431static void BoundedSaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000432 skiatest::Reporter* reporter,
433 CanvasTestStep* testStep) {
434 int saveCount = canvas->getSaveCount();
435 canvas->saveLayer(&kTestRect, NULL);
436 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000437 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000438 testStep->assertMessage());
439}
440TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
441
rmistry@google.comd6176b02012-08-23 18:14:13 +0000442static void PaintSaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000443 skiatest::Reporter* reporter,
444 CanvasTestStep* testStep) {
445 int saveCount = canvas->getSaveCount();
446 canvas->saveLayer(NULL, &kTestPaint);
447 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000448 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000449 testStep->assertMessage());
450}
451TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
452
rmistry@google.comd6176b02012-08-23 18:14:13 +0000453static void TwoClipOpsStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000454 skiatest::Reporter*,
455 CanvasTestStep*) {
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000456 // This test exercises a functionality in SkPicture that leads to the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000457 // recording of restore offset placeholders. This test will trigger an
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000458 // assertion at playback time if the placeholders are not properly
459 // filled when the recording ends.
460 canvas->clipRect(kTestRect);
461 canvas->clipRegion(kTestRegion);
462}
463TEST_STEP(TwoClipOps, TwoClipOpsStep);
464
epoger@google.com94fa43c2012-04-11 17:51:01 +0000465// exercise fix for http://code.google.com/p/skia/issues/detail?id=560
466// ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
rmistry@google.comd6176b02012-08-23 18:14:13 +0000467static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000468 skiatest::Reporter*,
469 CanvasTestStep*) {
epoger@google.com94fa43c2012-04-11 17:51:01 +0000470 SkPaint paint;
471 paint.setStrokeWidth(SkIntToScalar(1));
472 paint.setStyle(SkPaint::kStroke_Style);
473
commit-bot@chromium.org8c2ee592014-03-07 18:42:15 +0000474 canvas->drawPath(kNearlyZeroLengthPath, paint);
epoger@google.com94fa43c2012-04-11 17:51:01 +0000475}
476TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
477
rmistry@google.comd6176b02012-08-23 18:14:13 +0000478static void DrawVerticesShaderTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000479 skiatest::Reporter*,
480 CanvasTestStep*) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000481 SkPoint pts[4];
482 pts[0].set(0, 0);
483 pts[1].set(SkIntToScalar(kWidth), 0);
484 pts[2].set(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
485 pts[3].set(0, SkIntToScalar(kHeight));
486 SkPaint paint;
487 SkShader* shader = SkShader::CreateBitmapShader(kTestBitmap,
488 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
489 paint.setShader(shader)->unref();
490 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
491 NULL, NULL, NULL, 0, paint);
492}
edisonn@google.com77909122012-10-18 15:58:23 +0000493// NYI: issue 240.
494TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000495
rmistry@google.comd6176b02012-08-23 18:14:13 +0000496static void DrawPictureTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000497 skiatest::Reporter*,
498 CanvasTestStep*) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000499 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000500 SkCanvas* testCanvas = recorder.beginRecording(kWidth, kHeight, NULL, 0);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000501 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
502 testCanvas->clipRect(kTestRect);
503 testCanvas->drawRect(kTestRect, kTestPaint);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000504 SkAutoTUnref<SkPicture> testPicture(recorder.endRecording());
505
robertphillips9b14f262014-06-04 05:40:44 -0700506 canvas->drawPicture(testPicture);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000507}
508TEST_STEP(DrawPicture, DrawPictureTestStep);
509
rmistry@google.comd6176b02012-08-23 18:14:13 +0000510static void SaveRestoreTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000511 skiatest::Reporter* reporter,
512 CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000513 int baseSaveCount = canvas->getSaveCount();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000514 int n = canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000515 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount == n, testStep->assertMessage());
516 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000517 testStep->assertMessage());
518 canvas->save();
519 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000520 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000521 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000522 canvas->restoreToCount(baseSaveCount + 1);
523 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000524 testStep->assertMessage());
525
526 // should this pin to 1, or be a no-op, or crash?
527 canvas->restoreToCount(0);
528 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
529 testStep->assertMessage());
530}
531TEST_STEP(SaveRestore, SaveRestoreTestStep);
532
rmistry@google.comd6176b02012-08-23 18:14:13 +0000533static void DrawLayerTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000534 skiatest::Reporter* reporter,
535 CanvasTestStep* testStep) {
536 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
537 testStep->assertMessage());
538 canvas->save();
539 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
540 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000541 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000542
reed@google.com7c202932011-12-14 18:48:05 +0000543 const SkRect* bounds = NULL; // null means include entire bounds
544 const SkPaint* paint = NULL;
545
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000546 canvas->saveLayer(bounds, paint);
547 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
548 testStep->assertMessage());
549 canvas->restore();
550 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
551 testStep->assertMessage());
reed@google.com7c202932011-12-14 18:48:05 +0000552
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000553 canvas->saveLayer(bounds, paint);
554 canvas->saveLayer(bounds, paint);
555 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
556 testStep->assertMessage());
557 canvas->restore();
558 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
559 testStep->assertMessage());
560 canvas->restore();
reed@google.com7c202932011-12-14 18:48:05 +0000561 // now layer count should be 0
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000562 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
563 testStep->assertMessage());
564}
565TEST_STEP(DrawLayer, DrawLayerTestStep);
reed@google.com3b3e8952012-08-16 20:53:31 +0000566
567static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000568 skiatest::Reporter*,
569 CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000570 // This test step challenges the TestDeferredCanvasStateConsistency
571 // test cases because the opaque paint can trigger an optimization
572 // that discards previously recorded commands. The challenge is to maintain
573 // correct clip and matrix stack state.
574 canvas->resetMatrix();
575 canvas->rotate(SkIntToScalar(30));
576 canvas->save();
577 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
578 canvas->save();
579 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
580 SkPaint paint;
581 paint.setColor(0xFFFFFFFF);
582 canvas->drawPaint(paint);
583 canvas->restore();
584 canvas->restore();
585}
586TEST_STEP(NestedSaveRestoreWithSolidPaint, \
587 NestedSaveRestoreWithSolidPaintTestStep);
588
589static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000590 skiatest::Reporter*,
591 CanvasTestStep*) {
reed@google.com3b3e8952012-08-16 20:53:31 +0000592 // This test step challenges the TestDeferredCanvasStateConsistency
593 // test case because the canvas flush on a deferred canvas will
594 // reset the recording session. The challenge is to maintain correct
595 // clip and matrix stack state on the playback canvas.
596 canvas->resetMatrix();
597 canvas->rotate(SkIntToScalar(30));
598 canvas->save();
599 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
600 canvas->save();
601 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
602 canvas->drawRect(kTestRect,kTestPaint);
603 canvas->flush();
604 canvas->restore();
605 canvas->restore();
606}
607TEST_STEP(NestedSaveRestoreWithFlush, \
608 NestedSaveRestoreWithFlushTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000609
610static void AssertCanvasStatesEqual(skiatest::Reporter* reporter,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000611 const SkCanvas* canvas1,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000612 const SkCanvas* canvas2,
613 CanvasTestStep* testStep) {
614 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
615 canvas2->getDeviceSize(), testStep->assertMessage());
616 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
617 canvas2->getSaveCount(), testStep->assertMessage());
618 REPORTER_ASSERT_MESSAGE(reporter, canvas1->isDrawingToLayer() ==
619 canvas2->isDrawingToLayer(), testStep->assertMessage());
reed@google.com3b3e8952012-08-16 20:53:31 +0000620
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000621 SkRect bounds1, bounds2;
622 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.com3b3e8952012-08-16 20:53:31 +0000623 canvas1->getClipBounds(&bounds1) == canvas2->getClipBounds(&bounds2),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000624 testStep->assertMessage());
625 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
reed@google.com3b3e8952012-08-16 20:53:31 +0000626 testStep->assertMessage());
627
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000628 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
629 canvas2->getDrawFilter(), testStep->assertMessage());
630 SkIRect deviceBounds1, deviceBounds2;
631 REPORTER_ASSERT_MESSAGE(reporter,
632 canvas1->getClipDeviceBounds(&deviceBounds1) ==
633 canvas2->getClipDeviceBounds(&deviceBounds2),
634 testStep->assertMessage());
reed868074b2014-06-03 10:53:59 -0700635 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2, testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000636 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
637 canvas2->getTotalMatrix(), testStep->assertMessage());
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000638 REPORTER_ASSERT_MESSAGE(reporter, equal_clips(*canvas1, *canvas2), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000639
640 // The following test code is commented out because the test fails when
rmistry@google.comd6176b02012-08-23 18:14:13 +0000641 // the canvas is an SkPictureRecord or SkDeferredCanvas
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000642 // Issue: http://code.google.com/p/skia/issues/detail?id=498
643 // Also, creating a LayerIter on an SkProxyCanvas crashes
644 // Issue: http://code.google.com/p/skia/issues/detail?id=499
645 /*
646 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
647 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
648 while (!layerIter1.done() && !layerIter2.done()) {
649 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
650 layerIter2.matrix(), testStep->assertMessage());
651 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
652 layerIter2.clip(), testStep->assertMessage());
653 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
654 layerIter2.paint(), testStep->assertMessage());
655 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
656 layerIter2.x(), testStep->assertMessage());
657 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
658 layerIter2.y(), testStep->assertMessage());
659 layerIter1.next();
660 layerIter2.next();
661 }
662 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
663 testStep->assertMessage());
664 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
665 testStep->assertMessage());
666 */
667}
668
669// The following class groups static functions that need to access
670// the privates members of SkPictureRecord
671class SkPictureTester {
672private:
reed@google.come2589ae2012-07-10 19:38:01 +0000673 static int EQ(const SkFlatData* a, const SkFlatData* b) {
674 return *a == *b;
675 }
676
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000677 static void AssertFlattenedObjectsEqual(
678 SkPictureRecord* referenceRecord,
679 SkPictureRecord* testRecord,
680 skiatest::Reporter* reporter,
681 CanvasTestStep* testStep) {
682
683 REPORTER_ASSERT_MESSAGE(reporter,
djsollen@google.comc9ab9872012-08-29 18:52:07 +0000684 referenceRecord->fBitmapHeap->count() ==
685 testRecord->fBitmapHeap->count(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000686 REPORTER_ASSERT_MESSAGE(reporter,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000687 referenceRecord->fPaints.count() ==
688 testRecord->fPaints.count(), testStep->assertMessage());
689 for (int i = 0; i < referenceRecord->fPaints.count(); ++i) {
690 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.come2589ae2012-07-10 19:38:01 +0000691 EQ(referenceRecord->fPaints[i], testRecord->fPaints[i]),
692 testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000693 }
694 REPORTER_ASSERT_MESSAGE(reporter,
commit-bot@chromium.org8f831f22014-04-23 22:35:42 +0000695 !referenceRecord->fPicture->fPathHeap ==
696 !testRecord->fPicture->fPathHeap,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000697 testStep->assertMessage());
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000698 // The following tests are commented out because they currently
699 // fail. Issue: http://code.google.com/p/skia/issues/detail?id=507
700 /*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000701 if (referenceRecord->fPathHeap) {
702 REPORTER_ASSERT_MESSAGE(reporter,
703 referenceRecord->fPathHeap->count() ==
704 testRecord->fPathHeap->count(),
705 testStep->assertMessage());
706 for (int i = 0; i < referenceRecord->fPathHeap->count(); ++i) {
707 REPORTER_ASSERT_MESSAGE(reporter,
708 (*referenceRecord->fPathHeap)[i] ==
709 (*testRecord->fPathHeap)[i], testStep->assertMessage());
710 }
711 }
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000712 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000713
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000714 }
715
716public:
717
rmistry@google.comd6176b02012-08-23 18:14:13 +0000718 static void TestPictureFlattenedObjectReuse(skiatest::Reporter* reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000719 CanvasTestStep* testStep,
720 uint32_t recordFlags) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000721 // Verify that when a test step is executed twice, no extra resources
722 // are flattened during the second execution
723 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000724 SkPictureRecorder referenceRecorder;
skia.committer@gmail.com60bd7512014-04-18 03:03:54 +0000725 SkCanvas* referenceCanvas = referenceRecorder.beginRecording(kWidth, kHeight,
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000726 NULL, recordFlags);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000727 testStep->draw(referenceCanvas, reporter);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000728
729 SkPictureRecorder testRecorder;
skia.committer@gmail.com60bd7512014-04-18 03:03:54 +0000730 SkCanvas* testCanvas = testRecorder.beginRecording(kWidth, kHeight,
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000731 NULL, recordFlags);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000732 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000733 testStep->setAssertMessageFormat(kPictureSecondDrawAssertMessageFormat);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000734 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000735
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000736 SkPictureRecord* referenceRecord = static_cast<SkPictureRecord*>(referenceCanvas);
737 SkPictureRecord* testRecord = static_cast<SkPictureRecord*>(testCanvas);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000738 testStep->setAssertMessageFormat(kPictureResourceReuseMessageFormat);
739 AssertFlattenedObjectsEqual(referenceRecord, testRecord,
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000740 reporter, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000741 }
742};
743
edisonn@google.com77909122012-10-18 15:58:23 +0000744static void TestPdfDevice(skiatest::Reporter* reporter,
745 CanvasTestStep* testStep) {
746 SkISize pageSize = SkISize::Make(kWidth, kHeight);
747 SkPDFDevice device(pageSize, pageSize, SkMatrix::I());
748 SkCanvas canvas(&device);
749 testStep->setAssertMessageFormat(kPdfAssertMessageFormat);
750 testStep->draw(&canvas, reporter);
751 SkPDFDocument doc;
752 doc.appendPage(&device);
753 SkDynamicMemoryWStream stream;
754 doc.emitPDF(&stream);
755}
756
junov@chromium.org88e29142012-08-07 16:48:22 +0000757// The following class groups static functions that need to access
758// the privates members of SkDeferredCanvas
759class SkDeferredCanvasTester {
760public:
761 static void TestDeferredCanvasStateConsistency(
762 skiatest::Reporter* reporter,
763 CanvasTestStep* testStep,
junov@chromium.orgfb103892012-09-20 19:35:43 +0000764 const SkCanvas& referenceCanvas, bool silent) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000765
reed@google.com28183b42014-02-04 15:34:10 +0000766 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
767 SkAutoTUnref<SkDeferredCanvas> deferredCanvas(SkDeferredCanvas::Create(surface.get()));
768
junov@chromium.org88e29142012-08-07 16:48:22 +0000769 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
junov@chromium.org66070a52013-05-28 17:39:08 +0000770 testStep->draw(deferredCanvas, reporter);
junov@chromium.org88e29142012-08-07 16:48:22 +0000771 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
junov@chromium.org66070a52013-05-28 17:39:08 +0000772 AssertCanvasStatesEqual(reporter, deferredCanvas, &referenceCanvas,
junov@chromium.org88e29142012-08-07 16:48:22 +0000773 testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000774
junov@chromium.orgfb103892012-09-20 19:35:43 +0000775 if (silent) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000776 deferredCanvas->silentFlush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000777 } else {
junov@chromium.org66070a52013-05-28 17:39:08 +0000778 deferredCanvas->flush();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000779 }
780
skia.committer@gmail.com4c5ea442012-09-21 02:01:01 +0000781 testStep->setAssertMessageFormat(
junov@chromium.orgfb103892012-09-20 19:35:43 +0000782 silent ? kDeferredPostSilentFlushPlaybackAssertMessageFormat :
junov@chromium.org88e29142012-08-07 16:48:22 +0000783 kDeferredPostFlushPlaybackAssertMessageFormat);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000784 AssertCanvasStatesEqual(reporter,
junov@chromium.org66070a52013-05-28 17:39:08 +0000785 deferredCanvas->immediateCanvas(),
junov@chromium.org88e29142012-08-07 16:48:22 +0000786 &referenceCanvas, testStep);
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000787
junov@chromium.org88e29142012-08-07 16:48:22 +0000788 // Verified that deferred canvas state is not affected by flushing
789 // pending draw operations
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000790
junov@chromium.org88e29142012-08-07 16:48:22 +0000791 // The following test code is commented out because it currently fails.
792 // Issue: http://code.google.com/p/skia/issues/detail?id=496
793 /*
794 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
795 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
796 testStep);
797 */
798 }
799};
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000800
caryclark@google.com42639cd2012-06-06 12:03:39 +0000801// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000802static void TestProxyCanvasStateConsistency(
803 skiatest::Reporter* reporter,
804 CanvasTestStep* testStep,
805 const SkCanvas& referenceCanvas) {
806
807 SkBitmap indirectStore;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000808 createBitmap(&indirectStore, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000809 SkBitmapDevice indirectDevice(indirectStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000810 SkCanvas indirectCanvas(&indirectDevice);
811 SkProxyCanvas proxyCanvas(&indirectCanvas);
812 testStep->setAssertMessageFormat(kProxyDrawAssertMessageFormat);
813 testStep->draw(&proxyCanvas, reporter);
814 // Verify that the SkProxyCanvas reports consitent state
815 testStep->setAssertMessageFormat(kProxyStateAssertMessageFormat);
816 AssertCanvasStatesEqual(reporter, &proxyCanvas, &referenceCanvas,
817 testStep);
818 // Verify that the indirect canvas reports consitent state
819 testStep->setAssertMessageFormat(kProxyIndirectStateAssertMessageFormat);
820 AssertCanvasStatesEqual(reporter, &indirectCanvas, &referenceCanvas,
821 testStep);
822}
823
caryclark@google.com42639cd2012-06-06 12:03:39 +0000824// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000825static void TestNWayCanvasStateConsistency(
826 skiatest::Reporter* reporter,
827 CanvasTestStep* testStep,
828 const SkCanvas& referenceCanvas) {
829
830 SkBitmap indirectStore1;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000831 createBitmap(&indirectStore1, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000832 SkBitmapDevice indirectDevice1(indirectStore1);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000833 SkCanvas indirectCanvas1(&indirectDevice1);
834
835 SkBitmap indirectStore2;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000836 createBitmap(&indirectStore2, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000837 SkBitmapDevice indirectDevice2(indirectStore2);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000838 SkCanvas indirectCanvas2(&indirectDevice2);
839
djsollen@google.comf0a062b2012-05-01 16:50:25 +0000840 SkISize canvasSize = referenceCanvas.getDeviceSize();
841 SkNWayCanvas nWayCanvas(canvasSize.width(), canvasSize.height());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000842 nWayCanvas.addCanvas(&indirectCanvas1);
843 nWayCanvas.addCanvas(&indirectCanvas2);
844
845 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
846 testStep->draw(&nWayCanvas, reporter);
847 // Verify that the SkProxyCanvas reports consitent state
848 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
849 AssertCanvasStatesEqual(reporter, &nWayCanvas, &referenceCanvas,
850 testStep);
851 // Verify that the indirect canvases report consitent state
852 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
853 AssertCanvasStatesEqual(reporter, &indirectCanvas1, &referenceCanvas,
854 testStep);
855 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
856 AssertCanvasStatesEqual(reporter, &indirectCanvas2, &referenceCanvas,
857 testStep);
858}
859
860/*
861 * This sub-test verifies that the test step passes when executed
862 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
863 * that the all canvas derivatives report the same state as an SkCanvas
864 * after having executed the test step.
865 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000866static void TestOverrideStateConsistency(skiatest::Reporter* reporter,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000867 CanvasTestStep* testStep) {
868 SkBitmap referenceStore;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000869 createBitmap(&referenceStore, 0xFFFFFFFF);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000870 SkBitmapDevice referenceDevice(referenceStore);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000871 SkCanvas referenceCanvas(&referenceDevice);
872 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
873 testStep->draw(&referenceCanvas, reporter);
874
junov@chromium.orgfb103892012-09-20 19:35:43 +0000875 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, false);
876
877 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, true);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000878
caryclark@google.com42639cd2012-06-06 12:03:39 +0000879 // The following test code is disabled because SkProxyCanvas is
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000880 // missing a lot of virtual overrides on get* methods, which are used
881 // to verify canvas state.
882 // Issue: http://code.google.com/p/skia/issues/detail?id=500
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000883
caryclark@google.com42639cd2012-06-06 12:03:39 +0000884 if (false) { // avoid bit rot, suppress warning
885 TestProxyCanvasStateConsistency(reporter, testStep, referenceCanvas);
886 }
887
888 // The following test code is disabled because SkNWayCanvas does not
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000889 // report correct clipping and device bounds information
890 // Issue: http://code.google.com/p/skia/issues/detail?id=501
caryclark@google.com42639cd2012-06-06 12:03:39 +0000891
892 if (false) { // avoid bit rot, suppress warning
893 TestNWayCanvasStateConsistency(reporter, testStep, referenceCanvas);
894 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000895
caryclark@google.com42639cd2012-06-06 12:03:39 +0000896 if (false) { // avoid bit rot, suppress warning
897 test_clipVisitor(reporter, &referenceCanvas);
898 }
reed@google.com7c202932011-12-14 18:48:05 +0000899}
reed@google.com37f3ae02011-11-28 16:06:04 +0000900
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000901static void test_newraster(skiatest::Reporter* reporter) {
902 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
903 SkCanvas* canvas = SkCanvas::NewRaster(info);
904 REPORTER_ASSERT(reporter, canvas);
905
906 SkImageInfo info2;
907 size_t rowBytes;
908 const SkPMColor* addr = (const SkPMColor*)canvas->peekPixels(&info2, &rowBytes);
909 REPORTER_ASSERT(reporter, addr);
910 REPORTER_ASSERT(reporter, info == info2);
911 for (int y = 0; y < info.height(); ++y) {
912 for (int x = 0; x < info.width(); ++x) {
913 REPORTER_ASSERT(reporter, 0 == addr[x]);
914 }
915 addr = (const SkPMColor*)((const char*)addr + rowBytes);
916 }
917 SkDELETE(canvas);
918
919 // now try a deliberately bad info
920 info.fWidth = -1;
921 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRaster(info));
922
923 // too big
924 info.fWidth = 1 << 30;
925 info.fHeight = 1 << 30;
926 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRaster(info));
skia.committer@gmail.com0e530752014-02-28 03:02:05 +0000927
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000928 // not a valid pixel type
929 info.fWidth = info.fHeight = 10;
930 info.fColorType = kUnknown_SkColorType;
931 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRaster(info));
932
933 // We should succeed with a zero-sized valid info
934 info = SkImageInfo::MakeN32Premul(0, 0);
935 canvas = SkCanvas::NewRaster(info);
936 REPORTER_ASSERT(reporter, canvas);
937 SkDELETE(canvas);
938}
939
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000940DEF_TEST(Canvas, reporter) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000941 // Init global here because bitmap pixels cannot be alocated during
942 // static initialization
943 kTestBitmap = testBitmap();
reed@google.com37f3ae02011-11-28 16:06:04 +0000944
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000945 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
946 TestOverrideStateConsistency(reporter, testStepArray()[testStep]);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000947 SkPictureTester::TestPictureFlattenedObjectReuse(reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000948 testStepArray()[testStep], 0);
edisonn@google.com77909122012-10-18 15:58:23 +0000949 if (testStepArray()[testStep]->enablePdfTesting()) {
950 TestPdfDevice(reporter, testStepArray()[testStep]);
951 }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000952 }
junov@chromium.orgcd62ecf2012-08-02 17:43:25 +0000953
954 // Explicitly call reset(), so we don't leak the pixels (since kTestBitmap is a global)
955 kTestBitmap.reset();
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +0000956
957 test_newraster(reporter);
reed@google.com37f3ae02011-11-28 16:06:04 +0000958}