blob: bb03b8716933f18b9a29bcc8df153a35008e184c [file] [log] [blame]
Jason Samsd19f10d2009-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 Samsa9e7a052009-09-25 14:51:22 -070026class Context;
27
Jason Samsd19f10d2009-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 Samsa9e7a052009-09-25 14:51:22 -070032 ObjectBase(Context *rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -070033 virtual ~ObjectBase();
34
Jason Sams07ae4062009-08-27 20:23:34 -070035 void incSysRef() const;
Jason Samsa9e7a052009-09-25 14:51:22 -070036 bool decSysRef() const;
Jason Sams07ae4062009-08-27 20:23:34 -070037
38 void incUserRef() const;
Jason Samsa9e7a052009-09-25 14:51:22 -070039 bool decUserRef() const;
40 bool zeroUserRef() const;
Jason Samsd19f10d2009-05-22 14:03:28 -070041
Jason Sams3eaa3382009-06-10 15:04:38 -070042 const char * getName() const {
43 return mName;
44 }
45 void setName(const char *);
Jason Samsd5680f92009-06-10 18:39:40 -070046 void setName(const char *, uint32_t len);
Jason Sams3eaa3382009-06-10 15:04:38 -070047
Jason Samsa9e7a052009-09-25 14:51:22 -070048 Context * getContext() const {return mRSC;}
49 void setContext(Context *);
50
51 static void zeroAllUserRef(Context *rsc);
Jason Sams715333b2009-11-17 17:26:46 -080052 static void dumpAll(Context *rsc);
Jason Samsa9e7a052009-09-25 14:51:22 -070053
Jason Sams3c0dfba2009-09-27 17:50:38 -070054 virtual void dumpLOGV(const char *prefix) const;
Jason Sams61f08d62009-09-25 16:37:33 -070055
56protected:
57 const char *mAllocFile;
58 uint32_t mAllocLine;
Jason Samse4c487a2010-02-17 15:38:10 -080059 Context *mRSC;
Jason Sams61f08d62009-09-25 16:37:33 -070060
Jason Samsd19f10d2009-05-22 14:03:28 -070061private:
Jason Samsa9e7a052009-09-25 14:51:22 -070062 void add() const;
63 void remove() const;
64
65 bool checkDelete() const;
66
Jason Sams3eaa3382009-06-10 15:04:38 -070067 char * mName;
Jason Sams07ae4062009-08-27 20:23:34 -070068 mutable int32_t mSysRefCount;
69 mutable int32_t mUserRefCount;
Jason Samsd19f10d2009-05-22 14:03:28 -070070
Jason Samsa9e7a052009-09-25 14:51:22 -070071 mutable const ObjectBase * mPrev;
72 mutable const ObjectBase * mNext;
Jason Samsd19f10d2009-05-22 14:03:28 -070073};
74
Jason Sams07ae4062009-08-27 20:23:34 -070075template<class T>
76class ObjectBaseRef
Jason Samsd19f10d2009-05-22 14:03:28 -070077{
78public:
79 ObjectBaseRef() {
80 mRef = NULL;
81 }
82
Jason Samsda423d82009-06-09 12:15:30 -070083 ObjectBaseRef(const ObjectBaseRef &ref) {
84 mRef = ref.get();
Jason Sams3eaa3382009-06-10 15:04:38 -070085 if (mRef) {
Jason Sams07ae4062009-08-27 20:23:34 -070086 mRef->incSysRef();
Jason Sams3eaa3382009-06-10 15:04:38 -070087 }
Jason Samsda423d82009-06-09 12:15:30 -070088 }
89
90 ObjectBaseRef(T *ref) {
91 mRef = ref;
Jason Sams3eaa3382009-06-10 15:04:38 -070092 if (mRef) {
Jason Sams07ae4062009-08-27 20:23:34 -070093 ref->incSysRef();
Jason Sams3eaa3382009-06-10 15:04:38 -070094 }
Jason Samsda423d82009-06-09 12:15:30 -070095 }
96
Jason Samsd19f10d2009-05-22 14:03:28 -070097 ~ObjectBaseRef() {
98 clear();
99 }
100
101 void set(T *ref) {
102 if (mRef != ref) {
103 clear();
104 mRef = ref;
Jason Sams3eaa3382009-06-10 15:04:38 -0700105 if (mRef) {
Jason Sams07ae4062009-08-27 20:23:34 -0700106 ref->incSysRef();
Jason Sams3eaa3382009-06-10 15:04:38 -0700107 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700108 }
109 }
110
Jason Sams3eaa3382009-06-10 15:04:38 -0700111 void set(const ObjectBaseRef &ref) {
112 set(ref.mRef);
113 }
114
Jason Samsd19f10d2009-05-22 14:03:28 -0700115 void clear() {
116 if (mRef) {
Jason Sams07ae4062009-08-27 20:23:34 -0700117 mRef->decSysRef();
Jason Samsd19f10d2009-05-22 14:03:28 -0700118 }
119 mRef = NULL;
120 }
121
122 inline T * get() const {
123 return mRef;
124 }
125
Jason Sams07ae4062009-08-27 20:23:34 -0700126 inline T * operator-> () const {
127 return mRef;
Jason Samsd19f10d2009-05-22 14:03:28 -0700128 }
129
130protected:
131 T * mRef;
132
Jason Samsd19f10d2009-05-22 14:03:28 -0700133};
134
135
136}
137}
138
139#endif //ANDROID_RS_OBJECT_BASE_H
140