| 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 |  | 
| Artur Satayev | 53ada2a | 2019-12-10 17:47:56 +0000 | [diff] [blame] | 19 | import android.compat.annotation.UnsupportedAppUsage; | 
 | 20 |  | 
| Yang Ni | eb4dd08 | 2016-03-24 09:40:32 -0700 | [diff] [blame] | 21 | import dalvik.system.CloseGuard; | 
| Artur Satayev | 53ada2a | 2019-12-10 17:47:56 +0000 | [diff] [blame] | 22 |  | 
| Tim Murray | 504abb3 | 2014-01-07 11:13:56 -0800 | [diff] [blame] | 23 | import java.util.concurrent.locks.ReentrantReadWriteLock; | 
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 24 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 25 | /** | 
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 26 |  * BaseObj is the base class for all RenderScript objects owned by a RS context. | 
 | 27 |  * It is responsible for lifetime management and resource tracking. This class | 
 | 28 |  * should not be used by a user application. | 
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 29 |  * | 
| Xusong Wang | 1f8dc65 | 2021-01-05 10:09:52 -0800 | [diff] [blame] | 30 |  * @deprecated Renderscript has been deprecated in API level 31. Please refer to the <a | 
 | 31 |  * href="https://developer.android.com/guide/topics/renderscript/migration-guide">migration | 
 | 32 |  * guide</a> for the proposed alternatives. | 
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 33 |  **/ | 
| Xusong Wang | 1f8dc65 | 2021-01-05 10:09:52 -0800 | [diff] [blame] | 34 | @Deprecated | 
| Stephen Hines | ef353dd | 2011-03-31 14:45:36 -0700 | [diff] [blame] | 35 | public class BaseObj { | 
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 36 |     BaseObj(long id, RenderScript rs) { | 
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 37 |         rs.validate(); | 
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 38 |         mRS = rs; | 
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 39 |         mID = id; | 
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 40 |         mDestroyed = false; | 
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 41 |     } | 
 | 42 |  | 
| Ashok Bhat | 0e0c088 | 2014-02-04 14:57:58 +0000 | [diff] [blame] | 43 |     void setID(long id) { | 
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 44 |         if (mID != 0) { | 
 | 45 |             throw new RSRuntimeException("Internal Error, reset of object ID."); | 
 | 46 |         } | 
 | 47 |         mID = id; | 
 | 48 |     } | 
 | 49 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 50 |     /** | 
| Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 51 |      * Lookup the native object ID for this object.  Primarily used by the | 
 | 52 |      * generated reflected code. | 
 | 53 |      * | 
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 54 |      * @param rs Context to verify against internal context for | 
 | 55 |      *           match. | 
| Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 56 |      * | 
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 57 |      * @return long | 
| Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 58 |      */ | 
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 59 |     long getID(RenderScript rs) { | 
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 60 |         mRS.validate(); | 
| Jason Sams | 7aa150c | 2010-09-21 14:47:22 -0700 | [diff] [blame] | 61 |         if (mDestroyed) { | 
| Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 62 |             throw new RSInvalidStateException("using a destroyed object."); | 
| Jason Sams | 7aa150c | 2010-09-21 14:47:22 -0700 | [diff] [blame] | 63 |         } | 
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 64 |         if (mID == 0) { | 
 | 65 |             throw new RSRuntimeException("Internal error: Object id 0."); | 
 | 66 |         } | 
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 67 |         if ((rs != null) && (rs != mRS)) { | 
 | 68 |             throw new RSInvalidStateException("using object with mismatched context."); | 
 | 69 |         } | 
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 70 |         return mID; | 
 | 71 |     } | 
 | 72 |  | 
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 73 |     void checkValid() { | 
 | 74 |         if (mID == 0) { | 
 | 75 |             throw new RSIllegalArgumentException("Invalid object."); | 
 | 76 |         } | 
 | 77 |     } | 
 | 78 |  | 
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 79 |     private long mID; | 
| Yang Ni | eb4dd08 | 2016-03-24 09:40:32 -0700 | [diff] [blame] | 80 |     final CloseGuard guard = CloseGuard.get(); | 
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 81 |     private boolean mDestroyed; | 
 | 82 |     private String mName; | 
| Mathew Inwood | 1532447 | 2018-08-06 11:18:49 +0100 | [diff] [blame] | 83 |     @UnsupportedAppUsage | 
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 84 |     RenderScript mRS; | 
 | 85 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 86 |     /** | 
| Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 87 |      * 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] | 88 |      * by this name. | 
| Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 89 |      * | 
 | 90 |      * @param name The name to assign to the object. | 
 | 91 |      */ | 
 | 92 |     public void setName(String name) { | 
| Stephen Hines | 84a97ca | 2011-03-15 21:05:54 -0700 | [diff] [blame] | 93 |         if (name == null) { | 
 | 94 |             throw new RSIllegalArgumentException( | 
 | 95 |                 "setName requires a string of non-zero length."); | 
 | 96 |         } | 
| Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 97 |         if(name.length() < 1) { | 
| Stephen Hines | 84a97ca | 2011-03-15 21:05:54 -0700 | [diff] [blame] | 98 |             throw new RSIllegalArgumentException( | 
 | 99 |                 "setName does not accept a zero length string."); | 
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 100 |         } | 
 | 101 |         if(mName != null) { | 
| Stephen Hines | 84a97ca | 2011-03-15 21:05:54 -0700 | [diff] [blame] | 102 |             throw new RSIllegalArgumentException( | 
 | 103 |                 "setName object already has a name."); | 
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 104 |         } | 
 | 105 |  | 
 | 106 |         try { | 
| Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 107 |             byte[] bytes = name.getBytes("UTF-8"); | 
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 108 |             mRS.nAssignName(mID, bytes); | 
| Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 109 |             mName = name; | 
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 110 |         } catch (java.io.UnsupportedEncodingException e) { | 
 | 111 |             throw new RuntimeException(e); | 
 | 112 |         } | 
 | 113 |     } | 
 | 114 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 115 |     /** | 
| Alex Sakhartchouk | 0400b07 | 2011-07-26 14:13:32 -0700 | [diff] [blame] | 116 |      * @return name of the renderscript object | 
 | 117 |      */ | 
 | 118 |     public String getName() { | 
 | 119 |         return mName; | 
 | 120 |     } | 
 | 121 |  | 
| Tim Murray | 504abb3 | 2014-01-07 11:13:56 -0800 | [diff] [blame] | 122 |     private void helpDestroy() { | 
 | 123 |         boolean shouldDestroy = false; | 
 | 124 |         synchronized(this) { | 
 | 125 |             if (!mDestroyed) { | 
 | 126 |                 shouldDestroy = true; | 
 | 127 |                 mDestroyed = true; | 
 | 128 |             } | 
 | 129 |         } | 
 | 130 |  | 
 | 131 |         if (shouldDestroy) { | 
| Yang Ni | eb4dd08 | 2016-03-24 09:40:32 -0700 | [diff] [blame] | 132 |             guard.close(); | 
| Tim Murray | 504abb3 | 2014-01-07 11:13:56 -0800 | [diff] [blame] | 133 |             // must include nObjDestroy in the critical section | 
 | 134 |             ReentrantReadWriteLock.ReadLock rlock = mRS.mRWLock.readLock(); | 
 | 135 |             rlock.lock(); | 
| Tim Murray | 6d63c84 | 2014-02-12 11:16:17 -0800 | [diff] [blame] | 136 |             // AllocationAdapters are BaseObjs with an ID of 0 but should not be passed to nObjDestroy | 
 | 137 |             if(mRS.isAlive() && mID != 0) { | 
| Jason Sams | d78be37 | 2010-08-17 19:28:29 -0700 | [diff] [blame] | 138 |                 mRS.nObjDestroy(mID); | 
| Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 139 |             } | 
| Tim Murray | 504abb3 | 2014-01-07 11:13:56 -0800 | [diff] [blame] | 140 |             rlock.unlock(); | 
| Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 141 |             mRS = null; | 
| Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 142 |             mID = 0; | 
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 143 |         } | 
| Tim Murray | 504abb3 | 2014-01-07 11:13:56 -0800 | [diff] [blame] | 144 |     } | 
 | 145 |  | 
 | 146 |     protected void finalize() throws Throwable { | 
| Yang Ni | eb4dd08 | 2016-03-24 09:40:32 -0700 | [diff] [blame] | 147 |         try { | 
 | 148 |             if (guard != null) { | 
 | 149 |                 guard.warnIfOpen(); | 
 | 150 |             } | 
 | 151 |             helpDestroy(); | 
 | 152 |         } finally { | 
 | 153 |             super.finalize(); | 
 | 154 |         } | 
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 155 |     } | 
| Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 156 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 157 |     /** | 
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 158 |      * Frees any native resources associated with this object.  The | 
 | 159 |      * primary use is to force immediate cleanup of resources when it is | 
 | 160 |      * believed the GC will not respond quickly enough. | 
| Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 161 |      */ | 
| Tim Murray | 504abb3 | 2014-01-07 11:13:56 -0800 | [diff] [blame] | 162 |     public void destroy() { | 
| Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 163 |         if(mDestroyed) { | 
| Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 164 |             throw new RSInvalidStateException("Object already destroyed."); | 
| Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 165 |         } | 
| Tim Murray | 504abb3 | 2014-01-07 11:13:56 -0800 | [diff] [blame] | 166 |         helpDestroy(); | 
| Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 167 |     } | 
 | 168 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 169 |     /** | 
| Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 170 |      * If an object came from an a3d file, java fields need to be | 
 | 171 |      * created with objects from the native layer | 
 | 172 |      */ | 
| Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 173 |     void updateFromNative() { | 
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 174 |         mRS.validate(); | 
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 175 |         mName = mRS.nGetName(getID(mRS)); | 
| Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 176 |     } | 
 | 177 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 178 |     /** | 
| Stephen Hines | 705d2ea | 2011-06-09 10:11:54 -0700 | [diff] [blame] | 179 |      * Calculates the hash code value for a BaseObj. | 
 | 180 |      * | 
 | 181 |      * @return int | 
 | 182 |      */ | 
 | 183 |     @Override | 
 | 184 |     public int hashCode() { | 
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 185 |         return (int)((mID & 0xfffffff) ^ (mID >> 32)); | 
| Stephen Hines | 705d2ea | 2011-06-09 10:11:54 -0700 | [diff] [blame] | 186 |     } | 
 | 187 |  | 
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 188 |     /** | 
| Stephen Hines | 705d2ea | 2011-06-09 10:11:54 -0700 | [diff] [blame] | 189 |      * Compare the current BaseObj with another BaseObj for equality. | 
 | 190 |      * | 
 | 191 |      * @param obj The object to check equality with. | 
 | 192 |      * | 
 | 193 |      * @return boolean | 
 | 194 |      */ | 
 | 195 |     @Override | 
 | 196 |     public boolean equals(Object obj) { | 
 | 197 |         // Early-out check to see if both BaseObjs are actually the same | 
 | 198 |         if (this == obj) | 
 | 199 |             return true; | 
 | 200 |  | 
| Tim Murray | 78214c95 | 2014-02-28 16:57:47 -0800 | [diff] [blame] | 201 |         if (obj == null) { | 
 | 202 |             return false; | 
 | 203 |         } | 
 | 204 |  | 
| Stephen Hines | 705d2ea | 2011-06-09 10:11:54 -0700 | [diff] [blame] | 205 |         if (getClass() != obj.getClass()) { | 
 | 206 |             return false; | 
 | 207 |         } | 
 | 208 |  | 
 | 209 |         BaseObj b = (BaseObj) obj; | 
 | 210 |         return mID == b.mID; | 
 | 211 |     } | 
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 212 | } | 
 | 213 |  |