blob: d937ecffb3044cb24d00e452a4efba925016a948 [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"
Alex Sakhartchouke23d2392012-03-09 09:24:39 -080021#include "rsDefines.h"
Jason Sams84bf95d2015-04-09 14:36:37 -070022#include "rsInternalDefines.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070023
24namespace android {
25namespace renderscript {
26
Jason Samse514b452009-09-25 14:51:22 -070027class Context;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070028class OStream;
Jason Samse514b452009-09-25 14:51:22 -070029
Jason Sams326e0dd2009-05-22 14:03:28 -070030// An element is a group of Components that occupies one cell in a structure.
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080031class ObjectBase {
Jason Sams326e0dd2009-05-22 14:03:28 -070032public:
Jason Sams84bf95d2015-04-09 14:36:37 -070033 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 Hsieh10ab8bb2016-07-01 12:20:20 -070038 ObjectBase(Context *rsc); // NOLINT, implicit
Jason Sams326e0dd2009-05-22 14:03:28 -070039
Jason Sams9397e302009-08-27 20:23:34 -070040 void incSysRef() const;
Jason Samse514b452009-09-25 14:51:22 -070041 bool decSysRef() const;
Jason Sams9397e302009-08-27 20:23:34 -070042
43 void incUserRef() const;
Jason Samse514b452009-09-25 14:51:22 -070044 bool decUserRef() const;
45 bool zeroUserRef() const;
Jason Sams225afd32010-10-21 14:06:55 -070046
47 static bool checkDelete(const ObjectBase *);
Jason Sams326e0dd2009-05-22 14:03:28 -070048
Jason Samsa0a1b6f2009-06-10 15:04:38 -070049 const char * getName() const {
Stephen Hinesb0934b62013-07-03 17:27:38 -070050 return mName;
Jason Samsa0a1b6f2009-06-10 15:04:38 -070051 }
Jason Sams48ecf6a2013-07-09 15:35:29 -070052 void assignName(const char *s) {mName = s;}
Jason Samsa0a1b6f2009-06-10 15:04:38 -070053 void setName(const char *);
Jason Samsa4a54e42009-06-10 18:39:40 -070054 void setName(const char *, uint32_t len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -070055
Jason Samse514b452009-09-25 14:51:22 -070056 Context * getContext() const {return mRSC;}
Jason Samsc7cec1e2011-08-18 18:01:33 -070057 virtual bool freeChildren();
Jason Samse514b452009-09-25 14:51:22 -070058
59 static void zeroAllUserRef(Context *rsc);
Jason Samsc7cec1e2011-08-18 18:01:33 -070060 static void freeAllChildren(Context *rsc);
Jason Samsc21cf402009-11-17 17:26:46 -080061 static void dumpAll(Context *rsc);
Jason Samse514b452009-09-25 14:51:22 -070062
Jason Samse12c1c52009-09-27 17:50:38 -070063 virtual void dumpLOGV(const char *prefix) const;
Jason Samse3150cf2012-07-24 18:10:20 -070064 virtual void serialize(Context *rsc, OStream *stream) const = 0;
Alex Sakhartchoukb825f672010-06-04 10:06:50 -070065 virtual RsA3DClassID getClassId() const = 0;
Jason Samsf2649a92009-09-25 16:37:33 -070066
Jason Sams605048a2010-09-30 18:15:52 -070067 static bool isValid(const Context *rsc, const ObjectBase *obj);
68
Jason Sams225afd32010-10-21 14:06:55 -070069 // 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 Sams2353ae32010-10-14 17:48:46 -070073
Jason Samsa36c50a2014-06-17 12:06:06 -070074 virtual void callUpdateCacheObject(const Context *rsc, void *dstObj) const;
75
Jason Samsf2649a92009-09-25 16:37:33 -070076protected:
Jason Sams225afd32010-10-21 14:06:55 -070077 // Called inside the async lock for any object list management that is
78 // necessary in derived classes.
79 virtual void preDestroy() const;
80
Jason Sams81549542010-02-17 15:38:10 -080081 Context *mRSC;
Jason Sams225afd32010-10-21 14:06:55 -070082 virtual ~ObjectBase();
Jason Samsf2649a92009-09-25 16:37:33 -070083
Jason Sams326e0dd2009-05-22 14:03:28 -070084private:
Jason Sams2353ae32010-10-14 17:48:46 -070085 static pthread_mutex_t gObjectInitMutex;
86
Jason Samse514b452009-09-25 14:51:22 -070087 void add() const;
88 void remove() const;
89
Stephen Hinesb0934b62013-07-03 17:27:38 -070090 const char* mName;
Jason Sams9397e302009-08-27 20:23:34 -070091 mutable int32_t mSysRefCount;
92 mutable int32_t mUserRefCount;
Jason Sams326e0dd2009-05-22 14:03:28 -070093
Jason Samse514b452009-09-25 14:51:22 -070094 mutable const ObjectBase * mPrev;
95 mutable const ObjectBase * mNext;
Jason Sams225afd32010-10-21 14:06:55 -070096
Jason Sams84bf95d2015-04-09 14:36:37 -070097 class DebugHelper *mDH;
Jason Sams326e0dd2009-05-22 14:03:28 -070098};
99
Jason Sams9397e302009-08-27 20:23:34 -0700100template<class T>
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800101class ObjectBaseRef {
Jason Sams326e0dd2009-05-22 14:03:28 -0700102public:
103 ObjectBaseRef() {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700104 mRef = nullptr;
Jason Sams326e0dd2009-05-22 14:03:28 -0700105 }
106
Jason Sams10308932009-06-09 12:15:30 -0700107 ObjectBaseRef(const ObjectBaseRef &ref) {
108 mRef = ref.get();
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700109 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -0700110 mRef->incSysRef();
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700111 }
Jason Sams10308932009-06-09 12:15:30 -0700112 }
113
Chih-Hung Hsieh10ab8bb2016-07-01 12:20:20 -0700114 ObjectBaseRef(T *ref) { // NOLINT, implicit
Jason Sams10308932009-06-09 12:15:30 -0700115 mRef = ref;
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700116 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -0700117 ref->incSysRef();
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700118 }
Jason Sams10308932009-06-09 12:15:30 -0700119 }
120
Jason Sams225afd32010-10-21 14:06:55 -0700121 ObjectBaseRef & operator= (const ObjectBaseRef &ref) {
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700122 if (&ref != this) {
123 set(ref);
124 }
125 return *this;
Jason Sams225afd32010-10-21 14:06:55 -0700126 }
127
Jason Sams326e0dd2009-05-22 14:03:28 -0700128 ~ObjectBaseRef() {
129 clear();
130 }
131
132 void set(T *ref) {
133 if (mRef != ref) {
134 clear();
135 mRef = ref;
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700136 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -0700137 ref->incSysRef();
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700138 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700139 }
140 }
141
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700142 void set(const ObjectBaseRef &ref) {
143 set(ref.mRef);
144 }
145
Jason Sams326e0dd2009-05-22 14:03:28 -0700146 void clear() {
147 if (mRef) {
Jason Sams9397e302009-08-27 20:23:34 -0700148 mRef->decSysRef();
Jason Sams326e0dd2009-05-22 14:03:28 -0700149 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700150 mRef = nullptr;
Jason Sams326e0dd2009-05-22 14:03:28 -0700151 }
152
153 inline T * get() const {
154 return mRef;
155 }
156
Jason Sams9397e302009-08-27 20:23:34 -0700157 inline T * operator-> () const {
158 return mRef;
Jason Sams326e0dd2009-05-22 14:03:28 -0700159 }
160
161protected:
162 T * mRef;
Jason Sams326e0dd2009-05-22 14:03:28 -0700163};
164
Rahul Chaudhry7974fc02017-02-09 12:33:28 -0800165} // namespace renderscript
166} // namespace android
Jason Sams326e0dd2009-05-22 14:03:28 -0700167
168#endif //ANDROID_RS_OBJECT_BASE_H
169