• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package android.renderscript;
18 
19 import 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  **/
27 public class BaseObj {
BaseObj(int id, RenderScript rs)28     BaseObj(int id, RenderScript rs) {
29         rs.validate();
30         mRS = rs;
31         mID = id;
32         mDestroyed = false;
33     }
34 
setID(int id)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      */
getID()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 
checkValid()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      */
setName(String name)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     /**
101      * @return name of the renderscript object
102      */
getName()103     public String getName() {
104         return mName;
105     }
106 
finalize()107     protected void finalize() throws Throwable {
108         if (!mDestroyed) {
109             if(mID != 0 && mRS.isAlive()) {
110                 mRS.nObjDestroy(mID);
111             }
112             mRS = null;
113             mID = 0;
114             mDestroyed = true;
115             //Log.v(RenderScript.LOG_TAG, getClass() +
116             // " auto finalizing object without having released the RS reference.");
117         }
118         super.finalize();
119     }
120 
121     /**
122      * destroy disconnects the object from the native object effectively
123      * rendering this java object dead.  The primary use is to force immediate
124      * cleanup of resources when it is believed the GC will not respond quickly
125      * enough.
126      */
destroy()127     synchronized public void destroy() {
128         if(mDestroyed) {
129             throw new RSInvalidStateException("Object already destroyed.");
130         }
131         mDestroyed = true;
132         mRS.nObjDestroy(mID);
133     }
134 
135     /**
136      * If an object came from an a3d file, java fields need to be
137      * created with objects from the native layer
138      */
updateFromNative()139     void updateFromNative() {
140         mRS.validate();
141         mName = mRS.nGetName(getID());
142     }
143 
144     /**
145      * Calculates the hash code value for a BaseObj.
146      *
147      * @return int
148      */
149     @Override
hashCode()150     public int hashCode() {
151         return mID;
152     }
153 
154     /**
155      * Compare the current BaseObj with another BaseObj for equality.
156      *
157      * @param obj The object to check equality with.
158      *
159      * @return boolean
160      */
161     @Override
equals(Object obj)162     public boolean equals(Object obj) {
163         // Early-out check to see if both BaseObjs are actually the same
164         if (this == obj)
165             return true;
166 
167         if (getClass() != obj.getClass()) {
168             return false;
169         }
170 
171         BaseObj b = (BaseObj) obj;
172         return mID == b.mID;
173     }
174 }
175 
176