blob: d7fe9f47aad51a6d0f77e1986b1289d68ef458bd [file] [log] [blame]
commit-bot@chromium.org50a30432013-10-24 17:44:27 +00001#include "Test.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +00002
3#include "SkPixelRef.h"
4#include "SkMallocPixelRef.h"
5
reed@google.com9230ea22013-12-09 22:01:03 +00006static 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.org50a30432013-10-24 17:44:27 +000040namespace {
41
42class TestListener : public SkPixelRef::GenIDChangeListener {
43public:
44 explicit TestListener(int* ptr) : fPtr(ptr) {}
45 void onChange() SK_OVERRIDE { (*fPtr)++; }
46private:
47 int* fPtr;
48};
49
50} // namespace
51
52DEF_TEST(PixelRef_GenIDChange, r) {
commit-bot@chromium.org32678d92014-01-15 02:38:22 +000053 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
reed@google.combf790232013-12-13 19:45:58 +000054
55 SkAutoTUnref<SkPixelRef> pixelRef(SkMallocPixelRef::NewAllocate(info, 0, NULL));
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000056
57 // Register a listener.
58 int count = 0;
reed@google.combf790232013-12-13 19:45:58 +000059 pixelRef->addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count)));
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000060 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.combf790232013-12-13 19:45:58 +000064 pixelRef->notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000065 REPORTER_ASSERT(r, 0 == count);
66
67 // Force the generation ID to be calculated.
reed@google.combf790232013-12-13 19:45:58 +000068 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000069
70 // Our listener was dropped in the first call to notifyPixelsChanged(). This is a no-op.
reed@google.combf790232013-12-13 19:45:58 +000071 pixelRef->notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000072 REPORTER_ASSERT(r, 0 == count);
73
74 // Force the generation ID to be recalculated, then add a listener.
reed@google.combf790232013-12-13 19:45:58 +000075 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
76 pixelRef->addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count)));
77 pixelRef->notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000078 REPORTER_ASSERT(r, 1 == count);
79
80 // Quick check that NULL is safe.
reed@google.combf790232013-12-13 19:45:58 +000081 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
82 pixelRef->addGenIDChangeListener(NULL);
83 pixelRef->notifyPixelsChanged();
reed@google.com9230ea22013-12-09 22:01:03 +000084
85 test_info(r);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000086}