Flutter Engine
The Flutter Engine
Classes | Macros | Functions | Variables
RefCntTest.cpp File Reference
#include "include/core/SkRefCnt.h"
#include "include/core/SkTypes.h"
#include "include/private/SkWeakRefCnt.h"
#include "include/private/base/SkDebug.h"
#include "tests/Test.h"
#include <thread>
#include <utility>

Go to the source code of this file.

Classes

class  Effect
 
class  Paint
 
struct  EffectImpl
 

Macros

#define check(reporter, ref, unref, make, kill)
 

Functions

static void bounce_ref (void *data)
 
static void test_refCnt (skiatest::Reporter *reporter)
 
static void bounce_weak_ref (void *data)
 
static void bounce_weak_weak_ref (void *data)
 
static void test_weakRefCnt (skiatest::Reporter *reporter)
 
 DEF_TEST (RefCnt, reporter)
 
static sk_sp< EffectCreate ()
 
static sk_sp< Effectmake_effect ()
 
static void reset_counters ()
 
 DEF_TEST (sk_sp, reporter)
 
static sk_sp< FooAbstract > make_foo ()
 
 DEF_TEST (sk_make_sp, r)
 
 DEF_TEST (sk_sp_reset, r)
 
 DEF_TEST (sk_sp_ref, r)
 

Variables

static int gRefCounter
 
static int gUnrefCounter
 
static int gNewCounter
 
static int gDeleteCounter
 

Macro Definition Documentation

◆ check

#define check (   reporter,
  ref,
  unref,
  make,
  kill 
)
Value:
REPORTER_ASSERT(reporter, gUnrefCounter == unref); \
REPORTER_ASSERT(reporter, gNewCounter == make); \
REPORTER_ASSERT(reporter, gDeleteCounter == kill)
reporter
Definition: FontMgrTest.cpp:39
static int gRefCounter
Definition: RefCntTest.cpp:80
static int gDeleteCounter
Definition: RefCntTest.cpp:83
static int gUnrefCounter
Definition: RefCntTest.cpp:81
static int gNewCounter
Definition: RefCntTest.cpp:82
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
static void make(SkBitmap *bitmap, SkColorType colorType, SkAlphaType alphaType, sk_sp< SkColorSpace > colorSpace)
Definition: encode_srgb.cpp:35

Definition at line 85 of file RefCntTest.cpp.

Function Documentation

◆ bounce_ref()

static void bounce_ref ( void *  data)
static

Definition at line 17 of file RefCntTest.cpp.

17 {
18 SkRefCnt* ref = static_cast<SkRefCnt*>(data);
19 for (int i = 0; i < 100000; ++i) {
20 ref->ref();
21 ref->unref();
22 }
23}
void ref() const
Definition: SkRefCnt.h:62
void unref() const
Definition: SkRefCnt.h:72
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63

◆ bounce_weak_ref()

static void bounce_weak_ref ( void *  data)
static

Definition at line 38 of file RefCntTest.cpp.

38 {
39 SkWeakRefCnt* ref = static_cast<SkWeakRefCnt*>(data);
40 for (int i = 0; i < 100000; ++i) {
41 if (ref->try_ref()) {
42 ref->unref();
43 }
44 }
45}
bool try_ref() const
Definition: SkWeakRefCnt.h:103

◆ bounce_weak_weak_ref()

static void bounce_weak_weak_ref ( void *  data)
static

Definition at line 47 of file RefCntTest.cpp.

47 {
48 SkWeakRefCnt* ref = static_cast<SkWeakRefCnt*>(data);
49 for (int i = 0; i < 100000; ++i) {
50 ref->weak_ref();
51 ref->weak_unref();
52 }
53}
void weak_ref() const
Definition: SkWeakRefCnt.h:115
void weak_unref() const
Definition: SkWeakRefCnt.h:127

◆ Create()

static sk_sp< Effect > Create ( )
static

Definition at line 117 of file RefCntTest.cpp.

117 {
118 return sk_make_sp<Effect>();
119}

◆ DEF_TEST() [1/5]

DEF_TEST ( RefCnt  ,
reporter   
)

Definition at line 73 of file RefCntTest.cpp.

73 {
76}
static void test_weakRefCnt(skiatest::Reporter *reporter)
Definition: RefCntTest.cpp:55
static void test_refCnt(skiatest::Reporter *reporter)
Definition: RefCntTest.cpp:25

◆ DEF_TEST() [2/5]

DEF_TEST ( sk_make_sp  ,
 
)

Definition at line 346 of file RefCntTest.cpp.

346 {
347 auto x = make_foo();
348}
static sk_sp< FooAbstract > make_foo()
Definition: RefCntTest.cpp:341
double x

◆ DEF_TEST() [3/5]

DEF_TEST ( sk_sp  ,
reporter   
)

Definition at line 152 of file RefCntTest.cpp.

