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