rsObjectBase.cpp revision 1fddd90849deaae89b546ff492c345d485bbce42
1603073430bbcb1bd29db7afb9b14e2732ad589fbFred Quintana/* 2603073430bbcb1bd29db7afb9b14e2732ad589fbFred Quintana * Copyright (C) 2009 The Android Open Source Project 3603073430bbcb1bd29db7afb9b14e2732ad589fbFred Quintana * 4603073430bbcb1bd29db7afb9b14e2732ad589fbFred Quintana * Licensed under the Apache License, Version 2.0 (the "License"); 5603073430bbcb1bd29db7afb9b14e2732ad589fbFred Quintana * you may not use this file except in compliance with the License. 6603073430bbcb1bd29db7afb9b14e2732ad589fbFred Quintana * You may obtain a copy of the License at 7603073430bbcb1bd29db7afb9b14e2732ad589fbFred Quintana * 8603073430bbcb1bd29db7afb9b14e2732ad589fbFred Quintana * http://www.apache.org/licenses/LICENSE-2.0 9603073430bbcb1bd29db7afb9b14e2732ad589fbFred Quintana * 10603073430bbcb1bd29db7afb9b14e2732ad589fbFred Quintana * Unless required by applicable law or agreed to in writing, software 11603073430bbcb1bd29db7afb9b14e2732ad589fbFred Quintana * distributed under the License is distributed on an "AS IS" BASIS, 12603073430bbcb1bd29db7afb9b14e2732ad589fbFred Quintana * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13603073430bbcb1bd29db7afb9b14e2732ad589fbFred Quintana * See the License for the specific language governing permissions and 14603073430bbcb1bd29db7afb9b14e2732ad589fbFred Quintana * limitations under the License. 15603073430bbcb1bd29db7afb9b14e2732ad589fbFred Quintana */ 16603073430bbcb1bd29db7afb9b14e2732ad589fbFred Quintana 17603073430bbcb1bd29db7afb9b14e2732ad589fbFred Quintana#include "rsObjectBase.h" 18a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana#include "rsContext.h" 19603073430bbcb1bd29db7afb9b14e2732ad589fbFred Quintana 20603073430bbcb1bd29db7afb9b14e2732ad589fbFred Quintanausing namespace android; 21603073430bbcb1bd29db7afb9b14e2732ad589fbFred Quintanausing namespace android::renderscript; 223326920329cecb57c7ff1fc5c6add5c98aab9ed9Fred Quintana 23603073430bbcb1bd29db7afb9b14e2732ad589fbFred QuintanaObjectBase::ObjectBase(Context *rsc) 24603073430bbcb1bd29db7afb9b14e2732ad589fbFred Quintana{ 25a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana mUserRefCount = 0; 26a698f4276968d078b1b9e2f3738c4f559a3307b2Fred Quintana mSysRefCount = 0; 27603073430bbcb1bd29db7afb9b14e2732ad589fbFred Quintana mName = NULL; 28 mRSC = NULL; 29 mNext = NULL; 30 mPrev = NULL; 31 setContext(rsc); 32} 33 34ObjectBase::~ObjectBase() 35{ 36 //LOGV("~ObjectBase %p ref %i,%i", this, mUserRefCount, mSysRefCount); 37 rsAssert(!mUserRefCount); 38 rsAssert(!mSysRefCount); 39 remove(); 40} 41 42void ObjectBase::setContext(Context *rsc) 43{ 44 if (mRSC) { 45 remove(); 46 } 47 mRSC = rsc; 48 if (rsc) { 49 add(); 50 } 51} 52 53void ObjectBase::incUserRef() const 54{ 55 mUserRefCount ++; 56 //LOGV("ObjectBase %p inc ref %i", this, mRefCount); 57} 58 59void ObjectBase::incSysRef() const 60{ 61 mSysRefCount ++; 62 //LOGV("ObjectBase %p inc ref %i", this, mRefCount); 63} 64 65bool ObjectBase::checkDelete() const 66{ 67 if (!(mSysRefCount | mUserRefCount)) { 68 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 } 74 } 75 delete this; 76 return true; 77 } 78 return false; 79} 80 81bool ObjectBase::decUserRef() const 82{ 83 rsAssert(mUserRefCount > 0); 84 mUserRefCount --; 85 //LOGV("ObjectBase %p dec ref %i", this, mRefCount); 86 return checkDelete(); 87} 88 89bool 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 97{ 98 rsAssert(mSysRefCount > 0); 99 mSysRefCount --; 100 //LOGV("ObjectBase %p dec ref %i", this, mRefCount); 101 return checkDelete(); 102} 103 104void 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} 113 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 125void 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{ 160 if (rsc->props.mLogObjects) { 161 LOGV("Forcing release of all outstanding user refs."); 162 } 163 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 179