commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 1 | #include "Test.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 2 | |
| 3 | #include "SkPixelRef.h" |
| 4 | #include "SkMallocPixelRef.h" |
| 5 | |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 6 | static void test_info(skiatest::Reporter* reporter) { |
| 7 | static const struct { |
| 8 | SkBitmap::Config fConfig; |
| 9 | SkAlphaType fAlphaType; |
| 10 | SkColorType fExpectedColorType; |
| 11 | bool fExpectedSuccess; |
| 12 | } gRec[] = { |
| 13 | { SkBitmap::kNo_Config, kPremul_SkAlphaType, kPMColor_SkColorType, false }, |
| 14 | { SkBitmap::kARGB_8888_Config, kPremul_SkAlphaType, kPMColor_SkColorType, true }, |
| 15 | { SkBitmap::kARGB_8888_Config, kOpaque_SkAlphaType, kPMColor_SkColorType, true }, |
| 16 | { SkBitmap::kRGB_565_Config, kOpaque_SkAlphaType, kRGB_565_SkColorType, true }, |
| 17 | { SkBitmap::kARGB_4444_Config, kPremul_SkAlphaType, kARGB_4444_SkColorType, true }, |
| 18 | { SkBitmap::kARGB_4444_Config, kOpaque_SkAlphaType, kARGB_4444_SkColorType, true }, |
| 19 | { SkBitmap::kA8_Config, kPremul_SkAlphaType, kAlpha_8_SkColorType, true }, |
| 20 | { SkBitmap::kA8_Config, kOpaque_SkAlphaType, kAlpha_8_SkColorType, true }, |
| 21 | { SkBitmap::kIndex8_Config, kPremul_SkAlphaType, kIndex_8_SkColorType, true }, |
| 22 | { SkBitmap::kIndex8_Config, kOpaque_SkAlphaType, kIndex_8_SkColorType, true }, |
| 23 | }; |
| 24 | |
| 25 | SkBitmap bitmap; |
| 26 | SkImageInfo info; |
| 27 | |
| 28 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { |
| 29 | bool success = bitmap.setConfig(gRec[i].fConfig, 10, 10, 0, gRec[i].fAlphaType); |
| 30 | REPORTER_ASSERT(reporter, success); |
| 31 | success = bitmap.asImageInfo(&info); |
| 32 | REPORTER_ASSERT(reporter, success == gRec[i].fExpectedSuccess); |
| 33 | if (gRec[i].fExpectedSuccess) { |
| 34 | REPORTER_ASSERT(reporter, info.fAlphaType == gRec[i].fAlphaType); |
| 35 | REPORTER_ASSERT(reporter, info.fColorType == gRec[i].fExpectedColorType); |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 40 | namespace { |
| 41 | |
| 42 | class TestListener : public SkPixelRef::GenIDChangeListener { |
| 43 | public: |
| 44 | explicit TestListener(int* ptr) : fPtr(ptr) {} |
| 45 | void onChange() SK_OVERRIDE { (*fPtr)++; } |
| 46 | private: |
| 47 | int* fPtr; |
| 48 | }; |
| 49 | |
| 50 | } // namespace |
| 51 | |
| 52 | DEF_TEST(PixelRef_GenIDChange, r) { |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame^] | 53 | SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 54 | |
| 55 | SkAutoTUnref<SkPixelRef> pixelRef(SkMallocPixelRef::NewAllocate(info, 0, NULL)); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 56 | |
| 57 | // Register a listener. |
| 58 | int count = 0; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 59 | pixelRef->addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count))); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 60 | REPORTER_ASSERT(r, 0 == count); |
| 61 | |
| 62 | // No one has looked at our pixelRef's generation ID, so invalidating it doesn't make sense. |
| 63 | // (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] | 64 | pixelRef->notifyPixelsChanged(); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 65 | REPORTER_ASSERT(r, 0 == count); |
| 66 | |
| 67 | // Force the generation ID to be calculated. |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 68 | REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 69 | |
| 70 | // 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] | 71 | pixelRef->notifyPixelsChanged(); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 72 | REPORTER_ASSERT(r, 0 == count); |
| 73 | |
| 74 | // Force the generation ID to be recalculated, then add a listener. |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 75 | REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); |
| 76 | pixelRef->addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count))); |
| 77 | pixelRef->notifyPixelsChanged(); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 78 | REPORTER_ASSERT(r, 1 == count); |
| 79 | |
| 80 | // Quick check that NULL is safe. |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 81 | REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); |
| 82 | pixelRef->addGenIDChangeListener(NULL); |
| 83 | pixelRef->notifyPixelsChanged(); |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 84 | |
| 85 | test_info(r); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 86 | } |