198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams/* 298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * Copyright (C) 2012 The Android Open Source Project 398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * 498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * Licensed under the Apache License, Version 2.0 (the "License"); 598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * you may not use this file except in compliance with the License. 698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * You may obtain a copy of the License at 798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * 898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * http://www.apache.org/licenses/LICENSE-2.0 998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * 1098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * Unless required by applicable law or agreed to in writing, software 1198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * distributed under the License is distributed on an "AS IS" BASIS, 1298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * See the License for the specific language governing permissions and 1498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * limitations under the License. 1598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams */ 1698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams 1798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samspackage android.support.v8.renderscript; 1898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams 1998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport android.util.Log; 2098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams 2198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams/** 227d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * BaseObj is the base class for all RenderScript objects owned by a RS context. 237d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * It is responsible for lifetime management and resource tracking. This class 247d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * should not be used by a user application. 2598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * 2698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams **/ 2798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samspublic class BaseObj { 2898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams BaseObj(int id, RenderScript rs) { 2998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams rs.validate(); 3098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams mRS = rs; 3198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams mID = id; 3298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams mDestroyed = false; 3398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams } 3498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams 3598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams void setID(int id) { 3698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams if (mID != 0) { 3798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams throw new RSRuntimeException("Internal Error, reset of object ID."); 3898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams } 3998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams mID = id; 4098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams } 4198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams 4298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams /** 4398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * Lookup the native object ID for this object. Primarily used by the 4498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * generated reflected code. 4598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * 4698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * @param rs Context to verify against internal context for 4798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * match. 4898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * 4998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * @return int 5098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams */ 5198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams int getID(RenderScript rs) { 5298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams mRS.validate(); 53ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray if (rs.isNative) { 54ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray RenderScriptThunker rst = (RenderScriptThunker)rs; 55ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray if (getNObj() != null) { 56ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray return getNObj().hashCode(); 57ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray } 58ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray } 5998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams if (mDestroyed) { 6098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams throw new RSInvalidStateException("using a destroyed object."); 6198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams } 6298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams if (mID == 0) { 6398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams throw new RSRuntimeException("Internal error: Object id 0."); 6498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams } 6598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams if ((rs != null) && (rs != mRS)) { 6698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams throw new RSInvalidStateException("using object with mismatched context."); 6798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams } 6898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams return mID; 6998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams } 7098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams 71ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray android.renderscript.BaseObj getNObj() { 72ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray return null; 73ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray } 74ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray 7598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams void checkValid() { 76ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray if ((mID == 0) && (getNObj() == null)) { 7798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams throw new RSIllegalArgumentException("Invalid object."); 7898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams } 7998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams } 8098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams 8198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams private int mID; 8298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams private boolean mDestroyed; 8398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams RenderScript mRS; 8498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams 8598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams protected void finalize() throws Throwable { 8698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams if (!mDestroyed) { 8798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams if(mID != 0 && mRS.isAlive()) { 8898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams mRS.nObjDestroy(mID); 8998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams } 9098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams mRS = null; 9198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams mID = 0; 9298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams mDestroyed = true; 9398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams //Log.v(RenderScript.LOG_TAG, getClass() + 9498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams // " auto finalizing object without having released the RS reference."); 9598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams } 9698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams super.finalize(); 9798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams } 9898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams 9998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams /** 1007d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * Frees any native resources associated with this object. The 1017d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * primary use is to force immediate cleanup of resources when it is 1027d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * believed the GC will not respond quickly enough. 10398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams */ 10498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams synchronized public void destroy() { 10598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams if(mDestroyed) { 10698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams throw new RSInvalidStateException("Object already destroyed."); 10798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams } 10898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams mDestroyed = true; 10998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams mRS.nObjDestroy(mID); 11098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams } 11198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams 11298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams /** 11398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * Calculates the hash code value for a BaseObj. 11498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * 11598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * @return int 11698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams */ 11798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams @Override 11898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams public int hashCode() { 11998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams return mID; 12098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams } 12198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams 12298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams /** 12398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * Compare the current BaseObj with another BaseObj for equality. 12498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * 12598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * @param obj The object to check equality with. 12698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * 12798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * @return boolean 12898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams */ 12998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams @Override 13098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams public boolean equals(Object obj) { 13198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams // Early-out check to see if both BaseObjs are actually the same 13298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams if (this == obj) 13398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams return true; 13498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams 13598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams if (getClass() != obj.getClass()) { 13698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams return false; 13798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams } 13898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams 13998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams BaseObj b = (BaseObj) obj; 14098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams return mID == b.mID; 14198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams } 14298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams} 14398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams 144