RenderScript.java revision d78be37d81f6c1aba75180c7608753a027a881ee
1/* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.renderscript; 18 19import java.lang.reflect.Field; 20 21import android.graphics.Bitmap; 22import android.graphics.BitmapFactory; 23import android.util.Config; 24import android.util.Log; 25import android.view.Surface; 26 27 28/** 29 * @hide 30 * 31 **/ 32public class RenderScript { 33 static final String LOG_TAG = "RenderScript_jni"; 34 protected static final boolean DEBUG = false; 35 @SuppressWarnings({"UnusedDeclaration", "deprecation"}) 36 protected static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV; 37 38 39 40 /* 41 * We use a class initializer to allow the native code to cache some 42 * field offsets. 43 */ 44 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) 45 protected static boolean sInitialized; 46 native protected static void _nInit(); 47 48 49 static { 50 sInitialized = false; 51 try { 52 System.loadLibrary("rs_jni"); 53 _nInit(); 54 sInitialized = true; 55 } catch (UnsatisfiedLinkError e) { 56 Log.d(LOG_TAG, "RenderScript JNI library not found!"); 57 } 58 } 59 60 // Non-threadsafe functions. 61 native void nInitElements(int a8, int rgba4444, int rgba8888, int rgb565); 62 native int nDeviceCreate(); 63 native void nDeviceDestroy(int dev); 64 native void nDeviceSetConfig(int dev, int param, int value); 65 native int nContextGetMessage(int con, int[] data, boolean wait); 66 native void nContextInitToClient(int con); 67 native void nContextDeinitToClient(int con); 68 69 70 // Methods below are wrapped to protect the non-threadsafe 71 // lockless fifo. 72 native int rsnContextCreateGL(int dev, int ver, boolean useDepth); 73 synchronized int nContextCreateGL(int dev, int ver, boolean useDepth) { 74 return rsnContextCreateGL(dev, ver, useDepth); 75 } 76 native int rsnContextCreate(int dev, int ver); 77 synchronized int nContextCreate(int dev, int ver) { 78 return rsnContextCreate(dev, ver); 79 } 80 native void rsnContextDestroy(int con); 81 synchronized void nContextDestroy() { 82 rsnContextDestroy(mContext); 83 } 84 native void rsnContextSetSurface(int con, int w, int h, Surface sur); 85 synchronized void nContextSetSurface(int w, int h, Surface sur) { 86 rsnContextSetSurface(mContext, w, h, sur); 87 } 88 native void rsnContextSetPriority(int con, int p); 89 synchronized void nContextSetPriority(int p) { 90 rsnContextSetPriority(mContext, p); 91 } 92 native void rsnContextDump(int con, int bits); 93 synchronized void nContextDump(int bits) { 94 rsnContextDump(mContext, bits); 95 } 96 native void rsnContextFinish(int con); 97 synchronized void nContextFinish() { 98 rsnContextFinish(mContext); 99 } 100 101 native void rsnContextBindRootScript(int con, int script); 102 synchronized void nContextBindRootScript(int script) { 103 rsnContextBindRootScript(mContext, script); 104 } 105 native void rsnContextBindSampler(int con, int sampler, int slot); 106 synchronized void nContextBindSampler(int sampler, int slot) { 107 rsnContextBindSampler(mContext, sampler, slot); 108 } 109 native void rsnContextBindProgramStore(int con, int pfs); 110 synchronized void nContextBindProgramStore(int pfs) { 111 rsnContextBindProgramStore(mContext, pfs); 112 } 113 native void rsnContextBindProgramFragment(int con, int pf); 114 synchronized void nContextBindProgramFragment(int pf) { 115 rsnContextBindProgramFragment(mContext, pf); 116 } 117 native void rsnContextBindProgramVertex(int con, int pv); 118 synchronized void nContextBindProgramVertex(int pv) { 119 rsnContextBindProgramVertex(mContext, pv); 120 } 121 native void rsnContextBindProgramRaster(int con, int pr); 122 synchronized void nContextBindProgramRaster(int pr) { 123 rsnContextBindProgramRaster(mContext, pr); 124 } 125 native void rsnContextPause(int con); 126 synchronized void nContextPause() { 127 rsnContextPause(mContext); 128 } 129 native void rsnContextResume(int con); 130 synchronized void nContextResume() { 131 rsnContextResume(mContext); 132 } 133 134 native void rsnAssignName(int con, int obj, byte[] name); 135 synchronized void nAssignName(int obj, byte[] name) { 136 rsnAssignName(mContext, obj, name); 137 } 138 native String rsnGetName(int con, int obj); 139 synchronized String nGetName(int obj) { 140 return rsnGetName(mContext, obj); 141 } 142 native void rsnObjDestroy(int con, int id); 143 synchronized void nObjDestroy(int id) { 144 rsnObjDestroy(mContext, id); 145 } 146 native int rsnFileOpen(int con, byte[] name); 147 synchronized int nFileOpen(byte[] name) { 148 return rsnFileOpen(mContext, name); 149 } 150 151 native int rsnElementCreate(int con, int type, int kind, boolean norm, int vecSize); 152 synchronized int nElementCreate(int type, int kind, boolean norm, int vecSize) { 153 return rsnElementCreate(mContext, type, kind, norm, vecSize); 154 } 155 native int rsnElementCreate2(int con, int[] elements, String[] names); 156 synchronized int nElementCreate2(int[] elements, String[] names) { 157 return rsnElementCreate2(mContext, elements, names); 158 } 159 native void rsnElementGetNativeData(int con, int id, int[] elementData); 160 synchronized void nElementGetNativeData(int id, int[] elementData) { 161 rsnElementGetNativeData(mContext, id, elementData); 162 } 163 native void rsnElementGetSubElements(int con, int id, int[] IDs, String[] names); 164 synchronized void nElementGetSubElements(int id, int[] IDs, String[] names) { 165 rsnElementGetSubElements(mContext, id, IDs, names); 166 } 167 168 native void rsnTypeBegin(int con, int elementID); 169 synchronized void nTypeBegin(int elementID) { 170 rsnTypeBegin(mContext, elementID); 171 } 172 native void rsnTypeAdd(int con, int dim, int val); 173 synchronized void nTypeAdd(int dim, int val) { 174 rsnTypeAdd(mContext, dim, val); 175 } 176 native int rsnTypeCreate(int con); 177 synchronized int nTypeCreate() { 178 return rsnTypeCreate(mContext); 179 } 180 native void rsnTypeFinalDestroy(int con, Type t); 181 synchronized void nTypeFinalDestroy(Type t) { 182 rsnTypeFinalDestroy(mContext, t); 183 } 184 native void rsnTypeSetupFields(int con, Type t, int[] types, int[] bits, Field[] IDs); 185 synchronized void nTypeSetupFields(Type t, int[] types, int[] bits, Field[] IDs) { 186 rsnTypeSetupFields(mContext, t, types, bits, IDs); 187 } 188 native void rsnTypeGetNativeData(int con, int id, int[] typeData); 189 synchronized void nTypeGetNativeData(int id, int[] typeData) { 190 rsnTypeGetNativeData(mContext, id, typeData); 191 } 192 193 native int rsnAllocationCreateTyped(int con, int type); 194 synchronized int nAllocationCreateTyped(int type) { 195 return rsnAllocationCreateTyped(mContext, type); 196 } 197 native int rsnAllocationCreateFromBitmap(int con, int dstFmt, boolean genMips, Bitmap bmp); 198 synchronized int nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp) { 199 return rsnAllocationCreateFromBitmap(mContext, dstFmt, genMips, bmp); 200 } 201 native int rsnAllocationCreateBitmapRef(int con, int type, Bitmap bmp); 202 synchronized int nAllocationCreateBitmapRef(int type, Bitmap bmp) { 203 return rsnAllocationCreateBitmapRef(mContext, type, bmp); 204 } 205 native int rsnAllocationCreateFromBitmapBoxed(int con, int dstFmt, boolean genMips, Bitmap bmp); 206 synchronized int nAllocationCreateFromBitmapBoxed(int dstFmt, boolean genMips, Bitmap bmp) { 207 return rsnAllocationCreateFromBitmapBoxed(mContext, dstFmt, genMips, bmp); 208 } 209 native int rsnAllocationCreateFromAssetStream(int con, int dstFmt, boolean genMips, int assetStream); 210 synchronized int nAllocationCreateFromAssetStream(int dstFmt, boolean genMips, int assetStream) { 211 return rsnAllocationCreateFromAssetStream(mContext, dstFmt, genMips, assetStream); 212 } 213 214 native void rsnAllocationUploadToTexture(int con, int alloc, boolean genMips, int baseMioLevel); 215 synchronized void nAllocationUploadToTexture(int alloc, boolean genMips, int baseMioLevel) { 216 rsnAllocationUploadToTexture(mContext, alloc, genMips, baseMioLevel); 217 } 218 native void rsnAllocationUploadToBufferObject(int con, int alloc); 219 synchronized void nAllocationUploadToBufferObject(int alloc) { 220 rsnAllocationUploadToBufferObject(mContext, alloc); 221 } 222 223 native void rsnAllocationSubData1D(int con, int id, int off, int count, int[] d, int sizeBytes); 224 synchronized void nAllocationSubData1D(int id, int off, int count, int[] d, int sizeBytes) { 225 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes); 226 } 227 native void rsnAllocationSubData1D(int con, int id, int off, int count, short[] d, int sizeBytes); 228 synchronized void nAllocationSubData1D(int id, int off, int count, short[] d, int sizeBytes) { 229 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes); 230 } 231 native void rsnAllocationSubData1D(int con, int id, int off, int count, byte[] d, int sizeBytes); 232 synchronized void nAllocationSubData1D(int id, int off, int count, byte[] d, int sizeBytes) { 233 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes); 234 } 235 native void rsnAllocationSubData1D(int con, int id, int off, int count, float[] d, int sizeBytes); 236 synchronized void nAllocationSubData1D(int id, int off, int count, float[] d, int sizeBytes) { 237 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes); 238 } 239 240 native void rsnAllocationSubData2D(int con, int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes); 241 synchronized void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes) { 242 rsnAllocationSubData2D(mContext, id, xoff, yoff, w, h, d, sizeBytes); 243 } 244 native void rsnAllocationSubData2D(int con, int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes); 245 synchronized void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes) { 246 rsnAllocationSubData2D(mContext, id, xoff, yoff, w, h, d, sizeBytes); 247 } 248 native void rsnAllocationRead(int con, int id, int[] d); 249 synchronized void nAllocationRead(int id, int[] d) { 250 rsnAllocationRead(mContext, id, d); 251 } 252 native void rsnAllocationRead(int con, int id, float[] d); 253 synchronized void nAllocationRead(int id, float[] d) { 254 rsnAllocationRead(mContext, id, d); 255 } 256 native void rsnAllocationSubDataFromObject(int con, int id, Type t, int offset, Object o); 257 synchronized void nAllocationSubDataFromObject(int id, Type t, int offset, Object o) { 258 rsnAllocationSubDataFromObject(mContext, id, t, offset, o); 259 } 260 native void rsnAllocationSubReadFromObject(int con, int id, Type t, int offset, Object o); 261 synchronized void nAllocationSubReadFromObject(int id, Type t, int offset, Object o) { 262 rsnAllocationSubReadFromObject(mContext, id, t, offset, o); 263 } 264 native int rsnAllocationGetType(int con, int id); 265 synchronized int nAllocationGetType(int id) { 266 return rsnAllocationGetType(mContext, id); 267 } 268 269 native int rsnFileA3DCreateFromAssetStream(int con, int assetStream); 270 synchronized int nFileA3DCreateFromAssetStream(int assetStream) { 271 return rsnFileA3DCreateFromAssetStream(mContext, assetStream); 272 } 273 native int rsnFileA3DGetNumIndexEntries(int con, int fileA3D); 274 synchronized int nFileA3DGetNumIndexEntries(int fileA3D) { 275 return rsnFileA3DGetNumIndexEntries(mContext, fileA3D); 276 } 277 native void rsnFileA3DGetIndexEntries(int con, int fileA3D, int numEntries, int[] IDs, String[] names); 278 synchronized void nFileA3DGetIndexEntries(int fileA3D, int numEntries, int[] IDs, String[] names) { 279 rsnFileA3DGetIndexEntries(mContext, fileA3D, numEntries, IDs, names); 280 } 281 native int rsnFileA3DGetEntryByIndex(int con, int fileA3D, int index); 282 synchronized int nFileA3DGetEntryByIndex(int fileA3D, int index) { 283 return rsnFileA3DGetEntryByIndex(mContext, fileA3D, index); 284 } 285 286 native int rsnFontCreateFromFile(int con, String fileName, int size, int dpi); 287 synchronized int nFontCreateFromFile(String fileName, int size, int dpi) { 288 return rsnFontCreateFromFile(mContext, fileName, size, dpi); 289 } 290 291 native void rsnAdapter1DBindAllocation(int con, int ad, int alloc); 292 synchronized void nAdapter1DBindAllocation(int ad, int alloc) { 293 rsnAdapter1DBindAllocation(mContext, ad, alloc); 294 } 295 native void rsnAdapter1DSetConstraint(int con, int ad, int dim, int value); 296 synchronized void nAdapter1DSetConstraint(int ad, int dim, int value) { 297 rsnAdapter1DSetConstraint(mContext, ad, dim, value); 298 } 299 native void rsnAdapter1DData(int con, int ad, int[] d); 300 synchronized void nAdapter1DData(int ad, int[] d) { 301 rsnAdapter1DData(mContext, ad, d); 302 } 303 native void rsnAdapter1DData(int con, int ad, float[] d); 304 synchronized void nAdapter1DData(int ad, float[] d) { 305 rsnAdapter1DData(mContext, ad, d); 306 } 307 native void rsnAdapter1DSubData(int con, int ad, int off, int count, int[] d); 308 synchronized void nAdapter1DSubData(int ad, int off, int count, int[] d) { 309 rsnAdapter1DSubData(mContext, ad, off, count, d); 310 } 311 native void rsnAdapter1DSubData(int con, int ad, int off, int count, float[] d); 312 synchronized void nAdapter1DSubData(int ad, int off, int count, float[] d) { 313 rsnAdapter1DSubData(mContext, ad, off, count, d); 314 } 315 native int rsnAdapter1DCreate(int con); 316 synchronized int nAdapter1DCreate() { 317 return rsnAdapter1DCreate(mContext); 318 } 319 320 native void rsnAdapter2DBindAllocation(int con, int ad, int alloc); 321 synchronized void nAdapter2DBindAllocation(int ad, int alloc) { 322 rsnAdapter2DBindAllocation(mContext, ad, alloc); 323 } 324 native void rsnAdapter2DSetConstraint(int con, int ad, int dim, int value); 325 synchronized void nAdapter2DSetConstraint(int ad, int dim, int value) { 326 rsnAdapter2DSetConstraint(mContext, ad, dim, value); 327 } 328 native void rsnAdapter2DData(int con, int ad, int[] d); 329 synchronized void nAdapter2DData(int ad, int[] d) { 330 rsnAdapter2DData(mContext, ad, d); 331 } 332 native void rsnAdapter2DData(int con, int ad, float[] d); 333 synchronized void nAdapter2DData(int ad, float[] d) { 334 rsnAdapter2DData(mContext, ad, d); 335 } 336 native void rsnAdapter2DSubData(int con, int ad, int xoff, int yoff, int w, int h, int[] d); 337 synchronized void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, int[] d) { 338 rsnAdapter2DSubData(mContext, ad, xoff, yoff, w, h, d); 339 } 340 native void rsnAdapter2DSubData(int con, int ad, int xoff, int yoff, int w, int h, float[] d); 341 synchronized void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, float[] d) { 342 rsnAdapter2DSubData(mContext, ad, xoff, yoff, w, h, d); 343 } 344 native int rsnAdapter2DCreate(int con); 345 synchronized int nAdapter2DCreate() { 346 return rsnAdapter2DCreate(mContext); 347 } 348 349 native void rsnScriptBindAllocation(int con, int script, int alloc, int slot); 350 synchronized void nScriptBindAllocation(int script, int alloc, int slot) { 351 rsnScriptBindAllocation(mContext, script, alloc, slot); 352 } 353 native void rsnScriptSetTimeZone(int con, int script, byte[] timeZone); 354 synchronized void nScriptSetTimeZone(int script, byte[] timeZone) { 355 rsnScriptSetTimeZone(mContext, script, timeZone); 356 } 357 native void rsnScriptInvoke(int con, int id, int slot); 358 synchronized void nScriptInvoke(int id, int slot) { 359 rsnScriptInvoke(mContext, id, slot); 360 } 361 native void rsnScriptInvokeV(int con, int id, int slot, byte[] params); 362 synchronized void nScriptInvokeV(int id, int slot, byte[] params) { 363 rsnScriptInvokeV(mContext, id, slot, params); 364 } 365 native void rsnScriptSetVarI(int con, int id, int slot, int val); 366 synchronized void nScriptSetVarI(int id, int slot, int val) { 367 rsnScriptSetVarI(mContext, id, slot, val); 368 } 369 native void rsnScriptSetVarF(int con, int id, int slot, float val); 370 synchronized void nScriptSetVarF(int id, int slot, float val) { 371 rsnScriptSetVarF(mContext, id, slot, val); 372 } 373 native void rsnScriptSetVarV(int con, int id, int slot, byte[] val); 374 synchronized void nScriptSetVarV(int id, int slot, byte[] val) { 375 rsnScriptSetVarV(mContext, id, slot, val); 376 } 377 378 native void rsnScriptCBegin(int con); 379 synchronized void nScriptCBegin() { 380 rsnScriptCBegin(mContext); 381 } 382 native void rsnScriptCSetScript(int con, byte[] script, int offset, int length); 383 synchronized void nScriptCSetScript(byte[] script, int offset, int length) { 384 rsnScriptCSetScript(mContext, script, offset, length); 385 } 386 native int rsnScriptCCreate(int con); 387 synchronized int nScriptCCreate() { 388 return rsnScriptCCreate(mContext); 389 } 390 391 native void rsnSamplerBegin(int con); 392 synchronized void nSamplerBegin() { 393 rsnSamplerBegin(mContext); 394 } 395 native void rsnSamplerSet(int con, int param, int value); 396 synchronized void nSamplerSet(int param, int value) { 397 rsnSamplerSet(mContext, param, value); 398 } 399 native int rsnSamplerCreate(int con); 400 synchronized int nSamplerCreate() { 401 return rsnSamplerCreate(mContext); 402 } 403 404 native void rsnProgramStoreBegin(int con, int in, int out); 405 synchronized void nProgramStoreBegin(int in, int out) { 406 rsnProgramStoreBegin(mContext, in, out); 407 } 408 native void rsnProgramStoreDepthFunc(int con, int func); 409 synchronized void nProgramStoreDepthFunc(int func) { 410 rsnProgramStoreDepthFunc(mContext, func); 411 } 412 native void rsnProgramStoreDepthMask(int con, boolean enable); 413 synchronized void nProgramStoreDepthMask(boolean enable) { 414 rsnProgramStoreDepthMask(mContext, enable); 415 } 416 native void rsnProgramStoreColorMask(int con, boolean r, boolean g, boolean b, boolean a); 417 synchronized void nProgramStoreColorMask(boolean r, boolean g, boolean b, boolean a) { 418 rsnProgramStoreColorMask(mContext, r, g, b, a); 419 } 420 native void rsnProgramStoreBlendFunc(int con, int src, int dst); 421 synchronized void nProgramStoreBlendFunc(int src, int dst) { 422 rsnProgramStoreBlendFunc(mContext, src, dst); 423 } 424 native void rsnProgramStoreDither(int con, boolean enable); 425 synchronized void nProgramStoreDither(boolean enable) { 426 rsnProgramStoreDither(mContext, enable); 427 } 428 native int rsnProgramStoreCreate(int con); 429 synchronized int nProgramStoreCreate() { 430 return rsnProgramStoreCreate(mContext); 431 } 432 433 native int rsnProgramRasterCreate(int con, boolean pointSmooth, boolean lineSmooth, boolean pointSprite); 434 synchronized int nProgramRasterCreate(boolean pointSmooth, boolean lineSmooth, boolean pointSprite) { 435 return rsnProgramRasterCreate(mContext, pointSmooth, lineSmooth, pointSprite); 436 } 437 native void rsnProgramRasterSetLineWidth(int con, int pr, float v); 438 synchronized void nProgramRasterSetLineWidth(int pr, float v) { 439 rsnProgramRasterSetLineWidth(mContext, pr, v); 440 } 441 native void rsnProgramRasterSetCullMode(int con, int pr, int mode); 442 synchronized void nProgramRasterSetCullMode(int pr, int mode) { 443 rsnProgramRasterSetCullMode(mContext, pr, mode); 444 } 445 446 native void rsnProgramBindConstants(int con, int pv, int slot, int mID); 447 synchronized void nProgramBindConstants(int pv, int slot, int mID) { 448 rsnProgramBindConstants(mContext, pv, slot, mID); 449 } 450 native void rsnProgramBindTexture(int con, int vpf, int slot, int a); 451 synchronized void nProgramBindTexture(int vpf, int slot, int a) { 452 rsnProgramBindTexture(mContext, vpf, slot, a); 453 } 454 native void rsnProgramBindSampler(int con, int vpf, int slot, int s); 455 synchronized void nProgramBindSampler(int vpf, int slot, int s) { 456 rsnProgramBindSampler(mContext, vpf, slot, s); 457 } 458 459 native int rsnProgramFragmentCreate(int con, int[] params); 460 synchronized int nProgramFragmentCreate(int[] params) { 461 return rsnProgramFragmentCreate(mContext, params); 462 } 463 native int rsnProgramFragmentCreate2(int con, String shader, int[] params); 464 synchronized int nProgramFragmentCreate2(String shader, int[] params) { 465 return rsnProgramFragmentCreate2(mContext, shader, params); 466 } 467 468 native int rsnProgramVertexCreate(int con, boolean texMat); 469 synchronized int nProgramVertexCreate(boolean texMat) { 470 return rsnProgramVertexCreate(mContext, texMat); 471 } 472 native int rsnProgramVertexCreate2(int con, String shader, int[] params); 473 synchronized int nProgramVertexCreate2(String shader, int[] params) { 474 return rsnProgramVertexCreate2(mContext, shader, params); 475 } 476 477 478 native int rsnMeshCreate(int con, int vtxCount, int indexCount); 479 synchronized int nMeshCreate(int vtxCount, int indexCount) { 480 return rsnMeshCreate(mContext, vtxCount, indexCount); 481 } 482 native void rsnMeshBindVertex(int con, int id, int alloc, int slot); 483 synchronized void nMeshBindVertex(int id, int alloc, int slot) { 484 rsnMeshBindVertex(mContext, id, alloc, slot); 485 } 486 native void rsnMeshBindIndex(int con, int id, int alloc, int prim, int slot); 487 synchronized void nMeshBindIndex(int id, int alloc, int prim, int slot) { 488 rsnMeshBindIndex(mContext, id, alloc, prim, slot); 489 } 490 native int rsnMeshGetVertexBufferCount(int con, int id); 491 synchronized int nMeshGetVertexBufferCount(int id) { 492 return rsnMeshGetVertexBufferCount(mContext, id); 493 } 494 native int rsnMeshGetIndexCount(int con, int id); 495 synchronized int nMeshGetIndexCount(int id) { 496 return rsnMeshGetIndexCount(mContext, id); 497 } 498 native void rsnMeshGetVertices(int con, int id, int[] vtxIds, int vtxIdCount); 499 synchronized void nMeshGetVertices(int id, int[] vtxIds, int vtxIdCount) { 500 rsnMeshGetVertices(mContext, id, vtxIds, vtxIdCount); 501 } 502 native void rsnMeshGetIndices(int con, int id, int[] idxIds, int[] primitives, int vtxIdCount); 503 synchronized void nMeshGetIndices(int id, int[] idxIds, int[] primitives, int vtxIdCount) { 504 rsnMeshGetIndices(mContext, id, idxIds, primitives, vtxIdCount); 505 } 506 507 508 protected int mDev; 509 protected int mContext; 510 @SuppressWarnings({"FieldCanBeLocal"}) 511 protected MessageThread mMessageThread; 512 513 Element mElement_U8; 514 Element mElement_I8; 515 Element mElement_U16; 516 Element mElement_I16; 517 Element mElement_U32; 518 Element mElement_I32; 519 Element mElement_F32; 520 Element mElement_BOOLEAN; 521 522 Element mElement_ELEMENT; 523 Element mElement_TYPE; 524 Element mElement_ALLOCATION; 525 Element mElement_SAMPLER; 526 Element mElement_SCRIPT; 527 Element mElement_MESH; 528 Element mElement_PROGRAM_FRAGMENT; 529 Element mElement_PROGRAM_VERTEX; 530 Element mElement_PROGRAM_RASTER; 531 Element mElement_PROGRAM_STORE; 532 533 Element mElement_A_8; 534 Element mElement_RGB_565; 535 Element mElement_RGB_888; 536 Element mElement_RGBA_5551; 537 Element mElement_RGBA_4444; 538 Element mElement_RGBA_8888; 539 540 Element mElement_FLOAT_2; 541 Element mElement_FLOAT_3; 542 Element mElement_FLOAT_4; 543 Element mElement_UCHAR_4; 544 545 Sampler mSampler_CLAMP_NEAREST; 546 Sampler mSampler_CLAMP_LINEAR; 547 Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR; 548 Sampler mSampler_WRAP_NEAREST; 549 Sampler mSampler_WRAP_LINEAR; 550 Sampler mSampler_WRAP_LINEAR_MIP_LINEAR; 551 552 /////////////////////////////////////////////////////////////////////////////////// 553 // 554 555 public static class RSMessage implements Runnable { 556 protected int[] mData; 557 protected int mID; 558 public void run() { 559 } 560 } 561 public RSMessage mMessageCallback = null; 562 563 public enum Priority { 564 LOW (5), //ANDROID_PRIORITY_BACKGROUND + 5 565 NORMAL (-4); //ANDROID_PRIORITY_DISPLAY 566 567 int mID; 568 Priority(int id) { 569 mID = id; 570 } 571 } 572 573 void validate() { 574 if (mContext == 0) { 575 throw new IllegalStateException("Calling RS with no Context active."); 576 } 577 } 578 579 public void contextSetPriority(Priority p) { 580 validate(); 581 nContextSetPriority(p.mID); 582 } 583 584 protected static class MessageThread extends Thread { 585 RenderScript mRS; 586 boolean mRun = true; 587 588 MessageThread(RenderScript rs) { 589 super("RSMessageThread"); 590 mRS = rs; 591 592 } 593 594 public void run() { 595 // This function is a temporary solution. The final solution will 596 // used typed allocations where the message id is the type indicator. 597 int[] rbuf = new int[16]; 598 mRS.nContextInitToClient(mRS.mContext); 599 while(mRun) { 600 int msg = mRS.nContextGetMessage(mRS.mContext, rbuf, true); 601 if (msg == 0) { 602 // Should only happen during teardown. 603 // But we want to avoid starving other threads during 604 // teardown by yielding until the next line in the destructor 605 // can execute to set mRun = false 606 try { 607 sleep(1, 0); 608 } catch(InterruptedException e) { 609 } 610 } 611 if(mRS.mMessageCallback != null) { 612 mRS.mMessageCallback.mData = rbuf; 613 mRS.mMessageCallback.mID = msg; 614 mRS.mMessageCallback.run(); 615 } 616 } 617 Log.d(LOG_TAG, "MessageThread exiting."); 618 } 619 } 620 621 protected RenderScript() { 622 } 623 624 public static RenderScript create() { 625 RenderScript rs = new RenderScript(); 626 627 rs.mDev = rs.nDeviceCreate(); 628 rs.mContext = rs.nContextCreate(rs.mDev, 0); 629 rs.mMessageThread = new MessageThread(rs); 630 rs.mMessageThread.start(); 631 Element.initPredefined(rs); 632 return rs; 633 } 634 635 public void contextDump(int bits) { 636 validate(); 637 nContextDump(bits); 638 } 639 640 public void finish() { 641 nContextFinish(); 642 } 643 644 public void destroy() { 645 validate(); 646 nContextDeinitToClient(mContext); 647 mMessageThread.mRun = false; 648 649 nContextDestroy(); 650 mContext = 0; 651 652 nDeviceDestroy(mDev); 653 mDev = 0; 654 } 655 656 boolean isAlive() { 657 return mContext != 0; 658 } 659 660 /////////////////////////////////////////////////////////////////////////////////// 661 // Root state 662 663 protected int safeID(BaseObj o) { 664 if(o != null) { 665 return o.mID; 666 } 667 return 0; 668 } 669} 670 671 672 673