blob: 8d1ace1a676be705009531cbf1fd96d1912bbc7f [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;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070027class OStream;
Jason Samse514b452009-09-25 14:51:22 -070028
Jason Sams326e0dd2009-05-22 14:03:28 -070029// An element is a group of Components that occupies one cell in a structure.
30class ObjectBase
31{
32public:
Jason Samse514b452009-09-25 14:51:22 -070033 ObjectBase(Context *rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -070034 virtual ~ObjectBase();
35
Jason Sams9397e302009-08-27 20:23:34 -070036 void incSysRef() const;
Jason Samse514b452009-09-25 14:51:22 -070037 bool decSysRef() const;
Jason Sams9397e302009-08-27 20:23:34 -070038
39 void incUserRef() const;
Jason Samse514b452009-09-25 14:51:22 -070040 bool decUserRef() const;
41 bool zeroUserRef() const;
Jason Sams2353ae32010-10-14 17:48:46 -070042 void prelockedIncUserRef() const;
Jason Sams326e0dd2009-05-22 14:03:28 -070043
Jason Samsa0a1b6f2009-06-10 15:04:38 -070044 const char * getName() const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070045 return mName.string();
Jason Samsa0a1b6f2009-06-10 15:04:38 -070046 }
47 void setName(const char *);
Jason Samsa4a54e42009-06-10 18:39:40 -070048 void setName(const char *, uint32_t len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -070049
Jason Samse514b452009-09-25 14:51:22 -070050 Context * getContext() const {return mRSC;}
Jason Samse514b452009-09-25 14:51:22 -070051
52 static void zeroAllUserRef(Context *rsc);
Jason Samsc21cf402009-11-17 17:26:46 -080053 static void dumpAll(Context *rsc);
Jason Samse514b452009-09-25 14:51:22 -070054
Jason Samse12c1c52009-09-27 17:50:38 -070055 virtual void dumpLOGV(const char *prefix) const;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070056 virtual void serialize(OStream *stream) const = 0;
Alex Sakhartchoukb825f672010-06-04 10:06:50 -070057 virtual RsA3DClassID getClassId() const = 0;
Jason Samsf2649a92009-09-25 16:37:33 -070058
Jason Sams605048a2010-09-30 18:15:52 -070059 static bool isValid(const Context *rsc, const ObjectBase *obj);
60
Jason Sams2353ae32010-10-14 17:48:46 -070061 static void lockUserRef();
62 static void unlockUserRef();
63
Jason Samsf2649a92009-09-25 16:37:33 -070064protected:
65 const char *mAllocFile;
66 uint32_t mAllocLine;
Jason Sams81549542010-02-17 15:38:10 -080067 Context *mRSC;
Jason Samsf2649a92009-09-25 16:37:33 -070068
Jason Sams326e0dd2009-05-22 14:03:28 -070069private:
Jason Sams2353ae32010-10-14 17:48:46 -070070 static pthread_mutex_t gObjectInitMutex;
71
Jason Samse514b452009-09-25 14:51:22 -070072 void add() const;
73 void remove() const;
74
75 bool checkDelete() const;
76
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070077 String8 mName;
Jason Sams9397e302009-08-27 20:23:34 -070078 mutable int32_t mSysRefCount;
79 mutable int32_t mUserRefCount;
Jason Sams326e0dd2009-05-22 14:03:28 -070080
Jason Samse514b452009-09-25 14:51:22 -070081 mutable const ObjectBase * mPrev;
82 mutable const ObjectBase * mNext;
Jason Sams326e0dd2009-05-22 14:03:28 -070083};
84
Jason Sams9397e302009-08-27 20:23:34 -070085template<class T>
86class ObjectBaseRef
Jason Sams326e0dd2009-05-22 14:03:28 -070087{
88public:
89 ObjectBaseRef() {
90 mRef = NULL;
91 }
92
Jason Sams10308932009-06-09 12:15:30 -070093 ObjectBaseRef(const ObjectBaseRef &ref) {
94 mRef = ref.get();
Jason Samsa0a1b6f2009-06-10 15:04:38 -070095 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -070096 mRef->incSysRef();
Jason Samsa0a1b6f2009-06-10 15:04:38 -070097 }
Jason Sams10308932009-06-09 12:15:30 -070098 }
99
100 ObjectBaseRef(T *ref) {
101 mRef = ref;
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700102 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -0700103 ref->incSysRef();
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700104 }
Jason Sams10308932009-06-09 12:15:30 -0700105 }
106
Jason Sams326e0dd2009-05-22 14:03:28 -0700107 ~ObjectBaseRef() {
108 clear();
109 }
110
111 void set(T *ref) {
112 if (mRef != ref) {
113 clear();
114 mRef = ref;
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700115 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -0700116 ref->incSysRef();
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700117 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700118 }
119 }
120
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700121 void set(const ObjectBaseRef &ref) {
122 set(ref.mRef);
123 }
124
Jason Sams326e0dd2009-05-22 14:03:28 -0700125 void clear() {
126 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -0700127 mRef->decSysRef();
Jason Sams326e0dd2009-05-22 14:03:28 -0700128 }
129 mRef = NULL;
130 }
131
132 inline T * get() const {
133 return mRef;
134 }
135
Jason Sams9397e302009-08-27 20:23:34 -0700136 inline T * operator-> () const {
137 return mRef;
Jason Sams326e0dd2009-05-22 14:03:28 -0700138 }
139
140protected:
141 T * mRef;
142
Jason Sams326e0dd2009-05-22 14:03:28 -0700143};
144
145
146}
147}
148
149#endif //ANDROID_RS_OBJECT_BASE_H
150