Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 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 | |
| 17 | package android.renderscript; |
| 18 | |
| 19 | /** |
| 20 | * @hide |
| 21 | **/ |
| 22 | public class Script extends BaseObj { |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 23 | public static final int MAX_SLOT = 16; |
| 24 | |
Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 25 | boolean mIsRoot; |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 26 | Type[] mTypes; |
Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 27 | boolean[] mWritable; |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 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 | } |
Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 44 | |
Jason Sams | 2d71bc7 | 2010-03-26 16:06:43 -0700 | [diff] [blame^] | 45 | protected void invoke(int slot) { |
| 46 | mRS.nScriptInvoke(mID, slot); |
| 47 | } |
| 48 | |
Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 49 | Script(int id, RenderScript rs) { |
| 50 | super(rs); |
| 51 | mID = id; |
| 52 | } |
| 53 | |
Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 54 | public void bindAllocation(Allocation va, int slot) { |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 55 | mRS.validate(); |
Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 56 | mRS.nScriptBindAllocation(mID, va.mID, slot); |
| 57 | } |
| 58 | |
| 59 | public void setClearColor(float r, float g, float b, float a) { |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 60 | mRS.validate(); |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 61 | mRS.nScriptSetClearColor(mID, r, g, b, a); |
Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | public void setClearDepth(float d) { |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 65 | mRS.validate(); |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 66 | mRS.nScriptSetClearDepth(mID, d); |
Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | public void setClearStencil(int stencil) { |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 70 | mRS.validate(); |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 71 | mRS.nScriptSetClearStencil(mID, stencil); |
Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 74 | public void setTimeZone(String timeZone) { |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 75 | mRS.validate(); |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 76 | try { |
| 77 | mRS.nScriptSetTimeZone(mID, timeZone.getBytes("UTF-8")); |
| 78 | } catch (java.io.UnsupportedEncodingException e) { |
| 79 | throw new RuntimeException(e); |
| 80 | } |
| 81 | } |
Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 82 | |
| 83 | public static class Builder { |
| 84 | RenderScript mRS; |
| 85 | boolean mIsRoot = false; |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 86 | Type[] mTypes; |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 87 | String[] mNames; |
Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 88 | boolean[] mWritable; |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 89 | int mInvokableCount = 0; |
| 90 | Invokable[] mInvokables; |
Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 91 | |
| 92 | Builder(RenderScript rs) { |
| 93 | mRS = rs; |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 94 | mTypes = new Type[MAX_SLOT]; |
| 95 | mNames = new String[MAX_SLOT]; |
Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 96 | mWritable = new boolean[MAX_SLOT]; |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 97 | mInvokables = new Invokable[MAX_SLOT]; |
Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 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; |
Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 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 | |
Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 119 | public void setType(boolean writable, int slot) { |
| 120 | mWritable[slot] = writable; |
| 121 | } |
| 122 | |
Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 123 | void transferCreate() { |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 124 | mRS.nScriptSetRoot(mIsRoot); |
| 125 | for(int ct=0; ct < mTypes.length; ct++) { |
| 126 | if(mTypes[ct] != null) { |
Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 127 | mRS.nScriptSetType(mTypes[ct].mID, mWritable[ct], mNames[ct], ct); |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 128 | } |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 129 | } |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 130 | for(int ct=0; ct < mInvokableCount; ct++) { |
| 131 | mRS.nScriptSetInvokable(mInvokables[ct].mName, ct); |
| 132 | } |
Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void transferObject(Script s) { |
| 136 | s.mIsRoot = mIsRoot; |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 137 | s.mTypes = mTypes; |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 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; |
Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 146 | public void setRoot(boolean r) { |
| 147 | mIsRoot = r; |
| 148 | } |
| 149 | |
| 150 | } |
| 151 | |
Jason Sams | 2d71bc7 | 2010-03-26 16:06:43 -0700 | [diff] [blame^] | 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 | } |
Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 195 | } |
| 196 | |