blob: dc94bcc42ed2aacb198d86a46b448a3897e14ff7 [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;
Chris Wailes44bef6f2014-08-12 13:51:10 -070029 mNext = nullptr;
30 mPrev = nullptr;
31 mDH = nullptr;
32 mName = nullptr;
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() {
Jason Samsa36c50a2014-06-17 12:06:06 -070044 //ALOGE("~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;
Chris Wailes44bef6f2014-08-12 13:51:10 -070048 mDH = nullptr;
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);
Jason Samse514b452009-09-25 14:51:22 -0700181 mNext = mRSC->mObjHead;
182 if (mRSC->mObjHead) {
183 mRSC->mObjHead->mPrev = this;
184 }
185 mRSC->mObjHead = this;
Jason Sams2353ae32010-10-14 17:48:46 -0700186
Jason Sams225afd32010-10-21 14:06:55 -0700187 asyncUnlock();
Jason Samse514b452009-09-25 14:51:22 -0700188}
189
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800190void ObjectBase::remove() const {
Jason Samse514b452009-09-25 14:51:22 -0700191 if (!mRSC) {
192 rsAssert(!mPrev);
193 rsAssert(!mNext);
194 return;
195 }
Jason Sams2353ae32010-10-14 17:48:46 -0700196
Jason Samse514b452009-09-25 14:51:22 -0700197 if (mRSC->mObjHead == this) {
198 mRSC->mObjHead = mNext;
199 }
200 if (mPrev) {
201 mPrev->mNext = mNext;
202 }
203 if (mNext) {
204 mNext->mPrev = mPrev;
205 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700206 mPrev = nullptr;
207 mNext = nullptr;
Jason Samse514b452009-09-25 14:51:22 -0700208}
209
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800210void ObjectBase::zeroAllUserRef(Context *rsc) {
Jason Sams1fddd902009-09-25 15:25:00 -0700211 if (rsc->props.mLogObjects) {
Steve Block65982012011-10-20 11:56:00 +0100212 ALOGV("Forcing release of all outstanding user refs.");
Jason Sams1fddd902009-09-25 15:25:00 -0700213 }
Jason Samse514b452009-09-25 14:51:22 -0700214
215 // This operation can be slow, only to be called during context cleanup.
216 const ObjectBase * o = rsc->mObjHead;
217 while (o) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000218 //ALOGE("o %p", o);
Jason Samse514b452009-09-25 14:51:22 -0700219 if (o->zeroUserRef()) {
220 // deleted the object and possibly others, restart from head.
221 o = rsc->mObjHead;
Steve Blockaf12ac62012-01-06 19:20:56 +0000222 //ALOGE("o head %p", o);
Jason Samse514b452009-09-25 14:51:22 -0700223 } else {
224 o = o->mNext;
Steve Blockaf12ac62012-01-06 19:20:56 +0000225 //ALOGE("o next %p", o);
Jason Samse514b452009-09-25 14:51:22 -0700226 }
227 }
Jason Samsf2649a92009-09-25 16:37:33 -0700228
229 if (rsc->props.mLogObjects) {
Steve Block65982012011-10-20 11:56:00 +0100230 ALOGV("Objects remaining.");
Jason Sams25afc002009-11-19 13:08:17 -0800231 dumpAll(rsc);
Jason Samsf2649a92009-09-25 16:37:33 -0700232 }
Jason Samse514b452009-09-25 14:51:22 -0700233}
234
Jason Samsc7cec1e2011-08-18 18:01:33 -0700235void ObjectBase::freeAllChildren(Context *rsc) {
236 if (rsc->props.mLogObjects) {
Steve Block65982012011-10-20 11:56:00 +0100237 ALOGV("Forcing release of all child objects.");
Jason Samsc7cec1e2011-08-18 18:01:33 -0700238 }
239
240 // This operation can be slow, only to be called during context cleanup.
241 ObjectBase * o = (ObjectBase *)rsc->mObjHead;
242 while (o) {
243 if (o->freeChildren()) {
244 // deleted ref to self and possibly others, restart from head.
245 o = (ObjectBase *)rsc->mObjHead;
246 } else {
247 o = (ObjectBase *)o->mNext;
248 }
249 }
250
251 if (rsc->props.mLogObjects) {
Steve Block65982012011-10-20 11:56:00 +0100252 ALOGV("Objects remaining.");
Jason Samsc7cec1e2011-08-18 18:01:33 -0700253 dumpAll(rsc);
254 }
255}
256
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800257void ObjectBase::dumpAll(Context *rsc) {
Jason Sams225afd32010-10-21 14:06:55 -0700258 asyncLock();
Jason Sams2353ae32010-10-14 17:48:46 -0700259
Steve Block65982012011-10-20 11:56:00 +0100260 ALOGV("Dumping all objects");
Jason Sams25afc002009-11-19 13:08:17 -0800261 const ObjectBase * o = rsc->mObjHead;
262 while (o) {
Steve Block65982012011-10-20 11:56:00 +0100263 ALOGV(" Object %p", o);
Jason Sams25afc002009-11-19 13:08:17 -0800264 o->dumpLOGV(" ");
265 o = o->mNext;
Jason Samsc21cf402009-11-17 17:26:46 -0800266 }
Jason Sams2353ae32010-10-14 17:48:46 -0700267
Jason Sams225afd32010-10-21 14:06:55 -0700268 asyncUnlock();
Jason Samsc21cf402009-11-17 17:26:46 -0800269}
270
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800271bool ObjectBase::isValid(const Context *rsc, const ObjectBase *obj) {
Jason Sams225afd32010-10-21 14:06:55 -0700272 asyncLock();
Jason Sams2353ae32010-10-14 17:48:46 -0700273
Jason Sams605048a2010-09-30 18:15:52 -0700274 const ObjectBase * o = rsc->mObjHead;
275 while (o) {
276 if (o == obj) {
Jason Sams225afd32010-10-21 14:06:55 -0700277 asyncUnlock();
Jason Sams605048a2010-09-30 18:15:52 -0700278 return true;
279 }
280 o = o->mNext;
281 }
Jason Sams225afd32010-10-21 14:06:55 -0700282 asyncUnlock();
Jason Sams605048a2010-09-30 18:15:52 -0700283 return false;
284}
Jason Samsa36c50a2014-06-17 12:06:06 -0700285
286void ObjectBase::callUpdateCacheObject(const Context *rsc, void *dstObj) const {
287 //ALOGE("ObjectBase::callUpdateCacheObject %p %p", this, dstObj);
288 *((const void **)dstObj) = this;
289}
290