blob: 80f44c6d1191463f69ddd9c30c92f93e4dc94f63 [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 *
21 * static void MyTestStepFunction(SkCanvas* canvas,
22 * 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();
82
83 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
96static 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";
104static const char* const kCanvasDrawAssertMessageFormat =
105 "Drawing test step %s with SkCanvas";
106static const char* const kPictureDrawAssertMessageFormat =
107 "Drawing test step %s with SkPicture";
108static const char* const kPictureSecondDrawAssertMessageFormat =
109 "Duplicate draw of test step %s with SkPicture";
110static const char* const kPictureReDrawAssertMessageFormat =
111 "Playing back test step %s from an SkPicture to another SkPicture";
112static const char* const kDeferredDrawAssertMessageFormat =
113 "Drawing test step %s with SkDeferredCanvas";
114static const char* const kProxyDrawAssertMessageFormat =
115 "Drawing test step %s with SkProxyCanvas";
116static const char* const kNWayDrawAssertMessageFormat =
117 "Drawing test step %s with SkNWayCanvas";
118static const char* const kRoundTripAssertMessageFormat =
119 "test step %s, SkPicture consistency after round trip";
120static const char* const kPictureRecoringAssertMessageFormat =
121 "test step %s, SkPicture state consistency after recording";
122static const char* const kPicturePlaybackAssertMessageFormat =
123 "test step %s, SkPicture state consistency in playback canvas";
124static const char* const kDeferredPreFlushAssertMessageFormat =
125 "test step %s, SkDeferredCanvas state consistency before flush";
126static const char* const kDeferredPostFlushAssertMessageFormat =
127 "test step %s, SkDeferredCanvas state consistency after flush";
128static const char* const kPictureResourceReuseMessageFormat =
129 "test step %s, SkPicture duplicate flattened object test";
130static const char* const kProxyStateAssertMessageFormat =
131 "test step %s, SkProxyCanvas state consistency";
132static const char* const kProxyIndirectStateAssertMessageFormat =
133 "test step %s, SkProxyCanvas indirect canvas state consistency";
134static const char* const kNWayStateAssertMessageFormat =
135 "test step %s, SkNWayCanvas state consistency";
136static const char* const kNWayIndirect1StateAssertMessageFormat =
137 "test step %s, SkNWayCanvas indirect canvas 1 state consistency";
138static const char* const kNWayIndirect2StateAssertMessageFormat =
139 "test step %s, SkNWayCanvas indirect canvas 2 state consistency";
140
141static void createBitmap(SkBitmap* bm, SkBitmap::Config config, SkColor color) {
142 bm->setConfig(config, kWidth, kHeight);
143 bm->allocPixels();
144 bm->eraseColor(color);
145}
146
147class CanvasTestStep;
148static SkTDArray<CanvasTestStep*>& testStepArray() {
149 static SkTDArray<CanvasTestStep*> theTests;
150 return theTests;
151}
152
153class CanvasTestStep {
154public:
155 CanvasTestStep() {
156 *testStepArray().append() = this;
157 fAssertMessageFormat = kDefaultAssertMessageFormat;
158 }
djsollen@google.come63793a2012-03-21 15:39:03 +0000159 virtual ~CanvasTestStep() { }
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000160
161 virtual void draw(SkCanvas*, skiatest::Reporter*) = 0;
162 virtual const char* name() const = 0;
163
164 const char* assertMessage() {
165 fAssertMessage.printf(fAssertMessageFormat, name());
166 return fAssertMessage.c_str();
167 }
168
169 void setAssertMessageFormat(const char* format) {
170 fAssertMessageFormat = format;
171 }
172
173private:
174 SkString fAssertMessage;
175 const char* fAssertMessageFormat;
176};
177
178///////////////////////////////////////////////////////////////////////////////
179// Constants used by test steps
180
181const SkRect kTestRect =
182 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
183 SkIntToScalar(2), SkIntToScalar(1));
184static SkMatrix testMatrix() {
185 SkMatrix matrix;
186 matrix.reset();
187 matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
188 return matrix;
189}
190const SkMatrix kTestMatrix = testMatrix();
191static SkPath testPath() {
192 SkPath path;
193 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
194 SkIntToScalar(2), SkIntToScalar(1)));
195 return path;
196}
197const SkPath kTestPath = testPath();
198static SkRegion testRegion() {
199 SkRegion region;
200 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
201 region.setRect(rect);
202 return region;
203}
204const SkIRect kTestIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
205const SkRegion kTestRegion = testRegion();
206const SkColor kTestColor = 0x01020304;
207const SkPaint kTestPaint;
208const SkPoint kTestPoints[3] = {
209 {SkIntToScalar(0), SkIntToScalar(0)},
210 {SkIntToScalar(2), SkIntToScalar(1)},
211 {SkIntToScalar(0), SkIntToScalar(2)}
212};
213const size_t kTestPointCount = 3;
214static SkBitmap testBitmap() {
215 SkBitmap bitmap;
216 createBitmap(&bitmap, SkBitmap::kARGB_8888_Config, 0x05060708);
217 return bitmap;
218}
219SkBitmap kTestBitmap; // cannot be created during static init
220SkString kTestText("Hello World");
221SkPoint kTestPoint = SkPoint::Make(SkIntToScalar(0), SkIntToScalar(1));
222
223///////////////////////////////////////////////////////////////////////////////
224// Macros for defining test steps
225
226#define TEST_STEP(NAME, FUNCTION) \
227class NAME##_TestStep : public CanvasTestStep{ \
228public: \
229 virtual void draw(SkCanvas* canvas, skiatest::Reporter* reporter) { \
230 FUNCTION (canvas, reporter, this); \
231 } \
232 virtual const char* name() const {return #NAME ;} \
233}; \
234static NAME##_TestStep NAME##_TestStepInstance;
235
236#define SIMPLE_TEST_STEP(NAME, CALL) \
237static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter*, \
238 CanvasTestStep*) { \
239 canvas-> CALL ; \
240} \
241TEST_STEP(NAME, NAME##TestStep )
242
243#define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
244static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter* reporter, \
245 CanvasTestStep* testStep) { \
246 REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \
247 testStep->assertMessage()); \
248} \
249TEST_STEP(NAME, NAME##TestStep )
250
251
252///////////////////////////////////////////////////////////////////////////////
253// Basic test steps for most virtual methods in SkCanvas that draw or affect
254// the state of the canvas.
255
junov@chromium.orga907ac32012-02-24 21:54:07 +0000256SIMPLE_TEST_STEP(SaveMatrix, save(SkCanvas::kMatrix_SaveFlag));
257SIMPLE_TEST_STEP(SaveClip, save(SkCanvas::kClip_SaveFlag));
258SIMPLE_TEST_STEP(SaveMatrixClip, save(SkCanvas::kMatrixClip_SaveFlag));
259SIMPLE_TEST_STEP(SaveLayer, saveLayer(NULL, NULL));
260SIMPLE_TEST_STEP(BoundedSaveLayer, saveLayer(&kTestRect, NULL));
261SIMPLE_TEST_STEP(PaintSaveLayer, saveLayer(NULL, &kTestPaint));
262SIMPLE_TEST_STEP_WITH_ASSERT(Translate,
263 translate(SkIntToScalar(1), SkIntToScalar(2)));
264SIMPLE_TEST_STEP_WITH_ASSERT(Scale,
265 scale(SkIntToScalar(1), SkIntToScalar(2)));
266SIMPLE_TEST_STEP_WITH_ASSERT(Rotate, rotate(SkIntToScalar(1)));
267SIMPLE_TEST_STEP_WITH_ASSERT(Skew,
268 skew(SkIntToScalar(1), SkIntToScalar(2)));
269SIMPLE_TEST_STEP_WITH_ASSERT(Concat, concat(kTestMatrix));
270SIMPLE_TEST_STEP(SetMatrix, setMatrix(kTestMatrix));
271SIMPLE_TEST_STEP_WITH_ASSERT(ClipRect, clipRect(kTestRect));
272SIMPLE_TEST_STEP_WITH_ASSERT(ClipPath, clipPath(kTestPath));
273SIMPLE_TEST_STEP_WITH_ASSERT(ClipRegion,
274 clipRegion(kTestRegion, SkRegion::kReplace_Op));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000275SIMPLE_TEST_STEP(Clear, clear(kTestColor));
276SIMPLE_TEST_STEP(DrawPaint, drawPaint(kTestPaint));
277SIMPLE_TEST_STEP(DrawPointsPoints, drawPoints(SkCanvas::kPoints_PointMode,
278 kTestPointCount, kTestPoints, kTestPaint));
279SIMPLE_TEST_STEP(DrawPointsLiness, drawPoints(SkCanvas::kLines_PointMode,
280 kTestPointCount, kTestPoints, kTestPaint));
281SIMPLE_TEST_STEP(DrawPointsPolygon, drawPoints(SkCanvas::kPolygon_PointMode,
282 kTestPointCount, kTestPoints, kTestPaint));
283SIMPLE_TEST_STEP(DrawRect, drawRect(kTestRect, kTestPaint));
284SIMPLE_TEST_STEP(DrawPath, drawPath(kTestPath, kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000285SIMPLE_TEST_STEP(DrawBitmap, drawBitmap(kTestBitmap, 0, 0));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000286SIMPLE_TEST_STEP(DrawBitmapPaint, drawBitmap(kTestBitmap, 0, 0, &kTestPaint));
287SIMPLE_TEST_STEP(DrawBitmapRect, drawBitmapRect(kTestBitmap, NULL, kTestRect,
288 NULL));
289SIMPLE_TEST_STEP(DrawBitmapRectSrcRect, drawBitmapRect(kTestBitmap,
290 &kTestIRect, kTestRect, NULL));
291SIMPLE_TEST_STEP(DrawBitmapRectPaint, drawBitmapRect(kTestBitmap, NULL,
292 kTestRect, &kTestPaint));
293SIMPLE_TEST_STEP(DrawBitmapMatrix, drawBitmapMatrix(kTestBitmap, kTestMatrix,
294 NULL));
295SIMPLE_TEST_STEP(DrawBitmapMatrixPaint, drawBitmapMatrix(kTestBitmap,
296 kTestMatrix, &kTestPaint));
297SIMPLE_TEST_STEP(DrawBitmapNine, drawBitmapNine(kTestBitmap, kTestIRect,
298 kTestRect, NULL));
299SIMPLE_TEST_STEP(DrawBitmapNinePaint, drawBitmapNine(kTestBitmap, kTestIRect,
300 kTestRect, &kTestPaint));
junov@chromium.org87f982c2012-02-23 21:34:34 +0000301SIMPLE_TEST_STEP(DrawSprite, drawSprite(kTestBitmap, 0, 0, NULL));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000302SIMPLE_TEST_STEP(DrawSpritePaint, drawSprite(kTestBitmap, 0, 0, &kTestPaint));
303SIMPLE_TEST_STEP(DrawText, drawText(kTestText.c_str(), kTestText.size(),
304 0, 1, kTestPaint));
305SIMPLE_TEST_STEP(DrawPosText, drawPosText(kTestText.c_str(),
306 kTestText.size(), &kTestPoint, kTestPaint));
307SIMPLE_TEST_STEP(DrawTextOnPath, drawTextOnPath(kTestText.c_str(),
308 kTestText.size(), kTestPath, NULL, kTestPaint));
309SIMPLE_TEST_STEP(DrawTextOnPathMatrix, drawTextOnPath(kTestText.c_str(),
310 kTestText.size(), kTestPath, &kTestMatrix, kTestPaint));
311SIMPLE_TEST_STEP(SetExternalMatrix, setExternalMatrix(&kTestMatrix));
312SIMPLE_TEST_STEP(DrawData, drawData(kTestText.c_str(), kTestText.size()));
313
314///////////////////////////////////////////////////////////////////////////////
315// Complex test steps
316
epoger@google.com94fa43c2012-04-11 17:51:01 +0000317// exercise fix for http://code.google.com/p/skia/issues/detail?id=560
318// ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
319static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas,
320 skiatest::Reporter* reporter,
321 CanvasTestStep* testStep) {
322 SkPaint paint;
323 paint.setStrokeWidth(SkIntToScalar(1));
324 paint.setStyle(SkPaint::kStroke_Style);
325
326 SkPath path;
327 SkPoint pt1 = { 0, 0 };
328 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
329 SkPoint pt3 = { SkIntToScalar(1), 0 };
330 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
331 path.moveTo(pt1);
332 path.lineTo(pt2);
333 path.lineTo(pt3);
334 path.lineTo(pt4);
335
336 canvas->drawPath(path, paint);
337}
338TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
339
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000340static void DrawVerticesShaderTestStep(SkCanvas* canvas,
341 skiatest::Reporter* reporter,
342 CanvasTestStep* testStep) {
343 SkPoint pts[4];
344 pts[0].set(0, 0);
345 pts[1].set(SkIntToScalar(kWidth), 0);
346 pts[2].set(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
347 pts[3].set(0, SkIntToScalar(kHeight));
348 SkPaint paint;
349 SkShader* shader = SkShader::CreateBitmapShader(kTestBitmap,
350 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
351 paint.setShader(shader)->unref();
352 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
353 NULL, NULL, NULL, 0, paint);
354}
355TEST_STEP(DrawVerticesShader, DrawVerticesShaderTestStep);
356
357static void DrawPictureTestStep(SkCanvas* canvas,
358 skiatest::Reporter* reporter,
359 CanvasTestStep* testStep) {
360 SkPicture* testPicture = SkNEW_ARGS(SkPicture, ());
361 SkAutoUnref aup(testPicture);
362 SkCanvas* testCanvas = testPicture->beginRecording(kWidth, kHeight);
363 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
364 testCanvas->clipRect(kTestRect);
365 testCanvas->drawRect(kTestRect, kTestPaint);
366 canvas->drawPicture(*testPicture);
367}
368TEST_STEP(DrawPicture, DrawPictureTestStep);
369
370static void SaveRestoreTestStep(SkCanvas* canvas,
371 skiatest::Reporter* reporter,
372 CanvasTestStep* testStep) {
373 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
374 testStep->assertMessage());
375 size_t n = canvas->save();
376 REPORTER_ASSERT_MESSAGE(reporter, 1 == n, testStep->assertMessage());
377 REPORTER_ASSERT_MESSAGE(reporter, 2 == canvas->getSaveCount(),
378 testStep->assertMessage());
379 canvas->save();
380 canvas->save();
381 REPORTER_ASSERT_MESSAGE(reporter, 4 == canvas->getSaveCount(),
382 testStep->assertMessage());
383 canvas->restoreToCount(2);
384 REPORTER_ASSERT_MESSAGE(reporter, 2 == canvas->getSaveCount(),
385 testStep->assertMessage());
386
387 // should this pin to 1, or be a no-op, or crash?
388 canvas->restoreToCount(0);
389 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
390 testStep->assertMessage());
391}
392TEST_STEP(SaveRestore, SaveRestoreTestStep);
393
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000394static void DrawLayerTestStep(SkCanvas* canvas,
395 skiatest::Reporter* reporter,
396 CanvasTestStep* testStep) {
397 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
398 testStep->assertMessage());
399 canvas->save();
400 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
401 testStep->assertMessage());
reed@google.com7c202932011-12-14 18:48:05 +0000402
403 const SkRect* bounds = NULL; // null means include entire bounds
404 const SkPaint* paint = NULL;
405
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000406 canvas->saveLayer(bounds, paint);
407 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
408 testStep->assertMessage());
409 canvas->restore();
410 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
411 testStep->assertMessage());
reed@google.com7c202932011-12-14 18:48:05 +0000412
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000413 canvas->saveLayer(bounds, paint);
414 canvas->saveLayer(bounds, paint);
415 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
416 testStep->assertMessage());
417 canvas->restore();
418 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
419 testStep->assertMessage());
420 canvas->restore();
reed@google.com7c202932011-12-14 18:48:05 +0000421 // now layer count should be 0
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000422 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
423 testStep->assertMessage());
424}
425TEST_STEP(DrawLayer, DrawLayerTestStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000426
427static void AssertCanvasStatesEqual(skiatest::Reporter* reporter,
428 const SkCanvas* canvas1,
429 const SkCanvas* canvas2,
430 CanvasTestStep* testStep) {
431 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
432 canvas2->getDeviceSize(), testStep->assertMessage());
433 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
434 canvas2->getSaveCount(), testStep->assertMessage());
435 REPORTER_ASSERT_MESSAGE(reporter, canvas1->isDrawingToLayer() ==
436 canvas2->isDrawingToLayer(), testStep->assertMessage());
437 SkRect bounds1, bounds2;
438 REPORTER_ASSERT_MESSAGE(reporter,
439 canvas1->getClipBounds(&bounds1, SkCanvas::kAA_EdgeType) ==
440 canvas2->getClipBounds(&bounds2, SkCanvas::kAA_EdgeType),
441 testStep->assertMessage());
442 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
443 testStep->assertMessage());
444 REPORTER_ASSERT_MESSAGE(reporter,
445 canvas1->getClipBounds(&bounds1, SkCanvas::kBW_EdgeType) ==
446 canvas2->getClipBounds(&bounds2, SkCanvas::kBW_EdgeType),
447 testStep->assertMessage());
448 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
449 testStep->assertMessage());
450 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
451 canvas2->getDrawFilter(), testStep->assertMessage());
452 SkIRect deviceBounds1, deviceBounds2;
453 REPORTER_ASSERT_MESSAGE(reporter,
454 canvas1->getClipDeviceBounds(&deviceBounds1) ==
455 canvas2->getClipDeviceBounds(&deviceBounds2),
456 testStep->assertMessage());
457 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2,
458 testStep->assertMessage());
459 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getBounder() ==
460 canvas2->getBounder(), testStep->assertMessage());
461 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
462 canvas2->getTotalMatrix(), testStep->assertMessage());
463 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getClipType() ==
464 canvas2->getClipType(), testStep->assertMessage());
465 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalClip() ==
466 canvas2->getTotalClip(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000467
468 // The following test code is commented out because the test fails when
469 // the canvas is an SkPictureRecord or SkDeferredCanvas
470 // Issue: http://code.google.com/p/skia/issues/detail?id=498
471 // Also, creating a LayerIter on an SkProxyCanvas crashes
472 // Issue: http://code.google.com/p/skia/issues/detail?id=499
473 /*
474 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
475 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
476 while (!layerIter1.done() && !layerIter2.done()) {
477 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
478 layerIter2.matrix(), testStep->assertMessage());
479 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
480 layerIter2.clip(), testStep->assertMessage());
481 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
482 layerIter2.paint(), testStep->assertMessage());
483 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
484 layerIter2.x(), testStep->assertMessage());
485 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
486 layerIter2.y(), testStep->assertMessage());
487 layerIter1.next();
488 layerIter2.next();
489 }
490 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
491 testStep->assertMessage());
492 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
493 testStep->assertMessage());
494 */
495}
496
497// The following class groups static functions that need to access
498// the privates members of SkPictureRecord
499class SkPictureTester {
500private:
501 static void AssertFlattenedObjectsEqual(
502 SkPictureRecord* referenceRecord,
503 SkPictureRecord* testRecord,
504 skiatest::Reporter* reporter,
505 CanvasTestStep* testStep) {
506
507 REPORTER_ASSERT_MESSAGE(reporter,
508 referenceRecord->fBitmaps.count() ==
509 testRecord->fBitmaps.count(), testStep->assertMessage());
510 for (int i = 0; i < referenceRecord->fBitmaps.count(); ++i) {
511 REPORTER_ASSERT_MESSAGE(reporter,
512 SkFlatData::Compare(referenceRecord->fBitmaps[i],
513 testRecord->fBitmaps[i]) == 0, testStep->assertMessage());
514 }
515 REPORTER_ASSERT_MESSAGE(reporter,
516 referenceRecord->fMatrices.count() ==
517 testRecord->fMatrices.count(), testStep->assertMessage());
518 for (int i = 0; i < referenceRecord->fMatrices.count(); ++i) {
519 REPORTER_ASSERT_MESSAGE(reporter,
520 SkFlatData::Compare(referenceRecord->fMatrices[i],
521 testRecord->fMatrices[i]) == 0,
522 testStep->assertMessage());
523 }
524 REPORTER_ASSERT_MESSAGE(reporter,
525 referenceRecord->fPaints.count() ==
526 testRecord->fPaints.count(), testStep->assertMessage());
527 for (int i = 0; i < referenceRecord->fPaints.count(); ++i) {
528 REPORTER_ASSERT_MESSAGE(reporter,
529 SkFlatData::Compare(referenceRecord->fPaints[i],
530 testRecord->fPaints[i]) == 0, testStep->assertMessage());
531 }
532 REPORTER_ASSERT_MESSAGE(reporter,
533 referenceRecord->fRegions.count() ==
534 testRecord->fRegions.count(), testStep->assertMessage());
535 for (int i = 0; i < referenceRecord->fRegions.count(); ++i) {
536 REPORTER_ASSERT_MESSAGE(reporter,
537 SkFlatData::Compare(referenceRecord->fRegions[i],
538 testRecord->fRegions[i]) == 0, testStep->assertMessage());
539 }
540 REPORTER_ASSERT_MESSAGE(reporter,
541 !referenceRecord->fPathHeap ==
542 !testRecord->fPathHeap,
543 testStep->assertMessage());
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000544 // The following tests are commented out because they currently
545 // fail. Issue: http://code.google.com/p/skia/issues/detail?id=507
546 /*
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000547 if (referenceRecord->fPathHeap) {
548 REPORTER_ASSERT_MESSAGE(reporter,
549 referenceRecord->fPathHeap->count() ==
550 testRecord->fPathHeap->count(),
551 testStep->assertMessage());
552 for (int i = 0; i < referenceRecord->fPathHeap->count(); ++i) {
553 REPORTER_ASSERT_MESSAGE(reporter,
554 (*referenceRecord->fPathHeap)[i] ==
555 (*testRecord->fPathHeap)[i], testStep->assertMessage());
556 }
557 }
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000558 */
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000559
560 }
561
562public:
563
564 static void TestPictureSerializationRoundTrip(skiatest::Reporter* reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000565 CanvasTestStep* testStep,
566 uint32_t recordFlags) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000567 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
568 SkPicture referencePicture;
junov@chromium.org4866cc02012-06-01 21:23:07 +0000569 testStep->draw(referencePicture.beginRecording(kWidth, kHeight,
570 recordFlags), reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000571 SkPicture initialPicture;
junov@chromium.org4866cc02012-06-01 21:23:07 +0000572 testStep->draw(initialPicture.beginRecording(kWidth, kHeight,
573 recordFlags), reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000574 testStep->setAssertMessageFormat(kPictureReDrawAssertMessageFormat);
575 SkPicture roundTripPicture;
junov@chromium.org4866cc02012-06-01 21:23:07 +0000576 initialPicture.draw(roundTripPicture.beginRecording(kWidth, kHeight,
577 recordFlags));
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000578
579 SkPictureRecord* referenceRecord = static_cast<SkPictureRecord*>(
580 referencePicture.getRecordingCanvas());
581 SkPictureRecord* roundTripRecord = static_cast<SkPictureRecord*>(
582 roundTripPicture.getRecordingCanvas());
583
584 testStep->setAssertMessageFormat(kPictureReDrawAssertMessageFormat);
585
586 // Verify that deserialization-serialization round trip conserves all
587 // data by comparing referenceRecord to roundTripRecord
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000588 REPORTER_ASSERT_MESSAGE(reporter, referenceRecord->fBitmaps.count() ==
589 roundTripRecord->fBitmaps.count(), testStep->assertMessage());
590 REPORTER_ASSERT_MESSAGE(reporter, referenceRecord->fMatrices.count() ==
591 roundTripRecord->fMatrices.count(), testStep->assertMessage());
592 REPORTER_ASSERT_MESSAGE(reporter, referenceRecord->fPaints.count() ==
593 roundTripRecord->fPaints.count(), testStep->assertMessage());
594 REPORTER_ASSERT_MESSAGE(reporter, referenceRecord->fRegions.count() ==
595 roundTripRecord->fRegions.count(), testStep->assertMessage());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000596 char referenceBuffer[kMaxPictureBufferSize];
597 SkMemoryWStream referenceStream(referenceBuffer,
598 kMaxPictureBufferSize);
599 referenceRecord->fWriter.writeToStream(&referenceStream);
600 char roundTripBuffer[kMaxPictureBufferSize];
601 SkMemoryWStream roundTripStream(roundTripBuffer,
602 kMaxPictureBufferSize);
603 roundTripRecord->fWriter.writeToStream(&roundTripStream);
604 REPORTER_ASSERT_MESSAGE(reporter,
605 roundTripStream.bytesWritten() == referenceStream.bytesWritten(),
606 testStep->assertMessage());
607 REPORTER_ASSERT_MESSAGE(reporter, 0 == memcmp(referenceBuffer,
608 roundTripBuffer, roundTripStream.bytesWritten()),
609 testStep->assertMessage());
610 REPORTER_ASSERT_MESSAGE(reporter, referenceRecord->fRecordFlags ==
611 roundTripRecord->fRecordFlags, testStep->assertMessage());
612 REPORTER_ASSERT_MESSAGE(reporter,
613 referenceRecord->fRestoreOffsetStack ==
614 roundTripRecord->fRestoreOffsetStack,
615 testStep->assertMessage());
616 AssertFlattenedObjectsEqual(referenceRecord, roundTripRecord,
617 reporter, testStep);
618 AssertCanvasStatesEqual(reporter, referenceRecord, roundTripRecord,
619 testStep);
620 }
621
622 static void TestPictureFlattenedObjectReuse(skiatest::Reporter* reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000623 CanvasTestStep* testStep,
624 uint32_t recordFlags) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000625 // Verify that when a test step is executed twice, no extra resources
626 // are flattened during the second execution
627 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
628 SkPicture referencePicture;
629 SkCanvas* referenceCanvas = referencePicture.beginRecording(kWidth,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000630 kHeight, recordFlags);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000631 testStep->draw(referenceCanvas, reporter);
632 SkPicture testPicture;
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000633 SkCanvas* testCanvas = testPicture.beginRecording(kWidth,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000634 kHeight, recordFlags);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000635 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000636 testStep->setAssertMessageFormat(kPictureSecondDrawAssertMessageFormat);
junov@chromium.orgdadcfdc2012-02-23 14:59:22 +0000637 testStep->draw(testCanvas, reporter);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000638
639 SkPictureRecord* referenceRecord = static_cast<SkPictureRecord*>(
640 referenceCanvas);
641 SkPictureRecord* testRecord = static_cast<SkPictureRecord*>(
642 testCanvas);
643 testStep->setAssertMessageFormat(kPictureResourceReuseMessageFormat);
644 AssertFlattenedObjectsEqual(referenceRecord, testRecord,
junov@chromium.org76b9c4b2012-02-22 21:24:41 +0000645 reporter, testStep);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000646 }
647};
648
649static void TestPictureStateConsistency(skiatest::Reporter* reporter,
650 CanvasTestStep* testStep,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000651 const SkCanvas& referenceCanvas,
652 uint32_t recordFlags) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000653 // Verify that the recording canvas's state is consistent
654 // with that of a regular canvas
655 SkPicture testPicture;
junov@chromium.org4866cc02012-06-01 21:23:07 +0000656 SkCanvas* pictureCanvas = testPicture.beginRecording(kWidth, kHeight,
657 recordFlags);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000658 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
659 testStep->draw(pictureCanvas, reporter);
660 testStep->setAssertMessageFormat(kPictureRecoringAssertMessageFormat);
661 AssertCanvasStatesEqual(reporter, pictureCanvas, &referenceCanvas,
662 testStep);
663
664 SkBitmap playbackStore;
665 createBitmap(&playbackStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
666 SkDevice playbackDevice(playbackStore);
667 SkCanvas playbackCanvas(&playbackDevice);
668 testPicture.draw(&playbackCanvas);
669 testStep->setAssertMessageFormat(kPicturePlaybackAssertMessageFormat);
670 AssertCanvasStatesEqual(reporter, &playbackCanvas, &referenceCanvas,
671 testStep);
672
673 // The following test code is commented out because SkPicture is not
674 // currently expected to preserve state when restarting recording.
675 /*
junov@chromium.org4866cc02012-06-01 21:23:07 +0000676 SkCanvas* pictureCanvas = testPicture.beginRecording(kWidth, kHeight,
677 recordFlags);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000678 testStep->setAssertMessageFormat(kPictureResumeAssertMessageFormat);
679 AssertCanvasStatesEqual(reporter, pictureCanvas, &referenceCanvas,
680 testStep);
681 */
682}
683
684static void TestDeferredCanvasStateConsistency(
685 skiatest::Reporter* reporter,
686 CanvasTestStep* testStep,
687 const SkCanvas& referenceCanvas) {
688
689 SkBitmap deferredStore;
690 createBitmap(&deferredStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
691 SkDevice deferredDevice(deferredStore);
692 SkDeferredCanvas deferredCanvas(&deferredDevice);
693 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
694 testStep->draw(&deferredCanvas, reporter);
695 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
696 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
697 testStep);
698
699 // Verified that deferred canvas state is not affected by flushing
700 // pending draw operations
701
702 // The following test code is commented out because it currently fails.
703 // Issue: http://code.google.com/p/skia/issues/detail?id=496
704 /*
705 deferredCanvas.flush();
706 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
707 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
708 testStep);
709 */
710}
711
712static void TestProxyCanvasStateConsistency(
713 skiatest::Reporter* reporter,
714 CanvasTestStep* testStep,
715 const SkCanvas& referenceCanvas) {
716
717 SkBitmap indirectStore;
718 createBitmap(&indirectStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
719 SkDevice indirectDevice(indirectStore);
720 SkCanvas indirectCanvas(&indirectDevice);
721 SkProxyCanvas proxyCanvas(&indirectCanvas);
722 testStep->setAssertMessageFormat(kProxyDrawAssertMessageFormat);
723 testStep->draw(&proxyCanvas, reporter);
724 // Verify that the SkProxyCanvas reports consitent state
725 testStep->setAssertMessageFormat(kProxyStateAssertMessageFormat);
726 AssertCanvasStatesEqual(reporter, &proxyCanvas, &referenceCanvas,
727 testStep);
728 // Verify that the indirect canvas reports consitent state
729 testStep->setAssertMessageFormat(kProxyIndirectStateAssertMessageFormat);
730 AssertCanvasStatesEqual(reporter, &indirectCanvas, &referenceCanvas,
731 testStep);
732}
733
734static void TestNWayCanvasStateConsistency(
735 skiatest::Reporter* reporter,
736 CanvasTestStep* testStep,
737 const SkCanvas& referenceCanvas) {
738
739 SkBitmap indirectStore1;
740 createBitmap(&indirectStore1, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
741 SkDevice indirectDevice1(indirectStore1);
742 SkCanvas indirectCanvas1(&indirectDevice1);
743
744 SkBitmap indirectStore2;
745 createBitmap(&indirectStore2, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
746 SkDevice indirectDevice2(indirectStore2);
747 SkCanvas indirectCanvas2(&indirectDevice2);
748
djsollen@google.comf0a062b2012-05-01 16:50:25 +0000749 SkISize canvasSize = referenceCanvas.getDeviceSize();
750 SkNWayCanvas nWayCanvas(canvasSize.width(), canvasSize.height());
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000751 nWayCanvas.addCanvas(&indirectCanvas1);
752 nWayCanvas.addCanvas(&indirectCanvas2);
753
754 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
755 testStep->draw(&nWayCanvas, reporter);
756 // Verify that the SkProxyCanvas reports consitent state
757 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
758 AssertCanvasStatesEqual(reporter, &nWayCanvas, &referenceCanvas,
759 testStep);
760 // Verify that the indirect canvases report consitent state
761 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
762 AssertCanvasStatesEqual(reporter, &indirectCanvas1, &referenceCanvas,
763 testStep);
764 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
765 AssertCanvasStatesEqual(reporter, &indirectCanvas2, &referenceCanvas,
766 testStep);
767}
768
769/*
770 * This sub-test verifies that the test step passes when executed
771 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
772 * that the all canvas derivatives report the same state as an SkCanvas
773 * after having executed the test step.
774 */
775static void TestOverrideStateConsistency(skiatest::Reporter* reporter,
776 CanvasTestStep* testStep) {
777 SkBitmap referenceStore;
778 createBitmap(&referenceStore, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
779 SkDevice referenceDevice(referenceStore);
780 SkCanvas referenceCanvas(&referenceDevice);
781 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
782 testStep->draw(&referenceCanvas, reporter);
783
junov@chromium.org4866cc02012-06-01 21:23:07 +0000784 TestPictureStateConsistency(reporter, testStep, referenceCanvas, 0);
785 TestPictureStateConsistency(reporter, testStep, referenceCanvas,
786 SkPicture::kFlattenMutableNonTexturePixelRefs_RecordingFlag);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000787 TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas);
788
789 // The following test code is commented out because SkProxyCanvas is
790 // missing a lot of virtual overrides on get* methods, which are used
791 // to verify canvas state.
792 // Issue: http://code.google.com/p/skia/issues/detail?id=500
793
794 //TestProxyCanvasStateConsistency(reporter, testStep, referenceCanvas);
795
796 // The following test code is commented out because SkNWayCanvas does not
797 // report correct clipping and device bounds information
798 // Issue: http://code.google.com/p/skia/issues/detail?id=501
799
800 //TestNWayCanvasStateConsistency(reporter, testStep, referenceCanvas);
reed@google.com7c202932011-12-14 18:48:05 +0000801}
reed@google.com37f3ae02011-11-28 16:06:04 +0000802
803static void TestCanvas(skiatest::Reporter* reporter) {
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000804 // Init global here because bitmap pixels cannot be alocated during
805 // static initialization
806 kTestBitmap = testBitmap();
reed@google.com37f3ae02011-11-28 16:06:04 +0000807
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000808 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
809 TestOverrideStateConsistency(reporter, testStepArray()[testStep]);
810 SkPictureTester::TestPictureSerializationRoundTrip(reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000811 testStepArray()[testStep], 0);
812 SkPictureTester::TestPictureSerializationRoundTrip(reporter,
813 testStepArray()[testStep],
814 SkPicture::kFlattenMutableNonTexturePixelRefs_RecordingFlag);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000815 SkPictureTester::TestPictureFlattenedObjectReuse(reporter,
junov@chromium.org4866cc02012-06-01 21:23:07 +0000816 testStepArray()[testStep], 0);
817 SkPictureTester::TestPictureFlattenedObjectReuse(reporter,
818 testStepArray()[testStep],
819 SkPicture::kFlattenMutableNonTexturePixelRefs_RecordingFlag);
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000820 }
reed@google.com37f3ae02011-11-28 16:06:04 +0000821}
822
823#include "TestClassDef.h"
824DEFINE_TESTCLASS("Canvas", TestCanvasClass, TestCanvas)