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