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;
|
|
|
|
|
2014-01-07 11:13:56 -08:00
|
|
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
2009-07-31 16:26:13 -07:00
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* BaseObj is the base class for all RenderScript objects owned by a RS context.
|
|
|
|
* It is responsible for lifetime management and resource tracking. This class
|
|
|
|
* should not be used by a user application.
|
2010-11-09 17:11:40 -08:00
|
|
|
*
|
2009-07-31 16:26:13 -07:00
|
|
|
**/
|
2011-03-31 14:45:36 -07:00
|
|
|
public class BaseObj {
|
2013-11-19 12:45:54 -08:00
|
|
|
BaseObj(long 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
|
|
|
}
|
|
|
|
|
2014-02-04 14:57:58 +00:00
|
|
|
void setID(long id) {
|
2010-11-09 17:11:40 -08:00
|
|
|
if (mID != 0) {
|
|
|
|
throw new RSRuntimeException("Internal Error, reset of object ID.");
|
|
|
|
}
|
|
|
|
mID = id;
|
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
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
|
|
|
*
|
2013-11-19 12:45:54 -08:00
|
|
|
* @return long
|
2010-11-10 17:00:59 -08:00
|
|
|
*/
|
2013-11-19 12:45:54 -08:00
|
|
|
long getID(RenderScript rs) {
|
2012-04-03 15:36:36 -07:00
|
|
|
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.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-19 12:45:54 -08:00
|
|
|
private long mID;
|
2010-11-09 17:11:40 -08:00
|
|
|
private boolean mDestroyed;
|
|
|
|
private String mName;
|
2009-07-31 16:26:13 -07:00
|
|
|
RenderScript mRS;
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2010-11-10 17:00:59 -08:00
|
|
|
* setName assigns a name to an object. This object can later be looked up
|
2013-04-09 11:01:01 -07:00
|
|
|
* by this name.
|
2010-11-10 17:00:59 -08:00
|
|
|
*
|
|
|
|
* @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-05-07 15:34:29 -07:00
|
|
|
/**
|
2011-07-26 14:13:32 -07:00
|
|
|
* @return name of the renderscript object
|
|
|
|
*/
|
|
|
|
public String getName() {
|
|
|
|
return mName;
|
|
|
|
}
|
|
|
|
|
2014-01-07 11:13:56 -08:00
|
|
|
private void helpDestroy() {
|
|
|
|
boolean shouldDestroy = false;
|
|
|
|
synchronized(this) {
|
|
|
|
if (!mDestroyed) {
|
|
|
|
shouldDestroy = true;
|
|
|
|
mDestroyed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldDestroy) {
|
|
|
|
// must include nObjDestroy in the critical section
|
|
|
|
ReentrantReadWriteLock.ReadLock rlock = mRS.mRWLock.readLock();
|
|
|
|
rlock.lock();
|
2014-02-12 11:16:17 -08:00
|
|
|
// AllocationAdapters are BaseObjs with an ID of 0 but should not be passed to nObjDestroy
|
|
|
|
if(mRS.isAlive() && mID != 0) {
|
2010-08-17 19:28:29 -07:00
|
|
|
mRS.nObjDestroy(mID);
|
2009-08-18 17:07:09 -07:00
|
|
|
}
|
2014-01-07 11:13:56 -08:00
|
|
|
rlock.unlock();
|
2009-09-25 14:51:22 -07:00
|
|
|
mRS = null;
|
2009-08-18 17:07:09 -07:00
|
|
|
mID = 0;
|
2009-07-31 16:26:13 -07:00
|
|
|
}
|
2014-01-07 11:13:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void finalize() throws Throwable {
|
|
|
|
helpDestroy();
|
2009-07-31 16:26:13 -07:00
|
|
|
super.finalize();
|
|
|
|
}
|
2009-08-18 14:14:24 -07:00
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Frees any native resources associated with this object. The
|
|
|
|
* primary use is to force immediate cleanup of resources when it is
|
|
|
|
* believed the GC will not respond quickly enough.
|
2010-11-10 17:00:59 -08:00
|
|
|
*/
|
2014-01-07 11:13:56 -08:00
|
|
|
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
|
|
|
}
|
2014-01-07 11:13:56 -08:00
|
|
|
helpDestroy();
|
2009-08-18 14:14:24 -07:00
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
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-05-07 15:34:29 -07:00
|
|
|
/**
|
2011-06-09 10:11:54 -07:00
|
|
|
* Calculates the hash code value for a BaseObj.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
2013-11-19 12:45:54 -08:00
|
|
|
return (int)((mID & 0xfffffff) ^ (mID >> 32));
|
2011-06-09 10:11:54 -07:00
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
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
|
|
|
}
|
|
|
|
|