Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Functions
PixelRefTest.cpp File Reference
#include "include/core/SkBitmap.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkMallocPixelRef.h"
#include "include/core/SkPixelRef.h"
#include "include/core/SkRefCnt.h"
#include "include/private/SkIDChangeListener.h"
#include "tests/Test.h"

Go to the source code of this file.

Classes

class  TestListener
 

Functions

static void decrement_counter_proc (void *pixels, void *ctx)
 
static void test_dont_leak_install (skiatest::Reporter *reporter)
 
static void test_install (skiatest::Reporter *reporter)
 
 DEF_TEST (PixelRef_GenIDChange, r)
 

Function Documentation

◆ decrement_counter_proc()

static void decrement_counter_proc ( void *  pixels,
void *  ctx 
)
static

Definition at line 16 of file PixelRefTest.cpp.

16 {
17 int* counter = (int*)ctx;
18 *counter -= 1;
19}

◆ DEF_TEST()

DEF_TEST ( PixelRef_GenIDChange  ,
 
)

Definition at line 72 of file PixelRefTest.cpp.

72 {
74
76
77 // Register a listener.
78 int count = 0;
79 pixelRef->addGenIDChangeListener(sk_make_sp<TestListener>(&count));
80 REPORTER_ASSERT(r, 0 == count);
81
82 // No one has looked at our pixelRef's generation ID, so invalidating it doesn't make sense.
83 // (An SkPixelRef tree falls in the forest but there's nobody around to hear it. Do we care?)
84 pixelRef->notifyPixelsChanged();
85 REPORTER_ASSERT(r, 0 == count);
86
87 // Force the generation ID to be calculated.
88 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
89
90 // Our listener was dropped in the first call to notifyPixelsChanged(). This is a no-op.
91 pixelRef->notifyPixelsChanged();
92 REPORTER_ASSERT(r, 0 == count);
93
94 // Force the generation ID to be recalculated, then add a listener.
95 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
96 pixelRef->addGenIDChangeListener(sk_make_sp<TestListener>(&count));
97 pixelRef->notifyPixelsChanged();
98 REPORTER_ASSERT(r, 1 == count);
99
100 // Check that asking for deregistration causes the listener to not be called.
101 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
102 auto listener = sk_make_sp<TestListener>(&count);
103 pixelRef->addGenIDChangeListener(listener);
104 REPORTER_ASSERT(r, 1 == count);
105 listener->markShouldDeregister();
106 pixelRef->notifyPixelsChanged();
107 REPORTER_ASSERT(r, 1 == count);
108
109 // Check that we use deregistration to prevent unbounded growth.
110 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
111 listener = sk_make_sp<TestListener>(&count);
112 pixelRef->addGenIDChangeListener(listener);
113 REPORTER_ASSERT(r, 1 == count);
114 listener->markShouldDeregister();
115 // Add second listener. Should deregister first listener.
116 pixelRef->addGenIDChangeListener(sk_make_sp<TestListener>(&count));
117 REPORTER_ASSERT(r, listener->unique());
118
119 // Quick check that nullptr is safe.
120 REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
121 pixelRef->addGenIDChangeListener(nullptr);
122 pixelRef->notifyPixelsChanged();
123
124 test_install(r);
126}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
int count
static void test_dont_leak_install(skiatest::Reporter *reporter)
static void test_install(skiatest::Reporter *reporter)
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
SK_API sk_sp< SkPixelRef > MakeAllocate(const SkImageInfo &, size_t rowBytes)
static SkImageInfo MakeN32Premul(int width, int height)

◆ test_dont_leak_install()

static void test_dont_leak_install ( skiatest::Reporter reporter)
static

Definition at line 21 of file PixelRefTest.cpp.

21 {
22 bool success;
23 int release_counter;
25 SkBitmap bm;
26
28 release_counter = 1;
29 success = bm.installPixels(info, nullptr, 0, decrement_counter_proc, &release_counter);
30 REPORTER_ASSERT(reporter, true == success);
31 bm.reset();
32 REPORTER_ASSERT(reporter, 0 == release_counter);
33
35 release_counter = 1;
36 success = bm.installPixels(info, nullptr, 0, decrement_counter_proc, &release_counter);
37 REPORTER_ASSERT(reporter, true == success);
38 bm.reset();
39 REPORTER_ASSERT(reporter, 0 == release_counter);
40
42 release_counter = 1;
43 success = bm.installPixels(info, nullptr, 0, decrement_counter_proc, &release_counter);
44 REPORTER_ASSERT(reporter, false == success);
45 bm.reset();
46 REPORTER_ASSERT(reporter, 0 == release_counter);
47}
reporter
static void decrement_counter_proc(void *pixels, void *ctx)
bool installPixels(const SkImageInfo &info, void *pixels, size_t rowBytes, void(*releaseProc)(void *addr, void *context), void *context)
Definition SkBitmap.cpp:323
void reset()
Definition SkBitmap.cpp:92

◆ test_install()

static void test_install ( skiatest::Reporter reporter)
static

Definition at line 49 of file PixelRefTest.cpp.

49 {
50 bool success;
52 SkBitmap bm;
53 // make sure we don't assert on an empty install
54 success = bm.installPixels(info, nullptr, 0);
55 REPORTER_ASSERT(reporter, success);
56
57 // no pixels should be the same as setInfo()
59 success = bm.installPixels(info, nullptr, 0);
60 REPORTER_ASSERT(reporter, success);
61
62}