BaseObj.java revision c11e25c4e653124def1fb18e203b894f42106cbe
1/*
2 * Copyright (C) 2008 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.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 (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    private String mName;
74    RenderScript mRS;
75
76    /**
77     * setName assigns a name to an object.  This object can later be looked up
78     * by this name.
79     *
80     * @param name The name to assign to the object.
81     */
82    public void setName(String name) {
83        if (name == null) {
84            throw new RSIllegalArgumentException(
85                "setName requires a string of non-zero length.");
86        }
87        if(name.length() < 1) {
88            throw new RSIllegalArgumentException(
89                "setName does not accept a zero length string.");
90        }
91        if(mName != null) {
92            throw new RSIllegalArgumentException(
93                "setName object already has a name.");
94        }
95
96        try {
97            byte[] bytes = name.getBytes("UTF-8");
98            mRS.nAssignName(mID, bytes);
99            mName = name;
100        } catch (java.io.UnsupportedEncodingException e) {
101            throw new RuntimeException(e);
102        }
103    }
104
105    /**
106     * @return name of the renderscript object
107     */
108    public String getName() {
109        return mName;
110    }
111
112    protected void finalize() throws Throwable {
113        if (!mDestroyed) {
114            if(mID != 0 && mRS.isAlive()) {
115                mRS.nObjDestroy(mID);
116            }
117            mRS = null;
118            mID = 0;
119            mDestroyed = true;
120            //Log.v(RenderScript.LOG_TAG, getClass() +
121            // " auto finalizing object without having released the RS reference.");
122        }
123        super.finalize();
124    }
125
126    /**
127     * Frees any native resources associated with this object.  The
128     * primary use is to force immediate cleanup of resources when it is
129     * believed the GC will not respond quickly enough.
130     */
131    synchronized public void destroy() {
132        if(mDestroyed) {
133            throw new RSInvalidStateException("Object already destroyed.");
134        }
135        mDestroyed = true;
136        mRS.nObjDestroy(mID);
137    }
138
139    /**
140     * If an object came from an a3d file, java fields need to be
141     * created with objects from the native layer
142     */
143    void updateFromNative() {
144        mRS.validate();
145        mName = mRS.nGetName(getID(mRS));
146    }
147
148    /**
149     * Calculates the hash code value for a BaseObj.
150     *
151     * @return int
152     */
153    @Override
154    public int hashCode() {
155        return mID;
156    }
157
158    /**
159     * Compare the current BaseObj with another BaseObj for equality.
160     *
161     * @param obj The object to check equality with.
162     *
163     * @return boolean
164     */
165    @Override
166    public boolean equals(Object obj) {
167        // Early-out check to see if both BaseObjs are actually the same
168        if (this == obj)
169            return true;
170
171        if (getClass() != obj.getClass()) {
172            return false;
173        }
174
175        BaseObj b = (BaseObj) obj;
176        return mID == b.mID;
177    }
178}
179
180