blob: 669beac475726fef9dbf43214a95f6d385a6ffc5 [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/**
Jason Sams06d69de2010-11-09 17:11:40 -080022 * 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 *
Jason Sams36e612a2009-07-31 16:26:13 -070026 **/
27class BaseObj {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070028 BaseObj(int id, RenderScript rs) {
Jason Sams718cd1f2009-12-23 14:35:29 -080029 rs.validate();
Jason Sams36e612a2009-07-31 16:26:13 -070030 mRS = rs;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070031 mID = id;
Jason Sams1bada8c2009-08-09 17:01:55 -070032 mDestroyed = false;
Jason Sams36e612a2009-07-31 16:26:13 -070033 }
34
Jason Sams06d69de2010-11-09 17:11:40 -080035 void setID(int id) {
36 if (mID != 0) {
37 throw new RSRuntimeException("Internal Error, reset of object ID.");
38 }
39 mID = id;
40 }
41
Jason Sams27676fe2010-11-10 17:00:59 -080042 /**
43 * Lookup the native object ID for this object. Primarily used by the
44 * generated reflected code.
45 *
46 *
47 * @return int
48 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -080049 int getID() {
Jason Sams7aa150c2010-09-21 14:47:22 -070050 if (mDestroyed) {
Jason Samsc1d62102010-11-04 14:32:19 -070051 throw new RSInvalidStateException("using a destroyed object.");
Jason Sams7aa150c2010-09-21 14:47:22 -070052 }
Jason Sams5476b452010-12-08 16:14:36 -080053 if (mID == 0) {
54 throw new RSRuntimeException("Internal error: Object id 0.");
55 }
Jason Sams36e612a2009-07-31 16:26:13 -070056 return mID;
57 }
58
Jason Samsbf6ef8d72010-12-06 15:59:59 -080059 void checkValid() {
60 if (mID == 0) {
61 throw new RSIllegalArgumentException("Invalid object.");
62 }
63 }
64
Jason Sams06d69de2010-11-09 17:11:40 -080065 private int mID;
66 private boolean mDestroyed;
67 private String mName;
Jason Sams36e612a2009-07-31 16:26:13 -070068 RenderScript mRS;
69
Jason Sams27676fe2010-11-10 17:00:59 -080070 /**
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 */
77 public void setName(String name) {
78 if(name.length() < 1) {
Jason Samsc1d62102010-11-04 14:32:19 -070079 throw new RSIllegalArgumentException("setName does not accept a zero length string.");
Jason Sams36e612a2009-07-31 16:26:13 -070080 }
81 if(mName != null) {
Jason Samsc1d62102010-11-04 14:32:19 -070082 throw new RSIllegalArgumentException("setName object already has a name.");
Jason Sams36e612a2009-07-31 16:26:13 -070083 }
84
85 try {
Jason Sams27676fe2010-11-10 17:00:59 -080086 byte[] bytes = name.getBytes("UTF-8");
Jason Sams36e612a2009-07-31 16:26:13 -070087 mRS.nAssignName(mID, bytes);
Jason Sams27676fe2010-11-10 17:00:59 -080088 mName = name;
Jason Sams36e612a2009-07-31 16:26:13 -070089 } catch (java.io.UnsupportedEncodingException e) {
90 throw new RuntimeException(e);
91 }
92 }
93
Jason Samsc1d62102010-11-04 14:32:19 -070094 protected void finalize() throws Throwable {
Jason Sams1bada8c2009-08-09 17:01:55 -070095 if (!mDestroyed) {
Jason Samsa9e7a052009-09-25 14:51:22 -070096 if(mID != 0 && mRS.isAlive()) {
Jason Samsd78be372010-08-17 19:28:29 -070097 mRS.nObjDestroy(mID);
Jason Sams730ee652009-08-18 17:07:09 -070098 }
Jason Samsa9e7a052009-09-25 14:51:22 -070099 mRS = null;
Jason Sams730ee652009-08-18 17:07:09 -0700100 mID = 0;
101 mDestroyed = true;
Jason Sams5235cf32009-09-28 18:12:56 -0700102 //Log.v(RenderScript.LOG_TAG, getClass() +
103 // " auto finalizing object without having released the RS reference.");
Jason Sams36e612a2009-07-31 16:26:13 -0700104 }
105 super.finalize();
106 }
Jason Sams7ce033d2009-08-18 14:14:24 -0700107
Jason Sams27676fe2010-11-10 17:00:59 -0800108 /**
109 * destroy disconnects the object from the native object effectivly
110 * rendering this java object dead. The primary use is to force immediate
111 * cleanup of resources when its believed the GC will not respond quickly
112 * enough.
113 */
Jason Sams06d69de2010-11-09 17:11:40 -0800114 synchronized public void destroy() {
Jason Sams7ce033d2009-08-18 14:14:24 -0700115 if(mDestroyed) {
Jason Samsc1d62102010-11-04 14:32:19 -0700116 throw new RSInvalidStateException("Object already destroyed.");
Jason Sams7ce033d2009-08-18 14:14:24 -0700117 }
118 mDestroyed = true;
119 mRS.nObjDestroy(mID);
120 }
121
Jason Sams27676fe2010-11-10 17:00:59 -0800122 /**
123 * If an object came from an a3d file, java fields need to be
124 * created with objects from the native layer
125 */
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700126 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800127 mRS.validate();
128 mName = mRS.nGetName(getID());
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700129 }
130
Jason Sams36e612a2009-07-31 16:26:13 -0700131}
132