blob: d9f5f3ba124934704481544b97afe8280eced53d [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
Jason Sams225afd32010-10-21 14:06:55 -070022#define RS_OBJECT_DEBUG 0
23
Jason Samsf0c1df42010-10-26 13:09:17 -070024#include <utils/CallStack.h>
Jason Sams326e0dd2009-05-22 14:03:28 -070025
26namespace android {
27namespace renderscript {
28
Jason Samse514b452009-09-25 14:51:22 -070029class Context;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070030class OStream;
Jason Samse514b452009-09-25 14:51:22 -070031
Jason Sams326e0dd2009-05-22 14:03:28 -070032// An element is a group of Components that occupies one cell in a structure.
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080033class ObjectBase {
Jason Sams326e0dd2009-05-22 14:03:28 -070034public:
Jason Samse514b452009-09-25 14:51:22 -070035 ObjectBase(Context *rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -070036
Jason Sams9397e302009-08-27 20:23:34 -070037 void incSysRef() const;
Jason Samse514b452009-09-25 14:51:22 -070038 bool decSysRef() const;
Jason Sams9397e302009-08-27 20:23:34 -070039
40 void incUserRef() const;
Jason Samse514b452009-09-25 14:51:22 -070041 bool decUserRef() const;
42 bool zeroUserRef() const;
Jason Sams225afd32010-10-21 14:06:55 -070043
44 static bool checkDelete(const ObjectBase *);
Jason Sams326e0dd2009-05-22 14:03:28 -070045
Jason Samsa0a1b6f2009-06-10 15:04:38 -070046 const char * getName() const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070047 return mName.string();
Jason Samsa0a1b6f2009-06-10 15:04:38 -070048 }
49 void setName(const char *);
Jason Samsa4a54e42009-06-10 18:39:40 -070050 void setName(const char *, uint32_t len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -070051
Jason Samse514b452009-09-25 14:51:22 -070052 Context * getContext() const {return mRSC;}
Jason Samsc7cec1e2011-08-18 18:01:33 -070053 virtual bool freeChildren();
Jason Samse514b452009-09-25 14:51:22 -070054
55 static void zeroAllUserRef(Context *rsc);
Jason Samsc7cec1e2011-08-18 18:01:33 -070056 static void freeAllChildren(Context *rsc);
Jason Samsc21cf402009-11-17 17:26:46 -080057 static void dumpAll(Context *rsc);
Jason Samse514b452009-09-25 14:51:22 -070058
Jason Samse12c1c52009-09-27 17:50:38 -070059 virtual void dumpLOGV(const char *prefix) const;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070060 virtual void serialize(OStream *stream) const = 0;
Alex Sakhartchoukb825f672010-06-04 10:06:50 -070061 virtual RsA3DClassID getClassId() const = 0;
Jason Samsf2649a92009-09-25 16:37:33 -070062
Jason Sams605048a2010-09-30 18:15:52 -070063 static bool isValid(const Context *rsc, const ObjectBase *obj);
64
Jason Sams225afd32010-10-21 14:06:55 -070065 // The async lock is taken during object creation in non-rs threads
66 // and object deletion in the rs thread.
67 static void asyncLock();
68 static void asyncUnlock();
Jason Sams2353ae32010-10-14 17:48:46 -070069
Jason Samsf2649a92009-09-25 16:37:33 -070070protected:
Jason Sams225afd32010-10-21 14:06:55 -070071 // Called inside the async lock for any object list management that is
72 // necessary in derived classes.
73 virtual void preDestroy() const;
74
Jason Sams81549542010-02-17 15:38:10 -080075 Context *mRSC;
Jason Sams225afd32010-10-21 14:06:55 -070076 virtual ~ObjectBase();
Jason Samsf2649a92009-09-25 16:37:33 -070077
Jason Sams326e0dd2009-05-22 14:03:28 -070078private:
Jason Sams2353ae32010-10-14 17:48:46 -070079 static pthread_mutex_t gObjectInitMutex;
80
Jason Samse514b452009-09-25 14:51:22 -070081 void add() const;
82 void remove() const;
83
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070084 String8 mName;
Jason Sams9397e302009-08-27 20:23:34 -070085 mutable int32_t mSysRefCount;
86 mutable int32_t mUserRefCount;
Jason Sams326e0dd2009-05-22 14:03:28 -070087
Jason Samse514b452009-09-25 14:51:22 -070088 mutable const ObjectBase * mPrev;
89 mutable const ObjectBase * mNext;
Jason Sams225afd32010-10-21 14:06:55 -070090
91#if RS_OBJECT_DEBUG
92 CallStack mStack;
93#endif
94
Jason Sams326e0dd2009-05-22 14:03:28 -070095};
96
Jason Sams9397e302009-08-27 20:23:34 -070097template<class T>
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080098class ObjectBaseRef {
Jason Sams326e0dd2009-05-22 14:03:28 -070099public:
100 ObjectBaseRef() {
101 mRef = NULL;
102 }
103
Jason Sams10308932009-06-09 12:15:30 -0700104 ObjectBaseRef(const ObjectBaseRef &ref) {
105 mRef = ref.get();
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700106 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -0700107 mRef->incSysRef();
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700108 }
Jason Sams10308932009-06-09 12:15:30 -0700109 }
110
111 ObjectBaseRef(T *ref) {
112 mRef = ref;
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700113 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -0700114 ref->incSysRef();
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700115 }
Jason Sams10308932009-06-09 12:15:30 -0700116 }
117
Jason Sams225afd32010-10-21 14:06:55 -0700118 ObjectBaseRef & operator= (const ObjectBaseRef &ref) {
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700119 if (&ref != this) {
120 set(ref);
121 }
122 return *this;
Jason Sams225afd32010-10-21 14:06:55 -0700123 }
124
Jason Sams326e0dd2009-05-22 14:03:28 -0700125 ~ObjectBaseRef() {
126 clear();
127 }
128
129 void set(T *ref) {
130 if (mRef != ref) {
131 clear();
132 mRef = ref;
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700133 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -0700134 ref->incSysRef();
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700135 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700136 }
137 }
138
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700139 void set(const ObjectBaseRef &ref) {
140 set(ref.mRef);
141 }
142
Jason Sams326e0dd2009-05-22 14:03:28 -0700143 void clear() {
144 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -0700145 mRef->decSysRef();
Jason Sams326e0dd2009-05-22 14:03:28 -0700146 }
147 mRef = NULL;
148 }
149
150 inline T * get() const {
151 return mRef;
152 }
153
Jason Sams9397e302009-08-27 20:23:34 -0700154 inline T * operator-> () const {
155 return mRef;
Jason Sams326e0dd2009-05-22 14:03:28 -0700156 }
157
158protected:
159 T * mRef;
Jason Sams326e0dd2009-05-22 14:03:28 -0700160};
161
Jason Sams326e0dd2009-05-22 14:03:28 -0700162}
163}
164
165#endif //ANDROID_RS_OBJECT_BASE_H
166