Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "rsObjectBase.h" |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 18 | #include "rsContext.h" |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 19 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 20 | using namespace android; |
| 21 | using namespace android::renderscript; |
| 22 | |
Jason Sams | 2353ae3 | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 23 | pthread_mutex_t ObjectBase::gObjectInitMutex = PTHREAD_MUTEX_INITIALIZER; |
| 24 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 25 | ObjectBase::ObjectBase(Context *rsc) { |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 26 | mUserRefCount = 0; |
| 27 | mSysRefCount = 0; |
Jason Sams | 2353ae3 | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 28 | mRSC = rsc; |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 29 | mNext = NULL; |
| 30 | mPrev = NULL; |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 31 | |
| 32 | #if RS_OBJECT_DEBUG |
| 33 | mStack.update(2); |
| 34 | #endif |
Jason Sams | 2353ae3 | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 35 | |
| 36 | rsAssert(rsc); |
| 37 | add(); |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 38 | //ALOGV("ObjectBase %p con", this); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 41 | ObjectBase::~ObjectBase() { |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 42 | //ALOGV("~ObjectBase %p ref %i,%i", this, mUserRefCount, mSysRefCount); |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 43 | #if RS_OBJECT_DEBUG |
| 44 | mStack.dump(); |
| 45 | #endif |
| 46 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 47 | if (mPrev || mNext) { |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 48 | // While the normal practice is to call remove before we call |
| 49 | // delete. Its possible for objects without a re-use list |
| 50 | // for avoiding duplication to be created on the stack. In those |
| 51 | // cases we need to remove ourself here. |
| 52 | asyncLock(); |
| 53 | remove(); |
| 54 | asyncUnlock(); |
| 55 | } |
| 56 | |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 57 | rsAssert(!mUserRefCount); |
| 58 | rsAssert(!mSysRefCount); |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 61 | void ObjectBase::dumpLOGV(const char *op) const { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 62 | if (mName.size()) { |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 63 | ALOGV("%s RSobj %p, name %s, refs %i,%i links %p,%p,%p", |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 64 | op, this, mName.string(), mUserRefCount, mSysRefCount, mNext, mPrev, mRSC); |
Jason Sams | f2649a9 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 65 | } else { |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 66 | ALOGV("%s RSobj %p, no-name, refs %i,%i links %p,%p,%p", |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 67 | op, this, mUserRefCount, mSysRefCount, mNext, mPrev, mRSC); |
Jason Sams | f2649a9 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 71 | void ObjectBase::incUserRef() const { |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 72 | android_atomic_inc(&mUserRefCount); |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 73 | //ALOGV("ObjectBase %p incU ref %i, %i", this, mUserRefCount, mSysRefCount); |
Jason Sams | 2353ae3 | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 76 | void ObjectBase::incSysRef() const { |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 77 | android_atomic_inc(&mSysRefCount); |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 78 | //ALOGV("ObjectBase %p incS ref %i, %i", this, mUserRefCount, mSysRefCount); |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 81 | void ObjectBase::preDestroy() const { |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 82 | } |
Jason Sams | 2353ae3 | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 83 | |
Jason Sams | c7cec1e | 2011-08-18 18:01:33 -0700 | [diff] [blame] | 84 | bool ObjectBase::freeChildren() { |
| 85 | return false; |
| 86 | } |
| 87 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 88 | bool ObjectBase::checkDelete(const ObjectBase *ref) { |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 89 | if (!ref) { |
| 90 | return false; |
| 91 | } |
Jason Sams | 2353ae3 | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 92 | |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 93 | asyncLock(); |
| 94 | // This lock protects us against the non-RS threads changing |
| 95 | // the ref counts. At this point we should be the only thread |
| 96 | // working on them. |
| 97 | if (ref->mUserRefCount || ref->mSysRefCount) { |
| 98 | asyncUnlock(); |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | ref->remove(); |
| 103 | // At this point we can unlock because there should be no possible way |
| 104 | // for another thread to reference this object. |
| 105 | ref->preDestroy(); |
| 106 | asyncUnlock(); |
| 107 | delete ref; |
| 108 | return true; |
| 109 | } |
| 110 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 111 | bool ObjectBase::decUserRef() const { |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 112 | rsAssert(mUserRefCount > 0); |
Jason Sams | f0c1df4 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 113 | #if RS_OBJECT_DEBUG |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 114 | ALOGV("ObjectBase %p decU ref %i, %i", this, mUserRefCount, mSysRefCount); |
Jason Sams | f0c1df4 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 115 | if (mUserRefCount <= 0) { |
| 116 | mStack.dump(); |
| 117 | } |
| 118 | #endif |
| 119 | |
| 120 | |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 121 | if ((android_atomic_dec(&mUserRefCount) <= 1) && |
| 122 | (android_atomic_acquire_load(&mSysRefCount) <= 0)) { |
| 123 | return checkDelete(this); |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 124 | } |
| 125 | return false; |
| 126 | } |
| 127 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 128 | bool ObjectBase::zeroUserRef() const { |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 129 | //ALOGV("ObjectBase %p zeroU ref %i, %i", this, mUserRefCount, mSysRefCount); |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 130 | android_atomic_acquire_store(0, &mUserRefCount); |
| 131 | if (android_atomic_acquire_load(&mSysRefCount) <= 0) { |
| 132 | return checkDelete(this); |
| 133 | } |
| 134 | return false; |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 137 | bool ObjectBase::decSysRef() const { |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 138 | //ALOGV("ObjectBase %p decS ref %i, %i", this, mUserRefCount, mSysRefCount); |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 139 | rsAssert(mSysRefCount > 0); |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 140 | if ((android_atomic_dec(&mSysRefCount) <= 1) && |
| 141 | (android_atomic_acquire_load(&mUserRefCount) <= 0)) { |
| 142 | return checkDelete(this); |
| 143 | } |
| 144 | return false; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 147 | void ObjectBase::setName(const char *name) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 148 | mName.setTo(name); |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 149 | } |
Jason Sams | a4a54e4 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 150 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 151 | void ObjectBase::setName(const char *name, uint32_t len) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 152 | mName.setTo(name, len); |
Jason Sams | a4a54e4 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 155 | void ObjectBase::asyncLock() { |
Jason Sams | 2353ae3 | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 156 | pthread_mutex_lock(&gObjectInitMutex); |
| 157 | } |
| 158 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 159 | void ObjectBase::asyncUnlock() { |
Jason Sams | 2353ae3 | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 160 | pthread_mutex_unlock(&gObjectInitMutex); |
| 161 | } |
| 162 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 163 | void ObjectBase::add() const { |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 164 | asyncLock(); |
Jason Sams | 2353ae3 | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 165 | |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 166 | rsAssert(!mNext); |
| 167 | rsAssert(!mPrev); |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 168 | //ALOGV("calling add rsc %p", mRSC); |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 169 | mNext = mRSC->mObjHead; |
| 170 | if (mRSC->mObjHead) { |
| 171 | mRSC->mObjHead->mPrev = this; |
| 172 | } |
| 173 | mRSC->mObjHead = this; |
Jason Sams | 2353ae3 | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 174 | |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 175 | asyncUnlock(); |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 178 | void ObjectBase::remove() const { |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 179 | //ALOGV("calling remove rsc %p", mRSC); |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 180 | if (!mRSC) { |
| 181 | rsAssert(!mPrev); |
| 182 | rsAssert(!mNext); |
| 183 | return; |
| 184 | } |
Jason Sams | 2353ae3 | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 185 | |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 186 | if (mRSC->mObjHead == this) { |
| 187 | mRSC->mObjHead = mNext; |
| 188 | } |
| 189 | if (mPrev) { |
| 190 | mPrev->mNext = mNext; |
| 191 | } |
| 192 | if (mNext) { |
| 193 | mNext->mPrev = mPrev; |
| 194 | } |
| 195 | mPrev = NULL; |
| 196 | mNext = NULL; |
| 197 | } |
| 198 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 199 | void ObjectBase::zeroAllUserRef(Context *rsc) { |
Jason Sams | 1fddd90 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 200 | if (rsc->props.mLogObjects) { |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 201 | ALOGV("Forcing release of all outstanding user refs."); |
Jason Sams | 1fddd90 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 202 | } |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 203 | |
| 204 | // This operation can be slow, only to be called during context cleanup. |
| 205 | const ObjectBase * o = rsc->mObjHead; |
| 206 | while (o) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 207 | //ALOGE("o %p", o); |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 208 | if (o->zeroUserRef()) { |
| 209 | // deleted the object and possibly others, restart from head. |
| 210 | o = rsc->mObjHead; |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 211 | //ALOGE("o head %p", o); |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 212 | } else { |
| 213 | o = o->mNext; |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 214 | //ALOGE("o next %p", o); |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
Jason Sams | f2649a9 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 217 | |
| 218 | if (rsc->props.mLogObjects) { |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 219 | ALOGV("Objects remaining."); |
Jason Sams | 25afc00 | 2009-11-19 13:08:17 -0800 | [diff] [blame] | 220 | dumpAll(rsc); |
Jason Sams | f2649a9 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 221 | } |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Jason Sams | c7cec1e | 2011-08-18 18:01:33 -0700 | [diff] [blame] | 224 | void ObjectBase::freeAllChildren(Context *rsc) { |
| 225 | if (rsc->props.mLogObjects) { |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 226 | ALOGV("Forcing release of all child objects."); |
Jason Sams | c7cec1e | 2011-08-18 18:01:33 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | // This operation can be slow, only to be called during context cleanup. |
| 230 | ObjectBase * o = (ObjectBase *)rsc->mObjHead; |
| 231 | while (o) { |
| 232 | if (o->freeChildren()) { |
| 233 | // deleted ref to self and possibly others, restart from head. |
| 234 | o = (ObjectBase *)rsc->mObjHead; |
| 235 | } else { |
| 236 | o = (ObjectBase *)o->mNext; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | if (rsc->props.mLogObjects) { |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 241 | ALOGV("Objects remaining."); |
Jason Sams | c7cec1e | 2011-08-18 18:01:33 -0700 | [diff] [blame] | 242 | dumpAll(rsc); |
| 243 | } |
| 244 | } |
| 245 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 246 | void ObjectBase::dumpAll(Context *rsc) { |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 247 | asyncLock(); |
Jason Sams | 2353ae3 | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 248 | |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 249 | ALOGV("Dumping all objects"); |
Jason Sams | 25afc00 | 2009-11-19 13:08:17 -0800 | [diff] [blame] | 250 | const ObjectBase * o = rsc->mObjHead; |
| 251 | while (o) { |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 252 | ALOGV(" Object %p", o); |
Jason Sams | 25afc00 | 2009-11-19 13:08:17 -0800 | [diff] [blame] | 253 | o->dumpLOGV(" "); |
| 254 | o = o->mNext; |
Jason Sams | c21cf40 | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 255 | } |
Jason Sams | 2353ae3 | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 256 | |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 257 | asyncUnlock(); |
Jason Sams | c21cf40 | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 260 | bool ObjectBase::isValid(const Context *rsc, const ObjectBase *obj) { |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 261 | asyncLock(); |
Jason Sams | 2353ae3 | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 262 | |
Jason Sams | 605048a | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 263 | const ObjectBase * o = rsc->mObjHead; |
| 264 | while (o) { |
| 265 | if (o == obj) { |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 266 | asyncUnlock(); |
Jason Sams | 605048a | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 267 | return true; |
| 268 | } |
| 269 | o = o->mNext; |
| 270 | } |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 271 | asyncUnlock(); |
Jason Sams | 605048a | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 272 | return false; |
| 273 | } |
| 274 | |