blob: ad95b8175cee1289bbf37e96b6097ad45f0e2ab4 [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
59protected:
60 const char *mAllocFile;
61 uint32_t mAllocLine;
Jason Sams81549542010-02-17 15:38:10 -080062 Context *mRSC;
Jason Samsf2649a92009-09-25 16:37:33 -070063
Jason Sams326e0dd2009-05-22 14:03:28 -070064private:
Jason Samse514b452009-09-25 14:51:22 -070065 void add() const;
66 void remove() const;
67
68 bool checkDelete() const;
69
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070070 String8 mName;
Jason Sams9397e302009-08-27 20:23:34 -070071 mutable int32_t mSysRefCount;
72 mutable int32_t mUserRefCount;
Jason Sams326e0dd2009-05-22 14:03:28 -070073
Jason Samse514b452009-09-25 14:51:22 -070074 mutable const ObjectBase * mPrev;
75 mutable const ObjectBase * mNext;
Jason Sams326e0dd2009-05-22 14:03:28 -070076};
77
Jason Sams9397e302009-08-27 20:23:34 -070078template<class T>
79class ObjectBaseRef
Jason Sams326e0dd2009-05-22 14:03:28 -070080{
81public:
82 ObjectBaseRef() {
83 mRef = NULL;
84 }
85
Jason Sams10308932009-06-09 12:15:30 -070086 ObjectBaseRef(const ObjectBaseRef &ref) {
87 mRef = ref.get();
Jason Samsa0a1b6f2009-06-10 15:04:38 -070088 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -070089 mRef->incSysRef();
Jason Samsa0a1b6f2009-06-10 15:04:38 -070090 }
Jason Sams10308932009-06-09 12:15:30 -070091 }
92
93 ObjectBaseRef(T *ref) {
94 mRef = ref;
Jason Samsa0a1b6f2009-06-10 15:04:38 -070095 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -070096 ref->incSysRef();
Jason Samsa0a1b6f2009-06-10 15:04:38 -070097 }
Jason Sams10308932009-06-09 12:15:30 -070098 }
99
Jason Sams326e0dd2009-05-22 14:03:28 -0700100 ~ObjectBaseRef() {
101 clear();
102 }
103
104 void set(T *ref) {
105 if (mRef != ref) {
106 clear();
107 mRef = ref;
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700108 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -0700109 ref->incSysRef();
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700110 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700111 }
112 }
113
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700114 void set(const ObjectBaseRef &ref) {
115 set(ref.mRef);
116 }
117
Jason Sams326e0dd2009-05-22 14:03:28 -0700118 void clear() {
119 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -0700120 mRef->decSysRef();
Jason Sams326e0dd2009-05-22 14:03:28 -0700121 }
122 mRef = NULL;
123 }
124
125 inline T * get() const {
126 return mRef;
127 }
128
Jason Sams9397e302009-08-27 20:23:34 -0700129 inline T * operator-> () const {
130 return mRef;
Jason Sams326e0dd2009-05-22 14:03:28 -0700131 }
132
133protected:
134 T * mRef;
135
Jason Sams326e0dd2009-05-22 14:03:28 -0700136};
137
138
139}
140}
141
142#endif //ANDROID_RS_OBJECT_BASE_H
143