blob: 83fa482a52f0f161fbab7d58743384474ec70114 [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)) {
Jason Sams1fddd902009-09-25 15:25:00 -070068 if (mRSC && mRSC->props.mLogObjects) {
69 if (mName) {
70 LOGV("Deleting RS object %p, name %s", this, mName);
71 } else {
72 LOGV("Deleting RS object %p, no name", this);
73 }
Jason Samse514b452009-09-25 14:51:22 -070074 }
75 delete this;
76 return true;
77 }
78 return false;
79}
80
81bool ObjectBase::decUserRef() const
Jason Sams9397e302009-08-27 20:23:34 -070082{
83 rsAssert(mUserRefCount > 0);
84 mUserRefCount --;
Jason Sams992a0b72009-06-23 12:22:47 -070085 //LOGV("ObjectBase %p dec ref %i", this, mRefCount);
Jason Samse514b452009-09-25 14:51:22 -070086 return checkDelete();
Jason Sams9397e302009-08-27 20:23:34 -070087}
88
Jason Samse514b452009-09-25 14:51:22 -070089bool ObjectBase::zeroUserRef() const
90{
91 mUserRefCount = 0;
92 //LOGV("ObjectBase %p dec ref %i", this, mRefCount);
93 return checkDelete();
94}
95
96bool ObjectBase::decSysRef() const
Jason Sams9397e302009-08-27 20:23:34 -070097{
98 rsAssert(mSysRefCount > 0);
99 mSysRefCount --;
100 //LOGV("ObjectBase %p dec ref %i", this, mRefCount);
Jason Samse514b452009-09-25 14:51:22 -0700101 return checkDelete();
Jason Sams326e0dd2009-05-22 14:03:28 -0700102}
103
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700104void ObjectBase::setName(const char *name)
105{
106 delete mName;
107 mName = NULL;
108 if (name) {
109 mName = new char[strlen(name) +1];
110 strcpy(mName, name);
111 }
112}
Jason Samsa4a54e42009-06-10 18:39:40 -0700113
114void ObjectBase::setName(const char *name, uint32_t len)
115{
116 delete mName;
117 mName = NULL;
118 if (name) {
119 mName = new char[len + 1];
120 memcpy(mName, name, len);
121 mName[len] = 0;
122 }
123}
124
Jason Samse514b452009-09-25 14:51:22 -0700125void ObjectBase::add() const
126{
127 rsAssert(!mNext);
128 rsAssert(!mPrev);
129 //LOGV("calling add rsc %p", mRSC);
130 mNext = mRSC->mObjHead;
131 if (mRSC->mObjHead) {
132 mRSC->mObjHead->mPrev = this;
133 }
134 mRSC->mObjHead = this;
135}
136
137void ObjectBase::remove() const
138{
139 //LOGV("calling remove rsc %p", mRSC);
140 if (!mRSC) {
141 rsAssert(!mPrev);
142 rsAssert(!mNext);
143 return;
144 }
145 if (mRSC->mObjHead == this) {
146 mRSC->mObjHead = mNext;
147 }
148 if (mPrev) {
149 mPrev->mNext = mNext;
150 }
151 if (mNext) {
152 mNext->mPrev = mPrev;
153 }
154 mPrev = NULL;
155 mNext = NULL;
156}
157
158void ObjectBase::zeroAllUserRef(Context *rsc)
159{
Jason Sams1fddd902009-09-25 15:25:00 -0700160 if (rsc->props.mLogObjects) {
161 LOGV("Forcing release of all outstanding user refs.");
162 }
Jason Samse514b452009-09-25 14:51:22 -0700163
164 // This operation can be slow, only to be called during context cleanup.
165 const ObjectBase * o = rsc->mObjHead;
166 while (o) {
167 //LOGE("o %p", o);
168 if (o->zeroUserRef()) {
169 // deleted the object and possibly others, restart from head.
170 o = rsc->mObjHead;
171 //LOGE("o head %p", o);
172 } else {
173 o = o->mNext;
174 //LOGE("o next %p", o);
175 }
176 }
177}
178