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 | |
| 19 | import android.util.Log; |
| 20 | |
| 21 | /** |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 22 | * 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 Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 26 | **/ |
Stephen Hines | ef353dd | 2011-03-31 14:45:36 -0700 | [diff] [blame] | 27 | public class BaseObj { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 28 | BaseObj(int id, RenderScript rs) { |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 29 | rs.validate(); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 30 | mRS = rs; |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 31 | mID = id; |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 32 | mDestroyed = false; |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 35 | void setID(int id) { |
| 36 | if (mID != 0) { |
| 37 | throw new RSRuntimeException("Internal Error, reset of object ID."); |
| 38 | } |
| 39 | mID = id; |
| 40 | } |
| 41 | |
Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 42 | /** |
| 43 | * Lookup the native object ID for this object. Primarily used by the |
| 44 | * generated reflected code. |
| 45 | * |
| 46 | * |
| 47 | * @return int |
| 48 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 49 | int getID() { |
Jason Sams | 7aa150c | 2010-09-21 14:47:22 -0700 | [diff] [blame] | 50 | if (mDestroyed) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 51 | throw new RSInvalidStateException("using a destroyed object."); |
Jason Sams | 7aa150c | 2010-09-21 14:47:22 -0700 | [diff] [blame] | 52 | } |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 53 | if (mID == 0) { |
| 54 | throw new RSRuntimeException("Internal error: Object id 0."); |
| 55 | } |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 56 | return mID; |
| 57 | } |
| 58 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 59 | void checkValid() { |
| 60 | if (mID == 0) { |
| 61 | throw new RSIllegalArgumentException("Invalid object."); |
| 62 | } |
| 63 | } |
| 64 | |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 65 | private int mID; |
| 66 | private boolean mDestroyed; |
| 67 | private String mName; |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 68 | RenderScript mRS; |
| 69 | |
Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 70 | /** |
| 71 | * setName assigns a name to an object. This object can later be looked up |
| 72 | * by this name. This name will also be retained if the object is written |
| 73 | * to an A3D file. |
| 74 | * |
| 75 | * @param name The name to assign to the object. |
| 76 | */ |
| 77 | public void setName(String name) { |
Stephen Hines | 84a97ca | 2011-03-15 21:05:54 -0700 | [diff] [blame] | 78 | if (name == null) { |
| 79 | throw new RSIllegalArgumentException( |
| 80 | "setName requires a string of non-zero length."); |
| 81 | } |
Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 82 | if(name.length() < 1) { |
Stephen Hines | 84a97ca | 2011-03-15 21:05:54 -0700 | [diff] [blame] | 83 | throw new RSIllegalArgumentException( |
| 84 | "setName does not accept a zero length string."); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 85 | } |
| 86 | if(mName != null) { |
Stephen Hines | 84a97ca | 2011-03-15 21:05:54 -0700 | [diff] [blame] | 87 | throw new RSIllegalArgumentException( |
| 88 | "setName object already has a name."); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | try { |
Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 92 | byte[] bytes = name.getBytes("UTF-8"); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 93 | mRS.nAssignName(mID, bytes); |
Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 94 | mName = name; |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 95 | } catch (java.io.UnsupportedEncodingException e) { |
| 96 | throw new RuntimeException(e); |
| 97 | } |
| 98 | } |
| 99 | |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 100 | protected void finalize() throws Throwable { |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 101 | if (!mDestroyed) { |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 102 | if(mID != 0 && mRS.isAlive()) { |
Jason Sams | d78be37 | 2010-08-17 19:28:29 -0700 | [diff] [blame] | 103 | mRS.nObjDestroy(mID); |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 104 | } |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 105 | mRS = null; |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 106 | mID = 0; |
| 107 | mDestroyed = true; |
Jason Sams | 5235cf3 | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 108 | //Log.v(RenderScript.LOG_TAG, getClass() + |
| 109 | // " auto finalizing object without having released the RS reference."); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 110 | } |
| 111 | super.finalize(); |
| 112 | } |
Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 113 | |
Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 114 | /** |
Stephen Hines | 84a97ca | 2011-03-15 21:05:54 -0700 | [diff] [blame] | 115 | * destroy disconnects the object from the native object effectively |
Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 116 | * rendering this java object dead. The primary use is to force immediate |
Stephen Hines | 84a97ca | 2011-03-15 21:05:54 -0700 | [diff] [blame] | 117 | * cleanup of resources when it is believed the GC will not respond quickly |
Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 118 | * enough. |
| 119 | */ |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 120 | synchronized public void destroy() { |
Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 121 | if(mDestroyed) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 122 | throw new RSInvalidStateException("Object already destroyed."); |
Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 123 | } |
| 124 | mDestroyed = true; |
| 125 | mRS.nObjDestroy(mID); |
| 126 | } |
| 127 | |
Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 128 | /** |
| 129 | * If an object came from an a3d file, java fields need to be |
| 130 | * created with objects from the native layer |
| 131 | */ |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 132 | void updateFromNative() { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 133 | mRS.validate(); |
| 134 | mName = mRS.nGetName(getID()); |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 137 | } |
| 138 | |