blob: f95af1673730ebf847b1eff388a2fef1f14cbc24 [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
Yang Ni6484b6b2016-03-24 09:40:32 -070019import dalvik.system.CloseGuard;
Tim Murray504abb32014-01-07 11:13:56 -080020import java.util.concurrent.locks.ReentrantReadWriteLock;
Jason Sams36e612a2009-07-31 16:26:13 -070021
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070022/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070023 * BaseObj is the base class for all RenderScript objects owned by a RS context.
24 * It is responsible for lifetime management and resource tracking. This class
25 * should not be used by a user application.
Jason Sams06d69de2010-11-09 17:11:40 -080026 *
Jason Sams36e612a2009-07-31 16:26:13 -070027 **/
Stephen Hinesef353dd2011-03-31 14:45:36 -070028public class BaseObj {
Tim Murray7a629fa2013-11-19 12:45:54 -080029 BaseObj(long id, RenderScript rs) {
Jason Sams718cd1f2009-12-23 14:35:29 -080030 rs.validate();
Jason Sams36e612a2009-07-31 16:26:13 -070031 mRS = rs;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070032 mID = id;
Jason Sams1bada8c2009-08-09 17:01:55 -070033 mDestroyed = false;
Jason Sams36e612a2009-07-31 16:26:13 -070034 }
35
Ashok Bhat0e0c0882014-02-04 14:57:58 +000036 void setID(long id) {
Jason Sams06d69de2010-11-09 17:11:40 -080037 if (mID != 0) {
38 throw new RSRuntimeException("Internal Error, reset of object ID.");
39 }
40 mID = id;
41 }
42
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070043 /**
Jason Sams27676fe2010-11-10 17:00:59 -080044 * Lookup the native object ID for this object. Primarily used by the
45 * generated reflected code.
46 *
Jason Samse07694b2012-04-03 15:36:36 -070047 * @param rs Context to verify against internal context for
48 * match.
Jason Sams27676fe2010-11-10 17:00:59 -080049 *
Tim Murray7a629fa2013-11-19 12:45:54 -080050 * @return long
Jason Sams27676fe2010-11-10 17:00:59 -080051 */
Tim Murray7a629fa2013-11-19 12:45:54 -080052 long getID(RenderScript rs) {
Jason Samse07694b2012-04-03 15:36:36 -070053 mRS.validate();
Jason Sams7aa150c2010-09-21 14:47:22 -070054 if (mDestroyed) {
Jason Samsc1d62102010-11-04 14:32:19 -070055 throw new RSInvalidStateException("using a destroyed object.");
Jason Sams7aa150c2010-09-21 14:47:22 -070056 }
Jason Sams5476b452010-12-08 16:14:36 -080057 if (mID == 0) {
58 throw new RSRuntimeException("Internal error: Object id 0.");
59 }
Jason Samse07694b2012-04-03 15:36:36 -070060 if ((rs != null) && (rs != mRS)) {
61 throw new RSInvalidStateException("using object with mismatched context.");
62 }
Jason Sams36e612a2009-07-31 16:26:13 -070063 return mID;
64 }
65
Jason Samsbf6ef8d2010-12-06 15:59:59 -080066 void checkValid() {
67 if (mID == 0) {
68 throw new RSIllegalArgumentException("Invalid object.");
69 }
70 }
71
Tim Murray7a629fa2013-11-19 12:45:54 -080072 private long mID;
Yang Ni6484b6b2016-03-24 09:40:32 -070073 final CloseGuard guard = CloseGuard.get();
Jason Sams06d69de2010-11-09 17:11:40 -080074 private boolean mDestroyed;
75 private String mName;
Jason Sams36e612a2009-07-31 16:26:13 -070076 RenderScript mRS;
77
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070078 /**
Jason Sams27676fe2010-11-10 17:00:59 -080079 * setName assigns a name to an object. This object can later be looked up
Tim Murrayc11e25c2013-04-09 11:01:01 -070080 * by this name.
Jason Sams27676fe2010-11-10 17:00:59 -080081 *
82 * @param name The name to assign to the object.
83 */
84 public void setName(String name) {
Stephen Hines84a97ca2011-03-15 21:05:54 -070085 if (name == null) {
86 throw new RSIllegalArgumentException(
87 "setName requires a string of non-zero length.");
88 }
Jason Sams27676fe2010-11-10 17:00:59 -080089 if(name.length() < 1) {
Stephen Hines84a97ca2011-03-15 21:05:54 -070090 throw new RSIllegalArgumentException(
91 "setName does not accept a zero length string.");
Jason Sams36e612a2009-07-31 16:26:13 -070092 }
93 if(mName != null) {
Stephen Hines84a97ca2011-03-15 21:05:54 -070094 throw new RSIllegalArgumentException(
95 "setName object already has a name.");
Jason Sams36e612a2009-07-31 16:26:13 -070096 }
97
98 try {
Jason Sams27676fe2010-11-10 17:00:59 -080099 byte[] bytes = name.getBytes("UTF-8");
Jason Sams36e612a2009-07-31 16:26:13 -0700100 mRS.nAssignName(mID, bytes);
Jason Sams27676fe2010-11-10 17:00:59 -0800101 mName = name;
Jason Sams36e612a2009-07-31 16:26:13 -0700102 } catch (java.io.UnsupportedEncodingException e) {
103 throw new RuntimeException(e);
104 }
105 }
106
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700107 /**
Alex Sakhartchouk0400b072011-07-26 14:13:32 -0700108 * @return name of the renderscript object
109 */
110 public String getName() {
111 return mName;
112 }
113
Tim Murray504abb32014-01-07 11:13:56 -0800114 private void helpDestroy() {
115 boolean shouldDestroy = false;
116 synchronized(this) {
117 if (!mDestroyed) {
118 shouldDestroy = true;
119 mDestroyed = true;
120 }
121 }
122
123 if (shouldDestroy) {
Yang Ni6484b6b2016-03-24 09:40:32 -0700124 guard.close();
Tim Murray504abb32014-01-07 11:13:56 -0800125 // must include nObjDestroy in the critical section
126 ReentrantReadWriteLock.ReadLock rlock = mRS.mRWLock.readLock();
127 rlock.lock();
Tim Murray6d63c842014-02-12 11:16:17 -0800128 // AllocationAdapters are BaseObjs with an ID of 0 but should not be passed to nObjDestroy
129 if(mRS.isAlive() && mID != 0) {
Jason Samsd78be372010-08-17 19:28:29 -0700130 mRS.nObjDestroy(mID);
Jason Sams730ee652009-08-18 17:07:09 -0700131 }
Tim Murray504abb32014-01-07 11:13:56 -0800132 rlock.unlock();
Jason Samsa9e7a052009-09-25 14:51:22 -0700133 mRS = null;
Jason Sams730ee652009-08-18 17:07:09 -0700134 mID = 0;
Jason Sams36e612a2009-07-31 16:26:13 -0700135 }
Tim Murray504abb32014-01-07 11:13:56 -0800136 }
137
138 protected void finalize() throws Throwable {
Yang Ni6484b6b2016-03-24 09:40:32 -0700139 try {
140 if (guard != null) {
141 guard.warnIfOpen();
142 }
143 helpDestroy();
144 } finally {
145 super.finalize();
146 }
Jason Sams36e612a2009-07-31 16:26:13 -0700147 }
Jason Sams7ce033d2009-08-18 14:14:24 -0700148
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700149 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700150 * Frees any native resources associated with this object. The
151 * primary use is to force immediate cleanup of resources when it is
152 * believed the GC will not respond quickly enough.
Jason Sams27676fe2010-11-10 17:00:59 -0800153 */
Tim Murray504abb32014-01-07 11:13:56 -0800154 public void destroy() {
Jason Sams7ce033d2009-08-18 14:14:24 -0700155 if(mDestroyed) {
Jason Samsc1d62102010-11-04 14:32:19 -0700156 throw new RSInvalidStateException("Object already destroyed.");
Jason Sams7ce033d2009-08-18 14:14:24 -0700157 }
Tim Murray504abb32014-01-07 11:13:56 -0800158 helpDestroy();
Jason Sams7ce033d2009-08-18 14:14:24 -0700159 }
160
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700161 /**
Jason Sams27676fe2010-11-10 17:00:59 -0800162 * If an object came from an a3d file, java fields need to be
163 * created with objects from the native layer
164 */
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700165 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800166 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700167 mName = mRS.nGetName(getID(mRS));
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700168 }
169
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700170 /**
Stephen Hines705d2ea2011-06-09 10:11:54 -0700171 * Calculates the hash code value for a BaseObj.
172 *
173 * @return int
174 */
175 @Override
176 public int hashCode() {
Tim Murray7a629fa2013-11-19 12:45:54 -0800177 return (int)((mID & 0xfffffff) ^ (mID >> 32));
Stephen Hines705d2ea2011-06-09 10:11:54 -0700178 }
179
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700180 /**
Stephen Hines705d2ea2011-06-09 10:11:54 -0700181 * Compare the current BaseObj with another BaseObj for equality.
182 *
183 * @param obj The object to check equality with.
184 *
185 * @return boolean
186 */
187 @Override
188 public boolean equals(Object obj) {
189 // Early-out check to see if both BaseObjs are actually the same
190 if (this == obj)
191 return true;
192
Tim Murray78214c952014-02-28 16:57:47 -0800193 if (obj == null) {
194 return false;
195 }
196
Stephen Hines705d2ea2011-06-09 10:11:54 -0700197 if (getClass() != obj.getClass()) {
198 return false;
199 }
200
201 BaseObj b = (BaseObj) obj;
202 return mID == b.mID;
203 }
Jason Sams36e612a2009-07-31 16:26:13 -0700204}
205