2009-07-31 16:26:13 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package android.renderscript;
|
|
|
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
2012-04-19 16:30:58 -07:00
|
|
|
/** @deprecated renderscript is deprecated in J
|
2010-11-09 17:11:40 -08:00
|
|
|
* BaseObj is the base class for interfacing with native renderscript objects.
|
|
|
|
* It primarly contains code for tracking the native object ID and forcably
|
|
|
|
* disconecting the object from the native allocation for early cleanup.
|
|
|
|
*
|
2009-07-31 16:26:13 -07:00
|
|
|
**/
|
2011-03-31 14:45:36 -07:00
|
|
|
public class BaseObj {
|
2010-08-11 14:41:28 -07:00
|
|
|
BaseObj(int id, RenderScript rs) {
|
2009-12-23 14:35:29 -08:00
|
|
|
rs.validate();
|
2009-07-31 16:26:13 -07:00
|
|
|
mRS = rs;
|
2010-08-11 14:41:28 -07:00
|
|
|
mID = id;
|
2009-08-09 17:01:55 -07:00
|
|
|
mDestroyed = false;
|
2009-07-31 16:26:13 -07:00
|
|
|
}
|
|
|
|
|
2010-11-09 17:11:40 -08:00
|
|
|
void setID(int id) {
|
|
|
|
if (mID != 0) {
|
|
|
|
throw new RSRuntimeException("Internal Error, reset of object ID.");
|
|
|
|
}
|
|
|
|
mID = id;
|
|
|
|
}
|
|
|
|
|
2012-04-19 16:30:58 -07:00
|
|
|
/** @deprecated renderscript is deprecated in J
|
2010-11-10 17:00:59 -08:00
|
|
|
* Lookup the native object ID for this object. Primarily used by the
|
|
|
|
* generated reflected code.
|
|
|
|
*
|
2012-04-03 15:36:36 -07:00
|
|
|
* @param rs Context to verify against internal context for
|
|
|
|
* match.
|
2010-11-10 17:00:59 -08:00
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2012-04-03 15:36:36 -07:00
|
|
|
int getID(RenderScript rs) {
|
|
|
|
mRS.validate();
|
2010-09-21 14:47:22 -07:00
|
|
|
if (mDestroyed) {
|
2010-11-04 14:32:19 -07:00
|
|
|
throw new RSInvalidStateException("using a destroyed object.");
|
2010-09-21 14:47:22 -07:00
|
|
|
}
|
2010-12-08 16:14:36 -08:00
|
|
|
if (mID == 0) {
|
|
|
|
throw new RSRuntimeException("Internal error: Object id 0.");
|
|
|
|
}
|
2012-04-03 15:36:36 -07:00
|
|
|
if ((rs != null) && (rs != mRS)) {
|
|
|
|
throw new RSInvalidStateException("using object with mismatched context.");
|
|
|
|
}
|
2009-07-31 16:26:13 -07:00
|
|
|
return mID;
|
|
|
|
}
|
|
|
|
|
2010-12-06 15:59:59 -08:00
|
|
|
void checkValid() {
|
|
|
|
if (mID == 0) {
|
|
|
|
throw new RSIllegalArgumentException("Invalid object.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-09 17:11:40 -08:00
|
|
|
private int mID;
|
|
|
|
private boolean mDestroyed;
|
|
|
|
private String mName;
|
2009-07-31 16:26:13 -07:00
|
|
|
RenderScript mRS;
|
|
|
|
|
2012-04-19 16:30:58 -07:00
|
|
|
/** @deprecated renderscript is deprecated in J
|
2010-11-10 17:00:59 -08:00
|
|
|
* setName assigns a name to an object. This object can later be looked up
|
|
|
|
* by this name. This name will also be retained if the object is written
|
|
|
|
* to an A3D file.
|
|
|
|
*
|
|
|
|
* @param name The name to assign to the object.
|
|
|
|
*/
|
|
|
|
public void setName(String name) {
|
2011-03-15 21:05:54 -07:00
|
|
|
if (name == null) {
|
|
|
|
throw new RSIllegalArgumentException(
|
|
|
|
"setName requires a string of non-zero length.");
|
|
|
|
}
|
2010-11-10 17:00:59 -08:00
|
|
|
if(name.length() < 1) {
|
2011-03-15 21:05:54 -07:00
|
|
|
throw new RSIllegalArgumentException(
|
|
|
|
"setName does not accept a zero length string.");
|
2009-07-31 16:26:13 -07:00
|
|
|
}
|
|
|
|
if(mName != null) {
|
2011-03-15 21:05:54 -07:00
|
|
|
throw new RSIllegalArgumentException(
|
|
|
|
"setName object already has a name.");
|
2009-07-31 16:26:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2010-11-10 17:00:59 -08:00
|
|
|
byte[] bytes = name.getBytes("UTF-8");
|
2009-07-31 16:26:13 -07:00
|
|
|
mRS.nAssignName(mID, bytes);
|
2010-11-10 17:00:59 -08:00
|
|
|
mName = name;
|
2009-07-31 16:26:13 -07:00
|
|
|
} catch (java.io.UnsupportedEncodingException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-19 16:30:58 -07:00
|
|
|
/** @deprecated renderscript is deprecated in J
|
2011-07-26 14:13:32 -07:00
|
|
|
* @return name of the renderscript object
|
|
|
|
*/
|
|
|
|
public String getName() {
|
|
|
|
return mName;
|
|
|
|
}
|
|
|
|
|
2010-11-04 14:32:19 -07:00
|
|
|
protected void finalize() throws Throwable {
|
2009-08-09 17:01:55 -07:00
|
|
|
if (!mDestroyed) {
|
2009-09-25 14:51:22 -07:00
|
|
|
if(mID != 0 && mRS.isAlive()) {
|
2010-08-17 19:28:29 -07:00
|
|
|
mRS.nObjDestroy(mID);
|
2009-08-18 17:07:09 -07:00
|
|
|
}
|
2009-09-25 14:51:22 -07:00
|
|
|
mRS = null;
|
2009-08-18 17:07:09 -07:00
|
|
|
mID = 0;
|
|
|
|
mDestroyed = true;
|
2009-09-28 18:12:56 -07:00
|
|
|
//Log.v(RenderScript.LOG_TAG, getClass() +
|
|
|
|
// " auto finalizing object without having released the RS reference.");
|
2009-07-31 16:26:13 -07:00
|
|
|
}
|
|
|
|
super.finalize();
|
|
|
|
}
|
2009-08-18 14:14:24 -07:00
|
|
|
|
2012-04-19 16:30:58 -07:00
|
|
|
/** @deprecated renderscript is deprecated in J
|
2011-03-15 21:05:54 -07:00
|
|
|
* destroy disconnects the object from the native object effectively
|
2010-11-10 17:00:59 -08:00
|
|
|
* rendering this java object dead. The primary use is to force immediate
|
2011-03-15 21:05:54 -07:00
|
|
|
* cleanup of resources when it is believed the GC will not respond quickly
|
2010-11-10 17:00:59 -08:00
|
|
|
* enough.
|
|
|
|
*/
|
2010-11-09 17:11:40 -08:00
|
|
|
synchronized public void destroy() {
|
2009-08-18 14:14:24 -07:00
|
|
|
if(mDestroyed) {
|
2010-11-04 14:32:19 -07:00
|
|
|
throw new RSInvalidStateException("Object already destroyed.");
|
2009-08-18 14:14:24 -07:00
|
|
|
}
|
|
|
|
mDestroyed = true;
|
|
|
|
mRS.nObjDestroy(mID);
|
|
|
|
}
|
|
|
|
|
2012-04-19 16:30:58 -07:00
|
|
|
/** @deprecated renderscript is deprecated in J
|
2010-11-10 17:00:59 -08:00
|
|
|
* If an object came from an a3d file, java fields need to be
|
|
|
|
* created with objects from the native layer
|
|
|
|
*/
|
2010-07-12 15:50:32 -07:00
|
|
|
void updateFromNative() {
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.validate();
|
2012-04-03 15:36:36 -07:00
|
|
|
mName = mRS.nGetName(getID(mRS));
|
2010-07-12 15:50:32 -07:00
|
|
|
}
|
|
|
|
|
2012-04-19 16:30:58 -07:00
|
|
|
/** @deprecated renderscript is deprecated in J
|
2011-06-09 10:11:54 -07:00
|
|
|
* Calculates the hash code value for a BaseObj.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return mID;
|
|
|
|
}
|
|
|
|
|
2012-04-19 16:30:58 -07:00
|
|
|
/** @deprecated renderscript is deprecated in J
|
2011-06-09 10:11:54 -07:00
|
|
|
* Compare the current BaseObj with another BaseObj for equality.
|
|
|
|
*
|
|
|
|
* @param obj The object to check equality with.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
// Early-out check to see if both BaseObjs are actually the same
|
|
|
|
if (this == obj)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (getClass() != obj.getClass()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
BaseObj b = (BaseObj) obj;
|
|
|
|
return mID == b.mID;
|
|
|
|
}
|
2009-07-31 16:26:13 -07:00
|
|
|
}
|
|
|
|
|