blob: e0ffd7a6017537fa78b1f3f03a4bd519f3b8c7ec [file] [log] [blame]
commit-bot@chromium.org50a30432013-10-24 17:44:27 +00001#include "Test.h"
2#include "TestClassDef.h"
3
4#include "SkPixelRef.h"
5#include "SkMallocPixelRef.h"
6
reed@google.com9230ea22013-12-09 22:01:03 +00007static 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.org50a30432013-10-24 17:44:27 +000041namespace {
42
43class TestListener : public SkPixelRef::GenIDChangeListener {
44public:
45 explicit TestListener(int* ptr) : fPtr(ptr) {}
46 void onChange() SK_OVERRIDE { (*fPtr)++; }
47private:
48 int* fPtr;
49};
50
51} // namespace
52
53DEF_TEST(PixelRef_GenIDChange, r) {
reed@google.combf790232013-12-13 19:45:58 +000054 SkImageInfo info = { 10, 10, kPMColor_SkColorType, kPremul_SkAlphaType };
55
56 SkAutoTUnref<SkPixelRef> pixelRef(SkMallocPixelRef::NewAllocate(info, 0, NULL));
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000057
58 // Register a listener.
59 int count = 0;
reed@google.combf790232013-12-13 19:45:58 +000060 pixelRef->addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count)));
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000061 REPORTER_ASSERT(r, 0 == count);
62
63 // No one has looked at our pixelRef's generation ID, so invalidating it doesn't make sense.
64 // (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 +000065 pixelRef->notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000066 REPORTER_ASSERT(r, 0 == count);
67
68 // Force the generation ID to be calculated.
reed@google.combf790232013-12-13 19:45:58 +000069 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000070
71 // Our listener was dropped in the first call to notifyPixelsChanged(). This is a no-op.
reed@google.combf790232013-12-13 19:45:58 +000072 pixelRef->notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000073 REPORTER_ASSERT(r, 0 == count);
74
75 // Force the generation ID to be recalculated, then add a listener.
reed@google.combf790232013-12-13 19:45:58 +000076 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
77 pixelRef->addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count)));
78 pixelRef->notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000079 REPORTER_ASSERT(r, 1 == count);
80
81 // Quick check that NULL is safe.
reed@google.combf790232013-12-13 19:45:58 +000082 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
83 pixelRef->addGenIDChangeListener(NULL);
84 pixelRef->notifyPixelsChanged();
reed@google.com9230ea22013-12-09 22:01:03 +000085
86 test_info(r);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000087}