RenderScript.java revision b8c5a84e7c23746a3fc26013e0880d3d95ca6588
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 17package android.renderscript; 18 19import java.io.IOException; 20import java.io.InputStream; 21 22import android.content.res.Resources; 23import android.graphics.Bitmap; 24import android.util.Config; 25import android.util.Log; 26import android.view.Surface; 27 28 29/** 30 * @hide 31 * 32 **/ 33public class RenderScript { 34 static final String LOG_TAG = "libRS_jni"; 35 private static final boolean DEBUG = false; 36 private static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV; 37 38 39 40 /* 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 47 48 static { 49 sInitialized = false; 50 try { 51 System.loadLibrary("rs_jni"); 52 _nInit(); 53 sInitialized = true; 54 } catch (UnsatisfiedLinkError e) { 55 Log.d(LOG_TAG, "RenderScript JNI library not found!"); 56 } 57 } 58 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); 63 64 //void rsContextBindSampler (uint32_t slot, RsSampler sampler); 65 //void rsContextBindRootScript (RsScript sampler); 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); 71 72 native void nAssignName(int obj, byte[] name); 73 native int nFileOpen(byte[] name); 74 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); 81 82 native void nTypeBegin(int elementID); 83 native void nTypeAdd(int dim, int val); 84 native int nTypeCreate(); 85 native void nTypeDestroy(int id); 86 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); 92 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); 101 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(); 111 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(); 120 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(); 132 133 native void nSamplerDestroy(int sampler); 134 native void nSamplerBegin(); 135 native void nSamplerSet(int param, int value); 136 native int nSamplerCreate(); 137 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); 146 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); 155 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(); 163 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); 171 172 173 private int mDev; 174 private int mContext; 175 private Surface mSurface; 176 177 private static boolean mElementsInitialized = false; 178 179 /////////////////////////////////////////////////////////////////////////////////// 180 // 181 182 RenderScript(Surface sur) { 183 mSurface = sur; 184 mDev = nDeviceCreate(); 185 mContext = nContextCreate(mDev, mSurface, 0); 186 187 // TODO: This should be protected by a lock 188 if(!mElementsInitialized) { 189 Element.init(this); 190 mElementsInitialized = true; 191 } 192 } 193 194 ////////////////////////////////////////////////////////////////////////////////// 195 // Element 196 197 Element.Builder mElementBuilder = new Element.Builder(this); 198 public Element.Builder elementBuilderCreate() throws IllegalStateException { 199 mElementBuilder.begin(); 200 return mElementBuilder; 201 } 202 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 } 208 209 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 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 296 ////////////////////////////////////////////////////////////////////////////////// 297 // Triangle Mesh 298 299 public class TriangleMesh extends BaseObj { 300 TriangleMesh(int id) { 301 super(RenderScript.this); 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) { 312 Log.e("rs", "vtx " + vertex.toString() + " " + vertex.mID + " " + vertex.mPredefinedID); 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 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 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) { 350 super(RenderScript.this); 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 368 public void scriptCSetTimeZone(String timeZone) { 369 try { 370 byte[] bytes = timeZone.getBytes("UTF-8"); 371 nScriptCSetTimeZone(bytes); 372 } catch (java.io.UnsupportedEncodingException e) { 373 throw new RuntimeException(e); 374 } 375 } 376 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) { 398 try { 399 byte[] bytes = s.getBytes("UTF-8"); 400 nScriptCSetScript(bytes, 0, bytes.length); 401 } catch (java.io.UnsupportedEncodingException e) { 402 throw new RuntimeException(e); 403 } 404 } 405 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); 437 } 438 439 public Script scriptCCreate() { 440 int id = nScriptCCreate(); 441 return new Script(id); 442 } 443 444 ////////////////////////////////////////////////////////////////////////////////// 445 // ProgramVertex 446 447 public class ProgramVertex extends BaseObj { 448 ProgramVertex(int id) { 449 super(RenderScript.this); 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 } 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 479 public void programVertexSetTextureMatrixEnable(boolean enable) { 480 nProgramVertexSetTextureMatrixEnable(enable); 481 } 482 483 public void programVertexAddLight(Light l) { 484 nProgramVertexAddLight(l.mID); 485 } 486 487 public ProgramVertex programVertexCreate() { 488 int id = nProgramVertexCreate(); 489 return new ProgramVertex(id); 490 } 491 492 493 ////////////////////////////////////////////////////////////////////////////////// 494 // ProgramFragmentStore 495 496 public class ProgramFragmentStore extends BaseObj { 497 ProgramFragmentStore(int id) { 498 super(RenderScript.this); 499 mID = id; 500 } 501 502 public void destroy() { 503 nProgramFragmentStoreDestroy(mID); 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) { 550 super(RenderScript.this); 551 mID = id; 552 } 553 554 public void destroy() { 555 nProgramFragmentDestroy(mID); 556 mID = 0; 557 } 558 559 public void bindTexture(Allocation va, int slot) { 560 nProgramFragmentBindTexture(mID, slot, va.mID); 561 } 562 563 public void bindSampler(Sampler vs, int slot) { 564 nProgramFragmentBindSampler(mID, slot, vs.mID); 565 } 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 592 public void programFragmentSetTexEnvMode(int slot, EnvMode env) { 593 nProgramFragmentSetEnvMode(slot, env.mID); 594 } 595 596 public ProgramFragment programFragmentCreate() { 597 int id = nProgramFragmentCreate(); 598 return new ProgramFragment(id); 599 } 600 601 ////////////////////////////////////////////////////////////////////////////////// 602 // Sampler 603 604 public class Sampler extends BaseObj { 605 Sampler(int id) { 606 super(RenderScript.this); 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 629 ////////////////////////////////////////////////////////////////////////////////// 630 // Light 631 632 public class Light extends BaseObj { 633 Light(int id) { 634 super(RenderScript.this); 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 } 668 669 ////////////////////////////////////////////////////////////////////////////////// 670 // File 671 672 public class File extends BaseObj { 673 File(int id) { 674 super(RenderScript.this); 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 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 719 public void contextBindProgramVertex(ProgramVertex pf) { 720 nContextBindProgramVertex(pf.mID); 721 } 722 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 738 739