blob: 57ccfa36325b04c70f75abb3471107862eac1487 [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
45 Script(int id, RenderScript rs) {
46 super(rs);
47 mID = id;
48 }
49
Jason Sams69f0d312009-08-03 18:11:17 -070050 public void bindAllocation(Allocation va, int slot) {
Jason Sams771bebb2009-12-07 12:40:12 -080051 mRS.validate();
Jason Sams69f0d312009-08-03 18:11:17 -070052 mRS.nScriptBindAllocation(mID, va.mID, slot);
53 }
54
55 public void setClearColor(float r, float g, float b, float a) {
Jason Sams771bebb2009-12-07 12:40:12 -080056 mRS.validate();
Jason Sams22534172009-08-04 16:58:20 -070057 mRS.nScriptSetClearColor(mID, r, g, b, a);
Jason Sams69f0d312009-08-03 18:11:17 -070058 }
59
60 public void setClearDepth(float d) {
Jason Sams771bebb2009-12-07 12:40:12 -080061 mRS.validate();
Jason Sams22534172009-08-04 16:58:20 -070062 mRS.nScriptSetClearDepth(mID, d);
Jason Sams69f0d312009-08-03 18:11:17 -070063 }
64
65 public void setClearStencil(int stencil) {
Jason Sams771bebb2009-12-07 12:40:12 -080066 mRS.validate();
Jason Sams22534172009-08-04 16:58:20 -070067 mRS.nScriptSetClearStencil(mID, stencil);
Jason Sams69f0d312009-08-03 18:11:17 -070068 }
69
Jason Sams22534172009-08-04 16:58:20 -070070 public void setTimeZone(String timeZone) {
Jason Sams771bebb2009-12-07 12:40:12 -080071 mRS.validate();
Jason Sams22534172009-08-04 16:58:20 -070072 try {
73 mRS.nScriptSetTimeZone(mID, timeZone.getBytes("UTF-8"));
74 } catch (java.io.UnsupportedEncodingException e) {
75 throw new RuntimeException(e);
76 }
77 }
Jason Sams69f0d312009-08-03 18:11:17 -070078
79 public static class Builder {
80 RenderScript mRS;
81 boolean mIsRoot = false;
Jason Sams43ee06852009-08-12 17:54:11 -070082 Type[] mTypes;
Jason Samsfbf0b9e2009-08-13 12:59:04 -070083 String[] mNames;
Jason Sams334ea0c2009-08-17 13:56:09 -070084 boolean[] mWritable;
Jason Samsbe2e8412009-09-16 15:04:38 -070085 int mInvokableCount = 0;
86 Invokable[] mInvokables;
Jason Sams69f0d312009-08-03 18:11:17 -070087
88 Builder(RenderScript rs) {
89 mRS = rs;
Jason Samsfbf0b9e2009-08-13 12:59:04 -070090 mTypes = new Type[MAX_SLOT];
91 mNames = new String[MAX_SLOT];
Jason Sams334ea0c2009-08-17 13:56:09 -070092 mWritable = new boolean[MAX_SLOT];
Jason Samsbe2e8412009-09-16 15:04:38 -070093 mInvokables = new Invokable[MAX_SLOT];
Jason Sams69f0d312009-08-03 18:11:17 -070094 }
95
Jason Samsfbf0b9e2009-08-13 12:59:04 -070096 public void setType(Type t, int slot) {
97 mTypes[slot] = t;
98 mNames[slot] = null;
99 }
100
101 public void setType(Type t, String name, int slot) {
102 mTypes[slot] = t;
103 mNames[slot] = name;
Jason Sams69f0d312009-08-03 18:11:17 -0700104 }
105
Jason Samsbe2e8412009-09-16 15:04:38 -0700106 public Invokable addInvokable(String func) {
107 Invokable i = new Invokable();
108 i.mName = func;
109 i.mRS = mRS;
110 i.mSlot = mInvokableCount;
111 mInvokables[mInvokableCount++] = i;
112 return i;
113 }
114
Jason Sams334ea0c2009-08-17 13:56:09 -0700115 public void setType(boolean writable, int slot) {
116 mWritable[slot] = writable;
117 }
118
Jason Sams69f0d312009-08-03 18:11:17 -0700119 void transferCreate() {
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700120 mRS.nScriptSetRoot(mIsRoot);
121 for(int ct=0; ct < mTypes.length; ct++) {
122 if(mTypes[ct] != null) {
Jason Sams334ea0c2009-08-17 13:56:09 -0700123 mRS.nScriptSetType(mTypes[ct].mID, mWritable[ct], mNames[ct], ct);
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700124 }
Jason Sams43ee06852009-08-12 17:54:11 -0700125 }
Jason Samsbe2e8412009-09-16 15:04:38 -0700126 for(int ct=0; ct < mInvokableCount; ct++) {
127 mRS.nScriptSetInvokable(mInvokables[ct].mName, ct);
128 }
Jason Sams69f0d312009-08-03 18:11:17 -0700129 }
130
131 void transferObject(Script s) {
132 s.mIsRoot = mIsRoot;
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700133 s.mTypes = mTypes;
Jason Samsbe2e8412009-09-16 15:04:38 -0700134 s.mInvokables = new Invokable[mInvokableCount];
135 for(int ct=0; ct < mInvokableCount; ct++) {
136 s.mInvokables[ct] = mInvokables[ct];
137 s.mInvokables[ct].mScript = s;
138 }
139 s.mInvokables = null;
Jason Sams69f0d312009-08-03 18:11:17 -0700140 }
141
Jason Sams69f0d312009-08-03 18:11:17 -0700142 public void setRoot(boolean r) {
143 mIsRoot = r;
144 }
145
146 }
147
148}
149