| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "Test.h" | 8 #include "Test.h" |
| 9 | 9 |
| 10 #include "SkMallocPixelRef.h" | 10 #include "SkMallocPixelRef.h" |
| 11 #include "SkPixelRef.h" | 11 #include "SkPixelRef.h" |
| 12 | 12 |
| 13 static void decrement_counter_proc(void* pixels, void* ctx) { | 13 static void decrement_counter_proc(void* pixels, void* ctx) { |
| 14 int* counter = (int*)ctx; | 14 int* counter = (int*)ctx; |
| 15 *counter -= 1; | 15 *counter -= 1; |
| 16 } | 16 } |
| 17 | 17 |
| 18 static void test_dont_leak_install(skiatest::Reporter* reporter) { | 18 static void test_dont_leak_install(skiatest::Reporter* reporter) { |
| 19 bool success; | 19 bool success; |
| 20 int release_counter; | 20 int release_counter; |
| 21 SkImageInfo info; | 21 SkImageInfo info; |
| 22 SkBitmap bm; | 22 SkBitmap bm; |
| 23 | 23 |
| 24 info = SkImageInfo::MakeN32Premul(0, 0); | 24 info = SkImageInfo::MakeN32Premul(0, 0); |
| 25 release_counter = 1; | 25 release_counter = 1; |
| 26 success = bm.installPixels(info, NULL, 0, NULL, decrement_counter_proc, &rel
ease_counter); | 26 success = bm.installPixels(info, nullptr, 0, nullptr, decrement_counter_proc
, &release_counter); |
| 27 REPORTER_ASSERT(reporter, true == success); | 27 REPORTER_ASSERT(reporter, true == success); |
| 28 bm.reset(); | 28 bm.reset(); |
| 29 REPORTER_ASSERT(reporter, 0 == release_counter); | 29 REPORTER_ASSERT(reporter, 0 == release_counter); |
| 30 | 30 |
| 31 info = SkImageInfo::MakeN32Premul(10, 10); | 31 info = SkImageInfo::MakeN32Premul(10, 10); |
| 32 release_counter = 1; | 32 release_counter = 1; |
| 33 success = bm.installPixels(info, NULL, 0, NULL, decrement_counter_proc, &rel
ease_counter); | 33 success = bm.installPixels(info, nullptr, 0, nullptr, decrement_counter_proc
, &release_counter); |
| 34 REPORTER_ASSERT(reporter, true == success); | 34 REPORTER_ASSERT(reporter, true == success); |
| 35 bm.reset(); | 35 bm.reset(); |
| 36 REPORTER_ASSERT(reporter, 0 == release_counter); | 36 REPORTER_ASSERT(reporter, 0 == release_counter); |
| 37 | 37 |
| 38 info = SkImageInfo::MakeN32Premul(-10, -10); | 38 info = SkImageInfo::MakeN32Premul(-10, -10); |
| 39 release_counter = 1; | 39 release_counter = 1; |
| 40 success = bm.installPixels(info, NULL, 0, NULL, decrement_counter_proc, &rel
ease_counter); | 40 success = bm.installPixels(info, nullptr, 0, nullptr, decrement_counter_proc
, &release_counter); |
| 41 REPORTER_ASSERT(reporter, false == success); | 41 REPORTER_ASSERT(reporter, false == success); |
| 42 bm.reset(); | 42 bm.reset(); |
| 43 REPORTER_ASSERT(reporter, 0 == release_counter); | 43 REPORTER_ASSERT(reporter, 0 == release_counter); |
| 44 } | 44 } |
| 45 | 45 |
| 46 static void test_install(skiatest::Reporter* reporter) { | 46 static void test_install(skiatest::Reporter* reporter) { |
| 47 bool success; | 47 bool success; |
| 48 SkImageInfo info = SkImageInfo::MakeN32Premul(0, 0); | 48 SkImageInfo info = SkImageInfo::MakeN32Premul(0, 0); |
| 49 SkBitmap bm; | 49 SkBitmap bm; |
| 50 // make sure we don't assert on an empty install | 50 // make sure we don't assert on an empty install |
| 51 success = bm.installPixels(info, NULL, 0); | 51 success = bm.installPixels(info, nullptr, 0); |
| 52 REPORTER_ASSERT(reporter, success); | 52 REPORTER_ASSERT(reporter, success); |
| 53 | 53 |
| 54 // no pixels should be the same as setInfo() | 54 // no pixels should be the same as setInfo() |
| 55 info = SkImageInfo::MakeN32Premul(10, 10); | 55 info = SkImageInfo::MakeN32Premul(10, 10); |
| 56 success = bm.installPixels(info, NULL, 0); | 56 success = bm.installPixels(info, nullptr, 0); |
| 57 REPORTER_ASSERT(reporter, success); | 57 REPORTER_ASSERT(reporter, success); |
| 58 | 58 |
| 59 } | 59 } |
| 60 | 60 |
| 61 class TestListener : public SkPixelRef::GenIDChangeListener { | 61 class TestListener : public SkPixelRef::GenIDChangeListener { |
| 62 public: | 62 public: |
| 63 explicit TestListener(int* ptr) : fPtr(ptr) {} | 63 explicit TestListener(int* ptr) : fPtr(ptr) {} |
| 64 void onChange() override { (*fPtr)++; } | 64 void onChange() override { (*fPtr)++; } |
| 65 private: | 65 private: |
| 66 int* fPtr; | 66 int* fPtr; |
| 67 }; | 67 }; |
| 68 | 68 |
| 69 DEF_TEST(PixelRef_GenIDChange, r) { | 69 DEF_TEST(PixelRef_GenIDChange, r) { |
| 70 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); | 70 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); |
| 71 | 71 |
| 72 SkAutoTUnref<SkPixelRef> pixelRef(SkMallocPixelRef::NewAllocate(info, 0, NUL
L)); | 72 SkAutoTUnref<SkPixelRef> pixelRef(SkMallocPixelRef::NewAllocate(info, 0, nul
lptr)); |
| 73 | 73 |
| 74 // Register a listener. | 74 // Register a listener. |
| 75 int count = 0; | 75 int count = 0; |
| 76 pixelRef->addGenIDChangeListener(new TestListener(&count)); | 76 pixelRef->addGenIDChangeListener(new TestListener(&count)); |
| 77 REPORTER_ASSERT(r, 0 == count); | 77 REPORTER_ASSERT(r, 0 == count); |
| 78 | 78 |
| 79 // No one has looked at our pixelRef's generation ID, so invalidating it doe
sn't make sense. | 79 // No one has looked at our pixelRef's generation ID, so invalidating it doe
sn't make sense. |
| 80 // (An SkPixelRef tree falls in the forest but there's nobody around to hear
it. Do we care?) | 80 // (An SkPixelRef tree falls in the forest but there's nobody around to hear
it. Do we care?) |
| 81 pixelRef->notifyPixelsChanged(); | 81 pixelRef->notifyPixelsChanged(); |
| 82 REPORTER_ASSERT(r, 0 == count); | 82 REPORTER_ASSERT(r, 0 == count); |
| 83 | 83 |
| 84 // Force the generation ID to be calculated. | 84 // Force the generation ID to be calculated. |
| 85 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); | 85 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); |
| 86 | 86 |
| 87 // Our listener was dropped in the first call to notifyPixelsChanged(). Thi
s is a no-op. | 87 // Our listener was dropped in the first call to notifyPixelsChanged(). Thi
s is a no-op. |
| 88 pixelRef->notifyPixelsChanged(); | 88 pixelRef->notifyPixelsChanged(); |
| 89 REPORTER_ASSERT(r, 0 == count); | 89 REPORTER_ASSERT(r, 0 == count); |
| 90 | 90 |
| 91 // Force the generation ID to be recalculated, then add a listener. | 91 // Force the generation ID to be recalculated, then add a listener. |
| 92 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); | 92 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); |
| 93 pixelRef->addGenIDChangeListener(new TestListener(&count)); | 93 pixelRef->addGenIDChangeListener(new TestListener(&count)); |
| 94 pixelRef->notifyPixelsChanged(); | 94 pixelRef->notifyPixelsChanged(); |
| 95 REPORTER_ASSERT(r, 1 == count); | 95 REPORTER_ASSERT(r, 1 == count); |
| 96 | 96 |
| 97 // Quick check that NULL is safe. | 97 // Quick check that nullptr is safe. |
| 98 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); | 98 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); |
| 99 pixelRef->addGenIDChangeListener(NULL); | 99 pixelRef->addGenIDChangeListener(nullptr); |
| 100 pixelRef->notifyPixelsChanged(); | 100 pixelRef->notifyPixelsChanged(); |
| 101 | 101 |
| 102 test_install(r); | 102 test_install(r); |
| 103 test_dont_leak_install(r); | 103 test_dont_leak_install(r); |
| 104 } | 104 } |
| OLD | NEW |