| 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) { |
| 51 | mRS.nScriptBindAllocation(mID, va.mID, slot); |
| 52 | } |
| 53 | |
| 54 | public void setClearColor(float r, float g, float b, float a) { |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 55 | mRS.nScriptSetClearColor(mID, r, g, b, a); |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | public void setClearDepth(float d) { |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 59 | mRS.nScriptSetClearDepth(mID, d); |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | public void setClearStencil(int stencil) { |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 63 | mRS.nScriptSetClearStencil(mID, stencil); |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 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 | } |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 73 | |
| 74 | public static class Builder { |
| 75 | RenderScript mRS; |
| 76 | boolean mIsRoot = false; |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 77 | Type[] mTypes; |
| Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 78 | String[] mNames; |
| Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 79 | boolean[] mWritable; |
| Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame^] | 80 | int mInvokableCount = 0; |
| 81 | Invokable[] mInvokables; |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 82 | |
| 83 | Builder(RenderScript rs) { |
| 84 | mRS = rs; |
| Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 85 | mTypes = new Type[MAX_SLOT]; |
| 86 | mNames = new String[MAX_SLOT]; |
| Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 87 | mWritable = new boolean[MAX_SLOT]; |
| Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame^] | 88 | mInvokables = new Invokable[MAX_SLOT]; |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 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; |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame^] | 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 | |
| Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 110 | public void setType(boolean writable, int slot) { |
| 111 | mWritable[slot] = writable; |
| 112 | } |
| 113 | |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 114 | void transferCreate() { |
| Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 115 | mRS.nScriptSetRoot(mIsRoot); |
| 116 | for(int ct=0; ct < mTypes.length; ct++) { |
| 117 | if(mTypes[ct] != null) { |
| Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 118 | mRS.nScriptSetType(mTypes[ct].mID, mWritable[ct], mNames[ct], ct); |
| Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 119 | } |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 120 | } |
| Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame^] | 121 | for(int ct=0; ct < mInvokableCount; ct++) { |
| 122 | mRS.nScriptSetInvokable(mInvokables[ct].mName, ct); |
| 123 | } |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void transferObject(Script s) { |
| 127 | s.mIsRoot = mIsRoot; |
| Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 128 | s.mTypes = mTypes; |
| Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame^] | 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; |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 137 | public void setRoot(boolean r) { |
| 138 | mIsRoot = r; |
| 139 | } |
| 140 | |
| 141 | } |
| 142 | |
| 143 | } |
| 144 | |