Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.renderscript; |
| 18 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 19 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 20 | * 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 Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 23 | * |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 24 | **/ |
Stephen Hines | ef353dd | 2011-03-31 14:45:36 -0700 | [diff] [blame] | 25 | public class BaseObj { |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 26 | BaseObj(long id, RenderScript rs) { |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 27 | rs.validate(); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 28 | mRS = rs; |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 29 | mID = id; |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 30 | mDestroyed = false; |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 33 | 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 Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 40 | /** |
Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 41 | * Lookup the native object ID for this object. Primarily used by the |
| 42 | * generated reflected code. |
| 43 | * |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 44 | * @param rs Context to verify against internal context for |
| 45 | * match. |
Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 46 | * |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 47 | * @return long |
Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 48 | */ |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 49 | long getID(RenderScript rs) { |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 50 | mRS.validate(); |
Jason Sams | 7aa150c | 2010-09-21 14:47:22 -0700 | [diff] [blame] | 51 | if (mDestroyed) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 52 | throw new RSInvalidStateException("using a destroyed object."); |
Jason Sams | 7aa150c | 2010-09-21 14:47:22 -0700 | [diff] [blame] | 53 | } |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 54 | if (mID == 0) { |
| 55 | throw new RSRuntimeException("Internal error: Object id 0."); |
| 56 | } |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 57 | if ((rs != null) && (rs != mRS)) { |
| 58 | throw new RSInvalidStateException("using object with mismatched context."); |
| 59 | } |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 60 | return mID; |
| 61 | } |
| 62 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 63 | void checkValid() { |
| 64 | if (mID == 0) { |
| 65 | throw new RSIllegalArgumentException("Invalid object."); |
| 66 | } |
| 67 | } |
| 68 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 69 | private long mID; |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 70 | private boolean mDestroyed; |
| 71 | private String mName; |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 72 | RenderScript mRS; |
| 73 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 74 | /** |
Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 75 | * setName assigns a name to an object. This object can later be looked up |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 76 | * by this name. |
Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 77 | * |
| 78 | * @param name The name to assign to the object. |
| 79 | */ |
| 80 | public void setName(String name) { |
Stephen Hines | 84a97ca | 2011-03-15 21:05:54 -0700 | [diff] [blame] | 81 | if (name == null) { |
| 82 | throw new RSIllegalArgumentException( |
| 83 | "setName requires a string of non-zero length."); |
| 84 | } |
Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 85 | if(name.length() < 1) { |
Stephen Hines | 84a97ca | 2011-03-15 21:05:54 -0700 | [diff] [blame] | 86 | throw new RSIllegalArgumentException( |
| 87 | "setName does not accept a zero length string."); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 88 | } |
| 89 | if(mName != null) { |
Stephen Hines | 84a97ca | 2011-03-15 21:05:54 -0700 | [diff] [blame] | 90 | throw new RSIllegalArgumentException( |
| 91 | "setName object already has a name."); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | try { |
Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 95 | byte[] bytes = name.getBytes("UTF-8"); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 96 | mRS.nAssignName(mID, bytes); |
Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 97 | mName = name; |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 98 | } catch (java.io.UnsupportedEncodingException e) { |
| 99 | throw new RuntimeException(e); |
| 100 | } |
| 101 | } |
| 102 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 103 | /** |
Alex Sakhartchouk | 0400b07 | 2011-07-26 14:13:32 -0700 | [diff] [blame] | 104 | * @return name of the renderscript object |
| 105 | */ |
| 106 | public String getName() { |
| 107 | return mName; |
| 108 | } |
| 109 | |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 110 | protected void finalize() throws Throwable { |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 111 | if (!mDestroyed) { |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 112 | if(mID != 0 && mRS.isAlive()) { |
Jason Sams | d78be37 | 2010-08-17 19:28:29 -0700 | [diff] [blame] | 113 | mRS.nObjDestroy(mID); |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 114 | } |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 115 | mRS = null; |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 116 | mID = 0; |
| 117 | mDestroyed = true; |
Jason Sams | 5235cf3 | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 118 | //Log.v(RenderScript.LOG_TAG, getClass() + |
| 119 | // " auto finalizing object without having released the RS reference."); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 120 | } |
| 121 | super.finalize(); |
| 122 | } |
Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 123 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 124 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 125 | * 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 Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 128 | */ |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 129 | synchronized public void destroy() { |
Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 130 | if(mDestroyed) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 131 | throw new RSInvalidStateException("Object already destroyed."); |
Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 132 | } |
| 133 | mDestroyed = true; |
| 134 | mRS.nObjDestroy(mID); |
| 135 | } |
| 136 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 137 | /** |
Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 138 | * If an object came from an a3d file, java fields need to be |
| 139 | * created with objects from the native layer |
| 140 | */ |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 141 | void updateFromNative() { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 142 | mRS.validate(); |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 143 | mName = mRS.nGetName(getID(mRS)); |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 146 | /** |
Stephen Hines | 705d2ea | 2011-06-09 10:11:54 -0700 | [diff] [blame] | 147 | * Calculates the hash code value for a BaseObj. |
| 148 | * |
| 149 | * @return int |
| 150 | */ |
| 151 | @Override |
| 152 | public int hashCode() { |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 153 | return (int)((mID & 0xfffffff) ^ (mID >> 32)); |
Stephen Hines | 705d2ea | 2011-06-09 10:11:54 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 156 | /** |
Stephen Hines | 705d2ea | 2011-06-09 10:11:54 -0700 | [diff] [blame] | 157 | * 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 Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 176 | } |
| 177 | |