| 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 | |
| 45 | Script(int id, RenderScript rs) { |
| 46 | super(rs); |
| 47 | mID = id; |
| 48 | } |
| 49 | |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 50 | public void bindAllocation(Allocation va, int slot) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 51 | mRS.validate(); |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 52 | mRS.nScriptBindAllocation(mID, va.mID, slot); |
| 53 | } |
| 54 | |
| 55 | public void setClearColor(float r, float g, float b, float a) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 56 | mRS.validate(); |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 57 | mRS.nScriptSetClearColor(mID, r, g, b, a); |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | public void setClearDepth(float d) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 61 | mRS.validate(); |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 62 | mRS.nScriptSetClearDepth(mID, d); |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | public void setClearStencil(int stencil) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 66 | mRS.validate(); |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 67 | mRS.nScriptSetClearStencil(mID, stencil); |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 70 | public void setTimeZone(String timeZone) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 71 | mRS.validate(); |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 72 | try { |
| 73 | mRS.nScriptSetTimeZone(mID, timeZone.getBytes("UTF-8")); |
| 74 | } catch (java.io.UnsupportedEncodingException e) { |
| 75 | throw new RuntimeException(e); |
| 76 | } |
| 77 | } |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 78 | |
| 79 | public static class Builder { |
| 80 | RenderScript mRS; |
| 81 | boolean mIsRoot = false; |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 82 | Type[] mTypes; |
| Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 83 | String[] mNames; |
| Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 84 | boolean[] mWritable; |
| Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 85 | int mInvokableCount = 0; |
| 86 | Invokable[] mInvokables; |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 87 | |
| 88 | Builder(RenderScript rs) { |
| 89 | mRS = rs; |
| Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 90 | mTypes = new Type[MAX_SLOT]; |
| 91 | mNames = new String[MAX_SLOT]; |
| Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 92 | mWritable = new boolean[MAX_SLOT]; |
| Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 93 | mInvokables = new Invokable[MAX_SLOT]; |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 96 | public void setType(Type t, int slot) { |
| 97 | mTypes[slot] = t; |
| 98 | mNames[slot] = null; |
| 99 | } |
| 100 | |
| 101 | public void setType(Type t, String name, int slot) { |
| 102 | mTypes[slot] = t; |
| 103 | mNames[slot] = name; |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 106 | public Invokable addInvokable(String func) { |
| 107 | Invokable i = new Invokable(); |
| 108 | i.mName = func; |
| 109 | i.mRS = mRS; |
| 110 | i.mSlot = mInvokableCount; |
| 111 | mInvokables[mInvokableCount++] = i; |
| 112 | return i; |
| 113 | } |
| 114 | |
| Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 115 | public void setType(boolean writable, int slot) { |
| 116 | mWritable[slot] = writable; |
| 117 | } |
| 118 | |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 119 | void transferCreate() { |
| Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 120 | mRS.nScriptSetRoot(mIsRoot); |
| 121 | for(int ct=0; ct < mTypes.length; ct++) { |
| 122 | if(mTypes[ct] != null) { |
| Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 123 | mRS.nScriptSetType(mTypes[ct].mID, mWritable[ct], mNames[ct], ct); |
| Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 124 | } |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 125 | } |
| Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 126 | for(int ct=0; ct < mInvokableCount; ct++) { |
| 127 | mRS.nScriptSetInvokable(mInvokables[ct].mName, ct); |
| 128 | } |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void transferObject(Script s) { |
| 132 | s.mIsRoot = mIsRoot; |
| Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 133 | s.mTypes = mTypes; |
| Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 134 | s.mInvokables = new Invokable[mInvokableCount]; |
| 135 | for(int ct=0; ct < mInvokableCount; ct++) { |
| 136 | s.mInvokables[ct] = mInvokables[ct]; |
| 137 | s.mInvokables[ct].mScript = s; |
| 138 | } |
| 139 | s.mInvokables = null; |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 142 | public void setRoot(boolean r) { |
| 143 | mIsRoot = r; |
| 144 | } |
| 145 | |
| 146 | } |
| 147 | |
| 148 | } |
| 149 | |