blob: c7cfb0e2e8b019682a16ec50adfc211388a1fa19 [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
Jason Samsb38d5342010-10-21 14:06:55 -070022#define RS_OBJECT_DEBUG 0
23
Jason Sams31a7e422010-10-26 13:09:17 -070024#include <utils/CallStack.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070025
26namespace android {
27namespace renderscript {
28
Jason Samsa9e7a052009-09-25 14:51:22 -070029class Context;
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070030class OStream;
Jason Samsa9e7a052009-09-25 14:51:22 -070031
Jason Samsd19f10d2009-05-22 14:03:28 -070032// An element is a group of Components that occupies one cell in a structure.
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080033class ObjectBase {
Jason Samsd19f10d2009-05-22 14:03:28 -070034public:
Jason Samsa9e7a052009-09-25 14:51:22 -070035 ObjectBase(Context *rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -070036
Jason Sams07ae4062009-08-27 20:23:34 -070037 void incSysRef() const;
Jason Samsa9e7a052009-09-25 14:51:22 -070038 bool decSysRef() const;
Jason Sams07ae4062009-08-27 20:23:34 -070039
40 void incUserRef() const;
Jason Samsa9e7a052009-09-25 14:51:22 -070041 bool decUserRef() const;
42 bool zeroUserRef() const;
Jason Samsb38d5342010-10-21 14:06:55 -070043
44 static bool checkDelete(const ObjectBase *);
Jason Samsd19f10d2009-05-22 14:03:28 -070045
Jason Sams3eaa3382009-06-10 15:04:38 -070046 const char * getName() const {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070047 return mName.string();
Jason Sams3eaa3382009-06-10 15:04:38 -070048 }
49 void setName(const char *);
Jason Samsd5680f92009-06-10 18:39:40 -070050 void setName(const char *, uint32_t len);
Jason Sams3eaa3382009-06-10 15:04:38 -070051
Jason Samsa9e7a052009-09-25 14:51:22 -070052 Context * getContext() const {return mRSC;}
Jason Samsa9e7a052009-09-25 14:51:22 -070053
54 static void zeroAllUserRef(Context *rsc);
Jason Sams715333b2009-11-17 17:26:46 -080055 static void dumpAll(Context *rsc);
Jason Samsa9e7a052009-09-25 14:51:22 -070056
Jason Sams3c0dfba2009-09-27 17:50:38 -070057 virtual void dumpLOGV(const char *prefix) const;
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070058 virtual void serialize(OStream *stream) const = 0;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -070059 virtual RsA3DClassID getClassId() const = 0;
Jason Sams61f08d62009-09-25 16:37:33 -070060
Jason Samsf166d9b2010-09-30 18:15:52 -070061 static bool isValid(const Context *rsc, const ObjectBase *obj);
62
Jason Samsb38d5342010-10-21 14:06:55 -070063 // The async lock is taken during object creation in non-rs threads
64 // and object deletion in the rs thread.
65 static void asyncLock();
66 static void asyncUnlock();
Jason Sams3b9c52a2010-10-14 17:48:46 -070067
Jason Sams61f08d62009-09-25 16:37:33 -070068protected:
Jason Samsb38d5342010-10-21 14:06:55 -070069 // Called inside the async lock for any object list management that is
70 // necessary in derived classes.
71 virtual void preDestroy() const;
72
Jason Samse4c487a2010-02-17 15:38:10 -080073 Context *mRSC;
Jason Samsb38d5342010-10-21 14:06:55 -070074 virtual ~ObjectBase();
Jason Sams61f08d62009-09-25 16:37:33 -070075
Jason Samsd19f10d2009-05-22 14:03:28 -070076private:
Jason Sams3b9c52a2010-10-14 17:48:46 -070077 static pthread_mutex_t gObjectInitMutex;
78
Jason Samsa9e7a052009-09-25 14:51:22 -070079 void add() const;
80 void remove() const;
81
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070082 String8 mName;
Jason Sams07ae4062009-08-27 20:23:34 -070083 mutable int32_t mSysRefCount;
84 mutable int32_t mUserRefCount;
Jason Samsd19f10d2009-05-22 14:03:28 -070085
Jason Samsa9e7a052009-09-25 14:51:22 -070086 mutable const ObjectBase * mPrev;
87 mutable const ObjectBase * mNext;
Jason Samsb38d5342010-10-21 14:06:55 -070088
89#if RS_OBJECT_DEBUG
90 CallStack mStack;
91#endif
92
Jason Samsd19f10d2009-05-22 14:03:28 -070093};
94
Jason Sams07ae4062009-08-27 20:23:34 -070095template<class T>
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080096class ObjectBaseRef {
Jason Samsd19f10d2009-05-22 14:03:28 -070097public:
98 ObjectBaseRef() {
99 mRef = NULL;
100 }
101
Jason Samsda423d82009-06-09 12:15:30 -0700102 ObjectBaseRef(const ObjectBaseRef &ref) {
103 mRef = ref.get();
Jason Sams3eaa3382009-06-10 15:04:38 -0700104 if (mRef) {
Jason Sams07ae4062009-08-27 20:23:34 -0700105 mRef->incSysRef();
Jason Sams3eaa3382009-06-10 15:04:38 -0700106 }
Jason Samsda423d82009-06-09 12:15:30 -0700107 }
108
109 ObjectBaseRef(T *ref) {
110 mRef = ref;
Jason Sams3eaa3382009-06-10 15:04:38 -0700111 if (mRef) {
Jason Sams07ae4062009-08-27 20:23:34 -0700112 ref->incSysRef();
Jason Sams3eaa3382009-06-10 15:04:38 -0700113 }
Jason Samsda423d82009-06-09 12:15:30 -0700114 }
115
Jason Samsb38d5342010-10-21 14:06:55 -0700116 ObjectBaseRef & operator= (const ObjectBaseRef &ref) {
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700117 if (&ref != this) {
118 set(ref);
119 }
120 return *this;
Jason Samsb38d5342010-10-21 14:06:55 -0700121 }
122
Jason Samsd19f10d2009-05-22 14:03:28 -0700123 ~ObjectBaseRef() {
124 clear();
125 }
126
127 void set(T *ref) {
128 if (mRef != ref) {
129 clear();
130 mRef = ref;
Jason Sams3eaa3382009-06-10 15:04:38 -0700131 if (mRef) {
Jason Sams07ae4062009-08-27 20:23:34 -0700132 ref->incSysRef();
Jason Sams3eaa3382009-06-10 15:04:38 -0700133 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700134 }
135 }
136
Jason Sams3eaa3382009-06-10 15:04:38 -0700137 void set(const ObjectBaseRef &ref) {
138 set(ref.mRef);
139 }
140
Jason Samsd19f10d2009-05-22 14:03:28 -0700141 void clear() {
142 if (mRef) {
Jason Sams07ae4062009-08-27 20:23:34 -0700143 mRef->decSysRef();
Jason Samsd19f10d2009-05-22 14:03:28 -0700144 }
145 mRef = NULL;
146 }
147
148 inline T * get() const {
149 return mRef;
150 }
151
Jason Sams07ae4062009-08-27 20:23:34 -0700152 inline T * operator-> () const {
153 return mRef;
Jason Samsd19f10d2009-05-22 14:03:28 -0700154 }
155
156protected:
157 T * mRef;
Jason Samsd19f10d2009-05-22 14:03:28 -0700158};
159
Jason Samsd19f10d2009-05-22 14:03:28 -0700160}
161}
162
163#endif //ANDROID_RS_OBJECT_BASE_H
164