blob: bf5d3b5b4cf561d309bd0edf12873a56ffa06149 [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;
Stephen Hinesd216daf2013-04-02 16:32:34 -070031 mDH = NULL;
Stephen Hinesb0934b62013-07-03 17:27:38 -070032 mName = NULL;
Jason Sams225afd32010-10-21 14:06:55 -070033
34#if RS_OBJECT_DEBUG
Stephen Hinesd216daf2013-04-02 16:32:34 -070035 mDH = new DebugHelper();
Jason Sams225afd32010-10-21 14:06:55 -070036#endif
Jason Sams2353ae32010-10-14 17:48:46 -070037
38 rsAssert(rsc);
39 add();
Steve Block65982012011-10-20 11:56:00 +010040 //ALOGV("ObjectBase %p con", this);
Jason Sams326e0dd2009-05-22 14:03:28 -070041}
42
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080043ObjectBase::~ObjectBase() {
Steve Block65982012011-10-20 11:56:00 +010044 //ALOGV("~ObjectBase %p ref %i,%i", this, mUserRefCount, mSysRefCount);
Jason Sams225afd32010-10-21 14:06:55 -070045#if RS_OBJECT_DEBUG
Stephen Hinesd216daf2013-04-02 16:32:34 -070046 mDH->dump();
47 delete mDH;
48 mDH = NULL;
Jason Sams225afd32010-10-21 14:06:55 -070049#endif
50
Stephen Hinesb0934b62013-07-03 17:27:38 -070051 free(const_cast<char *>(mName));
52
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080053 if (mPrev || mNext) {
Jason Sams225afd32010-10-21 14:06:55 -070054 // While the normal practice is to call remove before we call
55 // delete. Its possible for objects without a re-use list
56 // for avoiding duplication to be created on the stack. In those
57 // cases we need to remove ourself here.
58 asyncLock();
59 remove();
60 asyncUnlock();
61 }
62
Jason Sams9397e302009-08-27 20:23:34 -070063 rsAssert(!mUserRefCount);
64 rsAssert(!mSysRefCount);
Jason Samse514b452009-09-25 14:51:22 -070065}
66
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080067void ObjectBase::dumpLOGV(const char *op) const {
Stephen Hinesb0934b62013-07-03 17:27:38 -070068 if (mName) {
Steve Block65982012011-10-20 11:56:00 +010069 ALOGV("%s RSobj %p, name %s, refs %i,%i links %p,%p,%p",
Stephen Hinesb0934b62013-07-03 17:27:38 -070070 op, this, mName, mUserRefCount, mSysRefCount, mNext, mPrev, mRSC);
Jason Samsf2649a92009-09-25 16:37:33 -070071 } else {
Steve Block65982012011-10-20 11:56:00 +010072 ALOGV("%s RSobj %p, no-name, refs %i,%i links %p,%p,%p",
Jason Sams225afd32010-10-21 14:06:55 -070073 op, this, mUserRefCount, mSysRefCount, mNext, mPrev, mRSC);
Jason Samsf2649a92009-09-25 16:37:33 -070074 }
75}
76
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080077void ObjectBase::incUserRef() const {
Tim Murray0b575de2013-03-15 15:56:43 -070078 __sync_fetch_and_add(&mUserRefCount, 1);
Steve Block65982012011-10-20 11:56:00 +010079 //ALOGV("ObjectBase %p incU ref %i, %i", this, mUserRefCount, mSysRefCount);
Jason Sams2353ae32010-10-14 17:48:46 -070080}
81
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080082void ObjectBase::incSysRef() const {
Tim Murray0b575de2013-03-15 15:56:43 -070083 __sync_fetch_and_add(&mSysRefCount, 1);
Steve Block65982012011-10-20 11:56:00 +010084 //ALOGV("ObjectBase %p incS ref %i, %i", this, mUserRefCount, mSysRefCount);
Jason Sams9397e302009-08-27 20:23:34 -070085}
86
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080087void ObjectBase::preDestroy() const {
Jason Sams225afd32010-10-21 14:06:55 -070088}
Jason Sams2353ae32010-10-14 17:48:46 -070089
Jason Samsc7cec1e2011-08-18 18:01:33 -070090bool ObjectBase::freeChildren() {
91 return false;
92}
93
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080094bool ObjectBase::checkDelete(const ObjectBase *ref) {
Jason Sams225afd32010-10-21 14:06:55 -070095 if (!ref) {
96 return false;
97 }
Jason Sams2353ae32010-10-14 17:48:46 -070098
Jason Sams225afd32010-10-21 14:06:55 -070099 asyncLock();
100 // This lock protects us against the non-RS threads changing
101 // the ref counts. At this point we should be the only thread
102 // working on them.
103 if (ref->mUserRefCount || ref->mSysRefCount) {
104 asyncUnlock();
105 return false;
106 }
107
108 ref->remove();
109 // At this point we can unlock because there should be no possible way
110 // for another thread to reference this object.
111 ref->preDestroy();
112 asyncUnlock();
113 delete ref;
114 return true;
115}
116
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800117bool ObjectBase::decUserRef() const {
Jason Sams225afd32010-10-21 14:06:55 -0700118 rsAssert(mUserRefCount > 0);
Jason Samsf0c1df42010-10-26 13:09:17 -0700119#if RS_OBJECT_DEBUG
Jason Sams110f1812013-03-14 16:02:18 -0700120 //ALOGV("ObjectBase %p decU ref %i, %i", this, mUserRefCount, mSysRefCount);
Jason Samsf0c1df42010-10-26 13:09:17 -0700121 if (mUserRefCount <= 0) {
Stephen Hinesd216daf2013-04-02 16:32:34 -0700122 mDH->dump();
Jason Samsf0c1df42010-10-26 13:09:17 -0700123 }
124#endif
125
126
Tim Murray0b575de2013-03-15 15:56:43 -0700127 if ((__sync_fetch_and_sub(&mUserRefCount, 1) <= 1)) {
128 __sync_synchronize();
129 if (mSysRefCount <= 0) {
130 return checkDelete(this);
131 }
Jason Samse514b452009-09-25 14:51:22 -0700132 }
133 return false;
134}
135
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800136bool ObjectBase::zeroUserRef() const {
Steve Block65982012011-10-20 11:56:00 +0100137 //ALOGV("ObjectBase %p zeroU ref %i, %i", this, mUserRefCount, mSysRefCount);
Tim Murray0b575de2013-03-15 15:56:43 -0700138 __sync_and_and_fetch(&mUserRefCount, 0);
Stephen Hines61c86952013-04-09 17:34:43 -0700139 if (mSysRefCount <= 0) {
140 return checkDelete(this);
Jason Sams225afd32010-10-21 14:06:55 -0700141 }
142 return false;
Jason Samse514b452009-09-25 14:51:22 -0700143}
144
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800145bool ObjectBase::decSysRef() const {
Steve Block65982012011-10-20 11:56:00 +0100146 //ALOGV("ObjectBase %p decS ref %i, %i", this, mUserRefCount, mSysRefCount);
Jason Sams9397e302009-08-27 20:23:34 -0700147 rsAssert(mSysRefCount > 0);
Tim Murray0b575de2013-03-15 15:56:43 -0700148 if ((__sync_fetch_and_sub(&mSysRefCount, 1) <= 1)) {
149 __sync_synchronize();
150 if (mUserRefCount <= 0) {
151 return checkDelete(this);
152 }
Jason Sams225afd32010-10-21 14:06:55 -0700153 }
154 return false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700155}
156
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800157void ObjectBase::setName(const char *name) {
Stephen Hinesb0934b62013-07-03 17:27:38 -0700158 mName = strdup(name);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700159}
Jason Samsa4a54e42009-06-10 18:39:40 -0700160
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800161void ObjectBase::setName(const char *name, uint32_t len) {
Stephen Hinesb0934b62013-07-03 17:27:38 -0700162 char *c = (char*)calloc(len + 1, sizeof(char));
163 rsAssert(c);
164 memcpy(c, name, len);
165 mName = c;
Jason Samsa4a54e42009-06-10 18:39:40 -0700166}
167
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800168void ObjectBase::asyncLock() {
Jason Sams2353ae32010-10-14 17:48:46 -0700169 pthread_mutex_lock(&gObjectInitMutex);
170}
171
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800172void ObjectBase::asyncUnlock() {
Jason Sams2353ae32010-10-14 17:48:46 -0700173 pthread_mutex_unlock(&gObjectInitMutex);
174}
175
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800176void ObjectBase::add() const {
Jason Sams225afd32010-10-21 14:06:55 -0700177 asyncLock();
Jason Sams2353ae32010-10-14 17:48:46 -0700178
Jason Samse514b452009-09-25 14:51:22 -0700179 rsAssert(!mNext);
180 rsAssert(!mPrev);
Steve Block65982012011-10-20 11:56:00 +0100181 //ALOGV("calling add rsc %p", mRSC);
Jason Samse514b452009-09-25 14:51:22 -0700182 mNext = mRSC->mObjHead;
183 if (mRSC->mObjHead) {
184 mRSC->mObjHead->mPrev = this;
185 }
186 mRSC->mObjHead = this;
Jason Sams2353ae32010-10-14 17:48:46 -0700187
Jason Sams225afd32010-10-21 14:06:55 -0700188 asyncUnlock();
Jason Samse514b452009-09-25 14:51:22 -0700189}
190
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800191void ObjectBase::remove() const {
Steve Block65982012011-10-20 11:56:00 +0100192 //ALOGV("calling remove rsc %p", mRSC);
Jason Samse514b452009-09-25 14:51:22 -0700193 if (!mRSC) {
194 rsAssert(!mPrev);
195 rsAssert(!mNext);
196 return;
197 }
Jason Sams2353ae32010-10-14 17:48:46 -0700198
Jason Samse514b452009-09-25 14:51:22 -0700199 if (mRSC->mObjHead == this) {
200 mRSC->mObjHead = mNext;
201 }
202 if (mPrev) {
203 mPrev->mNext = mNext;
204 }
205 if (mNext) {
206 mNext->mPrev = mPrev;
207 }
208 mPrev = NULL;
209 mNext = NULL;
210}
211
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800212void ObjectBase::zeroAllUserRef(Context *rsc) {
Jason Sams1fddd902009-09-25 15:25:00 -0700213 if (rsc->props.mLogObjects) {
Steve Block65982012011-10-20 11:56:00 +0100214 ALOGV("Forcing release of all outstanding user refs.");
Jason Sams1fddd902009-09-25 15:25:00 -0700215 }
Jason Samse514b452009-09-25 14:51:22 -0700216
217 // This operation can be slow, only to be called during context cleanup.
218 const ObjectBase * o = rsc->mObjHead;
219 while (o) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000220 //ALOGE("o %p", o);
Jason Samse514b452009-09-25 14:51:22 -0700221 if (o->zeroUserRef()) {
222 // deleted the object and possibly others, restart from head.
223 o = rsc->mObjHead;
Steve Blockaf12ac62012-01-06 19:20:56 +0000224 //ALOGE("o head %p", o);
Jason Samse514b452009-09-25 14:51:22 -0700225 } else {
226 o = o->mNext;
Steve Blockaf12ac62012-01-06 19:20:56 +0000227 //ALOGE("o next %p", o);
Jason Samse514b452009-09-25 14:51:22 -0700228 }
229 }
Jason Samsf2649a92009-09-25 16:37:33 -0700230
231 if (rsc->props.mLogObjects) {
Steve Block65982012011-10-20 11:56:00 +0100232 ALOGV("Objects remaining.");
Jason Sams25afc002009-11-19 13:08:17 -0800233 dumpAll(rsc);
Jason Samsf2649a92009-09-25 16:37:33 -0700234 }
Jason Samse514b452009-09-25 14:51:22 -0700235}
236
Jason Samsc7cec1e2011-08-18 18:01:33 -0700237void ObjectBase::freeAllChildren(Context *rsc) {
238 if (rsc->props.mLogObjects) {
Steve Block65982012011-10-20 11:56:00 +0100239 ALOGV("Forcing release of all child objects.");
Jason Samsc7cec1e2011-08-18 18:01:33 -0700240 }
241
242 // This operation can be slow, only to be called during context cleanup.
243 ObjectBase * o = (ObjectBase *)rsc->mObjHead;
244 while (o) {
245 if (o->freeChildren()) {
246 // deleted ref to self and possibly others, restart from head.
247 o = (ObjectBase *)rsc->mObjHead;
248 } else {
249 o = (ObjectBase *)o->mNext;
250 }
251 }
252
253 if (rsc->props.mLogObjects) {
Steve Block65982012011-10-20 11:56:00 +0100254 ALOGV("Objects remaining.");
Jason Samsc7cec1e2011-08-18 18:01:33 -0700255 dumpAll(rsc);
256 }
257}
258
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800259void ObjectBase::dumpAll(Context *rsc) {
Jason Sams225afd32010-10-21 14:06:55 -0700260 asyncLock();
Jason Sams2353ae32010-10-14 17:48:46 -0700261
Steve Block65982012011-10-20 11:56:00 +0100262 ALOGV("Dumping all objects");
Jason Sams25afc002009-11-19 13:08:17 -0800263 const ObjectBase * o = rsc->mObjHead;
264 while (o) {
Steve Block65982012011-10-20 11:56:00 +0100265 ALOGV(" Object %p", o);
Jason Sams25afc002009-11-19 13:08:17 -0800266 o->dumpLOGV(" ");
267 o = o->mNext;
Jason Samsc21cf402009-11-17 17:26:46 -0800268 }
Jason Sams2353ae32010-10-14 17:48:46 -0700269
Jason Sams225afd32010-10-21 14:06:55 -0700270 asyncUnlock();
Jason Samsc21cf402009-11-17 17:26:46 -0800271}
272
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800273bool ObjectBase::isValid(const Context *rsc, const ObjectBase *obj) {
Jason Sams225afd32010-10-21 14:06:55 -0700274 asyncLock();
Jason Sams2353ae32010-10-14 17:48:46 -0700275
Jason Sams605048a2010-09-30 18:15:52 -0700276 const ObjectBase * o = rsc->mObjHead;
277 while (o) {
278 if (o == obj) {
Jason Sams225afd32010-10-21 14:06:55 -0700279 asyncUnlock();
Jason Sams605048a2010-09-30 18:15:52 -0700280 return true;
281 }
282 o = o->mNext;
283 }
Jason Sams225afd32010-10-21 14:06:55 -0700284 asyncUnlock();
Jason Sams605048a2010-09-30 18:15:52 -0700285 return false;
286}