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