1 /* 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 17 package android.renderscript; 18 19 import android.content.Context; 20 import android.content.res.Resources; 21 import android.util.Log; 22 23 import java.io.File; 24 import java.io.IOException; 25 import java.io.InputStream; 26 import java.util.Map.Entry; 27 import java.util.HashMap; 28 29 import java.lang.reflect.Field; 30 import java.lang.reflect.Modifier; 31 32 /** 33 * The superclass for all user-defined scripts. This is only 34 * intended to be used by the generated derived classes. 35 **/ 36 public class ScriptC extends Script { 37 private static final String TAG = "ScriptC"; 38 39 /** 40 * Only intended for use by the generated derived classes. 41 * 42 * @param id 43 * @param rs 44 */ 45 protected ScriptC(int id, RenderScript rs) { 46 super(id, rs); 47 } 48 49 /** 50 * Only intended for use by the generated derived classes. 51 * 52 * 53 * @param rs 54 * @param resources 55 * @param resourceID 56 */ 57 protected ScriptC(RenderScript rs, Resources resources, int resourceID) { 58 super(0, rs); 59 int id = internalCreate(rs, resources, resourceID); 60 if (id == 0) { 61 throw new RSRuntimeException("Loading of ScriptC script failed."); 62 } 63 setID(id); 64 } 65 66 /** 67 * Name of the file that holds the object cache. 68 */ 69 private static final String CACHE_PATH = "com.android.renderscript.cache"; 70 71 static String mCachePath; 72 73 private static synchronized int internalCreate(RenderScript rs, Resources resources, int resourceID) { 74 byte[] pgm; 75 int pgmLength; 76 InputStream is = resources.openRawResource(resourceID); 77 try { 78 try { 79 pgm = new byte[1024]; 80 pgmLength = 0; 81 while(true) { 82 int bytesLeft = pgm.length - pgmLength; 83 if (bytesLeft == 0) { 84 byte[] buf2 = new byte[pgm.length * 2]; 85 System.arraycopy(pgm, 0, buf2, 0, pgm.length); 86 pgm = buf2; 87 bytesLeft = pgm.length - pgmLength; 88 } 89 int bytesRead = is.read(pgm, pgmLength, bytesLeft); 90 if (bytesRead <= 0) { 91 break; 92 } 93 pgmLength += bytesRead; 94 } 95 } finally { 96 is.close(); 97 } 98 } catch(IOException e) { 99 throw new Resources.NotFoundException(); 100 } 101 102 String resName = resources.getResourceEntryName(resourceID); 103 104 // Create the RS cache path if we haven't done so already. 105 if (mCachePath == null) { 106 File f = new File(rs.mCacheDir, CACHE_PATH); 107 mCachePath = f.getAbsolutePath(); 108 f.mkdirs(); 109 } 110 // Log.v(TAG, "Create script for resource = " + resName); 111 return rs.nScriptCCreate(resName, mCachePath, pgm, pgmLength); 112 } 113 } 114