| Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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" | 
| Alex Sakhartchouk | e23d239 | 2012-03-09 09:24:39 -0800 | [diff] [blame] | 21 | #include "rsDefines.h" | 
| Jason Sams | 84bf95d | 2015-04-09 14:36:37 -0700 | [diff] [blame] | 22 | #include "rsInternalDefines.h" | 
| Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 23 |  | 
|  | 24 | namespace android { | 
|  | 25 | namespace renderscript { | 
|  | 26 |  | 
| Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 27 | class Context; | 
| Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 28 | class OStream; | 
| Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 29 |  | 
| Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 30 | // An element is a group of Components that occupies one cell in a structure. | 
| Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 31 | class ObjectBase { | 
| Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 32 | public: | 
| Jason Sams | 84bf95d | 2015-04-09 14:36:37 -0700 | [diff] [blame] | 33 | static const bool gDebugStacks = false; | 
|  | 34 | static const bool gDebugReferences = false; | 
|  | 35 | static const bool gDebugLeaks = false; | 
|  | 36 | static const bool gDebugLifetime = false; | 
|  | 37 |  | 
| Chih-Hung Hsieh | 10ab8bb | 2016-07-01 12:20:20 -0700 | [diff] [blame] | 38 | ObjectBase(Context *rsc);  // NOLINT, implicit | 
| Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 39 |  | 
| Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 40 | void incSysRef() const; | 
| Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 41 | bool decSysRef() const; | 
| Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 42 |  | 
|  | 43 | void incUserRef() const; | 
| Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 44 | bool decUserRef() const; | 
|  | 45 | bool zeroUserRef() const; | 
| Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 46 |  | 
|  | 47 | static bool checkDelete(const ObjectBase *); | 
| Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 48 |  | 
| Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 49 | const char * getName() const { | 
| Stephen Hines | b0934b6 | 2013-07-03 17:27:38 -0700 | [diff] [blame] | 50 | return mName; | 
| Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 51 | } | 
| Jason Sams | 48ecf6a | 2013-07-09 15:35:29 -0700 | [diff] [blame] | 52 | void assignName(const char *s) {mName = s;} | 
| Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 53 | void setName(const char *); | 
| Jason Sams | a4a54e4 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 54 | void setName(const char *, uint32_t len); | 
| Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 55 |  | 
| Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 56 | Context * getContext() const {return mRSC;} | 
| Jason Sams | c7cec1e | 2011-08-18 18:01:33 -0700 | [diff] [blame] | 57 | virtual bool freeChildren(); | 
| Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 58 |  | 
|  | 59 | static void zeroAllUserRef(Context *rsc); | 
| Jason Sams | c7cec1e | 2011-08-18 18:01:33 -0700 | [diff] [blame] | 60 | static void freeAllChildren(Context *rsc); | 
| Jason Sams | c21cf40 | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 61 | static void dumpAll(Context *rsc); | 
| Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 62 |  | 
| Jason Sams | e12c1c5 | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 63 | virtual void dumpLOGV(const char *prefix) const; | 
| Jason Sams | e3150cf | 2012-07-24 18:10:20 -0700 | [diff] [blame] | 64 | virtual void serialize(Context *rsc, OStream *stream) const = 0; | 
| Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 65 | virtual RsA3DClassID getClassId() const = 0; | 
| Jason Sams | f2649a9 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 66 |  | 
| Jason Sams | 605048a | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 67 | static bool isValid(const Context *rsc, const ObjectBase *obj); | 
|  | 68 |  | 
| Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 69 | // The async lock is taken during object creation in non-rs threads | 
|  | 70 | // and object deletion in the rs thread. | 
|  | 71 | static void asyncLock(); | 
|  | 72 | static void asyncUnlock(); | 
| Jason Sams | 2353ae3 | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 73 |  | 
| Jason Sams | a36c50a | 2014-06-17 12:06:06 -0700 | [diff] [blame] | 74 | virtual void callUpdateCacheObject(const Context *rsc, void *dstObj) const; | 
|  | 75 |  | 
| Jason Sams | f2649a9 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 76 | protected: | 
| Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 77 | // Called inside the async lock for any object list management that is | 
|  | 78 | // necessary in derived classes. | 
|  | 79 | virtual void preDestroy() const; | 
|  | 80 |  | 
| Jason Sams | 8154954 | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 81 | Context *mRSC; | 
| Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 82 | virtual ~ObjectBase(); | 
| Jason Sams | f2649a9 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 83 |  | 
| Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 84 | private: | 
| Jason Sams | 2353ae3 | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 85 | static pthread_mutex_t gObjectInitMutex; | 
|  | 86 |  | 
| Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 87 | void add() const; | 
|  | 88 | void remove() const; | 
|  | 89 |  | 
| Stephen Hines | b0934b6 | 2013-07-03 17:27:38 -0700 | [diff] [blame] | 90 | const char* mName; | 
| Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 91 | mutable int32_t mSysRefCount; | 
|  | 92 | mutable int32_t mUserRefCount; | 
| Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 93 |  | 
| Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 94 | mutable const ObjectBase * mPrev; | 
|  | 95 | mutable const ObjectBase * mNext; | 
| Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 96 |  | 
| Jason Sams | 84bf95d | 2015-04-09 14:36:37 -0700 | [diff] [blame] | 97 | class DebugHelper *mDH; | 
| Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 98 | }; | 
|  | 99 |  | 
| Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 100 | template<class T> | 
| Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 101 | class ObjectBaseRef { | 
| Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 102 | public: | 
|  | 103 | ObjectBaseRef() { | 
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 104 | mRef = nullptr; | 
| Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 105 | } | 
|  | 106 |  | 
| Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 107 | ObjectBaseRef(const ObjectBaseRef &ref) { | 
|  | 108 | mRef = ref.get(); | 
| Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 109 | if (mRef) { | 
| Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 110 | mRef->incSysRef(); | 
| Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 111 | } | 
| Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 112 | } | 
|  | 113 |  | 
| Chih-Hung Hsieh | 10ab8bb | 2016-07-01 12:20:20 -0700 | [diff] [blame] | 114 | ObjectBaseRef(T *ref) {  // NOLINT, implicit | 
| Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 115 | mRef = ref; | 
| Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 116 | if (mRef) { | 
| Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 117 | ref->incSysRef(); | 
| Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 118 | } | 
| Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 119 | } | 
|  | 120 |  | 
| Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 121 | ObjectBaseRef & operator= (const ObjectBaseRef &ref) { | 
| Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 122 | if (&ref != this) { | 
|  | 123 | set(ref); | 
|  | 124 | } | 
|  | 125 | return *this; | 
| Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 126 | } | 
|  | 127 |  | 
| Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 128 | ~ObjectBaseRef() { | 
|  | 129 | clear(); | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | void set(T *ref) { | 
|  | 133 | if (mRef != ref) { | 
|  | 134 | clear(); | 
|  | 135 | mRef = ref; | 
| Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 136 | if (mRef) { | 
| Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 137 | ref->incSysRef(); | 
| Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 138 | } | 
| Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 139 | } | 
|  | 140 | } | 
|  | 141 |  | 
| Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 142 | void set(const ObjectBaseRef &ref) { | 
|  | 143 | set(ref.mRef); | 
|  | 144 | } | 
|  | 145 |  | 
| Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 146 | void clear() { | 
|  | 147 | if (mRef) { | 
| Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 148 | mRef->decSysRef(); | 
| Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 149 | } | 
| Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 150 | mRef = nullptr; | 
| Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 151 | } | 
|  | 152 |  | 
|  | 153 | inline T * get() const { | 
|  | 154 | return mRef; | 
|  | 155 | } | 
|  | 156 |  | 
| Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 157 | inline T * operator-> () const { | 
|  | 158 | return mRef; | 
| Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 159 | } | 
|  | 160 |  | 
|  | 161 | protected: | 
|  | 162 | T * mRef; | 
| Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 163 | }; | 
|  | 164 |  | 
| Rahul Chaudhry | 7974fc0 | 2017-02-09 12:33:28 -0800 | [diff] [blame] | 165 | } // namespace renderscript | 
|  | 166 | } // namespace android | 
| Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 167 |  | 
|  | 168 | #endif //ANDROID_RS_OBJECT_BASE_H | 
|  | 169 |  |