rsObjectBase.h revision f0c1df480304a72ce41e7d4b088319cbd7f0938a
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#ifndef ANDROID_RS_OBJECT_BASE_H
18#define ANDROID_RS_OBJECT_BASE_H
19
20#include "rsUtils.h"
21
22#define RS_OBJECT_DEBUG 0
23
24#include <utils/CallStack.h>
25
26namespace android {
27namespace renderscript {
28
29class Context;
30class OStream;
31
32// An element is a group of Components that occupies one cell in a structure.
33class ObjectBase
34{
35public:
36    ObjectBase(Context *rsc);
37
38    void incSysRef() const;
39    bool decSysRef() const;
40
41    void incUserRef() const;
42    bool decUserRef() const;
43    bool zeroUserRef() const;
44
45    static bool checkDelete(const ObjectBase *);
46
47    const char * getName() const {
48        return mName.string();
49    }
50    void setName(const char *);
51    void setName(const char *, uint32_t len);
52
53    Context * getContext() const {return mRSC;}
54
55    static void zeroAllUserRef(Context *rsc);
56    static void dumpAll(Context *rsc);
57
58    virtual void dumpLOGV(const char *prefix) const;
59    virtual void serialize(OStream *stream) const = 0;
60    virtual RsA3DClassID getClassId() const = 0;
61
62    static bool isValid(const Context *rsc, const ObjectBase *obj);
63
64    // The async lock is taken during object creation in non-rs threads
65    // and object deletion in the rs thread.
66    static void asyncLock();
67    static void asyncUnlock();
68
69protected:
70    // Called inside the async lock for any object list management that is
71    // necessary in derived classes.
72    virtual void preDestroy() const;
73
74    Context *mRSC;
75    virtual ~ObjectBase();
76
77private:
78    static pthread_mutex_t gObjectInitMutex;
79
80    void add() const;
81    void remove() const;
82
83    String8 mName;
84    mutable int32_t mSysRefCount;
85    mutable int32_t mUserRefCount;
86
87    mutable const ObjectBase * mPrev;
88    mutable const ObjectBase * mNext;
89
90#if RS_OBJECT_DEBUG
91    CallStack mStack;
92#endif
93
94};
95
96template<class T>
97class ObjectBaseRef
98{
99public:
100    ObjectBaseRef() {
101        mRef = NULL;
102    }
103
104    ObjectBaseRef(const ObjectBaseRef &ref) {
105        mRef = ref.get();
106        if (mRef) {
107            mRef->incSysRef();
108        }
109    }
110
111    ObjectBaseRef(T *ref) {
112        mRef = ref;
113        if (mRef) {
114            ref->incSysRef();
115        }
116    }
117
118    ObjectBaseRef & operator= (const ObjectBaseRef &ref) {
119        return ObjectBaseRef(ref);
120    }
121
122    ~ObjectBaseRef() {
123        clear();
124    }
125
126    void set(T *ref) {
127        if (mRef != ref) {
128            clear();
129            mRef = ref;
130            if (mRef) {
131                ref->incSysRef();
132            }
133        }
134    }
135
136    void set(const ObjectBaseRef &ref) {
137        set(ref.mRef);
138    }
139
140    void clear() {
141        if (mRef) {
142            mRef->decSysRef();
143        }
144        mRef = NULL;
145    }
146
147    inline T * get() const {
148        return mRef;
149    }
150
151    inline T * operator-> () const {
152        return mRef;
153    }
154
155protected:
156    T * mRef;
157
158};
159
160
161}
162}
163
164#endif //ANDROID_RS_OBJECT_BASE_H
165
166