blob: 0f231d645d320aed06a449643f6f4d3bc59f562a [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
27/**
28 * @hide
29 **/
30public class ScriptC extends Script {
31 ScriptC(int id, RenderScript rs) {
32 super(id, rs);
33 }
34
Jason Sams69f0d312009-08-03 18:11:17 -070035 public static class Builder extends Script.Builder {
36 byte[] mProgram;
37 int mProgramLength;
Joe Onoratod7b37742009-08-09 22:57:44 -070038 HashMap<String,Integer> mIntDefines = new HashMap();
39 HashMap<String,Float> mFloatDefines = new HashMap();
Jason Sams69f0d312009-08-03 18:11:17 -070040
41 public Builder(RenderScript rs) {
42 super(rs);
43 }
44
45 public void setScript(String s) {
46 try {
47 mProgram = s.getBytes("UTF-8");
48 mProgramLength = mProgram.length;
49 } catch (java.io.UnsupportedEncodingException e) {
50 throw new RuntimeException(e);
51 }
52 }
53
54 public void setScript(Resources resources, int id) {
55 InputStream is = resources.openRawResource(id);
56 try {
57 try {
58 setScript(is);
59 } finally {
60 is.close();
61 }
62 } catch(IOException e) {
63 throw new Resources.NotFoundException();
64 }
65 }
66
Joe Onoratod7b37742009-08-09 22:57:44 -070067 public void setScript(InputStream is) throws IOException {
Jason Sams69f0d312009-08-03 18:11:17 -070068 byte[] buf = new byte[1024];
69 int currentPos = 0;
70 while(true) {
71 int bytesLeft = buf.length - currentPos;
72 if (bytesLeft == 0) {
73 byte[] buf2 = new byte[buf.length * 2];
74 System.arraycopy(buf, 0, buf2, 0, buf.length);
75 buf = buf2;
76 bytesLeft = buf.length - currentPos;
77 }
78 int bytesRead = is.read(buf, currentPos, bytesLeft);
79 if (bytesRead <= 0) {
80 break;
81 }
82 currentPos += bytesRead;
83 }
84 mProgram = buf;
85 mProgramLength = currentPos;
86 }
87
88 static synchronized ScriptC internalCreate(Builder b) {
89 b.mRS.nScriptCBegin();
90 b.transferCreate();
91
Joe Onoratod7b37742009-08-09 22:57:44 -070092 for (Entry<String,Integer> e: b.mIntDefines.entrySet()) {
93 b.mRS.nScriptCAddDefineI32(e.getKey(), e.getValue().intValue());
94 }
95 for (Entry<String,Float> e: b.mFloatDefines.entrySet()) {
96 b.mRS.nScriptCAddDefineF(e.getKey(), e.getValue().floatValue());
97 }
Jason Sams69f0d312009-08-03 18:11:17 -070098
Joe Onoratod7b37742009-08-09 22:57:44 -070099 b.mRS.nScriptCSetScript(b.mProgram, 0, b.mProgramLength);
Jason Sams69f0d312009-08-03 18:11:17 -0700100
101 int id = b.mRS.nScriptCCreate();
102 ScriptC obj = new ScriptC(id, b.mRS);
103 b.transferObject(obj);
Joe Onoratod7b37742009-08-09 22:57:44 -0700104
Jason Sams69f0d312009-08-03 18:11:17 -0700105 return obj;
106 }
107
Joe Onoratod7b37742009-08-09 22:57:44 -0700108 public void addDefine(String name, int value) {
109 mIntDefines.put(name, value);
110 }
111
112 public void addDefine(String name, float value) {
113 mFloatDefines.put(name, value);
114 }
115
Jason Sams69f0d312009-08-03 18:11:17 -0700116 public ScriptC create() {
117 return internalCreate(this);
118 }
119 }
Jason Sams69f0d312009-08-03 18:11:17 -0700120}
121