blob: acfc5ceede67e6aaee7f2c75611415ce6a4960e9 [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 Sams326e0dd2009-05-22 14:03:28 -070019
20using namespace android;
21using namespace android::renderscript;
22
Jason Samse514b452009-09-25 14:51:22 -070023ObjectBase::ObjectBase(Context *rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -070024{
Jason Sams9397e302009-08-27 20:23:34 -070025 mUserRefCount = 0;
26 mSysRefCount = 0;
Jason Samsa0a1b6f2009-06-10 15:04:38 -070027 mName = NULL;
Jason Samse514b452009-09-25 14:51:22 -070028 mRSC = NULL;
29 mNext = NULL;
30 mPrev = NULL;
31 setContext(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -070032}
33
34ObjectBase::~ObjectBase()
35{
Jason Samse514b452009-09-25 14:51:22 -070036 //LOGV("~ObjectBase %p ref %i,%i", this, mUserRefCount, mSysRefCount);
Jason Sams9397e302009-08-27 20:23:34 -070037 rsAssert(!mUserRefCount);
38 rsAssert(!mSysRefCount);
Jason Samse514b452009-09-25 14:51:22 -070039 remove();
40}
41
42void ObjectBase::setContext(Context *rsc)
43{
44 if (mRSC) {
45 remove();
46 }
47 mRSC = rsc;
48 if (rsc) {
49 add();
50 }
Jason Sams326e0dd2009-05-22 14:03:28 -070051}
52
Jason Sams9397e302009-08-27 20:23:34 -070053void ObjectBase::incUserRef() const
Jason Sams326e0dd2009-05-22 14:03:28 -070054{
Jason Sams9397e302009-08-27 20:23:34 -070055 mUserRefCount ++;
Jason Sams992a0b72009-06-23 12:22:47 -070056 //LOGV("ObjectBase %p inc ref %i", this, mRefCount);
Jason Sams326e0dd2009-05-22 14:03:28 -070057}
58
Jason Sams9397e302009-08-27 20:23:34 -070059void ObjectBase::incSysRef() const
Jason Sams326e0dd2009-05-22 14:03:28 -070060{
Jason Sams9397e302009-08-27 20:23:34 -070061 mSysRefCount ++;
62 //LOGV("ObjectBase %p inc ref %i", this, mRefCount);
63}
64
Jason Samse514b452009-09-25 14:51:22 -070065bool ObjectBase::checkDelete() const
66{
67 if (!(mSysRefCount | mUserRefCount)) {
68 if (mName) {
69 LOGV("Deleting RS object %p, name %s", this, mName);
70 } else {
71 LOGV("Deleting RS object %p, no name", this);
72 }
73 delete this;
74 return true;
75 }
76 return false;
77}
78
79bool ObjectBase::decUserRef() const
Jason Sams9397e302009-08-27 20:23:34 -070080{
81 rsAssert(mUserRefCount > 0);
82 mUserRefCount --;
Jason Sams992a0b72009-06-23 12:22:47 -070083 //LOGV("ObjectBase %p dec ref %i", this, mRefCount);
Jason Samse514b452009-09-25 14:51:22 -070084 return checkDelete();
Jason Sams9397e302009-08-27 20:23:34 -070085}
86
Jason Samse514b452009-09-25 14:51:22 -070087bool ObjectBase::zeroUserRef() const
88{
89 mUserRefCount = 0;
90 //LOGV("ObjectBase %p dec ref %i", this, mRefCount);
91 return checkDelete();
92}
93
94bool ObjectBase::decSysRef() const
Jason Sams9397e302009-08-27 20:23:34 -070095{
96 rsAssert(mSysRefCount > 0);
97 mSysRefCount --;
98 //LOGV("ObjectBase %p dec ref %i", this, mRefCount);
Jason Samse514b452009-09-25 14:51:22 -070099 return checkDelete();
Jason Sams326e0dd2009-05-22 14:03:28 -0700100}
101
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700102void ObjectBase::setName(const char *name)
103{
104 delete mName;
105 mName = NULL;
106 if (name) {
107 mName = new char[strlen(name) +1];
108 strcpy(mName, name);
109 }
110}
Jason Samsa4a54e42009-06-10 18:39:40 -0700111
112void ObjectBase::setName(const char *name, uint32_t len)
113{
114 delete mName;
115 mName = NULL;
116 if (name) {
117 mName = new char[len + 1];
118 memcpy(mName, name, len);
119 mName[len] = 0;
120 }
121}
122
Jason Samse514b452009-09-25 14:51:22 -0700123void ObjectBase::add() const
124{
125 rsAssert(!mNext);
126 rsAssert(!mPrev);
127 //LOGV("calling add rsc %p", mRSC);
128 mNext = mRSC->mObjHead;
129 if (mRSC->mObjHead) {
130 mRSC->mObjHead->mPrev = this;
131 }
132 mRSC->mObjHead = this;
133}
134
135void ObjectBase::remove() const
136{
137 //LOGV("calling remove rsc %p", mRSC);
138 if (!mRSC) {
139 rsAssert(!mPrev);
140 rsAssert(!mNext);
141 return;
142 }
143 if (mRSC->mObjHead == this) {
144 mRSC->mObjHead = mNext;
145 }
146 if (mPrev) {
147 mPrev->mNext = mNext;
148 }
149 if (mNext) {
150 mNext->mPrev = mPrev;
151 }
152 mPrev = NULL;
153 mNext = NULL;
154}
155
156void ObjectBase::zeroAllUserRef(Context *rsc)
157{
158 LOGV("Forcing release of all outstanding user refs.");
159
160 // This operation can be slow, only to be called during context cleanup.
161 const ObjectBase * o = rsc->mObjHead;
162 while (o) {
163 //LOGE("o %p", o);
164 if (o->zeroUserRef()) {
165 // deleted the object and possibly others, restart from head.
166 o = rsc->mObjHead;
167 //LOGE("o head %p", o);
168 } else {
169 o = o->mNext;
170 //LOGE("o next %p", o);
171 }
172 }
173}
174