rsObjectBase.h revision 225afd317e101a7be5fe02c0a86361146ea89f05
1/* 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#define RS_OBJECT_DEBUG 0 23 24#if RS_OBJECT_DEBUG 25 #include <utils/CallStack.h> 26#endif 27 28namespace android { 29namespace renderscript { 30 31class Context; 32class OStream; 33 34// An element is a group of Components that occupies one cell in a structure. 35class ObjectBase 36{ 37public: 38 ObjectBase(Context *rsc); 39 40 void incSysRef() const; 41 bool decSysRef() const; 42 43 void incUserRef() const; 44 bool decUserRef() const; 45 bool zeroUserRef() const; 46 47 static bool checkDelete(const ObjectBase *); 48 49 const char * getName() const { 50 return mName.string(); 51 } 52 void setName(const char *); 53 void setName(const char *, uint32_t len); 54 55 Context * getContext() const {return mRSC;} 56 57 static void zeroAllUserRef(Context *rsc); 58 static void dumpAll(Context *rsc); 59 60 virtual void dumpLOGV(const char *prefix) const; 61 virtual void serialize(OStream *stream) const = 0; 62 virtual RsA3DClassID getClassId() const = 0; 63 64 static bool isValid(const Context *rsc, const ObjectBase *obj); 65 66 // The async lock is taken during object creation in non-rs threads 67 // and object deletion in the rs thread. 68 static void asyncLock(); 69 static void asyncUnlock(); 70 71protected: 72 // Called inside the async lock for any object list management that is 73 // necessary in derived classes. 74 virtual void preDestroy() const; 75 76 Context *mRSC; 77 virtual ~ObjectBase(); 78 79private: 80 static pthread_mutex_t gObjectInitMutex; 81 82 void add() const; 83 void remove() const; 84 85 String8 mName; 86 mutable int32_t mSysRefCount; 87 mutable int32_t mUserRefCount; 88 89 mutable const ObjectBase * mPrev; 90 mutable const ObjectBase * mNext; 91 92#if RS_OBJECT_DEBUG 93 CallStack mStack; 94#endif 95 96}; 97 98template<class T> 99class ObjectBaseRef 100{ 101public: 102 ObjectBaseRef() { 103 mRef = NULL; 104 } 105 106 ObjectBaseRef(const ObjectBaseRef &ref) { 107 mRef = ref.get(); 108 if (mRef) { 109 mRef->incSysRef(); 110 } 111 } 112 113 ObjectBaseRef(T *ref) { 114 mRef = ref; 115 if (mRef) { 116 ref->incSysRef(); 117 } 118 } 119 120 ObjectBaseRef & operator= (const ObjectBaseRef &ref) { 121 return ObjectBaseRef(ref); 122 } 123 124 ~ObjectBaseRef() { 125 clear(); 126 } 127 128 void set(T *ref) { 129 if (mRef != ref) { 130 clear(); 131 mRef = ref; 132 if (mRef) { 133 ref->incSysRef(); 134 } 135 } 136 } 137 138 void set(const ObjectBaseRef &ref) { 139 set(ref.mRef); 140 } 141 142 void clear() { 143 if (mRef) { 144 mRef->decSysRef(); 145 } 146 mRef = NULL; 147 } 148 149 inline T * get() const { 150 return mRef; 151 } 152 153 inline T * operator-> () const { 154 return mRef; 155 } 156 157protected: 158 T * mRef; 159 160}; 161 162 163} 164} 165 166#endif //ANDROID_RS_OBJECT_BASE_H 167 168