Script.java revision 2d71bc7b4c46a32cead32a35e6e137d13e8315ea
1/*
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 {
23    public static final int MAX_SLOT = 16;
24
25    boolean mIsRoot;
26    Type[] mTypes;
27    boolean[] mWritable;
28    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    }
44
45    protected void invoke(int slot) {
46        mRS.nScriptInvoke(mID, slot);
47    }
48
49    Script(int id, RenderScript rs) {
50        super(rs);
51        mID = id;
52    }
53
54    public void bindAllocation(Allocation va, int slot) {
55        mRS.validate();
56        mRS.nScriptBindAllocation(mID, va.mID, slot);
57    }
58
59    public void setClearColor(float r, float g, float b, float a) {
60        mRS.validate();
61        mRS.nScriptSetClearColor(mID, r, g, b, a);
62    }
63
64    public void setClearDepth(float d) {
65        mRS.validate();
66        mRS.nScriptSetClearDepth(mID, d);
67    }
68
69    public void setClearStencil(int stencil) {
70        mRS.validate();
71        mRS.nScriptSetClearStencil(mID, stencil);
72    }
73
74    public void setTimeZone(String timeZone) {
75        mRS.validate();
76        try {
77            mRS.nScriptSetTimeZone(mID, timeZone.getBytes("UTF-8"));
78        } catch (java.io.UnsupportedEncodingException e) {
79            throw new RuntimeException(e);
80        }
81    }
82
83    public static class Builder {
84        RenderScript mRS;
85        boolean mIsRoot = false;
86        Type[] mTypes;
87        String[] mNames;
88        boolean[] mWritable;
89        int mInvokableCount = 0;
90        Invokable[] mInvokables;
91
92        Builder(RenderScript rs) {
93            mRS = rs;
94            mTypes = new Type[MAX_SLOT];
95            mNames = new String[MAX_SLOT];
96            mWritable = new boolean[MAX_SLOT];
97            mInvokables = new Invokable[MAX_SLOT];
98        }
99
100        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;
108        }
109
110        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
119        public void setType(boolean writable, int slot) {
120            mWritable[slot] = writable;
121        }
122
123        void transferCreate() {
124            mRS.nScriptSetRoot(mIsRoot);
125            for(int ct=0; ct < mTypes.length; ct++) {
126                if(mTypes[ct] != null) {
127                    mRS.nScriptSetType(mTypes[ct].mID, mWritable[ct], mNames[ct], ct);
128                }
129            }
130            for(int ct=0; ct < mInvokableCount; ct++) {
131                mRS.nScriptSetInvokable(mInvokables[ct].mName, ct);
132            }
133        }
134
135        void transferObject(Script s) {
136            s.mIsRoot = mIsRoot;
137            s.mTypes = mTypes;
138            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;
144        }
145
146        public void setRoot(boolean r) {
147            mIsRoot = r;
148        }
149
150    }
151
152
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    }
195}
196
197