BaseObj.java revision 4658d779e37b0127060438b4cb364c3e0eab5956
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 interfacing with native renderscript objects.
23 * It primarly contains code for tracking the native object ID and forcably
24 * disconecting the object from the native allocation for early cleanup.
25 *
26 **/
27class 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     *
47     * @return int
48     */
49    int getID() {
50        if (mDestroyed) {
51            throw new RSInvalidStateException("using a destroyed object.");
52        }
53        if (mID == 0) {
54            throw new RSRuntimeException("Internal error: Object id 0.");
55        }
56        return mID;
57    }
58
59    void checkValid() {
60        if (mID == 0) {
61            throw new RSIllegalArgumentException("Invalid object.");
62        }
63    }
64
65    private int mID;
66    private boolean mDestroyed;
67    private String mName;
68    RenderScript mRS;
69
70    /**
71     * setName assigns a name to an object.  This object can later be looked up
72     * by this name.  This name will also be retained if the object is written
73     * to an A3D file.
74     *
75     * @param name The name to assign to the object.
76     */
77    public void setName(String name) {
78        if (name == null) {
79            throw new RSIllegalArgumentException(
80                "setName requires a string of non-zero length.");
81        }
82        if(name.length() < 1) {
83            throw new RSIllegalArgumentException(
84                "setName does not accept a zero length string.");
85        }
86        if(mName != null) {
87            throw new RSIllegalArgumentException(
88                "setName object already has a name.");
89        }
90
91        try {
92            byte[] bytes = name.getBytes("UTF-8");
93            mRS.nAssignName(mID, bytes);
94            mName = name;
95        } catch (java.io.UnsupportedEncodingException e) {
96            throw new RuntimeException(e);
97        }
98    }
99
100    protected void finalize() throws Throwable {
101        if (!mDestroyed) {
102            if(mID != 0 && mRS.isAlive()) {
103                mRS.nObjDestroy(mID);
104            }
105            mRS = null;
106            mID = 0;
107            mDestroyed = true;
108            //Log.v(RenderScript.LOG_TAG, getClass() +
109            // " auto finalizing object without having released the RS reference.");
110        }
111        super.finalize();
112    }
113
114    /**
115     * destroy disconnects the object from the native object effectively
116     * rendering this java object dead.  The primary use is to force immediate
117     * cleanup of resources when it is believed the GC will not respond quickly
118     * enough.
119     */
120    synchronized public void destroy() {
121        if(mDestroyed) {
122            throw new RSInvalidStateException("Object already destroyed.");
123        }
124        mDestroyed = true;
125        mRS.nObjDestroy(mID);
126    }
127
128    /**
129     * If an object came from an a3d file, java fields need to be
130     * created with objects from the native layer
131     */
132    void updateFromNative() {
133        mRS.validate();
134        mName = mRS.nGetName(getID());
135    }
136
137}
138
139