blob: c1e4358dd2c54e95c7a288f822a682ae823ad6ff [file] [log] [blame]
Jason Sams69f0d312009-08-03 18:11:17 -07001/*
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
17package android.renderscript;
18
Shih-wei Liao6b32fab2010-12-10 01:03:59 -080019import android.content.Context;
Joe Onoratod7b37742009-08-09 22:57:44 -070020import android.content.res.Resources;
21import android.util.Log;
22
Shih-wei Liao6b32fab2010-12-10 01:03:59 -080023import java.io.File;
Jason Sams69f0d312009-08-03 18:11:17 -070024import java.io.IOException;
25import java.io.InputStream;
Joe Onoratod7b37742009-08-09 22:57:44 -070026import java.util.Map.Entry;
27import java.util.HashMap;
Jason Sams69f0d312009-08-03 18:11:17 -070028
Joe Onoratof415cf22009-08-10 15:15:52 -070029import java.lang.reflect.Field;
30import java.lang.reflect.Modifier;
31
Jason Sams69f0d312009-08-03 18:11:17 -070032/**
33 * @hide
34 **/
35public class ScriptC extends Script {
Joe Onoratof415cf22009-08-10 15:15:52 -070036 private static final String TAG = "ScriptC";
37
Jason Sams3ba02b32010-11-03 23:01:38 -070038 protected ScriptC(int id, RenderScript rs) {
Jason Sams69f0d312009-08-03 18:11:17 -070039 super(id, rs);
40 }
41
Jason Sams3ba02b32010-11-03 23:01:38 -070042 protected ScriptC(RenderScript rs, Resources resources, int resourceID) {
Jason Sams2d71bc72010-03-26 16:06:43 -070043 super(0, rs);
Jason Sams06d69de2010-11-09 17:11:40 -080044 int id = internalCreate(rs, resources, resourceID);
45 setID(id);
Jason Sams2d71bc72010-03-26 16:06:43 -070046 }
47
48
Jason Sams1de0b872010-05-17 14:55:34 -070049 private static synchronized int internalCreate(RenderScript rs, Resources resources, int resourceID) {
Jason Sams2d71bc72010-03-26 16:06:43 -070050 byte[] pgm;
51 int pgmLength;
52 InputStream is = resources.openRawResource(resourceID);
53 try {
54 try {
55 pgm = new byte[1024];
56 pgmLength = 0;
57 while(true) {
58 int bytesLeft = pgm.length - pgmLength;
59 if (bytesLeft == 0) {
60 byte[] buf2 = new byte[pgm.length * 2];
61 System.arraycopy(pgm, 0, buf2, 0, pgm.length);
62 pgm = buf2;
63 bytesLeft = pgm.length - pgmLength;
64 }
65 int bytesRead = is.read(pgm, pgmLength, bytesLeft);
66 if (bytesRead <= 0) {
67 break;
68 }
69 pgmLength += bytesRead;
70 }
71 } finally {
72 is.close();
73 }
74 } catch(IOException e) {
75 throw new Resources.NotFoundException();
76 }
77
78 rs.nScriptCBegin();
79 rs.nScriptCSetScript(pgm, 0, pgmLength);
Shih-wei Liaoeeca4352010-12-20 20:45:56 +080080
81 // E.g, /system/apps/Fountain.apk
82 String packageName = rs.getApplicationContext().getPackageResourcePath();
83 // For res/raw/fountain.bc, it wil be /com.android.fountain:raw/fountain
84 String resName = resources.getResourceName(resourceID);
Shih-wei Liao6b32fab2010-12-10 01:03:59 -080085 String cacheDir = rs.getApplicationContext().getCacheDir().toString();
Shih-wei Liaoeeca4352010-12-20 20:45:56 +080086
87 Log.v(TAG, "Create script for resource = " + resName);
88 return rs.nScriptCCreate(packageName, resName, cacheDir);
Jason Sams2d71bc72010-03-26 16:06:43 -070089 }
Jason Sams69f0d312009-08-03 18:11:17 -070090}