Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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 | |||||
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame^] | 22 | #define RS_OBJECT_DEBUG 0 |
23 | |||||
24 | #if RS_OBJECT_DEBUG | ||||
25 | #include <utils/CallStack.h> | ||||
26 | #endif | ||||
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 27 | |
28 | namespace android { | ||||
29 | namespace renderscript { | ||||
30 | |||||
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 31 | class Context; |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 32 | class OStream; |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 33 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 34 | // An element is a group of Components that occupies one cell in a structure. |
35 | class ObjectBase | ||||
36 | { | ||||
37 | public: | ||||
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 38 | ObjectBase(Context *rsc); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 39 | |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 40 | void incSysRef() const; |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 41 | bool decSysRef() const; |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 42 | |
43 | void incUserRef() const; | ||||
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 44 | bool decUserRef() const; |
45 | bool zeroUserRef() const; | ||||
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame^] | 46 | |
47 | static bool checkDelete(const ObjectBase *); | ||||
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 48 | |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 49 | const char * getName() const { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 50 | return mName.string(); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 51 | } |
52 | void setName(const char *); | ||||
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 53 | void setName(const char *, uint32_t len); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 54 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 55 | Context * getContext() const {return mRSC;} |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 56 | |
57 | static void zeroAllUserRef(Context *rsc); | ||||
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 58 | static void dumpAll(Context *rsc); |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 59 | |
Jason Sams | 3c0dfba | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 60 | virtual void dumpLOGV(const char *prefix) const; |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 61 | virtual void serialize(OStream *stream) const = 0; |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 62 | virtual RsA3DClassID getClassId() const = 0; |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 63 | |
Jason Sams | f166d9b | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 64 | static bool isValid(const Context *rsc, const ObjectBase *obj); |
65 | |||||
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame^] | 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(); | ||||
Jason Sams | 3b9c52a | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 70 | |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 71 | protected: |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame^] | 72 | // Called inside the async lock for any object list management that is |
73 | // necessary in derived classes. | ||||
74 | virtual void preDestroy() const; | ||||
75 | |||||
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 76 | Context *mRSC; |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame^] | 77 | virtual ~ObjectBase(); |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 78 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 79 | private: |
Jason Sams | 3b9c52a | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 80 | static pthread_mutex_t gObjectInitMutex; |
81 | |||||
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 82 | void add() const; |
83 | void remove() const; | ||||
84 | |||||
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 85 | String8 mName; |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 86 | mutable int32_t mSysRefCount; |
87 | mutable int32_t mUserRefCount; | ||||
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 88 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 89 | mutable const ObjectBase * mPrev; |
90 | mutable const ObjectBase * mNext; | ||||
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame^] | 91 | |
92 | #if RS_OBJECT_DEBUG | ||||
93 | CallStack mStack; | ||||
94 | #endif | ||||
95 | |||||
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 96 | }; |
97 | |||||
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 98 | template<class T> |
99 | class ObjectBaseRef | ||||
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 100 | { |
101 | public: | ||||
102 | ObjectBaseRef() { | ||||
103 | mRef = NULL; | ||||
104 | } | ||||
105 | |||||
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 106 | ObjectBaseRef(const ObjectBaseRef &ref) { |
107 | mRef = ref.get(); | ||||
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 108 | if (mRef) { |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 109 | mRef->incSysRef(); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 110 | } |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 111 | } |
112 | |||||
113 | ObjectBaseRef(T *ref) { | ||||
114 | mRef = ref; | ||||
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 115 | if (mRef) { |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 116 | ref->incSysRef(); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 117 | } |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 118 | } |
119 | |||||
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame^] | 120 | ObjectBaseRef & operator= (const ObjectBaseRef &ref) { |
121 | return ObjectBaseRef(ref); | ||||
122 | } | ||||
123 | |||||
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 124 | ~ObjectBaseRef() { |
125 | clear(); | ||||
126 | } | ||||
127 | |||||
128 | void set(T *ref) { | ||||
129 | if (mRef != ref) { | ||||
130 | clear(); | ||||
131 | mRef = ref; | ||||
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 132 | if (mRef) { |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 133 | ref->incSysRef(); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 134 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 135 | } |
136 | } | ||||
137 | |||||
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 138 | void set(const ObjectBaseRef &ref) { |
139 | set(ref.mRef); | ||||
140 | } | ||||
141 | |||||
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 142 | void clear() { |
143 | if (mRef) { | ||||
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 144 | mRef->decSysRef(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 145 | } |
146 | mRef = NULL; | ||||
147 | } | ||||
148 | |||||
149 | inline T * get() const { | ||||
150 | return mRef; | ||||
151 | } | ||||
152 | |||||
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 153 | inline T * operator-> () const { |
154 | return mRef; | ||||
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 155 | } |
156 | |||||
157 | protected: | ||||
158 | T * mRef; | ||||
159 | |||||
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 160 | }; |
161 | |||||
162 | |||||
163 | } | ||||
164 | } | ||||
165 | |||||
166 | #endif //ANDROID_RS_OBJECT_BASE_H | ||||
167 |