blob: c0bdf54c25cb4d21202083f0659388df1efccdd4 [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
commit-bot@chromium.org50a30432013-10-24 17:44:27 +00003#include "SkMallocPixelRef.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +00004#include "SkPixelRef.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +00005
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 +000040class TestListener : public SkPixelRef::GenIDChangeListener {
41public:
42 explicit TestListener(int* ptr) : fPtr(ptr) {}
tfarina@chromium.org58674812014-01-21 23:39:22 +000043 virtual void onChange() SK_OVERRIDE { (*fPtr)++; }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000044private:
45 int* fPtr;
46};
47
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000048DEF_TEST(PixelRef_GenIDChange, r) {
commit-bot@chromium.org32678d92014-01-15 02:38:22 +000049 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
reed@google.combf790232013-12-13 19:45:58 +000050
51 SkAutoTUnref<SkPixelRef> pixelRef(SkMallocPixelRef::NewAllocate(info, 0, NULL));
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000052
53 // Register a listener.
54 int count = 0;
reed@google.combf790232013-12-13 19:45:58 +000055 pixelRef->addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count)));
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000056 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.combf790232013-12-13 19:45:58 +000060 pixelRef->notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000061 REPORTER_ASSERT(r, 0 == count);
62
63 // Force the generation ID to be calculated.
reed@google.combf790232013-12-13 19:45:58 +000064 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000065
66 // Our listener was dropped in the first call to notifyPixelsChanged(). This is a no-op.
reed@google.combf790232013-12-13 19:45:58 +000067 pixelRef->notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000068 REPORTER_ASSERT(r, 0 == count);
69
70 // Force the generation ID to be recalculated, then add a listener.
reed@google.combf790232013-12-13 19:45:58 +000071 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
72 pixelRef->addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count)));
73 pixelRef->notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000074 REPORTER_ASSERT(r, 1 == count);
75
76 // Quick check that NULL is safe.
reed@google.combf790232013-12-13 19:45:58 +000077 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
78 pixelRef->addGenIDChangeListener(NULL);
79 pixelRef->notifyPixelsChanged();
reed@google.com9230ea22013-12-09 22:01:03 +000080
81 test_info(r);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000082}