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 #include "SkBitmapDevice.h" | 8 #include "SkBitmapDevice.h" |
9 #include "SkBlurImageFilter.h" | 9 #include "SkBlurImageFilter.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
(...skipping 898 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
909 | 909 |
910 #endif | 910 #endif |
911 | 911 |
912 static void set_canvas_to_save_count_4(SkCanvas* canvas) { | 912 static void set_canvas_to_save_count_4(SkCanvas* canvas) { |
913 canvas->restoreToCount(1); | 913 canvas->restoreToCount(1); |
914 canvas->save(); | 914 canvas->save(); |
915 canvas->save(); | 915 canvas->save(); |
916 canvas->save(); | 916 canvas->save(); |
917 } | 917 } |
918 | 918 |
| 919 #ifdef SK_BUILD_FOR_ANDROID |
| 920 /** |
| 921 * A canvas that records the number of saves, saveLayers and restores. |
| 922 */ |
| 923 class SaveCountingCanvas : public SkCanvas { |
| 924 public: |
| 925 SaveCountingCanvas(int width, int height) |
| 926 : INHERITED(width, height) |
| 927 , fSaveCount(0) |
| 928 , fSaveLayerCount(0) |
| 929 , fRestoreCount(0){ |
| 930 } |
| 931 |
| 932 virtual SaveLayerStrategy willSaveLayer(const SkRect* bounds, const SkPaint*
paint, |
| 933 SaveFlags flags) SK_OVERRIDE { |
| 934 ++fSaveLayerCount; |
| 935 return this->INHERITED::willSaveLayer(bounds, paint, flags); |
| 936 } |
| 937 |
| 938 virtual void willSave(SaveFlags flags) SK_OVERRIDE { |
| 939 ++fSaveCount; |
| 940 this->INHERITED::willSave(flags); |
| 941 } |
| 942 |
| 943 virtual void willRestore() SK_OVERRIDE { |
| 944 ++fRestoreCount; |
| 945 this->INHERITED::willRestore(); |
| 946 } |
| 947 |
| 948 unsigned int getSaveCount() const { return fSaveCount; } |
| 949 unsigned int getSaveLayerCount() const { return fSaveLayerCount; } |
| 950 unsigned int getRestoreCount() const { return fRestoreCount; } |
| 951 |
| 952 private: |
| 953 unsigned int fSaveCount; |
| 954 unsigned int fSaveLayerCount; |
| 955 unsigned int fRestoreCount; |
| 956 |
| 957 typedef SkCanvas INHERITED; |
| 958 }; |
| 959 |
| 960 void check_save_state(skiatest::Reporter* reporter, SkPicture* picture, |
| 961 unsigned int numSaves, unsigned int numSaveLayers, |
| 962 unsigned int numRestores) { |
| 963 SaveCountingCanvas canvas(picture->width(), picture->height()); |
| 964 |
| 965 picture->draw(&canvas); |
| 966 |
| 967 REPORTER_ASSERT(reporter, numSaves == canvas.getSaveCount()); |
| 968 REPORTER_ASSERT(reporter, numSaveLayers == canvas.getSaveLayerCount()); |
| 969 REPORTER_ASSERT(reporter, numRestores == canvas.getRestoreCount()); |
| 970 } |
| 971 |
| 972 // This class exists so SkPicture can friend it and give it access to |
| 973 // the 'partialReplay' method. |
| 974 class SkPictureRecorderReplayTester { |
| 975 public: |
| 976 static SkPicture* Copy(SkPictureRecorder* recorder) { |
| 977 SkPictureRecorder recorder2; |
| 978 |
| 979 SkCanvas* canvas = recorder2.beginRecording(10, 10, NULL, 0); |
| 980 |
| 981 recorder->partialReplay(canvas); |
| 982 |
| 983 return recorder2.endRecording(); |
| 984 } |
| 985 }; |
| 986 |
| 987 // Test out SkPictureRecorder::partialReplay |
| 988 DEF_TEST(PictureRecorder_replay, reporter) { |
| 989 // check save/saveLayer state |
| 990 { |
| 991 SkPictureRecorder recorder; |
| 992 |
| 993 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0); |
| 994 |
| 995 canvas->saveLayer(NULL, NULL); |
| 996 |
| 997 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&record
er)); |
| 998 |
| 999 // The extra save and restore comes from the Copy process. |
| 1000 check_save_state(reporter, copy, 2, 1, 3); |
| 1001 |
| 1002 canvas->saveLayer(NULL, NULL); |
| 1003 |
| 1004 SkAutoTUnref<SkPicture> final(recorder.endRecording()); |
| 1005 |
| 1006 check_save_state(reporter, final, 1, 2, 3); |
| 1007 |
| 1008 // The copy shouldn't pick up any operations added after it was made |
| 1009 check_save_state(reporter, copy, 2, 1, 3); |
| 1010 } |
| 1011 |
| 1012 // (partially) check leakage of draw ops |
| 1013 { |
| 1014 SkPictureRecorder recorder; |
| 1015 |
| 1016 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0); |
| 1017 |
| 1018 SkRect r = SkRect::MakeWH(5, 5); |
| 1019 SkPaint p; |
| 1020 |
| 1021 canvas->drawRect(r, p); |
| 1022 |
| 1023 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&record
er)); |
| 1024 |
| 1025 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps()); |
| 1026 |
| 1027 SkBitmap bm; |
| 1028 make_bm(&bm, 10, 10, SK_ColorRED, true); |
| 1029 |
| 1030 r.offset(5.0f, 5.0f); |
| 1031 canvas->drawBitmapRectToRect(bm, NULL, r); |
| 1032 |
| 1033 SkAutoTUnref<SkPicture> final(recorder.endRecording()); |
| 1034 REPORTER_ASSERT(reporter, final->willPlayBackBitmaps()); |
| 1035 |
| 1036 REPORTER_ASSERT(reporter, copy->uniqueID() != final->uniqueID()); |
| 1037 |
| 1038 // The snapshot shouldn't pick up any operations added after it was made |
| 1039 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps()); |
| 1040 } |
| 1041 } |
| 1042 #endif |
| 1043 |
919 static void test_unbalanced_save_restores(skiatest::Reporter* reporter) { | 1044 static void test_unbalanced_save_restores(skiatest::Reporter* reporter) { |
920 SkCanvas testCanvas(100, 100); | 1045 SkCanvas testCanvas(100, 100); |
921 set_canvas_to_save_count_4(&testCanvas); | 1046 set_canvas_to_save_count_4(&testCanvas); |
922 | 1047 |
923 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); | 1048 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
924 | 1049 |
925 SkPaint paint; | 1050 SkPaint paint; |
926 SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000); | 1051 SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000); |
927 | 1052 |
928 SkPictureRecorder recorder; | 1053 SkPictureRecorder recorder; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
987 SkPicture p; | 1112 SkPicture p; |
988 | 1113 |
989 SkCanvas* canvas = p.beginRecording(100, 100); | 1114 SkCanvas* canvas = p.beginRecording(100, 100); |
990 for (int i = 0; i < 4; ++i) { | 1115 for (int i = 0; i < 4; ++i) { |
991 canvas->save(); | 1116 canvas->save(); |
992 } | 1117 } |
993 SkRect r = SkRect::MakeWH(50, 50); | 1118 SkRect r = SkRect::MakeWH(50, 50); |
994 SkPaint paint; | 1119 SkPaint paint; |
995 canvas->drawRect(r, paint); | 1120 canvas->drawRect(r, paint); |
996 | 1121 |
997 // Copying a mid-recording picture could result in unbalanced saves/rest
ores | 1122 // Check that copying a mid-recording picture does not result in unbalan
ced saves/restores |
998 SkPicture p2(p); | 1123 SkPicture p2(p); |
999 | 1124 |
1000 testCanvas.drawPicture(p2); | 1125 testCanvas.drawPicture(p2); |
1001 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); | 1126 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
1002 set_canvas_to_save_count_4(&testCanvas); | 1127 set_canvas_to_save_count_4(&testCanvas); |
1003 | 1128 |
1004 // Cloning a mid-recording picture could result in unbalanced saves/rest
ores | 1129 // Check that cloning a mid-recording picture does not result in unbalan
ced saves/restores |
1005 SkAutoTUnref<SkPicture> p3(p.clone()); | 1130 SkAutoTUnref<SkPicture> p3(p.clone()); |
1006 testCanvas.drawPicture(*p3); | 1131 testCanvas.drawPicture(*p3); |
1007 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); | 1132 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
1008 set_canvas_to_save_count_4(&testCanvas); | 1133 set_canvas_to_save_count_4(&testCanvas); |
1009 | 1134 |
1010 // Serializing a mid-recording picture could result in unbalanced saves/
restores | 1135 // Check that serializing a mid-recording picture doesn't result in unba
lanced |
| 1136 // saves/restores |
1011 SkDynamicMemoryWStream wStream; | 1137 SkDynamicMemoryWStream wStream; |
1012 p.serialize(&wStream); | 1138 p.serialize(&wStream); |
1013 SkAutoDataUnref data(wStream.copyToData()); | 1139 SkAutoDataUnref data(wStream.copyToData()); |
1014 SkMemoryStream stream(data); | 1140 SkMemoryStream stream(data); |
1015 SkAutoTUnref<SkPicture> p4(SkPicture::CreateFromStream(&stream, NULL)); | 1141 SkAutoTUnref<SkPicture> p4(SkPicture::CreateFromStream(&stream, NULL)); |
1016 testCanvas.drawPicture(*p4); | 1142 testCanvas.drawPicture(*p4); |
1017 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); | 1143 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
1018 } | 1144 } |
1019 #endif | 1145 #endif |
1020 } | 1146 } |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1292 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); | 1418 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
1293 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); | 1419 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
1294 } | 1420 } |
1295 } | 1421 } |
1296 | 1422 |
1297 /** | 1423 /** |
1298 * A canvas that records the number of clip commands. | 1424 * A canvas that records the number of clip commands. |
1299 */ | 1425 */ |
1300 class ClipCountingCanvas : public SkCanvas { | 1426 class ClipCountingCanvas : public SkCanvas { |
1301 public: | 1427 public: |
1302 explicit ClipCountingCanvas(int width, int height) | 1428 ClipCountingCanvas(int width, int height) |
1303 : INHERITED(width, height) | 1429 : INHERITED(width, height) |
1304 , fClipCount(0){ | 1430 , fClipCount(0){ |
1305 } | 1431 } |
1306 | 1432 |
1307 virtual void onClipRect(const SkRect& r, | 1433 virtual void onClipRect(const SkRect& r, |
1308 SkRegion::Op op, | 1434 SkRegion::Op op, |
1309 ClipEdgeStyle edgeStyle) SK_OVERRIDE { | 1435 ClipEdgeStyle edgeStyle) SK_OVERRIDE { |
1310 fClipCount += 1; | 1436 fClipCount += 1; |
1311 this->INHERITED::onClipRect(r, op, edgeStyle); | 1437 this->INHERITED::onClipRect(r, op, edgeStyle); |
1312 } | 1438 } |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1500 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); | 1626 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
1501 } | 1627 } |
1502 | 1628 |
1503 DEF_TEST(Canvas_EmptyBitmap, r) { | 1629 DEF_TEST(Canvas_EmptyBitmap, r) { |
1504 SkBitmap dst; | 1630 SkBitmap dst; |
1505 dst.allocN32Pixels(10, 10); | 1631 dst.allocN32Pixels(10, 10); |
1506 SkCanvas canvas(dst); | 1632 SkCanvas canvas(dst); |
1507 | 1633 |
1508 test_draw_bitmaps(&canvas); | 1634 test_draw_bitmaps(&canvas); |
1509 } | 1635 } |
OLD | NEW |