commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 1 | #include "Test.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 2 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 3 | #include "SkMallocPixelRef.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 4 | #include "SkPixelRef.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 5 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 6 | class TestListener : public SkPixelRef::GenIDChangeListener { |
| 7 | public: |
| 8 | explicit TestListener(int* ptr) : fPtr(ptr) {} |
tfarina@chromium.org | 5867481 | 2014-01-21 23:39:22 +0000 | [diff] [blame] | 9 | virtual void onChange() SK_OVERRIDE { (*fPtr)++; } |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 10 | private: |
| 11 | int* fPtr; |
| 12 | }; |
| 13 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 14 | DEF_TEST(PixelRef_GenIDChange, r) { |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 15 | SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 16 | |
| 17 | SkAutoTUnref<SkPixelRef> pixelRef(SkMallocPixelRef::NewAllocate(info, 0, NULL)); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 18 | |
| 19 | // Register a listener. |
| 20 | int count = 0; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 21 | pixelRef->addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count))); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 22 | REPORTER_ASSERT(r, 0 == count); |
| 23 | |
| 24 | // No one has looked at our pixelRef's generation ID, so invalidating it doesn't make sense. |
| 25 | // (An SkPixelRef tree falls in the forest but there's nobody around to hear it. Do we care?) |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 26 | pixelRef->notifyPixelsChanged(); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 27 | REPORTER_ASSERT(r, 0 == count); |
| 28 | |
| 29 | // Force the generation ID to be calculated. |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 30 | REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 31 | |
| 32 | // Our listener was dropped in the first call to notifyPixelsChanged(). This is a no-op. |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 33 | pixelRef->notifyPixelsChanged(); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 34 | REPORTER_ASSERT(r, 0 == count); |
| 35 | |
| 36 | // Force the generation ID to be recalculated, then add a listener. |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 37 | REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); |
| 38 | pixelRef->addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count))); |
| 39 | pixelRef->notifyPixelsChanged(); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 40 | REPORTER_ASSERT(r, 1 == count); |
| 41 | |
| 42 | // Quick check that NULL is safe. |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 43 | REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); |
| 44 | pixelRef->addGenIDChangeListener(NULL); |
| 45 | pixelRef->notifyPixelsChanged(); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 46 | } |