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;
|
|
|
|
|
2009-08-03 18:11:17 -07:00
|
|
|
/**
|
2011-01-04 18:59:12 -08:00
|
|
|
*
|
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";
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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);
|
2010-11-09 17:11:40 -08:00
|
|
|
int 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
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-17 14:55:34 -07:00
|
|
|
private static synchronized int 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();
|
|
|
|
}
|
|
|
|
|
2010-12-20 20:45:56 +08:00
|
|
|
// E.g, /system/apps/Fountain.apk
|
2011-03-16 16:29:28 -07:00
|
|
|
//String packageName = rs.getApplicationContext().getPackageResourcePath();
|
2010-12-20 20:45:56 +08:00
|
|
|
// For res/raw/fountain.bc, it wil be /com.android.fountain:raw/fountain
|
|
|
|
String resName = resources.getResourceName(resourceID);
|
2010-12-10 01:03:59 -08:00
|
|
|
String cacheDir = rs.getApplicationContext().getCacheDir().toString();
|
2010-12-20 20:45:56 +08:00
|
|
|
|
|
|
|
Log.v(TAG, "Create script for resource = " + resName);
|
2011-03-16 16:29:28 -07:00
|
|
|
return rs.nScriptCCreate(resName, cacheDir, pgm, pgmLength);
|
2010-03-26 16:06:43 -07:00
|
|
|
}
|
2009-08-03 18:11:17 -07:00
|
|
|
}
|