BaseObj.java revision c1d6210fb5cc558ccea95a59a2b33bb9015fc7de
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 * @hide
23 *
24 **/
25class BaseObj {
26
27    BaseObj(int id, RenderScript rs) {
28        rs.validate();
29        mRS = rs;
30        mID = id;
31        mDestroyed = false;
32    }
33
34    public int getID() {
35        if (mDestroyed) {
36            throw new RSInvalidStateException("using a destroyed object.");
37        }
38        return mID;
39    }
40
41    int mID;
42    boolean mDestroyed;
43    String mName;
44    RenderScript mRS;
45
46    public void setName(String s) {
47        if(s.length() < 1) {
48            throw new RSIllegalArgumentException("setName does not accept a zero length string.");
49        }
50        if(mName != null) {
51            throw new RSIllegalArgumentException("setName object already has a name.");
52        }
53
54        try {
55            byte[] bytes = s.getBytes("UTF-8");
56            mRS.nAssignName(mID, bytes);
57            mName = s;
58        } catch (java.io.UnsupportedEncodingException e) {
59            throw new RuntimeException(e);
60        }
61    }
62
63    protected void finalize() throws Throwable {
64        if (!mDestroyed) {
65            if(mID != 0 && mRS.isAlive()) {
66                mRS.nObjDestroy(mID);
67            }
68            mRS = null;
69            mID = 0;
70            mDestroyed = true;
71            //Log.v(RenderScript.LOG_TAG, getClass() +
72            // " auto finalizing object without having released the RS reference.");
73        }
74        super.finalize();
75    }
76
77    public void destroy() {
78        if(mDestroyed) {
79            throw new RSInvalidStateException("Object already destroyed.");
80        }
81        mDestroyed = true;
82        mRS.nObjDestroy(mID);
83    }
84
85    // If an object came from an a3d file, java fields need to be
86    // created with objects from the native layer
87    void updateFromNative() {
88    }
89
90}
91
92