blob: 657045081a7576e43146ba387e5447d9ac83fc55 [file] [log] [blame]
reed@google.com37f3ae02011-11-28 16:06:04 +00001
2/*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +00003 * Copyright 2012 Google Inc.
reed@google.com37f3ae02011-11-28 16:06:04 +00004 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +00008
9/* Description:
10 * This test defines a series of elementatry test steps that perform
11 * a single or a small group of canvas API calls. Each test step is
12 * used in several test cases that verify that different types of SkCanvas
13 * flavors and derivatives pass it and yield consistent behavior. The
14 * test cases analyse results that are queryable through the API. They do
15 * not look at rendering results.
16 *
17 * Adding test stepss:
18 * The general pattern for creating a new test step is to write a test
19 * function of the form:
20 *
rmistry@google.comd6176b02012-08-23 18:14:13 +000021 * static void MyTestStepFunction(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000022 * skiatest::Reporter* reporter,
23 * CanvasTestStep* testStep)
24 * {
25 * canvas->someCanvasAPImethod();
26 * (...)
27 * REPORTER_ASSERT_MESSAGE(reporter, (...), \
28 * testStep->assertMessage());
29 * }
30 *
31 * The definition of the test step function should be followed by an
32 * invocation of the TEST_STEP macro, which generates a class and
33 * instance for the test step:
34 *
35 * TEST_STEP(MyTestStep, MyTestStepFunction)
36 *
37 * There are also short hand macros for defining simple test steps
38 * in a single line of code. A simple test step is a one that is made
39 * of a single canvas API call.
40 *
41 * SIMPLE_TEST_STEP(MytestStep, someCanvasAPIMethod());
42 *
43 * There is another macro called SIMPLE_TEST_STEP_WITH_ASSERT that
44 * works the same way as SIMPLE_TEST_STEP, and additionally verifies
45 * that the invoked method returns a non-zero value.
46 */
reed@google.com37f3ae02011-11-28 16:06:04 +000047#include "SkBitmap.h"
48#include "SkCanvas.h"
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000049#include "SkDeferredCanvas.h"
50#include "SkDevice.h"
51#include "SkMatrix.h"
52#include "SkNWayCanvas.h"
53#include "SkPaint.h"
54#include "SkPath.h"
55#include "SkPicture.h"
56#include "SkPictureRecord.h"
57#include "SkProxyCanvas.h"
58#include "SkRect.h"
59#include "SkRegion.h"
60#include "SkShader.h"
61#include "SkStream.h"
62#include "SkTDArray.h"
63#include "Test.h"
reed@google.com37f3ae02011-11-28 16:06:04 +000064
reed@google.com90c07ea2012-04-13 13:50:27 +000065class Canvas2CanvasClipVisitor : public SkCanvas::ClipVisitor {
66public:
67 Canvas2CanvasClipVisitor(SkCanvas* target) : fTarget(target) {}
68
69 virtual void clipRect(const SkRect& r, SkRegion::Op op, bool aa) {
70 fTarget->clipRect(r, op, aa);
71 }
72 virtual void clipPath(const SkPath& p, SkRegion::Op op, bool aa) {
73 fTarget->clipPath(p, op, aa);
74 }
75
76private:
77 SkCanvas* fTarget;
78};
79
80static void test_clipVisitor(skiatest::Reporter* reporter, SkCanvas* canvas) {
81 SkISize size = canvas->getDeviceSize();
rmistry@google.comd6176b02012-08-23 18:14:13 +000082
reed@google.com90c07ea2012-04-13 13:50:27 +000083 SkBitmap bm;
84 bm.setConfig(SkBitmap::kARGB_8888_Config, size.width(), size.height());
85 SkCanvas c(bm);
86
87 Canvas2CanvasClipVisitor visitor(&c);
88 canvas->replayClips(&visitor);
89
90 REPORTER_ASSERT(reporter, c.getTotalClip() == canvas->getTotalClip());
91}
92
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000093static const int kWidth = 2;
94static const int kHeight = 2;
95// Maximum stream length for picture serialization
rmistry@google.comd6176b02012-08-23 18:14:13 +000096static const size_t kMaxPictureBufferSize = 1024;
reed@google.com7c202932011-12-14 18:48:05 +000097
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +000098// Format strings that describe the test context. The %s token is where
99// the name of the test step is inserted. The context is required for
100// disambiguating the error in the case of failures that are reported in
101// functions that are called multiple times in different contexts (test
102// cases and test steps).
103static const char* const kDefaultAssertMessageFormat = "%s";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000104static const char* const kCanvasDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000105 "Drawing test step %s with SkCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000106static const char* const kPictureDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000107 "Drawing test step %s with SkPicture";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000108static const char* const kPictureSecondDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000109 "Duplicate draw of test step %s with SkPicture";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000110static const char* const kPictureReDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000111 "Playing back test step %s from an SkPicture to another SkPicture";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000112static const char* const kDeferredDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000113 "Drawing test step %s with SkDeferredCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000114static const char* const kProxyDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000115 "Drawing test step %s with SkProxyCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000116static const char* const kNWayDrawAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000117 "Drawing test step %s with SkNWayCanvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000118static const char* const kRoundTripAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000119 "test step %s, SkPicture consistency after round trip";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000120static const char* const kPictureRecoringAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000121 "test step %s, SkPicture state consistency after recording";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000122static const char* const kPicturePlaybackAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000123 "test step %s, SkPicture state consistency in playback canvas";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000124static const char* const kDeferredPreFlushAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000125 "test step %s, SkDeferredCanvas state consistency before flush";
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000126static const char* const kDeferredPostFlushPlaybackAssertMessageFormat =
127 "test step %s, SkDeferredCanvas playback canvas state consistency after flush";
junov@chromium.orgfb103892012-09-20 19:35:43 +0000128static const char* const kDeferredPostSilentFlushPlaybackAssertMessageFormat =
129 "test step %s, SkDeferredCanvas playback canvas state consistency after silent flush";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000130static const char* const kDeferredPostFlushAssertMessageFormat =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000131 "test step %s, SkDeferredCanvas state consistency after flush";
132static const char* const kPictureResourceReuseMessageFormat =
133 "test step %s, SkPicture duplicate flattened object test";
134static const char* const kProxyStateAssertMessageFormat =
135 "test step %s, SkProxyCanvas state consistency";
136static const char* const kProxyIndirectStateAssertMessageFormat =
137 "test step %s, SkProxyCanvas indirect canvas state consistency";
138static const char* const kNWayStateAssertMessageFormat =
139 "test step %s, SkNWayCanvas state consistency";
140static const char* const kNWayIndirect1StateAssertMessageFormat =
141 "test step %s, SkNWayCanvas indirect canvas 1 state consistency";
142static const char* const kNWayIndirect2StateAssertMessageFormat =
143 "test step %s, SkNWayCanvas indirect canvas 2 state consistency";
144
145static void createBitmap(SkBitmap* bm, SkBitmap::Config config, SkColor color) {
146 bm->setConfig(config, kWidth, kHeight);
147 bm->allocPixels();
148 bm->eraseColor(color);
149}
150
151class CanvasTestStep;
152static SkTDArray<CanvasTestStep*>& testStepArray() {
153 static SkTDArray<CanvasTestStep*> theTests;
154 return theTests;
155}
156
157class CanvasTestStep {
158public:
159 CanvasTestStep() {
160 *testStepArray().append() = this;
161 fAssertMessageFormat = kDefaultAssertMessageFormat;
162 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000163 virtual ~CanvasTestStep() { }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000164
165 virtual void draw(SkCanvas*, skiatest::Reporter*) = 0;
166 virtual const char* name() const = 0;
167
168 const char* assertMessage() {
169 fAssertMessage.printf(fAssertMessageFormat, name());
170 return fAssertMessage.c_str();
171 }
172
173 void setAssertMessageFormat(const char* format) {
174 fAssertMessageFormat = format;
175 }
176
177private:
178 SkString fAssertMessage;
179 const char* fAssertMessageFormat;
180};
181
182///////////////////////////////////////////////////////////////////////////////
183// Constants used by test steps
184
rmistry@google.comd6176b02012-08-23 18:14:13 +0000185const SkRect kTestRect =
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000186 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
187 SkIntToScalar(2), SkIntToScalar(1));
188static SkMatrix testMatrix() {
189 SkMatrix matrix;
190 matrix.reset();
191 matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
192 return matrix;
193}
194const SkMatrix kTestMatrix = testMatrix();
195static SkPath testPath() {
196 SkPath path;
197 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
198 SkIntToScalar(2), SkIntToScalar(1)));
199 return path;
200}
201const SkPath kTestPath = testPath();
202static SkRegion testRegion() {
203 SkRegion region;
204 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
205 region.setRect(rect);
206 return region;
207}
208const SkIRect kTestIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
209const SkRegion kTestRegion = testRegion();
210const SkColor kTestColor = 0x01020304;
211const SkPaint kTestPaint;
212const SkPoint kTestPoints[3] = {
213 {SkIntToScalar(0), SkIntToScalar(0)},
214 {SkIntToScalar(2), SkIntToScalar(1)},
215 {SkIntToScalar(0), SkIntToScalar(2)}
216};
217const size_t kTestPointCount = 3;
218static SkBitmap testBitmap() {
219 SkBitmap bitmap;
220 createBitmap(&bitmap, SkBitmap::kARGB_8888_Config, 0x05060708);
221 return bitmap;
222}
223SkBitmap kTestBitmap; // cannot be created during static init
224SkString kTestText("Hello World");
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000225SkPoint kTestPoints2[] = {
226 { SkIntToScalar(0), SkIntToScalar(1) },
227 { SkIntToScalar(1), SkIntToScalar(1) },
228 { SkIntToScalar(2), SkIntToScalar(1) },
229 { SkIntToScalar(3), SkIntToScalar(1) },
230 { SkIntToScalar(4), SkIntToScalar(1) },
231 { SkIntToScalar(5), SkIntToScalar(1) },
232 { SkIntToScalar(6), SkIntToScalar(1) },
233 { SkIntToScalar(7), SkIntToScalar(1) },
234 { SkIntToScalar(8), SkIntToScalar(1) },
235 { SkIntToScalar(9), SkIntToScalar(1) },
236 { SkIntToScalar(10), SkIntToScalar(1) },
237};
rmistry@google.comd6176b02012-08-23 18:14:13 +0000238
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000239
240///////////////////////////////////////////////////////////////////////////////
241// Macros for defining test steps
242
243#define TEST_STEP(NAME, FUNCTION) \
244class NAME##_TestStep : public CanvasTestStep{ \
245public: \
246 virtual void draw(SkCanvas* canvas, skiatest::Reporter* reporter) { \
247 FUNCTION (canvas, reporter, this); \
248 } \
249 virtual const char* name() const {return #NAME ;} \
250}; \
251static NAME##_TestStep NAME##_TestStepInstance;
252
253#define SIMPLE_TEST_STEP(NAME, CALL) \
254static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter*, \
255 CanvasTestStep*) { \
256 canvas-> CALL ; \
257} \
258TEST_STEP(NAME, NAME##TestStep )
259
260#define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
261static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter* reporter, \
262 CanvasTestStep* testStep) { \
263 REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \
264 testStep->assertMessage()); \
265} \
266TEST_STEP(NAME, NAME##TestStep )
267
268
269///////////////////////////////////////////////////////////////////////////////
rmistry@google.comd6176b02012-08-23 18:14:13 +0000270// Basic test steps for most virtual methods in SkCanvas that draw or affect
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000271// the state of the canvas.
272
junov@chromium.orga907ac32012-02-24 21:54:07 +0000273SIMPLE_TEST_STEP_WITH_ASSERT(Translate,
274 translate(SkIntToScalar(1), SkIntToScalar(2)));
275SIMPLE_TEST_STEP_WITH_ASSERT(Scale,
276 scale(SkIntToScalar(1), SkIntToScalar(2)));
277SIMPLE_TEST_STEP_WITH_ASSERT(Rotate, rotate(SkIntToScalar(1)));
278SIMPLE_TEST_STEP_WITH_ASSERT(Skew,
279 skew(SkIntToScalar(1), SkIntToScalar(2)));
280SIMPLE_TEST_STEP_WITH_ASSERT(Concat, concat(kTestMatrix));
281SIMPLE_TEST_STEP(SetMatrix, setMatrix(kTestMatrix));
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000282SIMPLE_TEST_STEP(ClipRect, clipRect(kTestRect));
283SIMPLE_TEST_STEP(ClipPath, clipPath(kTestPath));
284SIMPLE_TEST_STEP(ClipRegion,
junov@chromium.orga907ac32012-02-24 21:54:07 +0000285 clipRegion(kTestRegion, SkRegion::kReplace_Op));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000286SIMPLE_TEST_STEP(Clear, clear(kTestColor));
287SIMPLE_TEST_STEP(DrawPaint, drawPaint(kTestPaint));
288SIMPLE_TEST_STEP(DrawPointsPoints, drawPoints(SkCanvas::kPoints_PointMode,
289 kTestPointCount, kTestPoints, kTestPaint));
290SIMPLE_TEST_STEP(DrawPointsLiness, drawPoints(SkCanvas::kLines_PointMode,
291 kTestPointCount, kTestPoints, kTestPaint));
292SIMPLE_TEST_STEP(DrawPointsPolygon, drawPoints(SkCanvas::kPolygon_PointMode,
293 kTestPointCount, kTestPoints, kTestPaint));
294SIMPLE_TEST_STEP(DrawRect, drawRect(kTestRect, kTestPaint));
295SIMPLE_TEST_STEP(DrawPath, drawPath(kTestPath, kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000296SIMPLE_TEST_STEP(DrawBitmap, drawBitmap(kTestBitmap, 0, 0));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000297SIMPLE_TEST_STEP(DrawBitmapPaint, drawBitmap(kTestBitmap, 0, 0, &kTestPaint));
298SIMPLE_TEST_STEP(DrawBitmapRect, drawBitmapRect(kTestBitmap, NULL, kTestRect,
299 NULL));
300SIMPLE_TEST_STEP(DrawBitmapRectSrcRect, drawBitmapRect(kTestBitmap,
301 &kTestIRect, kTestRect, NULL));
302SIMPLE_TEST_STEP(DrawBitmapRectPaint, drawBitmapRect(kTestBitmap, NULL,
303 kTestRect, &kTestPaint));
304SIMPLE_TEST_STEP(DrawBitmapMatrix, drawBitmapMatrix(kTestBitmap, kTestMatrix,
305 NULL));
306SIMPLE_TEST_STEP(DrawBitmapMatrixPaint, drawBitmapMatrix(kTestBitmap,
307 kTestMatrix, &kTestPaint));
308SIMPLE_TEST_STEP(DrawBitmapNine, drawBitmapNine(kTestBitmap, kTestIRect,
309 kTestRect, NULL));
310SIMPLE_TEST_STEP(DrawBitmapNinePaint, drawBitmapNine(kTestBitmap, kTestIRect,
311 kTestRect, &kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000312SIMPLE_TEST_STEP(DrawSprite, drawSprite(kTestBitmap, 0, 0, NULL));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000313SIMPLE_TEST_STEP(DrawSpritePaint, drawSprite(kTestBitmap, 0, 0, &kTestPaint));
314SIMPLE_TEST_STEP(DrawText, drawText(kTestText.c_str(), kTestText.size(),
315 0, 1, kTestPaint));
316SIMPLE_TEST_STEP(DrawPosText, drawPosText(kTestText.c_str(),
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000317 kTestText.size(), kTestPoints2, kTestPaint));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000318SIMPLE_TEST_STEP(DrawTextOnPath, drawTextOnPath(kTestText.c_str(),
319 kTestText.size(), kTestPath, NULL, kTestPaint));
320SIMPLE_TEST_STEP(DrawTextOnPathMatrix, drawTextOnPath(kTestText.c_str(),
321 kTestText.size(), kTestPath, &kTestMatrix, kTestPaint));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000322SIMPLE_TEST_STEP(DrawData, drawData(kTestText.c_str(), kTestText.size()));
323
324///////////////////////////////////////////////////////////////////////////////
325// Complex test steps
326
rmistry@google.comd6176b02012-08-23 18:14:13 +0000327// Save/restore calls cannot be in isolated simple test steps because the test
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000328// cases that use SkPicture require that save and restore calls be balanced.
rmistry@google.comd6176b02012-08-23 18:14:13 +0000329static void SaveMatrixStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000330 skiatest::Reporter* reporter,
331 CanvasTestStep* testStep) {
332 int saveCount = canvas->getSaveCount();
333 canvas->save(SkCanvas::kMatrix_SaveFlag);
334 canvas->clipRegion(kTestRegion);
335 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
336 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000337 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000338 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000339 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000340 testStep->assertMessage());
341 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() == kTestRegion,
342 testStep->assertMessage());
343}
344TEST_STEP(SaveMatrix, SaveMatrixStep);
345
rmistry@google.comd6176b02012-08-23 18:14:13 +0000346static void SaveClipStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000347 skiatest::Reporter* reporter,
348 CanvasTestStep* testStep) {
349 int saveCount = canvas->getSaveCount();
350 canvas->save(SkCanvas::kClip_SaveFlag);
351 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
352 canvas->clipRegion(kTestRegion);
353 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000354 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000355 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000356 REPORTER_ASSERT_MESSAGE(reporter, !canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000357 testStep->assertMessage());
358 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion,
359 testStep->assertMessage());
360}
361TEST_STEP(SaveClip, SaveClipStep);
362
rmistry@google.comd6176b02012-08-23 18:14:13 +0000363static void SaveMatrixClipStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000364 skiatest::Reporter* reporter,
365 CanvasTestStep* testStep) {
366 int saveCount = canvas->getSaveCount();
367 canvas->save(SkCanvas::kMatrixClip_SaveFlag);
368 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
369 canvas->clipRegion(kTestRegion);
370 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000371 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000372 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000373 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000374 testStep->assertMessage());
375 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion,
376 testStep->assertMessage());
377}
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,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000414 skiatest::Reporter* reporter,
415 CanvasTestStep* testStep) {
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,
epoger@google.com94fa43c2012-04-11 17:51:01 +0000428 skiatest::Reporter* reporter,
429 CanvasTestStep* testStep) {
430 SkPaint paint;
431 paint.setStrokeWidth(SkIntToScalar(1));
432 paint.setStyle(SkPaint::kStroke_Style);
433
434 SkPath path;
435 SkPoint pt1 = { 0, 0 };
436 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
437 SkPoint pt3 = { SkIntToScalar(1), 0 };
438 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
439 path.moveTo(pt1);
440 path.lineTo(pt2);
441 path.lineTo(pt3);
442 path.lineTo(pt4);
443
444 canvas->drawPath(path, paint);
445}
446TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
447
rmistry@google.comd6176b02012-08-23 18:14:13 +0000448static void DrawVerticesShaderTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000449 skiatest::Reporter* reporter,
450 CanvasTestStep* testStep) {
451 SkPoint pts[4];
452 pts[0].set(0, 0);
453 pts[1].set(SkIntToScalar(kWidth), 0);
454 pts[2].set(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
455 pts[3].set(0, SkIntToScalar(kHeight));
456 SkPaint paint;
457 SkShader* shader = SkShader::CreateBitmapShader(kTestBitmap,
458 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
459 paint.setShader(shader)->unref();
460 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
461 NULL, NULL, NULL, 0, paint);
462}
463TEST_STEP(DrawVerticesShader, DrawVerticesShaderTestStep);
464
rmistry@google.comd6176b02012-08-23 18:14:13 +0000465static void DrawPictureTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000466 skiatest::Reporter* reporter,
467 CanvasTestStep* testStep) {
468 SkPicture* testPicture = SkNEW_ARGS(SkPicture, ());
469 SkAutoUnref aup(testPicture);
470 SkCanvas* testCanvas = testPicture->beginRecording(kWidth, kHeight);
471 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
472 testCanvas->clipRect(kTestRect);
473 testCanvas->drawRect(kTestRect, kTestPaint);
474 canvas->drawPicture(*testPicture);
475}
476TEST_STEP(DrawPicture, DrawPictureTestStep);
477
rmistry@google.comd6176b02012-08-23 18:14:13 +0000478static void SaveRestoreTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000479 skiatest::Reporter* reporter,
480 CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000481 int baseSaveCount = canvas->getSaveCount();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000482 int n = canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000483 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount == n, testStep->assertMessage());
484 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000485 testStep->assertMessage());
486 canvas->save();
487 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000488 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000489 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000490 canvas->restoreToCount(baseSaveCount + 1);
491 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000492 testStep->assertMessage());
493
494 // should this pin to 1, or be a no-op, or crash?
495 canvas->restoreToCount(0);
496 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
497 testStep->assertMessage());
498}
499TEST_STEP(SaveRestore, SaveRestoreTestStep);
500
rmistry@google.comd6176b02012-08-23 18:14:13 +0000501static void DrawLayerTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000502 skiatest::Reporter* reporter,
503 CanvasTestStep* testStep) {
504 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
505 testStep->assertMessage());
506 canvas->save();
507 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
508 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000509 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000510
reed@google.com7c202932011-12-14 18:48:05 +0000511 const SkRect* bounds = NULL; // null means include entire bounds
512 const SkPaint* paint = NULL;
513
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000514 canvas->saveLayer(bounds, paint);
515 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
516 testStep->assertMessage());
517 canvas->restore();
518 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
519 testStep->assertMessage());
reed@google.com7c202932011-12-14 18:48:05 +0000520
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000521 canvas->saveLayer(bounds, paint);
522 canvas->saveLayer(bounds, paint);
523 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
524 testStep->assertMessage());
525 canvas->restore();
526 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
527 testStep->assertMessage());
528 canvas->restore();
reed@google.com7c202932011-12-14 18:48:05 +0000529 // now layer count should be 0
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000530 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
531 testStep->assertMessage());
532}
533TEST_STEP(DrawLayer, DrawLayerTestStep);
reed@google.com3b3e8952012-08-16 20:53:31 +0000534
535static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas,
536 skiatest::Reporter* reporter,
537 CanvasTestStep* testStep) {
538 // This test step challenges the TestDeferredCanvasStateConsistency
539 // test cases because the opaque paint can trigger an optimization
540 // that discards previously recorded commands. The challenge is to maintain
541 // correct clip and matrix stack state.
542 canvas->resetMatrix();
543 canvas->rotate(SkIntToScalar(30));
544 canvas->save();
545 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
546 canvas->save();
547 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
548 SkPaint paint;
549 paint.setColor(0xFFFFFFFF);
550 canvas->drawPaint(paint);
551 canvas->restore();
552 canvas->restore();
553}
554TEST_STEP(NestedSaveRestoreWithSolidPaint, \
555 NestedSaveRestoreWithSolidPaintTestStep);
556
557static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas,
558 skiatest::Reporter* reporter,
559 CanvasTestStep* testStep) {
560 // This test step challenges the TestDeferredCanvasStateConsistency
561 // test case because the canvas flush on a deferred canvas will
562 // reset the recording session. The challenge is to maintain correct
563 // clip and matrix stack state on the playback canvas.
564 canvas->resetMatrix();
565 canvas->rotate(SkIntToScalar(30));
566 canvas->save();
567 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
568 canvas->save();
569 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
570 canvas->drawRect(kTestRect,kTestPaint);
571 canvas->flush();
572 canvas->restore();
573 canvas->restore();
574}
575TEST_STEP(NestedSaveRestoreWithFlush, \
576 NestedSaveRestoreWithFlushTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000577
578static void AssertCanvasStatesEqual(skiatest::Reporter* reporter,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000579 const SkCanvas* canvas1,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000580 const SkCanvas* canvas2,
581 CanvasTestStep* testStep) {
582 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
583 canvas2->getDeviceSize(), testStep->assertMessage());
584 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
585 canvas2->getSaveCount(), testStep->assertMessage());
586 REPORTER_ASSERT_MESSAGE(reporter, canvas1->isDrawingToLayer() ==
587 canvas2->isDrawingToLayer(), testStep->assertMessage());
reed@google.com3b3e8952012-08-16 20:53:31 +0000588
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000589 SkRect bounds1, bounds2;
590 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.com3b3e8952012-08-16 20:53:31 +0000591 canvas1->getClipBounds(&bounds1) == canvas2->getClipBounds(&bounds2),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000592 testStep->assertMessage());
593 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
reed@google.com3b3e8952012-08-16 20:53:31 +0000594 testStep->assertMessage());
595
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000596 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
597 canvas2->getDrawFilter(), testStep->assertMessage());
598 SkIRect deviceBounds1, deviceBounds2;
599 REPORTER_ASSERT_MESSAGE(reporter,
600 canvas1->getClipDeviceBounds(&deviceBounds1) ==
601 canvas2->getClipDeviceBounds(&deviceBounds2),
602 testStep->assertMessage());
603 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2,
604 testStep->assertMessage());
605 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getBounder() ==
606 canvas2->getBounder(), testStep->assertMessage());
607 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
608 canvas2->getTotalMatrix(), testStep->assertMessage());
609 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getClipType() ==
610 canvas2->getClipType(), testStep->assertMessage());
611 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalClip() ==
612 canvas2->getTotalClip(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000613
614 // The following test code is commented out because the test fails when
rmistry@google.comd6176b02012-08-23 18:14:13 +0000615 // the canvas is an SkPictureRecord or SkDeferredCanvas
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000616 // Issue: http://code.google.com/p/skia/issues/detail?id=498
617 // Also, creating a LayerIter on an SkProxyCanvas crashes
618 // Issue: http://code.google.com/p/skia/issues/detail?id=499
619 /*
620 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
621 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
622 while (!layerIter1.done() && !layerIter2.done()) {
623 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
624 layerIter2.matrix(), testStep->assertMessage());
625 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
626 layerIter2.clip(), testStep->assertMessage());
627 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
628 layerIter2.paint(), testStep->assertMessage());
629 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
630 layerIter2.x(), testStep->assertMessage());
631 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
632 layerIter2.y(), testStep->assertMessage());
633 layerIter1.next();
634 layerIter2.next();
635 }
636 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
637 testStep->assertMessage());
638 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
639 testStep->assertMessage());
640 */
641}
642
643// The following class groups static functions that need to access
644// the privates members of SkPictureRecord
645class SkPictureTester {
646private:
reed@google.come2589ae2012-07-10 19:38:01 +0000647 static int EQ(const SkFlatData* a, const SkFlatData* b) {
648 return *a == *b;
649 }
650
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000651 static void AssertFlattenedObjectsEqual(
652 SkPictureRecord* referenceRecord,
653 SkPictureRecord* testRecord,
654 skiatest::Reporter* reporter,
655 CanvasTestStep* testStep) {
656
657 REPORTER_ASSERT_MESSAGE(reporter,
djsollen@google.comc9ab9872012-08-29 18:52:07 +0000658 referenceRecord->fBitmapHeap->count() ==
659 testRecord->fBitmapHeap->count(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000660 REPORTER_ASSERT_MESSAGE(reporter,
661 referenceRecord->fMatrices.count() ==
662 testRecord->fMatrices.count(), testStep->assertMessage());
663 for (int i = 0; i < referenceRecord->fMatrices.count(); ++i) {
664 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.come2589ae2012-07-10 19:38:01 +0000665 EQ(referenceRecord->fMatrices[i], testRecord->fMatrices[i]),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000666 testStep->assertMessage());
667 }
668 REPORTER_ASSERT_MESSAGE(reporter,
669 referenceRecord->fPaints.count() ==
670 testRecord->fPaints.count(), testStep->assertMessage());
671 for (int i = 0; i < referenceRecord->fPaints.count(); ++i) {
672 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.come2589ae2012-07-10 19:38:01 +0000673 EQ(referenceRecord->fPaints[i], testRecord->fPaints[i]),
674 testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000675 }
676 REPORTER_ASSERT_MESSAGE(reporter,
677 referenceRecord->fRegions.count() ==
678 testRecord->fRegions.count(), testStep->assertMessage());
679 for (int i = 0; i < referenceRecord->fRegions.count(); ++i) {
680 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.come2589ae2012-07-10 19:38:01 +0000681 EQ(referenceRecord->fRegions[i], testRecord->fRegions[i]),
682 testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000683 }
684 REPORTER_ASSERT_MESSAGE(reporter,
685 !referenceRecord->fPathHeap ==
686 !testRecord->fPathHeap,
687 testStep->assertMessage());
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000688 // The following tests are commented out because they currently
689 // fail. Issue: http://code.google.com/p/skia/issues/detail?id=507
690 /*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000691 if (referenceRecord->fPathHeap) {
692 REPORTER_ASSERT_MESSAGE(reporter,
693 referenceRecord->fPathHeap->count() ==
694 testRecord->fPathHeap->count(),
695 testStep->assertMessage());
696 for (int i = 0; i < referenceRecord->fPathHeap->count(); ++i) {
697 REPORTER_ASSERT_MESSAGE(reporter,
698 (*referenceRecord->fPathHeap)[i] ==
699 (*testRecord->fPathHeap)[i], testStep->assertMessage());
700 }
701 }
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000702 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000703
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000704 }
705
706public:
707
rmistry@google.comd6176b02012-08-23 18:14:13 +0000708 static void TestPictureFlattenedObjectReuse(skiatest::Reporter* reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000709 CanvasTestStep* testStep,
710 uint32_t recordFlags) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000711 // Verify that when a test step is executed twice, no extra resources
712 // are flattened during the second execution
713 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
714 SkPicture referencePicture;
715 SkCanvas* referenceCanvas = referencePicture.beginRecording(kWidth,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000716 kHeight, recordFlags);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000717 testStep->draw(referenceCanvas, reporter);
718 SkPicture testPicture;
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000719 SkCanvas* testCanvas = testPicture.beginRecording(kWidth,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000720 kHeight, recordFlags);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000721 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000722 testStep->setAssertMessageFormat(kPictureSecondDrawAssertMessageFormat);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000723 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000724
725 SkPictureRecord* referenceRecord = static_cast<SkPictureRecord*>(
726 referenceCanvas);
727 SkPictureRecord* testRecord = static_cast<SkPictureRecord*>(
728 testCanvas);
729 testStep->setAssertMessageFormat(kPictureResourceReuseMessageFormat);
730 AssertFlattenedObjectsEqual(referenceRecord, testRecord,
junov@chromium.org76b9c4b2012-02-22 21:24:41 +0000731 reporter, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000732 }
733};
734
junov@chromium.org88e29142012-08-07 16:48:22 +0000735// The following class groups static functions that need to access
736// the privates members of SkDeferredCanvas
737class SkDeferredCanvasTester {
738public:
739 static void TestDeferredCanvasStateConsistency(
740 skiatest::Reporter* reporter,
741 CanvasTestStep* testStep,
junov@chromium.orgfb103892012-09-20 19:35:43 +0000742 const SkCanvas& referenceCanvas, bool silent) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000743
junov@chromium.org88e29142012-08-07 16:48:22 +0000744 SkBitmap deferredStore;
745 createBitmap(&deferredStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
746 SkDevice deferredDevice(deferredStore);
747 SkDeferredCanvas deferredCanvas(&deferredDevice);
748 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
749 testStep->draw(&deferredCanvas, reporter);
750 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
751 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
752 testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000753
junov@chromium.orgfb103892012-09-20 19:35:43 +0000754 if (silent) {
755 deferredCanvas.silentFlush();
756 } else {
757 deferredCanvas.flush();
758 }
759
skia.committer@gmail.com4c5ea442012-09-21 02:01:01 +0000760 testStep->setAssertMessageFormat(
junov@chromium.orgfb103892012-09-20 19:35:43 +0000761 silent ? kDeferredPostSilentFlushPlaybackAssertMessageFormat :
junov@chromium.org88e29142012-08-07 16:48:22 +0000762 kDeferredPostFlushPlaybackAssertMessageFormat);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000763 AssertCanvasStatesEqual(reporter,
junov@chromium.org88e29142012-08-07 16:48:22 +0000764 deferredCanvas.immediateCanvas(),
765 &referenceCanvas, testStep);
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000766
junov@chromium.org88e29142012-08-07 16:48:22 +0000767 // Verified that deferred canvas state is not affected by flushing
768 // pending draw operations
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000769
junov@chromium.org88e29142012-08-07 16:48:22 +0000770 // The following test code is commented out because it currently fails.
771 // Issue: http://code.google.com/p/skia/issues/detail?id=496
772 /*
773 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
774 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
775 testStep);
776 */
777 }
778};
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000779
caryclark@google.com42639cd2012-06-06 12:03:39 +0000780// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000781static void TestProxyCanvasStateConsistency(
782 skiatest::Reporter* reporter,
783 CanvasTestStep* testStep,
784 const SkCanvas& referenceCanvas) {
785
786 SkBitmap indirectStore;
787 createBitmap(&indirectStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
788 SkDevice indirectDevice(indirectStore);
789 SkCanvas indirectCanvas(&indirectDevice);
790 SkProxyCanvas proxyCanvas(&indirectCanvas);
791 testStep->setAssertMessageFormat(kProxyDrawAssertMessageFormat);
792 testStep->draw(&proxyCanvas, reporter);
793 // Verify that the SkProxyCanvas reports consitent state
794 testStep->setAssertMessageFormat(kProxyStateAssertMessageFormat);
795 AssertCanvasStatesEqual(reporter, &proxyCanvas, &referenceCanvas,
796 testStep);
797 // Verify that the indirect canvas reports consitent state
798 testStep->setAssertMessageFormat(kProxyIndirectStateAssertMessageFormat);
799 AssertCanvasStatesEqual(reporter, &indirectCanvas, &referenceCanvas,
800 testStep);
801}
802
caryclark@google.com42639cd2012-06-06 12:03:39 +0000803// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000804static void TestNWayCanvasStateConsistency(
805 skiatest::Reporter* reporter,
806 CanvasTestStep* testStep,
807 const SkCanvas& referenceCanvas) {
808
809 SkBitmap indirectStore1;
810 createBitmap(&indirectStore1, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
811 SkDevice indirectDevice1(indirectStore1);
812 SkCanvas indirectCanvas1(&indirectDevice1);
813
814 SkBitmap indirectStore2;
815 createBitmap(&indirectStore2, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
816 SkDevice indirectDevice2(indirectStore2);
817 SkCanvas indirectCanvas2(&indirectDevice2);
818
djsollen@google.comf0a062b2012-05-01 16:50:25 +0000819 SkISize canvasSize = referenceCanvas.getDeviceSize();
820 SkNWayCanvas nWayCanvas(canvasSize.width(), canvasSize.height());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000821 nWayCanvas.addCanvas(&indirectCanvas1);
822 nWayCanvas.addCanvas(&indirectCanvas2);
823
824 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
825 testStep->draw(&nWayCanvas, reporter);
826 // Verify that the SkProxyCanvas reports consitent state
827 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
828 AssertCanvasStatesEqual(reporter, &nWayCanvas, &referenceCanvas,
829 testStep);
830 // Verify that the indirect canvases report consitent state
831 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
832 AssertCanvasStatesEqual(reporter, &indirectCanvas1, &referenceCanvas,
833 testStep);
834 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
835 AssertCanvasStatesEqual(reporter, &indirectCanvas2, &referenceCanvas,
836 testStep);
837}
838
839/*
840 * This sub-test verifies that the test step passes when executed
841 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
842 * that the all canvas derivatives report the same state as an SkCanvas
843 * after having executed the test step.
844 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000845static void TestOverrideStateConsistency(skiatest::Reporter* reporter,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000846 CanvasTestStep* testStep) {
847 SkBitmap referenceStore;
848 createBitmap(&referenceStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
849 SkDevice referenceDevice(referenceStore);
850 SkCanvas referenceCanvas(&referenceDevice);
851 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
852 testStep->draw(&referenceCanvas, reporter);
853
junov@chromium.orgfb103892012-09-20 19:35:43 +0000854 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, false);
855
856 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, true);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000857
caryclark@google.com42639cd2012-06-06 12:03:39 +0000858 // The following test code is disabled because SkProxyCanvas is
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000859 // missing a lot of virtual overrides on get* methods, which are used
860 // to verify canvas state.
861 // Issue: http://code.google.com/p/skia/issues/detail?id=500
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000862
caryclark@google.com42639cd2012-06-06 12:03:39 +0000863 if (false) { // avoid bit rot, suppress warning
864 TestProxyCanvasStateConsistency(reporter, testStep, referenceCanvas);
865 }
866
867 // The following test code is disabled because SkNWayCanvas does not
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000868 // report correct clipping and device bounds information
869 // Issue: http://code.google.com/p/skia/issues/detail?id=501
caryclark@google.com42639cd2012-06-06 12:03:39 +0000870
871 if (false) { // avoid bit rot, suppress warning
872 TestNWayCanvasStateConsistency(reporter, testStep, referenceCanvas);
873 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000874
caryclark@google.com42639cd2012-06-06 12:03:39 +0000875 if (false) { // avoid bit rot, suppress warning
876 test_clipVisitor(reporter, &referenceCanvas);
877 }
reed@google.com7c202932011-12-14 18:48:05 +0000878}
reed@google.com37f3ae02011-11-28 16:06:04 +0000879
880static void TestCanvas(skiatest::Reporter* reporter) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000881 // Init global here because bitmap pixels cannot be alocated during
882 // static initialization
883 kTestBitmap = testBitmap();
reed@google.com37f3ae02011-11-28 16:06:04 +0000884
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000885 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
886 TestOverrideStateConsistency(reporter, testStepArray()[testStep]);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000887 SkPictureTester::TestPictureFlattenedObjectReuse(reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000888 testStepArray()[testStep], 0);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000889 }
junov@chromium.orgcd62ecf2012-08-02 17:43:25 +0000890
891 // Explicitly call reset(), so we don't leak the pixels (since kTestBitmap is a global)
892 kTestBitmap.reset();
reed@google.com37f3ae02011-11-28 16:06:04 +0000893}
894
895#include "TestClassDef.h"
896DEFINE_TESTCLASS("Canvas", TestCanvasClass, TestCanvas)