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 "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
(...skipping 756 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
767 } | 767 } |
768 #endif | 768 #endif |
769 | 769 |
770 static void set_canvas_to_save_count_4(SkCanvas* canvas) { | 770 static void set_canvas_to_save_count_4(SkCanvas* canvas) { |
771 canvas->restoreToCount(1); | 771 canvas->restoreToCount(1); |
772 canvas->save(); | 772 canvas->save(); |
773 canvas->save(); | 773 canvas->save(); |
774 canvas->save(); | 774 canvas->save(); |
775 } | 775 } |
776 | 776 |
777 #ifdef SK_BUILD_FOR_ANDROID | |
778 /** | |
779 * A canvas that records the number of saves, saveLayers and restores. | |
780 */ | |
781 class SaveCountingCanvas : public SkCanvas { | |
782 public: | |
783 explicit SaveCountingCanvas(int width, int height) | |
mtklein
2014/04/30 20:20:11
I guess it's fine, but you don't need explicit her
robertphillips
2014/05/27 14:10:08
Done.
| |
784 : INHERITED(width, height) | |
785 , fSaveCount(0) | |
786 , fSaveLayerCount(0) | |
787 , fRestoreCount(0){ | |
788 } | |
789 | |
790 virtual SaveLayerStrategy willSaveLayer(const SkRect* bounds, const SkPaint* paint, | |
mtklein
2014/04/30 20:20:11
If you'd like to save space, virtual is not needed
robertphillips
2014/05/27 14:10:08
We seem to pull through the virtual keyword whenev
| |
791 SaveFlags flags) SK_OVERRIDE { | |
792 ++fSaveLayerCount; | |
793 return this->INHERITED::willSaveLayer(bounds, paint, flags); | |
794 } | |
795 | |
796 virtual void willSave(SaveFlags flags) SK_OVERRIDE { | |
797 ++fSaveCount; | |
798 this->INHERITED::willSave(flags); | |
799 } | |
800 | |
801 virtual void willRestore() SK_OVERRIDE { | |
802 ++fRestoreCount; | |
803 this->INHERITED::willRestore(); | |
804 } | |
805 | |
806 unsigned int getSaveCount() const { return fSaveCount; } | |
mtklein
2014/04/30 20:20:11
I think we tend to just write "unsigned"
| |
807 unsigned int getSaveLayerCount() const { return fSaveLayerCount; } | |
808 unsigned int getRestoreCount() const { return fRestoreCount; } | |
809 | |
810 private: | |
811 unsigned int fSaveCount; | |
812 unsigned int fSaveLayerCount; | |
813 unsigned int fRestoreCount; | |
814 | |
815 typedef SkCanvas INHERITED; | |
816 }; | |
817 | |
818 void check_save_state(skiatest::Reporter* reporter, SkPicture* picture, | |
819 int numSaves, int numSaveLayers, int numRestores) { | |
820 SaveCountingCanvas canvas(picture->width(), picture->height()); | |
821 | |
822 picture->draw(&canvas); | |
823 | |
824 REPORTER_ASSERT(reporter, numSaves == canvas.getSaveCount()); | |
825 REPORTER_ASSERT(reporter, numSaveLayers == canvas.getSaveLayerCount()); | |
826 REPORTER_ASSERT(reporter, numRestores == canvas.getRestoreCount()); | |
827 } | |
828 | |
829 class SkPictureRecorderReplayTester { | |
830 public: | |
831 static SkPicture* Copy(SkPictureRecorder* recorder) { | |
mtklein
2014/04/30 20:20:11
Can you add a note that the class is here so it ca
mtklein
2014/04/30 20:20:11
Is Android also going to be using this for copying
robertphillips
2014/05/27 14:10:08
Done.
robertphillips
2014/05/27 14:10:08
I proposed a "snapshot" version of this in https:/
| |
832 SkPictureRecorder recorder2; | |
833 | |
834 SkCanvas* canvas = recorder2.beginRecording(10, 10, NULL, 0); | |
835 | |
836 recorder->replay(canvas); | |
837 | |
robertphillips
2014/04/28 17:35:21
This is where the save/saveLayer balancing occurs
| |
838 return recorder2.endRecording(); | |
839 } | |
840 }; | |
841 | |
842 // Test out SkPictureRecorder::replay | |
843 static void test_replay(skiatest::Reporter* reporter) { | |
mtklein
2014/04/30 20:20:11
DEF_TEST(PictureRecorder_replay, reporter) { ...
robertphillips
2014/05/27 14:10:08
Done.
| |
844 | |
845 // check save/saveLayer state | |
846 { | |
847 SkPictureRecorder recorder; | |
848 | |
849 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0); | |
850 | |
851 canvas->saveLayer(NULL, NULL); | |
852 | |
853 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&record er)); | |
854 | |
robertphillips
2014/04/28 17:35:21
The extra save and restore comes from the Copy pro
mtklein
2014/04/30 20:20:11
Tack this on as a comment here?
robertphillips
2014/05/27 14:10:08
Done.
| |
855 check_save_state(reporter, copy, 2, 1, 3); | |
856 | |
857 canvas->saveLayer(NULL, NULL); | |
858 | |
859 SkAutoTUnref<SkPicture> final(recorder.endRecording()); | |
860 | |
861 check_save_state(reporter, final, 1, 2, 3); | |
862 | |
863 // The copy shouldn't pick up any operations added after it was made | |
864 check_save_state(reporter, copy, 2, 1, 3); | |
865 } | |
866 | |
867 // (partially) check leakage of draw ops | |
mtklein
2014/04/30 20:20:11
Might as well split this into a separate DEF_TEST
robertphillips
2014/05/27 14:10:08
I would prefer to keep these together since they a
| |
868 { | |
869 SkPictureRecorder recorder; | |
870 | |
871 SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0); | |
872 | |
873 SkRect r = SkRect::MakeWH(5, 5); | |
874 SkPaint p; | |
875 | |
876 canvas->drawRect(r, p); | |
877 | |
878 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&record er)); | |
879 | |
880 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps()); | |
881 | |
882 SkBitmap bm; | |
883 make_bm(&bm, 10, 10, SK_ColorRED, true); | |
884 | |
885 r.offset(5.0f, 5.0f); | |
886 canvas->drawBitmapRectToRect(bm, NULL, r); | |
887 | |
888 SkAutoTUnref<SkPicture> final(recorder.endRecording()); | |
889 REPORTER_ASSERT(reporter, final->willPlayBackBitmaps()); | |
890 | |
891 REPORTER_ASSERT(reporter, copy->uniqueID() != final->uniqueID()); | |
892 | |
893 // The snapshot shouldn't pick up any operations added after it was made | |
894 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps()); | |
895 } | |
896 } | |
897 #endif | |
898 | |
777 static void test_unbalanced_save_restores(skiatest::Reporter* reporter) { | 899 static void test_unbalanced_save_restores(skiatest::Reporter* reporter) { |
778 SkCanvas testCanvas(100, 100); | 900 SkCanvas testCanvas(100, 100); |
779 set_canvas_to_save_count_4(&testCanvas); | 901 set_canvas_to_save_count_4(&testCanvas); |
780 | 902 |
781 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); | 903 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
782 | 904 |
783 SkPaint paint; | 905 SkPaint paint; |
784 SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000); | 906 SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000); |
785 | 907 |
786 SkPictureRecorder recorder; | 908 SkPictureRecorder recorder; |
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1261 } | 1383 } |
1262 } | 1384 } |
1263 | 1385 |
1264 DEF_TEST(Picture, reporter) { | 1386 DEF_TEST(Picture, reporter) { |
1265 #ifdef SK_DEBUG | 1387 #ifdef SK_DEBUG |
1266 test_deleting_empty_playback(); | 1388 test_deleting_empty_playback(); |
1267 test_serializing_empty_picture(); | 1389 test_serializing_empty_picture(); |
1268 #else | 1390 #else |
1269 test_bad_bitmap(); | 1391 test_bad_bitmap(); |
1270 #endif | 1392 #endif |
1393 #ifdef SK_BUILD_FOR_ANDROID | |
1394 test_replay(reporter); | |
1395 #endif | |
1271 test_unbalanced_save_restores(reporter); | 1396 test_unbalanced_save_restores(reporter); |
1272 test_peephole(); | 1397 test_peephole(); |
1273 #if SK_SUPPORT_GPU | 1398 #if SK_SUPPORT_GPU |
1274 test_gpu_veto(reporter); | 1399 test_gpu_veto(reporter); |
1275 #endif | 1400 #endif |
1276 test_gatherpixelrefs(reporter); | 1401 test_gatherpixelrefs(reporter); |
1277 test_gatherpixelrefsandrects(reporter); | 1402 test_gatherpixelrefsandrects(reporter); |
1278 test_bitmap_with_encoded_data(reporter); | 1403 test_bitmap_with_encoded_data(reporter); |
1279 test_clone_empty(reporter); | 1404 test_clone_empty(reporter); |
1280 test_draw_empty(reporter); | 1405 test_draw_empty(reporter); |
(...skipping 29 matching lines...) Expand all Loading... | |
1310 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); | 1435 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
1311 } | 1436 } |
1312 | 1437 |
1313 DEF_TEST(Canvas_EmptyBitmap, r) { | 1438 DEF_TEST(Canvas_EmptyBitmap, r) { |
1314 SkBitmap dst; | 1439 SkBitmap dst; |
1315 dst.allocN32Pixels(10, 10); | 1440 dst.allocN32Pixels(10, 10); |
1316 SkCanvas canvas(dst); | 1441 SkCanvas canvas(dst); |
1317 | 1442 |
1318 test_draw_bitmaps(&canvas); | 1443 test_draw_bitmaps(&canvas); |
1319 } | 1444 } |
OLD | NEW |