rsObjectBase.h revision b0934b67b95cc27e2358c2aa4db5f7c1067c8f9b
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#include "rsDefines.h"
22#include "rsDebugHelper.h"
23
24namespace android {
25namespace renderscript {
26
27class Context;
28class OStream;
29
30// An element is a group of Components that occupies one cell in a structure.
31class ObjectBase {
32public:
33    ObjectBase(Context *rsc);
34
35    void incSysRef() const;
36    bool decSysRef() const;
37
38    void incUserRef() const;
39    bool decUserRef() const;
40    bool zeroUserRef() const;
41
42    static bool checkDelete(const ObjectBase *);
43
44    const char * getName() const {
45        return mName;
46    }
47    void setName(const char *);
48    void setName(const char *, uint32_t len);
49
50    Context * getContext() const {return mRSC;}
51    virtual bool freeChildren();
52
53    static void zeroAllUserRef(Context *rsc);
54    static void freeAllChildren(Context *rsc);
55    static void dumpAll(Context *rsc);
56
57    virtual void dumpLOGV(const char *prefix) const;
58    virtual void serialize(Context *rsc, 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    const char* mName;
83    mutable int32_t mSysRefCount;
84    mutable int32_t mUserRefCount;
85
86    mutable const ObjectBase * mPrev;
87    mutable const ObjectBase * mNext;
88
89    DebugHelper *mDH;
90};
91
92template<class T>
93class ObjectBaseRef {
94public:
95    ObjectBaseRef() {
96        mRef = NULL;
97    }
98
99    ObjectBaseRef(const ObjectBaseRef &ref) {
100        mRef = ref.get();
101        if (mRef) {
102            mRef->incSysRef();
103        }
104    }
105
106    ObjectBaseRef(T *ref) {
107        mRef = ref;
108        if (mRef) {
109            ref->incSysRef();
110        }
111    }
112
113    ObjectBaseRef & operator= (const ObjectBaseRef &ref) {
114        if (&ref != this) {
115            set(ref);
116        }
117        return *this;
118    }
119
120    ~ObjectBaseRef() {
121        clear();
122    }
123
124    void set(T *ref) {
125        if (mRef != ref) {
126            clear();
127            mRef = ref;
128            if (mRef) {
129                ref->incSysRef();
130            }
131        }
132    }
133
134    void set(const ObjectBaseRef &ref) {
135        set(ref.mRef);
136    }
137
138    void clear() {
139        if (mRef) {
140            mRef->decSysRef();
141        }
142        mRef = NULL;
143    }
144
145    inline T * get() const {
146        return mRef;
147    }
148
149    inline T * operator-> () const {
150        return mRef;
151    }
152
153protected:
154    T * mRef;
155};
156
157}
158}
159
160#endif //ANDROID_RS_OBJECT_BASE_H
161
162