Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
| 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 Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 18 | #include "rsContext.h" |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 19 | |
| 20 | using namespace android; |
| 21 | using namespace android::renderscript; |
| 22 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 23 | ObjectBase::ObjectBase(Context *rsc) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 24 | { |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 25 | mUserRefCount = 0; |
| 26 | mSysRefCount = 0; |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 27 | mName = NULL; |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 28 | mRSC = NULL; |
| 29 | mNext = NULL; |
| 30 | mPrev = NULL; |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 31 | mAllocFile = __FILE__; |
| 32 | mAllocLine = __LINE__; |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 33 | setContext(rsc); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | ObjectBase::~ObjectBase() |
| 37 | { |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 38 | //LOGV("~ObjectBase %p ref %i,%i", this, mUserRefCount, mSysRefCount); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 39 | rsAssert(!mUserRefCount); |
| 40 | rsAssert(!mSysRefCount); |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 41 | remove(); |
Jason Sams | b7a6c43 | 2009-11-02 14:25:10 -0800 | [diff] [blame] | 42 | delete[] mName; |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Jason Sams | 3c0dfba | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 45 | void ObjectBase::dumpLOGV(const char *op) const |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 46 | { |
| 47 | if (mName) { |
| 48 | LOGV("%s RSobj %p, name %s, refs %i,%i from %s,%i links %p,%p,%p", |
| 49 | op, this, mName, mUserRefCount, mSysRefCount, mAllocFile, mAllocLine, mNext, mPrev, mRSC); |
| 50 | } else { |
| 51 | LOGV("%s RSobj %p, no-name, refs %i,%i from %s,%i links %p,%p,%p", |
| 52 | op, this, mUserRefCount, mSysRefCount, mAllocFile, mAllocLine, mNext, mPrev, mRSC); |
| 53 | } |
| 54 | } |
| 55 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 56 | void ObjectBase::setContext(Context *rsc) |
| 57 | { |
| 58 | if (mRSC) { |
| 59 | remove(); |
| 60 | } |
| 61 | mRSC = rsc; |
| 62 | if (rsc) { |
| 63 | add(); |
| 64 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 67 | void ObjectBase::incUserRef() const |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 68 | { |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 69 | mUserRefCount ++; |
Jason Sams | f29ca50 | 2009-06-23 12:22:47 -0700 | [diff] [blame] | 70 | //LOGV("ObjectBase %p inc ref %i", this, mRefCount); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 73 | void ObjectBase::incSysRef() const |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 74 | { |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 75 | mSysRefCount ++; |
| 76 | //LOGV("ObjectBase %p inc ref %i", this, mRefCount); |
| 77 | } |
| 78 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 79 | bool ObjectBase::checkDelete() const |
| 80 | { |
| 81 | if (!(mSysRefCount | mUserRefCount)) { |
Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 82 | if (mRSC && mRSC->props.mLogObjects) { |
Jason Sams | 3c0dfba | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 83 | dumpLOGV("checkDelete"); |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 84 | } |
| 85 | delete this; |
| 86 | return true; |
| 87 | } |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | bool ObjectBase::decUserRef() const |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 92 | { |
| 93 | rsAssert(mUserRefCount > 0); |
| 94 | mUserRefCount --; |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 95 | //dumpObj("decUserRef"); |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 96 | return checkDelete(); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 99 | bool ObjectBase::zeroUserRef() const |
| 100 | { |
| 101 | mUserRefCount = 0; |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 102 | //dumpObj("zeroUserRef"); |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 103 | return checkDelete(); |
| 104 | } |
| 105 | |
| 106 | bool ObjectBase::decSysRef() const |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 107 | { |
| 108 | rsAssert(mSysRefCount > 0); |
| 109 | mSysRefCount --; |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 110 | //dumpObj("decSysRef"); |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 111 | return checkDelete(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 114 | void ObjectBase::setName(const char *name) |
| 115 | { |
Jason Sams | 9d5e03d | 2009-11-03 11:25:42 -0800 | [diff] [blame] | 116 | setName(name, strlen(name)); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 117 | } |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 118 | |
| 119 | void ObjectBase::setName(const char *name, uint32_t len) |
| 120 | { |
| 121 | delete mName; |
| 122 | mName = NULL; |
| 123 | if (name) { |
| 124 | mName = new char[len + 1]; |
| 125 | memcpy(mName, name, len); |
| 126 | mName[len] = 0; |
| 127 | } |
| 128 | } |
| 129 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 130 | void ObjectBase::add() const |
| 131 | { |
| 132 | rsAssert(!mNext); |
| 133 | rsAssert(!mPrev); |
| 134 | //LOGV("calling add rsc %p", mRSC); |
| 135 | mNext = mRSC->mObjHead; |
| 136 | if (mRSC->mObjHead) { |
| 137 | mRSC->mObjHead->mPrev = this; |
| 138 | } |
| 139 | mRSC->mObjHead = this; |
| 140 | } |
| 141 | |
| 142 | void ObjectBase::remove() const |
| 143 | { |
| 144 | //LOGV("calling remove rsc %p", mRSC); |
| 145 | if (!mRSC) { |
| 146 | rsAssert(!mPrev); |
| 147 | rsAssert(!mNext); |
| 148 | return; |
| 149 | } |
| 150 | if (mRSC->mObjHead == this) { |
| 151 | mRSC->mObjHead = mNext; |
| 152 | } |
| 153 | if (mPrev) { |
| 154 | mPrev->mNext = mNext; |
| 155 | } |
| 156 | if (mNext) { |
| 157 | mNext->mPrev = mPrev; |
| 158 | } |
| 159 | mPrev = NULL; |
| 160 | mNext = NULL; |
| 161 | } |
| 162 | |
| 163 | void ObjectBase::zeroAllUserRef(Context *rsc) |
| 164 | { |
Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 165 | if (rsc->props.mLogObjects) { |
| 166 | LOGV("Forcing release of all outstanding user refs."); |
| 167 | } |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 168 | |
| 169 | // This operation can be slow, only to be called during context cleanup. |
| 170 | const ObjectBase * o = rsc->mObjHead; |
| 171 | while (o) { |
| 172 | //LOGE("o %p", o); |
| 173 | if (o->zeroUserRef()) { |
| 174 | // deleted the object and possibly others, restart from head. |
| 175 | o = rsc->mObjHead; |
| 176 | //LOGE("o head %p", o); |
| 177 | } else { |
| 178 | o = o->mNext; |
| 179 | //LOGE("o next %p", o); |
| 180 | } |
| 181 | } |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 182 | |
| 183 | if (rsc->props.mLogObjects) { |
| 184 | LOGV("Objects remaining."); |
Jason Sams | ddac83a | 2009-11-19 13:08:17 -0800 | [diff] [blame] | 185 | dumpAll(rsc); |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 186 | } |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 189 | void ObjectBase::dumpAll(Context *rsc) |
| 190 | { |
Jason Sams | ddac83a | 2009-11-19 13:08:17 -0800 | [diff] [blame] | 191 | LOGV("Dumping all objects"); |
| 192 | const ObjectBase * o = rsc->mObjHead; |
| 193 | while (o) { |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 194 | LOGV(" Object %p", o); |
Jason Sams | ddac83a | 2009-11-19 13:08:17 -0800 | [diff] [blame] | 195 | o->dumpLOGV(" "); |
| 196 | o = o->mNext; |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |