blob: f5d5b2fa537097982f23372d54d7c1dcbd021c27 [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);
42 mID = internalCreate(rs, resources, resourceID, isRoot);
43 }
44
45
46 private static synchronized int internalCreate(RenderScript rs, Resources resources, int resourceID, boolean isRoot) {
47 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);
77 rs.nScriptSetRoot(isRoot);
78 return rs.nScriptCCreate();
79 }
80
Jason Sams69f0d312009-08-03 18:11:17 -070081 public static class Builder extends Script.Builder {
82 byte[] mProgram;
83 int mProgramLength;
Joe Onoratod7b37742009-08-09 22:57:44 -070084 HashMap<String,Integer> mIntDefines = new HashMap();
85 HashMap<String,Float> mFloatDefines = new HashMap();
Jason Sams69f0d312009-08-03 18:11:17 -070086
87 public Builder(RenderScript rs) {
88 super(rs);
89 }
90
91 public void setScript(String s) {
92 try {
93 mProgram = s.getBytes("UTF-8");
94 mProgramLength = mProgram.length;
95 } catch (java.io.UnsupportedEncodingException e) {
96 throw new RuntimeException(e);
97 }
98 }
99
100 public void setScript(Resources resources, int id) {
101 InputStream is = resources.openRawResource(id);
102 try {
103 try {
104 setScript(is);
105 } finally {
106 is.close();
107 }
108 } catch(IOException e) {
109 throw new Resources.NotFoundException();
110 }
111 }
112
Joe Onoratod7b37742009-08-09 22:57:44 -0700113 public void setScript(InputStream is) throws IOException {
Jason Sams69f0d312009-08-03 18:11:17 -0700114 byte[] buf = new byte[1024];
115 int currentPos = 0;
116 while(true) {
117 int bytesLeft = buf.length - currentPos;
118 if (bytesLeft == 0) {
119 byte[] buf2 = new byte[buf.length * 2];
120 System.arraycopy(buf, 0, buf2, 0, buf.length);
121 buf = buf2;
122 bytesLeft = buf.length - currentPos;
123 }
124 int bytesRead = is.read(buf, currentPos, bytesLeft);
125 if (bytesRead <= 0) {
126 break;
127 }
128 currentPos += bytesRead;
129 }
130 mProgram = buf;
131 mProgramLength = currentPos;
132 }
133
134 static synchronized ScriptC internalCreate(Builder b) {
135 b.mRS.nScriptCBegin();
136 b.transferCreate();
137
Joe Onoratod7b37742009-08-09 22:57:44 -0700138 for (Entry<String,Integer> e: b.mIntDefines.entrySet()) {
139 b.mRS.nScriptCAddDefineI32(e.getKey(), e.getValue().intValue());
140 }
141 for (Entry<String,Float> e: b.mFloatDefines.entrySet()) {
142 b.mRS.nScriptCAddDefineF(e.getKey(), e.getValue().floatValue());
143 }
Jason Sams69f0d312009-08-03 18:11:17 -0700144
Joe Onoratod7b37742009-08-09 22:57:44 -0700145 b.mRS.nScriptCSetScript(b.mProgram, 0, b.mProgramLength);
Jason Sams69f0d312009-08-03 18:11:17 -0700146
147 int id = b.mRS.nScriptCCreate();
148 ScriptC obj = new ScriptC(id, b.mRS);
149 b.transferObject(obj);
Joe Onoratod7b37742009-08-09 22:57:44 -0700150
Jason Sams69f0d312009-08-03 18:11:17 -0700151 return obj;
152 }
153
Joe Onoratod7b37742009-08-09 22:57:44 -0700154 public void addDefine(String name, int value) {
155 mIntDefines.put(name, value);
156 }
157
158 public void addDefine(String name, float value) {
159 mFloatDefines.put(name, value);
160 }
161
Joe Onoratof415cf22009-08-10 15:15:52 -0700162 /**
163 * Takes the all public static final fields for a class, and adds defines
164 * for them, using the name of the field as the name of the define.
165 */
166 public void addDefines(Class cl) {
167 addDefines(cl.getFields(), (Modifier.STATIC | Modifier.FINAL | Modifier.PUBLIC), null);
168 }
169
170 /**
171 * Takes the all public fields for an object, and adds defines
172 * for them, using the name of the field as the name of the define.
173 */
174 public void addDefines(Object o) {
175 addDefines(o.getClass().getFields(), Modifier.PUBLIC, o);
176 }
177
178 void addDefines(Field[] fields, int mask, Object o) {
179 for (Field f: fields) {
180 try {
181 if ((f.getModifiers() & mask) == mask) {
182 Class t = f.getType();
183 if (t == int.class) {
184 mIntDefines.put(f.getName(), f.getInt(o));
185 }
186 else if (t == float.class) {
187 mFloatDefines.put(f.getName(), f.getFloat(o));
188 }
189 }
190 } catch (IllegalAccessException ex) {
191 // TODO: Do we want this log?
192 Log.d(TAG, "addDefines skipping field " + f.getName());
193 }
194 }
195 }
196
Jason Sams69f0d312009-08-03 18:11:17 -0700197 public ScriptC create() {
198 return internalCreate(this);
199 }
200 }
Jason Sams69f0d312009-08-03 18:11:17 -0700201}
202