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