| 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 | * |
| 24 | **/ |
| 25 | class BaseObj { |
| 26 | |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 27 | BaseObj(int id, RenderScript rs) { |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 28 | rs.validate(); |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 29 | mRS = rs; |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 30 | mID = id; |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 31 | mDestroyed = false; |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | public int getID() { |
| Jason Sams | 7aa150c | 2010-09-21 14:47:22 -0700 | [diff] [blame] | 35 | if (mDestroyed) { |
| Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame^] | 36 | throw new RSInvalidStateException("using a destroyed object."); |
| Jason Sams | 7aa150c | 2010-09-21 14:47:22 -0700 | [diff] [blame] | 37 | } |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 38 | return mID; |
| 39 | } |
| 40 | |
| 41 | int mID; |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 42 | boolean mDestroyed; |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 43 | String mName; |
| 44 | RenderScript mRS; |
| 45 | |
| Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame^] | 46 | public void setName(String s) { |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 47 | if(s.length() < 1) { |
| Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame^] | 48 | throw new RSIllegalArgumentException("setName does not accept a zero length string."); |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 49 | } |
| 50 | if(mName != null) { |
| Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame^] | 51 | throw new RSIllegalArgumentException("setName object already has a name."); |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | try { |
| 55 | byte[] bytes = s.getBytes("UTF-8"); |
| 56 | mRS.nAssignName(mID, bytes); |
| 57 | mName = s; |
| 58 | } catch (java.io.UnsupportedEncodingException e) { |
| 59 | throw new RuntimeException(e); |
| 60 | } |
| 61 | } |
| 62 | |
| Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame^] | 63 | protected void finalize() throws Throwable { |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 64 | if (!mDestroyed) { |
| Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 65 | if(mID != 0 && mRS.isAlive()) { |
| Jason Sams | d78be37 | 2010-08-17 19:28:29 -0700 | [diff] [blame] | 66 | mRS.nObjDestroy(mID); |
| Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 67 | } |
| Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 68 | mRS = null; |
| Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 69 | mID = 0; |
| 70 | mDestroyed = true; |
| Jason Sams | 5235cf3 | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 71 | //Log.v(RenderScript.LOG_TAG, getClass() + |
| 72 | // " auto finalizing object without having released the RS reference."); |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 73 | } |
| 74 | super.finalize(); |
| 75 | } |
| Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 76 | |
| 77 | public void destroy() { |
| 78 | if(mDestroyed) { |
| Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame^] | 79 | throw new RSInvalidStateException("Object already destroyed."); |
| Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 80 | } |
| 81 | mDestroyed = true; |
| 82 | mRS.nObjDestroy(mID); |
| 83 | } |
| 84 | |
| Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 85 | // If an object came from an a3d file, java fields need to be |
| 86 | // created with objects from the native layer |
| 87 | void updateFromNative() { |
| 88 | } |
| 89 | |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 90 | } |
| 91 | |