blob: dc85ac7b21e3998a38663118819c171b93d61f26 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
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
23namespace android {
24namespace renderscript {
25
Jason Samse514b452009-09-25 14:51:22 -070026class Context;
27
Jason Sams326e0dd2009-05-22 14:03:28 -070028// An element is a group of Components that occupies one cell in a structure.
29class ObjectBase
30{
31public:
Jason Samse514b452009-09-25 14:51:22 -070032 ObjectBase(Context *rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -070033 virtual ~ObjectBase();
34
Jason Sams9397e302009-08-27 20:23:34 -070035 void incSysRef() const;
Jason Samse514b452009-09-25 14:51:22 -070036 bool decSysRef() const;
Jason Sams9397e302009-08-27 20:23:34 -070037
38 void incUserRef() const;
Jason Samse514b452009-09-25 14:51:22 -070039 bool decUserRef() const;
40 bool zeroUserRef() const;
Jason Sams326e0dd2009-05-22 14:03:28 -070041
Jason Samsa0a1b6f2009-06-10 15:04:38 -070042 const char * getName() const {
43 return mName;
44 }
45 void setName(const char *);
Jason Samsa4a54e42009-06-10 18:39:40 -070046 void setName(const char *, uint32_t len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -070047
Jason Samse514b452009-09-25 14:51:22 -070048 Context * getContext() const {return mRSC;}
49 void setContext(Context *);
50
51 static void zeroAllUserRef(Context *rsc);
52
Jason Samse12c1c52009-09-27 17:50:38 -070053 virtual void dumpLOGV(const char *prefix) const;
Jason Samsf2649a92009-09-25 16:37:33 -070054
55protected:
56 const char *mAllocFile;
57 uint32_t mAllocLine;
58
Jason Sams326e0dd2009-05-22 14:03:28 -070059private:
Jason Samse514b452009-09-25 14:51:22 -070060 void add() const;
61 void remove() const;
62
63 bool checkDelete() const;
64
Jason Samsa0a1b6f2009-06-10 15:04:38 -070065 char * mName;
Jason Samse514b452009-09-25 14:51:22 -070066 Context *mRSC;
Jason Sams9397e302009-08-27 20:23:34 -070067 mutable int32_t mSysRefCount;
68 mutable int32_t mUserRefCount;
Jason Sams326e0dd2009-05-22 14:03:28 -070069
Jason Samse514b452009-09-25 14:51:22 -070070 mutable const ObjectBase * mPrev;
71 mutable const ObjectBase * mNext;
Jason Sams326e0dd2009-05-22 14:03:28 -070072};
73
Jason Sams9397e302009-08-27 20:23:34 -070074template<class T>
75class ObjectBaseRef
Jason Sams326e0dd2009-05-22 14:03:28 -070076{
77public:
78 ObjectBaseRef() {
79 mRef = NULL;
80 }
81
Jason Sams10308932009-06-09 12:15:30 -070082 ObjectBaseRef(const ObjectBaseRef &ref) {
83 mRef = ref.get();
Jason Samsa0a1b6f2009-06-10 15:04:38 -070084 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -070085 mRef->incSysRef();
Jason Samsa0a1b6f2009-06-10 15:04:38 -070086 }
Jason Sams10308932009-06-09 12:15:30 -070087 }
88
89 ObjectBaseRef(T *ref) {
90 mRef = ref;
Jason Samsa0a1b6f2009-06-10 15:04:38 -070091 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -070092 ref->incSysRef();
Jason Samsa0a1b6f2009-06-10 15:04:38 -070093 }
Jason Sams10308932009-06-09 12:15:30 -070094 }
95
Jason Sams326e0dd2009-05-22 14:03:28 -070096 ~ObjectBaseRef() {
97 clear();
98 }
99
100 void set(T *ref) {
101 if (mRef != ref) {
102 clear();
103 mRef = ref;
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700104 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -0700105 ref->incSysRef();
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700106 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700107 }
108 }
109
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700110 void set(const ObjectBaseRef &ref) {
111 set(ref.mRef);
112 }
113
Jason Sams326e0dd2009-05-22 14:03:28 -0700114 void clear() {
115 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -0700116 mRef->decSysRef();
Jason Sams326e0dd2009-05-22 14:03:28 -0700117 }
118 mRef = NULL;
119 }
120
121 inline T * get() const {
122 return mRef;
123 }
124
Jason Sams9397e302009-08-27 20:23:34 -0700125 inline T * operator-> () const {
126 return mRef;
Jason Sams326e0dd2009-05-22 14:03:28 -0700127 }
128
129protected:
130 T * mRef;
131
Jason Sams326e0dd2009-05-22 14:03:28 -0700132};
133
134
135}
136}
137
138#endif //ANDROID_RS_OBJECT_BASE_H
139