blob: e13d0e07e5476804ced4dfeccf8748c02e578339 [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
commit-bot@chromium.org50a30432013-10-24 17:44:27 +00006class TestListener : public SkPixelRef::GenIDChangeListener {
7public:
8 explicit TestListener(int* ptr) : fPtr(ptr) {}
mtklein36352bf2015-03-25 18:17:31 -07009 void onChange() override { (*fPtr)++; }
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000010private:
11 int* fPtr;
12};
13
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000014DEF_TEST(PixelRef_GenIDChange, r) {
commit-bot@chromium.org32678d92014-01-15 02:38:22 +000015 SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
reed@google.combf790232013-12-13 19:45:58 +000016
17 SkAutoTUnref<SkPixelRef> pixelRef(SkMallocPixelRef::NewAllocate(info, 0, NULL));
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000018
19 // Register a listener.
20 int count = 0;
reed@google.combf790232013-12-13 19:45:58 +000021 pixelRef->addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count)));
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000022 REPORTER_ASSERT(r, 0 == count);
23
24 // No one has looked at our pixelRef's generation ID, so invalidating it doesn't make sense.
25 // (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 +000026 pixelRef->notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000027 REPORTER_ASSERT(r, 0 == count);
28
29 // Force the generation ID to be calculated.
reed@google.combf790232013-12-13 19:45:58 +000030 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000031
32 // Our listener was dropped in the first call to notifyPixelsChanged(). This is a no-op.
reed@google.combf790232013-12-13 19:45:58 +000033 pixelRef->notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000034 REPORTER_ASSERT(r, 0 == count);
35
36 // Force the generation ID to be recalculated, then add a listener.
reed@google.combf790232013-12-13 19:45:58 +000037 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
38 pixelRef->addGenIDChangeListener(SkNEW_ARGS(TestListener, (&count)));
39 pixelRef->notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000040 REPORTER_ASSERT(r, 1 == count);
41
42 // Quick check that NULL is safe.
reed@google.combf790232013-12-13 19:45:58 +000043 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
44 pixelRef->addGenIDChangeListener(NULL);
45 pixelRef->notifyPixelsChanged();
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000046}