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