blob: 59fb4a6b2ce26097488612c405f2eb5d97269833 [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 Sams326e0dd2009-05-22 14:03:28 -070042
Jason Samsa0a1b6f2009-06-10 15:04:38 -070043 const char * getName() const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070044 return mName.string();
Jason Samsa0a1b6f2009-06-10 15:04:38 -070045 }
46 void setName(const char *);
Jason Samsa4a54e42009-06-10 18:39:40 -070047 void setName(const char *, uint32_t len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -070048
Jason Samse514b452009-09-25 14:51:22 -070049 Context * getContext() const {return mRSC;}
50 void setContext(Context *);
51
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 Samsf2649a92009-09-25 16:37:33 -070061protected:
62 const char *mAllocFile;
63 uint32_t mAllocLine;
Jason Sams81549542010-02-17 15:38:10 -080064 Context *mRSC;
Jason Samsf2649a92009-09-25 16:37:33 -070065
Jason Sams326e0dd2009-05-22 14:03:28 -070066private:
Jason Samse514b452009-09-25 14:51:22 -070067 void add() const;
68 void remove() const;
69
70 bool checkDelete() const;
71
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070072 String8 mName;
Jason Sams9397e302009-08-27 20:23:34 -070073 mutable int32_t mSysRefCount;
74 mutable int32_t mUserRefCount;
Jason Sams326e0dd2009-05-22 14:03:28 -070075
Jason Samse514b452009-09-25 14:51:22 -070076 mutable const ObjectBase * mPrev;
77 mutable const ObjectBase * mNext;
Jason Sams326e0dd2009-05-22 14:03:28 -070078};
79
Jason Sams9397e302009-08-27 20:23:34 -070080template<class T>
81class ObjectBaseRef
Jason Sams326e0dd2009-05-22 14:03:28 -070082{
83public:
84 ObjectBaseRef() {
85 mRef = NULL;
86 }
87
Jason Sams10308932009-06-09 12:15:30 -070088 ObjectBaseRef(const ObjectBaseRef &ref) {
89 mRef = ref.get();
Jason Samsa0a1b6f2009-06-10 15:04:38 -070090 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -070091 mRef->incSysRef();
Jason Samsa0a1b6f2009-06-10 15:04:38 -070092 }
Jason Sams10308932009-06-09 12:15:30 -070093 }
94
95 ObjectBaseRef(T *ref) {
96 mRef = ref;
Jason Samsa0a1b6f2009-06-10 15:04:38 -070097 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -070098 ref->incSysRef();
Jason Samsa0a1b6f2009-06-10 15:04:38 -070099 }
Jason Sams10308932009-06-09 12:15:30 -0700100 }
101
Jason Sams326e0dd2009-05-22 14:03:28 -0700102 ~ObjectBaseRef() {
103 clear();
104 }
105
106 void set(T *ref) {
107 if (mRef != ref) {
108 clear();
109 mRef = ref;
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700110 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -0700111 ref->incSysRef();
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700112 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700113 }
114 }
115
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700116 void set(const ObjectBaseRef &ref) {
117 set(ref.mRef);
118 }
119
Jason Sams326e0dd2009-05-22 14:03:28 -0700120 void clear() {
121 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -0700122 mRef->decSysRef();
Jason Sams326e0dd2009-05-22 14:03:28 -0700123 }
124 mRef = NULL;
125 }
126
127 inline T * get() const {
128 return mRef;
129 }
130
Jason Sams9397e302009-08-27 20:23:34 -0700131 inline T * operator-> () const {
132 return mRef;
Jason Sams326e0dd2009-05-22 14:03:28 -0700133 }
134
135protected:
136 T * mRef;
137
Jason Sams326e0dd2009-05-22 14:03:28 -0700138};
139
140
141}
142}
143
144#endif //ANDROID_RS_OBJECT_BASE_H
145