152 {
154
155 Paint paint;
158 check(reporter, 0, 0, 0, 0);
159
160 paint.set(Create());
161 check(reporter, 0, 0, 1, 0);
163
164 if (paint.get()) {
166 } else {
168 }
169 if (!paint.get()) {
171 } else {
173 }
174
175 paint.set(nullptr);
176 check(reporter, 0, 1, 1, 1);
177
178 if (paint.get()) {
180 } else {
182 }
183 if (!paint.get()) {
185 } else {
187 }
188
189 auto e = Create();
190 REPORTER_ASSERT(reporter, sizeof(e) == sizeof(void*));
191
192 check(reporter, 0, 1, 2, 1);
193 paint.set(e);
194 check(reporter, 1, 1, 2, 1);
196
197 Paint paint2;
198 paint2.set(paint.get());
199 check(reporter, 2, 1, 2, 1);
201
202 // Test sk_sp::operator->
203 delete paint.get()->method();
204 check(reporter, 2, 1, 2, 1);
205
206 // Test sk_sp::operator*
207 delete (*paint.get()).method();
208 check(reporter, 2, 1, 2, 1);
209
210 paint.set(nullptr);
211 e = nullptr;
212 paint2.set(nullptr);
213 check(reporter, 2, 4, 2, 2);
214
216 {
217 // Test convertible sk_sp assignment.
218 check(reporter, 0, 0, 0, 0);
219 sk_sp<Effect> foo(nullptr);
221 foo = make_effect();
223 check(reporter, 0, 0, 1, 0);
224 }
225 check(reporter, 0, 1, 1, 1);
226
227 // Test passing convertible rvalue into funtion.
230 check(reporter, 0, 0, 1, 0);
231 paint.set(nullptr);
232 check(reporter, 0, 1, 1, 1);
233
235 auto baz = EffectImpl::Create();
236 check(reporter, 0, 0, 1, 0);
237 paint.set(std::move(baz));
238 check(reporter, 0, 0, 1, 0);
239 REPORTER_ASSERT(reporter, !baz); // NOLINT(bugprone-use-after-move)
240 paint.set(nullptr);
241 check(reporter, 0, 1, 1, 1);
242
244 {
245 // test comparison operator with convertible type.
247 sk_sp<Effect> bar2(bar1); // convertible copy constructor
248 check(reporter, 1, 0, 1, 0);
251 REPORTER_ASSERT(reporter, bar1 == bar2);
252 REPORTER_ASSERT(reporter, bar2 == bar1);
253 REPORTER_ASSERT(reporter, !(bar1 != bar2));
254 REPORTER_ASSERT(reporter, !(bar2 != bar1));
255 sk_sp<Effect> bar3(nullptr);
256 bar3 = bar1; // convertible copy assignment
257 check(reporter, 2, 0, 1, 0);
258
259 }
260 check(reporter, 2, 3, 1, 1);
261
262 // test passing convertible copy into funtion.
264 baz = EffectImpl::Create();
265 check(reporter, 0, 0, 1, 0);
266 paint.set(baz);
267 check(reporter, 1, 0, 1, 0);
268 baz = nullptr;
269 check(reporter, 1, 1, 1, 0);
270 paint.set(nullptr);
271 check(reporter, 1, 2, 1, 1);
272
273 {
275 sk_sp<SkRefCnt> notEmpty = sk_make_sp<SkRefCnt>();
277
278 REPORTER_ASSERT(reporter, notEmpty != empty);
279 REPORTER_ASSERT(reporter, empty != notEmpty);
280
281 REPORTER_ASSERT(reporter, nullptr == empty);
282 REPORTER_ASSERT(reporter, empty == nullptr);
284 }
285
286 {
287 sk_sp<SkRefCnt> a = sk_make_sp<SkRefCnt>();
288 sk_sp<SkRefCnt> b = sk_make_sp<SkRefCnt>();
291 }
292
293 // http://wg21.cmeerw.net/lwg/issue998
294 {
295 class foo : public SkRefCnt {
296 public:
297 foo() : bar(this) {}
298 void reset() { bar.reset(); }
299 private:
300 sk_sp<foo> bar;
301 };
302 // The following should properly delete the object and not cause undefined behavior.
303 // This is an ugly example, but the same issue can arise in more subtle ways.
304 (new foo)->reset();
305 }
306
307 // https://crrev.com/0d4ef2583a6f19c3e61be04d36eb1a60b133832c
308 {
309 struct StructB;
310 struct StructA : public SkRefCnt {
312 };
313
314 struct StructB : public SkRefCnt {
316 ~StructB() override {} // Some clang versions don't emit this implicitly.
317 };
318
319 // Create a reference cycle.
320 StructA* a = new StructA;
321 a->b.reset(new StructB);
322 a->b->a.reset(a);
323
324 // Break the cycle by calling reset(). This will cause |a| (and hence, |a.b|)
325 // to be deleted before the call to reset() returns. This tests that the
326 // implementation of sk_sp::reset() doesn't access |this| after it
327 // deletes the underlying pointer. This behaviour is consistent with the
328 // definition of unique_ptr::reset in C++11.
329 a->b.reset();
330 }
331}
m reset()
#define check(reporter, ref, unref, make, kill)
Definition: RefCntTest.cpp:85
static sk_sp< Effect > make_effect()
Definition: RefCntTest.cpp:140
static void reset_counters()
Definition: RefCntTest.cpp:146
static sk_sp< Effect > Create()
Definition: RefCntTest.cpp:117
int * method() const
Definition: RefCntTest.cpp:114
int fRefCnt
Definition: RefCntTest.cpp:98
void set(sk_sp< Effect > value)
Definition: RefCntTest.cpp:127
sk_sp< Effect > fEffect
Definition: RefCntTest.cpp:123
const sk_sp< Effect > & get() const
Definition: RefCntTest.cpp:125
T * get() const
Definition: SkRefCnt.h:303
const Paint & paint
Definition: color_source.cc:38
static bool b
struct MyStruct a[10]
EMSCRIPTEN_KEEPALIVE void empty()
static sk_sp< EffectImpl > Create()
Definition: RefCntTest.cpp:135

