blob: 69907d988ddec3e0409c326f6263b718f5eed3ab [file] [log] [blame]
Jason Sams36e612a2009-07-31 16:26:13 -07001/*
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
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070027 BaseObj(int id, RenderScript rs) {
Jason Sams718cd1f2009-12-23 14:35:29 -080028 rs.validate();
Jason Sams36e612a2009-07-31 16:26:13 -070029 mRS = rs;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070030 mID = id;
Jason Sams1bada8c2009-08-09 17:01:55 -070031 mDestroyed = false;
Jason Sams36e612a2009-07-31 16:26:13 -070032 }
33
34 public int getID() {
Jason Sams7aa150c2010-09-21 14:47:22 -070035 if (mDestroyed) {
Jason Samsc1d62102010-11-04 14:32:19 -070036 throw new RSInvalidStateException("using a destroyed object.");
Jason Sams7aa150c2010-09-21 14:47:22 -070037 }
Jason Sams36e612a2009-07-31 16:26:13 -070038 return mID;
39 }
40
41 int mID;
Jason Sams1bada8c2009-08-09 17:01:55 -070042 boolean mDestroyed;
Jason Sams36e612a2009-07-31 16:26:13 -070043 String mName;
44 RenderScript mRS;
45
Jason Samsc1d62102010-11-04 14:32:19 -070046 public void setName(String s) {
Jason Sams36e612a2009-07-31 16:26:13 -070047 if(s.length() < 1) {
Jason Samsc1d62102010-11-04 14:32:19 -070048 throw new RSIllegalArgumentException("setName does not accept a zero length string.");
Jason Sams36e612a2009-07-31 16:26:13 -070049 }
50 if(mName != null) {
Jason Samsc1d62102010-11-04 14:32:19 -070051 throw new RSIllegalArgumentException("setName object already has a name.");
Jason Sams36e612a2009-07-31 16:26:13 -070052 }
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
Jason Samsc1d62102010-11-04 14:32:19 -070063 protected void finalize() throws Throwable {
Jason Sams1bada8c2009-08-09 17:01:55 -070064 if (!mDestroyed) {
Jason Samsa9e7a052009-09-25 14:51:22 -070065 if(mID != 0 && mRS.isAlive()) {
Jason Samsd78be372010-08-17 19:28:29 -070066 mRS.nObjDestroy(mID);
Jason Sams730ee652009-08-18 17:07:09 -070067 }
Jason Samsa9e7a052009-09-25 14:51:22 -070068 mRS = null;
Jason Sams730ee652009-08-18 17:07:09 -070069 mID = 0;
70 mDestroyed = true;
Jason Sams5235cf32009-09-28 18:12:56 -070071 //Log.v(RenderScript.LOG_TAG, getClass() +
72 // " auto finalizing object without having released the RS reference.");
Jason Sams36e612a2009-07-31 16:26:13 -070073 }
74 super.finalize();
75 }
Jason Sams7ce033d2009-08-18 14:14:24 -070076
77 public void destroy() {
78 if(mDestroyed) {
Jason Samsc1d62102010-11-04 14:32:19 -070079 throw new RSInvalidStateException("Object already destroyed.");
Jason Sams7ce033d2009-08-18 14:14:24 -070080 }
81 mDestroyed = true;
82 mRS.nObjDestroy(mID);
83 }
84
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -070085 // 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
Jason Sams36e612a2009-07-31 16:26:13 -070090}
91