OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkRefCnt.h" | 8 #include "SkRefCnt.h" |
9 #include "SkThreadUtils.h" | 9 #include "SkThreadUtils.h" |
10 #include "SkTypes.h" | 10 #include "SkTypes.h" |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 | 72 |
73 REPORTER_ASSERT(reporter, ref->unique()); | 73 REPORTER_ASSERT(reporter, ref->unique()); |
74 REPORTER_ASSERT(reporter, ref->getWeakCnt() == 1); | 74 REPORTER_ASSERT(reporter, ref->getWeakCnt() == 1); |
75 ref->unref(); | 75 ref->unref(); |
76 } | 76 } |
77 | 77 |
78 DEF_TEST(RefCnt, reporter) { | 78 DEF_TEST(RefCnt, reporter) { |
79 test_refCnt(reporter); | 79 test_refCnt(reporter); |
80 test_weakRefCnt(reporter); | 80 test_weakRefCnt(reporter); |
81 } | 81 } |
| 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() { |
| 123 return sk_sp<Effect>(new Effect); |
| 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 |
| 137 DEF_TEST(sk_sp, reporter) { |
| 138 gRefCounter = 0; |
| 139 gUnrefCounter = 0; |
| 140 gNewCounter = 0; |
| 141 gDeleteCounter = 0; |
| 142 |
| 143 Paint paint; |
| 144 REPORTER_ASSERT(reporter, paint.fEffect.get() == nullptr); |
| 145 REPORTER_ASSERT(reporter, !paint.get()); |
| 146 check(reporter, 0, 0, 0, 0); |
| 147 |
| 148 paint.set(Create()); |
| 149 check(reporter, 0, 0, 1, 0); |
| 150 REPORTER_ASSERT(reporter, paint.fEffect.get()->fRefCnt == 1); |
| 151 |
| 152 paint.set(nullptr); |
| 153 check(reporter, 0, 1, 1, 1); |
| 154 |
| 155 auto e = Create(); |
| 156 REPORTER_ASSERT(reporter, sizeof(e) == sizeof(void*)); |
| 157 |
| 158 check(reporter, 0, 1, 2, 1); |
| 159 paint.set(e); |
| 160 check(reporter, 1, 1, 2, 1); |
| 161 REPORTER_ASSERT(reporter, paint.fEffect.get()->fRefCnt == 2); |
| 162 |
| 163 Paint paint2; |
| 164 paint2.set(paint.get()); |
| 165 check(reporter, 2, 1, 2, 1); |
| 166 REPORTER_ASSERT(reporter, paint.fEffect.get()->fRefCnt == 3); |
| 167 |
| 168 delete paint.get()->method(); |
| 169 check(reporter, 2, 1, 2, 1); |
| 170 } |
| 171 |
OLD | NEW |