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 all RenderScript objects owned by a RS context.
23 * It is responsible for lifetime management and resource tracking. This class
24 * should not be used by a user application.
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 (rs.isNative) {
54            RenderScriptThunker rst = (RenderScriptThunker)rs;
55            if (getNObj() != null) {
56                return getNObj().hashCode();
57            }
58        }
59        if (mDestroyed) {
60            throw new RSInvalidStateException("using a destroyed object.");
61        }
62        if (mID == 0) {
63            throw new RSRuntimeException("Internal error: Object id 0.");
64        }
65        if ((rs != null) && (rs != mRS)) {
66            throw new RSInvalidStateException("using object with mismatched context.");
67        }
68        return mID;
69    }
70
71    android.renderscript.BaseObj getNObj() {
72        return null;
73    }
74
75    void checkValid() {
76        if ((mID == 0) && (getNObj() == null)) {
77            throw new RSIllegalArgumentException("Invalid object.");
78        }
79    }
80
81    private int mID;
82    private boolean mDestroyed;
83    RenderScript mRS;
84
85    protected void finalize() throws Throwable {
86        if (!mDestroyed) {
87            if(mID != 0 && mRS.isAlive()) {
88                mRS.nObjDestroy(mID);
89            }
90            mRS = null;
91            mID = 0;
92            mDestroyed = true;
93            //Log.v(RenderScript.LOG_TAG, getClass() +
94            // " auto finalizing object without having released the RS reference.");
95        }
96        super.finalize();
97    }
98
99    /**
100     * Frees any native resources associated with this object.  The
101     * primary use is to force immediate cleanup of resources when it is
102     * believed the GC will not respond quickly enough.
103     */
104    synchronized public void destroy() {
105        if(mDestroyed) {
106            throw new RSInvalidStateException("Object already destroyed.");
107        }
108        mDestroyed = true;
109        mRS.nObjDestroy(mID);
110    }
111
112    /**
113     * Calculates the hash code value for a BaseObj.
114     *
115     * @return int
116     */
117    @Override
118    public int hashCode() {
119        return mID;
120    }
121
122    /**
123     * Compare the current BaseObj with another BaseObj for equality.
124     *
125     * @param obj The object to check equality with.
126     *
127     * @return boolean
128     */
129    @Override
130    public boolean equals(Object obj) {
131        // Early-out check to see if both BaseObjs are actually the same
132        if (this == obj)
133            return true;
134
135        if (getClass() != obj.getClass()) {
136            return false;
137        }
138
139        BaseObj b = (BaseObj) obj;
140        return mID == b.mID;
141    }
142}
143
144