Home | History | Annotate | Download | only in renderscript
      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.res.Resources;
     20 
     21 import java.io.File;
     22 import java.io.IOException;
     23 import java.io.InputStream;
     24 
     25 /**
     26  * The superclass for all user-defined scripts. This is only
     27  * intended to be used by the generated derived classes.
     28  **/
     29 public class ScriptC extends Script {
     30     private static final String TAG = "ScriptC";
     31 
     32     /**
     33      * Only intended for use by the generated derived classes.
     34      *
     35      * @param id
     36      * @param rs
     37      */
     38     protected ScriptC(int id, RenderScript rs) {
     39         super(id, rs);
     40     }
     41     /**
     42      * Only intended for use by the generated derived classes.
     43      *
     44      * @param id
     45      * @param rs
     46      *
     47      */
     48     protected ScriptC(long id, RenderScript rs) {
     49         super(id, rs);
     50     }
     51     /**
     52      * Only intended for use by the generated derived classes.
     53      *
     54      *
     55      * @param rs
     56      * @param resources
     57      * @param resourceID
     58      */
     59     protected ScriptC(RenderScript rs, Resources resources, int resourceID) {
     60         super(0, rs);
     61         long id = internalCreate(rs, resources, resourceID);
     62         if (id == 0) {
     63             throw new RSRuntimeException("Loading of ScriptC script failed.");
     64         }
     65         setID(id);
     66     }
     67 
     68     /**
     69      * Only intended for use by the generated derived classes.
     70      *
     71      * @param rs
     72      */
     73     protected ScriptC(RenderScript rs, String resName, byte[] bitcode32, byte[] bitcode64) {
     74         super(0, rs);
     75         long id = 0;
     76         if (RenderScript.sPointerSize == 4) {
     77             id = internalStringCreate(rs, resName, bitcode32);
     78         } else {
     79             id = internalStringCreate(rs, resName, bitcode64);
     80         }
     81         if (id == 0) {
     82             throw new RSRuntimeException("Loading of ScriptC script failed.");
     83         }
     84         setID(id);
     85     }
     86 
     87     private static synchronized long internalCreate(RenderScript rs, Resources resources, int resourceID) {
     88         byte[] pgm;
     89         int pgmLength;
     90         InputStream is = resources.openRawResource(resourceID);
     91         try {
     92             try {
     93                 pgm = new byte[1024];
     94                 pgmLength = 0;
     95                 while(true) {
     96                     int bytesLeft = pgm.length - pgmLength;
     97                     if (bytesLeft == 0) {
     98                         byte[] buf2 = new byte[pgm.length * 2];
     99                         System.arraycopy(pgm, 0, buf2, 0, pgm.length);
    100                         pgm = buf2;
    101                         bytesLeft = pgm.length - pgmLength;
    102                     }
    103                     int bytesRead = is.read(pgm, pgmLength, bytesLeft);
    104                     if (bytesRead <= 0) {
    105                         break;
    106                     }
    107                     pgmLength += bytesRead;
    108                 }
    109             } finally {
    110                 is.close();
    111             }
    112         } catch(IOException e) {
    113             throw new Resources.NotFoundException();
    114         }
    115 
    116         String resName = resources.getResourceEntryName(resourceID);
    117 
    118         //        Log.v(TAG, "Create script for resource = " + resName);
    119         return rs.nScriptCCreate(resName, RenderScript.getCachePath(), pgm, pgmLength);
    120     }
    121 
    122     private static synchronized long internalStringCreate(RenderScript rs, String resName, byte[] bitcode) {
    123         //        Log.v(TAG, "Create script for resource = " + resName);
    124         return rs.nScriptCCreate(resName, RenderScript.getCachePath(), bitcode, bitcode.length);
    125     }
    126 }
    127