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 const int kWidth = 2, kHeight = 2;
70 static void createBitmap(SkBitmap* bm, SkColor color) {
71 bm->allocN32Pixels(kWidth, kHeight);
72 bm->eraseColor(color);
75 static SkSurface* createSurface(SkColor color) {
76 SkSurface* surface = SkSurface::NewRasterPMColor(kWidth, kHeight);
77 surface->getCanvas()->clear(color);
81 ///////////////////////////////////////////////////////////////////////////////
82 // Constants used by test steps
83 const SkPoint kTestPoints[] = {
84 {SkIntToScalar(0), SkIntToScalar(0)},
85 {SkIntToScalar(2), SkIntToScalar(1)},
86 {SkIntToScalar(0), SkIntToScalar(2)}
88 const SkPoint kTestPoints2[] = {
89 { SkIntToScalar(0), SkIntToScalar(1) },
90 { SkIntToScalar(1), SkIntToScalar(1) },
91 { SkIntToScalar(2), SkIntToScalar(1) },
92 { SkIntToScalar(3), SkIntToScalar(1) },
93 { SkIntToScalar(4), SkIntToScalar(1) },
94 { SkIntToScalar(5), SkIntToScalar(1) },
95 { SkIntToScalar(6), SkIntToScalar(1) },
96 { SkIntToScalar(7), SkIntToScalar(1) },
97 { SkIntToScalar(8), SkIntToScalar(1) },
98 { SkIntToScalar(9), SkIntToScalar(1) },
99 { SkIntToScalar(10), SkIntToScalar(1) }
105 : fRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
106 SkIntToScalar(2), SkIntToScalar(1)))
107 , fMatrix(TestMatrix())
109 , fNearlyZeroLengthPath(TestNearlyZeroLengthPath())
110 , fIRect(SkIRect::MakeXYWH(0, 0, 2, 1))
111 , fRegion(TestRegion())
113 , fPoints(kTestPoints)
117 , fText("Hello World")
118 , fPoints2(kTestPoints2)
119 , fBitmap(TestBitmap())
125 SkPath fNearlyZeroLengthPath;
130 const SkPoint* fPoints;
135 const SkPoint* fPoints2;
139 static SkMatrix TestMatrix() {
142 matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
146 static SkPath TestPath() {
148 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
149 SkIntToScalar(2), SkIntToScalar(1)));
152 static SkPath TestNearlyZeroLengthPath() {
154 SkPoint pt1 = { 0, 0 };
155 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
156 SkPoint pt3 = { SkIntToScalar(1), 0 };
157 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
164 static SkRegion TestRegion() {
166 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
167 region.setRect(rect);
170 static SkBitmap TestBitmap() {
172 createBitmap(&bitmap, 0x05060708);
177 static bool equal_clips(const SkCanvas& a, const SkCanvas& b) {
178 if (a.isClipEmpty()) {
179 return b.isClipEmpty();
181 if (!a.isClipRect()) {
182 // this is liberally true, since we don't expose a way to know this exactly (for non-rects)
183 return !b.isClipRect();
186 a.getClipDeviceBounds(&ar);
187 b.getClipDeviceBounds(&br);
191 class Canvas2CanvasClipVisitor : public SkCanvas::ClipVisitor {
193 Canvas2CanvasClipVisitor(SkCanvas* target) : fTarget(target) {}
195 virtual void clipRect(const SkRect& r, SkRegion::Op op, bool aa) SK_OVERRIDE {
196 fTarget->clipRect(r, op, aa);
198 virtual void clipRRect(const SkRRect& r, SkRegion::Op op, bool aa) SK_OVERRIDE {
199 fTarget->clipRRect(r, op, aa);
201 virtual void clipPath(const SkPath& p, SkRegion::Op op, bool aa) SK_OVERRIDE {
202 fTarget->clipPath(p, op, aa);
209 static void test_clipVisitor(skiatest::Reporter* reporter, SkCanvas* canvas) {
210 SkISize size = canvas->getDeviceSize();
213 bm.setInfo(SkImageInfo::MakeN32Premul(size.width(), size.height()));
216 Canvas2CanvasClipVisitor visitor(&c);
217 canvas->replayClips(&visitor);
219 REPORTER_ASSERT(reporter, equal_clips(c, *canvas));
222 // Format strings that describe the test context. The %s token is where
223 // the name of the test step is inserted. The context is required for
224 // disambiguating the error in the case of failures that are reported in
225 // functions that are called multiple times in different contexts (test
226 // cases and test steps).
227 static const char* const kDefaultAssertMessageFormat = "%s";
228 static const char* const kCanvasDrawAssertMessageFormat =
229 "Drawing test step %s with SkCanvas";
230 static const char* const kDeferredDrawAssertMessageFormat =
231 "Drawing test step %s with SkDeferredCanvas";
232 static const char* const kProxyDrawAssertMessageFormat =
233 "Drawing test step %s with SkProxyCanvas";
234 static const char* const kNWayDrawAssertMessageFormat =
235 "Drawing test step %s with SkNWayCanvas";
236 static const char* const kDeferredPreFlushAssertMessageFormat =
237 "test step %s, SkDeferredCanvas state consistency before flush";
238 static const char* const kDeferredPostFlushPlaybackAssertMessageFormat =
239 "test step %s, SkDeferredCanvas playback canvas state consistency after flush";
240 static const char* const kDeferredPostSilentFlushPlaybackAssertMessageFormat =
241 "test step %s, SkDeferredCanvas playback canvas state consistency after silent flush";
242 static const char* const kProxyStateAssertMessageFormat =
243 "test step %s, SkProxyCanvas state consistency";
244 static const char* const kProxyIndirectStateAssertMessageFormat =
245 "test step %s, SkProxyCanvas indirect canvas state consistency";
246 static const char* const kNWayStateAssertMessageFormat =
247 "test step %s, SkNWayCanvas state consistency";
248 static const char* const kNWayIndirect1StateAssertMessageFormat =
249 "test step %s, SkNWayCanvas indirect canvas 1 state consistency";
250 static const char* const kNWayIndirect2StateAssertMessageFormat =
251 "test step %s, SkNWayCanvas indirect canvas 2 state consistency";
252 static const char* const kPdfAssertMessageFormat =
253 "PDF sanity check failed %s";
255 class CanvasTestStep;
256 static SkTDArray<CanvasTestStep*>& testStepArray() {
257 static SkTDArray<CanvasTestStep*> theTests;
261 class CanvasTestStep {
263 CanvasTestStep(bool fEnablePdfTesting = true) {
264 *testStepArray().append() = this;
265 fAssertMessageFormat = kDefaultAssertMessageFormat;
266 this->fEnablePdfTesting = fEnablePdfTesting;
268 virtual ~CanvasTestStep() { }
270 virtual void draw(SkCanvas*, const TestData&, skiatest::Reporter*) = 0;
271 virtual const char* name() const = 0;
273 const char* assertMessage() {
274 fAssertMessage.printf(fAssertMessageFormat, name());
275 return fAssertMessage.c_str();
278 void setAssertMessageFormat(const char* format) {
279 fAssertMessageFormat = format;
282 bool enablePdfTesting() { return fEnablePdfTesting; }
285 SkString fAssertMessage;
286 const char* fAssertMessageFormat;
287 bool fEnablePdfTesting;
290 ///////////////////////////////////////////////////////////////////////////////
291 // Macros for defining test steps
293 #define TEST_STEP(NAME, FUNCTION) \
294 class NAME##_TestStep : public CanvasTestStep{ \
296 virtual void draw(SkCanvas* canvas, const TestData& d, \
297 skiatest::Reporter* reporter) { \
298 FUNCTION (canvas, d, reporter, this); \
300 virtual const char* name() const {return #NAME ;} \
302 static NAME##_TestStep NAME##_TestStepInstance;
304 #define TEST_STEP_NO_PDF(NAME, FUNCTION) \
305 class NAME##_TestStep : public CanvasTestStep{ \
307 NAME##_TestStep() : CanvasTestStep(false) {} \
308 virtual void draw(SkCanvas* canvas, const TestData& d, \
309 skiatest::Reporter* reporter) { \
310 FUNCTION (canvas, d, reporter, this); \
312 virtual const char* name() const {return #NAME ;} \
314 static NAME##_TestStep NAME##_TestStepInstance;
316 #define SIMPLE_TEST_STEP(NAME, CALL) \
317 static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \
318 skiatest::Reporter*, CanvasTestStep*) { \
321 TEST_STEP(NAME, NAME##TestStep )
323 #define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
324 static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \
325 skiatest::Reporter*, CanvasTestStep* testStep) { \
326 REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \
327 testStep->assertMessage()); \
329 TEST_STEP(NAME, NAME##TestStep )
332 ///////////////////////////////////////////////////////////////////////////////
333 // Basic test steps for most virtual methods in SkCanvas that draw or affect
334 // the state of the canvas.
336 SIMPLE_TEST_STEP(Translate, translate(SkIntToScalar(1), SkIntToScalar(2)));
337 SIMPLE_TEST_STEP(Scale, scale(SkIntToScalar(1), SkIntToScalar(2)));
338 SIMPLE_TEST_STEP(Rotate, rotate(SkIntToScalar(1)));
339 SIMPLE_TEST_STEP(Skew, skew(SkIntToScalar(1), SkIntToScalar(2)));
340 SIMPLE_TEST_STEP(Concat, concat(d.fMatrix));
341 SIMPLE_TEST_STEP(SetMatrix, setMatrix(d.fMatrix));
342 SIMPLE_TEST_STEP(ClipRect, clipRect(d.fRect));
343 SIMPLE_TEST_STEP(ClipPath, clipPath(d.fPath));
344 SIMPLE_TEST_STEP(ClipRegion, clipRegion(d.fRegion, SkRegion::kReplace_Op));
345 SIMPLE_TEST_STEP(Clear, clear(d.fColor));
346 SIMPLE_TEST_STEP(BeginGroup, beginCommentGroup(d.fText.c_str()));
347 SIMPLE_TEST_STEP(AddComment, addComment(d.fText.c_str(), d.fText.c_str()));
348 SIMPLE_TEST_STEP(EndGroup, endCommentGroup());
350 ///////////////////////////////////////////////////////////////////////////////
351 // Complex test steps
353 static void SaveMatrixClipStep(SkCanvas* canvas, const TestData& d,
354 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
355 int saveCount = canvas->getSaveCount();
357 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
358 canvas->clipRegion(d.fRegion);
360 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
361 testStep->assertMessage());
362 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
363 testStep->assertMessage());
364 // REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion, testStep->assertMessage());
366 TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
368 static void SaveLayerStep(SkCanvas* canvas, const TestData& d,
369 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
370 int saveCount = canvas->getSaveCount();
371 canvas->saveLayer(NULL, NULL);
373 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
374 testStep->assertMessage());
376 TEST_STEP(SaveLayer, SaveLayerStep);
378 static void BoundedSaveLayerStep(SkCanvas* canvas, const TestData& d,
379 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
380 int saveCount = canvas->getSaveCount();
381 canvas->saveLayer(&d.fRect, NULL);
383 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
384 testStep->assertMessage());
386 TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
388 static void PaintSaveLayerStep(SkCanvas* canvas, const TestData& d,
389 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
390 int saveCount = canvas->getSaveCount();
391 canvas->saveLayer(NULL, &d.fPaint);
393 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
394 testStep->assertMessage());
396 TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
398 static void TwoClipOpsStep(SkCanvas* canvas, const TestData& d,
399 skiatest::Reporter*, CanvasTestStep*) {
400 // This test exercises a functionality in SkPicture that leads to the
401 // recording of restore offset placeholders. This test will trigger an
402 // assertion at playback time if the placeholders are not properly
403 // filled when the recording ends.
404 canvas->clipRect(d.fRect);
405 canvas->clipRegion(d.fRegion);
407 TEST_STEP(TwoClipOps, TwoClipOpsStep);
409 // exercise fix for http://code.google.com/p/skia/issues/detail?id=560
410 // ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
411 static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas, const TestData& d,
412 skiatest::Reporter*, CanvasTestStep*) {
414 paint.setStrokeWidth(SkIntToScalar(1));
415 paint.setStyle(SkPaint::kStroke_Style);
417 canvas->drawPath(d.fNearlyZeroLengthPath, paint);
419 TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
421 static void DrawVerticesShaderTestStep(SkCanvas* canvas, const TestData& d,
422 skiatest::Reporter*, CanvasTestStep*) {
425 pts[1].set(SkIntToScalar(d.fWidth), 0);
426 pts[2].set(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight));
427 pts[3].set(0, SkIntToScalar(d.fHeight));
429 SkShader* shader = SkShader::CreateBitmapShader(d.fBitmap,
430 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
431 paint.setShader(shader)->unref();
432 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
433 NULL, NULL, NULL, 0, paint);
436 TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep);
438 static void DrawPictureTestStep(SkCanvas* canvas, const TestData& d,
439 skiatest::Reporter*, CanvasTestStep*) {
440 SkPictureRecorder recorder;
441 SkCanvas* testCanvas = recorder.beginRecording(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight),
443 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
444 testCanvas->clipRect(d.fRect);
445 testCanvas->drawRect(d.fRect, d.fPaint);
446 SkAutoTUnref<SkPicture> testPicture(recorder.endRecording());
448 canvas->drawPicture(testPicture);
450 TEST_STEP(DrawPicture, DrawPictureTestStep);
452 static void SaveRestoreTestStep(SkCanvas* canvas, const TestData& d,
453 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
454 int baseSaveCount = canvas->getSaveCount();
455 int n = canvas->save();
456 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount == n, testStep->assertMessage());
457 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
458 testStep->assertMessage());
461 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
462 testStep->assertMessage());
463 canvas->restoreToCount(baseSaveCount + 1);
464 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
465 testStep->assertMessage());
467 // should this pin to 1, or be a no-op, or crash?
468 canvas->restoreToCount(0);
469 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
470 testStep->assertMessage());
472 TEST_STEP(SaveRestore, SaveRestoreTestStep);
474 static void DrawLayerTestStep(SkCanvas* canvas, const TestData& d,
475 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
476 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
477 testStep->assertMessage());
479 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
480 testStep->assertMessage());
483 const SkRect* bounds = NULL; // null means include entire bounds
484 const SkPaint* paint = NULL;
486 canvas->saveLayer(bounds, paint);
487 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
488 testStep->assertMessage());
490 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
491 testStep->assertMessage());
493 canvas->saveLayer(bounds, paint);
494 canvas->saveLayer(bounds, paint);
495 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
496 testStep->assertMessage());
498 REPORTER_ASSERT_MESSAGE(reporter, canvas->isDrawingToLayer(),
499 testStep->assertMessage());
501 // now layer count should be 0
502 REPORTER_ASSERT_MESSAGE(reporter, !canvas->isDrawingToLayer(),
503 testStep->assertMessage());
505 TEST_STEP(DrawLayer, DrawLayerTestStep);
507 static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas, const TestData& d,
508 skiatest::Reporter*, CanvasTestStep*) {
509 // This test step challenges the TestDeferredCanvasStateConsistency
510 // test cases because the opaque paint can trigger an optimization
511 // that discards previously recorded commands. The challenge is to maintain
512 // correct clip and matrix stack state.
513 canvas->resetMatrix();
514 canvas->rotate(SkIntToScalar(30));
516 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
518 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
520 paint.setColor(0xFFFFFFFF);
521 canvas->drawPaint(paint);
525 TEST_STEP(NestedSaveRestoreWithSolidPaint, \
526 NestedSaveRestoreWithSolidPaintTestStep);
528 static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas, const TestData& d,
529 skiatest::Reporter*, CanvasTestStep*) {
530 // This test step challenges the TestDeferredCanvasStateConsistency
531 // test case because the canvas flush on a deferred canvas will
532 // reset the recording session. The challenge is to maintain correct
533 // clip and matrix stack state on the playback canvas.
534 canvas->resetMatrix();
535 canvas->rotate(SkIntToScalar(30));
537 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
539 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
540 canvas->drawRect(d.fRect,d.fPaint);
545 TEST_STEP(NestedSaveRestoreWithFlush, NestedSaveRestoreWithFlushTestStep);
547 static void AssertCanvasStatesEqual(skiatest::Reporter* reporter, const TestData& d,
548 const SkCanvas* canvas1, const SkCanvas* canvas2,
549 CanvasTestStep* testStep) {
550 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
551 canvas2->getDeviceSize(), testStep->assertMessage());
552 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
553 canvas2->getSaveCount(), testStep->assertMessage());
554 REPORTER_ASSERT_MESSAGE(reporter, canvas1->isDrawingToLayer() ==
555 canvas2->isDrawingToLayer(), testStep->assertMessage());
557 SkRect bounds1, bounds2;
558 REPORTER_ASSERT_MESSAGE(reporter,
559 canvas1->getClipBounds(&bounds1) == canvas2->getClipBounds(&bounds2),
560 testStep->assertMessage());
561 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
562 testStep->assertMessage());
564 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
565 canvas2->getDrawFilter(), testStep->assertMessage());
566 SkIRect deviceBounds1, deviceBounds2;
567 REPORTER_ASSERT_MESSAGE(reporter,
568 canvas1->getClipDeviceBounds(&deviceBounds1) ==
569 canvas2->getClipDeviceBounds(&deviceBounds2),
570 testStep->assertMessage());
571 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2, testStep->assertMessage());
572 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
573 canvas2->getTotalMatrix(), testStep->assertMessage());
574 REPORTER_ASSERT_MESSAGE(reporter, equal_clips(*canvas1, *canvas2), testStep->assertMessage());
576 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
577 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
578 while (!layerIter1.done() && !layerIter2.done()) {
579 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
580 layerIter2.matrix(), testStep->assertMessage());
581 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
582 layerIter2.clip(), testStep->assertMessage());
583 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
584 layerIter2.paint(), testStep->assertMessage());
585 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
586 layerIter2.x(), testStep->assertMessage());
587 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
588 layerIter2.y(), testStep->assertMessage());
592 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
593 testStep->assertMessage());
594 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
595 testStep->assertMessage());
599 static void TestPdfDevice(skiatest::Reporter* reporter,
601 CanvasTestStep* testStep) {
602 SkISize pageSize = SkISize::Make(d.fWidth, d.fHeight);
603 SkPDFDevice device(pageSize, pageSize, SkMatrix::I());
604 SkCanvas canvas(&device);
605 testStep->setAssertMessageFormat(kPdfAssertMessageFormat);
606 testStep->draw(&canvas, d, reporter);
608 doc.appendPage(&device);
609 SkDynamicMemoryWStream stream;
610 doc.emitPDF(&stream);
613 // The following class groups static functions that need to access
614 // the privates members of SkDeferredCanvas
615 class SkDeferredCanvasTester {
617 static void TestDeferredCanvasStateConsistency(
618 skiatest::Reporter* reporter,
620 CanvasTestStep* testStep,
621 const SkCanvas& referenceCanvas, bool silent) {
623 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
624 SkAutoTUnref<SkDeferredCanvas> deferredCanvas(SkDeferredCanvas::Create(surface.get()));
626 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
627 testStep->draw(deferredCanvas, d, reporter);
628 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
629 AssertCanvasStatesEqual(reporter, d, deferredCanvas, &referenceCanvas, testStep);
632 deferredCanvas->silentFlush();
634 deferredCanvas->flush();
637 testStep->setAssertMessageFormat(
638 silent ? kDeferredPostSilentFlushPlaybackAssertMessageFormat :
639 kDeferredPostFlushPlaybackAssertMessageFormat);
640 AssertCanvasStatesEqual(reporter, d, deferredCanvas->immediateCanvas(),
641 &referenceCanvas, testStep);
643 // Verified that deferred canvas state is not affected by flushing
644 // pending draw operations
646 // The following test code is commented out because it currently fails.
647 // Issue: http://code.google.com/p/skia/issues/detail?id=496
649 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
650 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
657 static void TestProxyCanvasStateConsistency(
658 skiatest::Reporter* reporter,
660 CanvasTestStep* testStep,
661 const SkCanvas& referenceCanvas) {
663 SkBitmap indirectStore;
664 createBitmap(&indirectStore, 0xFFFFFFFF);
665 SkCanvas indirectCanvas(indirectStore);
666 SkProxyCanvas proxyCanvas(&indirectCanvas);
667 testStep->setAssertMessageFormat(kProxyDrawAssertMessageFormat);
668 testStep->draw(&proxyCanvas, d, reporter);
669 // Verify that the SkProxyCanvas reports consitent state
670 testStep->setAssertMessageFormat(kProxyStateAssertMessageFormat);
671 AssertCanvasStatesEqual(reporter, d, &proxyCanvas, &referenceCanvas, testStep);
672 // Verify that the indirect canvas reports consitent state
673 testStep->setAssertMessageFormat(kProxyIndirectStateAssertMessageFormat);
674 AssertCanvasStatesEqual(reporter, d, &indirectCanvas, &referenceCanvas, testStep);
678 static void TestNWayCanvasStateConsistency(
679 skiatest::Reporter* reporter,
681 CanvasTestStep* testStep,
682 const SkCanvas& referenceCanvas) {
684 SkBitmap indirectStore1;
685 createBitmap(&indirectStore1, 0xFFFFFFFF);
686 SkCanvas indirectCanvas1(indirectStore1);
688 SkBitmap indirectStore2;
689 createBitmap(&indirectStore2, 0xFFFFFFFF);
690 SkCanvas indirectCanvas2(indirectStore2);
692 SkISize canvasSize = referenceCanvas.getDeviceSize();
693 SkNWayCanvas nWayCanvas(canvasSize.width(), canvasSize.height());
694 nWayCanvas.addCanvas(&indirectCanvas1);
695 nWayCanvas.addCanvas(&indirectCanvas2);
697 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
698 testStep->draw(&nWayCanvas, d, reporter);
699 // Verify that the SkProxyCanvas reports consitent state
700 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
701 AssertCanvasStatesEqual(reporter, d, &nWayCanvas, &referenceCanvas, testStep);
702 // Verify that the indirect canvases report consitent state
703 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
704 AssertCanvasStatesEqual(reporter, d, &indirectCanvas1, &referenceCanvas, testStep);
705 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
706 AssertCanvasStatesEqual(reporter, d, &indirectCanvas2, &referenceCanvas, testStep);
710 * This sub-test verifies that the test step passes when executed
711 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
712 * that the all canvas derivatives report the same state as an SkCanvas
713 * after having executed the test step.
715 static void TestOverrideStateConsistency(skiatest::Reporter* reporter, const TestData& d,
716 CanvasTestStep* testStep) {
717 SkBitmap referenceStore;
718 createBitmap(&referenceStore, 0xFFFFFFFF);
719 SkCanvas referenceCanvas(referenceStore);
720 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
721 testStep->draw(&referenceCanvas, d, reporter);
723 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, d, testStep, referenceCanvas, false);
725 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, d, testStep, referenceCanvas, true);
727 // The following test code is disabled because SkProxyCanvas is
728 // missing a lot of virtual overrides on get* methods, which are used
729 // to verify canvas state.
730 // Issue: http://code.google.com/p/skia/issues/detail?id=500
732 if (false) { // avoid bit rot, suppress warning
733 TestProxyCanvasStateConsistency(reporter, d, testStep, referenceCanvas);
736 // The following test code is disabled because SkNWayCanvas does not
737 // report correct clipping and device bounds information
738 // Issue: http://code.google.com/p/skia/issues/detail?id=501
740 if (false) { // avoid bit rot, suppress warning
741 TestNWayCanvasStateConsistency(reporter, d, testStep, referenceCanvas);
744 if (false) { // avoid bit rot, suppress warning
745 test_clipVisitor(reporter, &referenceCanvas);
749 static void test_newraster(skiatest::Reporter* reporter) {
750 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
751 SkCanvas* canvas = SkCanvas::NewRaster(info);
752 REPORTER_ASSERT(reporter, canvas);
756 const SkPMColor* addr = (const SkPMColor*)canvas->peekPixels(&info2, &rowBytes);
757 REPORTER_ASSERT(reporter, addr);
758 REPORTER_ASSERT(reporter, info == info2);
759 for (int y = 0; y < info.height(); ++y) {
760 for (int x = 0; x < info.width(); ++x) {
761 REPORTER_ASSERT(reporter, 0 == addr[x]);
763 addr = (const SkPMColor*)((const char*)addr + rowBytes);
767 // now try a deliberately bad info
768 info = info.makeWH(-1, info.height());
769 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRaster(info));
772 info = info.makeWH(1 << 30, 1 << 30);
773 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRaster(info));
775 // not a valid pixel type
776 info = SkImageInfo::Make(10, 10, kUnknown_SkColorType, info.alphaType());
777 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRaster(info));
779 // We should succeed with a zero-sized valid info
780 info = SkImageInfo::MakeN32Premul(0, 0);
781 canvas = SkCanvas::NewRaster(info);
782 REPORTER_ASSERT(reporter, canvas);
786 DEF_TEST(Canvas, reporter) {
789 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
790 TestOverrideStateConsistency(reporter, d, testStepArray()[testStep]);
791 if (testStepArray()[testStep]->enablePdfTesting()) {
792 TestPdfDevice(reporter, d, testStepArray()[testStep]);
796 test_newraster(reporter);