reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 8 | #include "Test.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 9 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 10 | #include "SkMallocPixelRef.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 11 | #include "SkPixelRef.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 12 | |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 13 | static void decrement_counter_proc(void* pixels, void* ctx) { |
| 14 | int* counter = (int*)ctx; |
| 15 | *counter -= 1; |
| 16 | } |
| 17 | |
| 18 | static void test_dont_leak_install(skiatest::Reporter* reporter) { |
| 19 | bool success; |
| 20 | int release_counter; |
| 21 | SkImageInfo info; |
| 22 | SkBitmap bm; |
| 23 | |
| 24 | info = SkImageInfo::MakeN32Premul(0, 0); |
| 25 | release_counter = 1; |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame^] | 26 | success = bm.installPixels(info, nullptr, 0, decrement_counter_proc, &release_counter); |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 27 | REPORTER_ASSERT(reporter, true == success); |
| 28 | bm.reset(); |
| 29 | REPORTER_ASSERT(reporter, 0 == release_counter); |
| 30 | |
| 31 | info = SkImageInfo::MakeN32Premul(10, 10); |
| 32 | release_counter = 1; |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame^] | 33 | success = bm.installPixels(info, nullptr, 0, decrement_counter_proc, &release_counter); |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 34 | REPORTER_ASSERT(reporter, true == success); |
| 35 | bm.reset(); |
| 36 | REPORTER_ASSERT(reporter, 0 == release_counter); |
| 37 | |
| 38 | info = SkImageInfo::MakeN32Premul(-10, -10); |
| 39 | release_counter = 1; |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame^] | 40 | success = bm.installPixels(info, nullptr, 0, decrement_counter_proc, &release_counter); |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 41 | REPORTER_ASSERT(reporter, false == success); |
| 42 | bm.reset(); |
| 43 | REPORTER_ASSERT(reporter, 0 == release_counter); |
| 44 | } |
| 45 | |
| 46 | static void test_install(skiatest::Reporter* reporter) { |
| 47 | bool success; |
| 48 | SkImageInfo info = SkImageInfo::MakeN32Premul(0, 0); |
| 49 | SkBitmap bm; |
| 50 | // make sure we don't assert on an empty install |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 51 | success = bm.installPixels(info, nullptr, 0); |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 52 | REPORTER_ASSERT(reporter, success); |
| 53 | |
| 54 | // no pixels should be the same as setInfo() |
| 55 | info = SkImageInfo::MakeN32Premul(10, 10); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 56 | success = bm.installPixels(info, nullptr, 0); |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 57 | REPORTER_ASSERT(reporter, success); |
| 58 | |
| 59 | } |
| 60 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 61 | class TestListener : public SkPixelRef::GenIDChangeListener { |
| 62 | public: |
| 63 | explicit TestListener(int* ptr) : fPtr(ptr) {} |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 64 | void onChange() override { (*fPtr)++; } |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 65 | private: |
| 66 | int* fPtr; |
| 67 | }; |
| 68 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 69 | DEF_TEST(PixelRef_GenIDChange, r) { |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 70 | SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 71 | |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame^] | 72 | sk_sp<SkPixelRef> pixelRef = SkMallocPixelRef::MakeAllocate(info, 0); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 73 | |
| 74 | // Register a listener. |
| 75 | int count = 0; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 76 | pixelRef->addGenIDChangeListener(new TestListener(&count)); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 77 | REPORTER_ASSERT(r, 0 == count); |
| 78 | |
| 79 | // No one has looked at our pixelRef's generation ID, so invalidating it doesn't make sense. |
| 80 | // (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] | 81 | pixelRef->notifyPixelsChanged(); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 82 | REPORTER_ASSERT(r, 0 == count); |
| 83 | |
| 84 | // Force the generation ID to be calculated. |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 85 | REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 86 | |
| 87 | // 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] | 88 | pixelRef->notifyPixelsChanged(); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 89 | REPORTER_ASSERT(r, 0 == count); |
| 90 | |
| 91 | // Force the generation ID to be recalculated, then add a listener. |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 92 | REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 93 | pixelRef->addGenIDChangeListener(new TestListener(&count)); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 94 | pixelRef->notifyPixelsChanged(); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 95 | REPORTER_ASSERT(r, 1 == count); |
| 96 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 97 | // Quick check that nullptr is safe. |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 98 | REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 99 | pixelRef->addGenIDChangeListener(nullptr); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 100 | pixelRef->notifyPixelsChanged(); |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 101 | |
| 102 | test_install(r); |
| 103 | test_dont_leak_install(r); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 104 | } |