RenderScript.java revision 25207df658d6a8a3e885c7017fcc25702363583c
1/* 2 * Copyright (C) 2008-2012 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.File; 20import java.lang.reflect.Method; 21import java.util.concurrent.locks.ReentrantReadWriteLock; 22 23import android.content.Context; 24import android.content.res.AssetManager; 25import android.graphics.Bitmap; 26import android.graphics.SurfaceTexture; 27import android.os.Process; 28import android.util.Log; 29import android.view.Surface; 30import android.os.SystemProperties; 31import android.os.Trace; 32 33/** 34 * This class provides access to a RenderScript context, which controls RenderScript 35 * initialization, resource management, and teardown. An instance of the RenderScript 36 * class must be created before any other RS objects can be created. 37 * 38 * <div class="special reference"> 39 * <h3>Developer Guides</h3> 40 * <p>For more information about creating an application that uses RenderScript, read the 41 * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p> 42 * </div> 43 **/ 44public class RenderScript { 45 static final long TRACE_TAG = Trace.TRACE_TAG_RS; 46 47 static final String LOG_TAG = "RenderScript_jni"; 48 static final boolean DEBUG = false; 49 @SuppressWarnings({"UnusedDeclaration", "deprecation"}) 50 static final boolean LOG_ENABLED = false; 51 52 private Context mApplicationContext; 53 54 /* 55 * We use a class initializer to allow the native code to cache some 56 * field offsets. 57 */ 58 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) // TODO: now used locally; remove? 59 static boolean sInitialized; 60 native static void _nInit(); 61 62 static Object sRuntime; 63 static Method registerNativeAllocation; 64 static Method registerNativeFree; 65 66 /* 67 * Context creation flag that specifies a normal context. 68 */ 69 public static final int CREATE_FLAG_NONE = 0x0000; 70 71 /* 72 * Context creation flag which specifies a context optimized for low 73 * latency over peak performance. This is a hint and may have no effect 74 * on some implementations. 75 */ 76 public static final int CREATE_FLAG_LOW_LATENCY = 0x0002; 77 78 /* 79 * Context creation flag which specifies a context optimized for long 80 * battery life over peak performance. This is a hint and may have no effect 81 * on some implementations. 82 */ 83 public static final int CREATE_FLAG_LOW_POWER = 0x0004; 84 85 /* 86 * Detect the bitness of the VM to allow FieldPacker to do the right thing. 87 */ 88 static native int rsnSystemGetPointerSize(); 89 static int sPointerSize; 90 91 static { 92 sInitialized = false; 93 if (!SystemProperties.getBoolean("config.disable_renderscript", false)) { 94 try { 95 Class<?> vm_runtime = Class.forName("dalvik.system.VMRuntime"); 96 Method get_runtime = vm_runtime.getDeclaredMethod("getRuntime"); 97 sRuntime = get_runtime.invoke(null); 98 registerNativeAllocation = vm_runtime.getDeclaredMethod("registerNativeAllocation", Integer.TYPE); 99 registerNativeFree = vm_runtime.getDeclaredMethod("registerNativeFree", Integer.TYPE); 100 } catch (Exception e) { 101 Log.e(LOG_TAG, "Error loading GC methods: " + e); 102 throw new RSRuntimeException("Error loading GC methods: " + e); 103 } 104 try { 105 System.loadLibrary("rs_jni"); 106 _nInit(); 107 sInitialized = true; 108 sPointerSize = rsnSystemGetPointerSize(); 109 } catch (UnsatisfiedLinkError e) { 110 Log.e(LOG_TAG, "Error loading RS jni library: " + e); 111 throw new RSRuntimeException("Error loading RS jni library: " + e); 112 } 113 } 114 } 115 116 // Non-threadsafe functions. 117 native long nDeviceCreate(); 118 native void nDeviceDestroy(long dev); 119 native void nDeviceSetConfig(long dev, int param, int value); 120 native int nContextGetUserMessage(long con, int[] data); 121 native String nContextGetErrorMessage(long con); 122 native int nContextPeekMessage(long con, int[] subID); 123 native void nContextInitToClient(long con); 124 native void nContextDeinitToClient(long con); 125 126 static File mCacheDir; 127 128 // this should be a monotonically increasing ID 129 // used in conjunction with the API version of a device 130 static final long sMinorID = 1; 131 132 /** 133 * Returns an identifier that can be used to identify a particular 134 * minor version of RS. 135 * 136 * @hide 137 */ 138 public static long getMinorID() { 139 return sMinorID; 140 } 141 142 /** 143 * Sets the directory to use as a persistent storage for the 144 * renderscript object file cache. 145 * 146 * @hide 147 * @param cacheDir A directory the current process can write to 148 */ 149 public static void setupDiskCache(File cacheDir) { 150 if (!sInitialized) { 151 Log.e(LOG_TAG, "RenderScript.setupDiskCache() called when disabled"); 152 return; 153 } 154 155 // Defer creation of cache path to nScriptCCreate(). 156 mCacheDir = cacheDir; 157 } 158 159 /** 160 * ContextType specifies the specific type of context to be created. 161 * 162 */ 163 public enum ContextType { 164 /** 165 * NORMAL context, this is the default and what shipping apps should 166 * use. 167 */ 168 NORMAL (0), 169 170 /** 171 * DEBUG context, perform extra runtime checks to validate the 172 * kernels and APIs are being used as intended. Get and SetElementAt 173 * will be bounds checked in this mode. 174 */ 175 DEBUG (1), 176 177 /** 178 * PROFILE context, Intended to be used once the first time an 179 * application is run on a new device. This mode allows the runtime to 180 * do additional testing and performance tuning. 181 */ 182 PROFILE (2); 183 184 int mID; 185 ContextType(int id) { 186 mID = id; 187 } 188 } 189 190 ContextType mContextType; 191 ReentrantReadWriteLock mRWLock; 192 193 // Methods below are wrapped to protect the non-threadsafe 194 // lockless fifo. 195 native long rsnContextCreateGL(long dev, int ver, int sdkVer, 196 int colorMin, int colorPref, 197 int alphaMin, int alphaPref, 198 int depthMin, int depthPref, 199 int stencilMin, int stencilPref, 200 int samplesMin, int samplesPref, float samplesQ, int dpi); 201 synchronized long nContextCreateGL(long dev, int ver, int sdkVer, 202 int colorMin, int colorPref, 203 int alphaMin, int alphaPref, 204 int depthMin, int depthPref, 205 int stencilMin, int stencilPref, 206 int samplesMin, int samplesPref, float samplesQ, int dpi) { 207 return rsnContextCreateGL(dev, ver, sdkVer, colorMin, colorPref, 208 alphaMin, alphaPref, depthMin, depthPref, 209 stencilMin, stencilPref, 210 samplesMin, samplesPref, samplesQ, dpi); 211 } 212 native long rsnContextCreate(long dev, int ver, int sdkVer, int contextType); 213 synchronized long nContextCreate(long dev, int ver, int sdkVer, int contextType) { 214 return rsnContextCreate(dev, ver, sdkVer, contextType); 215 } 216 native void rsnContextDestroy(long con); 217 synchronized void nContextDestroy() { 218 validate(); 219 220 // take teardown lock 221 // teardown lock can only be taken when no objects are being destroyed 222 ReentrantReadWriteLock.WriteLock wlock = mRWLock.writeLock(); 223 wlock.lock(); 224 225 long curCon = mContext; 226 // context is considered dead as of this point 227 mContext = 0; 228 229 wlock.unlock(); 230 rsnContextDestroy(curCon); 231 } 232 native void rsnContextSetSurface(long con, int w, int h, Surface sur); 233 synchronized void nContextSetSurface(int w, int h, Surface sur) { 234 validate(); 235 rsnContextSetSurface(mContext, w, h, sur); 236 } 237 native void rsnContextSetSurfaceTexture(long con, int w, int h, SurfaceTexture sur); 238 synchronized void nContextSetSurfaceTexture(int w, int h, SurfaceTexture sur) { 239 validate(); 240 rsnContextSetSurfaceTexture(mContext, w, h, sur); 241 } 242 native void rsnContextSetPriority(long con, int p); 243 synchronized void nContextSetPriority(int p) { 244 validate(); 245 rsnContextSetPriority(mContext, p); 246 } 247 native void rsnContextDump(long con, int bits); 248 synchronized void nContextDump(int bits) { 249 validate(); 250 rsnContextDump(mContext, bits); 251 } 252 native void rsnContextFinish(long con); 253 synchronized void nContextFinish() { 254 validate(); 255 rsnContextFinish(mContext); 256 } 257 258 native void rsnContextSendMessage(long con, int id, int[] data); 259 synchronized void nContextSendMessage(int id, int[] data) { 260 validate(); 261 rsnContextSendMessage(mContext, id, data); 262 } 263 264 native void rsnContextBindRootScript(long con, long script); 265 synchronized void nContextBindRootScript(long script) { 266 validate(); 267 rsnContextBindRootScript(mContext, script); 268 } 269 native void rsnContextBindSampler(long con, int sampler, int slot); 270 synchronized void nContextBindSampler(int sampler, int slot) { 271 validate(); 272 rsnContextBindSampler(mContext, sampler, slot); 273 } 274 native void rsnContextBindProgramStore(long con, long pfs); 275 synchronized void nContextBindProgramStore(long pfs) { 276 validate(); 277 rsnContextBindProgramStore(mContext, pfs); 278 } 279 native void rsnContextBindProgramFragment(long con, long pf); 280 synchronized void nContextBindProgramFragment(long pf) { 281 validate(); 282 rsnContextBindProgramFragment(mContext, pf); 283 } 284 native void rsnContextBindProgramVertex(long con, long pv); 285 synchronized void nContextBindProgramVertex(long pv) { 286 validate(); 287 rsnContextBindProgramVertex(mContext, pv); 288 } 289 native void rsnContextBindProgramRaster(long con, long pr); 290 synchronized void nContextBindProgramRaster(long pr) { 291 validate(); 292 rsnContextBindProgramRaster(mContext, pr); 293 } 294 native void rsnContextPause(long con); 295 synchronized void nContextPause() { 296 validate(); 297 rsnContextPause(mContext); 298 } 299 native void rsnContextResume(long con); 300 synchronized void nContextResume() { 301 validate(); 302 rsnContextResume(mContext); 303 } 304 305 native long rsnClosureCreate(long con, long kernelID, long returnValue, 306 long[] fieldIDs, long[] values, int[] sizes, long[] depClosures, 307 long[] depFieldIDs); 308 synchronized long nClosureCreate(long kernelID, long returnValue, 309 long[] fieldIDs, long[] values, int[] sizes, long[] depClosures, 310 long[] depFieldIDs) { 311 validate(); 312 return rsnClosureCreate(mContext, kernelID, returnValue, fieldIDs, values, 313 sizes, depClosures, depFieldIDs); 314 } 315 316 native long rsnInvokeClosureCreate(long con, long invokeID, byte[] params, 317 long[] fieldIDs, long[] values, int[] sizes); 318 synchronized long nInvokeClosureCreate(long invokeID, byte[] params, 319 long[] fieldIDs, long[] values, int[] sizes) { 320 validate(); 321 return rsnInvokeClosureCreate(mContext, invokeID, params, fieldIDs, 322 values, sizes); 323 } 324 325 native void rsnClosureSetArg(long con, long closureID, int index, 326 long value, int size); 327 synchronized void nClosureSetArg(long closureID, int index, long value, 328 int size) { 329 validate(); 330 rsnClosureSetArg(mContext, closureID, index, value, size); 331 } 332 333 native void rsnClosureSetGlobal(long con, long closureID, long fieldID, 334 long value, int size); 335 // Does this have to be synchronized? 336 synchronized void nClosureSetGlobal(long closureID, long fieldID, 337 long value, int size) { 338 validate(); // TODO: is this necessary? 339 rsnClosureSetGlobal(mContext, closureID, fieldID, value, size); 340 } 341 342 native long rsnScriptGroup2Create(long con, String cachePath, long[] closures); 343 synchronized long nScriptGroup2Create(String cachePath, long[] closures) { 344 validate(); 345 return rsnScriptGroup2Create(mContext, cachePath, closures); 346 } 347 348 native void rsnScriptGroup2Execute(long con, long groupID); 349 synchronized void nScriptGroup2Execute(long groupID) { 350 validate(); 351 rsnScriptGroup2Execute(mContext, groupID); 352 } 353 354 native void rsnAssignName(long con, long obj, byte[] name); 355 synchronized void nAssignName(long obj, byte[] name) { 356 validate(); 357 rsnAssignName(mContext, obj, name); 358 } 359 native String rsnGetName(long con, long obj); 360 synchronized String nGetName(long obj) { 361 validate(); 362 return rsnGetName(mContext, obj); 363 } 364 365 // nObjDestroy is explicitly _not_ synchronous to prevent crashes in finalizers 366 native void rsnObjDestroy(long con, long id); 367 void nObjDestroy(long id) { 368 // There is a race condition here. The calling code may be run 369 // by the gc while teardown is occuring. This protects againts 370 // deleting dead objects. 371 if (mContext != 0) { 372 rsnObjDestroy(mContext, id); 373 } 374 } 375 376 native long rsnElementCreate(long con, long type, int kind, boolean norm, int vecSize); 377 synchronized long nElementCreate(long type, int kind, boolean norm, int vecSize) { 378 validate(); 379 return rsnElementCreate(mContext, type, kind, norm, vecSize); 380 } 381 native long rsnElementCreate2(long con, long[] elements, String[] names, int[] arraySizes); 382 synchronized long nElementCreate2(long[] elements, String[] names, int[] arraySizes) { 383 validate(); 384 return rsnElementCreate2(mContext, elements, names, arraySizes); 385 } 386 native void rsnElementGetNativeData(long con, long id, int[] elementData); 387 synchronized void nElementGetNativeData(long id, int[] elementData) { 388 validate(); 389 rsnElementGetNativeData(mContext, id, elementData); 390 } 391 native void rsnElementGetSubElements(long con, long id, 392 long[] IDs, String[] names, int[] arraySizes); 393 synchronized void nElementGetSubElements(long id, long[] IDs, String[] names, int[] arraySizes) { 394 validate(); 395 rsnElementGetSubElements(mContext, id, IDs, names, arraySizes); 396 } 397 398 native long rsnTypeCreate(long con, long eid, int x, int y, int z, boolean mips, boolean faces, int yuv); 399 synchronized long nTypeCreate(long eid, int x, int y, int z, boolean mips, boolean faces, int yuv) { 400 validate(); 401 return rsnTypeCreate(mContext, eid, x, y, z, mips, faces, yuv); 402 } 403 native void rsnTypeGetNativeData(long con, long id, long[] typeData); 404 synchronized void nTypeGetNativeData(long id, long[] typeData) { 405 validate(); 406 rsnTypeGetNativeData(mContext, id, typeData); 407 } 408 409 native long rsnAllocationCreateTyped(long con, long type, int mip, int usage, long pointer); 410 synchronized long nAllocationCreateTyped(long type, int mip, int usage, long pointer) { 411 validate(); 412 return rsnAllocationCreateTyped(mContext, type, mip, usage, pointer); 413 } 414 native long rsnAllocationCreateFromBitmap(long con, long type, int mip, Bitmap bmp, int usage); 415 synchronized long nAllocationCreateFromBitmap(long type, int mip, Bitmap bmp, int usage) { 416 validate(); 417 return rsnAllocationCreateFromBitmap(mContext, type, mip, bmp, usage); 418 } 419 420 native long rsnAllocationCreateBitmapBackedAllocation(long con, long type, int mip, Bitmap bmp, int usage); 421 synchronized long nAllocationCreateBitmapBackedAllocation(long type, int mip, Bitmap bmp, int usage) { 422 validate(); 423 return rsnAllocationCreateBitmapBackedAllocation(mContext, type, mip, bmp, usage); 424 } 425 426 native long rsnAllocationCubeCreateFromBitmap(long con, long type, int mip, Bitmap bmp, int usage); 427 synchronized long nAllocationCubeCreateFromBitmap(long type, int mip, Bitmap bmp, int usage) { 428 validate(); 429 return rsnAllocationCubeCreateFromBitmap(mContext, type, mip, bmp, usage); 430 } 431 native long rsnAllocationCreateBitmapRef(long con, long type, Bitmap bmp); 432 synchronized long nAllocationCreateBitmapRef(long type, Bitmap bmp) { 433 validate(); 434 return rsnAllocationCreateBitmapRef(mContext, type, bmp); 435 } 436 native long rsnAllocationCreateFromAssetStream(long con, int mips, int assetStream, int usage); 437 synchronized long nAllocationCreateFromAssetStream(int mips, int assetStream, int usage) { 438 validate(); 439 return rsnAllocationCreateFromAssetStream(mContext, mips, assetStream, usage); 440 } 441 442 native void rsnAllocationCopyToBitmap(long con, long alloc, Bitmap bmp); 443 synchronized void nAllocationCopyToBitmap(long alloc, Bitmap bmp) { 444 validate(); 445 rsnAllocationCopyToBitmap(mContext, alloc, bmp); 446 } 447 448 449 native void rsnAllocationSyncAll(long con, long alloc, int src); 450 synchronized void nAllocationSyncAll(long alloc, int src) { 451 validate(); 452 rsnAllocationSyncAll(mContext, alloc, src); 453 } 454 native Surface rsnAllocationGetSurface(long con, long alloc); 455 synchronized Surface nAllocationGetSurface(long alloc) { 456 validate(); 457 return rsnAllocationGetSurface(mContext, alloc); 458 } 459 native void rsnAllocationSetSurface(long con, long alloc, Surface sur); 460 synchronized void nAllocationSetSurface(long alloc, Surface sur) { 461 validate(); 462 rsnAllocationSetSurface(mContext, alloc, sur); 463 } 464 native void rsnAllocationIoSend(long con, long alloc); 465 synchronized void nAllocationIoSend(long alloc) { 466 validate(); 467 rsnAllocationIoSend(mContext, alloc); 468 } 469 native void rsnAllocationIoReceive(long con, long alloc); 470 synchronized void nAllocationIoReceive(long alloc) { 471 validate(); 472 rsnAllocationIoReceive(mContext, alloc); 473 } 474 475 476 native void rsnAllocationGenerateMipmaps(long con, long alloc); 477 synchronized void nAllocationGenerateMipmaps(long alloc) { 478 validate(); 479 rsnAllocationGenerateMipmaps(mContext, alloc); 480 } 481 native void rsnAllocationCopyFromBitmap(long con, long alloc, Bitmap bmp); 482 synchronized void nAllocationCopyFromBitmap(long alloc, Bitmap bmp) { 483 validate(); 484 rsnAllocationCopyFromBitmap(mContext, alloc, bmp); 485 } 486 487 488 native void rsnAllocationData1D(long con, long id, int off, int mip, int count, Object d, int sizeBytes, int dt); 489 synchronized void nAllocationData1D(long id, int off, int mip, int count, Object d, int sizeBytes, Element.DataType dt) { 490 validate(); 491 rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes, dt.mID); 492 } 493 494 native void rsnAllocationElementData1D(long con,long id, int xoff, int mip, int compIdx, byte[] d, int sizeBytes); 495 synchronized void nAllocationElementData1D(long id, int xoff, int mip, int compIdx, byte[] d, int sizeBytes) { 496 validate(); 497 rsnAllocationElementData1D(mContext, id, xoff, mip, compIdx, d, sizeBytes); 498 } 499 500 native void rsnAllocationData2D(long con, 501 long dstAlloc, int dstXoff, int dstYoff, 502 int dstMip, int dstFace, 503 int width, int height, 504 long srcAlloc, int srcXoff, int srcYoff, 505 int srcMip, int srcFace); 506 synchronized void nAllocationData2D(long dstAlloc, int dstXoff, int dstYoff, 507 int dstMip, int dstFace, 508 int width, int height, 509 long srcAlloc, int srcXoff, int srcYoff, 510 int srcMip, int srcFace) { 511 validate(); 512 rsnAllocationData2D(mContext, 513 dstAlloc, dstXoff, dstYoff, 514 dstMip, dstFace, 515 width, height, 516 srcAlloc, srcXoff, srcYoff, 517 srcMip, srcFace); 518 } 519 520 native void rsnAllocationData2D(long con, long id, int xoff, int yoff, int mip, int face, 521 int w, int h, Object d, int sizeBytes, int dt); 522 synchronized void nAllocationData2D(long id, int xoff, int yoff, int mip, int face, 523 int w, int h, Object d, int sizeBytes, Element.DataType dt) { 524 validate(); 525 rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes, dt.mID); 526 } 527 528 native void rsnAllocationData2D(long con, long id, int xoff, int yoff, int mip, int face, Bitmap b); 529 synchronized void nAllocationData2D(long id, int xoff, int yoff, int mip, int face, Bitmap b) { 530 validate(); 531 rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, b); 532 } 533 534 native void rsnAllocationData3D(long con, 535 long dstAlloc, int dstXoff, int dstYoff, int dstZoff, 536 int dstMip, 537 int width, int height, int depth, 538 long srcAlloc, int srcXoff, int srcYoff, int srcZoff, 539 int srcMip); 540 synchronized void nAllocationData3D(long dstAlloc, int dstXoff, int dstYoff, int dstZoff, 541 int dstMip, 542 int width, int height, int depth, 543 long srcAlloc, int srcXoff, int srcYoff, int srcZoff, 544 int srcMip) { 545 validate(); 546 rsnAllocationData3D(mContext, 547 dstAlloc, dstXoff, dstYoff, dstZoff, 548 dstMip, width, height, depth, 549 srcAlloc, srcXoff, srcYoff, srcZoff, srcMip); 550 } 551 552 native void rsnAllocationData3D(long con, long id, int xoff, int yoff, int zoff, int mip, 553 int w, int h, int depth, Object d, int sizeBytes, int dt); 554 synchronized void nAllocationData3D(long id, int xoff, int yoff, int zoff, int mip, 555 int w, int h, int depth, Object d, int sizeBytes, Element.DataType dt) { 556 validate(); 557 rsnAllocationData3D(mContext, id, xoff, yoff, zoff, mip, w, h, depth, d, sizeBytes, dt.mID); 558 } 559 560 native void rsnAllocationRead(long con, long id, Object d, int dt); 561 synchronized void nAllocationRead(long id, Object d, Element.DataType dt) { 562 validate(); 563 rsnAllocationRead(mContext, id, d, dt.mID); 564 } 565 566 native void rsnAllocationRead1D(long con, long id, int off, int mip, int count, Object d, 567 int sizeBytes, int dt); 568 synchronized void nAllocationRead1D(long id, int off, int mip, int count, Object d, 569 int sizeBytes, Element.DataType dt) { 570 validate(); 571 rsnAllocationRead1D(mContext, id, off, mip, count, d, sizeBytes, dt.mID); 572 } 573 574 native void rsnAllocationRead2D(long con, long id, int xoff, int yoff, int mip, int face, 575 int w, int h, Object d, int sizeBytes, int dt); 576 synchronized void nAllocationRead2D(long id, int xoff, int yoff, int mip, int face, 577 int w, int h, Object d, int sizeBytes, Element.DataType dt) { 578 validate(); 579 rsnAllocationRead2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes, dt.mID); 580 } 581 582 native long rsnAllocationGetType(long con, long id); 583 synchronized long nAllocationGetType(long id) { 584 validate(); 585 return rsnAllocationGetType(mContext, id); 586 } 587 588 native void rsnAllocationResize1D(long con, long id, int dimX); 589 synchronized void nAllocationResize1D(long id, int dimX) { 590 validate(); 591 rsnAllocationResize1D(mContext, id, dimX); 592 } 593 594 native long rsnAllocationAdapterCreate(long con, long allocId, long typeId); 595 synchronized long nAllocationAdapterCreate(long allocId, long typeId) { 596 validate(); 597 return rsnAllocationAdapterCreate(mContext, allocId, typeId); 598 } 599 600 native void rsnAllocationAdapterOffset(long con, long id, int x, int y, int z, 601 int mip, int face, int a1, int a2, int a3, int a4); 602 synchronized void nAllocationAdapterOffset(long id, int x, int y, int z, 603 int mip, int face, int a1, int a2, int a3, int a4) { 604 validate(); 605 rsnAllocationAdapterOffset(mContext, id, x, y, z, mip, face, a1, a2, a3, a4); 606 } 607 608 native long rsnFileA3DCreateFromAssetStream(long con, long assetStream); 609 synchronized long nFileA3DCreateFromAssetStream(long assetStream) { 610 validate(); 611 return rsnFileA3DCreateFromAssetStream(mContext, assetStream); 612 } 613 native long rsnFileA3DCreateFromFile(long con, String path); 614 synchronized long nFileA3DCreateFromFile(String path) { 615 validate(); 616 return rsnFileA3DCreateFromFile(mContext, path); 617 } 618 native long rsnFileA3DCreateFromAsset(long con, AssetManager mgr, String path); 619 synchronized long nFileA3DCreateFromAsset(AssetManager mgr, String path) { 620 validate(); 621 return rsnFileA3DCreateFromAsset(mContext, mgr, path); 622 } 623 native int rsnFileA3DGetNumIndexEntries(long con, long fileA3D); 624 synchronized int nFileA3DGetNumIndexEntries(long fileA3D) { 625 validate(); 626 return rsnFileA3DGetNumIndexEntries(mContext, fileA3D); 627 } 628 native void rsnFileA3DGetIndexEntries(long con, long fileA3D, int numEntries, int[] IDs, String[] names); 629 synchronized void nFileA3DGetIndexEntries(long fileA3D, int numEntries, int[] IDs, String[] names) { 630 validate(); 631 rsnFileA3DGetIndexEntries(mContext, fileA3D, numEntries, IDs, names); 632 } 633 native long rsnFileA3DGetEntryByIndex(long con, long fileA3D, int index); 634 synchronized long nFileA3DGetEntryByIndex(long fileA3D, int index) { 635 validate(); 636 return rsnFileA3DGetEntryByIndex(mContext, fileA3D, index); 637 } 638 639 native long rsnFontCreateFromFile(long con, String fileName, float size, int dpi); 640 synchronized long nFontCreateFromFile(String fileName, float size, int dpi) { 641 validate(); 642 return rsnFontCreateFromFile(mContext, fileName, size, dpi); 643 } 644 native long rsnFontCreateFromAssetStream(long con, String name, float size, int dpi, long assetStream); 645 synchronized long nFontCreateFromAssetStream(String name, float size, int dpi, long assetStream) { 646 validate(); 647 return rsnFontCreateFromAssetStream(mContext, name, size, dpi, assetStream); 648 } 649 native long rsnFontCreateFromAsset(long con, AssetManager mgr, String path, float size, int dpi); 650 synchronized long nFontCreateFromAsset(AssetManager mgr, String path, float size, int dpi) { 651 validate(); 652 return rsnFontCreateFromAsset(mContext, mgr, path, size, dpi); 653 } 654 655 656 native void rsnScriptBindAllocation(long con, long script, long alloc, int slot); 657 synchronized void nScriptBindAllocation(long script, long alloc, int slot) { 658 validate(); 659 rsnScriptBindAllocation(mContext, script, alloc, slot); 660 } 661 native void rsnScriptSetTimeZone(long con, long script, byte[] timeZone); 662 synchronized void nScriptSetTimeZone(long script, byte[] timeZone) { 663 validate(); 664 rsnScriptSetTimeZone(mContext, script, timeZone); 665 } 666 native void rsnScriptInvoke(long con, long id, int slot); 667 synchronized void nScriptInvoke(long id, int slot) { 668 validate(); 669 rsnScriptInvoke(mContext, id, slot); 670 } 671 672 native void rsnScriptForEach(long con, long id, int slot, long[] ains, 673 long aout, byte[] params, int[] limits); 674 675 synchronized void nScriptForEach(long id, int slot, long[] ains, long aout, 676 byte[] params, int[] limits) { 677 validate(); 678 rsnScriptForEach(mContext, id, slot, ains, aout, params, limits); 679 } 680 681 native void rsnScriptInvokeV(long con, long id, int slot, byte[] params); 682 synchronized void nScriptInvokeV(long id, int slot, byte[] params) { 683 validate(); 684 rsnScriptInvokeV(mContext, id, slot, params); 685 } 686 687 native void rsnScriptSetVarI(long con, long id, int slot, int val); 688 synchronized void nScriptSetVarI(long id, int slot, int val) { 689 validate(); 690 rsnScriptSetVarI(mContext, id, slot, val); 691 } 692 native int rsnScriptGetVarI(long con, long id, int slot); 693 synchronized int nScriptGetVarI(long id, int slot) { 694 validate(); 695 return rsnScriptGetVarI(mContext, id, slot); 696 } 697 698 native void rsnScriptSetVarJ(long con, long id, int slot, long val); 699 synchronized void nScriptSetVarJ(long id, int slot, long val) { 700 validate(); 701 rsnScriptSetVarJ(mContext, id, slot, val); 702 } 703 native long rsnScriptGetVarJ(long con, long id, int slot); 704 synchronized long nScriptGetVarJ(long id, int slot) { 705 validate(); 706 return rsnScriptGetVarJ(mContext, id, slot); 707 } 708 709 native void rsnScriptSetVarF(long con, long id, int slot, float val); 710 synchronized void nScriptSetVarF(long id, int slot, float val) { 711 validate(); 712 rsnScriptSetVarF(mContext, id, slot, val); 713 } 714 native float rsnScriptGetVarF(long con, long id, int slot); 715 synchronized float nScriptGetVarF(long id, int slot) { 716 validate(); 717 return rsnScriptGetVarF(mContext, id, slot); 718 } 719 native void rsnScriptSetVarD(long con, long id, int slot, double val); 720 synchronized void nScriptSetVarD(long id, int slot, double val) { 721 validate(); 722 rsnScriptSetVarD(mContext, id, slot, val); 723 } 724 native double rsnScriptGetVarD(long con, long id, int slot); 725 synchronized double nScriptGetVarD(long id, int slot) { 726 validate(); 727 return rsnScriptGetVarD(mContext, id, slot); 728 } 729 native void rsnScriptSetVarV(long con, long id, int slot, byte[] val); 730 synchronized void nScriptSetVarV(long id, int slot, byte[] val) { 731 validate(); 732 rsnScriptSetVarV(mContext, id, slot, val); 733 } 734 native void rsnScriptGetVarV(long con, long id, int slot, byte[] val); 735 synchronized void nScriptGetVarV(long id, int slot, byte[] val) { 736 validate(); 737 rsnScriptGetVarV(mContext, id, slot, val); 738 } 739 native void rsnScriptSetVarVE(long con, long id, int slot, byte[] val, 740 long e, int[] dims); 741 synchronized void nScriptSetVarVE(long id, int slot, byte[] val, 742 long e, int[] dims) { 743 validate(); 744 rsnScriptSetVarVE(mContext, id, slot, val, e, dims); 745 } 746 native void rsnScriptSetVarObj(long con, long id, int slot, long val); 747 synchronized void nScriptSetVarObj(long id, int slot, long val) { 748 validate(); 749 rsnScriptSetVarObj(mContext, id, slot, val); 750 } 751 752 native long rsnScriptCCreate(long con, String resName, String cacheDir, 753 byte[] script, int length); 754 synchronized long nScriptCCreate(String resName, String cacheDir, byte[] script, int length) { 755 validate(); 756 return rsnScriptCCreate(mContext, resName, cacheDir, script, length); 757 } 758 759 native long rsnScriptIntrinsicCreate(long con, int id, long eid); 760 synchronized long nScriptIntrinsicCreate(int id, long eid) { 761 validate(); 762 return rsnScriptIntrinsicCreate(mContext, id, eid); 763 } 764 765 native long rsnScriptKernelIDCreate(long con, long sid, int slot, int sig); 766 synchronized long nScriptKernelIDCreate(long sid, int slot, int sig) { 767 validate(); 768 return rsnScriptKernelIDCreate(mContext, sid, slot, sig); 769 } 770 771 native long rsnScriptInvokeIDCreate(long con, long sid, int slot); 772 synchronized long nScriptInvokeIDCreate(long sid, int slot) { 773 validate(); 774 return rsnScriptInvokeIDCreate(mContext, sid, slot); 775 } 776 777 native long rsnScriptFieldIDCreate(long con, long sid, int slot); 778 synchronized long nScriptFieldIDCreate(long sid, int slot) { 779 validate(); 780 return rsnScriptFieldIDCreate(mContext, sid, slot); 781 } 782 783 native long rsnScriptGroupCreate(long con, long[] kernels, long[] src, long[] dstk, long[] dstf, long[] types); 784 synchronized long nScriptGroupCreate(long[] kernels, long[] src, long[] dstk, long[] dstf, long[] types) { 785 validate(); 786 return rsnScriptGroupCreate(mContext, kernels, src, dstk, dstf, types); 787 } 788 789 native void rsnScriptGroupSetInput(long con, long group, long kernel, long alloc); 790 synchronized void nScriptGroupSetInput(long group, long kernel, long alloc) { 791 validate(); 792 rsnScriptGroupSetInput(mContext, group, kernel, alloc); 793 } 794 795 native void rsnScriptGroupSetOutput(long con, long group, long kernel, long alloc); 796 synchronized void nScriptGroupSetOutput(long group, long kernel, long alloc) { 797 validate(); 798 rsnScriptGroupSetOutput(mContext, group, kernel, alloc); 799 } 800 801 native void rsnScriptGroupExecute(long con, long group); 802 synchronized void nScriptGroupExecute(long group) { 803 validate(); 804 rsnScriptGroupExecute(mContext, group); 805 } 806 807 native long rsnSamplerCreate(long con, int magFilter, int minFilter, 808 int wrapS, int wrapT, int wrapR, float aniso); 809 synchronized long nSamplerCreate(int magFilter, int minFilter, 810 int wrapS, int wrapT, int wrapR, float aniso) { 811 validate(); 812 return rsnSamplerCreate(mContext, magFilter, minFilter, wrapS, wrapT, wrapR, aniso); 813 } 814 815 native long rsnProgramStoreCreate(long con, boolean r, boolean g, boolean b, boolean a, 816 boolean depthMask, boolean dither, 817 int srcMode, int dstMode, int depthFunc); 818 synchronized long nProgramStoreCreate(boolean r, boolean g, boolean b, boolean a, 819 boolean depthMask, boolean dither, 820 int srcMode, int dstMode, int depthFunc) { 821 validate(); 822 return rsnProgramStoreCreate(mContext, r, g, b, a, depthMask, dither, srcMode, 823 dstMode, depthFunc); 824 } 825 826 native long rsnProgramRasterCreate(long con, boolean pointSprite, int cullMode); 827 synchronized long nProgramRasterCreate(boolean pointSprite, int cullMode) { 828 validate(); 829 return rsnProgramRasterCreate(mContext, pointSprite, cullMode); 830 } 831 832 native void rsnProgramBindConstants(long con, long pv, int slot, long mID); 833 synchronized void nProgramBindConstants(long pv, int slot, long mID) { 834 validate(); 835 rsnProgramBindConstants(mContext, pv, slot, mID); 836 } 837 native void rsnProgramBindTexture(long con, long vpf, int slot, long a); 838 synchronized void nProgramBindTexture(long vpf, int slot, long a) { 839 validate(); 840 rsnProgramBindTexture(mContext, vpf, slot, a); 841 } 842 native void rsnProgramBindSampler(long con, long vpf, int slot, long s); 843 synchronized void nProgramBindSampler(long vpf, int slot, long s) { 844 validate(); 845 rsnProgramBindSampler(mContext, vpf, slot, s); 846 } 847 native long rsnProgramFragmentCreate(long con, String shader, String[] texNames, long[] params); 848 synchronized long nProgramFragmentCreate(String shader, String[] texNames, long[] params) { 849 validate(); 850 return rsnProgramFragmentCreate(mContext, shader, texNames, params); 851 } 852 native long rsnProgramVertexCreate(long con, String shader, String[] texNames, long[] params); 853 synchronized long nProgramVertexCreate(String shader, String[] texNames, long[] params) { 854 validate(); 855 return rsnProgramVertexCreate(mContext, shader, texNames, params); 856 } 857 858 native long rsnMeshCreate(long con, long[] vtx, long[] idx, int[] prim); 859 synchronized long nMeshCreate(long[] vtx, long[] idx, int[] prim) { 860 validate(); 861 return rsnMeshCreate(mContext, vtx, idx, prim); 862 } 863 native int rsnMeshGetVertexBufferCount(long con, long id); 864 synchronized int nMeshGetVertexBufferCount(long id) { 865 validate(); 866 return rsnMeshGetVertexBufferCount(mContext, id); 867 } 868 native int rsnMeshGetIndexCount(long con, long id); 869 synchronized int nMeshGetIndexCount(long id) { 870 validate(); 871 return rsnMeshGetIndexCount(mContext, id); 872 } 873 native void rsnMeshGetVertices(long con, long id, long[] vtxIds, int vtxIdCount); 874 synchronized void nMeshGetVertices(long id, long[] vtxIds, int vtxIdCount) { 875 validate(); 876 rsnMeshGetVertices(mContext, id, vtxIds, vtxIdCount); 877 } 878 native void rsnMeshGetIndices(long con, long id, long[] idxIds, int[] primitives, int vtxIdCount); 879 synchronized void nMeshGetIndices(long id, long[] idxIds, int[] primitives, int vtxIdCount) { 880 validate(); 881 rsnMeshGetIndices(mContext, id, idxIds, primitives, vtxIdCount); 882 } 883 884 native long rsnPathCreate(long con, int prim, boolean isStatic, long vtx, long loop, float q); 885 synchronized long nPathCreate(int prim, boolean isStatic, long vtx, long loop, float q) { 886 validate(); 887 return rsnPathCreate(mContext, prim, isStatic, vtx, loop, q); 888 } 889 890 native void rsnScriptIntrinsicBLAS_Single(long con, long id, int func, int TransA, 891 int TransB, int Side, int Uplo, int Diag, int M, int N, int K, 892 float alpha, long A, long B, float beta, long C, int incX, int incY, 893 int KL, int KU); 894 synchronized void nScriptIntrinsicBLAS_Single(long id, int func, int TransA, 895 int TransB, int Side, int Uplo, int Diag, int M, int N, int K, 896 float alpha, long A, long B, float beta, long C, int incX, int incY, 897 int KL, int KU) { 898 validate(); 899 rsnScriptIntrinsicBLAS_Single(mContext, id, func, TransA, TransB, Side, Uplo, Diag, M, N, K, alpha, A, B, beta, C, incX, incY, KL, KU); 900 } 901 902 native void rsnScriptIntrinsicBLAS_Double(long con, long id, int func, int TransA, 903 int TransB, int Side, int Uplo, int Diag, int M, int N, int K, 904 double alpha, long A, long B, double beta, long C, int incX, int incY, 905 int KL, int KU); 906 synchronized void nScriptIntrinsicBLAS_Double(long id, int func, int TransA, 907 int TransB, int Side, int Uplo, int Diag, int M, int N, int K, 908 double alpha, long A, long B, double beta, long C, int incX, int incY, 909 int KL, int KU) { 910 validate(); 911 rsnScriptIntrinsicBLAS_Double(mContext, id, func, TransA, TransB, Side, Uplo, Diag, M, N, K, alpha, A, B, beta, C, incX, incY, KL, KU); 912 } 913 914 native void rsnScriptIntrinsicBLAS_Complex(long con, long id, int func, int TransA, 915 int TransB, int Side, int Uplo, int Diag, int M, int N, int K, 916 float alphaX, float alphaY, long A, long B, float betaX, float betaY, long C, int incX, int incY, 917 int KL, int KU); 918 synchronized void nScriptIntrinsicBLAS_Complex(long id, int func, int TransA, 919 int TransB, int Side, int Uplo, int Diag, int M, int N, int K, 920 float alphaX, float alphaY, long A, long B, float betaX, float betaY, long C, int incX, int incY, 921 int KL, int KU) { 922 validate(); 923 rsnScriptIntrinsicBLAS_Complex(mContext, id, func, TransA, TransB, Side, Uplo, Diag, M, N, K, alphaX, alphaY, A, B, betaX, betaY, C, incX, incY, KL, KU); 924 } 925 926 native void rsnScriptIntrinsicBLAS_Z(long con, long id, int func, int TransA, 927 int TransB, int Side, int Uplo, int Diag, int M, int N, int K, 928 double alphaX, double alphaY, long A, long B, double betaX, double betaY, long C, int incX, int incY, 929 int KL, int KU); 930 synchronized void nScriptIntrinsicBLAS_Z(long id, int func, int TransA, 931 int TransB, int Side, int Uplo, int Diag, int M, int N, int K, 932 double alphaX, double alphaY, long A, long B, double betaX, double betaY, long C, int incX, int incY, 933 int KL, int KU) { 934 validate(); 935 rsnScriptIntrinsicBLAS_Z(mContext, id, func, TransA, TransB, Side, Uplo, Diag, M, N, K, alphaX, alphaY, A, B, betaX, betaY, C, incX, incY, KL, KU); 936 } 937 938 939 long mDev; 940 long mContext; 941 @SuppressWarnings({"FieldCanBeLocal"}) 942 MessageThread mMessageThread; 943 944 Element mElement_U8; 945 Element mElement_I8; 946 Element mElement_U16; 947 Element mElement_I16; 948 Element mElement_U32; 949 Element mElement_I32; 950 Element mElement_U64; 951 Element mElement_I64; 952 Element mElement_F16; 953 Element mElement_F32; 954 Element mElement_F64; 955 Element mElement_BOOLEAN; 956 957 Element mElement_ELEMENT; 958 Element mElement_TYPE; 959 Element mElement_ALLOCATION; 960 Element mElement_SAMPLER; 961 Element mElement_SCRIPT; 962 Element mElement_MESH; 963 Element mElement_PROGRAM_FRAGMENT; 964 Element mElement_PROGRAM_VERTEX; 965 Element mElement_PROGRAM_RASTER; 966 Element mElement_PROGRAM_STORE; 967 Element mElement_FONT; 968 969 Element mElement_A_8; 970 Element mElement_RGB_565; 971 Element mElement_RGB_888; 972 Element mElement_RGBA_5551; 973 Element mElement_RGBA_4444; 974 Element mElement_RGBA_8888; 975 976 Element mElement_HALF_2; 977 Element mElement_HALF_3; 978 Element mElement_HALF_4; 979 980 Element mElement_FLOAT_2; 981 Element mElement_FLOAT_3; 982 Element mElement_FLOAT_4; 983 984 Element mElement_DOUBLE_2; 985 Element mElement_DOUBLE_3; 986 Element mElement_DOUBLE_4; 987 988 Element mElement_UCHAR_2; 989 Element mElement_UCHAR_3; 990 Element mElement_UCHAR_4; 991 992 Element mElement_CHAR_2; 993 Element mElement_CHAR_3; 994 Element mElement_CHAR_4; 995 996 Element mElement_USHORT_2; 997 Element mElement_USHORT_3; 998 Element mElement_USHORT_4; 999 1000 Element mElement_SHORT_2; 1001 Element mElement_SHORT_3; 1002 Element mElement_SHORT_4; 1003 1004 Element mElement_UINT_2; 1005 Element mElement_UINT_3; 1006 Element mElement_UINT_4; 1007 1008 Element mElement_INT_2; 1009 Element mElement_INT_3; 1010 Element mElement_INT_4; 1011 1012 Element mElement_ULONG_2; 1013 Element mElement_ULONG_3; 1014 Element mElement_ULONG_4; 1015 1016 Element mElement_LONG_2; 1017 Element mElement_LONG_3; 1018 Element mElement_LONG_4; 1019 1020 Element mElement_YUV; 1021 1022 Element mElement_MATRIX_4X4; 1023 Element mElement_MATRIX_3X3; 1024 Element mElement_MATRIX_2X2; 1025 1026 Sampler mSampler_CLAMP_NEAREST; 1027 Sampler mSampler_CLAMP_LINEAR; 1028 Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR; 1029 Sampler mSampler_WRAP_NEAREST; 1030 Sampler mSampler_WRAP_LINEAR; 1031 Sampler mSampler_WRAP_LINEAR_MIP_LINEAR; 1032 Sampler mSampler_MIRRORED_REPEAT_NEAREST; 1033 Sampler mSampler_MIRRORED_REPEAT_LINEAR; 1034 Sampler mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR; 1035 1036 ProgramStore mProgramStore_BLEND_NONE_DEPTH_TEST; 1037 ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH; 1038 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_TEST; 1039 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH; 1040 1041 ProgramRaster mProgramRaster_CULL_BACK; 1042 ProgramRaster mProgramRaster_CULL_FRONT; 1043 ProgramRaster mProgramRaster_CULL_NONE; 1044 1045 /////////////////////////////////////////////////////////////////////////////////// 1046 // 1047 1048 /** 1049 * The base class from which an application should derive in order 1050 * to receive RS messages from scripts. When a script calls {@code 1051 * rsSendToClient}, the data fields will be filled, and the run 1052 * method will be called on a separate thread. This will occur 1053 * some time after {@code rsSendToClient} completes in the script, 1054 * as {@code rsSendToClient} is asynchronous. Message handlers are 1055 * not guaranteed to have completed when {@link 1056 * android.renderscript.RenderScript#finish} returns. 1057 * 1058 */ 1059 public static class RSMessageHandler implements Runnable { 1060 protected int[] mData; 1061 protected int mID; 1062 protected int mLength; 1063 public void run() { 1064 } 1065 } 1066 /** 1067 * If an application is expecting messages, it should set this 1068 * field to an instance of {@link RSMessageHandler}. This 1069 * instance will receive all the user messages sent from {@code 1070 * sendToClient} by scripts from this context. 1071 * 1072 */ 1073 RSMessageHandler mMessageCallback = null; 1074 1075 public void setMessageHandler(RSMessageHandler msg) { 1076 mMessageCallback = msg; 1077 } 1078 public RSMessageHandler getMessageHandler() { 1079 return mMessageCallback; 1080 } 1081 1082 /** 1083 * Place a message into the message queue to be sent back to the message 1084 * handler once all previous commands have been executed. 1085 * 1086 * @param id 1087 * @param data 1088 */ 1089 public void sendMessage(int id, int[] data) { 1090 nContextSendMessage(id, data); 1091 } 1092 1093 /** 1094 * The runtime error handler base class. An application should derive from this class 1095 * if it wishes to install an error handler. When errors occur at runtime, 1096 * the fields in this class will be filled, and the run method will be called. 1097 * 1098 */ 1099 public static class RSErrorHandler implements Runnable { 1100 protected String mErrorMessage; 1101 protected int mErrorNum; 1102 public void run() { 1103 } 1104 } 1105 1106 /** 1107 * Application Error handler. All runtime errors will be dispatched to the 1108 * instance of RSAsyncError set here. If this field is null a 1109 * {@link RSRuntimeException} will instead be thrown with details about the error. 1110 * This will cause program termaination. 1111 * 1112 */ 1113 RSErrorHandler mErrorCallback = null; 1114 1115 public void setErrorHandler(RSErrorHandler msg) { 1116 mErrorCallback = msg; 1117 } 1118 public RSErrorHandler getErrorHandler() { 1119 return mErrorCallback; 1120 } 1121 1122 /** 1123 * RenderScript worker thread priority enumeration. The default value is 1124 * NORMAL. Applications wishing to do background processing should set 1125 * their priority to LOW to avoid starving forground processes. 1126 */ 1127 public enum Priority { 1128 // These values used to represent official thread priority values 1129 // now they are simply enums to be used by the runtime side 1130 LOW (15), 1131 NORMAL (-8); 1132 1133 int mID; 1134 Priority(int id) { 1135 mID = id; 1136 } 1137 } 1138 1139 void validateObject(BaseObj o) { 1140 if (o != null) { 1141 if (o.mRS != this) { 1142 throw new RSIllegalArgumentException("Attempting to use an object across contexts."); 1143 } 1144 } 1145 } 1146 1147 void validate() { 1148 if (mContext == 0) { 1149 throw new RSInvalidStateException("Calling RS with no Context active."); 1150 } 1151 } 1152 1153 1154 /** 1155 * Change the priority of the worker threads for this context. 1156 * 1157 * @param p New priority to be set. 1158 */ 1159 public void setPriority(Priority p) { 1160 validate(); 1161 nContextSetPriority(p.mID); 1162 } 1163 1164 static class MessageThread extends Thread { 1165 RenderScript mRS; 1166 boolean mRun = true; 1167 int[] mAuxData = new int[2]; 1168 1169 static final int RS_MESSAGE_TO_CLIENT_NONE = 0; 1170 static final int RS_MESSAGE_TO_CLIENT_EXCEPTION = 1; 1171 static final int RS_MESSAGE_TO_CLIENT_RESIZE = 2; 1172 static final int RS_MESSAGE_TO_CLIENT_ERROR = 3; 1173 static final int RS_MESSAGE_TO_CLIENT_USER = 4; 1174 static final int RS_MESSAGE_TO_CLIENT_NEW_BUFFER = 5; 1175 1176 static final int RS_ERROR_FATAL_DEBUG = 0x0800; 1177 static final int RS_ERROR_FATAL_UNKNOWN = 0x1000; 1178 1179 MessageThread(RenderScript rs) { 1180 super("RSMessageThread"); 1181 mRS = rs; 1182 1183 } 1184 1185 public void run() { 1186 // This function is a temporary solution. The final solution will 1187 // used typed allocations where the message id is the type indicator. 1188 int[] rbuf = new int[16]; 1189 mRS.nContextInitToClient(mRS.mContext); 1190 while(mRun) { 1191 rbuf[0] = 0; 1192 int msg = mRS.nContextPeekMessage(mRS.mContext, mAuxData); 1193 int size = mAuxData[1]; 1194 int subID = mAuxData[0]; 1195 1196 if (msg == RS_MESSAGE_TO_CLIENT_USER) { 1197 if ((size>>2) >= rbuf.length) { 1198 rbuf = new int[(size + 3) >> 2]; 1199 } 1200 if (mRS.nContextGetUserMessage(mRS.mContext, rbuf) != 1201 RS_MESSAGE_TO_CLIENT_USER) { 1202 throw new RSDriverException("Error processing message from RenderScript."); 1203 } 1204 1205 if(mRS.mMessageCallback != null) { 1206 mRS.mMessageCallback.mData = rbuf; 1207 mRS.mMessageCallback.mID = subID; 1208 mRS.mMessageCallback.mLength = size; 1209 mRS.mMessageCallback.run(); 1210 } else { 1211 throw new RSInvalidStateException("Received a message from the script with no message handler installed."); 1212 } 1213 continue; 1214 } 1215 1216 if (msg == RS_MESSAGE_TO_CLIENT_ERROR) { 1217 String e = mRS.nContextGetErrorMessage(mRS.mContext); 1218 1219 // Throw RSRuntimeException under the following conditions: 1220 // 1221 // 1) It is an unknown fatal error. 1222 // 2) It is a debug fatal error, and we are not in a 1223 // debug context. 1224 // 3) It is a debug fatal error, and we do not have an 1225 // error callback. 1226 if (subID >= RS_ERROR_FATAL_UNKNOWN || 1227 (subID >= RS_ERROR_FATAL_DEBUG && 1228 (mRS.mContextType != ContextType.DEBUG || 1229 mRS.mErrorCallback == null))) { 1230 throw new RSRuntimeException("Fatal error " + subID + ", details: " + e); 1231 } 1232 1233 if(mRS.mErrorCallback != null) { 1234 mRS.mErrorCallback.mErrorMessage = e; 1235 mRS.mErrorCallback.mErrorNum = subID; 1236 mRS.mErrorCallback.run(); 1237 } else { 1238 android.util.Log.e(LOG_TAG, "non fatal RS error, " + e); 1239 // Do not throw here. In these cases, we do not have 1240 // a fatal error. 1241 } 1242 continue; 1243 } 1244 1245 if (msg == RS_MESSAGE_TO_CLIENT_NEW_BUFFER) { 1246 if (mRS.nContextGetUserMessage(mRS.mContext, rbuf) != 1247 RS_MESSAGE_TO_CLIENT_NEW_BUFFER) { 1248 throw new RSDriverException("Error processing message from RenderScript."); 1249 } 1250 long bufferID = ((long)rbuf[1] << 32L) + ((long)rbuf[0] & 0xffffffffL); 1251 Allocation.sendBufferNotification(bufferID); 1252 continue; 1253 } 1254 1255 // 2: teardown. 1256 // But we want to avoid starving other threads during 1257 // teardown by yielding until the next line in the destructor 1258 // can execute to set mRun = false 1259 try { 1260 sleep(1, 0); 1261 } catch(InterruptedException e) { 1262 } 1263 } 1264 //Log.d(LOG_TAG, "MessageThread exiting."); 1265 } 1266 } 1267 1268 RenderScript(Context ctx) { 1269 mContextType = ContextType.NORMAL; 1270 if (ctx != null) { 1271 mApplicationContext = ctx.getApplicationContext(); 1272 } 1273 mRWLock = new ReentrantReadWriteLock(); 1274 try { 1275 registerNativeAllocation.invoke(sRuntime, 4 * 1024 * 1024); // 4MB for GC sake 1276 } catch (Exception e) { 1277 Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e); 1278 throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e); 1279 } 1280 1281 } 1282 1283 /** 1284 * Gets the application context associated with the RenderScript context. 1285 * 1286 * @return The application context. 1287 */ 1288 public final Context getApplicationContext() { 1289 return mApplicationContext; 1290 } 1291 1292 /** 1293 * @hide 1294 */ 1295 public static RenderScript create(Context ctx, int sdkVersion) { 1296 return create(ctx, sdkVersion, ContextType.NORMAL, CREATE_FLAG_NONE); 1297 } 1298 1299 /** 1300 * Create a RenderScript context. 1301 * 1302 * @hide 1303 * @param ctx The context. 1304 * @return RenderScript 1305 */ 1306 public static RenderScript create(Context ctx, int sdkVersion, ContextType ct, int flags) { 1307 if (!sInitialized) { 1308 Log.e(LOG_TAG, "RenderScript.create() called when disabled; someone is likely to crash"); 1309 return null; 1310 } 1311 1312 if ((flags & ~(CREATE_FLAG_LOW_LATENCY | CREATE_FLAG_LOW_POWER)) != 0) { 1313 throw new RSIllegalArgumentException("Invalid flags passed."); 1314 } 1315 1316 RenderScript rs = new RenderScript(ctx); 1317 1318 rs.mDev = rs.nDeviceCreate(); 1319 rs.mContext = rs.nContextCreate(rs.mDev, flags, sdkVersion, ct.mID); 1320 rs.mContextType = ct; 1321 if (rs.mContext == 0) { 1322 throw new RSDriverException("Failed to create RS context."); 1323 } 1324 rs.mMessageThread = new MessageThread(rs); 1325 rs.mMessageThread.start(); 1326 return rs; 1327 } 1328 1329 /** 1330 * Create a RenderScript context. 1331 * 1332 * @param ctx The context. 1333 * @return RenderScript 1334 */ 1335 public static RenderScript create(Context ctx) { 1336 return create(ctx, ContextType.NORMAL); 1337 } 1338 1339 /** 1340 * Create a RenderScript context. 1341 * 1342 * 1343 * @param ctx The context. 1344 * @param ct The type of context to be created. 1345 * @return RenderScript 1346 */ 1347 public static RenderScript create(Context ctx, ContextType ct) { 1348 int v = ctx.getApplicationInfo().targetSdkVersion; 1349 return create(ctx, v, ct, CREATE_FLAG_NONE); 1350 } 1351 1352 /** 1353 * Create a RenderScript context. 1354 * 1355 * 1356 * @param ctx The context. 1357 * @param ct The type of context to be created. 1358 * @param flags The OR of the CREATE_FLAG_* options desired 1359 * @return RenderScript 1360 */ 1361 public static RenderScript create(Context ctx, ContextType ct, int flags) { 1362 int v = ctx.getApplicationInfo().targetSdkVersion; 1363 return create(ctx, v, ct, flags); 1364 } 1365 1366 /** 1367 * Print the currently available debugging information about the state of 1368 * the RS context to the log. 1369 * 1370 */ 1371 public void contextDump() { 1372 validate(); 1373 nContextDump(0); 1374 } 1375 1376 /** 1377 * Wait for any pending asynchronous opeations (such as copies to a RS 1378 * allocation or RS script executions) to complete. 1379 * 1380 */ 1381 public void finish() { 1382 nContextFinish(); 1383 } 1384 1385 /** 1386 * Destroys this RenderScript context. Once this function is called, 1387 * using this context or any objects belonging to this context is 1388 * illegal. 1389 * 1390 */ 1391 public void destroy() { 1392 validate(); 1393 nContextFinish(); 1394 1395 nContextDeinitToClient(mContext); 1396 mMessageThread.mRun = false; 1397 try { 1398 mMessageThread.join(); 1399 } catch(InterruptedException e) { 1400 } 1401 1402 nContextDestroy(); 1403 1404 nDeviceDestroy(mDev); 1405 mDev = 0; 1406 } 1407 1408 boolean isAlive() { 1409 return mContext != 0; 1410 } 1411 1412 long safeID(BaseObj o) { 1413 if(o != null) { 1414 return o.getID(this); 1415 } 1416 return 0; 1417 } 1418} 1419