2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 * This test defines a series of elementatry test steps that perform
10 * a single or a small group of canvas API calls. Each test step is
11 * used in several test cases that verify that different types of SkCanvas
12 * flavors and derivatives pass it and yield consistent behavior. The
13 * test cases analyse results that are queryable through the API. They do
14 * not look at rendering results.
17 * The general pattern for creating a new test step is to write a test
18 * function of the form:
20 * static void MyTestStepFunction(SkCanvas* canvas,
21 * skiatest::Reporter* reporter,
22 * CanvasTestStep* testStep)
24 * canvas->someCanvasAPImethod();
26 * REPORTER_ASSERT_MESSAGE(reporter, (...), \
27 * testStep->assertMessage());
30 * The definition of the test step function should be followed by an
31 * invocation of the TEST_STEP macro, which generates a class and
32 * instance for the test step:
34 * TEST_STEP(MyTestStep, MyTestStepFunction)
36 * There are also short hand macros for defining simple test steps
37 * in a single line of code. A simple test step is a one that is made
38 * of a single canvas API call.
40 * SIMPLE_TEST_STEP(MytestStep, someCanvasAPIMethod());
42 * There is another macro called SIMPLE_TEST_STEP_WITH_ASSERT that
43 * works the same way as SIMPLE_TEST_STEP, and additionally verifies
44 * that the invoked method returns a non-zero value.
48 #include "SkDeferredCanvas.h"
51 #include "SkNWayCanvas.h"
52 #include "SkPDFDevice.h"
53 #include "SkPDFDocument.h"
56 #include "SkPicture.h"
57 #include "SkPictureRecord.h"
58 #include "SkPictureRecorder.h"
59 #include "SkProxyCanvas.h"
64 #include "SkSurface.h"
65 #include "SkTDArray.h"
68 static bool equal_clips(const SkCanvas& a, const SkCanvas& b) {
69 if (a.isClipEmpty()) {
70 return b.isClipEmpty();
72 if (!a.isClipRect()) {
73 // this is liberally true, since we don't expose a way to know this exactly (for non-rects)
74 return !b.isClipRect();
77 a.getClipDeviceBounds(&ar);
78 b.getClipDeviceBounds(&br);
82 class Canvas2CanvasClipVisitor : public SkCanvas::ClipVisitor {
84 Canvas2CanvasClipVisitor(SkCanvas* target) : fTarget(target) {}
86 virtual void clipRect(const SkRect& r, SkRegion::Op op, bool aa) SK_OVERRIDE {
87 fTarget->clipRect(r, op, aa);
89 virtual void clipRRect(const SkRRect& r, SkRegion::Op op, bool aa) SK_OVERRIDE {
90 fTarget->clipRRect(r, op, aa);
92 virtual void clipPath(const SkPath& p, SkRegion::Op op, bool aa) SK_OVERRIDE {
93 fTarget->clipPath(p, op, aa);
100 static void test_clipVisitor(skiatest::Reporter* reporter, SkCanvas* canvas) {
101 SkISize size = canvas->getDeviceSize();
104 bm.setInfo(SkImageInfo::MakeN32Premul(size.width(), size.height()));
107 Canvas2CanvasClipVisitor visitor(&c);
108 canvas->replayClips(&visitor);
110 REPORTER_ASSERT(reporter, equal_clips(c, *canvas));
113 static const int kWidth = 2;
114 static const int kHeight = 2;
116 // Format strings that describe the test context. The %s token is where
117 // the name of the test step is inserted. The context is required for
118 // disambiguating the error in the case of failures that are reported in
119 // functions that are called multiple times in different contexts (test
120 // cases and test steps).
121 static const char* const kDefaultAssertMessageFormat = "%s";
122 static const char* const kCanvasDrawAssertMessageFormat =
123 "Drawing test step %s with SkCanvas";
124 static const char* const kPictureDrawAssertMessageFormat =
125 "Drawing test step %s with SkPicture";
126 static const char* const kPictureSecondDrawAssertMessageFormat =
127 "Duplicate draw of test step %s with SkPicture";
128 static const char* const kDeferredDrawAssertMessageFormat =
129 "Drawing test step %s with SkDeferredCanvas";
130 static const char* const kProxyDrawAssertMessageFormat =
131 "Drawing test step %s with SkProxyCanvas";
132 static const char* const kNWayDrawAssertMessageFormat =
133 "Drawing test step %s with SkNWayCanvas";
134 static const char* const kDeferredPreFlushAssertMessageFormat =
135 "test step %s, SkDeferredCanvas state consistency before flush";
136 static const char* const kDeferredPostFlushPlaybackAssertMessageFormat =
137 "test step %s, SkDeferredCanvas playback canvas state consistency after flush";
138 static const char* const kDeferredPostSilentFlushPlaybackAssertMessageFormat =
139 "test step %s, SkDeferredCanvas playback canvas state consistency after silent flush";
140 static const char* const kPictureResourceReuseMessageFormat =
141 "test step %s, SkPicture duplicate flattened object test";
142 static const char* const kProxyStateAssertMessageFormat =
143 "test step %s, SkProxyCanvas state consistency";
144 static const char* const kProxyIndirectStateAssertMessageFormat =
145 "test step %s, SkProxyCanvas indirect canvas state consistency";
146 static const char* const kNWayStateAssertMessageFormat =
147 "test step %s, SkNWayCanvas state consistency";
148 static const char* const kNWayIndirect1StateAssertMessageFormat =
149 "test step %s, SkNWayCanvas indirect canvas 1 state consistency";
150 static const char* const kNWayIndirect2StateAssertMessageFormat =
151 "test step %s, SkNWayCanvas indirect canvas 2 state consistency";
152 static const char* const kPdfAssertMessageFormat =
153 "PDF sanity check failed %s";
155 static void createBitmap(SkBitmap* bm, SkColor color) {
156 bm->allocN32Pixels(kWidth, kHeight);
157 bm->eraseColor(color);
160 static SkSurface* createSurface(SkColor color) {
161 SkSurface* surface = SkSurface::NewRasterPMColor(kWidth, kHeight);
162 surface->getCanvas()->clear(color);
166 class CanvasTestStep;
167 static SkTDArray<CanvasTestStep*>& testStepArray() {
168 static SkTDArray<CanvasTestStep*> theTests;
172 class CanvasTestStep {
174 CanvasTestStep(bool fEnablePdfTesting = true) {
175 *testStepArray().append() = this;
176 fAssertMessageFormat = kDefaultAssertMessageFormat;
177 this->fEnablePdfTesting = fEnablePdfTesting;
179 virtual ~CanvasTestStep() { }
181 virtual void draw(SkCanvas*, skiatest::Reporter*) = 0;
182 virtual const char* name() const = 0;
184 const char* assertMessage() {
185 fAssertMessage.printf(fAssertMessageFormat, name());
186 return fAssertMessage.c_str();
189 void setAssertMessageFormat(const char* format) {
190 fAssertMessageFormat = format;
193 bool enablePdfTesting() { return fEnablePdfTesting; }
196 SkString fAssertMessage;
197 const char* fAssertMessageFormat;
198 bool fEnablePdfTesting;
201 ///////////////////////////////////////////////////////////////////////////////
202 // Constants used by test steps
204 const SkRect kTestRect =
205 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
206 SkIntToScalar(2), SkIntToScalar(1));
207 static SkMatrix testMatrix() {
210 matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
213 const SkMatrix kTestMatrix = testMatrix();
214 static SkPath test_path() {
216 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
217 SkIntToScalar(2), SkIntToScalar(1)));
220 const SkPath kTestPath = test_path();
221 static SkPath test_nearly_zero_length_path() {
223 SkPoint pt1 = { 0, 0 };
224 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
225 SkPoint pt3 = { SkIntToScalar(1), 0 };
226 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
233 const SkPath kNearlyZeroLengthPath = test_nearly_zero_length_path();
234 static SkRegion testRegion() {
236 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
237 region.setRect(rect);
240 const SkIRect kTestIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
241 const SkRegion kTestRegion = testRegion();
242 const SkColor kTestColor = 0x01020304;
243 const SkPaint kTestPaint;
244 const SkPoint kTestPoints[3] = {
245 {SkIntToScalar(0), SkIntToScalar(0)},
246 {SkIntToScalar(2), SkIntToScalar(1)},
247 {SkIntToScalar(0), SkIntToScalar(2)}
249 const size_t kTestPointCount = 3;
250 static SkBitmap testBitmap() {
252 createBitmap(&bitmap, 0x05060708);
255 SkBitmap kTestBitmap; // cannot be created during static init
256 SkString kTestText("Hello World");
257 SkPoint kTestPoints2[] = {
258 { SkIntToScalar(0), SkIntToScalar(1) },
259 { SkIntToScalar(1), SkIntToScalar(1) },
260 { SkIntToScalar(2), SkIntToScalar(1) },
261 { SkIntToScalar(3), SkIntToScalar(1) },
262 { SkIntToScalar(4), SkIntToScalar(1) },
263 { SkIntToScalar(5), SkIntToScalar(1) },
264 { SkIntToScalar(6), SkIntToScalar(1) },
265 { SkIntToScalar(7), SkIntToScalar(1) },
266 { SkIntToScalar(8), SkIntToScalar(1) },
267 { SkIntToScalar(9), SkIntToScalar(1) },
268 { SkIntToScalar(10), SkIntToScalar(1) },
272 ///////////////////////////////////////////////////////////////////////////////
273 // Macros for defining test steps
275 #define TEST_STEP(NAME, FUNCTION) \
276 class NAME##_TestStep : public CanvasTestStep{ \
278 virtual void draw(SkCanvas* canvas, skiatest::Reporter* reporter) { \
279 FUNCTION (canvas, reporter, this); \
281 virtual const char* name() const {return #NAME ;} \
283 static NAME##_TestStep NAME##_TestStepInstance;
285 #define TEST_STEP_NO_PDF(NAME, FUNCTION) \
286 class NAME##_TestStep : public CanvasTestStep{ \
288 NAME##_TestStep() : CanvasTestStep(false) {} \
289 virtual void draw(SkCanvas* canvas, skiatest::Reporter* reporter) { \
290 FUNCTION (canvas, reporter, this); \
292 virtual const char* name() const {return #NAME ;} \
294 static NAME##_TestStep NAME##_TestStepInstance;
296 #define SIMPLE_TEST_STEP(NAME, CALL) \
297 static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter*, \
301 TEST_STEP(NAME, NAME##TestStep )
303 #define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
304 static void NAME##TestStep(SkCanvas* canvas, skiatest::Reporter* reporter, \
305 CanvasTestStep* testStep) { \
306 REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \
307 testStep->assertMessage()); \
309 TEST_STEP(NAME, NAME##TestStep )
312 ///////////////////////////////////////////////////////////////////////////////
313 // Basic test steps for most virtual methods in SkCanvas that draw or affect
314 // the state of the canvas.
316 SIMPLE_TEST_STEP(Translate, translate(SkIntToScalar(1), SkIntToScalar(2)));
317 SIMPLE_TEST_STEP(Scale, scale(SkIntToScalar(1), SkIntToScalar(2)));
318 SIMPLE_TEST_STEP(Rotate, rotate(SkIntToScalar(1)));
319 SIMPLE_TEST_STEP(Skew, skew(SkIntToScalar(1), SkIntToScalar(2)));
320 SIMPLE_TEST_STEP(Concat, concat(kTestMatrix));
321 SIMPLE_TEST_STEP(SetMatrix, setMatrix(kTestMatrix));
322 SIMPLE_TEST_STEP(ClipRect, clipRect(kTestRect));
323 SIMPLE_TEST_STEP(ClipPath, clipPath(kTestPath));
324 SIMPLE_TEST_STEP(ClipRegion,
325 clipRegion(kTestRegion, SkRegion::kReplace_Op));
326 SIMPLE_TEST_STEP(Clear, clear(kTestColor));
327 SIMPLE_TEST_STEP(DrawPaint, drawPaint(kTestPaint));
328 SIMPLE_TEST_STEP(DrawPointsPoints, drawPoints(SkCanvas::kPoints_PointMode,
329 kTestPointCount, kTestPoints, kTestPaint));
330 SIMPLE_TEST_STEP(DrawPointsLiness, drawPoints(SkCanvas::kLines_PointMode,
331 kTestPointCount, kTestPoints, kTestPaint));
332 SIMPLE_TEST_STEP(DrawPointsPolygon, drawPoints(SkCanvas::kPolygon_PointMode,
333 kTestPointCount, kTestPoints, kTestPaint));
334 SIMPLE_TEST_STEP(DrawRect, drawRect(kTestRect, kTestPaint));
335 SIMPLE_TEST_STEP(DrawPath, drawPath(kTestPath, kTestPaint));
336 SIMPLE_TEST_STEP(DrawBitmap, drawBitmap(kTestBitmap, 0, 0));
337 SIMPLE_TEST_STEP(DrawBitmapPaint, drawBitmap(kTestBitmap, 0, 0, &kTestPaint));
338 SIMPLE_TEST_STEP(DrawBitmapRect, drawBitmapRect(kTestBitmap, NULL, kTestRect,
340 SIMPLE_TEST_STEP(DrawBitmapRectSrcRect, drawBitmapRect(kTestBitmap,
341 &kTestIRect, kTestRect, NULL));
342 SIMPLE_TEST_STEP(DrawBitmapRectPaint, drawBitmapRect(kTestBitmap, NULL,
343 kTestRect, &kTestPaint));
344 SIMPLE_TEST_STEP(DrawBitmapMatrix, drawBitmapMatrix(kTestBitmap, kTestMatrix,
346 SIMPLE_TEST_STEP(DrawBitmapMatrixPaint, drawBitmapMatrix(kTestBitmap,
347 kTestMatrix, &kTestPaint));
348 SIMPLE_TEST_STEP(DrawBitmapNine, drawBitmapNine(kTestBitmap, kTestIRect,
350 SIMPLE_TEST_STEP(DrawBitmapNinePaint, drawBitmapNine(kTestBitmap, kTestIRect,
351 kTestRect, &kTestPaint));
352 SIMPLE_TEST_STEP(DrawSprite, drawSprite(kTestBitmap, 0, 0, NULL));
353 SIMPLE_TEST_STEP(DrawSpritePaint, drawSprite(kTestBitmap, 0, 0, &kTestPaint));
354 SIMPLE_TEST_STEP(DrawText, drawText(kTestText.c_str(), kTestText.size(),
356 SIMPLE_TEST_STEP(DrawPosText, drawPosText(kTestText.c_str(),
357 kTestText.size(), kTestPoints2, kTestPaint));
358 SIMPLE_TEST_STEP(DrawTextOnPath, drawTextOnPath(kTestText.c_str(),
359 kTestText.size(), kTestPath, NULL, kTestPaint));
360 SIMPLE_TEST_STEP(DrawTextOnPathMatrix, drawTextOnPath(kTestText.c_str(),
361 kTestText.size(), kTestPath, &kTestMatrix, kTestPaint));
362 SIMPLE_TEST_STEP(DrawData, drawData(kTestText.c_str(), kTestText.size()));
363 SIMPLE_TEST_STEP(BeginGroup, beginCommentGroup(kTestText.c_str()));
364 SIMPLE_TEST_STEP(AddComment, addComment(kTestText.c_str(), kTestText.c_str()));
365 SIMPLE_TEST_STEP(EndGroup, endCommentGroup());
367 ///////////////////////////////////////////////////////////////////////////////
368 // Complex test steps
370 static void SaveMatrixClipStep(SkCanvas* canvas,
371 skiatest::Reporter* reporter,
372 CanvasTestStep* testStep) {
373 int saveCount = canvas->getSaveCount();
375 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
376 canvas->clipRegion(kTestRegion);
378 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
379 testStep->assertMessage());
380 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
381 testStep->assertMessage());
382 // REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion, testStep->assertMessage());
384 TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
386 static void SaveLayerStep(SkCanvas* canvas,
387 skiatest::Reporter* reporter,
388 CanvasTestStep* testStep) {
389 int saveCount = canvas->getSaveCount();
390 canvas->saveLayer(NULL, NULL);
392 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
393 testStep->assertMessage());
395 TEST_STEP(SaveLayer, SaveLayerStep);
397 static void BoundedSaveLayerStep(SkCanvas* canvas,
398 skiatest::Reporter* reporter,
399 CanvasTestStep* testStep) {
400 int saveCount = canvas->getSaveCount();
401 canvas->saveLayer(&kTestRect, NULL);
403 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
404 testStep->assertMessage());
406 TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
408 static void PaintSaveLayerStep(SkCanvas* canvas,
409 skiatest::Reporter* reporter,
410 CanvasTestStep* testStep) {
411 int saveCount = canvas->getSaveCount();
412 canvas->saveLayer(NULL, &kTestPaint);
414 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
415 testStep->assertMessage());
417 TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
419 static void TwoClipOpsStep(SkCanvas* canvas,
422 // This test exercises a functionality in SkPicture that leads to the
423 // recording of restore offset placeholders. This test will trigger an
424 // assertion at playback time if the placeholders are not properly
425 // filled when the recording ends.
426 canvas->clipRect(kTestRect);
427 canvas->clipRegion(kTestRegion);
429 TEST_STEP(TwoClipOps, TwoClipOpsStep);
431 // exercise fix for http://code.google.com/p/skia/issues/detail?id=560
432 // ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
433 static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas,
437 paint.setStrokeWidth(SkIntToScalar(1));
438 paint.setStyle(SkPaint::kStroke_Style);
440 canvas->drawPath(kNearlyZeroLengthPath, paint);
442 TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
444 static void DrawVerticesShaderTestStep(SkCanvas* canvas,
449 pts[1].set(SkIntToScalar(kWidth), 0);
450 pts[2].set(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
451 pts[3].set(0, SkIntToScalar(kHeight));
453 SkShader* shader = SkShader::CreateBitmapShader(kTestBitmap,
454 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
455 paint.setShader(shader)->unref();
456 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
457 NULL, NULL, NULL, 0, paint);
460 TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep);
462 static void DrawPictureTestStep(SkCanvas* canvas,
465 SkPictureRecorder recorder;
466 SkCanvas* testCanvas = recorder.beginRecording(kWidth, kHeight, NULL, 0);
467 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
468 testCanvas->clipRect(kTestRect);
469 testCanvas->drawRect(kTestRect, kTestPaint);
470 SkAutoTUnref<SkPicture> testPicture(recorder.endRecording());
472 canvas->drawPicture(testPicture);
474 TEST_STEP(DrawPicture, DrawPictureTestStep);
476 static void SaveRestoreTestStep(SkCanvas* canvas,
477 skiatest::Reporter* reporter,
478 CanvasTestStep* testStep) {
479 int baseSaveCount = canvas->getSaveCount();
480 int n = canvas->save();
481 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount == n, testStep->assertMessage());
482 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
483 testStep->assertMessage());
486 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
487 testStep->assertMessage());
488 canvas->restoreToCount(baseSaveCount + 1);
489 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
490 testStep->assertMessage());
492 // should this pin to 1, or be a no-op, or crash?
493 canvas->restoreToCount(0);
494 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
495 testStep->assertMessage());
497 TEST_STEP(SaveRestore, SaveRestoreTestStep);
499 static void DrawLayerTestStep(SkCanvas* canvas,
500 skiatest::Reporter* reporter,
501 CanvasTestStep* testStep) {
502 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
503 testStep->assertMessage());
505 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
506 testStep->assertMessage());
509 const SkRect* bounds = NULL; // null means include entire bounds
510 const SkPaint* paint = NULL;
512 canvas->saveLayer(bounds, paint);
513 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
514 testStep->assertMessage());
516 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
517 testStep->assertMessage());
519 canvas->saveLayer(bounds, paint);
520 canvas->saveLayer(bounds, paint);
521 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
522 testStep->assertMessage());
524 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
525 testStep->assertMessage());
527 // now layer count should be 0
528 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
529 testStep->assertMessage());
531 TEST_STEP(DrawLayer, DrawLayerTestStep);
533 static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas,
536 // This test step challenges the TestDeferredCanvasStateConsistency
537 // test cases because the opaque paint can trigger an optimization
538 // that discards previously recorded commands. The challenge is to maintain
539 // correct clip and matrix stack state.
540 canvas->resetMatrix();
541 canvas->rotate(SkIntToScalar(30));
543 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
545 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
547 paint.setColor(0xFFFFFFFF);
548 canvas->drawPaint(paint);
552 TEST_STEP(NestedSaveRestoreWithSolidPaint, \
553 NestedSaveRestoreWithSolidPaintTestStep);
555 static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas,
558 // This test step challenges the TestDeferredCanvasStateConsistency
559 // test case because the canvas flush on a deferred canvas will
560 // reset the recording session. The challenge is to maintain correct
561 // clip and matrix stack state on the playback canvas.
562 canvas->resetMatrix();
563 canvas->rotate(SkIntToScalar(30));
565 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
567 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
568 canvas->drawRect(kTestRect,kTestPaint);
573 TEST_STEP(NestedSaveRestoreWithFlush, \
574 NestedSaveRestoreWithFlushTestStep);
576 static void AssertCanvasStatesEqual(skiatest::Reporter* reporter,
577 const SkCanvas* canvas1,
578 const SkCanvas* canvas2,
579 CanvasTestStep* testStep) {
580 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
581 canvas2->getDeviceSize(), testStep->assertMessage());
582 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
583 canvas2->getSaveCount(), testStep->assertMessage());
584 REPORTER_ASSERT_MESSAGE(reporter, canvas1->isDrawingToLayer() ==
585 canvas2->isDrawingToLayer(), testStep->assertMessage());
587 SkRect bounds1, bounds2;
588 REPORTER_ASSERT_MESSAGE(reporter,
589 canvas1->getClipBounds(&bounds1) == canvas2->getClipBounds(&bounds2),
590 testStep->assertMessage());
591 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
592 testStep->assertMessage());
594 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
595 canvas2->getDrawFilter(), testStep->assertMessage());
596 SkIRect deviceBounds1, deviceBounds2;
597 REPORTER_ASSERT_MESSAGE(reporter,
598 canvas1->getClipDeviceBounds(&deviceBounds1) ==
599 canvas2->getClipDeviceBounds(&deviceBounds2),
600 testStep->assertMessage());
601 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2, testStep->assertMessage());
602 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
603 canvas2->getTotalMatrix(), testStep->assertMessage());
604 REPORTER_ASSERT_MESSAGE(reporter, equal_clips(*canvas1, *canvas2), testStep->assertMessage());
606 // The following test code is commented out because the test fails when
607 // the canvas is an SkPictureRecord or SkDeferredCanvas
608 // Issue: http://code.google.com/p/skia/issues/detail?id=498
609 // Also, creating a LayerIter on an SkProxyCanvas crashes
610 // Issue: http://code.google.com/p/skia/issues/detail?id=499
612 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
613 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
614 while (!layerIter1.done() && !layerIter2.done()) {
615 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
616 layerIter2.matrix(), testStep->assertMessage());
617 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
618 layerIter2.clip(), testStep->assertMessage());
619 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
620 layerIter2.paint(), testStep->assertMessage());
621 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
622 layerIter2.x(), testStep->assertMessage());
623 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
624 layerIter2.y(), testStep->assertMessage());
628 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
629 testStep->assertMessage());
630 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
631 testStep->assertMessage());
635 // The following class groups static functions that need to access
636 // the privates members of SkPictureRecord
637 class SkPictureTester {
639 static int EQ(const SkFlatData* a, const SkFlatData* b) {
643 static void AssertFlattenedObjectsEqual(
644 SkPictureRecord* referenceRecord,
645 SkPictureRecord* testRecord,
646 skiatest::Reporter* reporter,
647 CanvasTestStep* testStep) {
649 REPORTER_ASSERT_MESSAGE(reporter,
650 referenceRecord->fBitmapHeap->count() ==
651 testRecord->fBitmapHeap->count(), testStep->assertMessage());
652 REPORTER_ASSERT_MESSAGE(reporter,
653 referenceRecord->fPaints.count() ==
654 testRecord->fPaints.count(), testStep->assertMessage());
655 for (int i = 0; i < referenceRecord->fPaints.count(); ++i) {
656 REPORTER_ASSERT_MESSAGE(reporter,
657 EQ(referenceRecord->fPaints[i], testRecord->fPaints[i]),
658 testStep->assertMessage());
660 REPORTER_ASSERT_MESSAGE(reporter,
661 !referenceRecord->fPathHeap == !testRecord->fPathHeap,
662 testStep->assertMessage());
663 // The following tests are commented out because they currently
664 // fail. Issue: http://code.google.com/p/skia/issues/detail?id=507
666 if (referenceRecord->fPathHeap) {
667 REPORTER_ASSERT_MESSAGE(reporter,
668 referenceRecord->fPathHeap->count() ==
669 testRecord->fPathHeap->count(),
670 testStep->assertMessage());
671 for (int i = 0; i < referenceRecord->fPathHeap->count(); ++i) {
672 REPORTER_ASSERT_MESSAGE(reporter,
673 (*referenceRecord->fPathHeap)[i] ==
674 (*testRecord->fPathHeap)[i], testStep->assertMessage());
683 static void TestPictureFlattenedObjectReuse(skiatest::Reporter* reporter,
684 CanvasTestStep* testStep,
685 uint32_t recordFlags) {
686 // Verify that when a test step is executed twice, no extra resources
687 // are flattened during the second execution
688 testStep->setAssertMessageFormat(kPictureDrawAssertMessageFormat);
689 SkPictureRecorder referenceRecorder;
690 SkCanvas* referenceCanvas = referenceRecorder.beginRecording(kWidth, kHeight,
692 testStep->draw(referenceCanvas, reporter);
694 SkPictureRecorder testRecorder;
695 SkCanvas* testCanvas = testRecorder.beginRecording(kWidth, kHeight,
697 testStep->draw(testCanvas, reporter);
698 testStep->setAssertMessageFormat(kPictureSecondDrawAssertMessageFormat);
699 testStep->draw(testCanvas, reporter);
701 SkPictureRecord* referenceRecord = static_cast<SkPictureRecord*>(referenceCanvas);
702 SkPictureRecord* testRecord = static_cast<SkPictureRecord*>(testCanvas);
703 testStep->setAssertMessageFormat(kPictureResourceReuseMessageFormat);
704 AssertFlattenedObjectsEqual(referenceRecord, testRecord,
709 static void TestPdfDevice(skiatest::Reporter* reporter,
710 CanvasTestStep* testStep) {
711 SkISize pageSize = SkISize::Make(kWidth, kHeight);
712 SkPDFDevice device(pageSize, pageSize, SkMatrix::I());
713 SkCanvas canvas(&device);
714 testStep->setAssertMessageFormat(kPdfAssertMessageFormat);
715 testStep->draw(&canvas, reporter);
717 doc.appendPage(&device);
718 SkDynamicMemoryWStream stream;
719 doc.emitPDF(&stream);
722 // The following class groups static functions that need to access
723 // the privates members of SkDeferredCanvas
724 class SkDeferredCanvasTester {
726 static void TestDeferredCanvasStateConsistency(
727 skiatest::Reporter* reporter,
728 CanvasTestStep* testStep,
729 const SkCanvas& referenceCanvas, bool silent) {
731 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
732 SkAutoTUnref<SkDeferredCanvas> deferredCanvas(SkDeferredCanvas::Create(surface.get()));
734 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
735 testStep->draw(deferredCanvas, reporter);
736 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
737 AssertCanvasStatesEqual(reporter, deferredCanvas, &referenceCanvas,
741 deferredCanvas->silentFlush();
743 deferredCanvas->flush();
746 testStep->setAssertMessageFormat(
747 silent ? kDeferredPostSilentFlushPlaybackAssertMessageFormat :
748 kDeferredPostFlushPlaybackAssertMessageFormat);
749 AssertCanvasStatesEqual(reporter,
750 deferredCanvas->immediateCanvas(),
751 &referenceCanvas, testStep);
753 // Verified that deferred canvas state is not affected by flushing
754 // pending draw operations
756 // The following test code is commented out because it currently fails.
757 // Issue: http://code.google.com/p/skia/issues/detail?id=496
759 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
760 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
767 static void TestProxyCanvasStateConsistency(
768 skiatest::Reporter* reporter,
769 CanvasTestStep* testStep,
770 const SkCanvas& referenceCanvas) {
772 SkBitmap indirectStore;
773 createBitmap(&indirectStore, 0xFFFFFFFF);
774 SkCanvas indirectCanvas(indirectStore);
775 SkProxyCanvas proxyCanvas(&indirectCanvas);
776 testStep->setAssertMessageFormat(kProxyDrawAssertMessageFormat);
777 testStep->draw(&proxyCanvas, reporter);
778 // Verify that the SkProxyCanvas reports consitent state
779 testStep->setAssertMessageFormat(kProxyStateAssertMessageFormat);
780 AssertCanvasStatesEqual(reporter, &proxyCanvas, &referenceCanvas,
782 // Verify that the indirect canvas reports consitent state
783 testStep->setAssertMessageFormat(kProxyIndirectStateAssertMessageFormat);
784 AssertCanvasStatesEqual(reporter, &indirectCanvas, &referenceCanvas,
789 static void TestNWayCanvasStateConsistency(
790 skiatest::Reporter* reporter,
791 CanvasTestStep* testStep,
792 const SkCanvas& referenceCanvas) {
794 SkBitmap indirectStore1;
795 createBitmap(&indirectStore1, 0xFFFFFFFF);
796 SkCanvas indirectCanvas1(indirectStore1);
798 SkBitmap indirectStore2;
799 createBitmap(&indirectStore2, 0xFFFFFFFF);
800 SkCanvas indirectCanvas2(indirectStore2);
802 SkISize canvasSize = referenceCanvas.getDeviceSize();
803 SkNWayCanvas nWayCanvas(canvasSize.width(), canvasSize.height());
804 nWayCanvas.addCanvas(&indirectCanvas1);
805 nWayCanvas.addCanvas(&indirectCanvas2);
807 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
808 testStep->draw(&nWayCanvas, reporter);
809 // Verify that the SkProxyCanvas reports consitent state
810 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
811 AssertCanvasStatesEqual(reporter, &nWayCanvas, &referenceCanvas,
813 // Verify that the indirect canvases report consitent state
814 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
815 AssertCanvasStatesEqual(reporter, &indirectCanvas1, &referenceCanvas,
817 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
818 AssertCanvasStatesEqual(reporter, &indirectCanvas2, &referenceCanvas,
823 * This sub-test verifies that the test step passes when executed
824 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
825 * that the all canvas derivatives report the same state as an SkCanvas
826 * after having executed the test step.
828 static void TestOverrideStateConsistency(skiatest::Reporter* reporter,
829 CanvasTestStep* testStep) {
830 SkBitmap referenceStore;
831 createBitmap(&referenceStore, 0xFFFFFFFF);
832 SkCanvas referenceCanvas(referenceStore);
833 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
834 testStep->draw(&referenceCanvas, reporter);
836 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, false);
838 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, testStep, referenceCanvas, true);
840 // The following test code is disabled because SkProxyCanvas is
841 // missing a lot of virtual overrides on get* methods, which are used
842 // to verify canvas state.
843 // Issue: http://code.google.com/p/skia/issues/detail?id=500
845 if (false) { // avoid bit rot, suppress warning
846 TestProxyCanvasStateConsistency(reporter, testStep, referenceCanvas);
849 // The following test code is disabled because SkNWayCanvas does not
850 // report correct clipping and device bounds information
851 // Issue: http://code.google.com/p/skia/issues/detail?id=501
853 if (false) { // avoid bit rot, suppress warning
854 TestNWayCanvasStateConsistency(reporter, testStep, referenceCanvas);
857 if (false) { // avoid bit rot, suppress warning
858 test_clipVisitor(reporter, &referenceCanvas);
862 static void test_newraster(skiatest::Reporter* reporter) {
863 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
864 SkCanvas* canvas = SkCanvas::NewRaster(info);
865 REPORTER_ASSERT(reporter, canvas);
869 const SkPMColor* addr = (const SkPMColor*)canvas->peekPixels(&info2, &rowBytes);
870 REPORTER_ASSERT(reporter, addr);
871 REPORTER_ASSERT(reporter, info == info2);
872 for (int y = 0; y < info.height(); ++y) {
873 for (int x = 0; x < info.width(); ++x) {
874 REPORTER_ASSERT(reporter, 0 == addr[x]);
876 addr = (const SkPMColor*)((const char*)addr + rowBytes);
880 // now try a deliberately bad info
882 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRaster(info));
885 info.fWidth = 1 << 30;
886 info.fHeight = 1 << 30;
887 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRaster(info));
889 // not a valid pixel type
890 info.fWidth = info.fHeight = 10;
891 info.fColorType = kUnknown_SkColorType;
892 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRaster(info));
894 // We should succeed with a zero-sized valid info
895 info = SkImageInfo::MakeN32Premul(0, 0);
896 canvas = SkCanvas::NewRaster(info);
897 REPORTER_ASSERT(reporter, canvas);
901 DEF_TEST(Canvas, reporter) {
902 // Init global here because bitmap pixels cannot be alocated during
903 // static initialization
904 kTestBitmap = testBitmap();
906 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
907 TestOverrideStateConsistency(reporter, testStepArray()[testStep]);
908 SkPictureTester::TestPictureFlattenedObjectReuse(reporter,
909 testStepArray()[testStep], 0);
910 if (testStepArray()[testStep]->enablePdfTesting()) {
911 TestPdfDevice(reporter, testStepArray()[testStep]);
915 // Explicitly call reset(), so we don't leak the pixels (since kTestBitmap is a global)
918 test_newraster(reporter);