• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *
34  **/
35 public 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      */
ScriptC(int id, RenderScript rs)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      */
ScriptC(RenderScript rs, Resources resources, int resourceID)56     protected ScriptC(RenderScript rs, Resources resources, int resourceID) {
57         super(0, rs);
58         int id = internalCreate(rs, resources, resourceID);
59         if (id == 0) {
60             throw new RSRuntimeException("Loading of ScriptC script failed.");
61         }
62         setID(id);
63     }
64 
65     /**
66      * Name of the file that holds the object cache.
67      */
68     private static final String CACHE_PATH = "com.android.renderscript.cache";
69 
70     static String mCachePath;
71 
internalCreate(RenderScript rs, Resources resources, int resourceID)72     private static synchronized int internalCreate(RenderScript rs, Resources resources, int resourceID) {
73         byte[] pgm;
74         int pgmLength;
75         InputStream is = resources.openRawResource(resourceID);
76         try {
77             try {
78                 pgm = new byte[1024];
79                 pgmLength = 0;
80                 while(true) {
81                     int bytesLeft = pgm.length - pgmLength;
82                     if (bytesLeft == 0) {
83                         byte[] buf2 = new byte[pgm.length * 2];
84                         System.arraycopy(pgm, 0, buf2, 0, pgm.length);
85                         pgm = buf2;
86                         bytesLeft = pgm.length - pgmLength;
87                     }
88                     int bytesRead = is.read(pgm, pgmLength, bytesLeft);
89                     if (bytesRead <= 0) {
90                         break;
91                     }
92                     pgmLength += bytesRead;
93                 }
94             } finally {
95                 is.close();
96             }
97         } catch(IOException e) {
98             throw new Resources.NotFoundException();
99         }
100 
101         String resName = resources.getResourceEntryName(resourceID);
102 
103         // Create the RS cache path if we haven't done so already.
104         if (mCachePath == null) {
105             File f = new File(rs.mCacheDir, CACHE_PATH);
106             mCachePath = f.getAbsolutePath();
107             f.mkdirs();
108         }
109         //        Log.v(TAG, "Create script for resource = " + resName);
110         return rs.nScriptCCreate(resName, mCachePath, pgm, pgmLength);
111     }
112 }
113