rsObjectBase.cpp revision 1fddd90849deaae89b546ff492c345d485bbce42
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"
18#include "rsContext.h"
19
20using namespace android;
21using namespace android::renderscript;
22
23ObjectBase::ObjectBase(Context *rsc)
24{
25    mUserRefCount = 0;
26    mSysRefCount = 0;
27    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