blob: 14e4ab5a00f2b54cacf9e784e35767b3909e8693 [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/**
Jason Samsa23d4e72011-01-04 18:59:12 -080033 *
Jason Sams69f0d312009-08-03 18:11:17 -070034 **/
35public class ScriptC extends Script {
Joe Onoratof415cf22009-08-10 15:15:52 -070036 private static final String TAG = "ScriptC";
37
Jason Sams67e3d202011-01-09 13:49:01 -080038 /**
39 * @hide
40 *
41 * Only intended for use by the generated derived classes.
42 *
43 * @param id
44 * @param rs
45 */
Jason Sams3ba02b32010-11-03 23:01:38 -070046 protected ScriptC(int id, RenderScript rs) {
Jason Sams69f0d312009-08-03 18:11:17 -070047 super(id, rs);
48 }
49
Jason Sams67e3d202011-01-09 13:49:01 -080050 /**
51 * @hide
52 *
53 * Only intended for use by the generated derived classes.
54 *
55 *
56 * @param rs
57 * @param resources
58 * @param resourceID
59 */
Jason Sams3ba02b32010-11-03 23:01:38 -070060 protected ScriptC(RenderScript rs, Resources resources, int resourceID) {
Jason Sams2d71bc72010-03-26 16:06:43 -070061 super(0, rs);
Jason Sams06d69de2010-11-09 17:11:40 -080062 int id = internalCreate(rs, resources, resourceID);
63 setID(id);
Jason Sams2d71bc72010-03-26 16:06:43 -070064 }
65
66
Jason Sams1de0b872010-05-17 14:55:34 -070067 private static synchronized int internalCreate(RenderScript rs, Resources resources, int resourceID) {
Jason Sams2d71bc72010-03-26 16:06:43 -070068 byte[] pgm;
69 int pgmLength;
70 InputStream is = resources.openRawResource(resourceID);
71 try {
72 try {
73 pgm = new byte[1024];
74 pgmLength = 0;
75 while(true) {
76 int bytesLeft = pgm.length - pgmLength;
77 if (bytesLeft == 0) {
78 byte[] buf2 = new byte[pgm.length * 2];
79 System.arraycopy(pgm, 0, buf2, 0, pgm.length);
80 pgm = buf2;
81 bytesLeft = pgm.length - pgmLength;
82 }
83 int bytesRead = is.read(pgm, pgmLength, bytesLeft);
84 if (bytesRead <= 0) {
85 break;
86 }
87 pgmLength += bytesRead;
88 }
89 } finally {
90 is.close();
91 }
92 } catch(IOException e) {
93 throw new Resources.NotFoundException();
94 }
95
96 rs.nScriptCBegin();
97 rs.nScriptCSetScript(pgm, 0, pgmLength);
Shih-wei Liaoeeca4352010-12-20 20:45:56 +080098
99 // E.g, /system/apps/Fountain.apk
100 String packageName = rs.getApplicationContext().getPackageResourcePath();
101 // For res/raw/fountain.bc, it wil be /com.android.fountain:raw/fountain
102 String resName = resources.getResourceName(resourceID);
Shih-wei Liao6b32fab2010-12-10 01:03:59 -0800103 String cacheDir = rs.getApplicationContext().getCacheDir().toString();
Shih-wei Liaoeeca4352010-12-20 20:45:56 +0800104
105 Log.v(TAG, "Create script for resource = " + resName);
106 return rs.nScriptCCreate(packageName, resName, cacheDir);
Jason Sams2d71bc72010-03-26 16:06:43 -0700107 }
Jason Sams69f0d312009-08-03 18:11:17 -0700108}