BaseObj.java revision 5476b450e50939940dcf3f15c92335cee2fc572d
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 17package android.renderscript; 18 19import android.util.Log; 20 21/** 22 * @hide 23 * 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 * 28 **/ 29class BaseObj { 30 BaseObj(int id, RenderScript rs) { 31 rs.validate(); 32 mRS = rs; 33 mID = id; 34 mDestroyed = false; 35 } 36 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 44 /** 45 * Lookup the native object ID for this object. Primarily used by the 46 * generated reflected code. 47 * 48 * 49 * @return int 50 */ 51 int getID() { 52 if (mDestroyed) { 53 throw new RSInvalidStateException("using a destroyed object."); 54 } 55 if (mID == 0) { 56 throw new RSRuntimeException("Internal error: Object id 0."); 57 } 58 return mID; 59 } 60 61 void checkValid() { 62 if (mID == 0) { 63 throw new RSIllegalArgumentException("Invalid object."); 64 } 65 } 66 67 private int mID; 68 private boolean mDestroyed; 69 private String mName; 70 RenderScript mRS; 71 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) { 81 throw new RSIllegalArgumentException("setName does not accept a zero length string."); 82 } 83 if(mName != null) { 84 throw new RSIllegalArgumentException("setName object already has a name."); 85 } 86 87 try { 88 byte[] bytes = name.getBytes("UTF-8"); 89 mRS.nAssignName(mID, bytes); 90 mName = name; 91 } catch (java.io.UnsupportedEncodingException e) { 92 throw new RuntimeException(e); 93 } 94 } 95 96 protected void finalize() throws Throwable { 97 if (!mDestroyed) { 98 if(mID != 0 && mRS.isAlive()) { 99 mRS.nObjDestroy(mID); 100 } 101 mRS = null; 102 mID = 0; 103 mDestroyed = true; 104 //Log.v(RenderScript.LOG_TAG, getClass() + 105 // " auto finalizing object without having released the RS reference."); 106 } 107 super.finalize(); 108 } 109 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 */ 116 synchronized public void destroy() { 117 if(mDestroyed) { 118 throw new RSInvalidStateException("Object already destroyed."); 119 } 120 mDestroyed = true; 121 mRS.nObjDestroy(mID); 122 } 123 124 /** 125 * If an object came from an a3d file, java fields need to be 126 * created with objects from the native layer 127 */ 128 void updateFromNative() { 129 mRS.validate(); 130 mName = mRS.nGetName(getID()); 131 } 132 133} 134 135