blob: 64ed75bcdc1af5cc03be41e900f9d3b87ada0fd0 [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
Joe Onoratod7b37742009-08-09 22:57:44 -070019import android.content.res.Resources;
20import android.util.Log;
21
Jason Sams69f0d312009-08-03 18:11:17 -070022import java.io.IOException;
23import java.io.InputStream;
Joe Onoratod7b37742009-08-09 22:57:44 -070024import java.util.Map.Entry;
25import java.util.HashMap;
Jason Sams69f0d312009-08-03 18:11:17 -070026
Joe Onoratof415cf22009-08-10 15:15:52 -070027import java.lang.reflect.Field;
28import java.lang.reflect.Modifier;
29
Jason Sams69f0d312009-08-03 18:11:17 -070030/**
31 * @hide
32 **/
33public class ScriptC extends Script {
Joe Onoratof415cf22009-08-10 15:15:52 -070034 private static final String TAG = "ScriptC";
35
Jason Sams3ba02b32010-11-03 23:01:38 -070036 protected ScriptC(int id, RenderScript rs) {
Jason Sams69f0d312009-08-03 18:11:17 -070037 super(id, rs);
38 }
39
Jason Sams3ba02b32010-11-03 23:01:38 -070040 protected ScriptC(RenderScript rs, Resources resources, int resourceID) {
Jason Sams2d71bc72010-03-26 16:06:43 -070041 super(0, rs);
Jason Sams06d69de2010-11-09 17:11:40 -080042 int id = internalCreate(rs, resources, resourceID);
43 setID(id);
Jason Sams2d71bc72010-03-26 16:06:43 -070044 }
45
46
Jason Sams1de0b872010-05-17 14:55:34 -070047 private static synchronized int internalCreate(RenderScript rs, Resources resources, int resourceID) {
Jason Sams2d71bc72010-03-26 16:06:43 -070048 byte[] pgm;
49 int pgmLength;
50 InputStream is = resources.openRawResource(resourceID);
51 try {
52 try {
53 pgm = new byte[1024];
54 pgmLength = 0;
55 while(true) {
56 int bytesLeft = pgm.length - pgmLength;
57 if (bytesLeft == 0) {
58 byte[] buf2 = new byte[pgm.length * 2];
59 System.arraycopy(pgm, 0, buf2, 0, pgm.length);
60 pgm = buf2;
61 bytesLeft = pgm.length - pgmLength;
62 }
63 int bytesRead = is.read(pgm, pgmLength, bytesLeft);
64 if (bytesRead <= 0) {
65 break;
66 }
67 pgmLength += bytesRead;
68 }
69 } finally {
70 is.close();
71 }
72 } catch(IOException e) {
73 throw new Resources.NotFoundException();
74 }
75
76 rs.nScriptCBegin();
77 rs.nScriptCSetScript(pgm, 0, pgmLength);
Shih-wei Liaoa914f342010-11-08 01:33:59 -080078 Log.v(TAG, "Create script for resource = " + resources.getResourceName(resourceID));
79 return rs.nScriptCCreate(resources.getResourceName(resourceID));
Jason Sams2d71bc72010-03-26 16:06:43 -070080 }
Jason Sams69f0d312009-08-03 18:11:17 -070081}