1/* 2 * Copyright (C) 2012 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.support.v8.renderscript; 18 19import android.util.Log; 20 21/** 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 * 26 **/ 27public class BaseObj { 28 BaseObj(int id, RenderScript rs) { 29 rs.validate(); 30 mRS = rs; 31 mID = id; 32 mDestroyed = false; 33 } 34 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 42 /** 43 * Lookup the native object ID for this object. Primarily used by the 44 * generated reflected code. 45 * 46 * @param rs Context to verify against internal context for 47 * match. 48 * 49 * @return int 50 */ 51 int getID(RenderScript rs) { 52 mRS.validate(); 53 if (mDestroyed) { 54 throw new RSInvalidStateException("using a destroyed object."); 55 } 56 if (mID == 0) { 57 throw new RSRuntimeException("Internal error: Object id 0."); 58 } 59 if ((rs != null) && (rs != mRS)) { 60 throw new RSInvalidStateException("using object with mismatched context."); 61 } 62 return mID; 63 } 64 65 void checkValid() { 66 if (mID == 0) { 67 throw new RSIllegalArgumentException("Invalid object."); 68 } 69 } 70 71 private int mID; 72 private boolean mDestroyed; 73 RenderScript mRS; 74 75 protected void finalize() throws Throwable { 76 if (!mDestroyed) { 77 if(mID != 0 && mRS.isAlive()) { 78 mRS.nObjDestroy(mID); 79 } 80 mRS = null; 81 mID = 0; 82 mDestroyed = true; 83 //Log.v(RenderScript.LOG_TAG, getClass() + 84 // " auto finalizing object without having released the RS reference."); 85 } 86 super.finalize(); 87 } 88 89 /** 90 * destroy disconnects the object from the native object effectively 91 * rendering this java object dead. The primary use is to force immediate 92 * cleanup of resources when it is believed the GC will not respond quickly 93 * enough. 94 */ 95 synchronized public void destroy() { 96 if(mDestroyed) { 97 throw new RSInvalidStateException("Object already destroyed."); 98 } 99 mDestroyed = true; 100 mRS.nObjDestroy(mID); 101 } 102 103 /** 104 * Calculates the hash code value for a BaseObj. 105 * 106 * @return int 107 */ 108 @Override 109 public int hashCode() { 110 return mID; 111 } 112 113 /** 114 * Compare the current BaseObj with another BaseObj for equality. 115 * 116 * @param obj The object to check equality with. 117 * 118 * @return boolean 119 */ 120 @Override 121 public boolean equals(Object obj) { 122 // Early-out check to see if both BaseObjs are actually the same 123 if (this == obj) 124 return true; 125 126 if (getClass() != obj.getClass()) { 127 return false; 128 } 129 130 BaseObj b = (BaseObj) obj; 131 return mID == b.mID; 132 } 133} 134 135