OLD | NEW |
| 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 |
1 #include "Test.h" | 8 #include "Test.h" |
2 | 9 |
3 #include "SkMallocPixelRef.h" | 10 #include "SkMallocPixelRef.h" |
4 #include "SkPixelRef.h" | 11 #include "SkPixelRef.h" |
5 | 12 |
| 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; |
| 26 success = bm.installPixels(info, NULL, 0, NULL, decrement_counter_proc, &rel
ease_counter); |
| 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; |
| 33 success = bm.installPixels(info, NULL, 0, NULL, decrement_counter_proc, &rel
ease_counter); |
| 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; |
| 40 success = bm.installPixels(info, NULL, 0, NULL, decrement_counter_proc, &rel
ease_counter); |
| 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 |
| 51 success = bm.installPixels(info, NULL, 0); |
| 52 REPORTER_ASSERT(reporter, success); |
| 53 |
| 54 // no pixels should be the same as setInfo() |
| 55 info = SkImageInfo::MakeN32Premul(10, 10); |
| 56 success = bm.installPixels(info, NULL, 0); |
| 57 REPORTER_ASSERT(reporter, success); |
| 58 |
| 59 } |
| 60 |
6 class TestListener : public SkPixelRef::GenIDChangeListener { | 61 class TestListener : public SkPixelRef::GenIDChangeListener { |
7 public: | 62 public: |
8 explicit TestListener(int* ptr) : fPtr(ptr) {} | 63 explicit TestListener(int* ptr) : fPtr(ptr) {} |
9 void onChange() override { (*fPtr)++; } | 64 void onChange() override { (*fPtr)++; } |
10 private: | 65 private: |
11 int* fPtr; | 66 int* fPtr; |
12 }; | 67 }; |
13 | 68 |
14 DEF_TEST(PixelRef_GenIDChange, r) { | 69 DEF_TEST(PixelRef_GenIDChange, r) { |
15 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); | 70 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); |
(...skipping 20 matching lines...) Expand all Loading... |
36 // Force the generation ID to be recalculated, then add a listener. | 91 // Force the generation ID to be recalculated, then add a listener. |
37 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); | 92 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); |
38 pixelRef->addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count))); | 93 pixelRef->addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count))); |
39 pixelRef->notifyPixelsChanged(); | 94 pixelRef->notifyPixelsChanged(); |
40 REPORTER_ASSERT(r, 1 == count); | 95 REPORTER_ASSERT(r, 1 == count); |
41 | 96 |
42 // Quick check that NULL is safe. | 97 // Quick check that NULL is safe. |
43 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); | 98 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); |
44 pixelRef->addGenIDChangeListener(NULL); | 99 pixelRef->addGenIDChangeListener(NULL); |
45 pixelRef->notifyPixelsChanged(); | 100 pixelRef->notifyPixelsChanged(); |
| 101 |
| 102 test_install(r); |
| 103 test_dont_leak_install(r); |
46 } | 104 } |
OLD | NEW |