blob: 0d7421269965165a94a17c7cfd0192df61c5ad46 [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
19import android.util.Log;
20
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -070021/** @deprecated renderscript is deprecated in J
Jason Sams06d69de2010-11-09 17:11:40 -080022 * BaseObj is the base class for interfacing with native renderscript objects.
23 * It primarly contains code for tracking the native object ID and forcably
24 * disconecting the object from the native allocation for early cleanup.
25 *
Jason Sams36e612a2009-07-31 16:26:13 -070026 **/
Stephen Hinesef353dd2011-03-31 14:45:36 -070027public class BaseObj {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070028 BaseObj(int 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
Jason Sams06d69de2010-11-09 17:11:40 -080035 void setID(int id) {
36 if (mID != 0) {
37 throw new RSRuntimeException("Internal Error, reset of object ID.");
38 }
39 mID = id;
40 }
41
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -070042 /** @deprecated renderscript is deprecated in J
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 *
49 * @return int
50 */
Jason Samse07694b2012-04-03 15:36:36 -070051 int getID(RenderScript rs) {
52 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 Samsbf6ef8d72010-12-06 15:59:59 -080065 void checkValid() {
66 if (mID == 0) {
67 throw new RSIllegalArgumentException("Invalid object.");
68 }
69 }
70
Jason Sams06d69de2010-11-09 17:11:40 -080071 private int mID;
72 private boolean mDestroyed;
73 private String mName;
Jason Sams36e612a2009-07-31 16:26:13 -070074 RenderScript mRS;
75
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -070076 /** @deprecated renderscript is deprecated in J
Jason Sams27676fe2010-11-10 17:00:59 -080077 * setName assigns a name to an object. This object can later be looked up
78 * by this name. This name will also be retained if the object is written
79 * to an A3D file.
80 *
81 * @param name The name to assign to the object.
82 */
83 public void setName(String name) {
Stephen Hines84a97ca2011-03-15 21:05:54 -070084 if (name == null) {
85 throw new RSIllegalArgumentException(
86 "setName requires a string of non-zero length.");
87 }
Jason Sams27676fe2010-11-10 17:00:59 -080088 if(name.length() < 1) {
Stephen Hines84a97ca2011-03-15 21:05:54 -070089 throw new RSIllegalArgumentException(
90 "setName does not accept a zero length string.");
Jason Sams36e612a2009-07-31 16:26:13 -070091 }
92 if(mName != null) {
Stephen Hines84a97ca2011-03-15 21:05:54 -070093 throw new RSIllegalArgumentException(
94 "setName object already has a name.");
Jason Sams36e612a2009-07-31 16:26:13 -070095 }
96
97 try {
Jason Sams27676fe2010-11-10 17:00:59 -080098 byte[] bytes = name.getBytes("UTF-8");
Jason Sams36e612a2009-07-31 16:26:13 -070099 mRS.nAssignName(mID, bytes);
Jason Sams27676fe2010-11-10 17:00:59 -0800100 mName = name;
Jason Sams36e612a2009-07-31 16:26:13 -0700101 } catch (java.io.UnsupportedEncodingException e) {
102 throw new RuntimeException(e);
103 }
104 }
105
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700106 /** @deprecated renderscript is deprecated in J
Alex Sakhartchouk0400b072011-07-26 14:13:32 -0700107 * @return name of the renderscript object
108 */
109 public String getName() {
110 return mName;
111 }
112
Jason Samsc1d62102010-11-04 14:32:19 -0700113 protected void finalize() throws Throwable {
Jason Sams1bada8c2009-08-09 17:01:55 -0700114 if (!mDestroyed) {
Jason Samsa9e7a052009-09-25 14:51:22 -0700115 if(mID != 0 && mRS.isAlive()) {
Jason Samsd78be372010-08-17 19:28:29 -0700116 mRS.nObjDestroy(mID);
Jason Sams730ee652009-08-18 17:07:09 -0700117 }
Jason Samsa9e7a052009-09-25 14:51:22 -0700118 mRS = null;
Jason Sams730ee652009-08-18 17:07:09 -0700119 mID = 0;
120 mDestroyed = true;
Jason Sams5235cf32009-09-28 18:12:56 -0700121 //Log.v(RenderScript.LOG_TAG, getClass() +
122 // " auto finalizing object without having released the RS reference.");
Jason Sams36e612a2009-07-31 16:26:13 -0700123 }
124 super.finalize();
125 }
Jason Sams7ce033d2009-08-18 14:14:24 -0700126
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700127 /** @deprecated renderscript is deprecated in J
Stephen Hines84a97ca2011-03-15 21:05:54 -0700128 * destroy disconnects the object from the native object effectively
Jason Sams27676fe2010-11-10 17:00:59 -0800129 * rendering this java object dead. The primary use is to force immediate
Stephen Hines84a97ca2011-03-15 21:05:54 -0700130 * cleanup of resources when it is believed the GC will not respond quickly
Jason Sams27676fe2010-11-10 17:00:59 -0800131 * enough.
132 */
Jason Sams06d69de2010-11-09 17:11:40 -0800133 synchronized public void destroy() {
Jason Sams7ce033d2009-08-18 14:14:24 -0700134 if(mDestroyed) {
Jason Samsc1d62102010-11-04 14:32:19 -0700135 throw new RSInvalidStateException("Object already destroyed.");
Jason Sams7ce033d2009-08-18 14:14:24 -0700136 }
137 mDestroyed = true;
138 mRS.nObjDestroy(mID);
139 }
140
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700141 /** @deprecated renderscript is deprecated in J
Jason Sams27676fe2010-11-10 17:00:59 -0800142 * If an object came from an a3d file, java fields need to be
143 * created with objects from the native layer
144 */
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700145 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800146 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700147 mName = mRS.nGetName(getID(mRS));
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700148 }
149
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700150 /** @deprecated renderscript is deprecated in J
Stephen Hines705d2ea2011-06-09 10:11:54 -0700151 * Calculates the hash code value for a BaseObj.
152 *
153 * @return int
154 */
155 @Override
156 public int hashCode() {
157 return mID;
158 }
159
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700160 /** @deprecated renderscript is deprecated in J
Stephen Hines705d2ea2011-06-09 10:11:54 -0700161 * Compare the current BaseObj with another BaseObj for equality.
162 *
163 * @param obj The object to check equality with.
164 *
165 * @return boolean
166 */
167 @Override
168 public boolean equals(Object obj) {
169 // Early-out check to see if both BaseObjs are actually the same
170 if (this == obj)
171 return true;
172
173 if (getClass() != obj.getClass()) {
174 return false;
175 }
176
177 BaseObj b = (BaseObj) obj;
178 return mID == b.mID;
179 }
Jason Sams36e612a2009-07-31 16:26:13 -0700180}
181