blob: 1372ab79e2642d56a868ee1d81a6fa6562920cad [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
Tim Murray504abb32014-01-07 11:13:56 -080019import java.util.concurrent.locks.ReentrantReadWriteLock;
Jason Sams36e612a2009-07-31 16:26:13 -070020
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070021/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070022 * BaseObj is the base class for all RenderScript objects owned by a RS context.
23 * It is responsible for lifetime management and resource tracking. This class
24 * should not be used by a user application.
Jason Sams06d69de2010-11-09 17:11:40 -080025 *
Jason Sams36e612a2009-07-31 16:26:13 -070026 **/
Stephen Hinesef353dd2011-03-31 14:45:36 -070027public class BaseObj {
Tim Murray7a629fa2013-11-19 12:45:54 -080028 BaseObj(long id, RenderScript rs) {
Jason Sams718cd1f2009-12-23 14:35:29 -080029 rs.validate();
Jason Sams36e612a2009-07-31 16:26:13 -070030 mRS = rs;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070031 mID = id;
Jason Sams1bada8c2009-08-09 17:01:55 -070032 mDestroyed = false;
Jason Sams36e612a2009-07-31 16:26:13 -070033 }
34
Ashok Bhat0e0c0882014-02-04 14:57:58 +000035 void setID(long id) {
Jason Sams06d69de2010-11-09 17:11:40 -080036 if (mID != 0) {
37 throw new RSRuntimeException("Internal Error, reset of object ID.");
38 }
39 mID = id;
40 }
41
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070042 /**
Jason Sams27676fe2010-11-10 17:00:59 -080043 * Lookup the native object ID for this object. Primarily used by the
44 * generated reflected code.
45 *
Jason Samse07694b2012-04-03 15:36:36 -070046 * @param rs Context to verify against internal context for
47 * match.
Jason Sams27676fe2010-11-10 17:00:59 -080048 *
Tim Murray7a629fa2013-11-19 12:45:54 -080049 * @return long
Jason Sams27676fe2010-11-10 17:00:59 -080050 */
Tim Murray7a629fa2013-11-19 12:45:54 -080051 long getID(RenderScript rs) {
Jason Samse07694b2012-04-03 15:36:36 -070052 mRS.validate();
Jason Sams7aa150c2010-09-21 14:47:22 -070053 if (mDestroyed) {
Jason Samsc1d62102010-11-04 14:32:19 -070054 throw new RSInvalidStateException("using a destroyed object.");
Jason Sams7aa150c2010-09-21 14:47:22 -070055 }
Jason Sams5476b452010-12-08 16:14:36 -080056 if (mID == 0) {
57 throw new RSRuntimeException("Internal error: Object id 0.");
58 }
Jason Samse07694b2012-04-03 15:36:36 -070059 if ((rs != null) && (rs != mRS)) {
60 throw new RSInvalidStateException("using object with mismatched context.");
61 }
Jason Sams36e612a2009-07-31 16:26:13 -070062 return mID;
63 }
64
Jason Samsbf6ef8d2010-12-06 15:59:59 -080065 void checkValid() {
66 if (mID == 0) {
67 throw new RSIllegalArgumentException("Invalid object.");
68 }
69 }
70
Tim Murray7a629fa2013-11-19 12:45:54 -080071 private long mID;
Jason Sams06d69de2010-11-09 17:11:40 -080072 private boolean mDestroyed;
73 private String mName;
Jason Sams36e612a2009-07-31 16:26:13 -070074 RenderScript mRS;
75
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070076 /**
Jason Sams27676fe2010-11-10 17:00:59 -080077 * setName assigns a name to an object. This object can later be looked up
Tim Murrayc11e25c2013-04-09 11:01:01 -070078 * by this name.
Jason Sams27676fe2010-11-10 17:00:59 -080079 *
80 * @param name The name to assign to the object.
81 */
82 public void setName(String name) {
Stephen Hines84a97ca2011-03-15 21:05:54 -070083 if (name == null) {
84 throw new RSIllegalArgumentException(
85 "setName requires a string of non-zero length.");
86 }
Jason Sams27676fe2010-11-10 17:00:59 -080087 if(name.length() < 1) {
Stephen Hines84a97ca2011-03-15 21:05:54 -070088 throw new RSIllegalArgumentException(
89 "setName does not accept a zero length string.");
Jason Sams36e612a2009-07-31 16:26:13 -070090 }
91 if(mName != null) {
Stephen Hines84a97ca2011-03-15 21:05:54 -070092 throw new RSIllegalArgumentException(
93 "setName object already has a name.");
Jason Sams36e612a2009-07-31 16:26:13 -070094 }
95
96 try {
Jason Sams27676fe2010-11-10 17:00:59 -080097 byte[] bytes = name.getBytes("UTF-8");
Jason Sams36e612a2009-07-31 16:26:13 -070098 mRS.nAssignName(mID, bytes);
Jason Sams27676fe2010-11-10 17:00:59 -080099 mName = name;
Jason Sams36e612a2009-07-31 16:26:13 -0700100 } catch (java.io.UnsupportedEncodingException e) {
101 throw new RuntimeException(e);
102 }
103 }
104
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700105 /**
Alex Sakhartchouk0400b072011-07-26 14:13:32 -0700106 * @return name of the renderscript object
107 */
108 public String getName() {
109 return mName;
110 }
111
Tim Murray504abb32014-01-07 11:13:56 -0800112 private void helpDestroy() {
113 boolean shouldDestroy = false;
114 synchronized(this) {
115 if (!mDestroyed) {
116 shouldDestroy = true;
117 mDestroyed = true;
118 }
119 }
120
121 if (shouldDestroy) {
122 // must include nObjDestroy in the critical section
123 ReentrantReadWriteLock.ReadLock rlock = mRS.mRWLock.readLock();
124 rlock.lock();
Tim Murray6d63c842014-02-12 11:16:17 -0800125 // AllocationAdapters are BaseObjs with an ID of 0 but should not be passed to nObjDestroy
126 if(mRS.isAlive() && mID != 0) {
Jason Samsd78be372010-08-17 19:28:29 -0700127 mRS.nObjDestroy(mID);
Jason Sams730ee652009-08-18 17:07:09 -0700128 }
Tim Murray504abb32014-01-07 11:13:56 -0800129 rlock.unlock();
Jason Samsa9e7a052009-09-25 14:51:22 -0700130 mRS = null;
Jason Sams730ee652009-08-18 17:07:09 -0700131 mID = 0;
Jason Sams36e612a2009-07-31 16:26:13 -0700132 }
Tim Murray504abb32014-01-07 11:13:56 -0800133 }
134
135 protected void finalize() throws Throwable {
136 helpDestroy();
Jason Sams36e612a2009-07-31 16:26:13 -0700137 super.finalize();
138 }
Jason Sams7ce033d2009-08-18 14:14:24 -0700139
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700140 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700141 * Frees any native resources associated with this object. The
142 * primary use is to force immediate cleanup of resources when it is
143 * believed the GC will not respond quickly enough.
Jason Sams27676fe2010-11-10 17:00:59 -0800144 */
Tim Murray504abb32014-01-07 11:13:56 -0800145 public void destroy() {
Jason Sams7ce033d2009-08-18 14:14:24 -0700146 if(mDestroyed) {
Jason Samsc1d62102010-11-04 14:32:19 -0700147 throw new RSInvalidStateException("Object already destroyed.");
Jason Sams7ce033d2009-08-18 14:14:24 -0700148 }
Tim Murray504abb32014-01-07 11:13:56 -0800149 helpDestroy();
Jason Sams7ce033d2009-08-18 14:14:24 -0700150 }
151
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700152 /**
Jason Sams27676fe2010-11-10 17:00:59 -0800153 * If an object came from an a3d file, java fields need to be
154 * created with objects from the native layer
155 */
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700156 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800157 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700158 mName = mRS.nGetName(getID(mRS));
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700159 }
160
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700161 /**
Stephen Hines705d2ea2011-06-09 10:11:54 -0700162 * Calculates the hash code value for a BaseObj.
163 *
164 * @return int
165 */
166 @Override
167 public int hashCode() {
Tim Murray7a629fa2013-11-19 12:45:54 -0800168 return (int)((mID & 0xfffffff) ^ (mID >> 32));
Stephen Hines705d2ea2011-06-09 10:11:54 -0700169 }
170
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700171 /**
Stephen Hines705d2ea2011-06-09 10:11:54 -0700172 * Compare the current BaseObj with another BaseObj for equality.
173 *
174 * @param obj The object to check equality with.
175 *
176 * @return boolean
177 */
178 @Override
179 public boolean equals(Object obj) {
180 // Early-out check to see if both BaseObjs are actually the same
181 if (this == obj)
182 return true;
183
Tim Murray78214c952014-02-28 16:57:47 -0800184 if (obj == null) {
185 return false;
186 }
187
Stephen Hines705d2ea2011-06-09 10:11:54 -0700188 if (getClass() != obj.getClass()) {
189 return false;
190 }
191
192 BaseObj b = (BaseObj) obj;
193 return mID == b.mID;
194 }
Jason Sams36e612a2009-07-31 16:26:13 -0700195}
196