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