rsObjectBase.h revision 2353ae303868d04e3a26002b2f2dc456c15e8170
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
23namespace android {
24namespace renderscript {
25
26class Context;
27class OStream;
28
29// An element is a group of Components that occupies one cell in a structure.
30class ObjectBase
31{
32public:
33    ObjectBase(Context *rsc);
34    virtual ~ObjectBase();
35
36    void incSysRef() const;
37    bool decSysRef() const;
38
39    void incUserRef() const;
40    bool decUserRef() const;
41    bool zeroUserRef() const;
42    void prelockedIncUserRef() const;
43
44    const char * getName() const {
45        return mName.string();
46    }
47    void setName(const char *);
48    void setName(const char *, uint32_t len);
49
50    Context * getContext() const {return mRSC;}
51
52    static void zeroAllUserRef(Context *rsc);
53    static void dumpAll(Context *rsc);
54
55    virtual void dumpLOGV(const char *prefix) const;
56    virtual void serialize(OStream *stream) const = 0;
57    virtual RsA3DClassID getClassId() const = 0;
58
59    static bool isValid(const Context *rsc, const ObjectBase *obj);
60
61    static void lockUserRef();
62    static void unlockUserRef();
63
64protected:
65    const char *mAllocFile;
66    uint32_t mAllocLine;
67    Context *mRSC;
68
69private:
70    static pthread_mutex_t gObjectInitMutex;
71
72    void add() const;
73    void remove() const;
74
75    bool checkDelete() const;
76
77    String8 mName;
78    mutable int32_t mSysRefCount;
79    mutable int32_t mUserRefCount;
80
81    mutable const ObjectBase * mPrev;
82    mutable const ObjectBase * mNext;
83};
84
85template<class T>
86class ObjectBaseRef
87{
88public:
89    ObjectBaseRef() {
90        mRef = NULL;
91    }
92
93    ObjectBaseRef(const ObjectBaseRef &ref) {
94        mRef = ref.get();
95        if (mRef) {
96            mRef->incSysRef();
97        }
98    }
99
100    ObjectBaseRef(T *ref) {
101        mRef = ref;
102        if (mRef) {
103            ref->incSysRef();
104        }
105    }
106
107    ~ObjectBaseRef() {
108        clear();
109    }
110
111    void set(T *ref) {
112        if (mRef != ref) {
113            clear();
114            mRef = ref;
115            if (mRef) {
116                ref->incSysRef();
117            }
118        }
119    }
120
121    void set(const ObjectBaseRef &ref) {
122        set(ref.mRef);
123    }
124
125    void clear() {
126        if (mRef) {
127            mRef->decSysRef();
128        }
129        mRef = NULL;
130    }
131
132    inline T * get() const {
133        return mRef;
134    }
135
136    inline T * operator-> () const {
137        return mRef;
138    }
139
140protected:
141    T * mRef;
142
143};
144
145
146}
147}
148
149#endif //ANDROID_RS_OBJECT_BASE_H
150
151