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