blob: 7b5514b8a0d1f9525254069d2fcd816be33a236a [file] [log] [blame]
Jason Sams36e612a2009-07-31 16:26:13 -07001/*
2 * Copyright (C) 2008 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
17package android.renderscript;
18
Artur Satayev2ebb31c2020-01-08 12:24:36 +000019import android.compat.annotation.UnsupportedAppUsage;
20
Yang Nieb4dd082016-03-24 09:40:32 -070021import dalvik.system.CloseGuard;
Artur Satayev2ebb31c2020-01-08 12:24:36 +000022
Tim Murray504abb32014-01-07 11:13:56 -080023import java.util.concurrent.locks.ReentrantReadWriteLock;
Jason Sams36e612a2009-07-31 16:26:13 -070024
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070025/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070026 * BaseObj is the base class for all RenderScript objects owned by a RS context.
27 * It is responsible for lifetime management and resource tracking. This class
28 * should not be used by a user application.
Jason Sams06d69de2010-11-09 17:11:40 -080029 *
Jason Sams36e612a2009-07-31 16:26:13 -070030 **/
Stephen Hinesef353dd2011-03-31 14:45:36 -070031public class BaseObj {
Tim Murray7a629fa2013-11-19 12:45:54 -080032 BaseObj(long id, RenderScript rs) {
Jason Sams718cd1f2009-12-23 14:35:29 -080033 rs.validate();
Jason Sams36e612a2009-07-31 16:26:13 -070034 mRS = rs;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070035 mID = id;
Jason Sams1bada8c2009-08-09 17:01:55 -070036 mDestroyed = false;
Jason Sams36e612a2009-07-31 16:26:13 -070037 }
38
Ashok Bhat0e0c0882014-02-04 14:57:58 +000039 void setID(long id) {
Jason Sams06d69de2010-11-09 17:11:40 -080040 if (mID != 0) {
41 throw new RSRuntimeException("Internal Error, reset of object ID.");
42 }
43 mID = id;
44 }
45
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070046 /**
Jason Sams27676fe2010-11-10 17:00:59 -080047 * Lookup the native object ID for this object. Primarily used by the
48 * generated reflected code.
49 *
Jason Samse07694b2012-04-03 15:36:36 -070050 * @param rs Context to verify against internal context for
51 * match.
Jason Sams27676fe2010-11-10 17:00:59 -080052 *
Tim Murray7a629fa2013-11-19 12:45:54 -080053 * @return long
Jason Sams27676fe2010-11-10 17:00:59 -080054 */
Tim Murray7a629fa2013-11-19 12:45:54 -080055 long getID(RenderScript rs) {
Jason Samse07694b2012-04-03 15:36:36 -070056 mRS.validate();
Jason Sams7aa150c2010-09-21 14:47:22 -070057 if (mDestroyed) {
Jason Samsc1d62102010-11-04 14:32:19 -070058 throw new RSInvalidStateException("using a destroyed object.");
Jason Sams7aa150c2010-09-21 14:47:22 -070059 }
Jason Sams5476b452010-12-08 16:14:36 -080060 if (mID == 0) {
61 throw new RSRuntimeException("Internal error: Object id 0.");
62 }
Jason Samse07694b2012-04-03 15:36:36 -070063 if ((rs != null) && (rs != mRS)) {
64 throw new RSInvalidStateException("using object with mismatched context.");
65 }
Jason Sams36e612a2009-07-31 16:26:13 -070066 return mID;
67 }
68
Jason Samsbf6ef8d2010-12-06 15:59:59 -080069 void checkValid() {
70 if (mID == 0) {
71 throw new RSIllegalArgumentException("Invalid object.");
72 }
73 }
74
Tim Murray7a629fa2013-11-19 12:45:54 -080075 private long mID;
Yang Nieb4dd082016-03-24 09:40:32 -070076 final CloseGuard guard = CloseGuard.get();
Jason Sams06d69de2010-11-09 17:11:40 -080077 private boolean mDestroyed;
78 private String mName;
Mathew Inwood15324472018-08-06 11:18:49 +010079 @UnsupportedAppUsage
Jason Sams36e612a2009-07-31 16:26:13 -070080 RenderScript mRS;
81
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070082 /**
Jason Sams27676fe2010-11-10 17:00:59 -080083 * setName assigns a name to an object. This object can later be looked up
Tim Murrayc11e25c2013-04-09 11:01:01 -070084 * by this name.
Jason Sams27676fe2010-11-10 17:00:59 -080085 *
86 * @param name The name to assign to the object.
87 */
88 public void setName(String name) {
Stephen Hines84a97ca2011-03-15 21:05:54 -070089 if (name == null) {
90 throw new RSIllegalArgumentException(
91 "setName requires a string of non-zero length.");
92 }
Jason Sams27676fe2010-11-10 17:00:59 -080093 if(name.length() < 1) {
Stephen Hines84a97ca2011-03-15 21:05:54 -070094 throw new RSIllegalArgumentException(
95 "setName does not accept a zero length string.");
Jason Sams36e612a2009-07-31 16:26:13 -070096 }
97 if(mName != null) {
Stephen Hines84a97ca2011-03-15 21:05:54 -070098 throw new RSIllegalArgumentException(
99 "setName object already has a name.");
Jason Sams36e612a2009-07-31 16:26:13 -0700100 }
101
102 try {
Jason Sams27676fe2010-11-10 17:00:59 -0800103 byte[] bytes = name.getBytes("UTF-8");
Jason Sams36e612a2009-07-31 16:26:13 -0700104 mRS.nAssignName(mID, bytes);
Jason Sams27676fe2010-11-10 17:00:59 -0800105 mName = name;
Jason Sams36e612a2009-07-31 16:26:13 -0700106 } catch (java.io.UnsupportedEncodingException e) {
107 throw new RuntimeException(e);
108 }
109 }
110
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700111 /**
Alex Sakhartchouk0400b072011-07-26 14:13:32 -0700112 * @return name of the renderscript object
113 */
114 public String getName() {
115 return mName;
116 }
117
Tim Murray504abb32014-01-07 11:13:56 -0800118 private void helpDestroy() {
119 boolean shouldDestroy = false;
120 synchronized(this) {
121 if (!mDestroyed) {
122 shouldDestroy = true;
123 mDestroyed = true;
124 }
125 }
126
127 if (shouldDestroy) {
Yang Nieb4dd082016-03-24 09:40:32 -0700128 guard.close();
Tim Murray504abb32014-01-07 11:13:56 -0800129 // must include nObjDestroy in the critical section
130 ReentrantReadWriteLock.ReadLock rlock = mRS.mRWLock.readLock();
131 rlock.lock();
Tim Murray6d63c842014-02-12 11:16:17 -0800132 // AllocationAdapters are BaseObjs with an ID of 0 but should not be passed to nObjDestroy
133 if(mRS.isAlive() && mID != 0) {
Jason Samsd78be372010-08-17 19:28:29 -0700134 mRS.nObjDestroy(mID);
Jason Sams730ee652009-08-18 17:07:09 -0700135 }
Tim Murray504abb32014-01-07 11:13:56 -0800136 rlock.unlock();
Jason Samsa9e7a052009-09-25 14:51:22 -0700137 mRS = null;
Jason Sams730ee652009-08-18 17:07:09 -0700138 mID = 0;
Jason Sams36e612a2009-07-31 16:26:13 -0700139 }
Tim Murray504abb32014-01-07 11:13:56 -0800140 }
141
142 protected void finalize() throws Throwable {
Yang Nieb4dd082016-03-24 09:40:32 -0700143 try {
144 if (guard != null) {
145 guard.warnIfOpen();
146 }
147 helpDestroy();
148 } finally {
149 super.finalize();
150 }
Jason Sams36e612a2009-07-31 16:26:13 -0700151 }
Jason Sams7ce033d2009-08-18 14:14:24 -0700152
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700153 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700154 * Frees any native resources associated with this object. The
155 * primary use is to force immediate cleanup of resources when it is
156 * believed the GC will not respond quickly enough.
Jason Sams27676fe2010-11-10 17:00:59 -0800157 */
Tim Murray504abb32014-01-07 11:13:56 -0800158 public void destroy() {
Jason Sams7ce033d2009-08-18 14:14:24 -0700159 if(mDestroyed) {
Jason Samsc1d62102010-11-04 14:32:19 -0700160 throw new RSInvalidStateException("Object already destroyed.");
Jason Sams7ce033d2009-08-18 14:14:24 -0700161 }
Tim Murray504abb32014-01-07 11:13:56 -0800162 helpDestroy();
Jason Sams7ce033d2009-08-18 14:14:24 -0700163 }
164
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700165 /**
Jason Sams27676fe2010-11-10 17:00:59 -0800166 * If an object came from an a3d file, java fields need to be
167 * created with objects from the native layer
168 */
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700169 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800170 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700171 mName = mRS.nGetName(getID(mRS));
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700172 }
173
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700174 /**
Stephen Hines705d2ea2011-06-09 10:11:54 -0700175 * Calculates the hash code value for a BaseObj.
176 *
177 * @return int
178 */
179 @Override
180 public int hashCode() {
Tim Murray7a629fa2013-11-19 12:45:54 -0800181 return (int)((mID & 0xfffffff) ^ (mID >> 32));
Stephen Hines705d2ea2011-06-09 10:11:54 -0700182 }
183
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700184 /**
Stephen Hines705d2ea2011-06-09 10:11:54 -0700185 * Compare the current BaseObj with another BaseObj for equality.
186 *
187 * @param obj The object to check equality with.
188 *
189 * @return boolean
190 */
191 @Override
192 public boolean equals(Object obj) {
193 // Early-out check to see if both BaseObjs are actually the same
194 if (this == obj)
195 return true;
196
Tim Murray78214c952014-02-28 16:57:47 -0800197 if (obj == null) {
198 return false;
199 }
200
Stephen Hines705d2ea2011-06-09 10:11:54 -0700201 if (getClass() != obj.getClass()) {
202 return false;
203 }
204
205 BaseObj b = (BaseObj) obj;
206 return mID == b.mID;
207 }
Jason Sams36e612a2009-07-31 16:26:13 -0700208}
209