blob: 0d21368242ec19278025203501e284f5bb6b8cdc [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
19/**
20 * @hide
21 **/
22public class Script extends BaseObj {
Jason Samsfbf0b9e2009-08-13 12:59:04 -070023 public static final int MAX_SLOT = 16;
24
Jason Sams69f0d312009-08-03 18:11:17 -070025 boolean mIsRoot;
Jason Sams43ee06852009-08-12 17:54:11 -070026 Type[] mTypes;
Jason Sams334ea0c2009-08-17 13:56:09 -070027 boolean[] mWritable;
Jason Samsbe2e8412009-09-16 15:04:38 -070028 Invokable[] mInvokables;
29
30 public static class Invokable {
31 RenderScript mRS;
32 Script mScript;
33 int mSlot;
34 String mName;
35
36 Invokable() {
37 mSlot = -1;
38 }
39
40 public void execute() {
41 mRS.nScriptInvoke(mScript.mID, mSlot);
42 }
43 }
Jason Sams69f0d312009-08-03 18:11:17 -070044
Jason Sams2d71bc72010-03-26 16:06:43 -070045 protected void invoke(int slot) {
46 mRS.nScriptInvoke(mID, slot);
47 }
48
Jason Sams69f0d312009-08-03 18:11:17 -070049 Script(int id, RenderScript rs) {
50 super(rs);
51 mID = id;
52 }
53
Jason Sams69f0d312009-08-03 18:11:17 -070054 public void bindAllocation(Allocation va, int slot) {
Jason Sams771bebb2009-12-07 12:40:12 -080055 mRS.validate();
Jason Sams69f0d312009-08-03 18:11:17 -070056 mRS.nScriptBindAllocation(mID, va.mID, slot);
57 }
58
59 public void setClearColor(float r, float g, float b, float a) {
Jason Sams771bebb2009-12-07 12:40:12 -080060 mRS.validate();
Jason Sams22534172009-08-04 16:58:20 -070061 mRS.nScriptSetClearColor(mID, r, g, b, a);
Jason Sams69f0d312009-08-03 18:11:17 -070062 }
63
64 public void setClearDepth(float d) {
Jason Sams771bebb2009-12-07 12:40:12 -080065 mRS.validate();
Jason Sams22534172009-08-04 16:58:20 -070066 mRS.nScriptSetClearDepth(mID, d);
Jason Sams69f0d312009-08-03 18:11:17 -070067 }
68
69 public void setClearStencil(int stencil) {
Jason Sams771bebb2009-12-07 12:40:12 -080070 mRS.validate();
Jason Sams22534172009-08-04 16:58:20 -070071 mRS.nScriptSetClearStencil(mID, stencil);
Jason Sams69f0d312009-08-03 18:11:17 -070072 }
73
Jason Sams22534172009-08-04 16:58:20 -070074 public void setTimeZone(String timeZone) {
Jason Sams771bebb2009-12-07 12:40:12 -080075 mRS.validate();
Jason Sams22534172009-08-04 16:58:20 -070076 try {
77 mRS.nScriptSetTimeZone(mID, timeZone.getBytes("UTF-8"));
78 } catch (java.io.UnsupportedEncodingException e) {
79 throw new RuntimeException(e);
80 }
81 }
Jason Sams69f0d312009-08-03 18:11:17 -070082
83 public static class Builder {
84 RenderScript mRS;
85 boolean mIsRoot = false;
Jason Sams43ee06852009-08-12 17:54:11 -070086 Type[] mTypes;
Jason Samsfbf0b9e2009-08-13 12:59:04 -070087 String[] mNames;
Jason Sams334ea0c2009-08-17 13:56:09 -070088 boolean[] mWritable;
Jason Samsbe2e8412009-09-16 15:04:38 -070089 int mInvokableCount = 0;
90 Invokable[] mInvokables;
Jason Sams69f0d312009-08-03 18:11:17 -070091
92 Builder(RenderScript rs) {
93 mRS = rs;
Jason Samsfbf0b9e2009-08-13 12:59:04 -070094 mTypes = new Type[MAX_SLOT];
95 mNames = new String[MAX_SLOT];
Jason Sams334ea0c2009-08-17 13:56:09 -070096 mWritable = new boolean[MAX_SLOT];
Jason Samsbe2e8412009-09-16 15:04:38 -070097 mInvokables = new Invokable[MAX_SLOT];
Jason Sams69f0d312009-08-03 18:11:17 -070098 }
99
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700100 public void setType(Type t, int slot) {
101 mTypes[slot] = t;
102 mNames[slot] = null;
103 }
104
105 public void setType(Type t, String name, int slot) {
106 mTypes[slot] = t;
107 mNames[slot] = name;
Jason Sams69f0d312009-08-03 18:11:17 -0700108 }
109
Jason Samsbe2e8412009-09-16 15:04:38 -0700110 public Invokable addInvokable(String func) {
111 Invokable i = new Invokable();
112 i.mName = func;
113 i.mRS = mRS;
114 i.mSlot = mInvokableCount;
115 mInvokables[mInvokableCount++] = i;
116 return i;
117 }
118
Jason Sams334ea0c2009-08-17 13:56:09 -0700119 public void setType(boolean writable, int slot) {
120 mWritable[slot] = writable;
121 }
122
Jason Sams69f0d312009-08-03 18:11:17 -0700123 void transferCreate() {
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700124 mRS.nScriptSetRoot(mIsRoot);
125 for(int ct=0; ct < mTypes.length; ct++) {
126 if(mTypes[ct] != null) {
Jason Sams334ea0c2009-08-17 13:56:09 -0700127 mRS.nScriptSetType(mTypes[ct].mID, mWritable[ct], mNames[ct], ct);
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700128 }
Jason Sams43ee06852009-08-12 17:54:11 -0700129 }
Jason Samsbe2e8412009-09-16 15:04:38 -0700130 for(int ct=0; ct < mInvokableCount; ct++) {
131 mRS.nScriptSetInvokable(mInvokables[ct].mName, ct);
132 }
Jason Sams69f0d312009-08-03 18:11:17 -0700133 }
134
135 void transferObject(Script s) {
136 s.mIsRoot = mIsRoot;
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700137 s.mTypes = mTypes;
Jason Samsbe2e8412009-09-16 15:04:38 -0700138 s.mInvokables = new Invokable[mInvokableCount];
139 for(int ct=0; ct < mInvokableCount; ct++) {
140 s.mInvokables[ct] = mInvokables[ct];
141 s.mInvokables[ct].mScript = s;
142 }
143 s.mInvokables = null;
Jason Sams69f0d312009-08-03 18:11:17 -0700144 }
145
Jason Sams69f0d312009-08-03 18:11:17 -0700146 public void setRoot(boolean r) {
147 mIsRoot = r;
148 }
149
150 }
151
Jason Sams2d71bc72010-03-26 16:06:43 -0700152
153 public static class FieldBase {
154 protected Element mElement;
155 protected Type mType;
156 protected Allocation mAllocation;
157
158 protected void init(RenderScript rs, int dimx) {
159 mAllocation = Allocation.createSized(rs, mElement, dimx);
160 mType = mAllocation.getType();
161 }
162
163 protected FieldBase() {
164 }
165
166 public Element getElement() {
167 return mElement;
168 }
169
170 public Type getType() {
171 return mType;
172 }
173
174 public Allocation getAllocation() {
175 return mAllocation;
176 }
177
178 //@Override
179 public void updateAllocation() {
180 }
181
182
183 //
184 /*
185 public class ScriptField_UserField
186 extends android.renderscript.Script.FieldBase {
187
188 protected
189
190 }
191
192 */
193
194 }
Jason Sams69f0d312009-08-03 18:11:17 -0700195}
196