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