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 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 3 | #include "SkMallocPixelRef.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 4 | #include "SkPixelRef.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 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); |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 33 | if (success && gRec[i].fExpectedSuccess) { |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 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 | class TestListener : public SkPixelRef::GenIDChangeListener { |
| 41 | public: |
| 42 | explicit TestListener(int* ptr) : fPtr(ptr) {} |
tfarina@chromium.org | 5867481 | 2014-01-21 23:39:22 +0000 | [diff] [blame] | 43 | virtual void onChange() SK_OVERRIDE { (*fPtr)++; } |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 44 | private: |
| 45 | int* fPtr; |
| 46 | }; |
| 47 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 48 | DEF_TEST(PixelRef_GenIDChange, r) { |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 49 | SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 50 | |
| 51 | SkAutoTUnref<SkPixelRef> pixelRef(SkMallocPixelRef::NewAllocate(info, 0, NULL)); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 52 | |
| 53 | // Register a listener. |
| 54 | int count = 0; |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 55 | pixelRef->addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count))); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 56 | REPORTER_ASSERT(r, 0 == count); |
| 57 | |
| 58 | // No one has looked at our pixelRef's generation ID, so invalidating it doesn't make sense. |
| 59 | // (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] | 60 | pixelRef->notifyPixelsChanged(); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 61 | REPORTER_ASSERT(r, 0 == count); |
| 62 | |
| 63 | // Force the generation ID to be calculated. |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 64 | REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 65 | |
| 66 | // 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] | 67 | pixelRef->notifyPixelsChanged(); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 68 | REPORTER_ASSERT(r, 0 == count); |
| 69 | |
| 70 | // Force the generation ID to be recalculated, then add a listener. |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 71 | REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); |
| 72 | pixelRef->addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count))); |
| 73 | pixelRef->notifyPixelsChanged(); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 74 | REPORTER_ASSERT(r, 1 == count); |
| 75 | |
| 76 | // Quick check that NULL is safe. |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 77 | REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID()); |
| 78 | pixelRef->addGenIDChangeListener(NULL); |
| 79 | pixelRef->notifyPixelsChanged(); |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 80 | |
| 81 | test_info(r); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 82 | } |