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