blob: addf932a8e678555ff943902ad4e11ee320c1b84 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
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 Samse514b452009-09-25 14:51:22 -070018#include "rsContext.h"
Jason Sams225afd32010-10-21 14:06:55 -070019
Jason Sams326e0dd2009-05-22 14:03:28 -070020using namespace android;
21using namespace android::renderscript;
22
Jason Sams2353ae32010-10-14 17:48:46 -070023pthread_mutex_t ObjectBase::gObjectInitMutex = PTHREAD_MUTEX_INITIALIZER;
24
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080025ObjectBase::ObjectBase(Context *rsc) {
Jason Sams9397e302009-08-27 20:23:34 -070026 mUserRefCount = 0;
27 mSysRefCount = 0;
Jason Sams2353ae32010-10-14 17:48:46 -070028 mRSC = rsc;
Jason Samse514b452009-09-25 14:51:22 -070029 mNext = NULL;
30 mPrev = NULL;
Jason Sams225afd32010-10-21 14:06:55 -070031
32#if RS_OBJECT_DEBUG
33 mStack.update(2);
34#endif
Jason Sams2353ae32010-10-14 17:48:46 -070035
36 rsAssert(rsc);
37 add();
Steve Block65982012011-10-20 11:56:00 +010038 //ALOGV("ObjectBase %p con", this);
Jason Sams326e0dd2009-05-22 14:03:28 -070039}
40
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080041ObjectBase::~ObjectBase() {
Steve Block65982012011-10-20 11:56:00 +010042 //ALOGV("~ObjectBase %p ref %i,%i", this, mUserRefCount, mSysRefCount);
Jason Sams225afd32010-10-21 14:06:55 -070043#if RS_OBJECT_DEBUG
44 mStack.dump();
45#endif
46
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080047 if (mPrev || mNext) {
Jason Sams225afd32010-10-21 14:06:55 -070048 // 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 Sams9397e302009-08-27 20:23:34 -070057 rsAssert(!mUserRefCount);
58 rsAssert(!mSysRefCount);
Jason Samse514b452009-09-25 14:51:22 -070059}
60
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080061void ObjectBase::dumpLOGV(const char *op) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070062 if (mName.size()) {
Steve Block65982012011-10-20 11:56:00 +010063 ALOGV("%s RSobj %p, name %s, refs %i,%i links %p,%p,%p",
Jason Sams225afd32010-10-21 14:06:55 -070064 op, this, mName.string(), mUserRefCount, mSysRefCount, mNext, mPrev, mRSC);
Jason Samsf2649a92009-09-25 16:37:33 -070065 } else {
Steve Block65982012011-10-20 11:56:00 +010066 ALOGV("%s RSobj %p, no-name, refs %i,%i links %p,%p,%p",
Jason Sams225afd32010-10-21 14:06:55 -070067 op, this, mUserRefCount, mSysRefCount, mNext, mPrev, mRSC);
Jason Samsf2649a92009-09-25 16:37:33 -070068 }
69}
70
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080071void ObjectBase::incUserRef() const {
Jason Sams225afd32010-10-21 14:06:55 -070072 android_atomic_inc(&mUserRefCount);
Steve Block65982012011-10-20 11:56:00 +010073 //ALOGV("ObjectBase %p incU ref %i, %i", this, mUserRefCount, mSysRefCount);
Jason Sams2353ae32010-10-14 17:48:46 -070074}
75
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080076void ObjectBase::incSysRef() const {
Jason Sams225afd32010-10-21 14:06:55 -070077 android_atomic_inc(&mSysRefCount);
Steve Block65982012011-10-20 11:56:00 +010078 //ALOGV("ObjectBase %p incS ref %i, %i", this, mUserRefCount, mSysRefCount);
Jason Sams9397e302009-08-27 20:23:34 -070079}
80
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080081void ObjectBase::preDestroy() const {
Jason Sams225afd32010-10-21 14:06:55 -070082}
Jason Sams2353ae32010-10-14 17:48:46 -070083
Jason Samsc7cec1e2011-08-18 18:01:33 -070084bool ObjectBase::freeChildren() {
85 return false;
86}
87
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080088bool ObjectBase::checkDelete(const ObjectBase *ref) {
Jason Sams225afd32010-10-21 14:06:55 -070089 if (!ref) {
90 return false;
91 }
Jason Sams2353ae32010-10-14 17:48:46 -070092
Jason Sams225afd32010-10-21 14:06:55 -070093 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 Sakhartchoukafb743a2010-11-09 17:00:54 -0800111bool ObjectBase::decUserRef() const {
Jason Sams225afd32010-10-21 14:06:55 -0700112 rsAssert(mUserRefCount > 0);
Jason Samsf0c1df42010-10-26 13:09:17 -0700113#if RS_OBJECT_DEBUG
Steve Block65982012011-10-20 11:56:00 +0100114 ALOGV("ObjectBase %p decU ref %i, %i", this, mUserRefCount, mSysRefCount);
Jason Samsf0c1df42010-10-26 13:09:17 -0700115 if (mUserRefCount <= 0) {
116 mStack.dump();
117 }
118#endif
119
120
Jason Sams225afd32010-10-21 14:06:55 -0700121 if ((android_atomic_dec(&mUserRefCount) <= 1) &&
122 (android_atomic_acquire_load(&mSysRefCount) <= 0)) {
123 return checkDelete(this);
Jason Samse514b452009-09-25 14:51:22 -0700124 }
125 return false;
126}
127
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800128bool ObjectBase::zeroUserRef() const {
Steve Block65982012011-10-20 11:56:00 +0100129 //ALOGV("ObjectBase %p zeroU ref %i, %i", this, mUserRefCount, mSysRefCount);
Jason Sams225afd32010-10-21 14:06:55 -0700130 android_atomic_acquire_store(0, &mUserRefCount);
131 if (android_atomic_acquire_load(&mSysRefCount) <= 0) {
132 return checkDelete(this);
133 }
134 return false;
Jason Samse514b452009-09-25 14:51:22 -0700135}
136
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800137bool ObjectBase::decSysRef() const {
Steve Block65982012011-10-20 11:56:00 +0100138 //ALOGV("ObjectBase %p decS ref %i, %i", this, mUserRefCount, mSysRefCount);
Jason Sams9397e302009-08-27 20:23:34 -0700139 rsAssert(mSysRefCount > 0);
Jason Sams225afd32010-10-21 14:06:55 -0700140 if ((android_atomic_dec(&mSysRefCount) <= 1) &&
141 (android_atomic_acquire_load(&mUserRefCount) <= 0)) {
142 return checkDelete(this);
143 }
144 return false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700145}
146
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800147void ObjectBase::setName(const char *name) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700148 mName.setTo(name);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700149}
Jason Samsa4a54e42009-06-10 18:39:40 -0700150
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800151void ObjectBase::setName(const char *name, uint32_t len) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700152 mName.setTo(name, len);
Jason Samsa4a54e42009-06-10 18:39:40 -0700153}
154
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800155void ObjectBase::asyncLock() {
Jason Sams2353ae32010-10-14 17:48:46 -0700156 pthread_mutex_lock(&gObjectInitMutex);
157}
158
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800159void ObjectBase::asyncUnlock() {
Jason Sams2353ae32010-10-14 17:48:46 -0700160 pthread_mutex_unlock(&gObjectInitMutex);
161}
162
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800163void ObjectBase::add() const {
Jason Sams225afd32010-10-21 14:06:55 -0700164 asyncLock();
Jason Sams2353ae32010-10-14 17:48:46 -0700165
Jason Samse514b452009-09-25 14:51:22 -0700166 rsAssert(!mNext);
167 rsAssert(!mPrev);
Steve Block65982012011-10-20 11:56:00 +0100168 //ALOGV("calling add rsc %p", mRSC);
Jason Samse514b452009-09-25 14:51:22 -0700169 mNext = mRSC->mObjHead;
170 if (mRSC->mObjHead) {
171 mRSC->mObjHead->mPrev = this;
172 }
173 mRSC->mObjHead = this;
Jason Sams2353ae32010-10-14 17:48:46 -0700174
Jason Sams225afd32010-10-21 14:06:55 -0700175 asyncUnlock();
Jason Samse514b452009-09-25 14:51:22 -0700176}
177
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800178void ObjectBase::remove() const {
Steve Block65982012011-10-20 11:56:00 +0100179 //ALOGV("calling remove rsc %p", mRSC);
Jason Samse514b452009-09-25 14:51:22 -0700180 if (!mRSC) {
181 rsAssert(!mPrev);
182 rsAssert(!mNext);
183 return;
184 }
Jason Sams2353ae32010-10-14 17:48:46 -0700185
Jason Samse514b452009-09-25 14:51:22 -0700186 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 Sakhartchoukafb743a2010-11-09 17:00:54 -0800199void ObjectBase::zeroAllUserRef(Context *rsc) {
Jason Sams1fddd902009-09-25 15:25:00 -0700200 if (rsc->props.mLogObjects) {
Steve Block65982012011-10-20 11:56:00 +0100201 ALOGV("Forcing release of all outstanding user refs.");
Jason Sams1fddd902009-09-25 15:25:00 -0700202 }
Jason Samse514b452009-09-25 14:51:22 -0700203
204 // This operation can be slow, only to be called during context cleanup.
205 const ObjectBase * o = rsc->mObjHead;
206 while (o) {
207 //LOGE("o %p", o);
208 if (o->zeroUserRef()) {
209 // deleted the object and possibly others, restart from head.
210 o = rsc->mObjHead;
211 //LOGE("o head %p", o);
212 } else {
213 o = o->mNext;
214 //LOGE("o next %p", o);
215 }
216 }
Jason Samsf2649a92009-09-25 16:37:33 -0700217
218 if (rsc->props.mLogObjects) {
Steve Block65982012011-10-20 11:56:00 +0100219 ALOGV("Objects remaining.");
Jason Sams25afc002009-11-19 13:08:17 -0800220 dumpAll(rsc);
Jason Samsf2649a92009-09-25 16:37:33 -0700221 }
Jason Samse514b452009-09-25 14:51:22 -0700222}
223
Jason Samsc7cec1e2011-08-18 18:01:33 -0700224void ObjectBase::freeAllChildren(Context *rsc) {
225 if (rsc->props.mLogObjects) {
Steve Block65982012011-10-20 11:56:00 +0100226 ALOGV("Forcing release of all child objects.");
Jason Samsc7cec1e2011-08-18 18:01:33 -0700227 }
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 Block65982012011-10-20 11:56:00 +0100241 ALOGV("Objects remaining.");
Jason Samsc7cec1e2011-08-18 18:01:33 -0700242 dumpAll(rsc);
243 }
244}
245
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800246void ObjectBase::dumpAll(Context *rsc) {
Jason Sams225afd32010-10-21 14:06:55 -0700247 asyncLock();
Jason Sams2353ae32010-10-14 17:48:46 -0700248
Steve Block65982012011-10-20 11:56:00 +0100249 ALOGV("Dumping all objects");
Jason Sams25afc002009-11-19 13:08:17 -0800250 const ObjectBase * o = rsc->mObjHead;
251 while (o) {
Steve Block65982012011-10-20 11:56:00 +0100252 ALOGV(" Object %p", o);
Jason Sams25afc002009-11-19 13:08:17 -0800253 o->dumpLOGV(" ");
254 o = o->mNext;
Jason Samsc21cf402009-11-17 17:26:46 -0800255 }
Jason Sams2353ae32010-10-14 17:48:46 -0700256
Jason Sams225afd32010-10-21 14:06:55 -0700257 asyncUnlock();
Jason Samsc21cf402009-11-17 17:26:46 -0800258}
259
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800260bool ObjectBase::isValid(const Context *rsc, const ObjectBase *obj) {
Jason Sams225afd32010-10-21 14:06:55 -0700261 asyncLock();
Jason Sams2353ae32010-10-14 17:48:46 -0700262
Jason Sams605048a2010-09-30 18:15:52 -0700263 const ObjectBase * o = rsc->mObjHead;
264 while (o) {
265 if (o == obj) {
Jason Sams225afd32010-10-21 14:06:55 -0700266 asyncUnlock();
Jason Sams605048a2010-09-30 18:15:52 -0700267 return true;
268 }
269 o = o->mNext;
270 }
Jason Sams225afd32010-10-21 14:06:55 -0700271 asyncUnlock();
Jason Sams605048a2010-09-30 18:15:52 -0700272 return false;
273}
274