| 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 | |
| 27 | BaseObj(RenderScript rs) { |
| 28 | mRS = rs; |
| 29 | mID = 0; |
| 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 | |
| 33 | public int getID() { |
| 34 | return mID; |
| 35 | } |
| 36 | |
| 37 | int mID; |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 38 | boolean mDestroyed; |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 39 | String mName; |
| 40 | RenderScript mRS; |
| 41 | |
| 42 | public void setName(String s) throws IllegalStateException, IllegalArgumentException |
| 43 | { |
| 44 | if(s.length() < 1) { |
| 45 | throw new IllegalArgumentException("setName does not accept a zero length string."); |
| 46 | } |
| 47 | if(mName != null) { |
| 48 | throw new IllegalArgumentException("setName object already has a name."); |
| 49 | } |
| 50 | |
| 51 | try { |
| 52 | byte[] bytes = s.getBytes("UTF-8"); |
| 53 | mRS.nAssignName(mID, bytes); |
| 54 | mName = s; |
| 55 | } catch (java.io.UnsupportedEncodingException e) { |
| 56 | throw new RuntimeException(e); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | protected void finalize() throws Throwable |
| 61 | { |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 62 | if (!mDestroyed) { |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 63 | Log.v(RenderScript.LOG_TAG, |
| Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame^] | 64 | getClass() + " finalized without having released the RS reference."); |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 65 | } |
| 66 | super.finalize(); |
| 67 | } |
| Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame^] | 68 | |
| 69 | public void destroy() { |
| 70 | if(mDestroyed) { |
| 71 | throw new IllegalStateException("Object already destroyed."); |
| 72 | } |
| 73 | mDestroyed = true; |
| 74 | mRS.nObjDestroy(mID); |
| 75 | } |
| 76 | |
| Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 77 | } |
| 78 | |