◆ DEF_TEST() [4/5]

DEF_TEST ( sk_sp_ref  ,
 
)

Definition at line 368 of file RefCntTest.cpp.

368 {
369 SkRefCnt* rc = new SkRefCnt;
370 REPORTER_ASSERT(r, rc->unique());
371
372 {
374 REPORTER_ASSERT(r, !rc->unique());
375 }
376
377 REPORTER_ASSERT(r, rc->unique());
378 rc->unref();
379}
sk_sp< T > sk_ref_sp(T *obj)
Definition: SkRefCnt.h:381

◆ DEF_TEST() [5/5]

DEF_TEST ( sk_sp_reset  ,
 
)

Definition at line 352 of file RefCntTest.cpp.

352 {
353 SkRefCnt* rc = new SkRefCnt;
354 REPORTER_ASSERT(r, rc->unique());
355
357 sp.reset(rc);
358 // We have transfered our ownership over to sp
359 REPORTER_ASSERT(r, rc->unique());
360
361 rc->ref(); // now "rc" is also an owner
362 REPORTER_ASSERT(r, !rc->unique());
363
364 sp.reset(rc); // this should transfer our ownership over to sp
365 REPORTER_ASSERT(r, rc->unique());
366}
void reset(T *ptr=nullptr)
Definition: SkRefCnt.h:310

◆ make_effect()

static sk_sp< Effect > make_effect ( )
static

Definition at line 140 of file RefCntTest.cpp.

140 {
141 auto foo = EffectImpl::Create();
142 foo->fValue = 42;
143 return foo;
144}

◆ make_foo()

static sk_sp< FooAbstract > make_foo ( )
static

Definition at line 341 of file RefCntTest.cpp.

341 {
342 // can not cast FooConcrete to FooAbstract.
343 // can cast FooConcrete* to FooAbstract*.
344 return sk_make_sp<FooConcrete>();
345}

◆ reset_counters()

static void reset_counters ( )
static

Definition at line 146 of file RefCntTest.cpp.

146 {
147 gRefCounter = 0;
148 gUnrefCounter = 0;
149 gNewCounter = 0;
150 gDeleteCounter = 0;
151}

◆ test_refCnt()

static void test_refCnt ( skiatest::Reporter reporter)
static

Definition at line 25 of file RefCntTest.cpp.

25 {
26 SkRefCnt* ref = new SkRefCnt();
27
28 std::thread thing1(bounce_ref, ref);
29 std::thread thing2(bounce_ref, ref);
30
31 thing1.join();
32 thing2.join();
33
35 ref->unref();
36}
static void bounce_ref(void *data)
Definition: RefCntTest.cpp:17
bool unique() const
Definition: SkRefCnt.h:50

◆ test_weakRefCnt()

static void test_weakRefCnt ( skiatest::Reporter reporter)
static

Definition at line 55 of file RefCntTest.cpp.

55 {
56 SkWeakRefCnt* ref = new SkWeakRefCnt();
57
58 std::thread thing1(bounce_ref, ref);
59 std::thread thing2(bounce_ref, ref);
60 std::thread thing3(bounce_weak_ref, ref);
61 std::thread thing4(bounce_weak_weak_ref, ref);
62
63 thing1.join();
64 thing2.join();
65 thing3.join();
66 thing4.join();
67
69 SkDEBUGCODE(REPORTER_ASSERT(reporter, ref->getWeakCnt() == 1));
70 ref->unref();
71}
static void bounce_weak_ref(void *data)
Definition: RefCntTest.cpp:38
static void bounce_weak_weak_ref(void *data)
Definition: RefCntTest.cpp:47
SkDEBUGCODE(SK_SPI) SkThreadID SkGetThreadID()

Variable Documentation

◆ gDeleteCounter

int gDeleteCounter
static

Definition at line 83 of file RefCntTest.cpp.

◆ gNewCounter

int gNewCounter
static

Definition at line 82 of file RefCntTest.cpp.

◆ gRefCounter

int gRefCounter
static

Definition at line 80 of file RefCntTest.cpp.

◆ gUnrefCounter

int gUnrefCounter
static

Definition at line 81 of file RefCntTest.cpp.