rsObjectBase.h revision 605048a9f9af925782f6c90bc1cdc5af59e337d8
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 23namespace android { 24namespace renderscript { 25 26class Context; 27class OStream; 28 29// An element is a group of Components that occupies one cell in a structure. 30class ObjectBase 31{ 32public: 33 ObjectBase(Context *rsc); 34 virtual ~ObjectBase(); 35 36 void incSysRef() const; 37 bool decSysRef() const; 38 39 void incUserRef() const; 40 bool decUserRef() const; 41 bool zeroUserRef() const; 42 43 const char * getName() const { 44 return mName.string(); 45 } 46 void setName(const char *); 47 void setName(const char *, uint32_t len); 48 49 Context * getContext() const {return mRSC;} 50 void setContext(Context *); 51 52 static void zeroAllUserRef(Context *rsc); 53 static void dumpAll(Context *rsc); 54 55 virtual void dumpLOGV(const char *prefix) const; 56 virtual void serialize(OStream *stream) const = 0; 57 virtual RsA3DClassID getClassId() const = 0; 58 59 static bool isValid(const Context *rsc, const ObjectBase *obj); 60 61protected: 62 const char *mAllocFile; 63 uint32_t mAllocLine; 64 Context *mRSC; 65 66private: 67 void add() const; 68 void remove() const; 69 70 bool checkDelete() const; 71 72 String8 mName; 73 mutable int32_t mSysRefCount; 74 mutable int32_t mUserRefCount; 75 76 mutable const ObjectBase * mPrev; 77 mutable const ObjectBase * mNext; 78}; 79 80template<class T> 81class ObjectBaseRef 82{ 83public: 84 ObjectBaseRef() { 85 mRef = NULL; 86 } 87 88 ObjectBaseRef(const ObjectBaseRef &ref) { 89 mRef = ref.get(); 90 if (mRef) { 91 mRef->incSysRef(); 92 } 93 } 94 95 ObjectBaseRef(T *ref) { 96 mRef = ref; 97 if (mRef) { 98 ref->incSysRef(); 99 } 100 } 101 102 ~ObjectBaseRef() { 103 clear(); 104 } 105 106 void set(T *ref) { 107 if (mRef != ref) { 108 clear(); 109 mRef = ref; 110 if (mRef) { 111 ref->incSysRef(); 112 } 113 } 114 } 115 116 void set(const ObjectBaseRef &ref) { 117 set(ref.mRef); 118 } 119 120 void clear() { 121 if (mRef) { 122 mRef->decSysRef(); 123 } 124 mRef = NULL; 125 } 126 127 inline T * get() const { 128 return mRef; 129 } 130 131 inline T * operator-> () const { 132 return mRef; 133 } 134 135protected: 136 T * mRef; 137 138}; 139 140 141} 142} 143 144#endif //ANDROID_RS_OBJECT_BASE_H 145 146