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