BaseObj.java revision a33549e51d1ba585a349bbf7b49cd32dbca0def3
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;
20import java.util.concurrent.locks.ReentrantReadWriteLock;
21
22/**
23 * BaseObj is the base class for all RenderScript objects owned by a RS context.
24 * It is responsible for lifetime management and resource tracking. This class
25 * should not be used by a user application.
26 *
27 **/
28public class BaseObj {
29    BaseObj(int id, RenderScript rs) {
30        rs.validate();
31        mRS = rs;
32        mID = id;
33        mDestroyed = false;
34    }
35
36    void setID(int id) {
37        if (mID != 0) {
38            throw new RSRuntimeException("Internal Error, reset of object ID.");
39        }
40        mID = id;
41    }
42
43    /**
44     * Lookup the native object ID for this object.  Primarily used by the
45     * generated reflected code.
46     *
47     * @param rs Context to verify against internal context for
48     *           match.
49     *
50     * @return int
51     */
52    int getID(RenderScript rs) {
53        mRS.validate();
54        if (rs.isNative) {
55            RenderScriptThunker rst = (RenderScriptThunker)rs;
56            if (getNObj() != null) {
57                return getNObj().hashCode();
58            }
59        }
60        if (mDestroyed) {
61            throw new RSInvalidStateException("using a destroyed object.");
62        }
63        if (mID == 0) {
64            throw new RSRuntimeException("Internal error: Object id 0.");
65        }
66        if ((rs != null) && (rs != mRS)) {
67            throw new RSInvalidStateException("using object with mismatched context.");
68        }
69        return mID;
70    }
71
72    android.renderscript.BaseObj getNObj() {
73        return null;
74    }
75
76    void checkValid() {
77        if ((mID == 0) && (getNObj() == null)) {
78            throw new RSIllegalArgumentException("Invalid object.");
79        }
80    }
81
82    private int mID;
83    private boolean mDestroyed;
84    RenderScript mRS;
85
86    private void helpDestroy() {
87        boolean shouldDestroy = false;
88        synchronized(this) {
89            if (!mDestroyed) {
90                shouldDestroy = true;
91                mDestroyed = true;
92            }
93        }
94
95        if (shouldDestroy) {
96            // must include nObjDestroy in the critical section
97            ReentrantReadWriteLock.ReadLock rlock = mRS.mRWLock.readLock();
98            rlock.lock();
99            if(mRS.isAlive()) {
100                mRS.nObjDestroy(mID);
101            }
102            rlock.unlock();
103            mRS = null;
104            mID = 0;
105        }
106    }
107
108
109    protected void finalize() throws Throwable {
110        helpDestroy();
111        super.finalize();
112    }
113
114    /**
115     * Frees any native resources associated with this object.  The
116     * primary use is to force immediate cleanup of resources when it is
117     * believed the GC will not respond quickly enough.
118     */
119    public void destroy() {
120        if(mDestroyed) {
121            throw new RSInvalidStateException("Object already destroyed.");
122        }
123        helpDestroy();
124    }
125
126    /**
127     * Calculates the hash code value for a BaseObj.
128     *
129     * @return int
130     */
131    @Override
132    public int hashCode() {
133        return mID;
134    }
135
136    /**
137     * Compare the current BaseObj with another BaseObj for equality.
138     *
139     * @param obj The object to check equality with.
140     *
141     * @return boolean
142     */
143    @Override
144    public boolean equals(Object obj) {
145        // Early-out check to see if both BaseObjs are actually the same
146        if (this == obj)
147            return true;
148
149        if (getClass() != obj.getClass()) {
150            return false;
151        }
152
153        BaseObj b = (BaseObj) obj;
154        return mID == b.mID;
155    }
156}
157
158