1/* 2 * Copyright (C) 2012 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 17package android.support.v8.renderscript; 18 19import android.content.Context; 20import android.content.res.Resources; 21import android.util.Log; 22 23import java.io.File; 24import java.io.IOException; 25import java.io.InputStream; 26import java.util.Map.Entry; 27import java.util.HashMap; 28 29import java.lang.reflect.Field; 30import 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 **/ 36public 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(long 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 long 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 * Only intended for use by the generated derived classes. 68 * 69 * @param rs 70 * @param resName 71 * @param bitcode32 72 * @param bitcode64 73 */ 74 protected ScriptC(RenderScript rs, String resName, byte[] bitcode32, byte[] bitcode64) { 75 super(0, rs); 76 long id = 0; 77 if (RenderScript.sPointerSize == 4) { 78 id = internalStringCreate(rs, resName, bitcode32); 79 } else { 80 id = internalStringCreate(rs, resName, bitcode64); 81 } 82 if (id == 0) { 83 throw new RSRuntimeException("Loading of ScriptC script failed."); 84 } 85 setID(id); 86 } 87 88 private static synchronized long internalCreate(RenderScript rs, Resources resources, int resourceID) { 89 byte[] pgm; 90 int pgmLength; 91 InputStream is = resources.openRawResource(resourceID); 92 try { 93 try { 94 pgm = new byte[1024]; 95 pgmLength = 0; 96 while(true) { 97 int bytesLeft = pgm.length - pgmLength; 98 if (bytesLeft == 0) { 99 byte[] buf2 = new byte[pgm.length * 2]; 100 System.arraycopy(pgm, 0, buf2, 0, pgm.length); 101 pgm = buf2; 102 bytesLeft = pgm.length - pgmLength; 103 } 104 int bytesRead = is.read(pgm, pgmLength, bytesLeft); 105 if (bytesRead <= 0) { 106 break; 107 } 108 pgmLength += bytesRead; 109 } 110 } finally { 111 is.close(); 112 } 113 } catch(IOException e) { 114 throw new Resources.NotFoundException(); 115 } 116 117 String resName = resources.getResourceEntryName(resourceID); 118 String cachePath = rs.getApplicationContext().getCacheDir().toString(); 119 120 // Log.v(TAG, "Create script for resource = " + resName + ", " + pgmLength + ", " + pgm); 121 //Log.v(TAG, " path = " + cachePath); 122 return rs.nScriptCCreate(resName, cachePath, pgm, pgmLength); 123 } 124 125 private static synchronized long internalStringCreate(RenderScript rs, String resName, byte[] bitcode) { 126 // Log.v(TAG, "Create script for resource = " + resName); 127 String cachePath = rs.getApplicationContext().getCacheDir().toString(); 128 return rs.nScriptCCreate(resName, cachePath, bitcode, bitcode.length); 129 } 130 131} 132