android_renderscript_RenderScript.cpp revision 2222aa90031604e9752ebc2909303a84139b8b18
1/* 2 * Copyright (C) 2006 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#define LOG_TAG "libRS_jni" 18 19#include <stdlib.h> 20#include <stdio.h> 21#include <fcntl.h> 22#include <unistd.h> 23#include <math.h> 24#include <utils/misc.h> 25 26#include <surfaceflinger/Surface.h> 27 28#include <core/SkBitmap.h> 29#include <core/SkPixelRef.h> 30#include <core/SkStream.h> 31#include <core/SkTemplates.h> 32#include <images/SkImageDecoder.h> 33 34#include <utils/Asset.h> 35#include <utils/ResourceTypes.h> 36 37#include "jni.h" 38#include "JNIHelp.h" 39#include "android_runtime/AndroidRuntime.h" 40#include "android_runtime/android_view_Surface.h" 41 42#include <RenderScript.h> 43#include <RenderScriptEnv.h> 44 45//#define LOG_API LOGE 46#define LOG_API(...) 47 48using namespace android; 49 50// --------------------------------------------------------------------------- 51 52static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL) 53{ 54 jclass npeClazz = env->FindClass(exc); 55 env->ThrowNew(npeClazz, msg); 56} 57 58static jfieldID gContextId = 0; 59static jfieldID gNativeBitmapID = 0; 60static jfieldID gTypeNativeCache = 0; 61 62static RsElement g_A_8 = NULL; 63static RsElement g_RGBA_4444 = NULL; 64static RsElement g_RGBA_8888 = NULL; 65static RsElement g_RGB_565 = NULL; 66 67static void _nInit(JNIEnv *_env, jclass _this) 68{ 69 gContextId = _env->GetFieldID(_this, "mContext", "I"); 70 71 jclass bitmapClass = _env->FindClass("android/graphics/Bitmap"); 72 gNativeBitmapID = _env->GetFieldID(bitmapClass, "mNativeBitmap", "I"); 73} 74 75static void nInitElements(JNIEnv *_env, jobject _this, jint a8, jint rgba4444, jint rgba8888, jint rgb565) 76{ 77 g_A_8 = reinterpret_cast<RsElement>(a8); 78 g_RGBA_4444 = reinterpret_cast<RsElement>(rgba4444); 79 g_RGBA_8888 = reinterpret_cast<RsElement>(rgba8888); 80 g_RGB_565 = reinterpret_cast<RsElement>(rgb565); 81} 82 83// --------------------------------------------------------------------------- 84 85static void 86nContextFinish(JNIEnv *_env, jobject _this, RsContext con) 87{ 88 LOG_API("nContextFinish, con(%p)", con); 89 rsContextFinish(con); 90} 91 92static void 93nAssignName(JNIEnv *_env, jobject _this, RsContext con, jint obj, jbyteArray str) 94{ 95 LOG_API("nAssignName, con(%p), obj(%p)", con, (void *)obj); 96 jint len = _env->GetArrayLength(str); 97 jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0); 98 rsAssignName(con, (void *)obj, (const char *)cptr, len); 99 _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT); 100} 101 102static jstring 103nGetName(JNIEnv *_env, jobject _this, RsContext con, jint obj) 104{ 105 LOG_API("nGetName, con(%p), obj(%p)", con, (void *)obj); 106 const char *name = NULL; 107 rsGetName(con, (void *)obj, &name); 108 return _env->NewStringUTF(name); 109} 110 111static void 112nObjDestroy(JNIEnv *_env, jobject _this, RsContext con, jint obj) 113{ 114 LOG_API("nObjDestroy, con(%p) obj(%p)", con, (void *)obj); 115 rsObjDestroy(con, (void *)obj); 116} 117 118 119static jint 120nFileOpen(JNIEnv *_env, jobject _this, RsContext con, jbyteArray str) 121{ 122 LOG_API("nFileOpen, con(%p)", con); 123 jint len = _env->GetArrayLength(str); 124 jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0); 125 jint ret = (jint)rsFileOpen(con, (const char *)cptr, len); 126 _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT); 127 return ret; 128} 129 130// --------------------------------------------------------------------------- 131 132static jint 133nDeviceCreate(JNIEnv *_env, jobject _this) 134{ 135 LOG_API("nDeviceCreate"); 136 return (jint)rsDeviceCreate(); 137} 138 139static void 140nDeviceDestroy(JNIEnv *_env, jobject _this, jint dev) 141{ 142 LOG_API("nDeviceDestroy"); 143 return rsDeviceDestroy((RsDevice)dev); 144} 145 146static void 147nDeviceSetConfig(JNIEnv *_env, jobject _this, jint dev, jint p, jint value) 148{ 149 LOG_API("nDeviceSetConfig dev(%p), param(%i), value(%i)", (void *)dev, p, value); 150 return rsDeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value); 151} 152 153static jint 154nContextCreate(JNIEnv *_env, jobject _this, jint dev, jint ver) 155{ 156 LOG_API("nContextCreate"); 157 return (jint)rsContextCreate((RsDevice)dev, ver); 158} 159 160static jint 161nContextCreateGL(JNIEnv *_env, jobject _this, jint dev, jint ver, jboolean useDepth) 162{ 163 LOG_API("nContextCreateGL"); 164 return (jint)rsContextCreateGL((RsDevice)dev, ver, useDepth); 165} 166 167static void 168nContextSetPriority(JNIEnv *_env, jobject _this, RsContext con, jint p) 169{ 170 LOG_API("ContextSetPriority, con(%p), priority(%i)", con, p); 171 rsContextSetPriority(con, p); 172} 173 174 175 176static void 177nContextSetSurface(JNIEnv *_env, jobject _this, RsContext con, jint width, jint height, jobject wnd) 178{ 179 LOG_API("nContextSetSurface, con(%p), width(%i), height(%i), surface(%p)", con, width, height, (Surface *)wnd); 180 181 Surface * window = NULL; 182 if (wnd == NULL) { 183 184 } else { 185 window = (Surface*) android_Surface_getNativeWindow(_env, wnd).get(); 186 } 187 188 rsContextSetSurface(con, width, height, window); 189} 190 191static void 192nContextDestroy(JNIEnv *_env, jobject _this, RsContext con) 193{ 194 LOG_API("nContextDestroy, con(%p)", con); 195 rsContextDestroy(con); 196} 197 198static void 199nContextDump(JNIEnv *_env, jobject _this, RsContext con, jint bits) 200{ 201 LOG_API("nContextDump, con(%p) bits(%i)", (RsContext)con, bits); 202 rsContextDump((RsContext)con, bits); 203} 204 205static void 206nContextPause(JNIEnv *_env, jobject _this, RsContext con) 207{ 208 LOG_API("nContextPause, con(%p)", con); 209 rsContextPause(con); 210} 211 212static void 213nContextResume(JNIEnv *_env, jobject _this, RsContext con) 214{ 215 LOG_API("nContextResume, con(%p)", con); 216 rsContextResume(con); 217} 218 219static jint 220nContextGetMessage(JNIEnv *_env, jobject _this, RsContext con, jintArray data, jboolean wait) 221{ 222 jint len = _env->GetArrayLength(data); 223 LOG_API("nContextGetMessage, con(%p), len(%i)", con, len); 224 jint *ptr = _env->GetIntArrayElements(data, NULL); 225 size_t receiveLen; 226 int id = rsContextGetMessage(con, ptr, &receiveLen, len * 4, wait); 227 if (!id && receiveLen) { 228 LOGV("message receive buffer too small. %i", receiveLen); 229 *ptr = (jint)receiveLen; 230 } 231 _env->ReleaseIntArrayElements(data, ptr, 0); 232 return id; 233} 234 235static void nContextInitToClient(JNIEnv *_env, jobject _this, RsContext con) 236{ 237 LOG_API("nContextInitToClient, con(%p)", con); 238 rsContextInitToClient(con); 239} 240 241static void nContextDeinitToClient(JNIEnv *_env, jobject _this, RsContext con) 242{ 243 LOG_API("nContextDeinitToClient, con(%p)", con); 244 rsContextDeinitToClient(con); 245} 246 247 248static jint 249nElementCreate(JNIEnv *_env, jobject _this, RsContext con, jint type, jint kind, jboolean norm, jint size) 250{ 251 LOG_API("nElementCreate, con(%p), type(%i), kind(%i), norm(%i), size(%i)", con, type, kind, norm, size); 252 return (jint)rsElementCreate(con, (RsDataType)type, (RsDataKind)kind, norm, size); 253} 254 255static jint 256nElementCreate2(JNIEnv *_env, jobject _this, RsContext con, jintArray _ids, jobjectArray _names, jintArray _arraySizes) 257{ 258 int fieldCount = _env->GetArrayLength(_ids); 259 LOG_API("nElementCreate2, con(%p)", con); 260 261 jint *ids = _env->GetIntArrayElements(_ids, NULL); 262 jint *arraySizes = _env->GetIntArrayElements(_arraySizes, NULL); 263 const char ** nameArray = (const char **)calloc(fieldCount, sizeof(char *)); 264 size_t* sizeArray = (size_t*)calloc(fieldCount, sizeof(size_t)); 265 266 for (int ct=0; ct < fieldCount; ct++) { 267 jstring s = (jstring)_env->GetObjectArrayElement(_names, ct); 268 nameArray[ct] = _env->GetStringUTFChars(s, NULL); 269 sizeArray[ct] = _env->GetStringUTFLength(s); 270 } 271 jint id = (jint)rsElementCreate2(con, fieldCount, (RsElement *)ids, nameArray, sizeArray, (const uint32_t *)arraySizes); 272 for (int ct=0; ct < fieldCount; ct++) { 273 jstring s = (jstring)_env->GetObjectArrayElement(_names, ct); 274 _env->ReleaseStringUTFChars(s, nameArray[ct]); 275 } 276 _env->ReleaseIntArrayElements(_ids, ids, JNI_ABORT); 277 _env->ReleaseIntArrayElements(_arraySizes, arraySizes, JNI_ABORT); 278 free(nameArray); 279 free(sizeArray); 280 return (jint)id; 281} 282 283static void 284nElementGetNativeData(JNIEnv *_env, jobject _this, RsContext con, jint id, jintArray _elementData) 285{ 286 int dataSize = _env->GetArrayLength(_elementData); 287 LOG_API("nElementGetNativeData, con(%p)", con); 288 289 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements 290 assert(dataSize == 5); 291 292 uint32_t elementData[5]; 293 rsElementGetNativeData(con, (RsElement)id, elementData, dataSize); 294 295 for(jint i = 0; i < dataSize; i ++) { 296 _env->SetIntArrayRegion(_elementData, i, 1, (const jint*)&elementData[i]); 297 } 298} 299 300 301static void 302nElementGetSubElements(JNIEnv *_env, jobject _this, RsContext con, jint id, jintArray _IDs, jobjectArray _names) 303{ 304 int dataSize = _env->GetArrayLength(_IDs); 305 LOG_API("nElementGetSubElements, con(%p)", con); 306 307 uint32_t *ids = (uint32_t *)malloc((uint32_t)dataSize * sizeof(uint32_t)); 308 const char **names = (const char **)malloc((uint32_t)dataSize * sizeof(const char *)); 309 310 rsElementGetSubElements(con, (RsElement)id, ids, names, (uint32_t)dataSize); 311 312 for(jint i = 0; i < dataSize; i ++) { 313 _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i])); 314 _env->SetIntArrayRegion(_IDs, i, 1, (const jint*)&ids[i]); 315 } 316 317 free(ids); 318 free(names); 319} 320 321// ----------------------------------- 322 323static void 324nTypeBegin(JNIEnv *_env, jobject _this, RsContext con, jint eID) 325{ 326 LOG_API("nTypeBegin, con(%p) e(%p)", con, (RsElement)eID); 327 rsTypeBegin(con, (RsElement)eID); 328} 329 330static void 331nTypeAdd(JNIEnv *_env, jobject _this, RsContext con, jint dim, jint val) 332{ 333 LOG_API("nTypeAdd, con(%p) dim(%i), val(%i)", con, dim, val); 334 rsTypeAdd(con, (RsDimension)dim, val); 335} 336 337static jint 338nTypeCreate(JNIEnv *_env, jobject _this, RsContext con) 339{ 340 LOG_API("nTypeCreate, con(%p)", con); 341 return (jint)rsTypeCreate(con); 342} 343 344static void 345nTypeGetNativeData(JNIEnv *_env, jobject _this, RsContext con, jint id, jintArray _typeData) 346{ 347 // We are packing 6 items: mDimX; mDimY; mDimZ; 348 // mDimLOD; mDimFaces; mElement; into typeData 349 int elementCount = _env->GetArrayLength(_typeData); 350 351 assert(elementCount == 6); 352 LOG_API("nTypeCreate, con(%p)", con); 353 354 uint32_t typeData[6]; 355 rsTypeGetNativeData(con, (RsType)id, typeData, 6); 356 357 for(jint i = 0; i < elementCount; i ++) { 358 _env->SetIntArrayRegion(_typeData, i, 1, (const jint*)&typeData[i]); 359 } 360} 361 362// ----------------------------------- 363 364static jint 365nAllocationCreateTyped(JNIEnv *_env, jobject _this, RsContext con, jint e) 366{ 367 LOG_API("nAllocationCreateTyped, con(%p), e(%p)", con, (RsElement)e); 368 return (jint) rsAllocationCreateTyped(con, (RsElement)e); 369} 370 371static void 372nAllocationUploadToTexture(JNIEnv *_env, jobject _this, RsContext con, jint a, jboolean genMip, jint mip) 373{ 374 LOG_API("nAllocationUploadToTexture, con(%p), a(%p), genMip(%i), mip(%i)", con, (RsAllocation)a, genMip, mip); 375 rsAllocationUploadToTexture(con, (RsAllocation)a, genMip, mip); 376} 377 378static void 379nAllocationUploadToBufferObject(JNIEnv *_env, jobject _this, RsContext con, jint a) 380{ 381 LOG_API("nAllocationUploadToBufferObject, con(%p), a(%p)", con, (RsAllocation)a); 382 rsAllocationUploadToBufferObject(con, (RsAllocation)a); 383} 384 385static RsElement SkBitmapToPredefined(SkBitmap::Config cfg) 386{ 387 switch (cfg) { 388 case SkBitmap::kA8_Config: 389 return g_A_8; 390 case SkBitmap::kARGB_4444_Config: 391 return g_RGBA_4444; 392 case SkBitmap::kARGB_8888_Config: 393 return g_RGBA_8888; 394 case SkBitmap::kRGB_565_Config: 395 return g_RGB_565; 396 397 default: 398 break; 399 } 400 // If we don't have a conversion mark it as a user type. 401 LOGE("Unsupported bitmap type"); 402 return NULL; 403} 404 405static int 406nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, RsContext con, jint dstFmt, jboolean genMips, jobject jbitmap) 407{ 408 SkBitmap const * nativeBitmap = 409 (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID); 410 const SkBitmap& bitmap(*nativeBitmap); 411 SkBitmap::Config config = bitmap.getConfig(); 412 413 RsElement e = SkBitmapToPredefined(config); 414 if (e) { 415 bitmap.lockPixels(); 416 const int w = bitmap.width(); 417 const int h = bitmap.height(); 418 const void* ptr = bitmap.getPixels(); 419 jint id = (jint)rsAllocationCreateFromBitmap(con, w, h, (RsElement)dstFmt, e, genMips, ptr); 420 bitmap.unlockPixels(); 421 return id; 422 } 423 return 0; 424} 425 426static void ReleaseBitmapCallback(void *bmp) 427{ 428 SkBitmap const * nativeBitmap = (SkBitmap const *)bmp; 429 nativeBitmap->unlockPixels(); 430} 431 432static int 433nAllocationCreateBitmapRef(JNIEnv *_env, jobject _this, RsContext con, jint type, jobject jbitmap) 434{ 435 SkBitmap * nativeBitmap = 436 (SkBitmap *)_env->GetIntField(jbitmap, gNativeBitmapID); 437 438 439 nativeBitmap->lockPixels(); 440 void* ptr = nativeBitmap->getPixels(); 441 jint id = (jint)rsAllocationCreateBitmapRef(con, (RsType)type, ptr, nativeBitmap, ReleaseBitmapCallback); 442 return id; 443} 444 445static int 446nAllocationCreateFromAssetStream(JNIEnv *_env, jobject _this, RsContext con, jint dstFmt, jboolean genMips, jint native_asset) 447{ 448 Asset* asset = reinterpret_cast<Asset*>(native_asset); 449 SkBitmap bitmap; 450 SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(), 451 &bitmap, SkBitmap::kNo_Config, SkImageDecoder::kDecodePixels_Mode); 452 453 SkBitmap::Config config = bitmap.getConfig(); 454 455 RsElement e = SkBitmapToPredefined(config); 456 457 if (e) { 458 bitmap.lockPixels(); 459 const int w = bitmap.width(); 460 const int h = bitmap.height(); 461 const void* ptr = bitmap.getPixels(); 462 jint id = (jint)rsAllocationCreateFromBitmap(con, w, h, (RsElement)dstFmt, e, genMips, ptr); 463 bitmap.unlockPixels(); 464 return id; 465 } 466 return 0; 467} 468 469static void 470nAllocationSubData1D_i(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint count, jintArray data, int sizeBytes) 471{ 472 jint len = _env->GetArrayLength(data); 473 LOG_API("nAllocation1DSubData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes); 474 jint *ptr = _env->GetIntArrayElements(data, NULL); 475 rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes); 476 _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT); 477} 478 479static void 480nAllocationSubData1D_s(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint count, jshortArray data, int sizeBytes) 481{ 482 jint len = _env->GetArrayLength(data); 483 LOG_API("nAllocation1DSubData_s, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes); 484 jshort *ptr = _env->GetShortArrayElements(data, NULL); 485 rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes); 486 _env->ReleaseShortArrayElements(data, ptr, JNI_ABORT); 487} 488 489static void 490nAllocationSubData1D_b(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint count, jbyteArray data, int sizeBytes) 491{ 492 jint len = _env->GetArrayLength(data); 493 LOG_API("nAllocation1DSubData_b, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes); 494 jbyte *ptr = _env->GetByteArrayElements(data, NULL); 495 rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes); 496 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT); 497} 498 499static void 500nAllocationSubData1D_f(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint count, jfloatArray data, int sizeBytes) 501{ 502 jint len = _env->GetArrayLength(data); 503 LOG_API("nAllocation1DSubData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes); 504 jfloat *ptr = _env->GetFloatArrayElements(data, NULL); 505 rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes); 506 _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT); 507} 508 509static void 510// native void rsnAllocationSubElementData1D(int con, int id, int xoff, int compIdx, byte[] d, int sizeBytes); 511nAllocationSubElementData1D(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint compIdx, jbyteArray data, int sizeBytes) 512{ 513 jint len = _env->GetArrayLength(data); 514 LOG_API("nAllocationSubElementData1D, con(%p), alloc(%p), offset(%i), comp(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, compIdx, len, sizeBytes); 515 jbyte *ptr = _env->GetByteArrayElements(data, NULL); 516 rsAllocation1DSubElementData(con, (RsAllocation)alloc, offset, ptr, compIdx, sizeBytes); 517 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT); 518} 519 520static void 521nAllocationSubData2D_i(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint w, jint h, jintArray data, int sizeBytes) 522{ 523 jint len = _env->GetArrayLength(data); 524 LOG_API("nAllocation2DSubData_i, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)", con, (RsAllocation)alloc, xoff, yoff, w, h, len); 525 jint *ptr = _env->GetIntArrayElements(data, NULL); 526 rsAllocation2DSubData(con, (RsAllocation)alloc, xoff, yoff, w, h, ptr, sizeBytes); 527 _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT); 528} 529 530static void 531nAllocationSubData2D_f(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint w, jint h, jfloatArray data, int sizeBytes) 532{ 533 jint len = _env->GetArrayLength(data); 534 LOG_API("nAllocation2DSubData_i, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)", con, (RsAllocation)alloc, xoff, yoff, w, h, len); 535 jfloat *ptr = _env->GetFloatArrayElements(data, NULL); 536 rsAllocation2DSubData(con, (RsAllocation)alloc, xoff, yoff, w, h, ptr, sizeBytes); 537 _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT); 538} 539 540static void 541nAllocationRead_i(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jintArray data) 542{ 543 jint len = _env->GetArrayLength(data); 544 LOG_API("nAllocationRead_i, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len); 545 jint *ptr = _env->GetIntArrayElements(data, NULL); 546 rsAllocationRead(con, (RsAllocation)alloc, ptr); 547 _env->ReleaseIntArrayElements(data, ptr, 0); 548} 549 550static void 551nAllocationRead_f(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jfloatArray data) 552{ 553 jint len = _env->GetArrayLength(data); 554 LOG_API("nAllocationRead_f, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len); 555 jfloat *ptr = _env->GetFloatArrayElements(data, NULL); 556 rsAllocationRead(con, (RsAllocation)alloc, ptr); 557 _env->ReleaseFloatArrayElements(data, ptr, 0); 558} 559 560static jint 561nAllocationGetType(JNIEnv *_env, jobject _this, RsContext con, jint a) 562{ 563 LOG_API("nAllocationGetType, con(%p), a(%p)", con, (RsAllocation)a); 564 return (jint) rsAllocationGetType(con, (RsAllocation)a); 565} 566 567static void 568nAllocationResize1D(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint dimX) 569{ 570 LOG_API("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", con, (RsAllocation)alloc, dimX); 571 rsAllocationResize1D(con, (RsAllocation)alloc, dimX); 572} 573 574static void 575nAllocationResize2D(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint dimX, jint dimY) 576{ 577 LOG_API("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i), sizeY(%i)", con, (RsAllocation)alloc, dimX, dimY); 578 rsAllocationResize2D(con, (RsAllocation)alloc, dimX, dimY); 579} 580 581// ----------------------------------- 582 583static int 584nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, RsContext con, jint native_asset) 585{ 586 LOGV("______nFileA3D %u", (uint32_t) native_asset); 587 588 Asset* asset = reinterpret_cast<Asset*>(native_asset); 589 590 jint id = (jint)rsFileA3DCreateFromAssetStream(con, asset->getBuffer(false), asset->getLength()); 591 return id; 592} 593 594static int 595nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, RsContext con, jint fileA3D) 596{ 597 int32_t numEntries = 0; 598 rsFileA3DGetNumIndexEntries(con, &numEntries, (RsFile)fileA3D); 599 return numEntries; 600} 601 602static void 603nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, RsContext con, jint fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries) 604{ 605 LOGV("______nFileA3D %u", (uint32_t) fileA3D); 606 RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry)); 607 608 rsFileA3DGetIndexEntries(con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D); 609 610 for(jint i = 0; i < numEntries; i ++) { 611 _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName)); 612 _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID); 613 } 614 615 free(fileEntries); 616} 617 618static int 619nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, RsContext con, jint fileA3D, jint index) 620{ 621 LOGV("______nFileA3D %u", (uint32_t) fileA3D); 622 jint id = (jint)rsFileA3DGetEntryByIndex(con, (uint32_t)index, (RsFile)fileA3D); 623 return id; 624} 625 626// ----------------------------------- 627 628static int 629nFontCreateFromFile(JNIEnv *_env, jobject _this, RsContext con, jstring fileName, jint fontSize, jint dpi) 630{ 631 const char* fileNameUTF = _env->GetStringUTFChars(fileName, NULL); 632 633 jint id = (jint)rsFontCreateFromFile(con, fileNameUTF, fontSize, dpi); 634 return id; 635} 636 637 638// ----------------------------------- 639 640static void 641nAdapter1DBindAllocation(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint alloc) 642{ 643 LOG_API("nAdapter1DBindAllocation, con(%p), adapter(%p), alloc(%p)", con, (RsAdapter1D)adapter, (RsAllocation)alloc); 644 rsAdapter1DBindAllocation(con, (RsAdapter1D)adapter, (RsAllocation)alloc); 645} 646 647static void 648nAdapter1DSetConstraint(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint dim, jint value) 649{ 650 LOG_API("nAdapter1DSetConstraint, con(%p), adapter(%p), dim(%i), value(%i)", con, (RsAdapter1D)adapter, dim, value); 651 rsAdapter1DSetConstraint(con, (RsAdapter1D)adapter, (RsDimension)dim, value); 652} 653 654static void 655nAdapter1DData_i(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jintArray data) 656{ 657 jint len = _env->GetArrayLength(data); 658 LOG_API("nAdapter1DData_i, con(%p), adapter(%p), len(%i)", con, (RsAdapter1D)adapter, len); 659 jint *ptr = _env->GetIntArrayElements(data, NULL); 660 rsAdapter1DData(con, (RsAdapter1D)adapter, ptr); 661 _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/); 662} 663 664static void 665nAdapter1DSubData_i(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint offset, jint count, jintArray data) 666{ 667 jint len = _env->GetArrayLength(data); 668 LOG_API("nAdapter1DSubData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAdapter1D)adapter, offset, count, len); 669 jint *ptr = _env->GetIntArrayElements(data, NULL); 670 rsAdapter1DSubData(con, (RsAdapter1D)adapter, offset, count, ptr); 671 _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/); 672} 673 674static void 675nAdapter1DData_f(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jfloatArray data) 676{ 677 jint len = _env->GetArrayLength(data); 678 LOG_API("nAdapter1DData_f, con(%p), adapter(%p), len(%i)", con, (RsAdapter1D)adapter, len); 679 jfloat *ptr = _env->GetFloatArrayElements(data, NULL); 680 rsAdapter1DData(con, (RsAdapter1D)adapter, ptr); 681 _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/); 682} 683 684static void 685nAdapter1DSubData_f(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint offset, jint count, jfloatArray data) 686{ 687 jint len = _env->GetArrayLength(data); 688 LOG_API("nAdapter1DSubData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAdapter1D)adapter, offset, count, len); 689 jfloat *ptr = _env->GetFloatArrayElements(data, NULL); 690 rsAdapter1DSubData(con, (RsAdapter1D)adapter, offset, count, ptr); 691 _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/); 692} 693 694static jint 695nAdapter1DCreate(JNIEnv *_env, jobject _this, RsContext con) 696{ 697 LOG_API("nAdapter1DCreate, con(%p)", con); 698 return (jint)rsAdapter1DCreate(con); 699} 700 701// ----------------------------------- 702 703static void 704nAdapter2DBindAllocation(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint alloc) 705{ 706 LOG_API("nAdapter2DBindAllocation, con(%p), adapter(%p), alloc(%p)", con, (RsAdapter2D)adapter, (RsAllocation)alloc); 707 rsAdapter2DBindAllocation(con, (RsAdapter2D)adapter, (RsAllocation)alloc); 708} 709 710static void 711nAdapter2DSetConstraint(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint dim, jint value) 712{ 713 LOG_API("nAdapter2DSetConstraint, con(%p), adapter(%p), dim(%i), value(%i)", con, (RsAdapter2D)adapter, dim, value); 714 rsAdapter2DSetConstraint(con, (RsAdapter2D)adapter, (RsDimension)dim, value); 715} 716 717static void 718nAdapter2DData_i(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jintArray data) 719{ 720 jint len = _env->GetArrayLength(data); 721 LOG_API("nAdapter2DData_i, con(%p), adapter(%p), len(%i)", con, (RsAdapter2D)adapter, len); 722 jint *ptr = _env->GetIntArrayElements(data, NULL); 723 rsAdapter2DData(con, (RsAdapter2D)adapter, ptr); 724 _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/); 725} 726 727static void 728nAdapter2DData_f(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jfloatArray data) 729{ 730 jint len = _env->GetArrayLength(data); 731 LOG_API("nAdapter2DData_f, con(%p), adapter(%p), len(%i)", con, (RsAdapter2D)adapter, len); 732 jfloat *ptr = _env->GetFloatArrayElements(data, NULL); 733 rsAdapter2DData(con, (RsAdapter2D)adapter, ptr); 734 _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/); 735} 736 737static void 738nAdapter2DSubData_i(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint xoff, jint yoff, jint w, jint h, jintArray data) 739{ 740 jint len = _env->GetArrayLength(data); 741 LOG_API("nAdapter2DSubData_i, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)", 742 con, (RsAdapter2D)adapter, xoff, yoff, w, h, len); 743 jint *ptr = _env->GetIntArrayElements(data, NULL); 744 rsAdapter2DSubData(con, (RsAdapter2D)adapter, xoff, yoff, w, h, ptr); 745 _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/); 746} 747 748static void 749nAdapter2DSubData_f(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint xoff, jint yoff, jint w, jint h, jfloatArray data) 750{ 751 jint len = _env->GetArrayLength(data); 752 LOG_API("nAdapter2DSubData_f, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)", 753 con, (RsAdapter2D)adapter, xoff, yoff, w, h, len); 754 jfloat *ptr = _env->GetFloatArrayElements(data, NULL); 755 rsAdapter2DSubData(con, (RsAdapter1D)adapter, xoff, yoff, w, h, ptr); 756 _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/); 757} 758 759static jint 760nAdapter2DCreate(JNIEnv *_env, jobject _this, RsContext con) 761{ 762 LOG_API("nAdapter2DCreate, con(%p)", con); 763 return (jint)rsAdapter2DCreate(con); 764} 765 766// ----------------------------------- 767 768static void 769nScriptBindAllocation(JNIEnv *_env, jobject _this, RsContext con, jint script, jint alloc, jint slot) 770{ 771 LOG_API("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", con, (RsScript)script, (RsAllocation)alloc, slot); 772 rsScriptBindAllocation(con, (RsScript)script, (RsAllocation)alloc, slot); 773} 774 775static void 776nScriptSetVarI(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jint val) 777{ 778 LOG_API("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", con, (void *)script, slot, val); 779 rsScriptSetVarI(con, (RsScript)script, slot, val); 780} 781 782static void 783nScriptSetVarF(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, float val) 784{ 785 LOG_API("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", con, (void *)script, slot, val); 786 rsScriptSetVarF(con, (RsScript)script, slot, val); 787} 788 789static void 790nScriptSetVarD(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, double val) 791{ 792 LOG_API("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", con, (void *)script, slot, val); 793 rsScriptSetVarD(con, (RsScript)script, slot, val); 794} 795 796static void 797nScriptSetVarV(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jbyteArray data) 798{ 799 LOG_API("nScriptSetVarV, con(%p), s(%p), slot(%i)", con, (void *)script, slot); 800 jint len = _env->GetArrayLength(data); 801 jbyte *ptr = _env->GetByteArrayElements(data, NULL); 802 rsScriptSetVarV(con, (RsScript)script, slot, ptr, len); 803 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT); 804} 805 806 807static void 808nScriptSetTimeZone(JNIEnv *_env, jobject _this, RsContext con, jint script, jbyteArray timeZone) 809{ 810 LOG_API("nScriptCSetTimeZone, con(%p), s(%p), timeZone(%s)", con, (void *)script, (const char *)timeZone); 811 812 jint length = _env->GetArrayLength(timeZone); 813 jbyte* timeZone_ptr; 814 timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0); 815 816 rsScriptSetTimeZone(con, (RsScript)script, (const char *)timeZone_ptr, length); 817 818 if (timeZone_ptr) { 819 _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0); 820 } 821} 822 823static void 824nScriptInvoke(JNIEnv *_env, jobject _this, RsContext con, jint obj, jint slot) 825{ 826 LOG_API("nScriptInvoke, con(%p), script(%p)", con, (void *)obj); 827 rsScriptInvoke(con, (RsScript)obj, slot); 828} 829 830static void 831nScriptInvokeV(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jbyteArray data) 832{ 833 LOG_API("nScriptInvokeV, con(%p), s(%p), slot(%i)", con, (void *)script, slot); 834 jint len = _env->GetArrayLength(data); 835 jbyte *ptr = _env->GetByteArrayElements(data, NULL); 836 rsScriptInvokeV(con, (RsScript)script, slot, ptr, len); 837 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT); 838} 839 840 841// ----------------------------------- 842 843static void 844nScriptCBegin(JNIEnv *_env, jobject _this, RsContext con) 845{ 846 LOG_API("nScriptCBegin, con(%p)", con); 847 rsScriptCBegin(con); 848} 849 850static void 851nScriptCSetScript(JNIEnv *_env, jobject _this, RsContext con, jbyteArray scriptRef, 852 jint offset, jint length) 853{ 854 LOG_API("!!! nScriptCSetScript, con(%p)", con); 855 jint _exception = 0; 856 jint remaining; 857 jbyte* script_base = 0; 858 jbyte* script_ptr; 859 if (!scriptRef) { 860 _exception = 1; 861 //_env->ThrowNew(IAEClass, "script == null"); 862 goto exit; 863 } 864 if (offset < 0) { 865 _exception = 1; 866 //_env->ThrowNew(IAEClass, "offset < 0"); 867 goto exit; 868 } 869 if (length < 0) { 870 _exception = 1; 871 //_env->ThrowNew(IAEClass, "length < 0"); 872 goto exit; 873 } 874 remaining = _env->GetArrayLength(scriptRef) - offset; 875 if (remaining < length) { 876 _exception = 1; 877 //_env->ThrowNew(IAEClass, "length > script.length - offset"); 878 goto exit; 879 } 880 script_base = (jbyte *) 881 _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0); 882 script_ptr = script_base + offset; 883 884 rsScriptCSetText(con, (const char *)script_ptr, length); 885 886exit: 887 if (script_base) { 888 _env->ReleasePrimitiveArrayCritical(scriptRef, script_base, 889 _exception ? JNI_ABORT: 0); 890 } 891} 892 893static jint 894nScriptCCreate(JNIEnv *_env, jobject _this, RsContext con) 895{ 896 LOG_API("nScriptCCreate, con(%p)", con); 897 return (jint)rsScriptCCreate(con); 898} 899 900// --------------------------------------------------------------------------- 901 902static void 903nProgramStoreBegin(JNIEnv *_env, jobject _this, RsContext con, jint in, jint out) 904{ 905 LOG_API("nProgramStoreBegin, con(%p), in(%p), out(%p)", con, (RsElement)in, (RsElement)out); 906 rsProgramStoreBegin(con, (RsElement)in, (RsElement)out); 907} 908 909static void 910nProgramStoreDepthFunc(JNIEnv *_env, jobject _this, RsContext con, jint func) 911{ 912 LOG_API("nProgramStoreDepthFunc, con(%p), func(%i)", con, func); 913 rsProgramStoreDepthFunc(con, (RsDepthFunc)func); 914} 915 916static void 917nProgramStoreDepthMask(JNIEnv *_env, jobject _this, RsContext con, jboolean enable) 918{ 919 LOG_API("nProgramStoreDepthMask, con(%p), enable(%i)", con, enable); 920 rsProgramStoreDepthMask(con, enable); 921} 922 923static void 924nProgramStoreColorMask(JNIEnv *_env, jobject _this, RsContext con, jboolean r, jboolean g, jboolean b, jboolean a) 925{ 926 LOG_API("nProgramStoreColorMask, con(%p), r(%i), g(%i), b(%i), a(%i)", con, r, g, b, a); 927 rsProgramStoreColorMask(con, r, g, b, a); 928} 929 930static void 931nProgramStoreBlendFunc(JNIEnv *_env, jobject _this, RsContext con, int src, int dst) 932{ 933 LOG_API("nProgramStoreBlendFunc, con(%p), src(%i), dst(%i)", con, src, dst); 934 rsProgramStoreBlendFunc(con, (RsBlendSrcFunc)src, (RsBlendDstFunc)dst); 935} 936 937static void 938nProgramStoreDither(JNIEnv *_env, jobject _this, RsContext con, jboolean enable) 939{ 940 LOG_API("nProgramStoreDither, con(%p), enable(%i)", con, enable); 941 rsProgramStoreDither(con, enable); 942} 943 944static jint 945nProgramStoreCreate(JNIEnv *_env, jobject _this, RsContext con) 946{ 947 LOG_API("nProgramStoreCreate, con(%p)", con); 948 return (jint)rsProgramStoreCreate(con); 949} 950 951// --------------------------------------------------------------------------- 952 953static void 954nProgramBindConstants(JNIEnv *_env, jobject _this, RsContext con, jint vpv, jint slot, jint a) 955{ 956 LOG_API("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", con, (RsProgramVertex)vpv, slot, (RsAllocation)a); 957 rsProgramBindConstants(con, (RsProgram)vpv, slot, (RsAllocation)a); 958} 959 960static void 961nProgramBindTexture(JNIEnv *_env, jobject _this, RsContext con, jint vpf, jint slot, jint a) 962{ 963 LOG_API("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsAllocation)a); 964 rsProgramBindTexture(con, (RsProgramFragment)vpf, slot, (RsAllocation)a); 965} 966 967static void 968nProgramBindSampler(JNIEnv *_env, jobject _this, RsContext con, jint vpf, jint slot, jint a) 969{ 970 LOG_API("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsSampler)a); 971 rsProgramBindSampler(con, (RsProgramFragment)vpf, slot, (RsSampler)a); 972} 973 974// --------------------------------------------------------------------------- 975 976static jint 977nProgramFragmentCreate(JNIEnv *_env, jobject _this, RsContext con, jstring shader, jintArray params) 978{ 979 const char* shaderUTF = _env->GetStringUTFChars(shader, NULL); 980 jint shaderLen = _env->GetStringUTFLength(shader); 981 jint *paramPtr = _env->GetIntArrayElements(params, NULL); 982 jint paramLen = _env->GetArrayLength(params); 983 984 LOG_API("nProgramFragmentCreate, con(%p), shaderLen(%i), paramLen(%i)", con, shaderLen, paramLen); 985 986 jint ret = (jint)rsProgramFragmentCreate(con, shaderUTF, shaderLen, (uint32_t *)paramPtr, paramLen); 987 _env->ReleaseStringUTFChars(shader, shaderUTF); 988 _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT); 989 return ret; 990} 991 992 993// --------------------------------------------------------------------------- 994 995static jint 996nProgramVertexCreate(JNIEnv *_env, jobject _this, RsContext con, jstring shader, jintArray params) 997{ 998 const char* shaderUTF = _env->GetStringUTFChars(shader, NULL); 999 jint shaderLen = _env->GetStringUTFLength(shader); 1000 jint *paramPtr = _env->GetIntArrayElements(params, NULL); 1001 jint paramLen = _env->GetArrayLength(params); 1002 1003 LOG_API("nProgramVertexCreate, con(%p), shaderLen(%i), paramLen(%i)", con, shaderLen, paramLen); 1004 1005 jint ret = (jint)rsProgramVertexCreate(con, shaderUTF, shaderLen, (uint32_t *)paramPtr, paramLen); 1006 _env->ReleaseStringUTFChars(shader, shaderUTF); 1007 _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT); 1008 return ret; 1009} 1010 1011// --------------------------------------------------------------------------- 1012 1013static jint 1014nProgramRasterCreate(JNIEnv *_env, jobject _this, RsContext con, jboolean pointSmooth, jboolean lineSmooth, jboolean pointSprite) 1015{ 1016 LOG_API("nProgramRasterCreate, con(%p), pointSmooth(%i), lineSmooth(%i), pointSprite(%i)", 1017 con, pointSmooth, lineSmooth, pointSprite); 1018 return (jint)rsProgramRasterCreate(con, pointSmooth, lineSmooth, pointSprite); 1019} 1020 1021static void 1022nProgramRasterSetLineWidth(JNIEnv *_env, jobject _this, RsContext con, jint vpr, jfloat v) 1023{ 1024 LOG_API("nProgramRasterSetLineWidth, con(%p), vpf(%p), value(%f)", con, (RsProgramRaster)vpr, v); 1025 rsProgramRasterSetLineWidth(con, (RsProgramRaster)vpr, v); 1026} 1027 1028static void 1029nProgramRasterSetCullMode(JNIEnv *_env, jobject _this, RsContext con, jint vpr, jint v) 1030{ 1031 LOG_API("nProgramRasterSetCullMode, con(%p), vpf(%p), value(%i)", con, (RsProgramRaster)vpr, v); 1032 rsProgramRasterSetCullMode(con, (RsProgramRaster)vpr, (RsCullMode)v); 1033} 1034 1035 1036// --------------------------------------------------------------------------- 1037 1038static void 1039nContextBindRootScript(JNIEnv *_env, jobject _this, RsContext con, jint script) 1040{ 1041 LOG_API("nContextBindRootScript, con(%p), script(%p)", con, (RsScript)script); 1042 rsContextBindRootScript(con, (RsScript)script); 1043} 1044 1045static void 1046nContextBindProgramStore(JNIEnv *_env, jobject _this, RsContext con, jint pfs) 1047{ 1048 LOG_API("nContextBindProgramStore, con(%p), pfs(%p)", con, (RsProgramStore)pfs); 1049 rsContextBindProgramStore(con, (RsProgramStore)pfs); 1050} 1051 1052static void 1053nContextBindProgramFragment(JNIEnv *_env, jobject _this, RsContext con, jint pf) 1054{ 1055 LOG_API("nContextBindProgramFragment, con(%p), pf(%p)", con, (RsProgramFragment)pf); 1056 rsContextBindProgramFragment(con, (RsProgramFragment)pf); 1057} 1058 1059static void 1060nContextBindProgramVertex(JNIEnv *_env, jobject _this, RsContext con, jint pf) 1061{ 1062 LOG_API("nContextBindProgramVertex, con(%p), pf(%p)", con, (RsProgramVertex)pf); 1063 rsContextBindProgramVertex(con, (RsProgramVertex)pf); 1064} 1065 1066static void 1067nContextBindProgramRaster(JNIEnv *_env, jobject _this, RsContext con, jint pf) 1068{ 1069 LOG_API("nContextBindProgramRaster, con(%p), pf(%p)", con, (RsProgramRaster)pf); 1070 rsContextBindProgramRaster(con, (RsProgramRaster)pf); 1071} 1072 1073 1074// --------------------------------------------------------------------------- 1075 1076static void 1077nSamplerBegin(JNIEnv *_env, jobject _this, RsContext con) 1078{ 1079 LOG_API("nSamplerBegin, con(%p)", con); 1080 rsSamplerBegin(con); 1081} 1082 1083static void 1084nSamplerSet(JNIEnv *_env, jobject _this, RsContext con, jint p, jint v) 1085{ 1086 LOG_API("nSamplerSet, con(%p), param(%i), value(%i)", con, p, v); 1087 rsSamplerSet(con, (RsSamplerParam)p, (RsSamplerValue)v); 1088} 1089 1090static void 1091nSamplerSet2(JNIEnv *_env, jobject _this, RsContext con, jint p, jfloat v) 1092{ 1093 LOG_API("nSamplerSet2, con(%p), param(%i), value(%f)", con, p, v); 1094 rsSamplerSet2(con, (RsSamplerParam)p, v); 1095} 1096 1097static jint 1098nSamplerCreate(JNIEnv *_env, jobject _this, RsContext con) 1099{ 1100 LOG_API("nSamplerCreate, con(%p)", con); 1101 return (jint)rsSamplerCreate(con); 1102} 1103 1104// --------------------------------------------------------------------------- 1105 1106static jint 1107nMeshCreate(JNIEnv *_env, jobject _this, RsContext con, jint vtxCount, jint idxCount) 1108{ 1109 LOG_API("nMeshCreate, con(%p), vtxCount(%i), idxCount(%i)", con, vtxCount, idxCount); 1110 int id = (int)rsMeshCreate(con, vtxCount, idxCount); 1111 return id; 1112} 1113 1114static void 1115nMeshBindVertex(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jint alloc, jint slot) 1116{ 1117 LOG_API("nMeshBindVertex, con(%p), Mesh(%p), Alloc(%p), slot(%i)", con, (RsMesh)mesh, (RsAllocation)alloc, slot); 1118 rsMeshBindVertex(con, (RsMesh)mesh, (RsAllocation)alloc, slot); 1119} 1120 1121static void 1122nMeshBindIndex(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jint alloc, jint primID, jint slot) 1123{ 1124 LOG_API("nMeshBindIndex, con(%p), Mesh(%p), Alloc(%p)", con, (RsMesh)mesh, (RsAllocation)alloc); 1125 rsMeshBindIndex(con, (RsMesh)mesh, (RsAllocation)alloc, primID, slot); 1126} 1127 1128static jint 1129nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, RsContext con, jint mesh) 1130{ 1131 LOG_API("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", con, (RsMesh)mesh); 1132 jint vtxCount = 0; 1133 rsMeshGetVertexBufferCount(con, (RsMesh)mesh, &vtxCount); 1134 return vtxCount; 1135} 1136 1137static jint 1138nMeshGetIndexCount(JNIEnv *_env, jobject _this, RsContext con, jint mesh) 1139{ 1140 LOG_API("nMeshGetIndexCount, con(%p), Mesh(%p)", con, (RsMesh)mesh); 1141 jint idxCount = 0; 1142 rsMeshGetIndexCount(con, (RsMesh)mesh, &idxCount); 1143 return idxCount; 1144} 1145 1146static void 1147nMeshGetVertices(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jintArray _ids, int numVtxIDs) 1148{ 1149 LOG_API("nMeshGetVertices, con(%p), Mesh(%p)", con, (RsMesh)mesh); 1150 1151 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation)); 1152 rsMeshGetVertices(con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs); 1153 1154 for(jint i = 0; i < numVtxIDs; i ++) { 1155 _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&allocs[i]); 1156 } 1157 1158 free(allocs); 1159} 1160 1161static void 1162nMeshGetIndices(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jintArray _idxIds, jintArray _primitives, int numIndices) 1163{ 1164 LOG_API("nMeshGetVertices, con(%p), Mesh(%p)", con, (RsMesh)mesh); 1165 1166 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation)); 1167 uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t)); 1168 1169 rsMeshGetIndices(con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices); 1170 1171 for(jint i = 0; i < numIndices; i ++) { 1172 _env->SetIntArrayRegion(_idxIds, i, 1, (const jint*)&allocs[i]); 1173 _env->SetIntArrayRegion(_primitives, i, 1, (const jint*)&prims[i]); 1174 } 1175 1176 free(allocs); 1177 free(prims); 1178} 1179 1180// --------------------------------------------------------------------------- 1181 1182 1183static const char *classPathName = "android/renderscript/RenderScript"; 1184 1185static JNINativeMethod methods[] = { 1186{"_nInit", "()V", (void*)_nInit }, 1187{"nInitElements", "(IIII)V", (void*)nInitElements }, 1188 1189{"nDeviceCreate", "()I", (void*)nDeviceCreate }, 1190{"nDeviceDestroy", "(I)V", (void*)nDeviceDestroy }, 1191{"nDeviceSetConfig", "(III)V", (void*)nDeviceSetConfig }, 1192{"nContextGetMessage", "(I[IZ)I", (void*)nContextGetMessage }, 1193{"nContextInitToClient", "(I)V", (void*)nContextInitToClient }, 1194{"nContextDeinitToClient", "(I)V", (void*)nContextDeinitToClient }, 1195 1196 1197// All methods below are thread protected in java. 1198{"rsnContextCreate", "(II)I", (void*)nContextCreate }, 1199{"rsnContextCreateGL", "(IIZ)I", (void*)nContextCreateGL }, 1200{"rsnContextFinish", "(I)V", (void*)nContextFinish }, 1201{"rsnContextSetPriority", "(II)V", (void*)nContextSetPriority }, 1202{"rsnContextSetSurface", "(IIILandroid/view/Surface;)V", (void*)nContextSetSurface }, 1203{"rsnContextDestroy", "(I)V", (void*)nContextDestroy }, 1204{"rsnContextDump", "(II)V", (void*)nContextDump }, 1205{"rsnContextPause", "(I)V", (void*)nContextPause }, 1206{"rsnContextResume", "(I)V", (void*)nContextResume }, 1207{"rsnAssignName", "(II[B)V", (void*)nAssignName }, 1208{"rsnGetName", "(II)Ljava/lang/String;", (void*)nGetName }, 1209{"rsnObjDestroy", "(II)V", (void*)nObjDestroy }, 1210 1211{"rsnFileOpen", "(I[B)I", (void*)nFileOpen }, 1212{"rsnFileA3DCreateFromAssetStream", "(II)I", (void*)nFileA3DCreateFromAssetStream }, 1213{"rsnFileA3DGetNumIndexEntries", "(II)I", (void*)nFileA3DGetNumIndexEntries }, 1214{"rsnFileA3DGetIndexEntries", "(III[I[Ljava/lang/String;)V", (void*)nFileA3DGetIndexEntries }, 1215{"rsnFileA3DGetEntryByIndex", "(III)I", (void*)nFileA3DGetEntryByIndex }, 1216 1217{"rsnFontCreateFromFile", "(ILjava/lang/String;II)I", (void*)nFontCreateFromFile }, 1218 1219{"rsnElementCreate", "(IIIZI)I", (void*)nElementCreate }, 1220{"rsnElementCreate2", "(I[I[Ljava/lang/String;[I)I", (void*)nElementCreate2 }, 1221{"rsnElementGetNativeData", "(II[I)V", (void*)nElementGetNativeData }, 1222{"rsnElementGetSubElements", "(II[I[Ljava/lang/String;)V", (void*)nElementGetSubElements }, 1223 1224{"rsnTypeBegin", "(II)V", (void*)nTypeBegin }, 1225{"rsnTypeAdd", "(III)V", (void*)nTypeAdd }, 1226{"rsnTypeCreate", "(I)I", (void*)nTypeCreate }, 1227{"rsnTypeGetNativeData", "(II[I)V", (void*)nTypeGetNativeData }, 1228 1229{"rsnAllocationCreateTyped", "(II)I", (void*)nAllocationCreateTyped }, 1230{"rsnAllocationCreateFromBitmap", "(IIZLandroid/graphics/Bitmap;)I", (void*)nAllocationCreateFromBitmap }, 1231{"rsnAllocationCreateBitmapRef", "(IILandroid/graphics/Bitmap;)I", (void*)nAllocationCreateBitmapRef }, 1232{"rsnAllocationCreateFromAssetStream","(IIZI)I", (void*)nAllocationCreateFromAssetStream }, 1233{"rsnAllocationUploadToTexture", "(IIZI)V", (void*)nAllocationUploadToTexture }, 1234{"rsnAllocationUploadToBufferObject","(II)V", (void*)nAllocationUploadToBufferObject }, 1235{"rsnAllocationSubData1D", "(IIII[II)V", (void*)nAllocationSubData1D_i }, 1236{"rsnAllocationSubData1D", "(IIII[SI)V", (void*)nAllocationSubData1D_s }, 1237{"rsnAllocationSubData1D", "(IIII[BI)V", (void*)nAllocationSubData1D_b }, 1238{"rsnAllocationSubData1D", "(IIII[FI)V", (void*)nAllocationSubData1D_f }, 1239{"rsnAllocationSubElementData1D", "(IIII[BI)V", (void*)nAllocationSubElementData1D }, 1240{"rsnAllocationSubData2D", "(IIIIII[II)V", (void*)nAllocationSubData2D_i }, 1241{"rsnAllocationSubData2D", "(IIIIII[FI)V", (void*)nAllocationSubData2D_f }, 1242{"rsnAllocationRead", "(II[I)V", (void*)nAllocationRead_i }, 1243{"rsnAllocationRead", "(II[F)V", (void*)nAllocationRead_f }, 1244{"rsnAllocationGetType", "(II)I", (void*)nAllocationGetType}, 1245{"rsnAllocationResize1D", "(III)V", (void*)nAllocationResize1D }, 1246{"rsnAllocationResize2D", "(IIII)V", (void*)nAllocationResize2D }, 1247 1248{"rsnAdapter1DBindAllocation", "(III)V", (void*)nAdapter1DBindAllocation }, 1249{"rsnAdapter1DSetConstraint", "(IIII)V", (void*)nAdapter1DSetConstraint }, 1250{"rsnAdapter1DData", "(II[I)V", (void*)nAdapter1DData_i }, 1251{"rsnAdapter1DData", "(II[F)V", (void*)nAdapter1DData_f }, 1252{"rsnAdapter1DSubData", "(IIII[I)V", (void*)nAdapter1DSubData_i }, 1253{"rsnAdapter1DSubData", "(IIII[F)V", (void*)nAdapter1DSubData_f }, 1254{"rsnAdapter1DCreate", "(I)I", (void*)nAdapter1DCreate }, 1255 1256{"rsnAdapter2DBindAllocation", "(III)V", (void*)nAdapter2DBindAllocation }, 1257{"rsnAdapter2DSetConstraint", "(IIII)V", (void*)nAdapter2DSetConstraint }, 1258{"rsnAdapter2DData", "(II[I)V", (void*)nAdapter2DData_i }, 1259{"rsnAdapter2DData", "(II[F)V", (void*)nAdapter2DData_f }, 1260{"rsnAdapter2DSubData", "(IIIIII[I)V", (void*)nAdapter2DSubData_i }, 1261{"rsnAdapter2DSubData", "(IIIIII[F)V", (void*)nAdapter2DSubData_f }, 1262{"rsnAdapter2DCreate", "(I)I", (void*)nAdapter2DCreate }, 1263 1264{"rsnScriptBindAllocation", "(IIII)V", (void*)nScriptBindAllocation }, 1265{"rsnScriptSetTimeZone", "(II[B)V", (void*)nScriptSetTimeZone }, 1266{"rsnScriptInvoke", "(III)V", (void*)nScriptInvoke }, 1267{"rsnScriptInvokeV", "(III[B)V", (void*)nScriptInvokeV }, 1268{"rsnScriptSetVarI", "(IIII)V", (void*)nScriptSetVarI }, 1269{"rsnScriptSetVarF", "(IIIF)V", (void*)nScriptSetVarF }, 1270{"rsnScriptSetVarD", "(IIID)V", (void*)nScriptSetVarD }, 1271{"rsnScriptSetVarV", "(III[B)V", (void*)nScriptSetVarV }, 1272 1273{"rsnScriptCBegin", "(I)V", (void*)nScriptCBegin }, 1274{"rsnScriptCSetScript", "(I[BII)V", (void*)nScriptCSetScript }, 1275{"rsnScriptCCreate", "(I)I", (void*)nScriptCCreate }, 1276 1277{"rsnProgramStoreBegin", "(III)V", (void*)nProgramStoreBegin }, 1278{"rsnProgramStoreDepthFunc", "(II)V", (void*)nProgramStoreDepthFunc }, 1279{"rsnProgramStoreDepthMask", "(IZ)V", (void*)nProgramStoreDepthMask }, 1280{"rsnProgramStoreColorMask", "(IZZZZ)V", (void*)nProgramStoreColorMask }, 1281{"rsnProgramStoreBlendFunc", "(III)V", (void*)nProgramStoreBlendFunc }, 1282{"rsnProgramStoreDither", "(IZ)V", (void*)nProgramStoreDither }, 1283{"rsnProgramStoreCreate", "(I)I", (void*)nProgramStoreCreate }, 1284 1285{"rsnProgramBindConstants", "(IIII)V", (void*)nProgramBindConstants }, 1286{"rsnProgramBindTexture", "(IIII)V", (void*)nProgramBindTexture }, 1287{"rsnProgramBindSampler", "(IIII)V", (void*)nProgramBindSampler }, 1288 1289{"rsnProgramFragmentCreate", "(ILjava/lang/String;[I)I", (void*)nProgramFragmentCreate }, 1290 1291{"rsnProgramRasterCreate", "(IZZZ)I", (void*)nProgramRasterCreate }, 1292{"rsnProgramRasterSetLineWidth", "(IIF)V", (void*)nProgramRasterSetLineWidth }, 1293{"rsnProgramRasterSetCullMode", "(III)V", (void*)nProgramRasterSetCullMode }, 1294 1295{"rsnProgramVertexCreate", "(ILjava/lang/String;[I)I", (void*)nProgramVertexCreate }, 1296 1297{"rsnContextBindRootScript", "(II)V", (void*)nContextBindRootScript }, 1298{"rsnContextBindProgramStore", "(II)V", (void*)nContextBindProgramStore }, 1299{"rsnContextBindProgramFragment", "(II)V", (void*)nContextBindProgramFragment }, 1300{"rsnContextBindProgramVertex", "(II)V", (void*)nContextBindProgramVertex }, 1301{"rsnContextBindProgramRaster", "(II)V", (void*)nContextBindProgramRaster }, 1302 1303{"rsnSamplerBegin", "(I)V", (void*)nSamplerBegin }, 1304{"rsnSamplerSet", "(III)V", (void*)nSamplerSet }, 1305{"rsnSamplerSet2", "(IIF)V", (void*)nSamplerSet2 }, 1306{"rsnSamplerCreate", "(I)I", (void*)nSamplerCreate }, 1307 1308{"rsnMeshCreate", "(III)I", (void*)nMeshCreate }, 1309{"rsnMeshBindVertex", "(IIII)V", (void*)nMeshBindVertex }, 1310{"rsnMeshBindIndex", "(IIIII)V", (void*)nMeshBindIndex }, 1311 1312{"rsnMeshGetVertexBufferCount", "(II)I", (void*)nMeshGetVertexBufferCount }, 1313{"rsnMeshGetIndexCount", "(II)I", (void*)nMeshGetIndexCount }, 1314{"rsnMeshGetVertices", "(II[II)V", (void*)nMeshGetVertices }, 1315{"rsnMeshGetIndices", "(II[I[II)V", (void*)nMeshGetIndices }, 1316 1317}; 1318 1319static int registerFuncs(JNIEnv *_env) 1320{ 1321 return android::AndroidRuntime::registerNativeMethods( 1322 _env, classPathName, methods, NELEM(methods)); 1323} 1324 1325// --------------------------------------------------------------------------- 1326 1327jint JNI_OnLoad(JavaVM* vm, void* reserved) 1328{ 1329 JNIEnv* env = NULL; 1330 jint result = -1; 1331 1332 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) { 1333 LOGE("ERROR: GetEnv failed\n"); 1334 goto bail; 1335 } 1336 assert(env != NULL); 1337 1338 if (registerFuncs(env) < 0) { 1339 LOGE("ERROR: MediaPlayer native registration failed\n"); 1340 goto bail; 1341 } 1342 1343 /* success -- return valid version number */ 1344 result = JNI_VERSION_1_4; 1345 1346bail: 1347 return result; 1348} 1349 1350