blob: f8657531cf67444a81617b2c6989e9f4ac29bf0f [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 /**
Jason Sams67e3d202011-01-09 13:49:01 -080039 * Only intended for use by the generated derived classes.
40 *
41 * @param id
42 * @param rs
43 */
Jason Sams3ba02b32010-11-03 23:01:38 -070044 protected ScriptC(int id, RenderScript rs) {
Jason Sams69f0d312009-08-03 18:11:17 -070045 super(id, rs);
46 }
47
Jason Sams67e3d202011-01-09 13:49:01 -080048 /**
Jason Sams67e3d202011-01-09 13:49:01 -080049 * Only intended for use by the generated derived classes.
50 *
51 *
52 * @param rs
53 * @param resources
54 * @param resourceID
55 */
Jason Sams3ba02b32010-11-03 23:01:38 -070056 protected ScriptC(RenderScript rs, Resources resources, int resourceID) {
Jason Sams2d71bc72010-03-26 16:06:43 -070057 super(0, rs);
Jason Sams06d69de2010-11-09 17:11:40 -080058 int id = internalCreate(rs, resources, resourceID);
Jason Samsfdc54a92011-01-19 16:14:21 -080059 if (id == 0) {
60 throw new RSRuntimeException("Loading of ScriptC script failed.");
61 }
Jason Sams06d69de2010-11-09 17:11:40 -080062 setID(id);
Jason Sams2d71bc72010-03-26 16:06:43 -070063 }
64
65
Jason Sams1de0b872010-05-17 14:55:34 -070066 private static synchronized int internalCreate(RenderScript rs, Resources resources, int resourceID) {
Jason Sams2d71bc72010-03-26 16:06:43 -070067 byte[] pgm;
68 int pgmLength;
69 InputStream is = resources.openRawResource(resourceID);
70 try {
71 try {
72 pgm = new byte[1024];
73 pgmLength = 0;
74 while(true) {
75 int bytesLeft = pgm.length - pgmLength;
76 if (bytesLeft == 0) {
77 byte[] buf2 = new byte[pgm.length * 2];
78 System.arraycopy(pgm, 0, buf2, 0, pgm.length);
79 pgm = buf2;
80 bytesLeft = pgm.length - pgmLength;
81 }
82 int bytesRead = is.read(pgm, pgmLength, bytesLeft);
83 if (bytesRead <= 0) {
84 break;
85 }
86 pgmLength += bytesRead;
87 }
88 } finally {
89 is.close();
90 }
91 } catch(IOException e) {
92 throw new Resources.NotFoundException();
93 }
94
Shih-wei Liaoeeca4352010-12-20 20:45:56 +080095 // E.g, /system/apps/Fountain.apk
Jason Samse4a06c52011-03-16 16:29:28 -070096 //String packageName = rs.getApplicationContext().getPackageResourcePath();
Shih-wei Liaoeeca4352010-12-20 20:45:56 +080097 // For res/raw/fountain.bc, it wil be /com.android.fountain:raw/fountain
98 String resName = resources.getResourceName(resourceID);
Shih-wei Liao6b32fab2010-12-10 01:03:59 -080099 String cacheDir = rs.getApplicationContext().getCacheDir().toString();
Shih-wei Liaoeeca4352010-12-20 20:45:56 +0800100
101 Log.v(TAG, "Create script for resource = " + resName);
Jason Samse4a06c52011-03-16 16:29:28 -0700102 return rs.nScriptCCreate(resName, cacheDir, pgm, pgmLength);
Jason Sams2d71bc72010-03-26 16:06:43 -0700103 }
Jason Sams69f0d312009-08-03 18:11:17 -0700104}