BaseObj.java revision 7aa150c0967b725850cf27de58f50a25a960b092
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 IllegalStateException("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) throws IllegalStateException, IllegalArgumentException
47    {
48        if(s.length() < 1) {
49            throw new IllegalArgumentException("setName does not accept a zero length string.");
50        }
51        if(mName != null) {
52            throw new IllegalArgumentException("setName object already has a name.");
53        }
54
55        try {
56            byte[] bytes = s.getBytes("UTF-8");
57            mRS.nAssignName(mID, bytes);
58            mName = s;
59        } catch (java.io.UnsupportedEncodingException e) {
60            throw new RuntimeException(e);
61        }
62    }
63
64    protected void finalize() throws Throwable
65    {
66        if (!mDestroyed) {
67            if(mID != 0 && mRS.isAlive()) {
68                mRS.nObjDestroy(mID);
69            }
70            mRS = null;
71            mID = 0;
72            mDestroyed = true;
73            //Log.v(RenderScript.LOG_TAG, getClass() +
74            // " auto finalizing object without having released the RS reference.");
75        }
76        super.finalize();
77    }
78
79    public void destroy() {
80        if(mDestroyed) {
81            throw new IllegalStateException("Object already destroyed.");
82        }
83        mDestroyed = true;
84        mRS.nObjDestroy(mID);
85    }
86
87    // If an object came from an a3d file, java fields need to be
88    // created with objects from the native layer
89    void updateFromNative() {
90    }
91
92}
93
94