| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 /* Description: | 8 /* Description: |
| 9 * This test defines a series of elementatry test steps that perform | 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 | 10 * a single or a small group of canvas API calls. Each test step is |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 region.setRect(rect); | 162 region.setRect(rect); |
| 163 return region; | 163 return region; |
| 164 } | 164 } |
| 165 static SkBitmap TestBitmap() { | 165 static SkBitmap TestBitmap() { |
| 166 SkBitmap bitmap; | 166 SkBitmap bitmap; |
| 167 createBitmap(&bitmap, 0x05060708); | 167 createBitmap(&bitmap, 0x05060708); |
| 168 return bitmap; | 168 return bitmap; |
| 169 } | 169 } |
| 170 }; | 170 }; |
| 171 | 171 |
| 172 static bool equal_clips(const SkCanvas& a, const SkCanvas& b) { | |
| 173 if (a.isClipEmpty()) { | |
| 174 return b.isClipEmpty(); | |
| 175 } | |
| 176 if (!a.isClipRect()) { | |
| 177 // this is liberally true, since we don't expose a way to know this exac
tly (for non-rects) | |
| 178 return !b.isClipRect(); | |
| 179 } | |
| 180 SkIRect ar, br; | |
| 181 a.getClipDeviceBounds(&ar); | |
| 182 b.getClipDeviceBounds(&br); | |
| 183 return ar == br; | |
| 184 } | |
| 185 | |
| 186 class Canvas2CanvasClipVisitor : public SkCanvas::ClipVisitor { | 172 class Canvas2CanvasClipVisitor : public SkCanvas::ClipVisitor { |
| 187 public: | 173 public: |
| 188 Canvas2CanvasClipVisitor(SkCanvas* target) : fTarget(target) {} | 174 Canvas2CanvasClipVisitor(SkCanvas* target) : fTarget(target) {} |
| 189 | 175 |
| 190 void clipRect(const SkRect& r, SkRegion::Op op, bool aa) override { | 176 void clipRect(const SkRect& r, SkRegion::Op op, bool aa) override { |
| 191 fTarget->clipRect(r, op, aa); | 177 fTarget->clipRect(r, op, aa); |
| 192 } | 178 } |
| 193 void clipRRect(const SkRRect& r, SkRegion::Op op, bool aa) override { | 179 void clipRRect(const SkRRect& r, SkRegion::Op op, bool aa) override { |
| 194 fTarget->clipRRect(r, op, aa); | 180 fTarget->clipRRect(r, op, aa); |
| 195 } | 181 } |
| 196 void clipPath(const SkPath& p, SkRegion::Op op, bool aa) override { | 182 void clipPath(const SkPath& p, SkRegion::Op op, bool aa) override { |
| 197 fTarget->clipPath(p, op, aa); | 183 fTarget->clipPath(p, op, aa); |
| 198 } | 184 } |
| 199 | 185 |
| 200 private: | 186 private: |
| 201 SkCanvas* fTarget; | 187 SkCanvas* fTarget; |
| 202 }; | 188 }; |
| 203 | 189 |
| 204 static void test_clipVisitor(skiatest::Reporter* reporter, SkCanvas* canvas) { | |
| 205 SkISize size = canvas->getDeviceSize(); | |
| 206 | |
| 207 SkBitmap bm; | |
| 208 bm.setInfo(SkImageInfo::MakeN32Premul(size.width(), size.height())); | |
| 209 SkCanvas c(bm); | |
| 210 | |
| 211 Canvas2CanvasClipVisitor visitor(&c); | |
| 212 canvas->replayClips(&visitor); | |
| 213 | |
| 214 REPORTER_ASSERT(reporter, equal_clips(c, *canvas)); | |
| 215 } | |
| 216 | |
| 217 static void test_clipstack(skiatest::Reporter* reporter) { | 190 static void test_clipstack(skiatest::Reporter* reporter) { |
| 218 // The clipstack is refcounted, and needs to be able to out-live the canvas
if a client has | 191 // The clipstack is refcounted, and needs to be able to out-live the canvas
if a client has |
| 219 // ref'd it. | 192 // ref'd it. |
| 220 const SkClipStack* cs = nullptr; | 193 const SkClipStack* cs = nullptr; |
| 221 { | 194 { |
| 222 SkCanvas canvas(10, 10); | 195 SkCanvas canvas(10, 10); |
| 223 cs = SkRef(canvas.getClipStack()); | 196 cs = SkRef(canvas.getClipStack()); |
| 224 } | 197 } |
| 225 REPORTER_ASSERT(reporter, cs->unique()); | 198 REPORTER_ASSERT(reporter, cs->unique()); |
| 226 cs->unref(); | 199 cs->unref(); |
| 227 } | 200 } |
| 228 | 201 |
| 229 // Format strings that describe the test context. The %s token is where | 202 // Format strings that describe the test context. The %s token is where |
| 230 // the name of the test step is inserted. The context is required for | 203 // the name of the test step is inserted. The context is required for |
| 231 // disambiguating the error in the case of failures that are reported in | 204 // disambiguating the error in the case of failures that are reported in |
| 232 // functions that are called multiple times in different contexts (test | 205 // functions that are called multiple times in different contexts (test |
| 233 // cases and test steps). | 206 // cases and test steps). |
| 234 static const char* const kDefaultAssertMessageFormat = "%s"; | 207 static const char* const kDefaultAssertMessageFormat = "%s"; |
| 235 static const char* const kCanvasDrawAssertMessageFormat = | 208 static const char* const kCanvasDrawAssertMessageFormat = |
| 236 "Drawing test step %s with SkCanvas"; | 209 "Drawing test step %s with SkCanvas"; |
| 237 static const char* const kNWayDrawAssertMessageFormat = | |
| 238 "Drawing test step %s with SkNWayCanvas"; | |
| 239 static const char* const kNWayStateAssertMessageFormat = | |
| 240 "test step %s, SkNWayCanvas state consistency"; | |
| 241 static const char* const kNWayIndirect1StateAssertMessageFormat = | |
| 242 "test step %s, SkNWayCanvas indirect canvas 1 state consistency"; | |
| 243 static const char* const kNWayIndirect2StateAssertMessageFormat = | |
| 244 "test step %s, SkNWayCanvas indirect canvas 2 state consistency"; | |
| 245 static const char* const kPdfAssertMessageFormat = | 210 static const char* const kPdfAssertMessageFormat = |
| 246 "PDF sanity check failed %s"; | 211 "PDF sanity check failed %s"; |
| 247 | 212 |
| 248 class CanvasTestStep; | 213 class CanvasTestStep; |
| 249 static SkTDArray<CanvasTestStep*>& testStepArray() { | 214 static SkTDArray<CanvasTestStep*>& testStepArray() { |
| 250 static SkTDArray<CanvasTestStep*> theTests; | 215 static SkTDArray<CanvasTestStep*> theTests; |
| 251 return theTests; | 216 return theTests; |
| 252 } | 217 } |
| 253 | 218 |
| 254 class CanvasTestStep { | 219 class CanvasTestStep { |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 531 REPORTER_ASSERT_MESSAGE(reporter, m == SkMatrix::MakeTrans(-1.f, -1.f), | 496 REPORTER_ASSERT_MESSAGE(reporter, m == SkMatrix::MakeTrans(-1.f, -1.f), |
| 532 testStep->assertMessage()); | 497 testStep->assertMessage()); |
| 533 REPORTER_ASSERT_MESSAGE(reporter, r == SkIRect::MakeXYWH(0, 0, 1, 1), | 498 REPORTER_ASSERT_MESSAGE(reporter, r == SkIRect::MakeXYWH(0, 0, 1, 1), |
| 534 testStep->assertMessage()); | 499 testStep->assertMessage()); |
| 535 canvas->restore(); | 500 canvas->restore(); |
| 536 | 501 |
| 537 } | 502 } |
| 538 TEST_STEP(DescribeTopLayer, DescribeTopLayerTestStep); | 503 TEST_STEP(DescribeTopLayer, DescribeTopLayerTestStep); |
| 539 | 504 |
| 540 | 505 |
| 541 class CanvasTestingAccess { | 506 static void TestPdfDevice(skiatest::Reporter* reporter, const TestData& d, Canva
sTestStep* step) { |
| 542 public: | |
| 543 static bool SameState(const SkCanvas* canvas1, const SkCanvas* canvas2) { | |
| 544 SkCanvas::LayerIter layerIter1(const_cast<SkCanvas*>(canvas1), false); | |
| 545 SkCanvas::LayerIter layerIter2(const_cast<SkCanvas*>(canvas2), false); | |
| 546 while (!layerIter1.done() && !layerIter2.done()) { | |
| 547 if (layerIter1.matrix() != layerIter2.matrix()) { | |
| 548 return false; | |
| 549 } | |
| 550 if (layerIter1.clip() != layerIter2.clip()) { | |
| 551 return false; | |
| 552 } | |
| 553 if (layerIter1.paint() != layerIter2.paint()) { | |
| 554 return false; | |
| 555 } | |
| 556 if (layerIter1.x() != layerIter2.x()) { | |
| 557 return false; | |
| 558 } | |
| 559 if (layerIter1.y() != layerIter2.y()) { | |
| 560 return false; | |
| 561 } | |
| 562 layerIter1.next(); | |
| 563 layerIter2.next(); | |
| 564 } | |
| 565 if (!layerIter1.done()) { | |
| 566 return false; | |
| 567 } | |
| 568 if (!layerIter2.done()) { | |
| 569 return false; | |
| 570 } | |
| 571 return true; | |
| 572 } | |
| 573 }; | |
| 574 | |
| 575 static void AssertCanvasStatesEqual(skiatest::Reporter* reporter, const TestData
& d, | |
| 576 const SkCanvas* canvas1, const SkCanvas* can
vas2, | |
| 577 CanvasTestStep* testStep) { | |
| 578 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDeviceSize() == | |
| 579 canvas2->getDeviceSize(), testStep->assertMessage()); | |
| 580 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getSaveCount() == | |
| 581 canvas2->getSaveCount(), testStep->assertMessage()); | |
| 582 | |
| 583 SkRect bounds1, bounds2; | |
| 584 REPORTER_ASSERT_MESSAGE(reporter, | |
| 585 canvas1->getClipBounds(&bounds1) == canvas2->getClipBounds(&bounds2), | |
| 586 testStep->assertMessage()); | |
| 587 REPORTER_ASSERT_MESSAGE(reporter, bounds1 == bounds2, | |
| 588 testStep->assertMessage()); | |
| 589 | |
| 590 #ifdef SK_SUPPORT_LEGACY_DRAWFILTER | |
| 591 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getDrawFilter() == | |
| 592 canvas2->getDrawFilter(), testStep->assertMessage()); | |
| 593 #endif | |
| 594 | |
| 595 SkIRect deviceBounds1, deviceBounds2; | |
| 596 REPORTER_ASSERT_MESSAGE(reporter, | |
| 597 canvas1->getClipDeviceBounds(&deviceBounds1) == | |
| 598 canvas2->getClipDeviceBounds(&deviceBounds2), | |
| 599 testStep->assertMessage()); | |
| 600 REPORTER_ASSERT_MESSAGE(reporter, deviceBounds1 == deviceBounds2, testStep->
assertMessage()); | |
| 601 REPORTER_ASSERT_MESSAGE(reporter, canvas1->getTotalMatrix() == | |
| 602 canvas2->getTotalMatrix(), testStep->assertMessage()); | |
| 603 REPORTER_ASSERT_MESSAGE(reporter, equal_clips(*canvas1, *canvas2), testStep-
>assertMessage()); | |
| 604 | |
| 605 REPORTER_ASSERT_MESSAGE(reporter, | |
| 606 CanvasTestingAccess::SameState(canvas1, canvas2), | |
| 607 testStep->assertMessage()); | |
| 608 } | |
| 609 | |
| 610 static void TestPdfDevice(skiatest::Reporter* reporter, | |
| 611 const TestData& d, | |
| 612 CanvasTestStep* testStep) { | |
| 613 SkDynamicMemoryWStream outStream; | 507 SkDynamicMemoryWStream outStream; |
| 614 sk_sp<SkDocument> doc(SkDocument::MakePDF(&outStream)); | 508 sk_sp<SkDocument> doc(SkDocument::MakePDF(&outStream)); |
| 615 REPORTER_ASSERT(reporter, doc); | 509 REPORTER_ASSERT(reporter, doc); |
| 616 if (!doc) { | 510 if (!doc) { |
| 617 return; | 511 return; |
| 618 } | 512 } |
| 619 SkCanvas* canvas = doc->beginPage(SkIntToScalar(d.fWidth), | 513 SkCanvas* canvas = doc->beginPage(SkIntToScalar(d.fWidth), |
| 620 SkIntToScalar(d.fHeight)); | 514 SkIntToScalar(d.fHeight)); |
| 621 REPORTER_ASSERT(reporter, canvas); | 515 REPORTER_ASSERT(reporter, canvas); |
| 622 testStep->setAssertMessageFormat(kPdfAssertMessageFormat); | 516 step->setAssertMessageFormat(kPdfAssertMessageFormat); |
| 623 testStep->draw(canvas, d, reporter); | 517 step->draw(canvas, d, reporter); |
| 624 | 518 |
| 625 REPORTER_ASSERT(reporter, doc->close()); | 519 REPORTER_ASSERT(reporter, doc->close()); |
| 626 } | 520 } |
| 627 | 521 |
| 628 // unused | |
| 629 static void TestNWayCanvasStateConsistency( | |
| 630 skiatest::Reporter* reporter, | |
| 631 const TestData& d, | |
| 632 CanvasTestStep* testStep, | |
| 633 const SkCanvas& referenceCanvas) { | |
| 634 | |
| 635 SkBitmap indirectStore1; | |
| 636 createBitmap(&indirectStore1, 0xFFFFFFFF); | |
| 637 SkCanvas indirectCanvas1(indirectStore1); | |
| 638 | |
| 639 SkBitmap indirectStore2; | |
| 640 createBitmap(&indirectStore2, 0xFFFFFFFF); | |
| 641 SkCanvas indirectCanvas2(indirectStore2); | |
| 642 | |
| 643 SkISize canvasSize = referenceCanvas.getDeviceSize(); | |
| 644 SkNWayCanvas nWayCanvas(canvasSize.width(), canvasSize.height()); | |
| 645 nWayCanvas.addCanvas(&indirectCanvas1); | |
| 646 nWayCanvas.addCanvas(&indirectCanvas2); | |
| 647 | |
| 648 testStep->setAssertMessageFormat(kNWayDrawAssertMessageFormat); | |
| 649 testStep->draw(&nWayCanvas, d, reporter); | |
| 650 // Verify that the SkNWayCanvas reports consitent state | |
| 651 testStep->setAssertMessageFormat(kNWayStateAssertMessageFormat); | |
| 652 AssertCanvasStatesEqual(reporter, d, &nWayCanvas, &referenceCanvas, testStep
); | |
| 653 // Verify that the indirect canvases report consitent state | |
| 654 testStep->setAssertMessageFormat(kNWayIndirect1StateAssertMessageFormat); | |
| 655 AssertCanvasStatesEqual(reporter, d, &indirectCanvas1, &referenceCanvas, tes
tStep); | |
| 656 testStep->setAssertMessageFormat(kNWayIndirect2StateAssertMessageFormat); | |
| 657 AssertCanvasStatesEqual(reporter, d, &indirectCanvas2, &referenceCanvas, tes
tStep); | |
| 658 } | |
| 659 | |
| 660 /* | 522 /* |
| 661 * This sub-test verifies that the test step passes when executed | 523 * This sub-test verifies that the test step passes when executed |
| 662 * with SkCanvas and with classes derrived from SkCanvas. It also verifies | 524 * with SkCanvas and with classes derrived from SkCanvas. It also verifies |
| 663 * that the all canvas derivatives report the same state as an SkCanvas | 525 * that the all canvas derivatives report the same state as an SkCanvas |
| 664 * after having executed the test step. | 526 * after having executed the test step. |
| 665 */ | 527 */ |
| 666 static void TestOverrideStateConsistency(skiatest::Reporter* reporter, const Tes
tData& d, | 528 static void TestOverrideStateConsistency(skiatest::Reporter* reporter, const Tes
tData& d, |
| 667 CanvasTestStep* testStep) { | 529 CanvasTestStep* testStep) { |
| 668 SkBitmap referenceStore; | 530 SkBitmap referenceStore; |
| 669 createBitmap(&referenceStore, 0xFFFFFFFF); | 531 createBitmap(&referenceStore, 0xFFFFFFFF); |
| 670 SkCanvas referenceCanvas(referenceStore); | 532 SkCanvas referenceCanvas(referenceStore); |
| 671 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat); | 533 testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat); |
| 672 testStep->draw(&referenceCanvas, d, reporter); | 534 testStep->draw(&referenceCanvas, d, reporter); |
| 673 | 535 |
| 674 // The following test code is disabled because SkNWayCanvas does not | |
| 675 // report correct clipping and device bounds information | |
| 676 // Issue: http://code.google.com/p/skia/issues/detail?id=501 | |
| 677 | |
| 678 if (false) { // avoid bit rot, suppress warning | |
| 679 TestNWayCanvasStateConsistency(reporter, d, testStep, referenceCanvas); | |
| 680 } | |
| 681 | |
| 682 if (false) { // avoid bit rot, suppress warning | |
| 683 test_clipVisitor(reporter, &referenceCanvas); | |
| 684 } | |
| 685 test_clipstack(reporter); | 536 test_clipstack(reporter); |
| 686 } | 537 } |
| 687 | 538 |
| 688 static void test_newraster(skiatest::Reporter* reporter) { | 539 static void test_newraster(skiatest::Reporter* reporter) { |
| 689 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); | 540 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); |
| 690 const size_t minRowBytes = info.minRowBytes(); | 541 const size_t minRowBytes = info.minRowBytes(); |
| 691 const size_t size = info.getSafeSize(minRowBytes); | 542 const size_t size = info.getSafeSize(minRowBytes); |
| 692 SkAutoTMalloc<SkPMColor> storage(size); | 543 SkAutoTMalloc<SkPMColor> storage(size); |
| 693 SkPMColor* baseAddr = storage.get(); | 544 SkPMColor* baseAddr = storage.get(); |
| 694 sk_bzero(baseAddr, size); | 545 sk_bzero(baseAddr, size); |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 880 SkPaint paint; | 731 SkPaint paint; |
| 881 // paint.setShader(SkShader::MakeColorShader(SK_ColorRED)); | 732 // paint.setShader(SkShader::MakeColorShader(SK_ColorRED)); |
| 882 | 733 |
| 883 canvas.save(); | 734 canvas.save(); |
| 884 canvas.clipRect(SkRect::MakeWH(55, 55)); | 735 canvas.clipRect(SkRect::MakeWH(55, 55)); |
| 885 canvas.translate(10, 20); | 736 canvas.translate(10, 20); |
| 886 canvas.drawRect(SkRect::MakeWH(50, 50), paint); | 737 canvas.drawRect(SkRect::MakeWH(50, 50), paint); |
| 887 canvas.restore(); | 738 canvas.restore(); |
| 888 } | 739 } |
| 889 | 740 |
| OLD | NEW |