BaseObj.java revision 27250e121155f570d1d0db7a1b09fcbaa1361aa4
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;
2027250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murrayimport java.util.concurrent.locks.ReentrantReadWriteLock;
2198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
2298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams/**
237d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * BaseObj is the base class for all RenderScript objects owned by a RS context.
247d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * It is responsible for lifetime management and resource tracking. This class
257d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * should not be used by a user application.
2698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams *
2798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams **/
2898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samspublic class BaseObj {
2998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    BaseObj(int id, RenderScript rs) {
3098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rs.validate();
3198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS = rs;
3298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mID = id;
3398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mDestroyed = false;
3498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
3598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
3698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    void setID(int id) {
3798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (mID != 0) {
3898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSRuntimeException("Internal Error, reset of object ID.");
3998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
4098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mID = id;
4198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
4298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
4398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
4498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * Lookup the native object ID for this object.  Primarily used by the
4598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * generated reflected code.
4698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
4798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to verify against internal context for
4898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *           match.
4998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
5098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return int
5198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
5298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    int getID(RenderScript rs) {
5398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
54ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        if (rs.isNative) {
55ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray            RenderScriptThunker rst = (RenderScriptThunker)rs;
56ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray            if (getNObj() != null) {
57ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray                return getNObj().hashCode();
58ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray            }
59ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        }
6098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (mDestroyed) {
6198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSInvalidStateException("using a destroyed object.");
6298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
6398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (mID == 0) {
6498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSRuntimeException("Internal error: Object id 0.");
6598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
6698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if ((rs != null) && (rs != mRS)) {
6798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSInvalidStateException("using object with mismatched context.");
6898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
6998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mID;
7098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
7198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
72ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray    android.renderscript.BaseObj getNObj() {
73ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        return null;
74ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray    }
75ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray
7698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    void checkValid() {
77ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        if ((mID == 0) && (getNObj() == null)) {
7898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Invalid object.");
7998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
8098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
8198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
8298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private int mID;
8398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private boolean mDestroyed;
8498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    RenderScript mRS;
8598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
8627250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray    private void helpDestroy() {
8727250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray        boolean shouldDestroy = false;
8827250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray        synchronized(this) {
8927250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray            if (!mDestroyed) {
9027250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray                shouldDestroy = true;
9127250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray                mDestroyed = true;
9227250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray            }
9327250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray        }
9427250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray
9527250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray        if (shouldDestroy) {
9627250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray            // must include nObjDestroy in the critical section
9727250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray            ReentrantReadWriteLock.ReadLock rlock = mRS.mRWLock.readLock();
9827250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray            rlock.lock();
9927250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray            if(mRS.isAlive()) {
10098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                mRS.nObjDestroy(mID);
10198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
10227250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray            rlock.unlock();
10398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mRS = null;
10498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mID = 0;
10598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
10627250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray    }
10727250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray
10827250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray
10927250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray    protected void finalize() throws Throwable {
11027250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray        helpDestroy();
11198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        super.finalize();
11298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
11398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
11498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
1157d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Frees any native resources associated with this object.  The
1167d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * primary use is to force immediate cleanup of resources when it is
1177d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * believed the GC will not respond quickly enough.
11898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
11927250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray    public void destroy() {
12098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(mDestroyed) {
12198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSInvalidStateException("Object already destroyed.");
12298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
12327250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray        helpDestroy();
12498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
12598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
12698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
12798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * Calculates the hash code value for a BaseObj.
12898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
12998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return int
13098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
13198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    @Override
13298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public int hashCode() {
13398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mID;
13498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
13598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
13698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
13798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * Compare the current BaseObj with another BaseObj for equality.
13898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
13998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param obj The object to check equality with.
14098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
14198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return boolean
14298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
14398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    @Override
14498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public boolean equals(Object obj) {
14598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        // Early-out check to see if both BaseObjs are actually the same
14698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (this == obj)
14798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return true;
14898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
14998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (getClass() != obj.getClass()) {
15098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return false;
15198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
15298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
15398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        BaseObj b = (BaseObj) obj;
15498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mID == b.mID;
15598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
15698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams}
15798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
158