Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(534)

Side by Side Diff: tests/PictureTest.cpp

Issue 214953003: split SkPictureRecorder out of SkPicture (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: update to ToT (again) Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #include "SkBitmapDevice.h" 8 #include "SkBitmapDevice.h"
9 #include "SkCanvas.h" 9 #include "SkCanvas.h"
10 #include "SkColorPriv.h" 10 #include "SkColorPriv.h"
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 indices, 6, paint); 294 indices, 6, paint);
295 *usedPixRefs->append() = bm.pixelRef(); 295 *usedPixRefs->append() = bm.pixelRef();
296 } 296 }
297 297
298 // Return a picture with the bitmaps drawn at the specified positions. 298 // Return a picture with the bitmaps drawn at the specified positions.
299 static SkPicture* record_bitmaps(const SkBitmap bm[], 299 static SkPicture* record_bitmaps(const SkBitmap bm[],
300 const SkPoint pos[], 300 const SkPoint pos[],
301 SkTDArray<SkPixelRef*> analytic[], 301 SkTDArray<SkPixelRef*> analytic[],
302 int count, 302 int count,
303 DrawBitmapProc proc) { 303 DrawBitmapProc proc) {
304 SkPicture* pic = new SkPicture; 304 SkPictureRecorder recorder;
305 SkCanvas* canvas = pic->beginRecording(1000, 1000); 305 SkCanvas* canvas = recorder.beginRecording(1000, 1000);
306 for (int i = 0; i < count; ++i) { 306 for (int i = 0; i < count; ++i) {
307 analytic[i].rewind(); 307 analytic[i].rewind();
308 canvas->save(); 308 canvas->save();
309 SkRect clipRect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, 309 SkRect clipRect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY,
310 SkIntToScalar(bm[i].width()), 310 SkIntToScalar(bm[i].width()),
311 SkIntToScalar(bm[i].height())); 311 SkIntToScalar(bm[i].height()));
312 canvas->clipRect(clipRect, SkRegion::kIntersect_Op); 312 canvas->clipRect(clipRect, SkRegion::kIntersect_Op);
313 proc(canvas, bm[i], bm[count+i], pos[i], &analytic[i]); 313 proc(canvas, bm[i], bm[count+i], pos[i], &analytic[i]);
314 canvas->restore(); 314 canvas->restore();
315 } 315 }
316 pic->endRecording(); 316 return recorder.endRecording();
317 return pic;
318 } 317 }
319 318
320 static void rand_rect(SkRect* rect, SkRandom& rand, SkScalar W, SkScalar H) { 319 static void rand_rect(SkRect* rect, SkRandom& rand, SkScalar W, SkScalar H) {
321 rect->fLeft = rand.nextRangeScalar(-W, 2*W); 320 rect->fLeft = rand.nextRangeScalar(-W, 2*W);
322 rect->fTop = rand.nextRangeScalar(-H, 2*H); 321 rect->fTop = rand.nextRangeScalar(-H, 2*H);
323 rect->fRight = rect->fLeft + rand.nextRangeScalar(0, W); 322 rect->fRight = rect->fLeft + rand.nextRangeScalar(0, W);
324 rect->fBottom = rect->fTop + rand.nextRangeScalar(0, H); 323 rect->fBottom = rect->fTop + rand.nextRangeScalar(0, H);
325 324
326 // we integralize rect to make our tests more predictable, since Gather is 325 // we integralize rect to make our tests more predictable, since Gather is
327 // a little sloppy. 326 // a little sloppy.
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
650 REPORTER_ASSERT(reporter, -1 != gatheredRefs.find(fromAnalytic[i ])); 649 REPORTER_ASSERT(reporter, -1 != gatheredRefs.find(fromAnalytic[i ]));
651 } 650 }
652 } 651 }
653 } 652 }
654 } 653 }
655 654
656 #ifdef SK_DEBUG 655 #ifdef SK_DEBUG
657 // Ensure that deleting SkPicturePlayback does not assert. Asserts only fire in debug mode, so only 656 // Ensure that deleting SkPicturePlayback does not assert. Asserts only fire in debug mode, so only
658 // run in debug mode. 657 // run in debug mode.
659 static void test_deleting_empty_playback() { 658 static void test_deleting_empty_playback() {
660 SkPicture picture; 659 SkPictureRecorder recorder;
661 // Creates an SkPictureRecord 660 // Creates an SkPictureRecord
662 picture.beginRecording(0, 0); 661 recorder.beginRecording(0, 0);
663 // Turns that into an SkPicturePlayback 662 // Turns that into an SkPicturePlayback
664 picture.endRecording(); 663 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
665 // Deletes the old SkPicturePlayback, and creates a new SkPictureRecord 664 // Deletes the old SkPicturePlayback, and creates a new SkPictureRecord
666 picture.beginRecording(0, 0); 665 recorder.beginRecording(0, 0);
667 } 666 }
668 667
669 // Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode. 668 // Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode.
670 static void test_serializing_empty_picture() { 669 static void test_serializing_empty_picture() {
671 SkPicture picture; 670 SkPictureRecorder recorder;
672 picture.beginRecording(0, 0); 671 recorder.beginRecording(0, 0);
673 picture.endRecording(); 672 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
674 SkDynamicMemoryWStream stream; 673 SkDynamicMemoryWStream stream;
675 picture.serialize(&stream); 674 picture->serialize(&stream);
676 } 675 }
677 #endif 676 #endif
678 677
679 static void rand_op(SkCanvas* canvas, SkRandom& rand) { 678 static void rand_op(SkCanvas* canvas, SkRandom& rand) {
680 SkPaint paint; 679 SkPaint paint;
681 SkRect rect = SkRect::MakeWH(50, 50); 680 SkRect rect = SkRect::MakeWH(50, 50);
682 681
683 SkScalar unit = rand.nextUScalar1(); 682 SkScalar unit = rand.nextUScalar1();
684 if (unit <= 0.3) { 683 if (unit <= 0.3) {
685 // SkDebugf("save\n"); 684 // SkDebugf("save\n");
(...skipping 19 matching lines...) Expand all
705 704
706 static void test_unbalanced_save_restores(skiatest::Reporter* reporter) { 705 static void test_unbalanced_save_restores(skiatest::Reporter* reporter) {
707 SkCanvas testCanvas(100, 100); 706 SkCanvas testCanvas(100, 100);
708 set_canvas_to_save_count_4(&testCanvas); 707 set_canvas_to_save_count_4(&testCanvas);
709 708
710 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); 709 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
711 710
712 SkPaint paint; 711 SkPaint paint;
713 SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000); 712 SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000);
714 713
715 SkPicture extra_save_picture; 714 SkPictureRecorder recorder;
716 extra_save_picture.beginRecording(100, 100);
717 extra_save_picture.getRecordingCanvas()->save();
718 extra_save_picture.getRecordingCanvas()->translate(10, 10);
719 extra_save_picture.getRecordingCanvas()->drawRect(rect, paint);
720 extra_save_picture.getRecordingCanvas()->save();
721 extra_save_picture.getRecordingCanvas()->translate(10, 10);
722 extra_save_picture.getRecordingCanvas()->drawRect(rect, paint);
723 715
724 testCanvas.drawPicture(extra_save_picture); 716 {
725 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); 717 // Create picture with 2 unbalanced saves
718 SkCanvas* canvas = recorder.beginRecording(100, 100);
719 canvas->save();
720 canvas->translate(10, 10);
721 canvas->drawRect(rect, paint);
722 canvas->save();
723 canvas->translate(10, 10);
724 canvas->drawRect(rect, paint);
725 SkAutoTUnref<SkPicture> extraSavePicture(recorder.endRecording());
726
727 testCanvas.drawPicture(*extraSavePicture);
728 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
729 }
726 730
727 set_canvas_to_save_count_4(&testCanvas); 731 set_canvas_to_save_count_4(&testCanvas);
728 732
729 SkPicture extra_restore_picture; 733 {
730 extra_restore_picture.beginRecording(100, 100); 734 // Create picture with 2 unbalanced restores
731 extra_restore_picture.getRecordingCanvas()->save(); 735 SkCanvas* canvas = recorder.beginRecording(100, 100);
732 extra_restore_picture.getRecordingCanvas()->translate(10, 10); 736 canvas->save();
733 extra_restore_picture.getRecordingCanvas()->drawRect(rect, paint); 737 canvas->translate(10, 10);
734 extra_restore_picture.getRecordingCanvas()->save(); 738 canvas->drawRect(rect, paint);
735 extra_restore_picture.getRecordingCanvas()->translate(10, 10); 739 canvas->save();
736 extra_restore_picture.getRecordingCanvas()->drawRect(rect, paint); 740 canvas->translate(10, 10);
737 extra_restore_picture.getRecordingCanvas()->restore(); 741 canvas->drawRect(rect, paint);
738 extra_restore_picture.getRecordingCanvas()->restore(); 742 canvas->restore();
739 extra_restore_picture.getRecordingCanvas()->restore(); 743 canvas->restore();
740 extra_restore_picture.getRecordingCanvas()->restore(); 744 canvas->restore();
745 canvas->restore();
746 SkAutoTUnref<SkPicture> extraRestorePicture(recorder.endRecording());
741 747
742 testCanvas.drawPicture(extra_save_picture); 748 testCanvas.drawPicture(*extraRestorePicture);
743 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); 749 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
750 }
744 751
745 SkPicture no_save_picture; 752 set_canvas_to_save_count_4(&testCanvas);
746 extra_restore_picture.beginRecording(100, 100);
747 extra_restore_picture.getRecordingCanvas()->translate(10, 10);
748 extra_restore_picture.getRecordingCanvas()->drawRect(rect, paint);
749 753
750 testCanvas.drawPicture(extra_save_picture); 754 {
751 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); 755 SkCanvas* canvas = recorder.beginRecording(100, 100);
752 REPORTER_ASSERT(reporter, testCanvas.getTotalMatrix().isIdentity()); 756 canvas->translate(10, 10);
757 canvas->drawRect(rect, paint);
758 SkAutoTUnref<SkPicture> noSavePicture(recorder.endRecording());
759
760 testCanvas.drawPicture(*noSavePicture);
761 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount());
762 REPORTER_ASSERT(reporter, testCanvas.getTotalMatrix().isIdentity());
763 }
753 } 764 }
754 765
755 static void test_peephole() { 766 static void test_peephole() {
756 SkRandom rand; 767 SkRandom rand;
757 768
769 SkPictureRecorder recorder;
770
758 for (int j = 0; j < 100; j++) { 771 for (int j = 0; j < 100; j++) {
759 SkRandom rand2(rand); // remember the seed 772 SkRandom rand2(rand); // remember the seed
760 773
761 SkPicture picture; 774 SkCanvas* canvas = recorder.beginRecording(100, 100);
762 SkCanvas* canvas = picture.beginRecording(100, 100);
763 775
764 for (int i = 0; i < 1000; ++i) { 776 for (int i = 0; i < 1000; ++i) {
765 rand_op(canvas, rand); 777 rand_op(canvas, rand);
766 } 778 }
767 picture.endRecording(); 779 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
768 780
769 rand = rand2; 781 rand = rand2;
770 } 782 }
771 783
772 { 784 {
773 SkPicture picture; 785 SkCanvas* canvas = recorder.beginRecording(100, 100);
774 SkCanvas* canvas = picture.beginRecording(100, 100);
775 SkRect rect = SkRect::MakeWH(50, 50); 786 SkRect rect = SkRect::MakeWH(50, 50);
776 787
777 for (int i = 0; i < 100; ++i) { 788 for (int i = 0; i < 100; ++i) {
778 canvas->save(); 789 canvas->save();
779 } 790 }
780 while (canvas->getSaveCount() > 1) { 791 while (canvas->getSaveCount() > 1) {
781 canvas->clipRect(rect); 792 canvas->clipRect(rect);
782 canvas->restore(); 793 canvas->restore();
783 } 794 }
784 picture.endRecording(); 795 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
785 } 796 }
786 } 797 }
787 798
788 #ifndef SK_DEBUG 799 #ifndef SK_DEBUG
789 // Only test this is in release mode. We deliberately crash in debug mode, since a valid caller 800 // Only test this is in release mode. We deliberately crash in debug mode, since a valid caller
790 // should never do this. 801 // should never do this.
791 static void test_bad_bitmap() { 802 static void test_bad_bitmap() {
792 // This bitmap has a width and height but no pixels. As a result, attempting to record it will 803 // This bitmap has a width and height but no pixels. As a result, attempting to record it will
793 // fail. 804 // fail.
794 SkBitmap bm; 805 SkBitmap bm;
795 bm.setConfig(SkImageInfo::MakeN32Premul(100, 100)); 806 bm.setConfig(SkImageInfo::MakeN32Premul(100, 100));
796 SkPicture picture; 807 SkPictureRecorder recorder;
797 SkCanvas* recordingCanvas = picture.beginRecording(100, 100); 808 SkCanvas* recordingCanvas = recorder.beginRecording(100, 100);
798 recordingCanvas->drawBitmap(bm, 0, 0); 809 recordingCanvas->drawBitmap(bm, 0, 0);
799 picture.endRecording(); 810 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
800 811
801 SkCanvas canvas; 812 SkCanvas canvas;
802 canvas.drawPicture(picture); 813 canvas.drawPicture(*picture);
803 } 814 }
804 #endif 815 #endif
805 816
806 static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) { 817 static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) {
807 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100); 818 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100);
808 } 819 }
809 820
810 static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) { 821 static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) {
811 SkPicture picture; 822 SkPictureRecorder recorder;
812 SkCanvas* canvas = picture.beginRecording(bitmap.width(), bitmap.height()); 823 SkCanvas* canvas = recorder.beginRecording(bitmap.width(), bitmap.height());
813 canvas->drawBitmap(bitmap, 0, 0); 824 canvas->drawBitmap(bitmap, 0, 0);
825 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
826
814 SkDynamicMemoryWStream wStream; 827 SkDynamicMemoryWStream wStream;
815 picture.serialize(&wStream, &encode_bitmap_to_data); 828 picture->serialize(&wStream, &encode_bitmap_to_data);
816 return wStream.copyToData(); 829 return wStream.copyToData();
817 } 830 }
818 831
819 struct ErrorContext { 832 struct ErrorContext {
820 int fErrors; 833 int fErrors;
821 skiatest::Reporter* fReporter; 834 skiatest::Reporter* fReporter;
822 }; 835 };
823 836
824 static void assert_one_parse_error_cb(SkError error, void* context) { 837 static void assert_one_parse_error_cb(SkError error, void* context) {
825 ErrorContext* errorContext = static_cast<ErrorContext*>(context); 838 ErrorContext* errorContext = static_cast<ErrorContext*>(context);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
865 REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL); 878 REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL);
866 SkClearLastError(); 879 SkClearLastError();
867 SkSetErrorCallback(NULL, NULL); 880 SkSetErrorCallback(NULL, NULL);
868 } 881 }
869 882
870 static void test_clone_empty(skiatest::Reporter* reporter) { 883 static void test_clone_empty(skiatest::Reporter* reporter) {
871 // This is a regression test for crbug.com/172062 884 // This is a regression test for crbug.com/172062
872 // Before the fix, we used to crash accessing a null pointer when we 885 // Before the fix, we used to crash accessing a null pointer when we
873 // had a picture with no paints. This test passes by not crashing. 886 // had a picture with no paints. This test passes by not crashing.
874 { 887 {
875 SkPicture picture; 888 SkPictureRecorder recorder;
876 picture.beginRecording(1, 1); 889 recorder.beginRecording(1, 1);
877 picture.endRecording(); 890 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
878 SkPicture* destPicture = picture.clone(); 891 SkAutoTUnref<SkPicture> destPicture(picture->clone());
879 REPORTER_ASSERT(reporter, NULL != destPicture); 892 REPORTER_ASSERT(reporter, NULL != destPicture);
880 destPicture->unref();
881 }
882 {
883 // Test without call to endRecording
884 SkPicture picture;
885 picture.beginRecording(1, 1);
886 SkPicture* destPicture = picture.clone();
887 REPORTER_ASSERT(reporter, NULL != destPicture);
888 destPicture->unref();
889 } 893 }
890 } 894 }
891 895
892 static void test_draw_empty(skiatest::Reporter* reporter) { 896 static void test_draw_empty(skiatest::Reporter* reporter) {
893 SkBitmap result; 897 SkBitmap result;
894 make_bm(&result, 2, 2, SK_ColorBLACK, false); 898 make_bm(&result, 2, 2, SK_ColorBLACK, false);
895 899
896 SkCanvas canvas(result); 900 SkCanvas canvas(result);
897 901
898 { 902 {
899 // stock SkPicture 903 // stock SkPicture
900 SkPicture picture; 904 SkPictureRecorder recorder;
901 picture.beginRecording(1, 1); 905 recorder.beginRecording(1, 1);
902 picture.endRecording(); 906 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
903 907
904 canvas.drawPicture(picture); 908 canvas.drawPicture(*picture);
905 } 909 }
906 910
907 { 911 {
908 // tile grid 912 // tile grid
909 SkTileGridPicture::TileGridInfo gridInfo; 913 SkTileGridPicture::TileGridInfo gridInfo;
910 gridInfo.fMargin.setEmpty(); 914 gridInfo.fMargin.setEmpty();
911 gridInfo.fOffset.setZero(); 915 gridInfo.fOffset.setZero();
912 gridInfo.fTileInterval.set(1, 1); 916 gridInfo.fTileInterval.set(1, 1);
913 917
914 SkTileGridPicture picture(1, 1, gridInfo); 918 SkAutoTUnref<SkPictureFactory> factory(SkNEW_ARGS(SkTileGridPictureFacto ry, (gridInfo)));
915 picture.beginRecording(1, 1, SkPicture::kOptimizeForClippedPlayback_Reco rdingFlag); 919 SkPictureRecorder recorder(factory);
916 picture.endRecording(); 920 recorder.beginRecording(1, 1, SkPicture::kOptimizeForClippedPlayback_Rec ordingFlag);
921 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
917 922
918 canvas.drawPicture(picture); 923 canvas.drawPicture(*picture);
919 } 924 }
920 925
921 { 926 {
922 // RTree 927 // RTree
923 SkPicture picture; 928 SkPictureRecorder recorder;
924 picture.beginRecording(1, 1, SkPicture::kOptimizeForClippedPlayback_Reco rdingFlag); 929 recorder.beginRecording(1, 1, SkPicture::kOptimizeForClippedPlayback_Rec ordingFlag);
925 picture.endRecording(); 930 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
926 931
927 canvas.drawPicture(picture); 932 canvas.drawPicture(*picture);
928 } 933 }
929 934
930 { 935 {
931 // quad tree 936 // quad tree
932 SkQuadTreePicture picture(SkIRect::MakeWH(1, 1)); 937 SkAutoTUnref<SkPictureFactory> factory(SkNEW(SkQuadTreePictureFactory));
933 picture.beginRecording(1, 1, SkPicture::kOptimizeForClippedPlayback_Reco rdingFlag); 938 SkPictureRecorder recorder(factory);
934 picture.endRecording(); 939 recorder.beginRecording(1, 1, SkPicture::kOptimizeForClippedPlayback_Rec ordingFlag);
940 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
935 941
936 canvas.drawPicture(picture); 942 canvas.drawPicture(*picture);
937 } 943 }
938 } 944 }
939 945
940 static void test_clip_bound_opt(skiatest::Reporter* reporter) { 946 static void test_clip_bound_opt(skiatest::Reporter* reporter) {
941 // Test for crbug.com/229011 947 // Test for crbug.com/229011
942 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4), 948 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4),
943 SkIntToScalar(2), SkIntToScalar(2)); 949 SkIntToScalar(2), SkIntToScalar(2));
944 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7), 950 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7),
945 SkIntToScalar(1), SkIntToScalar(1)); 951 SkIntToScalar(1), SkIntToScalar(1));
946 SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6), 952 SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6),
947 SkIntToScalar(1), SkIntToScalar(1)); 953 SkIntToScalar(1), SkIntToScalar(1));
948 954
949 SkPath invPath; 955 SkPath invPath;
950 invPath.addOval(rect1); 956 invPath.addOval(rect1);
951 invPath.setFillType(SkPath::kInverseEvenOdd_FillType); 957 invPath.setFillType(SkPath::kInverseEvenOdd_FillType);
952 SkPath path; 958 SkPath path;
953 path.addOval(rect2); 959 path.addOval(rect2);
954 SkPath path2; 960 SkPath path2;
955 path2.addOval(rect3); 961 path2.addOval(rect3);
956 SkIRect clipBounds; 962 SkIRect clipBounds;
963 SkPictureRecorder recorder;
957 // Minimalist test set for 100% code coverage of 964 // Minimalist test set for 100% code coverage of
958 // SkPictureRecord::updateClipConservativelyUsingBounds 965 // SkPictureRecord::updateClipConservativelyUsingBounds
959 { 966 {
960 SkPicture picture; 967 SkCanvas* canvas = recorder.beginRecording(10, 10,
961 SkCanvas* canvas = picture.beginRecording(10, 10,
962 SkPicture::kUsePathBoundsForClip_RecordingFlag); 968 SkPicture::kUsePathBoundsForClip_RecordingFlag);
963 canvas->clipPath(invPath, SkRegion::kIntersect_Op); 969 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
964 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); 970 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
965 REPORTER_ASSERT(reporter, true == nonEmpty); 971 REPORTER_ASSERT(reporter, true == nonEmpty);
966 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); 972 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
967 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); 973 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
968 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); 974 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
969 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); 975 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
970 } 976 }
971 { 977 {
972 SkPicture picture; 978 SkCanvas* canvas = recorder.beginRecording(10, 10,
973 SkCanvas* canvas = picture.beginRecording(10, 10,
974 SkPicture::kUsePathBoundsForClip_RecordingFlag); 979 SkPicture::kUsePathBoundsForClip_RecordingFlag);
975 canvas->clipPath(path, SkRegion::kIntersect_Op); 980 canvas->clipPath(path, SkRegion::kIntersect_Op);
976 canvas->clipPath(invPath, SkRegion::kIntersect_Op); 981 canvas->clipPath(invPath, SkRegion::kIntersect_Op);
977 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); 982 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
978 REPORTER_ASSERT(reporter, true == nonEmpty); 983 REPORTER_ASSERT(reporter, true == nonEmpty);
979 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); 984 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
980 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); 985 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
981 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); 986 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
982 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); 987 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
983 } 988 }
984 { 989 {
985 SkPicture picture; 990 SkCanvas* canvas = recorder.beginRecording(10, 10,
986 SkCanvas* canvas = picture.beginRecording(10, 10,
987 SkPicture::kUsePathBoundsForClip_RecordingFlag); 991 SkPicture::kUsePathBoundsForClip_RecordingFlag);
988 canvas->clipPath(path, SkRegion::kIntersect_Op); 992 canvas->clipPath(path, SkRegion::kIntersect_Op);
989 canvas->clipPath(invPath, SkRegion::kUnion_Op); 993 canvas->clipPath(invPath, SkRegion::kUnion_Op);
990 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); 994 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
991 REPORTER_ASSERT(reporter, true == nonEmpty); 995 REPORTER_ASSERT(reporter, true == nonEmpty);
992 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); 996 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
993 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); 997 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
994 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); 998 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
995 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); 999 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
996 } 1000 }
997 { 1001 {
998 SkPicture picture; 1002 SkCanvas* canvas = recorder.beginRecording(10, 10,
999 SkCanvas* canvas = picture.beginRecording(10, 10,
1000 SkPicture::kUsePathBoundsForClip_RecordingFlag); 1003 SkPicture::kUsePathBoundsForClip_RecordingFlag);
1001 canvas->clipPath(path, SkRegion::kDifference_Op); 1004 canvas->clipPath(path, SkRegion::kDifference_Op);
1002 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); 1005 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1003 REPORTER_ASSERT(reporter, true == nonEmpty); 1006 REPORTER_ASSERT(reporter, true == nonEmpty);
1004 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); 1007 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft);
1005 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); 1008 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop);
1006 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); 1009 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom);
1007 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); 1010 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight);
1008 } 1011 }
1009 { 1012 {
1010 SkPicture picture; 1013 SkCanvas* canvas = recorder.beginRecording(10, 10,
1011 SkCanvas* canvas = picture.beginRecording(10, 10,
1012 SkPicture::kUsePathBoundsForClip_RecordingFlag); 1014 SkPicture::kUsePathBoundsForClip_RecordingFlag);
1013 canvas->clipPath(path, SkRegion::kReverseDifference_Op); 1015 canvas->clipPath(path, SkRegion::kReverseDifference_Op);
1014 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); 1016 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1015 // True clip is actually empty in this case, but the best 1017 // True clip is actually empty in this case, but the best
1016 // determination we can make using only bounds as input is that the 1018 // determination we can make using only bounds as input is that the
1017 // clip is included in the bounds of 'path'. 1019 // clip is included in the bounds of 'path'.
1018 REPORTER_ASSERT(reporter, true == nonEmpty); 1020 REPORTER_ASSERT(reporter, true == nonEmpty);
1019 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); 1021 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft);
1020 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); 1022 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop);
1021 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); 1023 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1022 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); 1024 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1023 } 1025 }
1024 { 1026 {
1025 SkPicture picture; 1027 SkCanvas* canvas = recorder.beginRecording(10, 10,
1026 SkCanvas* canvas = picture.beginRecording(10, 10,
1027 SkPicture::kUsePathBoundsForClip_RecordingFlag); 1028 SkPicture::kUsePathBoundsForClip_RecordingFlag);
1028 canvas->clipPath(path, SkRegion::kIntersect_Op); 1029 canvas->clipPath(path, SkRegion::kIntersect_Op);
1029 canvas->clipPath(path2, SkRegion::kXOR_Op); 1030 canvas->clipPath(path2, SkRegion::kXOR_Op);
1030 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); 1031 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds);
1031 REPORTER_ASSERT(reporter, true == nonEmpty); 1032 REPORTER_ASSERT(reporter, true == nonEmpty);
1032 REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft); 1033 REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft);
1033 REPORTER_ASSERT(reporter, 6 == clipBounds.fTop); 1034 REPORTER_ASSERT(reporter, 6 == clipBounds.fTop);
1034 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); 1035 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom);
1035 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); 1036 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight);
1036 } 1037 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1074 1075
1075 unsigned getClipCount() const { return fClipCount; } 1076 unsigned getClipCount() const { return fClipCount; }
1076 1077
1077 private: 1078 private:
1078 unsigned fClipCount; 1079 unsigned fClipCount;
1079 1080
1080 typedef SkCanvas INHERITED; 1081 typedef SkCanvas INHERITED;
1081 }; 1082 };
1082 1083
1083 static void test_clip_expansion(skiatest::Reporter* reporter) { 1084 static void test_clip_expansion(skiatest::Reporter* reporter) {
1084 SkPicture picture; 1085 SkPictureRecorder recorder;
1085 SkCanvas* canvas = picture.beginRecording(10, 10, 0); 1086 SkCanvas* canvas = recorder.beginRecording(10, 10, 0);
1086 1087
1087 canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op); 1088 canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op);
1088 // The following expanding clip should not be skipped. 1089 // The following expanding clip should not be skipped.
1089 canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op); 1090 canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op);
1090 // Draw something so the optimizer doesn't just fold the world. 1091 // Draw something so the optimizer doesn't just fold the world.
1091 SkPaint p; 1092 SkPaint p;
1092 p.setColor(SK_ColorBLUE); 1093 p.setColor(SK_ColorBLUE);
1093 canvas->drawPaint(p); 1094 canvas->drawPaint(p);
1095 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1094 1096
1095 ClipCountingCanvas testCanvas(10, 10); 1097 ClipCountingCanvas testCanvas(10, 10);
1096 picture.draw(&testCanvas); 1098 picture->draw(&testCanvas);
1097 1099
1098 // Both clips should be present on playback. 1100 // Both clips should be present on playback.
1099 REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2); 1101 REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2);
1100 } 1102 }
1101 1103
1102 static void test_hierarchical(skiatest::Reporter* reporter) { 1104 static void test_hierarchical(skiatest::Reporter* reporter) {
1103 SkBitmap bm; 1105 SkBitmap bm;
1104 make_bm(&bm, 10, 10, SK_ColorRED, true); 1106 make_bm(&bm, 10, 10, SK_ColorRED, true);
1105 1107
1106 SkCanvas* canvas; 1108 SkPictureRecorder recorder;
1107 1109
1108 SkPicture childPlain; 1110 recorder.beginRecording(10, 10);
1109 childPlain.beginRecording(10, 10); 1111 SkAutoTUnref<SkPicture> childPlain(recorder.endRecording());
1110 childPlain.endRecording(); 1112 REPORTER_ASSERT(reporter, !childPlain->willPlayBackBitmaps()); // 0
1111 REPORTER_ASSERT(reporter, !childPlain.willPlayBackBitmaps()); // 0
1112 1113
1113 SkPicture childWithBitmap; 1114 recorder.beginRecording(10, 10)->drawBitmap(bm, 0, 0);
1114 childWithBitmap.beginRecording(10, 10)->drawBitmap(bm, 0, 0); 1115 SkAutoTUnref<SkPicture> childWithBitmap(recorder.endRecording());
1115 childWithBitmap.endRecording(); 1116 REPORTER_ASSERT(reporter, childWithBitmap->willPlayBackBitmaps()); // 1
1116 REPORTER_ASSERT(reporter, childWithBitmap.willPlayBackBitmaps()); // 1
1117 1117
1118 SkPicture parentPP; 1118 {
1119 canvas = parentPP.beginRecording(10, 10); 1119 SkCanvas* canvas = recorder.beginRecording(10, 10);
1120 canvas->drawPicture(childPlain); 1120 canvas->drawPicture(*childPlain);
1121 parentPP.endRecording(); 1121 SkAutoTUnref<SkPicture> parentPP(recorder.endRecording());
1122 REPORTER_ASSERT(reporter, !parentPP.willPlayBackBitmaps()); // 0 1122 REPORTER_ASSERT(reporter, !parentPP->willPlayBackBitmaps()); // 0
1123 1123 }
1124 SkPicture parentPWB; 1124 {
1125 canvas = parentPWB.beginRecording(10, 10); 1125 SkCanvas* canvas = recorder.beginRecording(10, 10);
1126 canvas->drawPicture(childWithBitmap); 1126 canvas->drawPicture(*childWithBitmap);
1127 parentPWB.endRecording(); 1127 SkAutoTUnref<SkPicture> parentPWB(recorder.endRecording());
1128 REPORTER_ASSERT(reporter, parentPWB.willPlayBackBitmaps()); // 1 1128 REPORTER_ASSERT(reporter, parentPWB->willPlayBackBitmaps()); // 1
1129 1129 }
1130 SkPicture parentWBP; 1130 {
1131 canvas = parentWBP.beginRecording(10, 10); 1131 SkCanvas* canvas = recorder.beginRecording(10, 10);
1132 canvas->drawBitmap(bm, 0, 0); 1132 canvas->drawBitmap(bm, 0, 0);
1133 canvas->drawPicture(childPlain); 1133 canvas->drawPicture(*childPlain);
1134 parentWBP.endRecording(); 1134 SkAutoTUnref<SkPicture> parentWBP(recorder.endRecording());
1135 REPORTER_ASSERT(reporter, parentWBP.willPlayBackBitmaps()); // 1 1135 REPORTER_ASSERT(reporter, parentWBP->willPlayBackBitmaps()); // 1
1136 1136 }
1137 SkPicture parentWBWB; 1137 {
1138 canvas = parentWBWB.beginRecording(10, 10); 1138 SkCanvas* canvas = recorder.beginRecording(10, 10);
1139 canvas->drawBitmap(bm, 0, 0); 1139 canvas->drawBitmap(bm, 0, 0);
1140 canvas->drawPicture(childWithBitmap); 1140 canvas->drawPicture(*childWithBitmap);
1141 parentWBWB.endRecording(); 1141 SkAutoTUnref<SkPicture> parentWBWB(recorder.endRecording());
1142 REPORTER_ASSERT(reporter, parentWBWB.willPlayBackBitmaps()); // 2 1142 REPORTER_ASSERT(reporter, parentWBWB->willPlayBackBitmaps()); // 2
1143 }
1143 } 1144 }
1144 1145
1145 static void test_gen_id(skiatest::Reporter* reporter) { 1146 static void test_gen_id(skiatest::Reporter* reporter) {
1146 1147
1147 SkPicture hasData, empty, midRecord; 1148 SkPicture empty;
1148 1149
1149 uint32_t beforeID = hasData.uniqueID(); 1150 // Empty pictures should still have a valid ID
1150 REPORTER_ASSERT(reporter, SK_InvalidGenID != beforeID); 1151 REPORTER_ASSERT(reporter, empty.uniqueID() != SK_InvalidGenID);
1151 1152
1152 // all 3 pictures should have different ids 1153 SkPictureRecorder recorder;
1153 REPORTER_ASSERT(reporter, beforeID != empty.uniqueID());
1154 REPORTER_ASSERT(reporter, beforeID != midRecord.uniqueID());
1155 REPORTER_ASSERT(reporter, empty.uniqueID() != midRecord.uniqueID());
1156 1154
1157 hasData.beginRecording(1, 1); 1155 SkCanvas* canvas = recorder.beginRecording(1, 1);
1158 // gen ID should be invalid mid-record 1156 canvas->drawARGB(255, 255, 255, 255);
1159 REPORTER_ASSERT(reporter, SK_InvalidGenID == hasData.uniqueID()); 1157 SkAutoTUnref<SkPicture> hasData(recorder.endRecording());
1160 hasData.endRecording(); 1158 // picture should have a non-zero id after recording
1161 // picture should get a new (non-zero) id after recording 1159 REPORTER_ASSERT(reporter, hasData->uniqueID() != SK_InvalidGenID);
1162 REPORTER_ASSERT(reporter, hasData.uniqueID() != beforeID);
1163 REPORTER_ASSERT(reporter, hasData.uniqueID() != SK_InvalidGenID);
1164 1160
1165 midRecord.beginRecording(1, 1); 1161 // both pictures should have different ids
1166 REPORTER_ASSERT(reporter, SK_InvalidGenID == midRecord.uniqueID()); 1162 REPORTER_ASSERT(reporter, hasData->uniqueID() != empty.uniqueID());
1167 1163
1168 // test out copy constructor 1164 // test out copy constructor
1169 SkPicture copyWithData(hasData); 1165 SkPicture copyWithData(*hasData);
1170 REPORTER_ASSERT(reporter, hasData.uniqueID() == copyWithData.uniqueID()); 1166 REPORTER_ASSERT(reporter, hasData->uniqueID() == copyWithData.uniqueID());
1171 1167
1172 SkPicture emptyCopy(empty); 1168 SkPicture emptyCopy(empty);
1173 REPORTER_ASSERT(reporter, empty.uniqueID() != emptyCopy.uniqueID()); 1169 REPORTER_ASSERT(reporter, empty.uniqueID() != emptyCopy.uniqueID());
1174 1170
1175 SkPicture copyMidRecord(midRecord);
1176 REPORTER_ASSERT(reporter, midRecord.uniqueID() != copyMidRecord.uniqueID());
1177 REPORTER_ASSERT(reporter, copyMidRecord.uniqueID() != SK_InvalidGenID);
1178
1179 // test out swap 1171 // test out swap
1180 beforeID = copyMidRecord.uniqueID(); 1172 {
1181 copyWithData.swap(copyMidRecord); 1173 SkPicture swapWithData;
1182 REPORTER_ASSERT(reporter, copyWithData.uniqueID() == beforeID); 1174 uint32_t beforeID1 = swapWithData.uniqueID();
1183 REPORTER_ASSERT(reporter, copyMidRecord.uniqueID() == hasData.uniqueID()); 1175 uint32_t beforeID2 = copyWithData.uniqueID();
1176 swapWithData.swap(copyWithData);
1177 REPORTER_ASSERT(reporter, copyWithData.uniqueID() == beforeID1);
1178 REPORTER_ASSERT(reporter, swapWithData.uniqueID() == beforeID2);
1179 }
1184 1180
1185 // test out clone 1181 // test out clone
1186 SkAutoTUnref<SkPicture> cloneWithData(hasData.clone()); 1182 {
1187 REPORTER_ASSERT(reporter, hasData.uniqueID() == cloneWithData->uniqueID()); 1183 SkAutoTUnref<SkPicture> cloneWithData(hasData->clone());
1184 REPORTER_ASSERT(reporter, hasData->uniqueID() == cloneWithData->uniqueID ());
1188 1185
1189 SkAutoTUnref<SkPicture> emptyClone(empty.clone()); 1186 SkAutoTUnref<SkPicture> emptyClone(empty.clone());
1190 REPORTER_ASSERT(reporter, empty.uniqueID() != emptyClone->uniqueID()); 1187 REPORTER_ASSERT(reporter, empty.uniqueID() != emptyClone->uniqueID());
1191 1188 }
1192 SkAutoTUnref<SkPicture> cloneMidRecord(midRecord.clone());
1193 REPORTER_ASSERT(reporter, midRecord.uniqueID() != cloneMidRecord->uniqueID() );
1194 REPORTER_ASSERT(reporter, cloneMidRecord->uniqueID() != SK_InvalidGenID);
1195 } 1189 }
1196 1190
1197 DEF_TEST(Picture, reporter) { 1191 DEF_TEST(Picture, reporter) {
1198 #ifdef SK_DEBUG 1192 #ifdef SK_DEBUG
1199 test_deleting_empty_playback(); 1193 test_deleting_empty_playback();
1200 test_serializing_empty_picture(); 1194 test_serializing_empty_picture();
1201 #else 1195 #else
1202 test_bad_bitmap(); 1196 test_bad_bitmap();
1203 #endif 1197 #endif
1204 test_unbalanced_save_restores(reporter); 1198 test_unbalanced_save_restores(reporter);
(...skipping 23 matching lines...) Expand all
1228 } 1222 }
1229 1223
1230 static void test_draw_bitmaps(SkCanvas* canvas) { 1224 static void test_draw_bitmaps(SkCanvas* canvas) {
1231 SkBitmap empty; 1225 SkBitmap empty;
1232 draw_bitmaps(empty, canvas); 1226 draw_bitmaps(empty, canvas);
1233 empty.setConfig(SkImageInfo::MakeN32Premul(10, 10)); 1227 empty.setConfig(SkImageInfo::MakeN32Premul(10, 10));
1234 draw_bitmaps(empty, canvas); 1228 draw_bitmaps(empty, canvas);
1235 } 1229 }
1236 1230
1237 DEF_TEST(Picture_EmptyBitmap, r) { 1231 DEF_TEST(Picture_EmptyBitmap, r) {
1238 SkPicture picture; 1232 SkPictureRecorder recorder;
1239 test_draw_bitmaps(picture.beginRecording(10, 10)); 1233 test_draw_bitmaps(recorder.beginRecording(10, 10));
1240 picture.endRecording(); 1234 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
1241 } 1235 }
1242 1236
1243 DEF_TEST(Canvas_EmptyBitmap, r) { 1237 DEF_TEST(Canvas_EmptyBitmap, r) {
1244 SkBitmap dst; 1238 SkBitmap dst;
1245 dst.allocN32Pixels(10, 10); 1239 dst.allocN32Pixels(10, 10);
1246 SkCanvas canvas(dst); 1240 SkCanvas canvas(dst);
1247 1241
1248 test_draw_bitmaps(&canvas); 1242 test_draw_bitmaps(&canvas);
1249 } 1243 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698