2009-08-03 18:11:17 -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;
|
|
|
|
|
2010-12-10 01:03:59 -08:00
|
|
|
import android.content.Context;
|
2009-08-03 18:11:17 -07:00
|
|
|
import android.content.res.Resources;
|
2009-08-09 22:57:44 -07:00
|
|
|
import android.util.Log;
|
2009-08-03 18:11:17 -07:00
|
|
|
|
2010-12-10 01:03:59 -08:00
|
|
|
import java.io.File;
|
2009-08-09 22:57:44 -07:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
import java.util.HashMap;
|
2009-08-03 18:11:17 -07:00
|
|
|
|
2009-08-10 15:15:52 -07:00
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.lang.reflect.Modifier;
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* The superclass for all user-defined scripts. This is only
|
|
|
|
* intended to be used by the generated derived classes.
|
2009-08-03 18:11:17 -07:00
|
|
|
**/
|
|
|
|
public class ScriptC extends Script {
|
2009-08-10 15:15:52 -07:00
|
|
|
private static final String TAG = "ScriptC";
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2011-01-09 13:49:01 -08:00
|
|
|
* Only intended for use by the generated derived classes.
|
|
|
|
*
|
|
|
|
* @param id
|
|
|
|
* @param rs
|
|
|
|
*/
|
2010-11-03 23:01:38 -07:00
|
|
|
protected ScriptC(int id, RenderScript rs) {
|
2009-08-03 18:11:17 -07:00
|
|
|
super(id, rs);
|
|
|
|
}
|
2014-02-04 14:57:58 +00:00
|
|
|
/**
|
|
|
|
* Only intended for use by the generated derived classes.
|
|
|
|
*
|
|
|
|
* @param id
|
|
|
|
* @param rs
|
|
|
|
*
|
|
|
|
* @hide
|
|
|
|
*/
|
|
|
|
protected ScriptC(long id, RenderScript rs) {
|
|
|
|
super(id, rs);
|
|
|
|
}
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2011-01-09 13:49:01 -08:00
|
|
|
* Only intended for use by the generated derived classes.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param rs
|
|
|
|
* @param resources
|
|
|
|
* @param resourceID
|
|
|
|
*/
|
2010-11-03 23:01:38 -07:00
|
|
|
protected ScriptC(RenderScript rs, Resources resources, int resourceID) {
|
2010-03-26 16:06:43 -07:00
|
|
|
super(0, rs);
|
2014-02-04 14:57:58 +00:00
|
|
|
long id = internalCreate(rs, resources, resourceID);
|
2011-01-19 16:14:21 -08:00
|
|
|
if (id == 0) {
|
|
|
|
throw new RSRuntimeException("Loading of ScriptC script failed.");
|
|
|
|
}
|
2010-11-09 17:11:40 -08:00
|
|
|
setID(id);
|
2010-03-26 16:06:43 -07:00
|
|
|
}
|
|
|
|
|
2013-04-09 23:51:56 -07:00
|
|
|
/**
|
|
|
|
* Name of the file that holds the object cache.
|
|
|
|
*/
|
|
|
|
private static final String CACHE_PATH = "com.android.renderscript.cache";
|
|
|
|
|
|
|
|
static String mCachePath;
|
2010-03-26 16:06:43 -07:00
|
|
|
|
2014-02-04 14:57:58 +00:00
|
|
|
private static synchronized long internalCreate(RenderScript rs, Resources resources, int resourceID) {
|
2010-03-26 16:06:43 -07:00
|
|
|
byte[] pgm;
|
|
|
|
int pgmLength;
|
|
|
|
InputStream is = resources.openRawResource(resourceID);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
pgm = new byte[1024];
|
|
|
|
pgmLength = 0;
|
|
|
|
while(true) {
|
|
|
|
int bytesLeft = pgm.length - pgmLength;
|
|
|
|
if (bytesLeft == 0) {
|
|
|
|
byte[] buf2 = new byte[pgm.length * 2];
|
|
|
|
System.arraycopy(pgm, 0, buf2, 0, pgm.length);
|
|
|
|
pgm = buf2;
|
|
|
|
bytesLeft = pgm.length - pgmLength;
|
|
|
|
}
|
|
|
|
int bytesRead = is.read(pgm, pgmLength, bytesLeft);
|
|
|
|
if (bytesRead <= 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pgmLength += bytesRead;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
is.close();
|
|
|
|
}
|
|
|
|
} catch(IOException e) {
|
|
|
|
throw new Resources.NotFoundException();
|
|
|
|
}
|
|
|
|
|
2011-07-11 15:32:24 +08:00
|
|
|
String resName = resources.getResourceEntryName(resourceID);
|
2010-12-20 20:45:56 +08:00
|
|
|
|
2013-04-09 23:51:56 -07:00
|
|
|
// Create the RS cache path if we haven't done so already.
|
|
|
|
if (mCachePath == null) {
|
|
|
|
File f = new File(rs.mCacheDir, CACHE_PATH);
|
|
|
|
mCachePath = f.getAbsolutePath();
|
|
|
|
f.mkdirs();
|
|
|
|
}
|
2013-05-09 12:02:50 -07:00
|
|
|
// Log.v(TAG, "Create script for resource = " + resName);
|
2013-04-09 23:51:56 -07:00
|
|
|
return rs.nScriptCCreate(resName, mCachePath, pgm, pgmLength);
|
2010-03-26 16:06:43 -07:00
|
|
|
}
|
2009-08-03 18:11:17 -07:00
|
|
|
}
|