bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 8 | #include "SkRefCnt.h" |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 9 | #include "SkThreadUtils.h" |
| 10 | #include "SkTypes.h" |
| 11 | #include "SkWeakRefCnt.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 12 | #include "Test.h" |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 13 | |
| 14 | static void bounce_ref(void* data) { |
| 15 | SkRefCnt* ref = static_cast<SkRefCnt*>(data); |
| 16 | for (int i = 0; i < 100000; ++i) { |
| 17 | ref->ref(); |
| 18 | ref->unref(); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | static void test_refCnt(skiatest::Reporter* reporter) { |
| 23 | SkRefCnt* ref = new SkRefCnt(); |
| 24 | |
| 25 | SkThread thing1(bounce_ref, ref); |
| 26 | SkThread thing2(bounce_ref, ref); |
| 27 | |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 28 | SkASSERT(thing1.start()); |
| 29 | SkASSERT(thing2.start()); |
| 30 | |
| 31 | thing1.join(); |
| 32 | thing2.join(); |
| 33 | |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 34 | REPORTER_ASSERT(reporter, ref->unique()); |
bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame] | 35 | ref->unref(); |
| 36 | } |
| 37 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 38 | static void bounce_weak_ref(void* data) { |
| 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 | } |
| 46 | |
| 47 | static void bounce_weak_weak_ref(void* data) { |
| 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 | } |
| 54 | |
| 55 | static void test_weakRefCnt(skiatest::Reporter* reporter) { |
| 56 | SkWeakRefCnt* ref = new SkWeakRefCnt(); |
| 57 | |
| 58 | SkThread thing1(bounce_ref, ref); |
| 59 | SkThread thing2(bounce_ref, ref); |
| 60 | SkThread thing3(bounce_weak_ref, ref); |
| 61 | SkThread thing4(bounce_weak_weak_ref, ref); |
| 62 | |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 63 | SkASSERT(thing1.start()); |
| 64 | SkASSERT(thing2.start()); |
| 65 | SkASSERT(thing3.start()); |
| 66 | SkASSERT(thing4.start()); |
| 67 | |
| 68 | thing1.join(); |
| 69 | thing2.join(); |
| 70 | thing3.join(); |
| 71 | thing4.join(); |
| 72 | |
mtklein | bbb61d7 | 2014-11-24 13:09:39 -0800 | [diff] [blame] | 73 | REPORTER_ASSERT(reporter, ref->unique()); |
Mike Klein | 874a62a | 2014-07-09 09:04:07 -0400 | [diff] [blame] | 74 | REPORTER_ASSERT(reporter, ref->getWeakCnt() == 1); |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 75 | ref->unref(); |
| 76 | } |
| 77 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 78 | DEF_TEST(RefCnt, reporter) { |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 79 | test_refCnt(reporter); |
| 80 | test_weakRefCnt(reporter); |
| 81 | } |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 82 | |
| 83 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 84 | |
| 85 | static int gRefCounter; |
| 86 | static int gUnrefCounter; |
| 87 | static int gNewCounter; |
| 88 | static int gDeleteCounter; |
| 89 | |
| 90 | #define check(reporter, ref, unref, make, kill) \ |
| 91 | REPORTER_ASSERT(reporter, gRefCounter == ref); \ |
| 92 | REPORTER_ASSERT(reporter, gUnrefCounter == unref); \ |
| 93 | REPORTER_ASSERT(reporter, gNewCounter == make); \ |
| 94 | REPORTER_ASSERT(reporter, gDeleteCounter == kill); |
| 95 | |
| 96 | |
| 97 | class Effect { |
| 98 | public: |
| 99 | Effect() : fRefCnt(1) { |
| 100 | gNewCounter += 1; |
| 101 | } |
| 102 | |
| 103 | int fRefCnt; |
| 104 | |
| 105 | void ref() { |
| 106 | gRefCounter += 1; |
| 107 | fRefCnt += 1; |
| 108 | } |
| 109 | void unref() { |
| 110 | gUnrefCounter += 1; |
| 111 | |
| 112 | SkASSERT(fRefCnt > 0); |
| 113 | if (0 == --fRefCnt) { |
| 114 | gDeleteCounter += 1; |
| 115 | delete this; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | int* method() const { return new int; } |
| 120 | }; |
| 121 | |
| 122 | static sk_sp<Effect> Create() { |
halcanary | 217c0b3 | 2016-03-02 08:06:20 -0800 | [diff] [blame] | 123 | return sk_make_sp<Effect>(); |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | class Paint { |
| 127 | public: |
| 128 | sk_sp<Effect> fEffect; |
| 129 | |
| 130 | const sk_sp<Effect>& get() const { return fEffect; } |
| 131 | |
| 132 | void set(sk_sp<Effect> value) { |
| 133 | fEffect = std::move(value); |
| 134 | } |
| 135 | }; |
| 136 | |
halcanary | cb6cb38 | 2016-03-02 08:11:26 -0800 | [diff] [blame] | 137 | struct EffectImpl : public Effect { |
| 138 | static sk_sp<EffectImpl> Create() { |
| 139 | return sk_sp<EffectImpl>(new EffectImpl); |
| 140 | } |
| 141 | int fValue; |
| 142 | }; |
| 143 | static sk_sp<Effect> make_effect() { |
| 144 | auto foo = EffectImpl::Create(); |
| 145 | foo->fValue = 42; |
| 146 | return std::move(foo); |
| 147 | } |
| 148 | |
| 149 | static void reset_counters() { |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 150 | gRefCounter = 0; |
| 151 | gUnrefCounter = 0; |
| 152 | gNewCounter = 0; |
| 153 | gDeleteCounter = 0; |
halcanary | cb6cb38 | 2016-03-02 08:11:26 -0800 | [diff] [blame] | 154 | } |
| 155 | DEF_TEST(sk_sp, reporter) { |
| 156 | reset_counters(); |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 157 | |
| 158 | Paint paint; |
| 159 | REPORTER_ASSERT(reporter, paint.fEffect.get() == nullptr); |
| 160 | REPORTER_ASSERT(reporter, !paint.get()); |
| 161 | check(reporter, 0, 0, 0, 0); |
| 162 | |
| 163 | paint.set(Create()); |
| 164 | check(reporter, 0, 0, 1, 0); |
| 165 | REPORTER_ASSERT(reporter, paint.fEffect.get()->fRefCnt == 1); |
| 166 | |
bungeman | beab9e7 | 2016-03-03 07:50:49 -0800 | [diff] [blame] | 167 | if (paint.get()) { |
| 168 | REPORTER_ASSERT(reporter, true); |
| 169 | } else { |
| 170 | REPORTER_ASSERT(reporter, false); |
| 171 | } |
| 172 | if (!paint.get()) { |
| 173 | REPORTER_ASSERT(reporter, false); |
| 174 | } else { |
| 175 | REPORTER_ASSERT(reporter, true); |
| 176 | } |
| 177 | |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 178 | paint.set(nullptr); |
| 179 | check(reporter, 0, 1, 1, 1); |
| 180 | |
bungeman | beab9e7 | 2016-03-03 07:50:49 -0800 | [diff] [blame] | 181 | if (paint.get()) { |
| 182 | REPORTER_ASSERT(reporter, false); |
| 183 | } else { |
| 184 | REPORTER_ASSERT(reporter, true); |
| 185 | } |
| 186 | if (!paint.get()) { |
| 187 | REPORTER_ASSERT(reporter, true); |
| 188 | } else { |
| 189 | REPORTER_ASSERT(reporter, false); |
| 190 | } |
| 191 | |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 192 | auto e = Create(); |
| 193 | REPORTER_ASSERT(reporter, sizeof(e) == sizeof(void*)); |
| 194 | |
| 195 | check(reporter, 0, 1, 2, 1); |
| 196 | paint.set(e); |
| 197 | check(reporter, 1, 1, 2, 1); |
| 198 | REPORTER_ASSERT(reporter, paint.fEffect.get()->fRefCnt == 2); |
| 199 | |
| 200 | Paint paint2; |
| 201 | paint2.set(paint.get()); |
| 202 | check(reporter, 2, 1, 2, 1); |
| 203 | REPORTER_ASSERT(reporter, paint.fEffect.get()->fRefCnt == 3); |
| 204 | |
bungeman | beab9e7 | 2016-03-03 07:50:49 -0800 | [diff] [blame] | 205 | // Test sk_sp::operator-> |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 206 | delete paint.get()->method(); |
| 207 | check(reporter, 2, 1, 2, 1); |
halcanary | cb6cb38 | 2016-03-02 08:11:26 -0800 | [diff] [blame] | 208 | |
bungeman | beab9e7 | 2016-03-03 07:50:49 -0800 | [diff] [blame] | 209 | // Test sk_sp::operator* |
| 210 | delete (*paint.get()).method(); |
| 211 | check(reporter, 2, 1, 2, 1); |
| 212 | |
halcanary | cb6cb38 | 2016-03-02 08:11:26 -0800 | [diff] [blame] | 213 | paint.set(nullptr); |
| 214 | e = nullptr; |
| 215 | paint2.set(nullptr); |
| 216 | check(reporter, 2, 4, 2, 2); |
| 217 | |
| 218 | reset_counters(); |
| 219 | { |
| 220 | // Test convertible sk_sp assignment. |
| 221 | check(reporter, 0, 0, 0, 0); |
| 222 | sk_sp<Effect> foo(nullptr); |
| 223 | REPORTER_ASSERT(reporter, !foo); |
| 224 | foo = make_effect(); |
| 225 | REPORTER_ASSERT(reporter, foo); |
| 226 | check(reporter, 0, 0, 1, 0); |
| 227 | } |
| 228 | check(reporter, 0, 1, 1, 1); |
| 229 | |
| 230 | // Test passing convertible rvalue into funtion. |
| 231 | reset_counters(); |
| 232 | paint.set(EffectImpl::Create()); |
| 233 | check(reporter, 0, 0, 1, 0); |
| 234 | paint.set(nullptr); |
| 235 | check(reporter, 0, 1, 1, 1); |
| 236 | |
| 237 | reset_counters(); |
| 238 | auto baz = EffectImpl::Create(); |
| 239 | check(reporter, 0, 0, 1, 0); |
| 240 | paint.set(std::move(baz)); |
| 241 | check(reporter, 0, 0, 1, 0); |
| 242 | REPORTER_ASSERT(reporter, !baz); |
| 243 | paint.set(nullptr); |
| 244 | check(reporter, 0, 1, 1, 1); |
| 245 | |
| 246 | reset_counters(); |
| 247 | { |
| 248 | // test comparison operator with convertible type. |
| 249 | sk_sp<EffectImpl> bar1 = EffectImpl::Create(); |
| 250 | sk_sp<Effect> bar2(bar1); // convertible copy constructor |
| 251 | check(reporter, 1, 0, 1, 0); |
| 252 | REPORTER_ASSERT(reporter, bar1); |
| 253 | REPORTER_ASSERT(reporter, bar2); |
| 254 | REPORTER_ASSERT(reporter, bar1 == bar2); |
| 255 | REPORTER_ASSERT(reporter, bar2 == bar1); |
| 256 | REPORTER_ASSERT(reporter, !(bar1 != bar2)); |
| 257 | REPORTER_ASSERT(reporter, !(bar2 != bar1)); |
| 258 | sk_sp<Effect> bar3(nullptr); |
| 259 | bar3 = bar1; // convertible copy assignment |
| 260 | check(reporter, 2, 0, 1, 0); |
| 261 | |
| 262 | } |
| 263 | check(reporter, 2, 3, 1, 1); |
| 264 | |
| 265 | // test passing convertible copy into funtion. |
| 266 | reset_counters(); |
| 267 | baz = EffectImpl::Create(); |
| 268 | check(reporter, 0, 0, 1, 0); |
| 269 | paint.set(baz); |
| 270 | check(reporter, 1, 0, 1, 0); |
| 271 | baz = nullptr; |
| 272 | check(reporter, 1, 1, 1, 0); |
| 273 | paint.set(nullptr); |
| 274 | check(reporter, 1, 2, 1, 1); |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 275 | } |
| 276 | |
halcanary | ffa39e0 | 2016-03-05 08:30:28 -0800 | [diff] [blame] | 277 | namespace { |
| 278 | struct FooAbstract : public SkRefCnt { |
| 279 | virtual void f() = 0; |
| 280 | }; |
| 281 | struct FooConcrete : public FooAbstract { |
| 282 | void f() override {} |
| 283 | }; |
| 284 | } |
| 285 | static sk_sp<FooAbstract> make_foo() { |
| 286 | // can not cast FooConcrete to FooAbstract. |
| 287 | // can cast FooConcrete* to FooAbstract*. |
| 288 | return sk_make_sp<FooConcrete>(); |
| 289 | } |
| 290 | DEF_TEST(sk_make_sp, r) { |
| 291 | auto x = make_foo(); |
| 292 | } |
reed | 941da9d | 2016-03-06 13:54:00 -0800 | [diff] [blame^] | 293 | |
| 294 | // Test that reset() "adopts" ownership from the caller, even if we are given the same ptr twice |
| 295 | // |
| 296 | DEF_TEST(sk_sp_reset, r) { |
| 297 | SkRefCnt* rc = new SkRefCnt; |
| 298 | REPORTER_ASSERT(r, rc->unique()); |
| 299 | |
| 300 | sk_sp<SkRefCnt> sp; |
| 301 | sp.reset(rc); |
| 302 | // We have transfered our ownership over to sp |
| 303 | REPORTER_ASSERT(r, rc->unique()); |
| 304 | |
| 305 | rc->ref(); // now "rc" is also an owner |
| 306 | REPORTER_ASSERT(r, !rc->unique()); |
| 307 | |
| 308 | sp.reset(rc); // this should transfer our ownership over to sp |
| 309 | REPORTER_ASSERT(r, rc->unique()); |
| 310 | } |