blob: fd62893392616359f465046cd6f6e445b4b75bba [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));
322SIMPLE_TEST_STEP(SetExternalMatrix, setExternalMatrix(&kTestMatrix));
323SIMPLE_TEST_STEP(DrawData, drawData(kTestText.c_str(), kTestText.size()));
324
325///////////////////////////////////////////////////////////////////////////////
326// Complex test steps
327
rmistry@google.comd6176b02012-08-23 18:14:13 +0000328// Save/restore calls cannot be in isolated simple test steps because the test
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000329// cases that use SkPicture require that save and restore calls be balanced.
rmistry@google.comd6176b02012-08-23 18:14:13 +0000330static void SaveMatrixStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000331 skiatest::Reporter* reporter,
332 CanvasTestStep* testStep) {
333 int saveCount = canvas->getSaveCount();
334 canvas->save(SkCanvas::kMatrix_SaveFlag);
335 canvas->clipRegion(kTestRegion);
336 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
337 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000338 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000339 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000340 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000341 testStep->assertMessage());
342 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() == kTestRegion,
343 testStep->assertMessage());
344}
345TEST_STEP(SaveMatrix, SaveMatrixStep);
346
rmistry@google.comd6176b02012-08-23 18:14:13 +0000347static void SaveClipStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000348 skiatest::Reporter* reporter,
349 CanvasTestStep* testStep) {
350 int saveCount = canvas->getSaveCount();
351 canvas->save(SkCanvas::kClip_SaveFlag);
352 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
353 canvas->clipRegion(kTestRegion);
354 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000355 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000356 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000357 REPORTER_ASSERT_MESSAGE(reporter, !canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000358 testStep->assertMessage());
359 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion,
360 testStep->assertMessage());
361}
362TEST_STEP(SaveClip, SaveClipStep);
363
rmistry@google.comd6176b02012-08-23 18:14:13 +0000364static void SaveMatrixClipStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000365 skiatest::Reporter* reporter,
366 CanvasTestStep* testStep) {
367 int saveCount = canvas->getSaveCount();
368 canvas->save(SkCanvas::kMatrixClip_SaveFlag);
369 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
370 canvas->clipRegion(kTestRegion);
371 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000372 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000373 testStep->assertMessage());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000374 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000375 testStep->assertMessage());
376 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion,
377 testStep->assertMessage());
378}
379TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
380
rmistry@google.comd6176b02012-08-23 18:14:13 +0000381static void SaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000382 skiatest::Reporter* reporter,
383 CanvasTestStep* testStep) {
384 int saveCount = canvas->getSaveCount();
385 canvas->saveLayer(NULL, NULL);
386 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000387 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000388 testStep->assertMessage());
389}
390TEST_STEP(SaveLayer, SaveLayerStep);
391
rmistry@google.comd6176b02012-08-23 18:14:13 +0000392static void BoundedSaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000393 skiatest::Reporter* reporter,
394 CanvasTestStep* testStep) {
395 int saveCount = canvas->getSaveCount();
396 canvas->saveLayer(&kTestRect, NULL);
397 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000398 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000399 testStep->assertMessage());
400}
401TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
402
rmistry@google.comd6176b02012-08-23 18:14:13 +0000403static void PaintSaveLayerStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000404 skiatest::Reporter* reporter,
405 CanvasTestStep* testStep) {
406 int saveCount = canvas->getSaveCount();
407 canvas->saveLayer(NULL, &kTestPaint);
408 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000409 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000410 testStep->assertMessage());
411}
412TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
413
rmistry@google.comd6176b02012-08-23 18:14:13 +0000414static void TwoClipOpsStep(SkCanvas* canvas,
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000415 skiatest::Reporter* reporter,
416 CanvasTestStep* testStep) {
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000417 // This test exercises a functionality in SkPicture that leads to the
rmistry@google.comd6176b02012-08-23 18:14:13 +0000418 // recording of restore offset placeholders. This test will trigger an
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000419 // assertion at playback time if the placeholders are not properly
420 // filled when the recording ends.
421 canvas->clipRect(kTestRect);
422 canvas->clipRegion(kTestRegion);
423}
424TEST_STEP(TwoClipOps, TwoClipOpsStep);
425
epoger@google.com94fa43c2012-04-11 17:51:01 +0000426// exercise fix for http://code.google.com/p/skia/issues/detail?id=560
427// ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
rmistry@google.comd6176b02012-08-23 18:14:13 +0000428static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas,
epoger@google.com94fa43c2012-04-11 17:51:01 +0000429 skiatest::Reporter* reporter,
430 CanvasTestStep* testStep) {
431 SkPaint paint;
432 paint.setStrokeWidth(SkIntToScalar(1));
433 paint.setStyle(SkPaint::kStroke_Style);
434
435 SkPath path;
436 SkPoint pt1 = { 0, 0 };
437 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
438 SkPoint pt3 = { SkIntToScalar(1), 0 };
439 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
440 path.moveTo(pt1);
441 path.lineTo(pt2);
442 path.lineTo(pt3);
443 path.lineTo(pt4);
444
445 canvas->drawPath(path, paint);
446}
447TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
448
rmistry@google.comd6176b02012-08-23 18:14:13 +0000449static void DrawVerticesShaderTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000450 skiatest::Reporter* reporter,
451 CanvasTestStep* testStep) {
452 SkPoint pts[4];
453 pts[0].set(0, 0);
454 pts[1].set(SkIntToScalar(kWidth), 0);
455 pts[2].set(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
456 pts[3].set(0, SkIntToScalar(kHeight));
457 SkPaint paint;
458 SkShader* shader = SkShader::CreateBitmapShader(kTestBitmap,
459 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
460 paint.setShader(shader)->unref();
461 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
462 NULL, NULL, NULL, 0, paint);
463}
464TEST_STEP(DrawVerticesShader, DrawVerticesShaderTestStep);
465
rmistry@google.comd6176b02012-08-23 18:14:13 +0000466static void DrawPictureTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000467 skiatest::Reporter* reporter,
468 CanvasTestStep* testStep) {
469 SkPicture* testPicture = SkNEW_ARGS(SkPicture, ());
470 SkAutoUnref aup(testPicture);
471 SkCanvas* testCanvas = testPicture->beginRecording(kWidth, kHeight);
472 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
473 testCanvas->clipRect(kTestRect);
474 testCanvas->drawRect(kTestRect, kTestPaint);
475 canvas->drawPicture(*testPicture);
476}
477TEST_STEP(DrawPicture, DrawPictureTestStep);
478
rmistry@google.comd6176b02012-08-23 18:14:13 +0000479static void SaveRestoreTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000480 skiatest::Reporter* reporter,
481 CanvasTestStep* testStep) {
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000482 int baseSaveCount = canvas->getSaveCount();
tomhudson@google.com8afae612012-08-14 15:03:35 +0000483 int n = canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000484 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount == n, testStep->assertMessage());
485 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000486 testStep->assertMessage());
487 canvas->save();
488 canvas->save();
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000489 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000490 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000491 canvas->restoreToCount(baseSaveCount + 1);
492 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000493 testStep->assertMessage());
494
495 // should this pin to 1, or be a no-op, or crash?
496 canvas->restoreToCount(0);
497 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
498 testStep->assertMessage());
499}
500TEST_STEP(SaveRestore, SaveRestoreTestStep);
501
rmistry@google.comd6176b02012-08-23 18:14:13 +0000502static void DrawLayerTestStep(SkCanvas* canvas,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000503 skiatest::Reporter* reporter,
504 CanvasTestStep* testStep) {
505 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
506 testStep->assertMessage());
507 canvas->save();
508 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
509 testStep->assertMessage());
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000510 canvas->restore();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000511
reed@google.com7c202932011-12-14 18:48:05 +0000512 const SkRect* bounds = NULL; // null means include entire bounds
513 const SkPaint* paint = NULL;
514
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000515 canvas->saveLayer(bounds, paint);
516 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
517 testStep->assertMessage());
518 canvas->restore();
519 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
520 testStep->assertMessage());
reed@google.com7c202932011-12-14 18:48:05 +0000521
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000522 canvas->saveLayer(bounds, paint);
523 canvas->saveLayer(bounds, paint);
524 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
525 testStep->assertMessage());
526 canvas->restore();
527 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
528 testStep->assertMessage());
529 canvas->restore();
reed@google.com7c202932011-12-14 18:48:05 +0000530 // now layer count should be 0
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000531 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
532 testStep->assertMessage());
533}
534TEST_STEP(DrawLayer, DrawLayerTestStep);
reed@google.com3b3e8952012-08-16 20:53:31 +0000535
536static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas,
537 skiatest::Reporter* reporter,
538 CanvasTestStep* testStep) {
539 // This test step challenges the TestDeferredCanvasStateConsistency
540 // test cases because the opaque paint can trigger an optimization
541 // that discards previously recorded commands. The challenge is to maintain
542 // correct clip and matrix stack state.
543 canvas->resetMatrix();
544 canvas->rotate(SkIntToScalar(30));
545 canvas->save();
546 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
547 canvas->save();
548 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
549 SkPaint paint;
550 paint.setColor(0xFFFFFFFF);
551 canvas->drawPaint(paint);
552 canvas->restore();
553 canvas->restore();
554}
555TEST_STEP(NestedSaveRestoreWithSolidPaint, \
556 NestedSaveRestoreWithSolidPaintTestStep);
557
558static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas,
559 skiatest::Reporter* reporter,
560 CanvasTestStep* testStep) {
561 // This test step challenges the TestDeferredCanvasStateConsistency
562 // test case because the canvas flush on a deferred canvas will
563 // reset the recording session. The challenge is to maintain correct
564 // clip and matrix stack state on the playback canvas.
565 canvas->resetMatrix();
566 canvas->rotate(SkIntToScalar(30));
567 canvas->save();
568 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
569 canvas->save();
570 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
571 canvas->drawRect(kTestRect,kTestPaint);
572 canvas->flush();
573 canvas->restore();
574 canvas->restore();
575}
576TEST_STEP(NestedSaveRestoreWithFlush, \
577 NestedSaveRestoreWithFlushTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000578
579static void AssertCanvasStatesEqual(skiatest::Reporter* reporter,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000580 const SkCanvas* canvas1,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000581 const SkCanvas* canvas2,
582 CanvasTestStep* testStep) {
583 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
584 canvas2->getDeviceSize(), testStep->assertMessage());
585 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
586 canvas2->getSaveCount(), testStep->assertMessage());
587 REPORTER_ASSERT_MESSAGE(reporter, canvas1->isDrawingToLayer() ==
588 canvas2->isDrawingToLayer(), testStep->assertMessage());
reed@google.com3b3e8952012-08-16 20:53:31 +0000589
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000590 SkRect bounds1, bounds2;
591 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.com3b3e8952012-08-16 20:53:31 +0000592 canvas1->getClipBounds(&bounds1) == canvas2->getClipBounds(&bounds2),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000593 testStep->assertMessage());
594 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
reed@google.com3b3e8952012-08-16 20:53:31 +0000595 testStep->assertMessage());
596
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000597 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
598 canvas2->getDrawFilter(), testStep->assertMessage());
599 SkIRect deviceBounds1, deviceBounds2;
600 REPORTER_ASSERT_MESSAGE(reporter,
601 canvas1->getClipDeviceBounds(&deviceBounds1) ==
602 canvas2->getClipDeviceBounds(&deviceBounds2),
603 testStep->assertMessage());
604 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2,
605 testStep->assertMessage());
606 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getBounder() ==
607 canvas2->getBounder(), testStep->assertMessage());
608 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
609 canvas2->getTotalMatrix(), testStep->assertMessage());
610 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getClipType() ==
611 canvas2->getClipType(), testStep->assertMessage());
612 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalClip() ==
613 canvas2->getTotalClip(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000614
615 // The following test code is commented out because the test fails when
rmistry@google.comd6176b02012-08-23 18:14:13 +0000616 // the canvas is an SkPictureRecord or SkDeferredCanvas
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000617 // Issue: http://code.google.com/p/skia/issues/detail?id=498
618 // Also, creating a LayerIter on an SkProxyCanvas crashes
619 // Issue: http://code.google.com/p/skia/issues/detail?id=499
620 /*
621 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
622 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
623 while (!layerIter1.done() && !layerIter2.done()) {
624 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
625 layerIter2.matrix(), testStep->assertMessage());
626 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
627 layerIter2.clip(), testStep->assertMessage());
628 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
629 layerIter2.paint(), testStep->assertMessage());
630 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
631 layerIter2.x(), testStep->assertMessage());
632 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
633 layerIter2.y(), testStep->assertMessage());
634 layerIter1.next();
635 layerIter2.next();
636 }
637 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
638 testStep->assertMessage());
639 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
640 testStep->assertMessage());
641 */
642}
643
644// The following class groups static functions that need to access
645// the privates members of SkPictureRecord
646class SkPictureTester {
647private:
reed@google.come2589ae2012-07-10 19:38:01 +0000648 static int EQ(const SkFlatData* a, const SkFlatData* b) {
649 return *a == *b;
650 }
651
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000652 static void AssertFlattenedObjectsEqual(
653 SkPictureRecord* referenceRecord,
654 SkPictureRecord* testRecord,
655 skiatest::Reporter* reporter,
656 CanvasTestStep* testStep) {
657
658 REPORTER_ASSERT_MESSAGE(reporter,
djsollen@google.comc9ab9872012-08-29 18:52:07 +0000659 referenceRecord->fBitmapHeap->count() ==
660 testRecord->fBitmapHeap->count(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000661 REPORTER_ASSERT_MESSAGE(reporter,
662 referenceRecord->fMatrices.count() ==
663 testRecord->fMatrices.count(), testStep->assertMessage());
664 for (int i = 0; i < referenceRecord->fMatrices.count(); ++i) {
665 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.come2589ae2012-07-10 19:38:01 +0000666 EQ(referenceRecord->fMatrices[i], testRecord->fMatrices[i]),
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000667 testStep->assertMessage());
668 }
669 REPORTER_ASSERT_MESSAGE(reporter,
670 referenceRecord->fPaints.count() ==
671 testRecord->fPaints.count(), testStep->assertMessage());
672 for (int i = 0; i < referenceRecord->fPaints.count(); ++i) {
673 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.come2589ae2012-07-10 19:38:01 +0000674 EQ(referenceRecord->fPaints[i], testRecord->fPaints[i]),
675 testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000676 }
677 REPORTER_ASSERT_MESSAGE(reporter,
678 referenceRecord->fRegions.count() ==
679 testRecord->fRegions.count(), testStep->assertMessage());
680 for (int i = 0; i < referenceRecord->fRegions.count(); ++i) {
681 REPORTER_ASSERT_MESSAGE(reporter,
reed@google.come2589ae2012-07-10 19:38:01 +0000682 EQ(referenceRecord->fRegions[i], testRecord->fRegions[i]),
683 testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000684 }
685 REPORTER_ASSERT_MESSAGE(reporter,
686 !referenceRecord->fPathHeap ==
687 !testRecord->fPathHeap,
688 testStep->assertMessage());
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000689 // The following tests are commented out because they currently
690 // fail. Issue: http://code.google.com/p/skia/issues/detail?id=507
691 /*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000692 if (referenceRecord->fPathHeap) {
693 REPORTER_ASSERT_MESSAGE(reporter,
694 referenceRecord->fPathHeap->count() ==
695 testRecord->fPathHeap->count(),
696 testStep->assertMessage());
697 for (int i = 0; i < referenceRecord->fPathHeap->count(); ++i) {
698 REPORTER_ASSERT_MESSAGE(reporter,
699 (*referenceRecord->fPathHeap)[i] ==
700 (*testRecord->fPathHeap)[i], testStep->assertMessage());
701 }
702 }
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000703 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000704
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000705 }
706
707public:
708
rmistry@google.comd6176b02012-08-23 18:14:13 +0000709 static void TestPictureFlattenedObjectReuse(skiatest::Reporter* reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000710 CanvasTestStep* testStep,
711 uint32_t recordFlags) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000712 // Verify that when a test step is executed twice, no extra resources
713 // are flattened during the second execution
714 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
715 SkPicture referencePicture;
716 SkCanvas* referenceCanvas = referencePicture.beginRecording(kWidth,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000717 kHeight, recordFlags);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000718 testStep->draw(referenceCanvas, reporter);
719 SkPicture testPicture;
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000720 SkCanvas* testCanvas = testPicture.beginRecording(kWidth,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000721 kHeight, recordFlags);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000722 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000723 testStep->setAssertMessageFormat(kPictureSecondDrawAssertMessageFormat);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000724 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000725
726 SkPictureRecord* referenceRecord = static_cast<SkPictureRecord*>(
727 referenceCanvas);
728 SkPictureRecord* testRecord = static_cast<SkPictureRecord*>(
729 testCanvas);
730 testStep->setAssertMessageFormat(kPictureResourceReuseMessageFormat);
731 AssertFlattenedObjectsEqual(referenceRecord, testRecord,
junov@chromium.org76b9c4b2012-02-22 21:24:41 +0000732 reporter, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000733 }
734};
735
junov@chromium.org88e29142012-08-07 16:48:22 +0000736// The following class groups static functions that need to access
737// the privates members of SkDeferredCanvas
738class SkDeferredCanvasTester {
739public:
740 static void TestDeferredCanvasStateConsistency(
741 skiatest::Reporter* reporter,
742 CanvasTestStep* testStep,
junov@chromium.orgfb103892012-09-20 19:35:43 +0000743 const SkCanvas& referenceCanvas, bool silent) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000744
junov@chromium.org88e29142012-08-07 16:48:22 +0000745 SkBitmap deferredStore;
746 createBitmap(&deferredStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
747 SkDevice deferredDevice(deferredStore);
748 SkDeferredCanvas deferredCanvas(&deferredDevice);
749 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
750 testStep->draw(&deferredCanvas, reporter);
751 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
752 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
753 testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000754
junov@chromium.orgfb103892012-09-20 19:35:43 +0000755 if (silent) {
756 deferredCanvas.silentFlush();
757 } else {
758 deferredCanvas.flush();
759 }
760
761 testStep->setAssertMessageFormat(
762 silent ? kDeferredPostSilentFlushPlaybackAssertMessageFormat :
junov@chromium.org88e29142012-08-07 16:48:22 +0000763 kDeferredPostFlushPlaybackAssertMessageFormat);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000764 AssertCanvasStatesEqual(reporter,
junov@chromium.org88e29142012-08-07 16:48:22 +0000765 deferredCanvas.immediateCanvas(),
766 &referenceCanvas, testStep);
junov@chromium.orgcff01c52012-07-18 21:50:26 +0000767
junov@chromium.org88e29142012-08-07 16:48:22 +0000768 // Verified that deferred canvas state is not affected by flushing
769 // pending draw operations
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000770
junov@chromium.org88e29142012-08-07 16:48:22 +0000771 // The following test code is commented out because it currently fails.
772 // Issue: http://code.google.com/p/skia/issues/detail?id=496
773 /*
774 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
775 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
776 testStep);
777 */
778 }
779};
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000780
caryclark@google.com42639cd2012-06-06 12:03:39 +0000781// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000782static void TestProxyCanvasStateConsistency(
783 skiatest::Reporter* reporter,
784 CanvasTestStep* testStep,
785 const SkCanvas& referenceCanvas) {
786
787 SkBitmap indirectStore;
788 createBitmap(&indirectStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
789 SkDevice indirectDevice(indirectStore);
790 SkCanvas indirectCanvas(&indirectDevice);
791 SkProxyCanvas proxyCanvas(&indirectCanvas);
792 testStep->setAssertMessageFormat(kProxyDrawAssertMessageFormat);
793 testStep->draw(&proxyCanvas, reporter);
794 // Verify that the SkProxyCanvas reports consitent state
795 testStep->setAssertMessageFormat(kProxyStateAssertMessageFormat);
796 AssertCanvasStatesEqual(reporter, &proxyCanvas, &referenceCanvas,
797 testStep);
798 // Verify that the indirect canvas reports consitent state
799 testStep->setAssertMessageFormat(kProxyIndirectStateAssertMessageFormat);
800 AssertCanvasStatesEqual(reporter, &indirectCanvas, &referenceCanvas,
801 testStep);
802}
803
caryclark@google.com42639cd2012-06-06 12:03:39 +0000804// unused
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000805static void TestNWayCanvasStateConsistency(
806 skiatest::Reporter* reporter,
807 CanvasTestStep* testStep,
808 const SkCanvas& referenceCanvas) {
809
810 SkBitmap indirectStore1;
811 createBitmap(&indirectStore1, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
812 SkDevice indirectDevice1(indirectStore1);
813 SkCanvas indirectCanvas1(&indirectDevice1);
814
815 SkBitmap indirectStore2;
816 createBitmap(&indirectStore2, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
817 SkDevice indirectDevice2(indirectStore2);
818 SkCanvas indirectCanvas2(&indirectDevice2);
819
djsollen@google.comf0a062b2012-05-01 16:50:25 +0000820 SkISize canvasSize = referenceCanvas.getDeviceSize();
821 SkNWayCanvas nWayCanvas(canvasSize.width(), canvasSize.height());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000822 nWayCanvas.addCanvas(&indirectCanvas1);
823 nWayCanvas.addCanvas(&indirectCanvas2);
824
825 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
826 testStep->draw(&nWayCanvas, reporter);
827 // Verify that the SkProxyCanvas reports consitent state
828 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
829 AssertCanvasStatesEqual(reporter, &nWayCanvas, &referenceCanvas,
830 testStep);
831 // Verify that the indirect canvases report consitent state
832 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
833 AssertCanvasStatesEqual(reporter, &indirectCanvas1, &referenceCanvas,
834 testStep);
835 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
836 AssertCanvasStatesEqual(reporter, &indirectCanvas2, &referenceCanvas,
837 testStep);
838}
839
840/*
841 * This sub-test verifies that the test step passes when executed
842 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
843 * that the all canvas derivatives report the same state as an SkCanvas
844 * after having executed the test step.
845 */
rmistry@google.comd6176b02012-08-23 18:14:13 +0000846static void TestOverrideStateConsistency(skiatest::Reporter* reporter,
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000847 CanvasTestStep* testStep) {
848 SkBitmap referenceStore;
849 createBitmap(&referenceStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
850 SkDevice referenceDevice(referenceStore);
851 SkCanvas referenceCanvas(&referenceDevice);
852 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
853 testStep->draw(&referenceCanvas, reporter);
854
junov@chromium.orgfb103892012-09-20 19:35:43 +0000855 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, false);
856
857 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, true);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000858
caryclark@google.com42639cd2012-06-06 12:03:39 +0000859 // The following test code is disabled because SkProxyCanvas is
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000860 // missing a lot of virtual overrides on get* methods, which are used
861 // to verify canvas state.
862 // Issue: http://code.google.com/p/skia/issues/detail?id=500
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000863
caryclark@google.com42639cd2012-06-06 12:03:39 +0000864 if (false) { // avoid bit rot, suppress warning
865 TestProxyCanvasStateConsistency(reporter, testStep, referenceCanvas);
866 }
867
868 // The following test code is disabled because SkNWayCanvas does not
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000869 // report correct clipping and device bounds information
870 // Issue: http://code.google.com/p/skia/issues/detail?id=501
caryclark@google.com42639cd2012-06-06 12:03:39 +0000871
872 if (false) { // avoid bit rot, suppress warning
873 TestNWayCanvasStateConsistency(reporter, testStep, referenceCanvas);
874 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000875
caryclark@google.com42639cd2012-06-06 12:03:39 +0000876 if (false) { // avoid bit rot, suppress warning
877 test_clipVisitor(reporter, &referenceCanvas);
878 }
reed@google.com7c202932011-12-14 18:48:05 +0000879}
reed@google.com37f3ae02011-11-28 16:06:04 +0000880
881static void TestCanvas(skiatest::Reporter* reporter) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000882 // Init global here because bitmap pixels cannot be alocated during
883 // static initialization
884 kTestBitmap = testBitmap();
reed@google.com37f3ae02011-11-28 16:06:04 +0000885
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000886 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
887 TestOverrideStateConsistency(reporter, testStepArray()[testStep]);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000888 SkPictureTester::TestPictureFlattenedObjectReuse(reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000889 testStepArray()[testStep], 0);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000890 }
junov@chromium.orgcd62ecf2012-08-02 17:43:25 +0000891
892 // Explicitly call reset(), so we don't leak the pixels (since kTestBitmap is a global)
893 kTestBitmap.reset();
reed@google.com37f3ae02011-11-28 16:06:04 +0000894}
895
896#include "TestClassDef.h"
897DEFINE_TESTCLASS("Canvas", TestCanvasClass, TestCanvas)