| 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() { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 41 | mRS.nScriptInvoke(mScript.getID(), mSlot); |
| Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 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) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 46 | mRS.nScriptInvoke(getID(), slot); |
| Jason Sams | 2d71bc7 | 2010-03-26 16:06:43 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| Jason Sams | 96ed4cf | 2010-06-15 12:15:57 -0700 | [diff] [blame] | 49 | protected void invoke(int slot, FieldPacker v) { |
| 50 | if (v != null) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 51 | mRS.nScriptInvokeV(getID(), slot, v.getData()); |
| Jason Sams | 96ed4cf | 2010-06-15 12:15:57 -0700 | [diff] [blame] | 52 | } else { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 53 | mRS.nScriptInvoke(getID(), slot); |
| Jason Sams | 96ed4cf | 2010-06-15 12:15:57 -0700 | [diff] [blame] | 54 | } |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 58 | Script(int id, RenderScript rs) { |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 59 | super(id, rs); |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 62 | public void bindAllocation(Allocation va, int slot) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 63 | mRS.validate(); |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 64 | if (va != null) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 65 | mRS.nScriptBindAllocation(getID(), va.getID(), slot); |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 66 | } else { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 67 | mRS.nScriptBindAllocation(getID(), 0, slot); |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
| 71 | public void setVar(int index, float v) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 72 | mRS.nScriptSetVarF(getID(), index, v); |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| Stephen Hines | ca54ec3 | 2010-09-20 17:20:30 -0700 | [diff] [blame] | 75 | public void setVar(int index, double v) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 76 | mRS.nScriptSetVarD(getID(), index, v); |
| Stephen Hines | ca54ec3 | 2010-09-20 17:20:30 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 79 | public void setVar(int index, int v) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 80 | mRS.nScriptSetVarI(getID(), index, v); |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| Stephen Hines | 031ec58c | 2010-10-11 10:54:21 -0700 | [diff] [blame] | 83 | public void setVar(int index, long v) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 84 | mRS.nScriptSetVarJ(getID(), index, v); |
| Stephen Hines | 031ec58c | 2010-10-11 10:54:21 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| Jason Sams | 0b9a22c | 2010-07-02 15:35:19 -0700 | [diff] [blame] | 87 | public void setVar(int index, boolean v) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 88 | mRS.nScriptSetVarI(getID(), index, v ? 1 : 0); |
| Jason Sams | 0b9a22c | 2010-07-02 15:35:19 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| Jason Sams | 6f4cf0b | 2010-11-16 17:37:02 -0800 | [diff] [blame] | 91 | public void setVar(int index, BaseObj o) { |
| 92 | mRS.nScriptSetVarObj(getID(), index, (o == null) ? 0 : o.getID()); |
| 93 | } |
| 94 | |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 95 | public void setVar(int index, FieldPacker v) { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 96 | mRS.nScriptSetVarV(getID(), index, v.getData()); |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 99 | public void setTimeZone(String timeZone) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 100 | mRS.validate(); |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 101 | try { |
| Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 102 | mRS.nScriptSetTimeZone(getID(), timeZone.getBytes("UTF-8")); |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 103 | } catch (java.io.UnsupportedEncodingException e) { |
| 104 | throw new RuntimeException(e); |
| 105 | } |
| 106 | } |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 107 | |
| 108 | public static class Builder { |
| 109 | RenderScript mRS; |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 110 | |
| 111 | Builder(RenderScript rs) { |
| 112 | mRS = rs; |
| 113 | } |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| Jason Sams | 2d71bc7 | 2010-03-26 16:06:43 -0700 | [diff] [blame] | 116 | |
| 117 | public static class FieldBase { |
| 118 | protected Element mElement; |
| Jason Sams | 2d71bc7 | 2010-03-26 16:06:43 -0700 | [diff] [blame] | 119 | protected Allocation mAllocation; |
| 120 | |
| 121 | protected void init(RenderScript rs, int dimx) { |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 122 | mAllocation = Allocation.createSized(rs, mElement, dimx, Allocation.USAGE_SCRIPT); |
| 123 | } |
| 124 | |
| 125 | protected void init(RenderScript rs, int dimx, int usages) { |
| 126 | mAllocation = Allocation.createSized(rs, mElement, dimx, Allocation.USAGE_SCRIPT | usages); |
| Jason Sams | 2d71bc7 | 2010-03-26 16:06:43 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | protected FieldBase() { |
| 130 | } |
| 131 | |
| 132 | public Element getElement() { |
| 133 | return mElement; |
| 134 | } |
| 135 | |
| 136 | public Type getType() { |
| Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 137 | return mAllocation.getType(); |
| Jason Sams | 2d71bc7 | 2010-03-26 16:06:43 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | public Allocation getAllocation() { |
| 141 | return mAllocation; |
| 142 | } |
| 143 | |
| 144 | //@Override |
| 145 | public void updateAllocation() { |
| 146 | } |
| Jason Sams | 2d71bc7 | 2010-03-26 16:06:43 -0700 | [diff] [blame] | 147 | } |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 148 | } |
| 149 | |