Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -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 | |
Jason Sams | 94d8e90a | 2009-06-10 16:09:05 -0700 | [diff] [blame] | 17 | package android.renderscript; |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 18 | |
Jack Palevich | 43702d8 | 2009-05-28 13:38:16 -0700 | [diff] [blame] | 19 | import java.io.IOException; |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 20 | import java.io.InputStream; |
| 21 | |
| 22 | import android.content.res.Resources; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame^] | 23 | import android.graphics.Bitmap; |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 24 | import android.util.Config; |
| 25 | import android.util.Log; |
| 26 | import android.view.Surface; |
Jack Palevich | 43702d8 | 2009-05-28 13:38:16 -0700 | [diff] [blame] | 27 | |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 28 | |
Jason Sams | e29d471 | 2009-07-23 15:19:03 -0700 | [diff] [blame] | 29 | /** |
| 30 | * @hide |
| 31 | * |
| 32 | **/ |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 33 | public class RenderScript { |
Jason Sams | f29ca50 | 2009-06-23 12:22:47 -0700 | [diff] [blame] | 34 | static final String LOG_TAG = "libRS_jni"; |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 35 | private static final boolean DEBUG = false; |
| 36 | private static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV; |
| 37 | |
| 38 | |
| 39 | |
Jason Sams | 02fb2cb | 2009-05-28 15:37:57 -0700 | [diff] [blame] | 40 | /* |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 41 | * We use a class initializer to allow the native code to cache some |
| 42 | * field offsets. |
| 43 | */ |
| 44 | private static boolean sInitialized; |
| 45 | native private static void _nInit(); |
| 46 | |
Jason Sams | dba3ba5 | 2009-07-30 14:56:12 -0700 | [diff] [blame] | 47 | |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 48 | static { |
| 49 | sInitialized = false; |
| 50 | try { |
Jason Sams | e29d471 | 2009-07-23 15:19:03 -0700 | [diff] [blame] | 51 | System.loadLibrary("rs_jni"); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 52 | _nInit(); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 53 | sInitialized = true; |
| 54 | } catch (UnsatisfiedLinkError e) { |
| 55 | Log.d(LOG_TAG, "RenderScript JNI library not found!"); |
| 56 | } |
| 57 | } |
| 58 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 59 | native int nDeviceCreate(); |
| 60 | native void nDeviceDestroy(int dev); |
| 61 | native int nContextCreate(int dev, Surface sur, int ver); |
| 62 | native void nContextDestroy(int con); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 63 | |
| 64 | //void rsContextBindSampler (uint32_t slot, RsSampler sampler); |
| 65 | //void rsContextBindRootScript (RsScript sampler); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 66 | native void nContextBindRootScript(int script); |
| 67 | native void nContextBindSampler(int sampler, int slot); |
| 68 | native void nContextBindProgramFragmentStore(int pfs); |
| 69 | native void nContextBindProgramFragment(int pf); |
| 70 | native void nContextBindProgramVertex(int pf); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 71 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 72 | native void nAssignName(int obj, byte[] name); |
| 73 | native int nFileOpen(byte[] name); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 74 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 75 | native void nElementBegin(); |
| 76 | native void nElementAddPredefined(int predef); |
| 77 | native void nElementAdd(int kind, int type, int norm, int bits); |
| 78 | native int nElementCreate(); |
| 79 | native int nElementGetPredefined(int predef); |
| 80 | native void nElementDestroy(int obj); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 81 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 82 | native void nTypeBegin(int elementID); |
| 83 | native void nTypeAdd(int dim, int val); |
| 84 | native int nTypeCreate(); |
| 85 | native void nTypeDestroy(int id); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 86 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 87 | native int nAllocationCreateTyped(int type); |
| 88 | native int nAllocationCreatePredefSized(int predef, int count); |
| 89 | native int nAllocationCreateSized(int elem, int count); |
| 90 | native int nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp); |
| 91 | native int nAllocationCreateFromBitmapBoxed(int dstFmt, boolean genMips, Bitmap bmp); |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 92 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 93 | native void nAllocationUploadToTexture(int alloc, int baseMioLevel); |
| 94 | native void nAllocationDestroy(int alloc); |
| 95 | native void nAllocationData(int id, int[] d); |
| 96 | native void nAllocationData(int id, float[] d); |
| 97 | native void nAllocationSubData1D(int id, int off, int count, int[] d); |
| 98 | native void nAllocationSubData1D(int id, int off, int count, float[] d); |
| 99 | native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d); |
| 100 | native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 101 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 102 | native void nTriangleMeshDestroy(int id); |
| 103 | native void nTriangleMeshBegin(int vertex, int index); |
| 104 | native void nTriangleMeshAddVertex_XY (float x, float y); |
| 105 | native void nTriangleMeshAddVertex_XYZ (float x, float y, float z); |
| 106 | native void nTriangleMeshAddVertex_XY_ST (float x, float y, float s, float t); |
| 107 | native void nTriangleMeshAddVertex_XYZ_ST (float x, float y, float z, float s, float t); |
| 108 | native void nTriangleMeshAddVertex_XYZ_ST_NORM (float x, float y, float z, float s, float t, float nx, float ny, float nz); |
| 109 | native void nTriangleMeshAddTriangle(int i1, int i2, int i3); |
| 110 | native int nTriangleMeshCreate(); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 111 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 112 | native void nAdapter1DDestroy(int id); |
| 113 | native void nAdapter1DBindAllocation(int ad, int alloc); |
| 114 | native void nAdapter1DSetConstraint(int ad, int dim, int value); |
| 115 | native void nAdapter1DData(int ad, int[] d); |
| 116 | native void nAdapter1DSubData(int ad, int off, int count, int[] d); |
| 117 | native void nAdapter1DData(int ad, float[] d); |
| 118 | native void nAdapter1DSubData(int ad, int off, int count, float[] d); |
| 119 | native int nAdapter1DCreate(); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 120 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 121 | native void nScriptDestroy(int script); |
| 122 | native void nScriptBindAllocation(int vtm, int alloc, int slot); |
| 123 | native void nScriptCBegin(); |
| 124 | native void nScriptCSetClearColor(float r, float g, float b, float a); |
| 125 | native void nScriptCSetClearDepth(float depth); |
| 126 | native void nScriptCSetClearStencil(int stencil); |
| 127 | native void nScriptCSetTimeZone(byte[] timeZone); |
| 128 | native void nScriptCAddType(int type); |
| 129 | native void nScriptCSetRoot(boolean isRoot); |
| 130 | native void nScriptCSetScript(byte[] script, int offset, int length); |
| 131 | native int nScriptCCreate(); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 132 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 133 | native void nSamplerDestroy(int sampler); |
| 134 | native void nSamplerBegin(); |
| 135 | native void nSamplerSet(int param, int value); |
| 136 | native int nSamplerCreate(); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 137 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 138 | native void nProgramFragmentStoreBegin(int in, int out); |
| 139 | native void nProgramFragmentStoreDepthFunc(int func); |
| 140 | native void nProgramFragmentStoreDepthMask(boolean enable); |
| 141 | native void nProgramFragmentStoreColorMask(boolean r, boolean g, boolean b, boolean a); |
| 142 | native void nProgramFragmentStoreBlendFunc(int src, int dst); |
| 143 | native void nProgramFragmentStoreDither(boolean enable); |
| 144 | native int nProgramFragmentStoreCreate(); |
| 145 | native void nProgramFragmentStoreDestroy(int pgm); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 146 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 147 | native void nProgramFragmentBegin(int in, int out); |
| 148 | native void nProgramFragmentBindTexture(int vpf, int slot, int a); |
| 149 | native void nProgramFragmentBindSampler(int vpf, int slot, int s); |
| 150 | native void nProgramFragmentSetType(int slot, int vt); |
| 151 | native void nProgramFragmentSetEnvMode(int slot, int env); |
| 152 | native void nProgramFragmentSetTexEnable(int slot, boolean enable); |
| 153 | native int nProgramFragmentCreate(); |
| 154 | native void nProgramFragmentDestroy(int pgm); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 155 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 156 | native void nProgramVertexDestroy(int pv); |
| 157 | native void nProgramVertexBindAllocation(int pv, int slot, int mID); |
| 158 | native void nProgramVertexBegin(int inID, int outID); |
| 159 | native void nProgramVertexSetType(int slot, int mID); |
| 160 | native void nProgramVertexSetTextureMatrixEnable(boolean enable); |
| 161 | native void nProgramVertexAddLight(int id); |
| 162 | native int nProgramVertexCreate(); |
Jason Sams | 1fe9b8c | 2009-06-11 14:46:10 -0700 | [diff] [blame] | 163 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 164 | native void nLightBegin(); |
| 165 | native void nLightSetIsMono(boolean isMono); |
| 166 | native void nLightSetIsLocal(boolean isLocal); |
| 167 | native int nLightCreate(); |
| 168 | native void nLightDestroy(int l); |
| 169 | native void nLightSetColor(int l, float r, float g, float b); |
| 170 | native void nLightSetPosition(int l, float x, float y, float z); |
Jason Sams | bba134c | 2009-06-22 15:49:21 -0700 | [diff] [blame] | 171 | |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 172 | |
| 173 | private int mDev; |
| 174 | private int mContext; |
| 175 | private Surface mSurface; |
| 176 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 177 | private static boolean mElementsInitialized = false; |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 178 | |
| 179 | /////////////////////////////////////////////////////////////////////////////////// |
Jack Palevich | 43702d8 | 2009-05-28 13:38:16 -0700 | [diff] [blame] | 180 | // |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 181 | |
| 182 | RenderScript(Surface sur) { |
| 183 | mSurface = sur; |
| 184 | mDev = nDeviceCreate(); |
| 185 | mContext = nContextCreate(mDev, mSurface, 0); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 186 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 187 | // TODO: This should be protected by a lock |
| 188 | if(!mElementsInitialized) { |
| 189 | Element.init(this); |
| 190 | mElementsInitialized = true; |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 194 | ////////////////////////////////////////////////////////////////////////////////// |
| 195 | // Element |
| 196 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 197 | Element.Builder mElementBuilder = new Element.Builder(this); |
| 198 | public Element.Builder elementBuilderCreate() throws IllegalStateException { |
| 199 | mElementBuilder.begin(); |
| 200 | return mElementBuilder; |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame^] | 203 | Type.Builder mTypeBuilder = new Type.Builder(this); |
| 204 | public Type.Builder typeBuilderCreate(Element e) throws IllegalStateException { |
| 205 | mTypeBuilder.begin(e); |
| 206 | return mTypeBuilder; |
| 207 | } |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 208 | |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 209 | |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 210 | |
| 211 | public enum DepthFunc { |
| 212 | ALWAYS (0), |
| 213 | LESS (1), |
| 214 | LEQUAL (2), |
| 215 | GREATER (3), |
| 216 | GEQUAL (4), |
| 217 | EQUAL (5), |
| 218 | NOTEQUAL (6); |
| 219 | |
| 220 | int mID; |
| 221 | DepthFunc(int id) { |
| 222 | mID = id; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | public enum BlendSrcFunc { |
| 227 | ZERO (0), |
| 228 | ONE (1), |
| 229 | DST_COLOR (2), |
| 230 | ONE_MINUS_DST_COLOR (3), |
| 231 | SRC_ALPHA (4), |
| 232 | ONE_MINUS_SRC_ALPHA (5), |
| 233 | DST_ALPHA (6), |
| 234 | ONE_MINUS_DST_ALPA (7), |
| 235 | SRC_ALPHA_SATURATE (8); |
| 236 | |
| 237 | int mID; |
| 238 | BlendSrcFunc(int id) { |
| 239 | mID = id; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | public enum BlendDstFunc { |
| 244 | ZERO (0), |
| 245 | ONE (1), |
| 246 | SRC_COLOR (2), |
| 247 | ONE_MINUS_SRC_COLOR (3), |
| 248 | SRC_ALPHA (4), |
| 249 | ONE_MINUS_SRC_ALPHA (5), |
| 250 | DST_ALPHA (6), |
| 251 | ONE_MINUS_DST_ALPA (7); |
| 252 | |
| 253 | int mID; |
| 254 | BlendDstFunc(int id) { |
| 255 | mID = id; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | public enum EnvMode { |
| 260 | REPLACE (0), |
| 261 | MODULATE (1), |
| 262 | DECAL (2); |
| 263 | |
| 264 | int mID; |
| 265 | EnvMode(int id) { |
| 266 | mID = id; |
| 267 | } |
| 268 | } |
| 269 | |
Jason Sams | 02fb2cb | 2009-05-28 15:37:57 -0700 | [diff] [blame] | 270 | public enum SamplerParam { |
| 271 | FILTER_MIN (0), |
| 272 | FILTER_MAG (1), |
| 273 | WRAP_MODE_S (2), |
| 274 | WRAP_MODE_T (3), |
| 275 | WRAP_MODE_R (4); |
| 276 | |
| 277 | int mID; |
| 278 | SamplerParam(int id) { |
| 279 | mID = id; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | public enum SamplerValue { |
| 284 | NEAREST (0), |
| 285 | LINEAR (1), |
| 286 | LINEAR_MIP_LINEAR (2), |
| 287 | WRAP (3), |
| 288 | CLAMP (4); |
| 289 | |
| 290 | int mID; |
| 291 | SamplerValue(int id) { |
| 292 | mID = id; |
| 293 | } |
| 294 | } |
| 295 | |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 296 | ////////////////////////////////////////////////////////////////////////////////// |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 297 | // Triangle Mesh |
| 298 | |
| 299 | public class TriangleMesh extends BaseObj { |
| 300 | TriangleMesh(int id) { |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 301 | super(RenderScript.this); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 302 | mID = id; |
| 303 | } |
| 304 | |
| 305 | public void destroy() { |
| 306 | nTriangleMeshDestroy(mID); |
| 307 | mID = 0; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | public void triangleMeshBegin(Element vertex, Element index) { |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 312 | Log.e("rs", "vtx " + vertex.toString() + " " + vertex.mID + " " + vertex.mPredefinedID); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 313 | nTriangleMeshBegin(vertex.mID, index.mID); |
| 314 | } |
| 315 | |
| 316 | public void triangleMeshAddVertex_XY(float x, float y) { |
| 317 | nTriangleMeshAddVertex_XY(x, y); |
| 318 | } |
| 319 | |
| 320 | public void triangleMeshAddVertex_XYZ(float x, float y, float z) { |
| 321 | nTriangleMeshAddVertex_XYZ(x, y, z); |
| 322 | } |
| 323 | |
| 324 | public void triangleMeshAddVertex_XY_ST(float x, float y, float s, float t) { |
| 325 | nTriangleMeshAddVertex_XY_ST(x, y, s, t); |
| 326 | } |
| 327 | |
| 328 | public void triangleMeshAddVertex_XYZ_ST(float x, float y, float z, float s, float t) { |
| 329 | nTriangleMeshAddVertex_XYZ_ST(x, y, z, s, t); |
| 330 | } |
| 331 | |
Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 332 | public void triangleMeshAddVertex_XYZ_ST_NORM(float x, float y, float z, float s, float t, float nx, float ny, float nz) { |
| 333 | nTriangleMeshAddVertex_XYZ_ST_NORM(x, y, z, s, t, nx, ny, nz); |
| 334 | } |
| 335 | |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 336 | public void triangleMeshAddTriangle(int i1, int i2, int i3) { |
| 337 | nTriangleMeshAddTriangle(i1, i2, i3); |
| 338 | } |
| 339 | |
| 340 | public TriangleMesh triangleMeshCreate() { |
| 341 | int id = nTriangleMeshCreate(); |
| 342 | return new TriangleMesh(id); |
| 343 | } |
| 344 | |
| 345 | ////////////////////////////////////////////////////////////////////////////////// |
| 346 | // Script |
| 347 | |
| 348 | public class Script extends BaseObj { |
| 349 | Script(int id) { |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 350 | super(RenderScript.this); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 351 | mID = id; |
| 352 | } |
| 353 | |
| 354 | public void destroy() { |
| 355 | nScriptDestroy(mID); |
| 356 | mID = 0; |
| 357 | } |
| 358 | |
| 359 | public void bindAllocation(Allocation va, int slot) { |
| 360 | nScriptBindAllocation(mID, va.mID, slot); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | public void scriptCBegin() { |
| 365 | nScriptCBegin(); |
| 366 | } |
| 367 | |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 368 | public void scriptCSetTimeZone(String timeZone) { |
| 369 | try { |
| 370 | byte[] bytes = timeZone.getBytes("UTF-8"); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 371 | nScriptCSetTimeZone(bytes); |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 372 | } catch (java.io.UnsupportedEncodingException e) { |
| 373 | throw new RuntimeException(e); |
| 374 | } |
| 375 | } |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 376 | |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 377 | public void scriptCSetClearColor(float r, float g, float b, float a) { |
| 378 | nScriptCSetClearColor(r, g, b, a); |
| 379 | } |
| 380 | |
| 381 | public void scriptCSetClearDepth(float d) { |
| 382 | nScriptCSetClearDepth(d); |
| 383 | } |
| 384 | |
| 385 | public void scriptCSetClearStencil(int stencil) { |
| 386 | nScriptCSetClearStencil(stencil); |
| 387 | } |
| 388 | |
| 389 | public void scriptCAddType(Type t) { |
| 390 | nScriptCAddType(t.mID); |
| 391 | } |
| 392 | |
| 393 | public void scriptCSetRoot(boolean r) { |
| 394 | nScriptCSetRoot(r); |
| 395 | } |
| 396 | |
| 397 | public void scriptCSetScript(String s) { |
Jack Palevich | 43702d8 | 2009-05-28 13:38:16 -0700 | [diff] [blame] | 398 | try { |
Jack Palevich | 63975dd | 2009-05-28 15:34:15 -0700 | [diff] [blame] | 399 | byte[] bytes = s.getBytes("UTF-8"); |
| 400 | nScriptCSetScript(bytes, 0, bytes.length); |
Jack Palevich | 43702d8 | 2009-05-28 13:38:16 -0700 | [diff] [blame] | 401 | } catch (java.io.UnsupportedEncodingException e) { |
| 402 | throw new RuntimeException(e); |
| 403 | } |
| 404 | } |
| 405 | |
Jack Palevich | 43702d8 | 2009-05-28 13:38:16 -0700 | [diff] [blame] | 406 | public void scriptCSetScript(Resources resources, int id) { |
| 407 | InputStream is = resources.openRawResource(id); |
| 408 | try { |
| 409 | try { |
| 410 | scriptCSetScript(is); |
| 411 | } finally { |
| 412 | is.close(); |
| 413 | } |
| 414 | } catch(IOException e) { |
| 415 | throw new Resources.NotFoundException(); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | public void scriptCSetScript(InputStream is) throws IOException { |
| 420 | byte[] buf = new byte[1024]; |
| 421 | int currentPos = 0; |
| 422 | while(true) { |
| 423 | int bytesLeft = buf.length - currentPos; |
| 424 | if (bytesLeft == 0) { |
| 425 | byte[] buf2 = new byte[buf.length * 2]; |
| 426 | System.arraycopy(buf, 0, buf2, 0, buf.length); |
| 427 | buf = buf2; |
| 428 | bytesLeft = buf.length - currentPos; |
| 429 | } |
| 430 | int bytesRead = is.read(buf, currentPos, bytesLeft); |
| 431 | if (bytesRead <= 0) { |
| 432 | break; |
| 433 | } |
| 434 | currentPos += bytesRead; |
| 435 | } |
| 436 | nScriptCSetScript(buf, 0, currentPos); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | public Script scriptCCreate() { |
| 440 | int id = nScriptCCreate(); |
| 441 | return new Script(id); |
| 442 | } |
| 443 | |
| 444 | ////////////////////////////////////////////////////////////////////////////////// |
Jason Sams | 1fe9b8c | 2009-06-11 14:46:10 -0700 | [diff] [blame] | 445 | // ProgramVertex |
| 446 | |
| 447 | public class ProgramVertex extends BaseObj { |
| 448 | ProgramVertex(int id) { |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 449 | super(RenderScript.this); |
Jason Sams | 1fe9b8c | 2009-06-11 14:46:10 -0700 | [diff] [blame] | 450 | mID = id; |
| 451 | } |
| 452 | |
| 453 | public void destroy() { |
| 454 | nProgramVertexDestroy(mID); |
| 455 | mID = 0; |
| 456 | } |
| 457 | |
| 458 | public void bindAllocation(int slot, Allocation va) { |
| 459 | nProgramVertexBindAllocation(mID, slot, va.mID); |
| 460 | } |
Jason Sams | 1fe9b8c | 2009-06-11 14:46:10 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | public void programVertexBegin(Element in, Element out) { |
| 464 | int inID = 0; |
| 465 | int outID = 0; |
| 466 | if (in != null) { |
| 467 | inID = in.mID; |
| 468 | } |
| 469 | if (out != null) { |
| 470 | outID = out.mID; |
| 471 | } |
| 472 | nProgramVertexBegin(inID, outID); |
| 473 | } |
| 474 | |
| 475 | public void programVertexSetType(int slot, Type t) { |
| 476 | nProgramVertexSetType(slot, t.mID); |
| 477 | } |
| 478 | |
Jason Sams | 1fe9b8c | 2009-06-11 14:46:10 -0700 | [diff] [blame] | 479 | public void programVertexSetTextureMatrixEnable(boolean enable) { |
| 480 | nProgramVertexSetTextureMatrixEnable(enable); |
| 481 | } |
| 482 | |
Jason Sams | ee41112 | 2009-07-21 12:20:54 -0700 | [diff] [blame] | 483 | public void programVertexAddLight(Light l) { |
| 484 | nProgramVertexAddLight(l.mID); |
| 485 | } |
| 486 | |
Jason Sams | 1fe9b8c | 2009-06-11 14:46:10 -0700 | [diff] [blame] | 487 | public ProgramVertex programVertexCreate() { |
| 488 | int id = nProgramVertexCreate(); |
| 489 | return new ProgramVertex(id); |
| 490 | } |
| 491 | |
| 492 | |
| 493 | ////////////////////////////////////////////////////////////////////////////////// |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 494 | // ProgramFragmentStore |
| 495 | |
| 496 | public class ProgramFragmentStore extends BaseObj { |
| 497 | ProgramFragmentStore(int id) { |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 498 | super(RenderScript.this); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 499 | mID = id; |
| 500 | } |
| 501 | |
| 502 | public void destroy() { |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 503 | nProgramFragmentStoreDestroy(mID); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 504 | mID = 0; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | public void programFragmentStoreBegin(Element in, Element out) { |
| 509 | int inID = 0; |
| 510 | int outID = 0; |
| 511 | if (in != null) { |
| 512 | inID = in.mID; |
| 513 | } |
| 514 | if (out != null) { |
| 515 | outID = out.mID; |
| 516 | } |
| 517 | nProgramFragmentStoreBegin(inID, outID); |
| 518 | } |
| 519 | |
| 520 | public void programFragmentStoreDepthFunc(DepthFunc func) { |
| 521 | nProgramFragmentStoreDepthFunc(func.mID); |
| 522 | } |
| 523 | |
| 524 | public void programFragmentStoreDepthMask(boolean enable) { |
| 525 | nProgramFragmentStoreDepthMask(enable); |
| 526 | } |
| 527 | |
| 528 | public void programFragmentStoreColorMask(boolean r, boolean g, boolean b, boolean a) { |
| 529 | nProgramFragmentStoreColorMask(r,g,b,a); |
| 530 | } |
| 531 | |
| 532 | public void programFragmentStoreBlendFunc(BlendSrcFunc src, BlendDstFunc dst) { |
| 533 | nProgramFragmentStoreBlendFunc(src.mID, dst.mID); |
| 534 | } |
| 535 | |
| 536 | public void programFragmentStoreDitherEnable(boolean enable) { |
| 537 | nProgramFragmentStoreDither(enable); |
| 538 | } |
| 539 | |
| 540 | public ProgramFragmentStore programFragmentStoreCreate() { |
| 541 | int id = nProgramFragmentStoreCreate(); |
| 542 | return new ProgramFragmentStore(id); |
| 543 | } |
| 544 | |
| 545 | ////////////////////////////////////////////////////////////////////////////////// |
| 546 | // ProgramFragment |
| 547 | |
| 548 | public class ProgramFragment extends BaseObj { |
| 549 | ProgramFragment(int id) { |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 550 | super(RenderScript.this); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 551 | mID = id; |
| 552 | } |
| 553 | |
| 554 | public void destroy() { |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 555 | nProgramFragmentDestroy(mID); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 556 | mID = 0; |
| 557 | } |
| 558 | |
| 559 | public void bindTexture(Allocation va, int slot) { |
| 560 | nProgramFragmentBindTexture(mID, slot, va.mID); |
| 561 | } |
| 562 | |
Jason Sams | 02fb2cb | 2009-05-28 15:37:57 -0700 | [diff] [blame] | 563 | public void bindSampler(Sampler vs, int slot) { |
| 564 | nProgramFragmentBindSampler(mID, slot, vs.mID); |
| 565 | } |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | public void programFragmentBegin(Element in, Element out) { |
| 569 | int inID = 0; |
| 570 | int outID = 0; |
| 571 | if (in != null) { |
| 572 | inID = in.mID; |
| 573 | } |
| 574 | if (out != null) { |
| 575 | outID = out.mID; |
| 576 | } |
| 577 | nProgramFragmentBegin(inID, outID); |
| 578 | } |
| 579 | |
| 580 | public void programFragmentSetType(int slot, Type t) { |
| 581 | nProgramFragmentSetType(slot, t.mID); |
| 582 | } |
| 583 | |
| 584 | public void programFragmentSetType(int slot, EnvMode t) { |
| 585 | nProgramFragmentSetEnvMode(slot, t.mID); |
| 586 | } |
| 587 | |
| 588 | public void programFragmentSetTexEnable(int slot, boolean enable) { |
| 589 | nProgramFragmentSetTexEnable(slot, enable); |
| 590 | } |
| 591 | |
Jason Sams | e6c8e9b | 2009-07-17 17:29:09 -0700 | [diff] [blame] | 592 | public void programFragmentSetTexEnvMode(int slot, EnvMode env) { |
| 593 | nProgramFragmentSetEnvMode(slot, env.mID); |
| 594 | } |
| 595 | |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 596 | public ProgramFragment programFragmentCreate() { |
| 597 | int id = nProgramFragmentCreate(); |
| 598 | return new ProgramFragment(id); |
| 599 | } |
| 600 | |
Jason Sams | 02fb2cb | 2009-05-28 15:37:57 -0700 | [diff] [blame] | 601 | ////////////////////////////////////////////////////////////////////////////////// |
| 602 | // Sampler |
| 603 | |
| 604 | public class Sampler extends BaseObj { |
| 605 | Sampler(int id) { |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 606 | super(RenderScript.this); |
Jason Sams | 02fb2cb | 2009-05-28 15:37:57 -0700 | [diff] [blame] | 607 | mID = id; |
| 608 | } |
| 609 | |
| 610 | public void destroy() { |
| 611 | nSamplerDestroy(mID); |
| 612 | mID = 0; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | public void samplerBegin() { |
| 617 | nSamplerBegin(); |
| 618 | } |
| 619 | |
| 620 | public void samplerSet(SamplerParam p, SamplerValue v) { |
| 621 | nSamplerSet(p.mID, v.mID); |
| 622 | } |
| 623 | |
| 624 | public Sampler samplerCreate() { |
| 625 | int id = nSamplerCreate(); |
| 626 | return new Sampler(id); |
| 627 | } |
| 628 | |
Jason Sams | bba134c | 2009-06-22 15:49:21 -0700 | [diff] [blame] | 629 | ////////////////////////////////////////////////////////////////////////////////// |
| 630 | // Light |
| 631 | |
| 632 | public class Light extends BaseObj { |
| 633 | Light(int id) { |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 634 | super(RenderScript.this); |
Jason Sams | bba134c | 2009-06-22 15:49:21 -0700 | [diff] [blame] | 635 | mID = id; |
| 636 | } |
| 637 | |
| 638 | public void destroy() { |
| 639 | nLightDestroy(mID); |
| 640 | mID = 0; |
| 641 | } |
| 642 | |
| 643 | public void setColor(float r, float g, float b) { |
| 644 | nLightSetColor(mID, r, g, b); |
| 645 | } |
| 646 | |
| 647 | public void setPosition(float x, float y, float z) { |
| 648 | nLightSetPosition(mID, x, y, z); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | public void lightBegin() { |
| 653 | nLightBegin(); |
| 654 | } |
| 655 | |
| 656 | public void lightSetIsMono(boolean isMono) { |
| 657 | nLightSetIsMono(isMono); |
| 658 | } |
| 659 | |
| 660 | public void lightSetIsLocal(boolean isLocal) { |
| 661 | nLightSetIsLocal(isLocal); |
| 662 | } |
| 663 | |
| 664 | public Light lightCreate() { |
| 665 | int id = nLightCreate(); |
| 666 | return new Light(id); |
| 667 | } |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 668 | |
Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 669 | ////////////////////////////////////////////////////////////////////////////////// |
| 670 | // File |
| 671 | |
| 672 | public class File extends BaseObj { |
| 673 | File(int id) { |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 674 | super(RenderScript.this); |
Jason Sams | 64676f3 | 2009-07-08 18:01:53 -0700 | [diff] [blame] | 675 | mID = id; |
| 676 | } |
| 677 | |
| 678 | public void destroy() { |
| 679 | //nLightDestroy(mID); |
| 680 | mID = 0; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | public File fileOpen(String s) throws IllegalStateException, IllegalArgumentException |
| 685 | { |
| 686 | if(s.length() < 1) { |
| 687 | throw new IllegalArgumentException("fileOpen does not accept a zero length string."); |
| 688 | } |
| 689 | |
| 690 | try { |
| 691 | byte[] bytes = s.getBytes("UTF-8"); |
| 692 | int id = nFileOpen(bytes); |
| 693 | return new File(id); |
| 694 | } catch (java.io.UnsupportedEncodingException e) { |
| 695 | throw new RuntimeException(e); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 700 | /////////////////////////////////////////////////////////////////////////////////// |
| 701 | // Root state |
| 702 | |
| 703 | public void contextBindRootScript(Script s) { |
| 704 | nContextBindRootScript(s.mID); |
| 705 | } |
| 706 | |
| 707 | //public void contextBindSampler(Sampler s, int slot) { |
| 708 | //nContextBindSampler(s.mID); |
| 709 | //} |
| 710 | |
| 711 | public void contextBindProgramFragmentStore(ProgramFragmentStore pfs) { |
| 712 | nContextBindProgramFragmentStore(pfs.mID); |
| 713 | } |
| 714 | |
| 715 | public void contextBindProgramFragment(ProgramFragment pf) { |
| 716 | nContextBindProgramFragment(pf.mID); |
| 717 | } |
| 718 | |
Jason Sams | 0826a6f | 2009-06-15 19:04:56 -0700 | [diff] [blame] | 719 | public void contextBindProgramVertex(ProgramVertex pf) { |
| 720 | nContextBindProgramVertex(pf.mID); |
| 721 | } |
| 722 | |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 723 | /* |
| 724 | RsAdapter2D rsAdapter2DCreate (); |
| 725 | void rsAdapter2DBindAllocation (RsAdapter2D adapt, RsAllocation alloc); |
| 726 | void rsAdapter2DDestroy (RsAdapter2D adapter); |
| 727 | void rsAdapter2DSetConstraint (RsAdapter2D adapter, RsDimension dim, uint32_t value); |
| 728 | void rsAdapter2DData (RsAdapter2D adapter, const void * data); |
| 729 | void rsAdapter2DSubData (RsAdapter2D adapter, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void * data); |
| 730 | void rsSamplerBegin (); |
| 731 | void rsSamplerSet (RsSamplerParam p, RsSamplerValue value); |
| 732 | RsSampler rsSamplerCreate (); |
| 733 | void rsSamplerBind (RsSampler sampler, RsAllocation alloc); |
| 734 | */ |
| 735 | |
| 736 | } |
| 737 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 738 | |