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 | |
Jack Palevich | df98851 | 2009-05-27 17:00:45 -0700 | [diff] [blame] | 17 | package com.android.fountain; |
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.InputStream; |
| 20 | import java.io.IOException; |
| 21 | |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 22 | import android.os.Bundle; |
Jack Palevich | 43702d8 | 2009-05-28 13:38:16 -0700 | [diff] [blame^] | 23 | import android.content.res.Resources; |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 24 | import android.util.Log; |
| 25 | import android.util.Config; |
| 26 | import android.view.Menu; |
| 27 | import android.view.MenuItem; |
| 28 | import android.view.Window; |
| 29 | import android.view.View; |
| 30 | import android.view.Surface; |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 31 | import android.graphics.Bitmap; |
| 32 | import android.graphics.Color; |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 33 | |
| 34 | public class RenderScript { |
| 35 | private static final String LOG_TAG = "libRS_jni"; |
| 36 | private static final boolean DEBUG = false; |
| 37 | private static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV; |
| 38 | |
| 39 | |
| 40 | |
| 41 | /* |
| 42 | * We use a class initializer to allow the native code to cache some |
| 43 | * field offsets. |
| 44 | */ |
| 45 | private static boolean sInitialized; |
| 46 | native private static void _nInit(); |
| 47 | |
| 48 | static { |
| 49 | sInitialized = false; |
| 50 | try { |
| 51 | System.loadLibrary("RS_jni"); |
| 52 | Log.e(LOG_TAG, "*** Renderscript INIT"); |
| 53 | _nInit(); |
| 54 | Log.e(LOG_TAG, "*** Renderscript INIT 3"); |
| 55 | sInitialized = true; |
| 56 | } catch (UnsatisfiedLinkError e) { |
| 57 | Log.d(LOG_TAG, "RenderScript JNI library not found!"); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | native private int nDeviceCreate(); |
| 62 | native private void nDeviceDestroy(int dev); |
| 63 | native private int nContextCreate(int dev, Surface sur, int ver); |
| 64 | native private void nContextDestroy(int con); |
| 65 | |
| 66 | //void rsContextBindSampler (uint32_t slot, RsSampler sampler); |
| 67 | //void rsContextBindRootScript (RsScript sampler); |
| 68 | native private void nContextBindRootScript(int script); |
| 69 | native private void nContextBindSampler(int sampler, int slot); |
| 70 | native private void nContextBindProgramFragmentStore(int pfs); |
| 71 | native private void nContextBindProgramFragment(int pf); |
| 72 | |
| 73 | native private void nElementBegin(); |
| 74 | native private void nElementAddPredefined(int predef); |
| 75 | native private void nElementAdd(int kind, int type, int norm, int bits); |
| 76 | native private int nElementCreate(); |
| 77 | native private int nElementGetPredefined(int predef); |
| 78 | native private void nElementDestroy(int obj); |
| 79 | |
| 80 | native private void nTypeBegin(int elementID); |
| 81 | native private void nTypeAdd(int dim, int val); |
| 82 | native private int nTypeCreate(); |
| 83 | native private void nTypeDestroy(int id); |
| 84 | |
| 85 | native private int nAllocationCreateTyped(int type); |
| 86 | native private int nAllocationCreatePredefSized(int predef, int count); |
| 87 | native private int nAllocationCreateSized(int elem, int count); |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 88 | native private int nAllocationCreateFromBitmap(int w, int h, int dstFmt, int srcFmt, boolean genMips, int[] data); |
| 89 | |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 90 | //native private int nAllocationCreateFromBitmap(type.mID); |
| 91 | native private void nAllocationUploadToTexture(int alloc, int baseMioLevel); |
| 92 | native private void nAllocationDestroy(int alloc); |
| 93 | native private void nAllocationData(int id, int[] d); |
| 94 | native private void nAllocationData(int id, float[] d); |
| 95 | native private void nAllocationSubData1D(int id, int off, int count, int[] d); |
| 96 | native private void nAllocationSubData1D(int id, int off, int count, float[] d); |
| 97 | native private void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d); |
| 98 | native private void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d); |
| 99 | |
| 100 | native private void nTriangleMeshDestroy(int id); |
| 101 | native private void nTriangleMeshBegin(int vertex, int index); |
| 102 | native private void nTriangleMeshAddVertex_XY (float x, float y); |
| 103 | native private void nTriangleMeshAddVertex_XYZ (float x, float y, float z); |
| 104 | native private void nTriangleMeshAddVertex_XY_ST (float x, float y, float s, float t); |
| 105 | native private void nTriangleMeshAddVertex_XYZ_ST (float x, float y, float z, float s, float t); |
| 106 | native private void nTriangleMeshAddTriangle(int i1, int i2, int i3); |
| 107 | native private int nTriangleMeshCreate(); |
| 108 | |
| 109 | native private void nAdapter1DDestroy(int id); |
| 110 | native private void nAdapter1DBindAllocation(int ad, int alloc); |
| 111 | native private void nAdapter1DSetConstraint(int ad, int dim, int value); |
| 112 | native private void nAdapter1DData(int ad, int[] d); |
| 113 | native private void nAdapter1DSubData(int ad, int off, int count, int[] d); |
| 114 | native private void nAdapter1DData(int ad, float[] d); |
| 115 | native private void nAdapter1DSubData(int ad, int off, int count, float[] d); |
| 116 | native private int nAdapter1DCreate(); |
| 117 | |
| 118 | native private void nScriptDestroy(int script); |
| 119 | native private void nScriptBindAllocation(int vtm, int alloc, int slot); |
| 120 | native private void nScriptCBegin(); |
| 121 | native private void nScriptCSetClearColor(float r, float g, float b, float a); |
| 122 | native private void nScriptCSetClearDepth(float depth); |
| 123 | native private void nScriptCSetClearStencil(int stencil); |
| 124 | native private void nScriptCAddType(int type); |
| 125 | native private void nScriptCSetRoot(boolean isRoot); |
Jack Palevich | 43702d8 | 2009-05-28 13:38:16 -0700 | [diff] [blame^] | 126 | native private void nScriptCSetScript(byte[] script, int offset, int length); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 127 | native private int nScriptCCreate(); |
| 128 | |
| 129 | |
| 130 | native private void nProgramFragmentStoreBegin(int in, int out); |
| 131 | native private void nProgramFragmentStoreDepthFunc(int func); |
| 132 | native private void nProgramFragmentStoreDepthMask(boolean enable); |
| 133 | native private void nProgramFragmentStoreColorMask(boolean r, boolean g, boolean b, boolean a); |
| 134 | native private void nProgramFragmentStoreBlendFunc(int src, int dst); |
| 135 | native private void nProgramFragmentStoreDither(boolean enable); |
| 136 | native private int nProgramFragmentStoreCreate(); |
| 137 | |
| 138 | native private void nProgramFragmentBegin(int in, int out); |
| 139 | native private void nProgramFragmentBindTexture(int vpf, int slot, int a); |
| 140 | native private void nProgramFragmentBindSampler(int vpf, int slot, int s); |
| 141 | native private void nProgramFragmentSetType(int slot, int vt); |
| 142 | native private void nProgramFragmentSetEnvMode(int slot, int env); |
| 143 | native private void nProgramFragmentSetTexEnable(int slot, boolean enable); |
| 144 | native private int nProgramFragmentCreate(); |
| 145 | |
| 146 | |
| 147 | private int mDev; |
| 148 | private int mContext; |
| 149 | private Surface mSurface; |
| 150 | |
| 151 | |
| 152 | |
| 153 | /////////////////////////////////////////////////////////////////////////////////// |
Jack Palevich | 43702d8 | 2009-05-28 13:38:16 -0700 | [diff] [blame^] | 154 | // |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 155 | |
| 156 | RenderScript(Surface sur) { |
| 157 | mSurface = sur; |
| 158 | mDev = nDeviceCreate(); |
| 159 | mContext = nContextCreate(mDev, mSurface, 0); |
| 160 | } |
| 161 | |
| 162 | private class BaseObj { |
| 163 | BaseObj() { |
| 164 | mID = 0; |
| 165 | } |
| 166 | |
| 167 | int mID; |
| 168 | protected void finalize() throws Throwable |
| 169 | { |
| 170 | if (mID != 0) { |
Jack Palevich | 43702d8 | 2009-05-28 13:38:16 -0700 | [diff] [blame^] | 171 | Log.v(LOG_TAG, |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 172 | "Element finalized without having released the RS reference."); |
| 173 | } |
| 174 | super.finalize(); |
| 175 | } |
| 176 | } |
| 177 | |
Jack Palevich | 43702d8 | 2009-05-28 13:38:16 -0700 | [diff] [blame^] | 178 | |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 179 | ////////////////////////////////////////////////////////////////////////////////// |
| 180 | // Element |
| 181 | |
| 182 | public enum ElementPredefined { |
| 183 | USER_U8 (0), |
| 184 | USER_I8 (1), |
| 185 | USER_U16 (2), |
| 186 | USER_I16 (3), |
| 187 | USER_U32 (4), |
| 188 | USER_I32 (5), |
| 189 | USER_FLOAT (6), |
| 190 | |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 191 | A_8 (7), |
| 192 | RGB_565 (8), |
| 193 | RGBA_5551 (9), |
| 194 | RGBA_4444 (10), |
| 195 | RGB_888 (11), |
| 196 | RGBA_8888 (12), |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 197 | |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 198 | INDEX_16 (13), |
| 199 | INDEX_32 (14), |
| 200 | XY_F32 (15), |
| 201 | XYZ_F32 (16), |
| 202 | ST_XY_F32 (17), |
| 203 | ST_XYZ_F32 (18), |
| 204 | NORM_XYZ_F32 (19), |
| 205 | NORM_ST_XYZ_F32 (20); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 206 | |
| 207 | int mID; |
| 208 | ElementPredefined(int id) { |
| 209 | mID = id; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | public enum DataType { |
| 214 | FLOAT (0), |
| 215 | UNSIGNED (1), |
| 216 | SIGNED (2); |
| 217 | |
| 218 | int mID; |
| 219 | DataType(int id) { |
| 220 | mID = id; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | public enum DataKind { |
| 225 | USER (0), |
| 226 | RED (1), |
| 227 | GREEN (2), |
| 228 | BLUE (3), |
| 229 | ALPHA (4), |
| 230 | LUMINANCE (5), |
| 231 | INTENSITY (6), |
| 232 | X (7), |
| 233 | Y (8), |
| 234 | Z (9), |
| 235 | W (10), |
| 236 | S (11), |
| 237 | T (12), |
| 238 | Q (13), |
| 239 | R (14), |
| 240 | NX (15), |
| 241 | NY (16), |
| 242 | NZ (17), |
| 243 | INDEX (18); |
| 244 | |
| 245 | int mID; |
| 246 | DataKind(int id) { |
| 247 | mID = id; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | public enum DepthFunc { |
| 252 | ALWAYS (0), |
| 253 | LESS (1), |
| 254 | LEQUAL (2), |
| 255 | GREATER (3), |
| 256 | GEQUAL (4), |
| 257 | EQUAL (5), |
| 258 | NOTEQUAL (6); |
| 259 | |
| 260 | int mID; |
| 261 | DepthFunc(int id) { |
| 262 | mID = id; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | public enum BlendSrcFunc { |
| 267 | ZERO (0), |
| 268 | ONE (1), |
| 269 | DST_COLOR (2), |
| 270 | ONE_MINUS_DST_COLOR (3), |
| 271 | SRC_ALPHA (4), |
| 272 | ONE_MINUS_SRC_ALPHA (5), |
| 273 | DST_ALPHA (6), |
| 274 | ONE_MINUS_DST_ALPA (7), |
| 275 | SRC_ALPHA_SATURATE (8); |
| 276 | |
| 277 | int mID; |
| 278 | BlendSrcFunc(int id) { |
| 279 | mID = id; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | public enum BlendDstFunc { |
| 284 | ZERO (0), |
| 285 | ONE (1), |
| 286 | SRC_COLOR (2), |
| 287 | ONE_MINUS_SRC_COLOR (3), |
| 288 | SRC_ALPHA (4), |
| 289 | ONE_MINUS_SRC_ALPHA (5), |
| 290 | DST_ALPHA (6), |
| 291 | ONE_MINUS_DST_ALPA (7); |
| 292 | |
| 293 | int mID; |
| 294 | BlendDstFunc(int id) { |
| 295 | mID = id; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | public enum EnvMode { |
| 300 | REPLACE (0), |
| 301 | MODULATE (1), |
| 302 | DECAL (2); |
| 303 | |
| 304 | int mID; |
| 305 | EnvMode(int id) { |
| 306 | mID = id; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | public class Element extends BaseObj { |
| 311 | Element(int id) { |
| 312 | mID = id; |
| 313 | } |
| 314 | |
| 315 | public void estroy() { |
| 316 | nElementDestroy(mID); |
| 317 | mID = 0; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | public void elementBegin() { |
| 322 | nElementBegin(); |
| 323 | } |
| 324 | |
| 325 | public void elementAddPredefined(ElementPredefined e) { |
| 326 | nElementAddPredefined(e.mID); |
| 327 | } |
| 328 | |
| 329 | public void elementAdd(DataType dt, DataKind dk, boolean isNormalized, int bits) { |
| 330 | int norm = 0; |
| 331 | if (isNormalized) { |
| 332 | norm = 1; |
| 333 | } |
| 334 | nElementAdd(dt.mID, dk.mID, norm, bits); |
| 335 | } |
| 336 | |
| 337 | public Element elementCreate() { |
| 338 | int id = nElementCreate(); |
| 339 | return new Element(id); |
| 340 | } |
| 341 | |
| 342 | public Element elementGetPredefined(ElementPredefined predef) { |
| 343 | int id = nElementGetPredefined(predef.mID); |
| 344 | return new Element(id); |
| 345 | } |
| 346 | |
| 347 | |
| 348 | ////////////////////////////////////////////////////////////////////////////////// |
| 349 | // Type |
| 350 | |
| 351 | public enum Dimension { |
| 352 | X (0), |
| 353 | Y (1), |
| 354 | Z (2), |
| 355 | LOD (3), |
| 356 | FACE (4), |
| 357 | ARRAY_0 (100); |
| 358 | |
| 359 | int mID; |
| 360 | Dimension(int id) { |
| 361 | mID = id; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | public class Type extends BaseObj { |
| 366 | Type(int id) { |
| 367 | mID = id; |
| 368 | } |
| 369 | |
| 370 | public void destroy() { |
| 371 | nTypeDestroy(mID); |
| 372 | mID = 0; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | public void typeBegin(Element e) { |
| 377 | nTypeBegin(e.mID); |
| 378 | } |
| 379 | |
| 380 | public void typeAdd(Dimension d, int value) { |
| 381 | nTypeAdd(d.mID, value); |
| 382 | } |
| 383 | |
| 384 | public Type typeCreate() { |
| 385 | int id = nTypeCreate(); |
| 386 | return new Type(id); |
| 387 | } |
| 388 | |
| 389 | |
| 390 | ////////////////////////////////////////////////////////////////////////////////// |
| 391 | // Allocation |
| 392 | |
| 393 | public class Allocation extends BaseObj { |
| 394 | Allocation(int id) { |
| 395 | mID = id; |
| 396 | } |
| 397 | |
| 398 | public void uploadToTexture(int baseMipLevel) { |
| 399 | nAllocationUploadToTexture(mID, baseMipLevel); |
| 400 | } |
| 401 | |
| 402 | public void destroy() { |
| 403 | nAllocationDestroy(mID); |
| 404 | mID = 0; |
| 405 | } |
| 406 | |
| 407 | public void data(int[] d) { |
| 408 | nAllocationData(mID, d); |
| 409 | } |
| 410 | |
| 411 | public void data(float[] d) { |
| 412 | nAllocationData(mID, d); |
| 413 | } |
| 414 | |
| 415 | public void subData1D(int off, int count, int[] d) { |
| 416 | nAllocationSubData1D(mID, off, count, d); |
| 417 | } |
| 418 | |
| 419 | public void subData1D(int off, int count, float[] d) { |
| 420 | nAllocationSubData1D(mID, off, count, d); |
| 421 | } |
| 422 | |
| 423 | public void subData2D(int xoff, int yoff, int w, int h, int[] d) { |
| 424 | nAllocationSubData2D(mID, xoff, yoff, w, h, d); |
| 425 | } |
| 426 | |
| 427 | public void subData2D(int xoff, int yoff, int w, int h, float[] d) { |
| 428 | nAllocationSubData2D(mID, xoff, yoff, w, h, d); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | public Allocation allocationCreateTyped(Type type) { |
| 433 | int id = nAllocationCreateTyped(type.mID); |
| 434 | return new Allocation(id); |
| 435 | } |
| 436 | |
| 437 | public Allocation allocationCreatePredefSized(ElementPredefined e, int count) { |
| 438 | int id = nAllocationCreatePredefSized(e.mID, count); |
| 439 | return new Allocation(id); |
| 440 | } |
| 441 | |
| 442 | public Allocation allocationCreateSized(Element e, int count) { |
| 443 | int id = nAllocationCreateSized(e.mID, count); |
| 444 | return new Allocation(id); |
| 445 | } |
| 446 | |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 447 | public Allocation allocationCreateFromBitmap(Bitmap b, ElementPredefined dstFmt, boolean genMips) { |
| 448 | int w = b.getWidth(); |
| 449 | int h = b.getHeight(); |
| 450 | int[] data = new int[w * h]; |
| 451 | |
| 452 | int outPtr = 0; |
| 453 | for(int y=0; y < h; y++) { |
| 454 | for(int x=0; x < w; x++) { |
| 455 | data[outPtr] = b.getPixel(x, y); |
| 456 | outPtr++; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | int srcFmt = 0; |
| 461 | /* |
| 462 | switch(b.getConfig()) { |
| 463 | case ALPHA_8: |
| 464 | srcFmt = ElementPredefined.A_8.mID; |
| 465 | break; |
| 466 | case ARGB_4444: |
| 467 | srcFmt = ElementPredefined.RGBA_4444.mID; |
| 468 | break; |
| 469 | case ARGB_8888: |
| 470 | srcFmt = ElementPredefined.RGBA_8888.mID; |
| 471 | break; |
| 472 | case RGB_565: |
| 473 | srcFmt = ElementPredefined.RGB_565.mID; |
| 474 | break; |
| 475 | default: |
| 476 | Log.e(LOG_TAG, "allocationCreateFromBitmap, unknown bitmap format"); |
Jack Palevich | 43702d8 | 2009-05-28 13:38:16 -0700 | [diff] [blame^] | 477 | } |
| 478 | */ |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 479 | |
| 480 | srcFmt = ElementPredefined.RGBA_8888.mID; |
| 481 | |
| 482 | int id = nAllocationCreateFromBitmap(w, h, dstFmt.mID, srcFmt, genMips, data); |
| 483 | return new Allocation(id); |
| 484 | } |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 485 | |
| 486 | ////////////////////////////////////////////////////////////////////////////////// |
| 487 | // Adapter1D |
| 488 | |
| 489 | public class Adapter1D extends BaseObj { |
| 490 | Adapter1D(int id) { |
| 491 | mID = id; |
| 492 | } |
| 493 | |
| 494 | public void destroy() { |
| 495 | nAdapter1DDestroy(mID); |
| 496 | mID = 0; |
| 497 | } |
| 498 | |
| 499 | public void bindAllocation(Allocation a) { |
| 500 | nAdapter1DBindAllocation(mID, a.mID); |
| 501 | } |
| 502 | |
| 503 | public void setConstraint(Dimension dim, int value) { |
| 504 | nAdapter1DSetConstraint(mID, dim.mID, value); |
| 505 | } |
| 506 | |
| 507 | public void data(int[] d) { |
| 508 | nAdapter1DData(mID, d); |
| 509 | } |
| 510 | |
| 511 | public void subData(int off, int count, int[] d) { |
| 512 | nAdapter1DSubData(mID, off, count, d); |
| 513 | } |
| 514 | |
| 515 | public void data(float[] d) { |
| 516 | nAdapter1DData(mID, d); |
| 517 | } |
| 518 | |
| 519 | public void subData(int off, int count, float[] d) { |
| 520 | nAdapter1DSubData(mID, off, count, d); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | public Adapter1D adapter1DCreate() { |
| 525 | int id = nAdapter1DCreate(); |
| 526 | return new Adapter1D(id); |
| 527 | } |
| 528 | |
| 529 | |
| 530 | ////////////////////////////////////////////////////////////////////////////////// |
| 531 | // Triangle Mesh |
| 532 | |
| 533 | public class TriangleMesh extends BaseObj { |
| 534 | TriangleMesh(int id) { |
| 535 | mID = id; |
| 536 | } |
| 537 | |
| 538 | public void destroy() { |
| 539 | nTriangleMeshDestroy(mID); |
| 540 | mID = 0; |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | public void triangleMeshBegin(Element vertex, Element index) { |
| 545 | nTriangleMeshBegin(vertex.mID, index.mID); |
| 546 | } |
| 547 | |
| 548 | public void triangleMeshAddVertex_XY(float x, float y) { |
| 549 | nTriangleMeshAddVertex_XY(x, y); |
| 550 | } |
| 551 | |
| 552 | public void triangleMeshAddVertex_XYZ(float x, float y, float z) { |
| 553 | nTriangleMeshAddVertex_XYZ(x, y, z); |
| 554 | } |
| 555 | |
| 556 | public void triangleMeshAddVertex_XY_ST(float x, float y, float s, float t) { |
| 557 | nTriangleMeshAddVertex_XY_ST(x, y, s, t); |
| 558 | } |
| 559 | |
| 560 | public void triangleMeshAddVertex_XYZ_ST(float x, float y, float z, float s, float t) { |
| 561 | nTriangleMeshAddVertex_XYZ_ST(x, y, z, s, t); |
| 562 | } |
| 563 | |
| 564 | public void triangleMeshAddTriangle(int i1, int i2, int i3) { |
| 565 | nTriangleMeshAddTriangle(i1, i2, i3); |
| 566 | } |
| 567 | |
| 568 | public TriangleMesh triangleMeshCreate() { |
| 569 | int id = nTriangleMeshCreate(); |
| 570 | return new TriangleMesh(id); |
| 571 | } |
| 572 | |
| 573 | ////////////////////////////////////////////////////////////////////////////////// |
| 574 | // Script |
| 575 | |
| 576 | public class Script extends BaseObj { |
| 577 | Script(int id) { |
| 578 | mID = id; |
| 579 | } |
| 580 | |
| 581 | public void destroy() { |
| 582 | nScriptDestroy(mID); |
| 583 | mID = 0; |
| 584 | } |
| 585 | |
| 586 | public void bindAllocation(Allocation va, int slot) { |
| 587 | nScriptBindAllocation(mID, va.mID, slot); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | public void scriptCBegin() { |
| 592 | nScriptCBegin(); |
| 593 | } |
| 594 | |
| 595 | public void scriptCSetClearColor(float r, float g, float b, float a) { |
| 596 | nScriptCSetClearColor(r, g, b, a); |
| 597 | } |
| 598 | |
| 599 | public void scriptCSetClearDepth(float d) { |
| 600 | nScriptCSetClearDepth(d); |
| 601 | } |
| 602 | |
| 603 | public void scriptCSetClearStencil(int stencil) { |
| 604 | nScriptCSetClearStencil(stencil); |
| 605 | } |
| 606 | |
| 607 | public void scriptCAddType(Type t) { |
| 608 | nScriptCAddType(t.mID); |
| 609 | } |
| 610 | |
| 611 | public void scriptCSetRoot(boolean r) { |
| 612 | nScriptCSetRoot(r); |
| 613 | } |
| 614 | |
| 615 | public void scriptCSetScript(String s) { |
Jack Palevich | 43702d8 | 2009-05-28 13:38:16 -0700 | [diff] [blame^] | 616 | try { |
| 617 | scriptCSetScript(s.getBytes("UTF-8")); |
| 618 | } catch (java.io.UnsupportedEncodingException e) { |
| 619 | throw new RuntimeException(e); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | public void scriptCSetScript(byte[] utf8Bytes) { |
| 624 | scriptCSetScript(utf8Bytes, 0, utf8Bytes.length); |
| 625 | } |
| 626 | |
| 627 | public void scriptCSetScript(byte[] utf8Bytes, int offset, int length) { |
| 628 | nScriptCSetScript(utf8Bytes, offset, length); |
| 629 | } |
| 630 | |
| 631 | public void scriptCSetScript(Resources resources, int id) { |
| 632 | InputStream is = resources.openRawResource(id); |
| 633 | try { |
| 634 | try { |
| 635 | scriptCSetScript(is); |
| 636 | } finally { |
| 637 | is.close(); |
| 638 | } |
| 639 | } catch(IOException e) { |
| 640 | throw new Resources.NotFoundException(); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | public void scriptCSetScript(InputStream is) throws IOException { |
| 645 | byte[] buf = new byte[1024]; |
| 646 | int currentPos = 0; |
| 647 | while(true) { |
| 648 | int bytesLeft = buf.length - currentPos; |
| 649 | if (bytesLeft == 0) { |
| 650 | byte[] buf2 = new byte[buf.length * 2]; |
| 651 | System.arraycopy(buf, 0, buf2, 0, buf.length); |
| 652 | buf = buf2; |
| 653 | bytesLeft = buf.length - currentPos; |
| 654 | } |
| 655 | int bytesRead = is.read(buf, currentPos, bytesLeft); |
| 656 | if (bytesRead <= 0) { |
| 657 | break; |
| 658 | } |
| 659 | currentPos += bytesRead; |
| 660 | } |
| 661 | nScriptCSetScript(buf, 0, currentPos); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | public Script scriptCCreate() { |
| 665 | int id = nScriptCCreate(); |
| 666 | return new Script(id); |
| 667 | } |
| 668 | |
| 669 | ////////////////////////////////////////////////////////////////////////////////// |
| 670 | // ProgramFragmentStore |
| 671 | |
| 672 | public class ProgramFragmentStore extends BaseObj { |
| 673 | ProgramFragmentStore(int id) { |
| 674 | mID = id; |
| 675 | } |
| 676 | |
| 677 | public void destroy() { |
| 678 | nScriptDestroy(mID); |
| 679 | mID = 0; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | public void programFragmentStoreBegin(Element in, Element out) { |
| 684 | int inID = 0; |
| 685 | int outID = 0; |
| 686 | if (in != null) { |
| 687 | inID = in.mID; |
| 688 | } |
| 689 | if (out != null) { |
| 690 | outID = out.mID; |
| 691 | } |
| 692 | nProgramFragmentStoreBegin(inID, outID); |
| 693 | } |
| 694 | |
| 695 | public void programFragmentStoreDepthFunc(DepthFunc func) { |
| 696 | nProgramFragmentStoreDepthFunc(func.mID); |
| 697 | } |
| 698 | |
| 699 | public void programFragmentStoreDepthMask(boolean enable) { |
| 700 | nProgramFragmentStoreDepthMask(enable); |
| 701 | } |
| 702 | |
| 703 | public void programFragmentStoreColorMask(boolean r, boolean g, boolean b, boolean a) { |
| 704 | nProgramFragmentStoreColorMask(r,g,b,a); |
| 705 | } |
| 706 | |
| 707 | public void programFragmentStoreBlendFunc(BlendSrcFunc src, BlendDstFunc dst) { |
| 708 | nProgramFragmentStoreBlendFunc(src.mID, dst.mID); |
| 709 | } |
| 710 | |
| 711 | public void programFragmentStoreDitherEnable(boolean enable) { |
| 712 | nProgramFragmentStoreDither(enable); |
| 713 | } |
| 714 | |
| 715 | public ProgramFragmentStore programFragmentStoreCreate() { |
| 716 | int id = nProgramFragmentStoreCreate(); |
| 717 | return new ProgramFragmentStore(id); |
| 718 | } |
| 719 | |
| 720 | ////////////////////////////////////////////////////////////////////////////////// |
| 721 | // ProgramFragment |
| 722 | |
| 723 | public class ProgramFragment extends BaseObj { |
| 724 | ProgramFragment(int id) { |
| 725 | mID = id; |
| 726 | } |
| 727 | |
| 728 | public void destroy() { |
| 729 | nScriptDestroy(mID); |
| 730 | mID = 0; |
| 731 | } |
| 732 | |
| 733 | public void bindTexture(Allocation va, int slot) { |
| 734 | nProgramFragmentBindTexture(mID, slot, va.mID); |
| 735 | } |
| 736 | |
| 737 | //public void bindSampler(Sampler vs, int slot) { |
| 738 | //nProgramFragmentBindSampler(mID, slot, vs.mID); |
| 739 | //} |
| 740 | } |
| 741 | |
| 742 | public void programFragmentBegin(Element in, Element out) { |
| 743 | int inID = 0; |
| 744 | int outID = 0; |
| 745 | if (in != null) { |
| 746 | inID = in.mID; |
| 747 | } |
| 748 | if (out != null) { |
| 749 | outID = out.mID; |
| 750 | } |
| 751 | nProgramFragmentBegin(inID, outID); |
| 752 | } |
| 753 | |
| 754 | public void programFragmentSetType(int slot, Type t) { |
| 755 | nProgramFragmentSetType(slot, t.mID); |
| 756 | } |
| 757 | |
| 758 | public void programFragmentSetType(int slot, EnvMode t) { |
| 759 | nProgramFragmentSetEnvMode(slot, t.mID); |
| 760 | } |
| 761 | |
| 762 | public void programFragmentSetTexEnable(int slot, boolean enable) { |
| 763 | nProgramFragmentSetTexEnable(slot, enable); |
| 764 | } |
| 765 | |
| 766 | public ProgramFragment programFragmentCreate() { |
| 767 | int id = nProgramFragmentCreate(); |
| 768 | return new ProgramFragment(id); |
| 769 | } |
| 770 | |
| 771 | |
| 772 | /////////////////////////////////////////////////////////////////////////////////// |
| 773 | // Root state |
| 774 | |
| 775 | public void contextBindRootScript(Script s) { |
| 776 | nContextBindRootScript(s.mID); |
| 777 | } |
| 778 | |
| 779 | //public void contextBindSampler(Sampler s, int slot) { |
| 780 | //nContextBindSampler(s.mID); |
| 781 | //} |
| 782 | |
| 783 | public void contextBindProgramFragmentStore(ProgramFragmentStore pfs) { |
| 784 | nContextBindProgramFragmentStore(pfs.mID); |
| 785 | } |
| 786 | |
| 787 | public void contextBindProgramFragment(ProgramFragment pf) { |
| 788 | nContextBindProgramFragment(pf.mID); |
| 789 | } |
| 790 | |
| 791 | /* |
| 792 | RsAdapter2D rsAdapter2DCreate (); |
| 793 | void rsAdapter2DBindAllocation (RsAdapter2D adapt, RsAllocation alloc); |
| 794 | void rsAdapter2DDestroy (RsAdapter2D adapter); |
| 795 | void rsAdapter2DSetConstraint (RsAdapter2D adapter, RsDimension dim, uint32_t value); |
| 796 | void rsAdapter2DData (RsAdapter2D adapter, const void * data); |
| 797 | void rsAdapter2DSubData (RsAdapter2D adapter, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void * data); |
| 798 | void rsSamplerBegin (); |
| 799 | void rsSamplerSet (RsSamplerParam p, RsSamplerValue value); |
| 800 | RsSampler rsSamplerCreate (); |
| 801 | void rsSamplerBind (RsSampler sampler, RsAllocation alloc); |
| 802 | */ |
| 803 | |
| 804 | } |
| 805 | |