• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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     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 
getName()44     const char * getName() const {
45         return mName;
46     }
assignName(const char * s)47     void assignName(const char *s) {mName = s;}
48     void setName(const char *);
49     void setName(const char *, uint32_t len);
50 
getContext()51     Context * getContext() const {return mRSC;}
52     virtual bool freeChildren();
53 
54     static void zeroAllUserRef(Context *rsc);
55     static void freeAllChildren(Context *rsc);
56     static void dumpAll(Context *rsc);
57 
58     virtual void dumpLOGV(const char *prefix) const;
59     virtual void serialize(Context *rsc, OStream *stream) const = 0;
60     virtual RsA3DClassID getClassId() const = 0;
61 
62     static bool isValid(const Context *rsc, const ObjectBase *obj);
63 
64     // The async lock is taken during object creation in non-rs threads
65     // and object deletion in the rs thread.
66     static void asyncLock();
67     static void asyncUnlock();
68 
69     virtual void callUpdateCacheObject(const Context *rsc, void *dstObj) const;
70 
71 protected:
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 
79 private:
80     static pthread_mutex_t gObjectInitMutex;
81 
82     void add() const;
83     void remove() const;
84 
85     const char* mName;
86     mutable int32_t mSysRefCount;
87     mutable int32_t mUserRefCount;
88 
89     mutable const ObjectBase * mPrev;
90     mutable const ObjectBase * mNext;
91 
92     DebugHelper *mDH;
93 };
94 
95 template<class T>
96 class ObjectBaseRef {
97 public:
ObjectBaseRef()98     ObjectBaseRef() {
99         mRef = NULL;
100     }
101 
ObjectBaseRef(const ObjectBaseRef & ref)102     ObjectBaseRef(const ObjectBaseRef &ref) {
103         mRef = ref.get();
104         if (mRef) {
105             mRef->incSysRef();
106         }
107     }
108 
ObjectBaseRef(T * ref)109     ObjectBaseRef(T *ref) {
110         mRef = ref;
111         if (mRef) {
112             ref->incSysRef();
113         }
114     }
115 
116     ObjectBaseRef & operator= (const ObjectBaseRef &ref) {
117         if (&ref != this) {
118             set(ref);
119         }
120         return *this;
121     }
122 
~ObjectBaseRef()123     ~ObjectBaseRef() {
124         clear();
125     }
126 
set(T * ref)127     void set(T *ref) {
128         if (mRef != ref) {
129             clear();
130             mRef = ref;
131             if (mRef) {
132                 ref->incSysRef();
133             }
134         }
135     }
136 
set(const ObjectBaseRef & ref)137     void set(const ObjectBaseRef &ref) {
138         set(ref.mRef);
139     }
140 
clear()141     void clear() {
142         if (mRef) {
143             mRef->decSysRef();
144         }
145         mRef = NULL;
146     }
147 
get()148     inline T * get() const {
149         return mRef;
150     }
151 
152     inline T * operator-> () const {
153         return mRef;
154     }
155 
156 protected:
157     T * mRef;
158 };
159 
160 }
161 }
162 
163 #endif //ANDROID_RS_OBJECT_BASE_H
164 
165