blob: 683e249626418f2c28e54c9714275f97e02ea702 [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
commit-bot@chromium.org50a30432013-10-24 17:44:27 +00008#include "Test.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +00009
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000010#include "SkMallocPixelRef.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000011#include "SkPixelRef.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000012
reeddb74f622015-05-30 13:41:15 -070013static void decrement_counter_proc(void* pixels, void* ctx) {
14 int* counter = (int*)ctx;
15 *counter -= 1;
16}
17
18static 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;
halcanary96fcdcc2015-08-27 07:41:13 -070026 success = bm.installPixels(info, nullptr, 0, nullptr, decrement_counter_proc, &release_counter);
reeddb74f622015-05-30 13:41:15 -070027 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;
halcanary96fcdcc2015-08-27 07:41:13 -070033 success = bm.installPixels(info, nullptr, 0, nullptr, decrement_counter_proc, &release_counter);
reeddb74f622015-05-30 13:41:15 -070034 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;
halcanary96fcdcc2015-08-27 07:41:13 -070040 success = bm.installPixels(info, nullptr, 0, nullptr, decrement_counter_proc, &release_counter);
reeddb74f622015-05-30 13:41:15 -070041 REPORTER_ASSERT(reporter, false == success);
42 bm.reset();
43 REPORTER_ASSERT(reporter, 0 == release_counter);
44}
45
46static 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
halcanary96fcdcc2015-08-27 07:41:13 -070051 success = bm.installPixels(info, nullptr, 0);
reeddb74f622015-05-30 13:41:15 -070052 REPORTER_ASSERT(reporter, success);
53
54 // no pixels should be the same as setInfo()
55 info = SkImageInfo::MakeN32Premul(10, 10);
halcanary96fcdcc2015-08-27 07:41:13 -070056 success = bm.installPixels(info, nullptr, 0);
reeddb74f622015-05-30 13:41:15 -070057 REPORTER_ASSERT(reporter, success);
58
59}
60
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000061class TestListener : public SkPixelRef::GenIDChangeListener {
62public:
63 explicit TestListener(int* ptr) : fPtr(ptr) {}
mtklein36352bf2015-03-25 18:17:31 -070064 void onChange() override { (*fPtr)++; }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000065private:
66 int* fPtr;
67};
68
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000069DEF_TEST(PixelRef_GenIDChange, r) {
commit-bot@chromium.org32678d92014-01-15 02:38:22 +000070 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
reed@google.combf790232013-12-13 19:45:58 +000071
Mike Reed6b3155c2017-04-03 14:41:44 -040072 sk_sp<SkPixelRef> pixelRef = SkMallocPixelRef::MakeAllocate(info, 0, nullptr);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000073
74 // Register a listener.
75 int count = 0;
halcanary385fe4d2015-08-26 13:07:48 -070076 pixelRef->addGenIDChangeListener(new TestListener(&count));
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000077 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.combf790232013-12-13 19:45:58 +000081 pixelRef->notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000082 REPORTER_ASSERT(r, 0 == count);
83
84 // Force the generation ID to be calculated.
reed@google.combf790232013-12-13 19:45:58 +000085 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000086
87 // Our listener was dropped in the first call to notifyPixelsChanged(). This is a no-op.
reed@google.combf790232013-12-13 19:45:58 +000088 pixelRef->notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000089 REPORTER_ASSERT(r, 0 == count);
90
91 // Force the generation ID to be recalculated, then add a listener.
reed@google.combf790232013-12-13 19:45:58 +000092 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
halcanary385fe4d2015-08-26 13:07:48 -070093 pixelRef->addGenIDChangeListener(new TestListener(&count));
reed@google.combf790232013-12-13 19:45:58 +000094 pixelRef->notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000095 REPORTER_ASSERT(r, 1 == count);
96
halcanary96fcdcc2015-08-27 07:41:13 -070097 // Quick check that nullptr is safe.
reed@google.combf790232013-12-13 19:45:58 +000098 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
halcanary96fcdcc2015-08-27 07:41:13 -070099 pixelRef->addGenIDChangeListener(nullptr);
reed@google.combf790232013-12-13 19:45:58 +0000100 pixelRef->notifyPixelsChanged();
reeddb74f622015-05-30 13:41:15 -0700101
102 test_install(r);
103 test_dont_leak_install(r);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000104}