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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "tests/Test.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkMallocPixelRef.h" |
| 11 | #include "include/core/SkPixelRef.h" |
Brian Salomon | 71fe945 | 2020-03-02 16:59:40 -0500 | [diff] [blame] | 12 | #include "include/private/SkIDChangeListener.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 13 | |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 14 | static void decrement_counter_proc(void* pixels, void* ctx) { |
| 15 | int* counter = (int*)ctx; |
| 16 | *counter -= 1; |
| 17 | } |
| 18 | |
| 19 | static void test_dont_leak_install(skiatest::Reporter* reporter) { |
| 20 | bool success; |
| 21 | int release_counter; |
| 22 | SkImageInfo info; |
| 23 | SkBitmap bm; |
| 24 | |
| 25 | info = SkImageInfo::MakeN32Premul(0, 0); |
| 26 | release_counter = 1; |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 27 | success = bm.installPixels(info, nullptr, 0, decrement_counter_proc, &release_counter); |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 28 | REPORTER_ASSERT(reporter, true == success); |
| 29 | bm.reset(); |
| 30 | REPORTER_ASSERT(reporter, 0 == release_counter); |
| 31 | |
| 32 | info = SkImageInfo::MakeN32Premul(10, 10); |
| 33 | release_counter = 1; |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 34 | success = bm.installPixels(info, nullptr, 0, decrement_counter_proc, &release_counter); |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 35 | REPORTER_ASSERT(reporter, true == success); |
| 36 | bm.reset(); |
| 37 | REPORTER_ASSERT(reporter, 0 == release_counter); |
| 38 | |
| 39 | info = SkImageInfo::MakeN32Premul(-10, -10); |
| 40 | release_counter = 1; |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 41 | success = bm.installPixels(info, nullptr, 0, decrement_counter_proc, &release_counter); |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 42 | REPORTER_ASSERT(reporter, false == success); |
| 43 | bm.reset(); |
| 44 | REPORTER_ASSERT(reporter, 0 == release_counter); |
| 45 | } |
| 46 | |
| 47 | static void test_install(skiatest::Reporter* reporter) { |
| 48 | bool success; |
| 49 | SkImageInfo info = SkImageInfo::MakeN32Premul(0, 0); |
| 50 | SkBitmap bm; |
| 51 | // make sure we don't assert on an empty install |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 52 | success = bm.installPixels(info, nullptr, 0); |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 53 | REPORTER_ASSERT(reporter, success); |
| 54 | |
| 55 | // no pixels should be the same as setInfo() |
| 56 | info = SkImageInfo::MakeN32Premul(10, 10); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 57 | success = bm.installPixels(info, nullptr, 0); |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 58 | REPORTER_ASSERT(reporter, success); |
| 59 | |
| 60 | } |
| 61 | |
Brian Salomon | 71fe945 | 2020-03-02 16:59:40 -0500 | [diff] [blame] | 62 | class TestListener : public SkIDChangeListener { |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 63 | public: |
| 64 | explicit TestListener(int* ptr) : fPtr(ptr) {} |
Brian Salomon | 71fe945 | 2020-03-02 16:59:40 -0500 | [diff] [blame] | 65 | void changed() override { (*fPtr)++; } |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 66 | private: |
| 67 | int* fPtr; |
| 68 | }; |
| 69 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 70 | DEF_TEST(PixelRef_GenIDChange, r) { |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 71 | SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 72 | |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 73 | sk_sp<SkPixelRef> pixelRef = SkMallocPixelRef::MakeAllocate(info, 0); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 74 | |
| 75 | // Register a listener. |
| 76 | int count = 0; |
Brian Salomon | 71fe945 | 2020-03-02 16:59:40 -0500 | [diff] [blame] | 77 | pixelRef->addGenIDChangeListener(sk_make_sp<TestListener>(&count)); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 78 | REPORTER_ASSERT(r, 0 == count); |
| 79 | |
| 80 | // No one has looked at our pixelRef's generation ID, so invalidating it doesn't make sense. |
| 81 | // (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] | 82 | pixelRef->notifyPixelsChanged(); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 83 | REPORTER_ASSERT(r, 0 == count); |
| 84 | |
| 85 | // Force the generation ID to be calculated. |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 86 | REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 87 | |
| 88 | // 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] | 89 | pixelRef->notifyPixelsChanged(); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 90 | REPORTER_ASSERT(r, 0 == count); |
| 91 | |
| 92 | // Force the generation ID to be recalculated, then add a listener. |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 93 | REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); |
Brian Salomon | 71fe945 | 2020-03-02 16:59:40 -0500 | [diff] [blame] | 94 | pixelRef->addGenIDChangeListener(sk_make_sp<TestListener>(&count)); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 95 | pixelRef->notifyPixelsChanged(); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 96 | REPORTER_ASSERT(r, 1 == count); |
| 97 | |
Brian Salomon | 71fe945 | 2020-03-02 16:59:40 -0500 | [diff] [blame] | 98 | // Check that asking for deregistration causes the listener to not be called. |
| 99 | REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); |
| 100 | auto listener = sk_make_sp<TestListener>(&count); |
| 101 | pixelRef->addGenIDChangeListener(listener); |
| 102 | REPORTER_ASSERT(r, 1 == count); |
| 103 | listener->markShouldDeregister(); |
| 104 | pixelRef->notifyPixelsChanged(); |
| 105 | REPORTER_ASSERT(r, 1 == count); |
| 106 | |
| 107 | // Check that we use deregistration to prevent unbounded growth. |
| 108 | REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); |
| 109 | listener = sk_make_sp<TestListener>(&count); |
| 110 | pixelRef->addGenIDChangeListener(listener); |
| 111 | REPORTER_ASSERT(r, 1 == count); |
| 112 | listener->markShouldDeregister(); |
| 113 | // Add second listener. Should deregister first listener. |
| 114 | pixelRef->addGenIDChangeListener(sk_make_sp<TestListener>(&count)); |
| 115 | REPORTER_ASSERT(r, listener->unique()); |
| 116 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 117 | // Quick check that nullptr is safe. |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 118 | REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 119 | pixelRef->addGenIDChangeListener(nullptr); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 120 | pixelRef->notifyPixelsChanged(); |
reed | db74f62 | 2015-05-30 13:41:15 -0700 | [diff] [blame] | 121 | |
| 122 | test_install(r); |
| 123 | test_dont_leak_install(r); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 124 | } |