blob: 026f7de10bcda6766dc842de07c356b25a3a663d [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 *
Jason Sams06d69de2010-11-09 17:11:40 -080024 * 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 *
Jason Sams36e612a2009-07-31 16:26:13 -070028 **/
29class BaseObj {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070030 BaseObj(int id, RenderScript rs) {
Jason Sams718cd1f2009-12-23 14:35:29 -080031 rs.validate();
Jason Sams36e612a2009-07-31 16:26:13 -070032 mRS = rs;
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070033 mID = id;
Jason Sams1bada8c2009-08-09 17:01:55 -070034 mDestroyed = false;
Jason Sams36e612a2009-07-31 16:26:13 -070035 }
36
Jason Sams06d69de2010-11-09 17:11:40 -080037 void setID(int id) {
38 if (mID != 0) {
39 throw new RSRuntimeException("Internal Error, reset of object ID.");
40 }
41 mID = id;
42 }
43
Jason Sams36e612a2009-07-31 16:26:13 -070044 public int getID() {
Jason Sams7aa150c2010-09-21 14:47:22 -070045 if (mDestroyed) {
Jason Samsc1d62102010-11-04 14:32:19 -070046 throw new RSInvalidStateException("using a destroyed object.");
Jason Sams7aa150c2010-09-21 14:47:22 -070047 }
Jason Sams36e612a2009-07-31 16:26:13 -070048 return mID;
49 }
50
Jason Sams06d69de2010-11-09 17:11:40 -080051 private int mID;
52 private boolean mDestroyed;
53 private String mName;
Jason Sams36e612a2009-07-31 16:26:13 -070054 RenderScript mRS;
55
Jason Samsc1d62102010-11-04 14:32:19 -070056 public void setName(String s) {
Jason Sams36e612a2009-07-31 16:26:13 -070057 if(s.length() < 1) {
Jason Samsc1d62102010-11-04 14:32:19 -070058 throw new RSIllegalArgumentException("setName does not accept a zero length string.");
Jason Sams36e612a2009-07-31 16:26:13 -070059 }
60 if(mName != null) {
Jason Samsc1d62102010-11-04 14:32:19 -070061 throw new RSIllegalArgumentException("setName object already has a name.");
Jason Sams36e612a2009-07-31 16:26:13 -070062 }
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
Jason Samsc1d62102010-11-04 14:32:19 -070073 protected void finalize() throws Throwable {
Jason Sams1bada8c2009-08-09 17:01:55 -070074 if (!mDestroyed) {
Jason Samsa9e7a052009-09-25 14:51:22 -070075 if(mID != 0 && mRS.isAlive()) {
Jason Samsd78be372010-08-17 19:28:29 -070076 mRS.nObjDestroy(mID);
Jason Sams730ee652009-08-18 17:07:09 -070077 }
Jason Samsa9e7a052009-09-25 14:51:22 -070078 mRS = null;
Jason Sams730ee652009-08-18 17:07:09 -070079 mID = 0;
80 mDestroyed = true;
Jason Sams5235cf32009-09-28 18:12:56 -070081 //Log.v(RenderScript.LOG_TAG, getClass() +
82 // " auto finalizing object without having released the RS reference.");
Jason Sams36e612a2009-07-31 16:26:13 -070083 }
84 super.finalize();
85 }
Jason Sams7ce033d2009-08-18 14:14:24 -070086
Jason Sams06d69de2010-11-09 17:11:40 -080087 synchronized public void destroy() {
Jason Sams7ce033d2009-08-18 14:14:24 -070088 if(mDestroyed) {
Jason Samsc1d62102010-11-04 14:32:19 -070089 throw new RSInvalidStateException("Object already destroyed.");
Jason Sams7ce033d2009-08-18 14:14:24 -070090 }
91 mDestroyed = true;
92 mRS.nObjDestroy(mID);
93 }
94
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -070095 // If an object came from an a3d file, java fields need to be
96 // created with objects from the native layer
97 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -080098 mRS.validate();
99 mName = mRS.nGetName(getID());
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700100 }
101
Jason Sams36e612a2009-07-31 16:26:13 -0700102}
103