Script.java revision be2e84193f709419634de4cc3ba0e67acf6976f3
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    Script(int id, RenderScript rs) {
46        super(rs);
47        mID = id;
48    }
49
50    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) {
55        mRS.nScriptSetClearColor(mID, r, g, b, a);
56    }
57
58    public void setClearDepth(float d) {
59        mRS.nScriptSetClearDepth(mID, d);
60    }
61
62    public void setClearStencil(int stencil) {
63        mRS.nScriptSetClearStencil(mID, stencil);
64    }
65
66    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    }
73
74    public static class Builder {
75        RenderScript mRS;
76        boolean mIsRoot = false;
77        Type[] mTypes;
78        String[] mNames;
79        boolean[] mWritable;
80        int mInvokableCount = 0;
81        Invokable[] mInvokables;
82
83        Builder(RenderScript rs) {
84            mRS = rs;
85            mTypes = new Type[MAX_SLOT];
86            mNames = new String[MAX_SLOT];
87            mWritable = new boolean[MAX_SLOT];
88            mInvokables = new Invokable[MAX_SLOT];
89        }
90
91        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;
99        }
100
101        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
110        public void setType(boolean writable, int slot) {
111            mWritable[slot] = writable;
112        }
113
114        void transferCreate() {
115            mRS.nScriptSetRoot(mIsRoot);
116            for(int ct=0; ct < mTypes.length; ct++) {
117                if(mTypes[ct] != null) {
118                    mRS.nScriptSetType(mTypes[ct].mID, mWritable[ct], mNames[ct], ct);
119                }
120            }
121            for(int ct=0; ct < mInvokableCount; ct++) {
122                mRS.nScriptSetInvokable(mInvokables[ct].mName, ct);
123            }
124        }
125
126        void transferObject(Script s) {
127            s.mIsRoot = mIsRoot;
128            s.mTypes = mTypes;
129            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;
135        }
136
137        public void setRoot(boolean r) {
138            mIsRoot = r;
139        }
140
141    }
142
143}
144
145