blob: 35791a3bc91e5a7cd853e9868589014e2df67cbb [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) {
51 mRS.nScriptBindAllocation(mID, va.mID, slot);
52 }
53
54 public void setClearColor(float r, float g, float b, float a) {
Jason Sams22534172009-08-04 16:58:20 -070055 mRS.nScriptSetClearColor(mID, r, g, b, a);
Jason Sams69f0d312009-08-03 18:11:17 -070056 }
57
58 public void setClearDepth(float d) {
Jason Sams22534172009-08-04 16:58:20 -070059 mRS.nScriptSetClearDepth(mID, d);
Jason Sams69f0d312009-08-03 18:11:17 -070060 }
61
62 public void setClearStencil(int stencil) {
Jason Sams22534172009-08-04 16:58:20 -070063 mRS.nScriptSetClearStencil(mID, stencil);
Jason Sams69f0d312009-08-03 18:11:17 -070064 }
65
Jason Sams22534172009-08-04 16:58:20 -070066 public void setTimeZone(String timeZone) {
67 try {
68 mRS.nScriptSetTimeZone(mID, timeZone.getBytes("UTF-8"));
69 } catch (java.io.UnsupportedEncodingException e) {
70 throw new RuntimeException(e);
71 }
72 }
Jason Sams69f0d312009-08-03 18:11:17 -070073
74 public static class Builder {
75 RenderScript mRS;
76 boolean mIsRoot = false;
Jason Sams43ee06852009-08-12 17:54:11 -070077 Type[] mTypes;
Jason Samsfbf0b9e2009-08-13 12:59:04 -070078 String[] mNames;
Jason Sams334ea0c2009-08-17 13:56:09 -070079 boolean[] mWritable;
Jason Samsbe2e8412009-09-16 15:04:38 -070080 int mInvokableCount = 0;
81 Invokable[] mInvokables;
Jason Sams69f0d312009-08-03 18:11:17 -070082
83 Builder(RenderScript rs) {
84 mRS = rs;
Jason Samsfbf0b9e2009-08-13 12:59:04 -070085 mTypes = new Type[MAX_SLOT];
86 mNames = new String[MAX_SLOT];
Jason Sams334ea0c2009-08-17 13:56:09 -070087 mWritable = new boolean[MAX_SLOT];
Jason Samsbe2e8412009-09-16 15:04:38 -070088 mInvokables = new Invokable[MAX_SLOT];
Jason Sams69f0d312009-08-03 18:11:17 -070089 }
90
Jason Samsfbf0b9e2009-08-13 12:59:04 -070091 public void setType(Type t, int slot) {
92 mTypes[slot] = t;
93 mNames[slot] = null;
94 }
95
96 public void setType(Type t, String name, int slot) {
97 mTypes[slot] = t;
98 mNames[slot] = name;
Jason Sams69f0d312009-08-03 18:11:17 -070099 }
100
Jason Samsbe2e8412009-09-16 15:04:38 -0700101 public Invokable addInvokable(String func) {
102 Invokable i = new Invokable();
103 i.mName = func;
104 i.mRS = mRS;
105 i.mSlot = mInvokableCount;
106 mInvokables[mInvokableCount++] = i;
107 return i;
108 }
109
Jason Sams334ea0c2009-08-17 13:56:09 -0700110 public void setType(boolean writable, int slot) {
111 mWritable[slot] = writable;
112 }
113
Jason Sams69f0d312009-08-03 18:11:17 -0700114 void transferCreate() {
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700115 mRS.nScriptSetRoot(mIsRoot);
116 for(int ct=0; ct < mTypes.length; ct++) {
117 if(mTypes[ct] != null) {
Jason Sams334ea0c2009-08-17 13:56:09 -0700118 mRS.nScriptSetType(mTypes[ct].mID, mWritable[ct], mNames[ct], ct);
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700119 }
Jason Sams43ee06852009-08-12 17:54:11 -0700120 }
Jason Samsbe2e8412009-09-16 15:04:38 -0700121 for(int ct=0; ct < mInvokableCount; ct++) {
122 mRS.nScriptSetInvokable(mInvokables[ct].mName, ct);
123 }
Jason Sams69f0d312009-08-03 18:11:17 -0700124 }
125
126 void transferObject(Script s) {
127 s.mIsRoot = mIsRoot;
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700128 s.mTypes = mTypes;
Jason Samsbe2e8412009-09-16 15:04:38 -0700129 s.mInvokables = new Invokable[mInvokableCount];
130 for(int ct=0; ct < mInvokableCount; ct++) {
131 s.mInvokables[ct] = mInvokables[ct];
132 s.mInvokables[ct].mScript = s;
133 }
134 s.mInvokables = null;
Jason Sams69f0d312009-08-03 18:11:17 -0700135 }
136
Jason Sams69f0d312009-08-03 18:11:17 -0700137 public void setRoot(boolean r) {
138 mIsRoot = r;
139 }
140
141 }
142
143}
144