blob: 60c012a9ea0a59922bfd07900e3795cce0b870cb [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
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070019/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070020 * BaseObj is the base class for all RenderScript objects owned by a RS context.
21 * It is responsible for lifetime management and resource tracking. This class
22 * should not be used by a user application.
Jason Sams06d69de2010-11-09 17:11:40 -080023 *
Jason Sams36e612a2009-07-31 16:26:13 -070024 **/
Stephen Hinesef353dd2011-03-31 14:45:36 -070025public class BaseObj {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070026 BaseObj(int id, RenderScript rs) {
Jason Sams718cd1f2009-12-23 14:35:29 -080027 rs.validate();
Jason Sams36e612a2009-07-31 16:26:13 -070028 mRS = rs;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070029 mID = id;
Jason Sams1bada8c2009-08-09 17:01:55 -070030 mDestroyed = false;
Jason Sams36e612a2009-07-31 16:26:13 -070031 }
32
Jason Sams06d69de2010-11-09 17:11:40 -080033 void setID(int id) {
34 if (mID != 0) {
35 throw new RSRuntimeException("Internal Error, reset of object ID.");
36 }
37 mID = id;
38 }
39
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070040 /**
Jason Sams27676fe2010-11-10 17:00:59 -080041 * Lookup the native object ID for this object. Primarily used by the
42 * generated reflected code.
43 *
Jason Samse07694b2012-04-03 15:36:36 -070044 * @param rs Context to verify against internal context for
45 * match.
Jason Sams27676fe2010-11-10 17:00:59 -080046 *
47 * @return int
48 */
Jason Samse07694b2012-04-03 15:36:36 -070049 int getID(RenderScript rs) {
50 mRS.validate();
Jason Sams7aa150c2010-09-21 14:47:22 -070051 if (mDestroyed) {
Jason Samsc1d62102010-11-04 14:32:19 -070052 throw new RSInvalidStateException("using a destroyed object.");
Jason Sams7aa150c2010-09-21 14:47:22 -070053 }
Jason Sams5476b452010-12-08 16:14:36 -080054 if (mID == 0) {
55 throw new RSRuntimeException("Internal error: Object id 0.");
56 }
Jason Samse07694b2012-04-03 15:36:36 -070057 if ((rs != null) && (rs != mRS)) {
58 throw new RSInvalidStateException("using object with mismatched context.");
59 }
Jason Sams36e612a2009-07-31 16:26:13 -070060 return mID;
61 }
62
Jason Samsbf6ef8d2010-12-06 15:59:59 -080063 void checkValid() {
64 if (mID == 0) {
65 throw new RSIllegalArgumentException("Invalid object.");
66 }
67 }
68
Jason Sams06d69de2010-11-09 17:11:40 -080069 private int mID;
70 private boolean mDestroyed;
71 private String mName;
Jason Sams36e612a2009-07-31 16:26:13 -070072 RenderScript mRS;
73
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070074 /**
Jason Sams27676fe2010-11-10 17:00:59 -080075 * setName assigns a name to an object. This object can later be looked up
Tim Murrayc11e25c2013-04-09 11:01:01 -070076 * by this name.
Jason Sams27676fe2010-11-10 17:00:59 -080077 *
78 * @param name The name to assign to the object.
79 */
80 public void setName(String name) {
Stephen Hines84a97ca2011-03-15 21:05:54 -070081 if (name == null) {
82 throw new RSIllegalArgumentException(
83 "setName requires a string of non-zero length.");
84 }
Jason Sams27676fe2010-11-10 17:00:59 -080085 if(name.length() < 1) {
Stephen Hines84a97ca2011-03-15 21:05:54 -070086 throw new RSIllegalArgumentException(
87 "setName does not accept a zero length string.");
Jason Sams36e612a2009-07-31 16:26:13 -070088 }
89 if(mName != null) {
Stephen Hines84a97ca2011-03-15 21:05:54 -070090 throw new RSIllegalArgumentException(
91 "setName object already has a name.");
Jason Sams36e612a2009-07-31 16:26:13 -070092 }
93
94 try {
Jason Sams27676fe2010-11-10 17:00:59 -080095 byte[] bytes = name.getBytes("UTF-8");
Jason Sams36e612a2009-07-31 16:26:13 -070096 mRS.nAssignName(mID, bytes);
Jason Sams27676fe2010-11-10 17:00:59 -080097 mName = name;
Jason Sams36e612a2009-07-31 16:26:13 -070098 } catch (java.io.UnsupportedEncodingException e) {
99 throw new RuntimeException(e);
100 }
101 }
102
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700103 /**
Alex Sakhartchouk0400b072011-07-26 14:13:32 -0700104 * @return name of the renderscript object
105 */
106 public String getName() {
107 return mName;
108 }
109
Jason Samsc1d62102010-11-04 14:32:19 -0700110 protected void finalize() throws Throwable {
Jason Sams1bada8c2009-08-09 17:01:55 -0700111 if (!mDestroyed) {
Jason Samsa9e7a052009-09-25 14:51:22 -0700112 if(mID != 0 && mRS.isAlive()) {
Jason Samsd78be372010-08-17 19:28:29 -0700113 mRS.nObjDestroy(mID);
Jason Sams730ee652009-08-18 17:07:09 -0700114 }
Jason Samsa9e7a052009-09-25 14:51:22 -0700115 mRS = null;
Jason Sams730ee652009-08-18 17:07:09 -0700116 mID = 0;
117 mDestroyed = true;
Jason Sams5235cf32009-09-28 18:12:56 -0700118 //Log.v(RenderScript.LOG_TAG, getClass() +
119 // " auto finalizing object without having released the RS reference.");
Jason Sams36e612a2009-07-31 16:26:13 -0700120 }
121 super.finalize();
122 }
Jason Sams7ce033d2009-08-18 14:14:24 -0700123
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700124 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700125 * Frees any native resources associated with this object. The
126 * primary use is to force immediate cleanup of resources when it is
127 * believed the GC will not respond quickly enough.
Jason Sams27676fe2010-11-10 17:00:59 -0800128 */
Jason Sams06d69de2010-11-09 17:11:40 -0800129 synchronized public void destroy() {
Jason Sams7ce033d2009-08-18 14:14:24 -0700130 if(mDestroyed) {
Jason Samsc1d62102010-11-04 14:32:19 -0700131 throw new RSInvalidStateException("Object already destroyed.");
Jason Sams7ce033d2009-08-18 14:14:24 -0700132 }
133 mDestroyed = true;
134 mRS.nObjDestroy(mID);
135 }
136
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700137 /**
Jason Sams27676fe2010-11-10 17:00:59 -0800138 * If an object came from an a3d file, java fields need to be
139 * created with objects from the native layer
140 */
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700141 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800142 mRS.validate();
Jason Samse07694b2012-04-03 15:36:36 -0700143 mName = mRS.nGetName(getID(mRS));
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700144 }
145
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700146 /**
Stephen Hines705d2ea2011-06-09 10:11:54 -0700147 * Calculates the hash code value for a BaseObj.
148 *
149 * @return int
150 */
151 @Override
152 public int hashCode() {
153 return mID;
154 }
155
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700156 /**
Stephen Hines705d2ea2011-06-09 10:11:54 -0700157 * Compare the current BaseObj with another BaseObj for equality.
158 *
159 * @param obj The object to check equality with.
160 *
161 * @return boolean
162 */
163 @Override
164 public boolean equals(Object obj) {
165 // Early-out check to see if both BaseObjs are actually the same
166 if (this == obj)
167 return true;
168
169 if (getClass() != obj.getClass()) {
170 return false;
171 }
172
173 BaseObj b = (BaseObj) obj;
174 return mID == b.mID;
175 }
Jason Sams36e612a2009-07-31 16:26:13 -0700176}
177