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