Chromium Code Reviews| 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 int numSaves, int numSaveLayers, int numRestores) { | |
| 962 SaveCountingCanvas canvas(picture->width(), picture->height()); | |
| 963 | |
| 964 picture->draw(&canvas); | |
| 965 | |
| 966 REPORTER_ASSERT(reporter, numSaves == canvas.getSaveCount()); | |
| 967 REPORTER_ASSERT(reporter, numSaveLayers == canvas.getSaveLayerCount()); | |
| 968 REPORTER_ASSERT(reporter, numRestores == canvas.getRestoreCount()); | |
| 969 } | |
| 970 | |
| 971 // This class exists so SkPicture can friend it and give it access to | |
| 972 // the 'partialReplay' method. | |
| 973 class SkPictureRecorderReplayTester { | |
| 974 public: | |
| 975 static SkPicture* Copy(SkPictureRecorder* recorder) { | |
| 976 SkPictureRecorder recorder2; | |
| 977 | |
| 978 SkCanvas* canvas = recorder2.beginRecording(10, 10, NULL, 0); | |
| 979 | |
| 980 recorder->partialReplay(canvas); | |
| 981 | |
| 982 return recorder2.endRecording(); | |
| 983 } | |
| 984 }; | |
| 985 | |
| 986 // Test out SkPictureRecorder::partialReplay | |
| 987 DEF_TEST(PictureRecorder_replay, reporter) { | |
| 988 // check save/saveLayer state | |
| 989 { | |
| 990 SkPictureRecorder recorder; | |
| 991 | |
| 992 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0); | |
| 993 | |
| 994 canvas->saveLayer(NULL, NULL); | |
| 995 | |
| 996 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&record er)); | |
| 997 | |
| 998 // The extra save and restore comes from the Copy process. | |
| 999 check_save_state(reporter, copy, 2, 1, 3); | |
| 1000 | |
| 1001 canvas->saveLayer(NULL, NULL); | |
| 1002 | |
| 1003 SkAutoTUnref<SkPicture> final(recorder.endRecording()); | |
| 1004 | |
| 1005 check_save_state(reporter, final, 1, 2, 3); | |
| 1006 | |
| 1007 // The copy shouldn't pick up any operations added after it was made | |
| 1008 check_save_state(reporter, copy, 2, 1, 3); | |
| 1009 } | |
| 1010 | |
| 1011 // (partially) check leakage of draw ops | |
| 1012 { | |
| 1013 SkPictureRecorder recorder; | |
| 1014 | |
| 1015 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0); | |
| 1016 | |
| 1017 SkRect r = SkRect::MakeWH(5, 5); | |
| 1018 SkPaint p; | |
| 1019 | |
| 1020 canvas->drawRect(r, p); | |
| 1021 | |
| 1022 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&record er)); | |
| 1023 | |
| 1024 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps()); | |
| 1025 | |
| 1026 SkBitmap bm; | |
| 1027 make_bm(&bm, 10, 10, SK_ColorRED, true); | |
| 1028 | |
| 1029 r.offset(5.0f, 5.0f); | |
| 1030 canvas->drawBitmapRectToRect(bm, NULL, r); | |
| 1031 | |
| 1032 SkAutoTUnref<SkPicture> final(recorder.endRecording()); | |
| 1033 REPORTER_ASSERT(reporter, final->willPlayBackBitmaps()); | |
| 1034 | |
| 1035 REPORTER_ASSERT(reporter, copy->uniqueID() != final->uniqueID()); | |
| 1036 | |
| 1037 // The snapshot shouldn't pick up any operations added after it was made | |
| 1038 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps()); | |
| 1039 } | |
| 1040 } | |
| 1041 #endif | |
| 1042 | |
| 919 static void test_unbalanced_save_restores(skiatest::Reporter* reporter) { | 1043 static void test_unbalanced_save_restores(skiatest::Reporter* reporter) { |
| 920 SkCanvas testCanvas(100, 100); | 1044 SkCanvas testCanvas(100, 100); |
| 921 set_canvas_to_save_count_4(&testCanvas); | 1045 set_canvas_to_save_count_4(&testCanvas); |
| 922 | 1046 |
| 923 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); | 1047 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 924 | 1048 |
| 925 SkPaint paint; | 1049 SkPaint paint; |
| 926 SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000); | 1050 SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000); |
| 927 | 1051 |
| 928 SkPictureRecorder recorder; | 1052 SkPictureRecorder recorder; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 980 defined(SK_SUPPORT_LEGACY_DERIVED_PICTURE_CLASSES) | 1104 defined(SK_SUPPORT_LEGACY_DERIVED_PICTURE_CLASSES) |
| 981 set_canvas_to_save_count_4(&testCanvas); | 1105 set_canvas_to_save_count_4(&testCanvas); |
| 982 | 1106 |
| 983 // Due to "fake" endRecording, the old SkPicture recording interface | 1107 // Due to "fake" endRecording, the old SkPicture recording interface |
| 984 // allowed unbalanced saves/restores to leak out. This sub-test checks | 1108 // allowed unbalanced saves/restores to leak out. This sub-test checks |
| 985 // that the situation has been remedied. | 1109 // that the situation has been remedied. |
| 986 { | 1110 { |
| 987 SkPicture p; | 1111 SkPicture p; |
| 988 | 1112 |
| 989 SkCanvas* canvas = p.beginRecording(100, 100); | 1113 SkCanvas* canvas = p.beginRecording(100, 100); |
| 990 for (int i = 0; i < 4; ++i) { | 1114 for (int i = 0; i < 4; ++i) { |
|
scroggo
2014/05/27 14:50:03
It looks like this is slightly different from set_
robertphillips
2014/05/27 17:23:39
In this instance the restoreToCount in set_canvas_
| |
| 991 canvas->save(); | 1115 canvas->save(); |
| 992 } | 1116 } |
| 993 SkRect r = SkRect::MakeWH(50, 50); | 1117 SkRect r = SkRect::MakeWH(50, 50); |
| 994 SkPaint paint; | 1118 SkPaint paint; |
| 995 canvas->drawRect(r, paint); | 1119 canvas->drawRect(r, paint); |
| 996 | 1120 |
| 997 // Copying a mid-recording picture could result in unbalanced saves/rest ores | 1121 // Copying a mid-recording picture could result in unbalanced saves/rest ores |
|
scroggo
2014/05/27 14:50:03
Instead of "could result in", don't we really mean
robertphillips
2014/05/27 17:23:39
Done.
| |
| 998 SkPicture p2(p); | 1122 SkPicture p2(p); |
| 999 | 1123 |
| 1000 testCanvas.drawPicture(p2); | 1124 testCanvas.drawPicture(p2); |
| 1001 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); | 1125 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 1002 set_canvas_to_save_count_4(&testCanvas); | 1126 set_canvas_to_save_count_4(&testCanvas); |
|
scroggo
2014/05/27 14:50:03
Is this in case the prior assert failed?
robertphillips
2014/05/27 17:23:39
Yes. I extended this test case to check the "fake"
| |
| 1003 | 1127 |
| 1004 // Cloning a mid-recording picture could result in unbalanced saves/rest ores | 1128 // Cloning a mid-recording picture could result in unbalanced saves/rest ores |
| 1005 SkAutoTUnref<SkPicture> p3(p.clone()); | 1129 SkAutoTUnref<SkPicture> p3(p.clone()); |
| 1006 testCanvas.drawPicture(*p3); | 1130 testCanvas.drawPicture(*p3); |
| 1007 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); | 1131 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 1008 set_canvas_to_save_count_4(&testCanvas); | 1132 set_canvas_to_save_count_4(&testCanvas); |
| 1009 | 1133 |
| 1010 // Serializing a mid-recording picture could result in unbalanced saves/ restores | 1134 // Serializing a mid-recording picture could result in unbalanced saves/ restores |
| 1011 SkDynamicMemoryWStream wStream; | 1135 SkDynamicMemoryWStream wStream; |
| 1012 p.serialize(&wStream); | 1136 p.serialize(&wStream); |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1292 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); | 1416 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 1293 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); | 1417 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 1294 } | 1418 } |
| 1295 } | 1419 } |
| 1296 | 1420 |
| 1297 /** | 1421 /** |
| 1298 * A canvas that records the number of clip commands. | 1422 * A canvas that records the number of clip commands. |
| 1299 */ | 1423 */ |
| 1300 class ClipCountingCanvas : public SkCanvas { | 1424 class ClipCountingCanvas : public SkCanvas { |
| 1301 public: | 1425 public: |
| 1302 explicit ClipCountingCanvas(int width, int height) | 1426 ClipCountingCanvas(int width, int height) |
| 1303 : INHERITED(width, height) | 1427 : INHERITED(width, height) |
| 1304 , fClipCount(0){ | 1428 , fClipCount(0){ |
| 1305 } | 1429 } |
| 1306 | 1430 |
| 1307 virtual void onClipRect(const SkRect& r, | 1431 virtual void onClipRect(const SkRect& r, |
| 1308 SkRegion::Op op, | 1432 SkRegion::Op op, |
| 1309 ClipEdgeStyle edgeStyle) SK_OVERRIDE { | 1433 ClipEdgeStyle edgeStyle) SK_OVERRIDE { |
| 1310 fClipCount += 1; | 1434 fClipCount += 1; |
| 1311 this->INHERITED::onClipRect(r, op, edgeStyle); | 1435 this->INHERITED::onClipRect(r, op, edgeStyle); |
| 1312 } | 1436 } |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1500 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); | 1624 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 1501 } | 1625 } |
| 1502 | 1626 |
| 1503 DEF_TEST(Canvas_EmptyBitmap, r) { | 1627 DEF_TEST(Canvas_EmptyBitmap, r) { |
| 1504 SkBitmap dst; | 1628 SkBitmap dst; |
| 1505 dst.allocN32Pixels(10, 10); | 1629 dst.allocN32Pixels(10, 10); |
| 1506 SkCanvas canvas(dst); | 1630 SkCanvas canvas(dst); |
| 1507 | 1631 |
| 1508 test_draw_bitmaps(&canvas); | 1632 test_draw_bitmaps(&canvas); |
| 1509 } | 1633 } |
| OLD | NEW |