blob: 44dc0425ea72d86351c863b80b2b4fa69aa78d2d [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"
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070018
19#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Samse514b452009-09-25 14:51:22 -070020#include "rsContext.h"
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070021#else
22#include "rsContextHostStub.h"
23#endif
Jason Sams326e0dd2009-05-22 14:03:28 -070024
Jason Sams225afd32010-10-21 14:06:55 -070025
Jason Sams326e0dd2009-05-22 14:03:28 -070026using namespace android;
27using namespace android::renderscript;
28
Jason Sams2353ae32010-10-14 17:48:46 -070029pthread_mutex_t ObjectBase::gObjectInitMutex = PTHREAD_MUTEX_INITIALIZER;
30
Jason Samse514b452009-09-25 14:51:22 -070031ObjectBase::ObjectBase(Context *rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -070032{
Jason Sams9397e302009-08-27 20:23:34 -070033 mUserRefCount = 0;
34 mSysRefCount = 0;
Jason Sams2353ae32010-10-14 17:48:46 -070035 mRSC = rsc;
Jason Samse514b452009-09-25 14:51:22 -070036 mNext = NULL;
37 mPrev = NULL;
Jason Sams225afd32010-10-21 14:06:55 -070038
39#if RS_OBJECT_DEBUG
40 mStack.update(2);
41#endif
Jason Sams2353ae32010-10-14 17:48:46 -070042
43 rsAssert(rsc);
44 add();
Jason Sams225afd32010-10-21 14:06:55 -070045 //LOGV("ObjectBase %p con", this);
Jason Sams326e0dd2009-05-22 14:03:28 -070046}
47
48ObjectBase::~ObjectBase()
49{
Jason Samse514b452009-09-25 14:51:22 -070050 //LOGV("~ObjectBase %p ref %i,%i", this, mUserRefCount, mSysRefCount);
Jason Sams225afd32010-10-21 14:06:55 -070051#if RS_OBJECT_DEBUG
52 mStack.dump();
53#endif
54
55 if(mPrev || mNext) {
56 // While the normal practice is to call remove before we call
57 // delete. Its possible for objects without a re-use list
58 // for avoiding duplication to be created on the stack. In those
59 // cases we need to remove ourself here.
60 asyncLock();
61 remove();
62 asyncUnlock();
63 }
64
Jason Sams9397e302009-08-27 20:23:34 -070065 rsAssert(!mUserRefCount);
66 rsAssert(!mSysRefCount);
Jason Samse514b452009-09-25 14:51:22 -070067}
68
Jason Samse12c1c52009-09-27 17:50:38 -070069void ObjectBase::dumpLOGV(const char *op) const
Jason Samsf2649a92009-09-25 16:37:33 -070070{
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070071 if (mName.size()) {
Jason Sams225afd32010-10-21 14:06:55 -070072 LOGV("%s RSobj %p, name %s, refs %i,%i links %p,%p,%p",
73 op, this, mName.string(), mUserRefCount, mSysRefCount, mNext, mPrev, mRSC);
Jason Samsf2649a92009-09-25 16:37:33 -070074 } else {
Jason Sams225afd32010-10-21 14:06:55 -070075 LOGV("%s RSobj %p, no-name, refs %i,%i links %p,%p,%p",
76 op, this, mUserRefCount, mSysRefCount, mNext, mPrev, mRSC);
Jason Samsf2649a92009-09-25 16:37:33 -070077 }
78}
79
Jason Sams9397e302009-08-27 20:23:34 -070080void ObjectBase::incUserRef() const
Jason Sams326e0dd2009-05-22 14:03:28 -070081{
Jason Sams225afd32010-10-21 14:06:55 -070082 android_atomic_inc(&mUserRefCount);
83 //LOGV("ObjectBase %p incU ref %i, %i", this, mUserRefCount, mSysRefCount);
Jason Sams2353ae32010-10-14 17:48:46 -070084}
85
Jason Sams9397e302009-08-27 20:23:34 -070086void ObjectBase::incSysRef() const
Jason Sams326e0dd2009-05-22 14:03:28 -070087{
Jason Sams225afd32010-10-21 14:06:55 -070088 android_atomic_inc(&mSysRefCount);
89 //LOGV("ObjectBase %p incS ref %i, %i", this, mUserRefCount, mSysRefCount);
Jason Sams9397e302009-08-27 20:23:34 -070090}
91
Jason Sams225afd32010-10-21 14:06:55 -070092void ObjectBase::preDestroy() const
Jason Samse514b452009-09-25 14:51:22 -070093{
Jason Sams225afd32010-10-21 14:06:55 -070094}
Jason Sams2353ae32010-10-14 17:48:46 -070095
Jason Sams225afd32010-10-21 14:06:55 -070096bool ObjectBase::checkDelete(const ObjectBase *ref)
97{
98 if (!ref) {
99 return false;
100 }
Jason Sams2353ae32010-10-14 17:48:46 -0700101
Jason Sams225afd32010-10-21 14:06:55 -0700102 asyncLock();
103 // This lock protects us against the non-RS threads changing
104 // the ref counts. At this point we should be the only thread
105 // working on them.
106 if (ref->mUserRefCount || ref->mSysRefCount) {
107 asyncUnlock();
108 return false;
109 }
110
111 ref->remove();
112 // At this point we can unlock because there should be no possible way
113 // for another thread to reference this object.
114 ref->preDestroy();
115 asyncUnlock();
116 delete ref;
117 return true;
118}
119
120
121bool ObjectBase::decUserRef() const
122{
Jason Sams225afd32010-10-21 14:06:55 -0700123 rsAssert(mUserRefCount > 0);
Jason Samsf0c1df42010-10-26 13:09:17 -0700124#if RS_OBJECT_DEBUG
125 LOGV("ObjectBase %p decU ref %i, %i", this, mUserRefCount, mSysRefCount);
126 if (mUserRefCount <= 0) {
127 mStack.dump();
128 }
129#endif
130
131
Jason Sams225afd32010-10-21 14:06:55 -0700132 if ((android_atomic_dec(&mUserRefCount) <= 1) &&
133 (android_atomic_acquire_load(&mSysRefCount) <= 0)) {
134 return checkDelete(this);
Jason Samse514b452009-09-25 14:51:22 -0700135 }
136 return false;
137}
138
Jason Samse514b452009-09-25 14:51:22 -0700139bool ObjectBase::zeroUserRef() const
140{
Jason Sams225afd32010-10-21 14:06:55 -0700141 //LOGV("ObjectBase %p zeroU ref %i, %i", this, mUserRefCount, mSysRefCount);
142 android_atomic_acquire_store(0, &mUserRefCount);
143 if (android_atomic_acquire_load(&mSysRefCount) <= 0) {
144 return checkDelete(this);
145 }
146 return false;
Jason Samse514b452009-09-25 14:51:22 -0700147}
148
149bool ObjectBase::decSysRef() const
Jason Sams9397e302009-08-27 20:23:34 -0700150{
Jason Sams225afd32010-10-21 14:06:55 -0700151 //LOGV("ObjectBase %p decS ref %i, %i", this, mUserRefCount, mSysRefCount);
Jason Sams9397e302009-08-27 20:23:34 -0700152 rsAssert(mSysRefCount > 0);
Jason Sams225afd32010-10-21 14:06:55 -0700153 if ((android_atomic_dec(&mSysRefCount) <= 1) &&
154 (android_atomic_acquire_load(&mUserRefCount) <= 0)) {
155 return checkDelete(this);
156 }
157 return false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700158}
159
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700160void ObjectBase::setName(const char *name)
161{
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700162 mName.setTo(name);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700163}
Jason Samsa4a54e42009-06-10 18:39:40 -0700164
165void ObjectBase::setName(const char *name, uint32_t len)
166{
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700167 mName.setTo(name, len);
Jason Samsa4a54e42009-06-10 18:39:40 -0700168}
169
Jason Sams225afd32010-10-21 14:06:55 -0700170void ObjectBase::asyncLock()
Jason Sams2353ae32010-10-14 17:48:46 -0700171{
172 pthread_mutex_lock(&gObjectInitMutex);
173}
174
Jason Sams225afd32010-10-21 14:06:55 -0700175void ObjectBase::asyncUnlock()
Jason Sams2353ae32010-10-14 17:48:46 -0700176{
177 pthread_mutex_unlock(&gObjectInitMutex);
178}
179
Jason Samse514b452009-09-25 14:51:22 -0700180void ObjectBase::add() const
181{
Jason Sams225afd32010-10-21 14:06:55 -0700182 asyncLock();
Jason Sams2353ae32010-10-14 17:48:46 -0700183
Jason Samse514b452009-09-25 14:51:22 -0700184 rsAssert(!mNext);
185 rsAssert(!mPrev);
186 //LOGV("calling add rsc %p", mRSC);
187 mNext = mRSC->mObjHead;
188 if (mRSC->mObjHead) {
189 mRSC->mObjHead->mPrev = this;
190 }
191 mRSC->mObjHead = this;
Jason Sams2353ae32010-10-14 17:48:46 -0700192
Jason Sams225afd32010-10-21 14:06:55 -0700193 asyncUnlock();
Jason Samse514b452009-09-25 14:51:22 -0700194}
195
196void ObjectBase::remove() const
197{
198 //LOGV("calling remove rsc %p", mRSC);
199 if (!mRSC) {
200 rsAssert(!mPrev);
201 rsAssert(!mNext);
202 return;
203 }
Jason Sams2353ae32010-10-14 17:48:46 -0700204
Jason Samse514b452009-09-25 14:51:22 -0700205 if (mRSC->mObjHead == this) {
206 mRSC->mObjHead = mNext;
207 }
208 if (mPrev) {
209 mPrev->mNext = mNext;
210 }
211 if (mNext) {
212 mNext->mPrev = mPrev;
213 }
214 mPrev = NULL;
215 mNext = NULL;
216}
217
218void ObjectBase::zeroAllUserRef(Context *rsc)
219{
Jason Sams1fddd902009-09-25 15:25:00 -0700220 if (rsc->props.mLogObjects) {
221 LOGV("Forcing release of all outstanding user refs.");
222 }
Jason Samse514b452009-09-25 14:51:22 -0700223
224 // This operation can be slow, only to be called during context cleanup.
225 const ObjectBase * o = rsc->mObjHead;
226 while (o) {
227 //LOGE("o %p", o);
228 if (o->zeroUserRef()) {
229 // deleted the object and possibly others, restart from head.
230 o = rsc->mObjHead;
231 //LOGE("o head %p", o);
232 } else {
233 o = o->mNext;
234 //LOGE("o next %p", o);
235 }
236 }
Jason Samsf2649a92009-09-25 16:37:33 -0700237
238 if (rsc->props.mLogObjects) {
239 LOGV("Objects remaining.");
Jason Sams25afc002009-11-19 13:08:17 -0800240 dumpAll(rsc);
Jason Samsf2649a92009-09-25 16:37:33 -0700241 }
Jason Samse514b452009-09-25 14:51:22 -0700242}
243
Jason Samsc21cf402009-11-17 17:26:46 -0800244void ObjectBase::dumpAll(Context *rsc)
245{
Jason Sams225afd32010-10-21 14:06:55 -0700246 asyncLock();
Jason Sams2353ae32010-10-14 17:48:46 -0700247
Jason Sams25afc002009-11-19 13:08:17 -0800248 LOGV("Dumping all objects");
249 const ObjectBase * o = rsc->mObjHead;
250 while (o) {
Jason Sams81549542010-02-17 15:38:10 -0800251 LOGV(" Object %p", o);
Jason Sams25afc002009-11-19 13:08:17 -0800252 o->dumpLOGV(" ");
253 o = o->mNext;
Jason Samsc21cf402009-11-17 17:26:46 -0800254 }
Jason Sams2353ae32010-10-14 17:48:46 -0700255
Jason Sams225afd32010-10-21 14:06:55 -0700256 asyncUnlock();
Jason Samsc21cf402009-11-17 17:26:46 -0800257}
258
Jason Sams605048a2010-09-30 18:15:52 -0700259bool ObjectBase::isValid(const Context *rsc, const ObjectBase *obj)
260{
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