blob: 470221c0fea0c44196751c9fcfc96e94fea81d44 [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.com398337b2013-12-11 21:22:39 +000054 SkMallocPixelRef pixelRef(NULL, 0, NULL); // We don't really care about the pixels here.
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000055
56 // Register a listener.
57 int count = 0;
reed@google.com398337b2013-12-11 21:22:39 +000058 pixelRef.addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count)));
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000059 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.com398337b2013-12-11 21:22:39 +000063 pixelRef.notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000064 REPORTER_ASSERT(r, 0 == count);
65
66 // Force the generation ID to be calculated.
reed@google.com398337b2013-12-11 21:22:39 +000067 REPORTER_ASSERT(r, 0 != pixelRef.getGenerationID());
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000068
69 // Our listener was dropped in the first call to notifyPixelsChanged(). This is a no-op.
reed@google.com398337b2013-12-11 21:22:39 +000070 pixelRef.notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000071 REPORTER_ASSERT(r, 0 == count);
72
73 // Force the generation ID to be recalculated, then add a listener.
reed@google.com398337b2013-12-11 21:22:39 +000074 REPORTER_ASSERT(r, 0 != pixelRef.getGenerationID());
75 pixelRef.addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count)));
76 pixelRef.notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000077 REPORTER_ASSERT(r, 1 == count);
78
79 // Quick check that NULL is safe.
reed@google.com398337b2013-12-11 21:22:39 +000080 REPORTER_ASSERT(r, 0 != pixelRef.getGenerationID());
81 pixelRef.addGenIDChangeListener(NULL);
82 pixelRef.notifyPixelsChanged();
reed@google.com9230ea22013-12-09 22:01:03 +000083
84 test_info(r);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000085}