blob: b66bb738d29d881f8c992e8973bd3c2278b4400a [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 {
Tim Murray0b575de2013-03-15 15:56:43 -070072 __sync_fetch_and_add(&mUserRefCount, 1);
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 {
Tim Murray0b575de2013-03-15 15:56:43 -070077 __sync_fetch_and_add(&mSysRefCount, 1);
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
Jason Sams110f1812013-03-14 16:02:18 -0700114 //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
Tim Murray0b575de2013-03-15 15:56:43 -0700121 if ((__sync_fetch_and_sub(&mUserRefCount, 1) <= 1)) {
122 __sync_synchronize();
123 if (mSysRefCount <= 0) {
124 return checkDelete(this);
125 }
Jason Samse514b452009-09-25 14:51:22 -0700126 }
127 return false;
128}
129
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800130bool ObjectBase::zeroUserRef() const {
Steve Block65982012011-10-20 11:56:00 +0100131 //ALOGV("ObjectBase %p zeroU ref %i, %i", this, mUserRefCount, mSysRefCount);
Tim Murray0b575de2013-03-15 15:56:43 -0700132 __sync_and_and_fetch(&mUserRefCount, 0);
133 if ((__sync_fetch_and_sub(&mUserRefCount, 1) <= 1)) {
134 __sync_synchronize();
135 if (mSysRefCount <= 0) {
136 return checkDelete(this);
137 }
Jason Sams225afd32010-10-21 14:06:55 -0700138 }
139 return false;
Jason Samse514b452009-09-25 14:51:22 -0700140}
141
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800142bool ObjectBase::decSysRef() const {
Steve Block65982012011-10-20 11:56:00 +0100143 //ALOGV("ObjectBase %p decS ref %i, %i", this, mUserRefCount, mSysRefCount);
Jason Sams9397e302009-08-27 20:23:34 -0700144 rsAssert(mSysRefCount > 0);
Tim Murray0b575de2013-03-15 15:56:43 -0700145 if ((__sync_fetch_and_sub(&mSysRefCount, 1) <= 1)) {
146 __sync_synchronize();
147 if (mUserRefCount <= 0) {
148 return checkDelete(this);
149 }
Jason Sams225afd32010-10-21 14:06:55 -0700150 }
151 return false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700152}
153
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800154void ObjectBase::setName(const char *name) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700155 mName.setTo(name);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700156}
Jason Samsa4a54e42009-06-10 18:39:40 -0700157
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800158void ObjectBase::setName(const char *name, uint32_t len) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700159 mName.setTo(name, len);
Jason Samsa4a54e42009-06-10 18:39:40 -0700160}
161
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800162void ObjectBase::asyncLock() {
Jason Sams2353ae32010-10-14 17:48:46 -0700163 pthread_mutex_lock(&gObjectInitMutex);
164}
165
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800166void ObjectBase::asyncUnlock() {
Jason Sams2353ae32010-10-14 17:48:46 -0700167 pthread_mutex_unlock(&gObjectInitMutex);
168}
169
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800170void ObjectBase::add() const {
Jason Sams225afd32010-10-21 14:06:55 -0700171 asyncLock();
Jason Sams2353ae32010-10-14 17:48:46 -0700172
Jason Samse514b452009-09-25 14:51:22 -0700173 rsAssert(!mNext);
174 rsAssert(!mPrev);
Steve Block65982012011-10-20 11:56:00 +0100175 //ALOGV("calling add rsc %p", mRSC);
Jason Samse514b452009-09-25 14:51:22 -0700176 mNext = mRSC->mObjHead;
177 if (mRSC->mObjHead) {
178 mRSC->mObjHead->mPrev = this;
179 }
180 mRSC->mObjHead = this;
Jason Sams2353ae32010-10-14 17:48:46 -0700181
Jason Sams225afd32010-10-21 14:06:55 -0700182 asyncUnlock();
Jason Samse514b452009-09-25 14:51:22 -0700183}
184
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800185void ObjectBase::remove() const {
Steve Block65982012011-10-20 11:56:00 +0100186 //ALOGV("calling remove rsc %p", mRSC);
Jason Samse514b452009-09-25 14:51:22 -0700187 if (!mRSC) {
188 rsAssert(!mPrev);
189 rsAssert(!mNext);
190 return;
191 }
Jason Sams2353ae32010-10-14 17:48:46 -0700192
Jason Samse514b452009-09-25 14:51:22 -0700193 if (mRSC->mObjHead == this) {
194 mRSC->mObjHead = mNext;
195 }
196 if (mPrev) {
197 mPrev->mNext = mNext;
198 }
199 if (mNext) {
200 mNext->mPrev = mPrev;
201 }
202 mPrev = NULL;
203 mNext = NULL;
204}
205
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800206void ObjectBase::zeroAllUserRef(Context *rsc) {
Jason Sams1fddd902009-09-25 15:25:00 -0700207 if (rsc->props.mLogObjects) {
Steve Block65982012011-10-20 11:56:00 +0100208 ALOGV("Forcing release of all outstanding user refs.");
Jason Sams1fddd902009-09-25 15:25:00 -0700209 }
Jason Samse514b452009-09-25 14:51:22 -0700210
211 // This operation can be slow, only to be called during context cleanup.
212 const ObjectBase * o = rsc->mObjHead;
213 while (o) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000214 //ALOGE("o %p", o);
Jason Samse514b452009-09-25 14:51:22 -0700215 if (o->zeroUserRef()) {
216 // deleted the object and possibly others, restart from head.
217 o = rsc->mObjHead;
Steve Blockaf12ac62012-01-06 19:20:56 +0000218 //ALOGE("o head %p", o);
Jason Samse514b452009-09-25 14:51:22 -0700219 } else {
220 o = o->mNext;
Steve Blockaf12ac62012-01-06 19:20:56 +0000221 //ALOGE("o next %p", o);
Jason Samse514b452009-09-25 14:51:22 -0700222 }
223 }
Jason Samsf2649a92009-09-25 16:37:33 -0700224
225 if (rsc->props.mLogObjects) {
Steve Block65982012011-10-20 11:56:00 +0100226 ALOGV("Objects remaining.");
Jason Sams25afc002009-11-19 13:08:17 -0800227 dumpAll(rsc);
Jason Samsf2649a92009-09-25 16:37:33 -0700228 }
Jason Samse514b452009-09-25 14:51:22 -0700229}
230
Jason Samsc7cec1e2011-08-18 18:01:33 -0700231void ObjectBase::freeAllChildren(Context *rsc) {
232 if (rsc->props.mLogObjects) {
Steve Block65982012011-10-20 11:56:00 +0100233 ALOGV("Forcing release of all child objects.");
Jason Samsc7cec1e2011-08-18 18:01:33 -0700234 }
235
236 // This operation can be slow, only to be called during context cleanup.
237 ObjectBase * o = (ObjectBase *)rsc->mObjHead;
238 while (o) {
239 if (o->freeChildren()) {
240 // deleted ref to self and possibly others, restart from head.
241 o = (ObjectBase *)rsc->mObjHead;
242 } else {
243 o = (ObjectBase *)o->mNext;
244 }
245 }
246
247 if (rsc->props.mLogObjects) {
Steve Block65982012011-10-20 11:56:00 +0100248 ALOGV("Objects remaining.");
Jason Samsc7cec1e2011-08-18 18:01:33 -0700249 dumpAll(rsc);
250 }
251}
252
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800253void ObjectBase::dumpAll(Context *rsc) {
Jason Sams225afd32010-10-21 14:06:55 -0700254 asyncLock();
Jason Sams2353ae32010-10-14 17:48:46 -0700255
Steve Block65982012011-10-20 11:56:00 +0100256 ALOGV("Dumping all objects");
Jason Sams25afc002009-11-19 13:08:17 -0800257 const ObjectBase * o = rsc->mObjHead;
258 while (o) {
Steve Block65982012011-10-20 11:56:00 +0100259 ALOGV(" Object %p", o);
Jason Sams25afc002009-11-19 13:08:17 -0800260 o->dumpLOGV(" ");
261 o = o->mNext;
Jason Samsc21cf402009-11-17 17:26:46 -0800262 }
Jason Sams2353ae32010-10-14 17:48:46 -0700263
Jason Sams225afd32010-10-21 14:06:55 -0700264 asyncUnlock();
Jason Samsc21cf402009-11-17 17:26:46 -0800265}
266
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800267bool ObjectBase::isValid(const Context *rsc, const ObjectBase *obj) {
Jason Sams225afd32010-10-21 14:06:55 -0700268 asyncLock();
Jason Sams2353ae32010-10-14 17:48:46 -0700269
Jason Sams605048a2010-09-30 18:15:52 -0700270 const ObjectBase * o = rsc->mObjHead;
271 while (o) {
272 if (o == obj) {
Jason Sams225afd32010-10-21 14:06:55 -0700273 asyncUnlock();
Jason Sams605048a2010-09-30 18:15:52 -0700274 return true;
275 }
276 o = o->mNext;
277 }
Jason Sams225afd32010-10-21 14:06:55 -0700278 asyncUnlock();
Jason Sams605048a2010-09-30 18:15:52 -0700279 return false;
280}
281