blob: 9fef367b1ebcbd9ebe6b14a5a364ca8f7e9dfa6e [file] [log] [blame]
reeddb74f622015-05-30 13:41:15 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tests/Test.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +00009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkMallocPixelRef.h"
11#include "include/core/SkPixelRef.h"
Brian Salomon71fe9452020-03-02 16:59:40 -050012#include "include/private/SkIDChangeListener.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000013
reeddb74f622015-05-30 13:41:15 -070014static void decrement_counter_proc(void* pixels, void* ctx) {
15 int* counter = (int*)ctx;
16 *counter -= 1;
17}
18
19static 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 Reed086a4272017-07-18 10:53:11 -040027 success = bm.installPixels(info, nullptr, 0, decrement_counter_proc, &release_counter);
reeddb74f622015-05-30 13:41:15 -070028 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 Reed086a4272017-07-18 10:53:11 -040034 success = bm.installPixels(info, nullptr, 0, decrement_counter_proc, &release_counter);
reeddb74f622015-05-30 13:41:15 -070035 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 Reed086a4272017-07-18 10:53:11 -040041 success = bm.installPixels(info, nullptr, 0, decrement_counter_proc, &release_counter);
reeddb74f622015-05-30 13:41:15 -070042 REPORTER_ASSERT(reporter, false == success);
43 bm.reset();
44 REPORTER_ASSERT(reporter, 0 == release_counter);
45}
46
47static 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
halcanary96fcdcc2015-08-27 07:41:13 -070052 success = bm.installPixels(info, nullptr, 0);
reeddb74f622015-05-30 13:41:15 -070053 REPORTER_ASSERT(reporter, success);
54
55 // no pixels should be the same as setInfo()
56 info = SkImageInfo::MakeN32Premul(10, 10);
halcanary96fcdcc2015-08-27 07:41:13 -070057 success = bm.installPixels(info, nullptr, 0);
reeddb74f622015-05-30 13:41:15 -070058 REPORTER_ASSERT(reporter, success);
59
60}
61
Brian Salomon71fe9452020-03-02 16:59:40 -050062class TestListener : public SkIDChangeListener {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000063public:
64 explicit TestListener(int* ptr) : fPtr(ptr) {}
Brian Salomon71fe9452020-03-02 16:59:40 -050065 void changed() override { (*fPtr)++; }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000066private:
67 int* fPtr;
68};
69
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000070DEF_TEST(PixelRef_GenIDChange, r) {
commit-bot@chromium.org32678d92014-01-15 02:38:22 +000071 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
reed@google.combf790232013-12-13 19:45:58 +000072
Mike Reed086a4272017-07-18 10:53:11 -040073 sk_sp<SkPixelRef> pixelRef = SkMallocPixelRef::MakeAllocate(info, 0);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000074
75 // Register a listener.
76 int count = 0;
Brian Salomon71fe9452020-03-02 16:59:40 -050077 pixelRef->addGenIDChangeListener(sk_make_sp<TestListener>(&count));
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000078 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.combf790232013-12-13 19:45:58 +000082 pixelRef->notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000083 REPORTER_ASSERT(r, 0 == count);
84
85 // Force the generation ID to be calculated.
reed@google.combf790232013-12-13 19:45:58 +000086 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000087
88 // Our listener was dropped in the first call to notifyPixelsChanged(). This is a no-op.
reed@google.combf790232013-12-13 19:45:58 +000089 pixelRef->notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000090 REPORTER_ASSERT(r, 0 == count);
91
92 // Force the generation ID to be recalculated, then add a listener.
reed@google.combf790232013-12-13 19:45:58 +000093 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
Brian Salomon71fe9452020-03-02 16:59:40 -050094 pixelRef->addGenIDChangeListener(sk_make_sp<TestListener>(&count));
reed@google.combf790232013-12-13 19:45:58 +000095 pixelRef->notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000096 REPORTER_ASSERT(r, 1 == count);
97
Brian Salomon71fe9452020-03-02 16:59:40 -050098 // 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
halcanary96fcdcc2015-08-27 07:41:13 -0700117 // Quick check that nullptr is safe.
reed@google.combf790232013-12-13 19:45:58 +0000118 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
halcanary96fcdcc2015-08-27 07:41:13 -0700119 pixelRef->addGenIDChangeListener(nullptr);
reed@google.combf790232013-12-13 19:45:58 +0000120 pixelRef->notifyPixelsChanged();
reeddb74f622015-05-30 13:41:15 -0700121
122 test_install(r);
123 test_dont_leak_install(r);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000124}