blob: 5959be4412f1e1b8d6e3234f8387b6271d43bc44 [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 Sams69f0d312009-08-03 18:11:17 -070036 ScriptC(int id, RenderScript rs) {
37 super(id, rs);
38 }
39
Jason Sams2d71bc72010-03-26 16:06:43 -070040 protected ScriptC(RenderScript rs, Resources resources, int resourceID, boolean isRoot) {
41 super(0, rs);
Jason Sams1de0b872010-05-17 14:55:34 -070042 mID = internalCreate(rs, resources, resourceID);
Jason Sams2d71bc72010-03-26 16:06:43 -070043 }
44
45
Jason Sams1de0b872010-05-17 14:55:34 -070046 private static synchronized int internalCreate(RenderScript rs, Resources resources, int resourceID) {
Jason Sams2d71bc72010-03-26 16:06:43 -070047 byte[] pgm;
48 int pgmLength;
49 InputStream is = resources.openRawResource(resourceID);
50 try {
51 try {
52 pgm = new byte[1024];
53 pgmLength = 0;
54 while(true) {
55 int bytesLeft = pgm.length - pgmLength;
56 if (bytesLeft == 0) {
57 byte[] buf2 = new byte[pgm.length * 2];
58 System.arraycopy(pgm, 0, buf2, 0, pgm.length);
59 pgm = buf2;
60 bytesLeft = pgm.length - pgmLength;
61 }
62 int bytesRead = is.read(pgm, pgmLength, bytesLeft);
63 if (bytesRead <= 0) {
64 break;
65 }
66 pgmLength += bytesRead;
67 }
68 } finally {
69 is.close();
70 }
71 } catch(IOException e) {
72 throw new Resources.NotFoundException();
73 }
74
75 rs.nScriptCBegin();
76 rs.nScriptCSetScript(pgm, 0, pgmLength);
Jason Sams2d71bc72010-03-26 16:06:43 -070077 return rs.nScriptCCreate();
78 }
79
Jason Sams69f0d312009-08-03 18:11:17 -070080 public static class Builder extends Script.Builder {
81 byte[] mProgram;
82 int mProgramLength;
83
84 public Builder(RenderScript rs) {
85 super(rs);
86 }
87
88 public void setScript(String s) {
89 try {
90 mProgram = s.getBytes("UTF-8");
91 mProgramLength = mProgram.length;
92 } catch (java.io.UnsupportedEncodingException e) {
93 throw new RuntimeException(e);
94 }
95 }
96
97 public void setScript(Resources resources, int id) {
98 InputStream is = resources.openRawResource(id);
99 try {
100 try {
101 setScript(is);
102 } finally {
103 is.close();
104 }
105 } catch(IOException e) {
106 throw new Resources.NotFoundException();
107 }
108 }
109
Joe Onoratod7b37742009-08-09 22:57:44 -0700110 public void setScript(InputStream is) throws IOException {
Jason Sams69f0d312009-08-03 18:11:17 -0700111 byte[] buf = new byte[1024];
112 int currentPos = 0;
113 while(true) {
114 int bytesLeft = buf.length - currentPos;
115 if (bytesLeft == 0) {
116 byte[] buf2 = new byte[buf.length * 2];
117 System.arraycopy(buf, 0, buf2, 0, buf.length);
118 buf = buf2;
119 bytesLeft = buf.length - currentPos;
120 }
121 int bytesRead = is.read(buf, currentPos, bytesLeft);
122 if (bytesRead <= 0) {
123 break;
124 }
125 currentPos += bytesRead;
126 }
127 mProgram = buf;
128 mProgramLength = currentPos;
129 }
130
131 static synchronized ScriptC internalCreate(Builder b) {
132 b.mRS.nScriptCBegin();
Jason Sams69f0d312009-08-03 18:11:17 -0700133
Jason Sams4d339932010-05-11 14:03:58 -0700134 android.util.Log.e("rs", "len = " + b.mProgramLength);
Joe Onoratod7b37742009-08-09 22:57:44 -0700135 b.mRS.nScriptCSetScript(b.mProgram, 0, b.mProgramLength);
Jason Sams69f0d312009-08-03 18:11:17 -0700136
137 int id = b.mRS.nScriptCCreate();
138 ScriptC obj = new ScriptC(id, b.mRS);
Jason Sams69f0d312009-08-03 18:11:17 -0700139 return obj;
140 }
141
Jason Sams4d339932010-05-11 14:03:58 -0700142 public void addDefine(String name, int value) {}
143 public void addDefine(String name, float value) {}
144 public void addDefines(Class cl) {}
145 public void addDefines(Object o) {}
146 void addDefines(Field[] fields, int mask, Object o) {}
Joe Onoratof415cf22009-08-10 15:15:52 -0700147
Jason Sams69f0d312009-08-03 18:11:17 -0700148 public ScriptC create() {
149 return internalCreate(this);
150 }
151 }
Jason Sams69f0d312009-08-03 18:11:17 -0700152}
153