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