ScriptC.java revision c89ad4606ae29e103b85bdfc40c57e36c8877dba
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 * 34 **/ 35public class ScriptC extends Script { 36 private static final String TAG = "ScriptC"; 37 38 /** 39 * Only intended for use by the generated derived classes. 40 * 41 * @param id 42 * @param rs 43 */ 44 protected ScriptC(int id, RenderScript rs) { 45 super(id, rs); 46 } 47 48 /** 49 * Only intended for use by the generated derived classes. 50 * 51 * 52 * @param rs 53 * @param resources 54 * @param resourceID 55 */ 56 protected ScriptC(RenderScript rs, Resources resources, int resourceID) { 57 super(0, rs); 58 59 if (rs.isNative) { 60 RenderScriptThunker rst = (RenderScriptThunker)rs; 61 ScriptCThunker s = new ScriptCThunker(rst, resources, resourceID); 62 mT = s; 63 return; 64 } 65 66 int id = internalCreate(rs, resources, resourceID); 67 if (id == 0) { 68 throw new RSRuntimeException("Loading of ScriptC script failed."); 69 } 70 setID(id); 71 } 72 73 74 private static synchronized int internalCreate(RenderScript rs, Resources resources, int resourceID) { 75 byte[] pgm; 76 int pgmLength; 77 InputStream is = resources.openRawResource(resourceID); 78 try { 79 try { 80 pgm = new byte[1024]; 81 pgmLength = 0; 82 while(true) { 83 int bytesLeft = pgm.length - pgmLength; 84 if (bytesLeft == 0) { 85 byte[] buf2 = new byte[pgm.length * 2]; 86 System.arraycopy(pgm, 0, buf2, 0, pgm.length); 87 pgm = buf2; 88 bytesLeft = pgm.length - pgmLength; 89 } 90 int bytesRead = is.read(pgm, pgmLength, bytesLeft); 91 if (bytesRead <= 0) { 92 break; 93 } 94 pgmLength += bytesRead; 95 } 96 } finally { 97 is.close(); 98 } 99 } catch(IOException e) { 100 throw new Resources.NotFoundException(); 101 } 102 103 String resName = resources.getResourceEntryName(resourceID); 104 String cachePath = rs.getApplicationContext().getCacheDir().toString(); 105 106 // Log.v(TAG, "Create script for resource = " + resName + ", " + pgmLength + ", " + pgm); 107 //Log.v(TAG, " path = " + cachePath); 108 return rs.nScriptCCreate(resName, cachePath, pgm, pgmLength); 109 } 110} 111