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 | } |
mtklein | 71aca54 | 2016-03-07 12:28:17 -0800 | [diff] [blame^] | 102 | virtual ~Effect() {} |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 103 | |
| 104 | int fRefCnt; |
| 105 | |
| 106 | void ref() { |
| 107 | gRefCounter += 1; |
| 108 | fRefCnt += 1; |
| 109 | } |
| 110 | void unref() { |
| 111 | gUnrefCounter += 1; |
| 112 | |
| 113 | SkASSERT(fRefCnt > 0); |
| 114 | if (0 == --fRefCnt) { |
| 115 | gDeleteCounter += 1; |
| 116 | delete this; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | int* method() const { return new int; } |
| 121 | }; |
| 122 | |
| 123 | static sk_sp<Effect> Create() { |
halcanary | 217c0b3 | 2016-03-02 08:06:20 -0800 | [diff] [blame] | 124 | return sk_make_sp<Effect>(); |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | class Paint { |
| 128 | public: |
| 129 | sk_sp<Effect> fEffect; |
| 130 | |
| 131 | const sk_sp<Effect>& get() const { return fEffect; } |
| 132 | |
| 133 | void set(sk_sp<Effect> value) { |
| 134 | fEffect = std::move(value); |
| 135 | } |
| 136 | }; |
| 137 | |
halcanary | cb6cb38 | 2016-03-02 08:11:26 -0800 | [diff] [blame] | 138 | struct EffectImpl : public Effect { |
mtklein | 71aca54 | 2016-03-07 12:28:17 -0800 | [diff] [blame^] | 139 | ~EffectImpl() override {} |
| 140 | |
halcanary | cb6cb38 | 2016-03-02 08:11:26 -0800 | [diff] [blame] | 141 | static sk_sp<EffectImpl> Create() { |
| 142 | return sk_sp<EffectImpl>(new EffectImpl); |
| 143 | } |
| 144 | int fValue; |
| 145 | }; |
| 146 | static sk_sp<Effect> make_effect() { |
| 147 | auto foo = EffectImpl::Create(); |
| 148 | foo->fValue = 42; |
| 149 | return std::move(foo); |
| 150 | } |
| 151 | |
| 152 | static void reset_counters() { |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 153 | gRefCounter = 0; |
| 154 | gUnrefCounter = 0; |
| 155 | gNewCounter = 0; |
| 156 | gDeleteCounter = 0; |
halcanary | cb6cb38 | 2016-03-02 08:11:26 -0800 | [diff] [blame] | 157 | } |
| 158 | DEF_TEST(sk_sp, reporter) { |
| 159 | reset_counters(); |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 160 | |
| 161 | Paint paint; |
| 162 | REPORTER_ASSERT(reporter, paint.fEffect.get() == nullptr); |
| 163 | REPORTER_ASSERT(reporter, !paint.get()); |
| 164 | check(reporter, 0, 0, 0, 0); |
| 165 | |
| 166 | paint.set(Create()); |
| 167 | check(reporter, 0, 0, 1, 0); |
| 168 | REPORTER_ASSERT(reporter, paint.fEffect.get()->fRefCnt == 1); |
| 169 | |
bungeman | beab9e7 | 2016-03-03 07:50:49 -0800 | [diff] [blame] | 170 | if (paint.get()) { |
| 171 | REPORTER_ASSERT(reporter, true); |
| 172 | } else { |
| 173 | REPORTER_ASSERT(reporter, false); |
| 174 | } |
| 175 | if (!paint.get()) { |
| 176 | REPORTER_ASSERT(reporter, false); |
| 177 | } else { |
| 178 | REPORTER_ASSERT(reporter, true); |
| 179 | } |
| 180 | |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 181 | paint.set(nullptr); |
| 182 | check(reporter, 0, 1, 1, 1); |
| 183 | |
bungeman | beab9e7 | 2016-03-03 07:50:49 -0800 | [diff] [blame] | 184 | if (paint.get()) { |
| 185 | REPORTER_ASSERT(reporter, false); |
| 186 | } else { |
| 187 | REPORTER_ASSERT(reporter, true); |
| 188 | } |
| 189 | if (!paint.get()) { |
| 190 | REPORTER_ASSERT(reporter, true); |
| 191 | } else { |
| 192 | REPORTER_ASSERT(reporter, false); |
| 193 | } |
| 194 | |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 195 | auto e = Create(); |
| 196 | REPORTER_ASSERT(reporter, sizeof(e) == sizeof(void*)); |
| 197 | |
| 198 | check(reporter, 0, 1, 2, 1); |
| 199 | paint.set(e); |
| 200 | check(reporter, 1, 1, 2, 1); |
| 201 | REPORTER_ASSERT(reporter, paint.fEffect.get()->fRefCnt == 2); |
| 202 | |
| 203 | Paint paint2; |
| 204 | paint2.set(paint.get()); |
| 205 | check(reporter, 2, 1, 2, 1); |
| 206 | REPORTER_ASSERT(reporter, paint.fEffect.get()->fRefCnt == 3); |
| 207 | |
bungeman | beab9e7 | 2016-03-03 07:50:49 -0800 | [diff] [blame] | 208 | // Test sk_sp::operator-> |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 209 | delete paint.get()->method(); |
| 210 | check(reporter, 2, 1, 2, 1); |
halcanary | cb6cb38 | 2016-03-02 08:11:26 -0800 | [diff] [blame] | 211 | |
bungeman | beab9e7 | 2016-03-03 07:50:49 -0800 | [diff] [blame] | 212 | // Test sk_sp::operator* |
| 213 | delete (*paint.get()).method(); |
| 214 | check(reporter, 2, 1, 2, 1); |
| 215 | |
halcanary | cb6cb38 | 2016-03-02 08:11:26 -0800 | [diff] [blame] | 216 | paint.set(nullptr); |
| 217 | e = nullptr; |
| 218 | paint2.set(nullptr); |
| 219 | check(reporter, 2, 4, 2, 2); |
| 220 | |
| 221 | reset_counters(); |
| 222 | { |
| 223 | // Test convertible sk_sp assignment. |
| 224 | check(reporter, 0, 0, 0, 0); |
| 225 | sk_sp<Effect> foo(nullptr); |
| 226 | REPORTER_ASSERT(reporter, !foo); |
| 227 | foo = make_effect(); |
| 228 | REPORTER_ASSERT(reporter, foo); |
| 229 | check(reporter, 0, 0, 1, 0); |
| 230 | } |
| 231 | check(reporter, 0, 1, 1, 1); |
| 232 | |
| 233 | // Test passing convertible rvalue into funtion. |
| 234 | reset_counters(); |
| 235 | paint.set(EffectImpl::Create()); |
| 236 | check(reporter, 0, 0, 1, 0); |
| 237 | paint.set(nullptr); |
| 238 | check(reporter, 0, 1, 1, 1); |
| 239 | |
| 240 | reset_counters(); |
| 241 | auto baz = EffectImpl::Create(); |
| 242 | check(reporter, 0, 0, 1, 0); |
| 243 | paint.set(std::move(baz)); |
| 244 | check(reporter, 0, 0, 1, 0); |
| 245 | REPORTER_ASSERT(reporter, !baz); |
| 246 | paint.set(nullptr); |
| 247 | check(reporter, 0, 1, 1, 1); |
| 248 | |
| 249 | reset_counters(); |
| 250 | { |
| 251 | // test comparison operator with convertible type. |
| 252 | sk_sp<EffectImpl> bar1 = EffectImpl::Create(); |
| 253 | sk_sp<Effect> bar2(bar1); // convertible copy constructor |
| 254 | check(reporter, 1, 0, 1, 0); |
| 255 | REPORTER_ASSERT(reporter, bar1); |
| 256 | REPORTER_ASSERT(reporter, bar2); |
| 257 | REPORTER_ASSERT(reporter, bar1 == bar2); |
| 258 | REPORTER_ASSERT(reporter, bar2 == bar1); |
| 259 | REPORTER_ASSERT(reporter, !(bar1 != bar2)); |
| 260 | REPORTER_ASSERT(reporter, !(bar2 != bar1)); |
| 261 | sk_sp<Effect> bar3(nullptr); |
| 262 | bar3 = bar1; // convertible copy assignment |
| 263 | check(reporter, 2, 0, 1, 0); |
| 264 | |
| 265 | } |
| 266 | check(reporter, 2, 3, 1, 1); |
| 267 | |
| 268 | // test passing convertible copy into funtion. |
| 269 | reset_counters(); |
| 270 | baz = EffectImpl::Create(); |
| 271 | check(reporter, 0, 0, 1, 0); |
| 272 | paint.set(baz); |
| 273 | check(reporter, 1, 0, 1, 0); |
| 274 | baz = nullptr; |
| 275 | check(reporter, 1, 1, 1, 0); |
| 276 | paint.set(nullptr); |
| 277 | check(reporter, 1, 2, 1, 1); |
reed | bb7b043 | 2016-03-01 07:28:51 -0800 | [diff] [blame] | 278 | } |
| 279 | |
halcanary | ffa39e0 | 2016-03-05 08:30:28 -0800 | [diff] [blame] | 280 | namespace { |
| 281 | struct FooAbstract : public SkRefCnt { |
| 282 | virtual void f() = 0; |
| 283 | }; |
| 284 | struct FooConcrete : public FooAbstract { |
| 285 | void f() override {} |
| 286 | }; |
| 287 | } |
| 288 | static sk_sp<FooAbstract> make_foo() { |
| 289 | // can not cast FooConcrete to FooAbstract. |
| 290 | // can cast FooConcrete* to FooAbstract*. |
| 291 | return sk_make_sp<FooConcrete>(); |
| 292 | } |
| 293 | DEF_TEST(sk_make_sp, r) { |
| 294 | auto x = make_foo(); |
| 295 | } |
reed | 941da9d | 2016-03-06 13:54:00 -0800 | [diff] [blame] | 296 | |
| 297 | // Test that reset() "adopts" ownership from the caller, even if we are given the same ptr twice |
| 298 | // |
| 299 | DEF_TEST(sk_sp_reset, r) { |
| 300 | SkRefCnt* rc = new SkRefCnt; |
| 301 | REPORTER_ASSERT(r, rc->unique()); |
| 302 | |
| 303 | sk_sp<SkRefCnt> sp; |
| 304 | sp.reset(rc); |
| 305 | // We have transfered our ownership over to sp |
| 306 | REPORTER_ASSERT(r, rc->unique()); |
| 307 | |
| 308 | rc->ref(); // now "rc" is also an owner |
| 309 | REPORTER_ASSERT(r, !rc->unique()); |
| 310 | |
| 311 | sp.reset(rc); // this should transfer our ownership over to sp |
| 312 | REPORTER_ASSERT(r, rc->unique()); |
| 313 | } |