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"
50 #include "SkDocument.h"
52 #include "SkNWayCanvas.h"
55 #include "SkPicture.h"
56 #include "SkPictureRecord.h"
57 #include "SkPictureRecorder.h"
62 #include "SkSurface.h"
63 #include "SkTDArray.h"
66 static const int kWidth = 2, kHeight = 2;
68 static void createBitmap(SkBitmap* bm, SkColor color) {
69 bm->allocN32Pixels(kWidth, kHeight);
70 bm->eraseColor(color);
73 static SkSurface* createSurface(SkColor color) {
74 SkSurface* surface = SkSurface::NewRasterN32Premul(kWidth, kHeight);
75 surface->getCanvas()->clear(color);
79 ///////////////////////////////////////////////////////////////////////////////
80 // Constants used by test steps
81 const SkPoint kTestPoints[] = {
82 {SkIntToScalar(0), SkIntToScalar(0)},
83 {SkIntToScalar(2), SkIntToScalar(1)},
84 {SkIntToScalar(0), SkIntToScalar(2)}
86 const SkPoint kTestPoints2[] = {
87 { SkIntToScalar(0), SkIntToScalar(1) },
88 { SkIntToScalar(1), SkIntToScalar(1) },
89 { SkIntToScalar(2), SkIntToScalar(1) },
90 { SkIntToScalar(3), SkIntToScalar(1) },
91 { SkIntToScalar(4), SkIntToScalar(1) },
92 { SkIntToScalar(5), SkIntToScalar(1) },
93 { SkIntToScalar(6), SkIntToScalar(1) },
94 { SkIntToScalar(7), SkIntToScalar(1) },
95 { SkIntToScalar(8), SkIntToScalar(1) },
96 { SkIntToScalar(9), SkIntToScalar(1) },
97 { SkIntToScalar(10), SkIntToScalar(1) }
103 : fRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
104 SkIntToScalar(2), SkIntToScalar(1)))
105 , fMatrix(TestMatrix())
107 , fNearlyZeroLengthPath(TestNearlyZeroLengthPath())
108 , fIRect(SkIRect::MakeXYWH(0, 0, 2, 1))
109 , fRegion(TestRegion())
111 , fPoints(kTestPoints)
115 , fText("Hello World")
116 , fPoints2(kTestPoints2)
117 , fBitmap(TestBitmap())
123 SkPath fNearlyZeroLengthPath;
128 const SkPoint* fPoints;
133 const SkPoint* fPoints2;
137 static SkMatrix TestMatrix() {
140 matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
144 static SkPath TestPath() {
146 path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
147 SkIntToScalar(2), SkIntToScalar(1)));
150 static SkPath TestNearlyZeroLengthPath() {
152 SkPoint pt1 = { 0, 0 };
153 SkPoint pt2 = { 0, SK_ScalarNearlyZero };
154 SkPoint pt3 = { SkIntToScalar(1), 0 };
155 SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
162 static SkRegion TestRegion() {
164 SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
165 region.setRect(rect);
168 static SkBitmap TestBitmap() {
170 createBitmap(&bitmap, 0x05060708);
175 static bool equal_clips(const SkCanvas& a, const SkCanvas& b) {
176 if (a.isClipEmpty()) {
177 return b.isClipEmpty();
179 if (!a.isClipRect()) {
180 // this is liberally true, since we don't expose a way to know this exactly (for non-rects)
181 return !b.isClipRect();
184 a.getClipDeviceBounds(&ar);
185 b.getClipDeviceBounds(&br);
189 class Canvas2CanvasClipVisitor : public SkCanvas::ClipVisitor {
191 Canvas2CanvasClipVisitor(SkCanvas* target) : fTarget(target) {}
193 void clipRect(const SkRect& r, SkRegion::Op op, bool aa) override {
194 fTarget->clipRect(r, op, aa);
196 void clipRRect(const SkRRect& r, SkRegion::Op op, bool aa) override {
197 fTarget->clipRRect(r, op, aa);
199 void clipPath(const SkPath& p, SkRegion::Op op, bool aa) override {
200 fTarget->clipPath(p, op, aa);
207 static void test_clipVisitor(skiatest::Reporter* reporter, SkCanvas* canvas) {
208 SkISize size = canvas->getDeviceSize();
211 bm.setInfo(SkImageInfo::MakeN32Premul(size.width(), size.height()));
214 Canvas2CanvasClipVisitor visitor(&c);
215 canvas->replayClips(&visitor);
217 REPORTER_ASSERT(reporter, equal_clips(c, *canvas));
220 // Format strings that describe the test context. The %s token is where
221 // the name of the test step is inserted. The context is required for
222 // disambiguating the error in the case of failures that are reported in
223 // functions that are called multiple times in different contexts (test
224 // cases and test steps).
225 static const char* const kDefaultAssertMessageFormat = "%s";
226 static const char* const kCanvasDrawAssertMessageFormat =
227 "Drawing test step %s with SkCanvas";
228 static const char* const kDeferredDrawAssertMessageFormat =
229 "Drawing test step %s with SkDeferredCanvas";
230 static const char* const kNWayDrawAssertMessageFormat =
231 "Drawing test step %s with SkNWayCanvas";
232 static const char* const kDeferredPreFlushAssertMessageFormat =
233 "test step %s, SkDeferredCanvas state consistency before flush";
234 static const char* const kDeferredPostFlushPlaybackAssertMessageFormat =
235 "test step %s, SkDeferredCanvas playback canvas state consistency after flush";
236 static const char* const kDeferredPostSilentFlushPlaybackAssertMessageFormat =
237 "test step %s, SkDeferredCanvas playback canvas state consistency after silent flush";
238 static const char* const kNWayStateAssertMessageFormat =
239 "test step %s, SkNWayCanvas state consistency";
240 static const char* const kNWayIndirect1StateAssertMessageFormat =
241 "test step %s, SkNWayCanvas indirect canvas 1 state consistency";
242 static const char* const kNWayIndirect2StateAssertMessageFormat =
243 "test step %s, SkNWayCanvas indirect canvas 2 state consistency";
244 static const char* const kPdfAssertMessageFormat =
245 "PDF sanity check failed %s";
247 class CanvasTestStep;
248 static SkTDArray<CanvasTestStep*>& testStepArray() {
249 static SkTDArray<CanvasTestStep*> theTests;
253 class CanvasTestStep {
255 CanvasTestStep(bool fEnablePdfTesting = true) {
256 *testStepArray().append() = this;
257 fAssertMessageFormat = kDefaultAssertMessageFormat;
258 this->fEnablePdfTesting = fEnablePdfTesting;
260 virtual ~CanvasTestStep() { }
262 virtual void draw(SkCanvas*, const TestData&, skiatest::Reporter*) = 0;
263 virtual const char* name() const = 0;
265 const char* assertMessage() {
266 fAssertMessage.printf(fAssertMessageFormat, name());
267 return fAssertMessage.c_str();
270 void setAssertMessageFormat(const char* format) {
271 fAssertMessageFormat = format;
274 bool enablePdfTesting() { return fEnablePdfTesting; }
277 SkString fAssertMessage;
278 const char* fAssertMessageFormat;
279 bool fEnablePdfTesting;
282 ///////////////////////////////////////////////////////////////////////////////
283 // Macros for defining test steps
285 #define TEST_STEP(NAME, FUNCTION) \
286 class NAME##_TestStep : public CanvasTestStep{ \
288 virtual void draw(SkCanvas* canvas, const TestData& d, \
289 skiatest::Reporter* reporter) { \
290 FUNCTION (canvas, d, reporter, this); \
292 virtual const char* name() const {return #NAME ;} \
294 static NAME##_TestStep NAME##_TestStepInstance;
296 #define TEST_STEP_NO_PDF(NAME, FUNCTION) \
297 class NAME##_TestStep : public CanvasTestStep{ \
299 NAME##_TestStep() : CanvasTestStep(false) {} \
300 virtual void draw(SkCanvas* canvas, const TestData& d, \
301 skiatest::Reporter* reporter) { \
302 FUNCTION (canvas, d, reporter, this); \
304 virtual const char* name() const {return #NAME ;} \
306 static NAME##_TestStep NAME##_TestStepInstance;
308 #define SIMPLE_TEST_STEP(NAME, CALL) \
309 static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \
310 skiatest::Reporter*, CanvasTestStep*) { \
313 TEST_STEP(NAME, NAME##TestStep )
315 #define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
316 static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \
317 skiatest::Reporter*, CanvasTestStep* testStep) { \
318 REPORTER_ASSERT_MESSAGE(reporter, canvas-> CALL , \
319 testStep->assertMessage()); \
321 TEST_STEP(NAME, NAME##TestStep )
324 ///////////////////////////////////////////////////////////////////////////////
325 // Basic test steps for most virtual methods in SkCanvas that draw or affect
326 // the state of the canvas.
328 SIMPLE_TEST_STEP(Translate, translate(SkIntToScalar(1), SkIntToScalar(2)));
329 SIMPLE_TEST_STEP(Scale, scale(SkIntToScalar(1), SkIntToScalar(2)));
330 SIMPLE_TEST_STEP(Rotate, rotate(SkIntToScalar(1)));
331 SIMPLE_TEST_STEP(Skew, skew(SkIntToScalar(1), SkIntToScalar(2)));
332 SIMPLE_TEST_STEP(Concat, concat(d.fMatrix));
333 SIMPLE_TEST_STEP(SetMatrix, setMatrix(d.fMatrix));
334 SIMPLE_TEST_STEP(ClipRect, clipRect(d.fRect));
335 SIMPLE_TEST_STEP(ClipPath, clipPath(d.fPath));
336 SIMPLE_TEST_STEP(ClipRegion, clipRegion(d.fRegion, SkRegion::kReplace_Op));
337 SIMPLE_TEST_STEP(Clear, clear(d.fColor));
338 SIMPLE_TEST_STEP(BeginGroup, beginCommentGroup(d.fText.c_str()));
339 SIMPLE_TEST_STEP(AddComment, addComment(d.fText.c_str(), d.fText.c_str()));
340 SIMPLE_TEST_STEP(EndGroup, endCommentGroup());
342 ///////////////////////////////////////////////////////////////////////////////
343 // Complex test steps
345 static void SaveMatrixClipStep(SkCanvas* canvas, const TestData& d,
346 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
347 int saveCount = canvas->getSaveCount();
349 canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
350 canvas->clipRegion(d.fRegion);
352 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
353 testStep->assertMessage());
354 REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalMatrix().isIdentity(),
355 testStep->assertMessage());
356 // REPORTER_ASSERT_MESSAGE(reporter, canvas->getTotalClip() != kTestRegion, testStep->assertMessage());
358 TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
360 static void SaveLayerStep(SkCanvas* canvas, const TestData& d,
361 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
362 int saveCount = canvas->getSaveCount();
363 canvas->saveLayer(NULL, NULL);
365 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
366 testStep->assertMessage());
368 TEST_STEP(SaveLayer, SaveLayerStep);
370 static void BoundedSaveLayerStep(SkCanvas* canvas, const TestData& d,
371 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
372 int saveCount = canvas->getSaveCount();
373 canvas->saveLayer(&d.fRect, NULL);
375 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
376 testStep->assertMessage());
378 TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
380 static void PaintSaveLayerStep(SkCanvas* canvas, const TestData& d,
381 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
382 int saveCount = canvas->getSaveCount();
383 canvas->saveLayer(NULL, &d.fPaint);
385 REPORTER_ASSERT_MESSAGE(reporter, canvas->getSaveCount() == saveCount,
386 testStep->assertMessage());
388 TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
390 static void TwoClipOpsStep(SkCanvas* canvas, const TestData& d,
391 skiatest::Reporter*, CanvasTestStep*) {
392 // This test exercises a functionality in SkPicture that leads to the
393 // recording of restore offset placeholders. This test will trigger an
394 // assertion at playback time if the placeholders are not properly
395 // filled when the recording ends.
396 canvas->clipRect(d.fRect);
397 canvas->clipRegion(d.fRegion);
399 TEST_STEP(TwoClipOps, TwoClipOpsStep);
401 // exercise fix for http://code.google.com/p/skia/issues/detail?id=560
402 // ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
403 static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas, const TestData& d,
404 skiatest::Reporter*, CanvasTestStep*) {
406 paint.setStrokeWidth(SkIntToScalar(1));
407 paint.setStyle(SkPaint::kStroke_Style);
409 canvas->drawPath(d.fNearlyZeroLengthPath, paint);
411 TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
413 static void DrawVerticesShaderTestStep(SkCanvas* canvas, const TestData& d,
414 skiatest::Reporter*, CanvasTestStep*) {
417 pts[1].set(SkIntToScalar(d.fWidth), 0);
418 pts[2].set(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight));
419 pts[3].set(0, SkIntToScalar(d.fHeight));
421 SkShader* shader = SkShader::CreateBitmapShader(d.fBitmap,
422 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
423 paint.setShader(shader)->unref();
424 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, pts, pts,
425 NULL, NULL, NULL, 0, paint);
428 TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep);
430 static void DrawPictureTestStep(SkCanvas* canvas, const TestData& d,
431 skiatest::Reporter*, CanvasTestStep*) {
432 SkPictureRecorder recorder;
433 SkCanvas* testCanvas = recorder.beginRecording(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight),
435 testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
436 testCanvas->clipRect(d.fRect);
437 testCanvas->drawRect(d.fRect, d.fPaint);
438 SkAutoTUnref<SkPicture> testPicture(recorder.endRecording());
440 canvas->drawPicture(testPicture);
442 TEST_STEP(DrawPicture, DrawPictureTestStep);
444 static void SaveRestoreTestStep(SkCanvas* canvas, const TestData& d,
445 skiatest::Reporter* reporter, CanvasTestStep* testStep) {
446 int baseSaveCount = canvas->getSaveCount();
447 int n = canvas->save();
448 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount == n, testStep->assertMessage());
449 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
450 testStep->assertMessage());
453 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
454 testStep->assertMessage());
455 canvas->restoreToCount(baseSaveCount + 1);
456 REPORTER_ASSERT_MESSAGE(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
457 testStep->assertMessage());
459 // should this pin to 1, or be a no-op, or crash?
460 canvas->restoreToCount(0);
461 REPORTER_ASSERT_MESSAGE(reporter, 1 == canvas->getSaveCount(),
462 testStep->assertMessage());
464 TEST_STEP(SaveRestore, SaveRestoreTestStep);
466 static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas, const TestData& d,
467 skiatest::Reporter*, CanvasTestStep*) {
468 // This test step challenges the TestDeferredCanvasStateConsistency
469 // test cases because the opaque paint can trigger an optimization
470 // that discards previously recorded commands. The challenge is to maintain
471 // correct clip and matrix stack state.
472 canvas->resetMatrix();
473 canvas->rotate(SkIntToScalar(30));
475 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
477 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
479 paint.setColor(0xFFFFFFFF);
480 canvas->drawPaint(paint);
484 TEST_STEP(NestedSaveRestoreWithSolidPaint, \
485 NestedSaveRestoreWithSolidPaintTestStep);
487 static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas, const TestData& d,
488 skiatest::Reporter*, CanvasTestStep*) {
489 // This test step challenges the TestDeferredCanvasStateConsistency
490 // test case because the canvas flush on a deferred canvas will
491 // reset the recording session. The challenge is to maintain correct
492 // clip and matrix stack state on the playback canvas.
493 canvas->resetMatrix();
494 canvas->rotate(SkIntToScalar(30));
496 canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
498 canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
499 canvas->drawRect(d.fRect,d.fPaint);
504 TEST_STEP(NestedSaveRestoreWithFlush, NestedSaveRestoreWithFlushTestStep);
506 static void AssertCanvasStatesEqual(skiatest::Reporter* reporter, const TestData& d,
507 const SkCanvas* canvas1, const SkCanvas* canvas2,
508 CanvasTestStep* testStep) {
509 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() ==
510 canvas2->getDeviceSize(), testStep->assertMessage());
511 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() ==
512 canvas2->getSaveCount(), testStep->assertMessage());
514 SkRect bounds1, bounds2;
515 REPORTER_ASSERT_MESSAGE(reporter,
516 canvas1->getClipBounds(&bounds1) == canvas2->getClipBounds(&bounds2),
517 testStep->assertMessage());
518 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2,
519 testStep->assertMessage());
521 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() ==
522 canvas2->getDrawFilter(), testStep->assertMessage());
523 SkIRect deviceBounds1, deviceBounds2;
524 REPORTER_ASSERT_MESSAGE(reporter,
525 canvas1->getClipDeviceBounds(&deviceBounds1) ==
526 canvas2->getClipDeviceBounds(&deviceBounds2),
527 testStep->assertMessage());
528 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2, testStep->assertMessage());
529 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() ==
530 canvas2->getTotalMatrix(), testStep->assertMessage());
531 REPORTER_ASSERT_MESSAGE(reporter, equal_clips(*canvas1, *canvas2), testStep->assertMessage());
533 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false);
534 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false);
535 while (!layerIter1.done() && !layerIter2.done()) {
536 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.matrix() ==
537 layerIter2.matrix(), testStep->assertMessage());
538 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.clip() ==
539 layerIter2.clip(), testStep->assertMessage());
540 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.paint() ==
541 layerIter2.paint(), testStep->assertMessage());
542 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.x() ==
543 layerIter2.x(), testStep->assertMessage());
544 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.y() ==
545 layerIter2.y(), testStep->assertMessage());
549 REPORTER_ASSERT_MESSAGE(reporter, layerIter1.done(),
550 testStep->assertMessage());
551 REPORTER_ASSERT_MESSAGE(reporter, layerIter2.done(),
552 testStep->assertMessage());
556 static void TestPdfDevice(skiatest::Reporter* reporter,
558 CanvasTestStep* testStep) {
559 SkDynamicMemoryWStream outStream;
560 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&outStream));
561 SkCanvas* canvas = doc->beginPage(SkIntToScalar(d.fWidth),
562 SkIntToScalar(d.fHeight));
563 REPORTER_ASSERT(reporter, canvas);
564 testStep->setAssertMessageFormat(kPdfAssertMessageFormat);
565 testStep->draw(canvas, d, reporter);
567 REPORTER_ASSERT(reporter, doc->close());
570 // The following class groups static functions that need to access
571 // the privates members of SkDeferredCanvas
572 class SkDeferredCanvasTester {
574 static void TestDeferredCanvasStateConsistency(
575 skiatest::Reporter* reporter,
577 CanvasTestStep* testStep,
578 const SkCanvas& referenceCanvas, bool silent) {
580 SkAutoTUnref<SkSurface> surface(createSurface(0xFFFFFFFF));
581 SkAutoTUnref<SkDeferredCanvas> deferredCanvas(SkDeferredCanvas::Create(surface.get()));
583 testStep->setAssertMessageFormat(kDeferredDrawAssertMessageFormat);
584 testStep->draw(deferredCanvas, d, reporter);
585 testStep->setAssertMessageFormat(kDeferredPreFlushAssertMessageFormat);
586 AssertCanvasStatesEqual(reporter, d, deferredCanvas, &referenceCanvas, testStep);
589 deferredCanvas->silentFlush();
591 deferredCanvas->flush();
594 testStep->setAssertMessageFormat(
595 silent ? kDeferredPostSilentFlushPlaybackAssertMessageFormat :
596 kDeferredPostFlushPlaybackAssertMessageFormat);
597 AssertCanvasStatesEqual(reporter, d, deferredCanvas->immediateCanvas(),
598 &referenceCanvas, testStep);
600 // Verified that deferred canvas state is not affected by flushing
601 // pending draw operations
603 // The following test code is commented out because it currently fails.
604 // Issue: http://code.google.com/p/skia/issues/detail?id=496
606 testStep->setAssertMessageFormat(kDeferredPostFlushAssertMessageFormat);
607 AssertCanvasStatesEqual(reporter, &deferredCanvas, &referenceCanvas,
614 static void TestNWayCanvasStateConsistency(
615 skiatest::Reporter* reporter,
617 CanvasTestStep* testStep,
618 const SkCanvas& referenceCanvas) {
620 SkBitmap indirectStore1;
621 createBitmap(&indirectStore1, 0xFFFFFFFF);
622 SkCanvas indirectCanvas1(indirectStore1);
624 SkBitmap indirectStore2;
625 createBitmap(&indirectStore2, 0xFFFFFFFF);
626 SkCanvas indirectCanvas2(indirectStore2);
628 SkISize canvasSize = referenceCanvas.getDeviceSize();
629 SkNWayCanvas nWayCanvas(canvasSize.width(), canvasSize.height());
630 nWayCanvas.addCanvas(&indirectCanvas1);
631 nWayCanvas.addCanvas(&indirectCanvas2);
633 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat);
634 testStep->draw(&nWayCanvas, d, reporter);
635 // Verify that the SkNWayCanvas reports consitent state
636 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat);
637 AssertCanvasStatesEqual(reporter, d, &nWayCanvas, &referenceCanvas, testStep);
638 // Verify that the indirect canvases report consitent state
639 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat);
640 AssertCanvasStatesEqual(reporter, d, &indirectCanvas1, &referenceCanvas, testStep);
641 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat);
642 AssertCanvasStatesEqual(reporter, d, &indirectCanvas2, &referenceCanvas, testStep);
646 * This sub-test verifies that the test step passes when executed
647 * with SkCanvas and with classes derrived from SkCanvas. It also verifies
648 * that the all canvas derivatives report the same state as an SkCanvas
649 * after having executed the test step.
651 static void TestOverrideStateConsistency(skiatest::Reporter* reporter, const TestData& d,
652 CanvasTestStep* testStep) {
653 SkBitmap referenceStore;
654 createBitmap(&referenceStore, 0xFFFFFFFF);
655 SkCanvas referenceCanvas(referenceStore);
656 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
657 testStep->draw(&referenceCanvas, d, reporter);
659 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, d, testStep, referenceCanvas, false);
661 SkDeferredCanvasTester::TestDeferredCanvasStateConsistency(reporter, d, testStep, referenceCanvas, true);
663 // The following test code is disabled because SkNWayCanvas does not
664 // report correct clipping and device bounds information
665 // Issue: http://code.google.com/p/skia/issues/detail?id=501
667 if (false) { // avoid bit rot, suppress warning
668 TestNWayCanvasStateConsistency(reporter, d, testStep, referenceCanvas);
671 if (false) { // avoid bit rot, suppress warning
672 test_clipVisitor(reporter, &referenceCanvas);
676 static void test_newraster(skiatest::Reporter* reporter) {
677 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
678 const size_t minRowBytes = info.minRowBytes();
679 const size_t size = info.getSafeSize(minRowBytes);
680 SkAutoMalloc storage(size);
681 SkPMColor* baseAddr = static_cast<SkPMColor*>(storage.get());
682 sk_bzero(baseAddr, size);
684 SkCanvas* canvas = SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes);
685 REPORTER_ASSERT(reporter, canvas);
689 const SkPMColor* addr = (const SkPMColor*)canvas->peekPixels(&info2, &rowBytes);
690 REPORTER_ASSERT(reporter, addr);
691 REPORTER_ASSERT(reporter, info == info2);
692 REPORTER_ASSERT(reporter, minRowBytes == rowBytes);
693 for (int y = 0; y < info.height(); ++y) {
694 for (int x = 0; x < info.width(); ++x) {
695 REPORTER_ASSERT(reporter, 0 == addr[x]);
697 addr = (const SkPMColor*)((const char*)addr + rowBytes);
701 // now try a deliberately bad info
702 info = info.makeWH(-1, info.height());
703 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
706 info = info.makeWH(1 << 30, 1 << 30);
707 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
709 // not a valid pixel type
710 info = SkImageInfo::Make(10, 10, kUnknown_SkColorType, info.alphaType());
711 REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
713 // We should succeed with a zero-sized valid info
714 info = SkImageInfo::MakeN32Premul(0, 0);
715 canvas = SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes);
716 REPORTER_ASSERT(reporter, canvas);
720 DEF_TEST(Canvas, reporter) {
723 for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
724 TestOverrideStateConsistency(reporter, d, testStepArray()[testStep]);
725 if (testStepArray()[testStep]->enablePdfTesting()) {
726 TestPdfDevice(reporter, d, testStepArray()[testStep]);
730 test_newraster(reporter);
733 DEF_TEST(Canvas_SaveState, reporter) {
734 SkCanvas canvas(10, 10);
735 REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
737 int n = canvas.save();
738 REPORTER_ASSERT(reporter, 1 == n);
739 REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
741 n = canvas.saveLayer(NULL, NULL);
742 REPORTER_ASSERT(reporter, 2 == n);
743 REPORTER_ASSERT(reporter, 3 == canvas.getSaveCount());
746 REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
748 REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
751 DEF_TEST(Canvas_ClipEmptyPath, reporter) {
752 SkCanvas canvas(10, 10);
755 canvas.clipPath(path);
759 canvas.clipPath(path);
763 canvas.clipPath(path); // should not assert here