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