android_renderscript_RenderScript.cpp revision 69335f8eaf555a8dc65ff04c5b49d1fa31e35f59
1/* 2 * Copyright (C) 2011-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#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 <android/bitmap.h> 25#include <android/log.h> 26 27#include <rsEnv.h> 28#include "rsDispatch.h" 29#include <dlfcn.h> 30 31//#define LOG_API ALOG 32#define LOG_API(...) 33 34#define NELEM(m) (sizeof(m) / sizeof((m)[0])) 35 36template <typename... T> 37void UNUSED(T... t) {} 38#define PER_ARRAY_TYPE(flag, fnc, readonly, ...) { \ 39 jint len = 0; \ 40 void *ptr = nullptr; \ 41 void *srcPtr = nullptr; \ 42 size_t typeBytes = 0; \ 43 jint relFlag = 0; \ 44 if (readonly) { \ 45 /* The on-release mode should only be JNI_ABORT for read-only accesses. */ \ 46 /* readonly = true, also indicates we are copying to the allocation . */ \ 47 relFlag = JNI_ABORT; \ 48 } \ 49 switch(dataType) { \ 50 case RS_TYPE_FLOAT_32: \ 51 len = _env->GetArrayLength((jfloatArray)data); \ 52 ptr = _env->GetFloatArrayElements((jfloatArray)data, flag); \ 53 typeBytes = 4; \ 54 if (usePadding) { \ 55 srcPtr = ptr; \ 56 len = len / 3 * 4; \ 57 if (count == 0) { \ 58 count = len / 4; \ 59 } \ 60 ptr = malloc (len * typeBytes); \ 61 if (readonly) { \ 62 copyWithPadding(ptr, srcPtr, mSize, count); \ 63 fnc(__VA_ARGS__); \ 64 } else { \ 65 fnc(__VA_ARGS__); \ 66 copyWithUnPadding(srcPtr, ptr, mSize, count); \ 67 } \ 68 free(ptr); \ 69 ptr = srcPtr; \ 70 } else { \ 71 fnc(__VA_ARGS__); \ 72 } \ 73 _env->ReleaseFloatArrayElements((jfloatArray)data, (jfloat *)ptr, relFlag); \ 74 return; \ 75 case RS_TYPE_FLOAT_64: \ 76 len = _env->GetArrayLength((jdoubleArray)data); \ 77 ptr = _env->GetDoubleArrayElements((jdoubleArray)data, flag); \ 78 typeBytes = 8; \ 79 if (usePadding) { \ 80 srcPtr = ptr; \ 81 len = len / 3 * 4; \ 82 if (count == 0) { \ 83 count = len / 4; \ 84 } \ 85 ptr = malloc (len * typeBytes); \ 86 if (readonly) { \ 87 copyWithPadding(ptr, srcPtr, mSize, count); \ 88 fnc(__VA_ARGS__); \ 89 } else { \ 90 fnc(__VA_ARGS__); \ 91 copyWithUnPadding(srcPtr, ptr, mSize, count); \ 92 } \ 93 free(ptr); \ 94 ptr = srcPtr; \ 95 } else { \ 96 fnc(__VA_ARGS__); \ 97 } \ 98 _env->ReleaseDoubleArrayElements((jdoubleArray)data, (jdouble *)ptr, relFlag); \ 99 return; \ 100 case RS_TYPE_SIGNED_8: \ 101 case RS_TYPE_UNSIGNED_8: \ 102 len = _env->GetArrayLength((jbyteArray)data); \ 103 ptr = _env->GetByteArrayElements((jbyteArray)data, flag); \ 104 typeBytes = 1; \ 105 if (usePadding) { \ 106 srcPtr = ptr; \ 107 len = len / 3 * 4; \ 108 if (count == 0) { \ 109 count = len / 4; \ 110 } \ 111 ptr = malloc (len * typeBytes); \ 112 if (readonly) { \ 113 copyWithPadding(ptr, srcPtr, mSize, count); \ 114 fnc(__VA_ARGS__); \ 115 } else { \ 116 fnc(__VA_ARGS__); \ 117 copyWithUnPadding(srcPtr, ptr, mSize, count); \ 118 } \ 119 free(ptr); \ 120 ptr = srcPtr; \ 121 } else { \ 122 fnc(__VA_ARGS__); \ 123 } \ 124 _env->ReleaseByteArrayElements((jbyteArray)data, (jbyte*)ptr, relFlag); \ 125 return; \ 126 case RS_TYPE_SIGNED_16: \ 127 case RS_TYPE_UNSIGNED_16: \ 128 len = _env->GetArrayLength((jshortArray)data); \ 129 ptr = _env->GetShortArrayElements((jshortArray)data, flag); \ 130 typeBytes = 2; \ 131 if (usePadding) { \ 132 srcPtr = ptr; \ 133 len = len / 3 * 4; \ 134 if (count == 0) { \ 135 count = len / 4; \ 136 } \ 137 ptr = malloc (len * typeBytes); \ 138 if (readonly) { \ 139 copyWithPadding(ptr, srcPtr, mSize, count); \ 140 fnc(__VA_ARGS__); \ 141 } else { \ 142 fnc(__VA_ARGS__); \ 143 copyWithUnPadding(srcPtr, ptr, mSize, count); \ 144 } \ 145 free(ptr); \ 146 ptr = srcPtr; \ 147 } else { \ 148 fnc(__VA_ARGS__); \ 149 } \ 150 _env->ReleaseShortArrayElements((jshortArray)data, (jshort *)ptr, relFlag); \ 151 return; \ 152 case RS_TYPE_SIGNED_32: \ 153 case RS_TYPE_UNSIGNED_32: \ 154 len = _env->GetArrayLength((jintArray)data); \ 155 ptr = _env->GetIntArrayElements((jintArray)data, flag); \ 156 typeBytes = 4; \ 157 if (usePadding) { \ 158 srcPtr = ptr; \ 159 len = len / 3 * 4; \ 160 if (count == 0) { \ 161 count = len / 4; \ 162 } \ 163 ptr = malloc (len * typeBytes); \ 164 if (readonly) { \ 165 copyWithPadding(ptr, srcPtr, mSize, count); \ 166 fnc(__VA_ARGS__); \ 167 } else { \ 168 fnc(__VA_ARGS__); \ 169 copyWithUnPadding(srcPtr, ptr, mSize, count); \ 170 } \ 171 free(ptr); \ 172 ptr = srcPtr; \ 173 } else { \ 174 fnc(__VA_ARGS__); \ 175 } \ 176 _env->ReleaseIntArrayElements((jintArray)data, (jint *)ptr, relFlag); \ 177 return; \ 178 case RS_TYPE_SIGNED_64: \ 179 case RS_TYPE_UNSIGNED_64: \ 180 len = _env->GetArrayLength((jlongArray)data); \ 181 ptr = _env->GetLongArrayElements((jlongArray)data, flag); \ 182 typeBytes = 8; \ 183 if (usePadding) { \ 184 srcPtr = ptr; \ 185 len = len / 3 * 4; \ 186 if (count == 0) { \ 187 count = len / 4; \ 188 } \ 189 ptr = malloc (len * typeBytes); \ 190 if (readonly) { \ 191 copyWithPadding(ptr, srcPtr, mSize, count); \ 192 fnc(__VA_ARGS__); \ 193 } else { \ 194 fnc(__VA_ARGS__); \ 195 copyWithUnPadding(srcPtr, ptr, mSize, count); \ 196 } \ 197 free(ptr); \ 198 ptr = srcPtr; \ 199 } else { \ 200 fnc(__VA_ARGS__); \ 201 } \ 202 _env->ReleaseLongArrayElements((jlongArray)data, (jlong *)ptr, relFlag); \ 203 return; \ 204 default: \ 205 break; \ 206 } \ 207 UNUSED(len, ptr, srcPtr, typeBytes, relFlag); \ 208} 209 210 211class AutoJavaStringToUTF8 { 212public: 213 AutoJavaStringToUTF8(JNIEnv* env, jstring str) : fEnv(env), fJStr(str) { 214 fCStr = env->GetStringUTFChars(str, NULL); 215 fLength = env->GetStringUTFLength(str); 216 } 217 ~AutoJavaStringToUTF8() { 218 fEnv->ReleaseStringUTFChars(fJStr, fCStr); 219 } 220 const char* c_str() const { return fCStr; } 221 jsize length() const { return fLength; } 222 223private: 224 JNIEnv* fEnv; 225 jstring fJStr; 226 const char* fCStr; 227 jsize fLength; 228}; 229 230class AutoJavaStringArrayToUTF8 { 231public: 232 AutoJavaStringArrayToUTF8(JNIEnv* env, jobjectArray strings, jsize stringsLength) 233 : mEnv(env), mStrings(strings), mStringsLength(stringsLength) { 234 mCStrings = NULL; 235 mSizeArray = NULL; 236 if (stringsLength > 0) { 237 mCStrings = (const char **)calloc(stringsLength, sizeof(char *)); 238 mSizeArray = (size_t*)calloc(stringsLength, sizeof(size_t)); 239 for (jsize ct = 0; ct < stringsLength; ct ++) { 240 jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct); 241 mCStrings[ct] = mEnv->GetStringUTFChars(s, NULL); 242 mSizeArray[ct] = mEnv->GetStringUTFLength(s); 243 } 244 } 245 } 246 ~AutoJavaStringArrayToUTF8() { 247 for (jsize ct=0; ct < mStringsLength; ct++) { 248 jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct); 249 mEnv->ReleaseStringUTFChars(s, mCStrings[ct]); 250 } 251 free(mCStrings); 252 free(mSizeArray); 253 } 254 const char **c_str() const { return mCStrings; } 255 size_t *c_str_len() const { return mSizeArray; } 256 jsize length() const { return mStringsLength; } 257 258private: 259 JNIEnv *mEnv; 260 jobjectArray mStrings; 261 const char **mCStrings; 262 size_t *mSizeArray; 263 jsize mStringsLength; 264}; 265 266 267// --------------------------------------------------------------------------- 268static dispatchTable dispatchTab; 269// Incremental Support lib 270static dispatchTable dispatchTabInc; 271 272static jboolean nLoadSO(JNIEnv *_env, jobject _this, jboolean useNative, jint deviceApi) { 273 void* handle = NULL; 274 if (useNative) { 275 handle = dlopen("libRS.so", RTLD_LAZY | RTLD_LOCAL); 276 } else { 277 handle = dlopen("libRSSupport.so", RTLD_LAZY | RTLD_LOCAL); 278 } 279 if (handle == NULL) { 280 LOG_API("couldn't dlopen %s, %s", filename, dlerror()); 281 return false; 282 } 283 284 if (loadSymbols(handle, dispatchTab, deviceApi) == false) { 285 LOG_API("%s init failed!", filename); 286 return false; 287 } 288 LOG_API("Successfully loaded %s", filename); 289 return true; 290} 291 292static ioSuppDT ioDispatch; 293static jboolean nLoadIOSO(JNIEnv *_env, jobject _this) { 294 void* handleIO = NULL; 295 handleIO = dlopen("libRSSupportIO.so", RTLD_LAZY | RTLD_LOCAL); 296 if (handleIO == NULL) { 297 LOG_API("Couldn't load libRSSupportIO.so"); 298 return false; 299 } 300 if (loadIOSuppSyms(handleIO, ioDispatch) == false) { 301 LOG_API("libRSSupportIO init failed!"); 302 return false; 303 } 304 return true; 305} 306 307// --------------------------------------------------------------------------- 308 309static void copyWithPadding(void* ptr, void* srcPtr, int mSize, int count) { 310 int sizeBytesPad = mSize * 4; 311 int sizeBytes = mSize * 3; 312 uint8_t *dst = static_cast<uint8_t *>(ptr); 313 uint8_t *src = static_cast<uint8_t *>(srcPtr); 314 for (int i = 0; i < count; i++) { 315 memcpy(dst, src, sizeBytes); 316 dst += sizeBytesPad; 317 src += sizeBytes; 318 } 319} 320 321static void copyWithUnPadding(void* ptr, void* srcPtr, int mSize, int count) { 322 int sizeBytesPad = mSize * 4; 323 int sizeBytes = mSize * 3; 324 uint8_t *dst = static_cast<uint8_t *>(ptr); 325 uint8_t *src = static_cast<uint8_t *>(srcPtr); 326 for (int i = 0; i < count; i++) { 327 memcpy(dst, src, sizeBytes); 328 dst += sizeBytes; 329 src += sizeBytesPad; 330 } 331} 332 333 334// --------------------------------------------------------------------------- 335 336static void 337nContextFinish(JNIEnv *_env, jobject _this, jlong con) 338{ 339 LOG_API("nContextFinish, con(%p)", (RsContext)con); 340 dispatchTab.ContextFinish((RsContext)con); 341} 342 343static jlong 344nClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong kernelID, 345 jlong returnValue, jlongArray fieldIDArray, 346 jlongArray valueArray, jintArray sizeArray, 347 jlongArray depClosureArray, jlongArray depFieldIDArray) { 348 jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr); 349 jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray); 350 RsScriptFieldID* fieldIDs = 351 (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * fieldIDs_length); 352 for (int i = 0; i< fieldIDs_length; i++) { 353 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i]; 354 } 355 356 jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr); 357 jsize values_length = _env->GetArrayLength(valueArray); 358 uintptr_t* values = (uintptr_t*)alloca(sizeof(uintptr_t) * values_length); 359 for (int i = 0; i < values_length; i++) { 360 values[i] = (uintptr_t)jValues[i]; 361 } 362 363 jint* sizes = _env->GetIntArrayElements(sizeArray, nullptr); 364 jsize sizes_length = _env->GetArrayLength(sizeArray); 365 366 jlong* jDepClosures = 367 _env->GetLongArrayElements(depClosureArray, nullptr); 368 jsize depClosures_length = _env->GetArrayLength(depClosureArray); 369 RsClosure* depClosures = 370 (RsClosure*)alloca(sizeof(RsClosure) * depClosures_length); 371 for (int i = 0; i < depClosures_length; i++) { 372 depClosures[i] = (RsClosure)jDepClosures[i]; 373 } 374 375 jlong* jDepFieldIDs = 376 _env->GetLongArrayElements(depFieldIDArray, nullptr); 377 jsize depFieldIDs_length = _env->GetArrayLength(depFieldIDArray); 378 RsScriptFieldID* depFieldIDs = 379 (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * depFieldIDs_length); 380 for (int i = 0; i < depClosures_length; i++) { 381 depFieldIDs[i] = (RsClosure)jDepFieldIDs[i]; 382 } 383 384 return (jlong)(uintptr_t)dispatchTab.ClosureCreate( 385 (RsContext)con, (RsScriptKernelID)kernelID, (RsAllocation)returnValue, 386 fieldIDs, (size_t)fieldIDs_length, values, (size_t)values_length, 387 (size_t*)sizes, (size_t)sizes_length, 388 depClosures, (size_t)depClosures_length, 389 depFieldIDs, (size_t)depFieldIDs_length); 390} 391 392static jlong 393nInvokeClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong invokeID, 394 jbyteArray paramArray, jlongArray fieldIDArray, jlongArray valueArray, 395 jintArray sizeArray) { 396 jbyte* jParams = _env->GetByteArrayElements(paramArray, nullptr); 397 jsize jParamLength = _env->GetArrayLength(paramArray); 398 399 jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr); 400 jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray); 401 RsScriptFieldID* fieldIDs = 402 (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * fieldIDs_length); 403 for (int i = 0; i< fieldIDs_length; i++) { 404 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i]; 405 } 406 407 jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr); 408 jsize values_length = _env->GetArrayLength(valueArray); 409 uintptr_t* values = (uintptr_t*)alloca(sizeof(uintptr_t) * values_length); 410 for (int i = 0; i < values_length; i++) { 411 values[i] = (uintptr_t)jValues[i]; 412 } 413 414 jint* sizes = _env->GetIntArrayElements(sizeArray, nullptr); 415 jsize sizes_length = _env->GetArrayLength(sizeArray); 416 417 return (jlong)(uintptr_t)dispatchTab.InvokeClosureCreate( 418 (RsContext)con, (RsScriptInvokeID)invokeID, jParams, jParamLength, 419 fieldIDs, (size_t)fieldIDs_length, values, (size_t)values_length, 420 (size_t*)sizes, (size_t)sizes_length); 421} 422 423static void 424nClosureSetArg(JNIEnv *_env, jobject _this, jlong con, jlong closureID, 425 jint index, jlong value, jint size) { 426 dispatchTab.ClosureSetArg((RsContext)con, (RsClosure)closureID, (uint32_t)index, 427 (uintptr_t)value, (size_t)size); 428} 429 430static void 431nClosureSetGlobal(JNIEnv *_env, jobject _this, jlong con, jlong closureID, 432 jlong fieldID, jlong value, jint size) { 433 dispatchTab.ClosureSetGlobal((RsContext)con, (RsClosure)closureID, 434 (RsScriptFieldID)fieldID, (uintptr_t)value, (size_t)size); 435} 436 437static long 438nScriptGroup2Create(JNIEnv *_env, jobject _this, jlong con, 439 jstring cacheDir, jlongArray closureArray) { 440 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir); 441 442 jlong* jClosures = _env->GetLongArrayElements(closureArray, nullptr); 443 jsize numClosures = _env->GetArrayLength(closureArray); 444 RsClosure* closures = (RsClosure*)alloca(sizeof(RsClosure) * numClosures); 445 for (int i = 0; i < numClosures; i++) { 446 closures[i] = (RsClosure)jClosures[i]; 447 } 448 449 return (jlong)(uintptr_t)dispatchTab.ScriptGroup2Create( 450 (RsContext)con, cacheDirUTF.c_str(), cacheDirUTF.length(), 451 closures, numClosures); 452} 453 454static void 455nScriptGroup2Execute(JNIEnv *_env, jobject _this, jlong con, jlong groupID) { 456 dispatchTab.ScriptGroupExecute((RsContext)con, (RsScriptGroup2)groupID); 457} 458 459static void 460nObjDestroy(JNIEnv *_env, jobject _this, jlong con, jlong obj) 461{ 462 LOG_API("nObjDestroy, con(%p) obj(%p)", (RsContext)con, (void *)obj); 463 dispatchTab.ObjDestroy((RsContext)con, (void *)obj); 464} 465 466// --------------------------------------------------------------------------- 467static jlong 468nDeviceCreate(JNIEnv *_env, jobject _this) 469{ 470 LOG_API("nDeviceCreate"); 471 return (jlong)(uintptr_t)dispatchTab.DeviceCreate(); 472} 473 474static void 475nDeviceDestroy(JNIEnv *_env, jobject _this, jlong dev) 476{ 477 LOG_API("nDeviceDestroy"); 478 return dispatchTab.DeviceDestroy((RsDevice)dev); 479} 480 481static void 482nDeviceSetConfig(JNIEnv *_env, jobject _this, jlong dev, jint p, jint value) 483{ 484 LOG_API("nDeviceSetConfig dev(%p), param(%i), value(%i)", (void *)dev, p, value); 485 return dispatchTab.DeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value); 486} 487 488static jlong 489nContextCreate(JNIEnv *_env, jobject _this, jlong dev, jint ver, jint sdkVer, 490 jint ct, jstring nativeLibDirJava) 491{ 492 LOG_API("nContextCreate"); 493 // Access the NativeLibDir in the Java Context. 494 const char * nativeLibDir = _env->GetStringUTFChars(nativeLibDirJava, JNI_FALSE); 495 size_t length = (size_t)_env->GetStringUTFLength(nativeLibDirJava); 496 497 jlong id = (jlong)(uintptr_t)dispatchTab.ContextCreate((RsDevice)dev, ver, 498 sdkVer, 499 (RsContextType)ct, 0); 500 if (dispatchTab.SetNativeLibDir) { 501 dispatchTab.SetNativeLibDir((RsContext)id, nativeLibDir, length); 502 } 503 504 _env->ReleaseStringUTFChars(nativeLibDirJava, nativeLibDir); 505 return id; 506} 507 508 509static void 510nContextSetPriority(JNIEnv *_env, jobject _this, jlong con, jint p) 511{ 512 LOG_API("ContextSetPriority, con(%p), priority(%i)", (RsContext)con, p); 513 dispatchTab.ContextSetPriority((RsContext)con, p); 514} 515 516 517 518static void 519nContextDestroy(JNIEnv *_env, jobject _this, jlong con) 520{ 521 LOG_API("nContextDestroy, con(%p)", (RsContext)con); 522 dispatchTab.ContextDestroy((RsContext)con); 523} 524 525static void 526nContextDump(JNIEnv *_env, jobject _this, jlong con, jint bits) 527{ 528 LOG_API("nContextDump, con(%p) bits(%i)", (RsContext)con, bits); 529 dispatchTab.ContextDump((RsContext)con, bits); 530} 531 532 533static jstring 534nContextGetErrorMessage(JNIEnv *_env, jobject _this, jlong con) 535{ 536 LOG_API("nContextGetErrorMessage, con(%p)", (RsContext)con); 537 char buf[1024]; 538 539 size_t receiveLen; 540 uint32_t subID; 541 int id = dispatchTab.ContextGetMessage((RsContext)con, 542 buf, sizeof(buf), 543 &receiveLen, sizeof(receiveLen), 544 &subID, sizeof(subID)); 545 if (!id && receiveLen) { 546 // __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, 547 // "message receive buffer too small. %zu", receiveLen); 548 } 549 return _env->NewStringUTF(buf); 550} 551 552static jint 553nContextGetUserMessage(JNIEnv *_env, jobject _this, jlong con, jintArray data) 554{ 555 jint len = _env->GetArrayLength(data); 556 LOG_API("nContextGetMessage, con(%p), len(%i)", (RsContext)con, len); 557 jint *ptr = _env->GetIntArrayElements(data, NULL); 558 size_t receiveLen; 559 uint32_t subID; 560 int id = dispatchTab.ContextGetMessage((RsContext)con, 561 ptr, len * 4, 562 &receiveLen, sizeof(receiveLen), 563 &subID, sizeof(subID)); 564 if (!id && receiveLen) { 565 // __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, 566 // "message receive buffer too small. %zu", receiveLen); 567 } 568 _env->ReleaseIntArrayElements(data, ptr, 0); 569 return (jint)id; 570} 571 572static jint 573nContextPeekMessage(JNIEnv *_env, jobject _this, jlong con, jintArray auxData) 574{ 575 LOG_API("nContextPeekMessage, con(%p)", (RsContext)con); 576 jint *auxDataPtr = _env->GetIntArrayElements(auxData, NULL); 577 size_t receiveLen; 578 uint32_t subID; 579 int id = dispatchTab.ContextPeekMessage((RsContext)con, &receiveLen, sizeof(receiveLen), 580 &subID, sizeof(subID)); 581 auxDataPtr[0] = (jint)subID; 582 auxDataPtr[1] = (jint)receiveLen; 583 _env->ReleaseIntArrayElements(auxData, auxDataPtr, 0); 584 return (jint)id; 585} 586 587static void nContextInitToClient(JNIEnv *_env, jobject _this, jlong con) 588{ 589 LOG_API("nContextInitToClient, con(%p)", (RsContext)con); 590 dispatchTab.ContextInitToClient((RsContext)con); 591} 592 593static void nContextDeinitToClient(JNIEnv *_env, jobject _this, jlong con) 594{ 595 LOG_API("nContextDeinitToClient, con(%p)", (RsContext)con); 596 dispatchTab.ContextDeinitToClient((RsContext)con); 597} 598 599static void 600nContextSendMessage(JNIEnv *_env, jobject _this, jlong con, jint id, jintArray data) 601{ 602 jint *ptr = NULL; 603 jint len = 0; 604 if (data) { 605 len = _env->GetArrayLength(data); 606 jint *ptr = _env->GetIntArrayElements(data, NULL); 607 } 608 LOG_API("nContextSendMessage, con(%p), id(%i), len(%i)", (RsContext)con, id, len); 609 dispatchTab.ContextSendMessage((RsContext)con, id, (const uint8_t *)ptr, len * sizeof(int)); 610 if (data) { 611 _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT); 612 } 613} 614 615 616 617static jlong 618nElementCreate(JNIEnv *_env, jobject _this, jlong con, jlong type, jint kind, 619 jboolean norm, jint size) 620{ 621 LOG_API("nElementCreate, con(%p), type(%i), kind(%i), norm(%i), size(%i)", (RsContext)con, 622 type, kind, norm, size); 623 return (jlong)(uintptr_t)dispatchTab.ElementCreate((RsContext)con, 624 (RsDataType)type, 625 (RsDataKind)kind, 626 norm, size); 627} 628 629static jlong 630nElementCreate2(JNIEnv *_env, jobject _this, jlong con, 631 jlongArray _ids, jobjectArray _names, jintArray _arraySizes) 632{ 633 int fieldCount = _env->GetArrayLength(_ids); 634 LOG_API("nElementCreate2, con(%p)", (RsContext)con); 635 636 jlong *jIds = _env->GetLongArrayElements(_ids, NULL); 637 jint *jArraySizes = _env->GetIntArrayElements(_arraySizes, NULL); 638 639 RsElement *ids = (RsElement*)malloc(fieldCount * sizeof(RsElement)); 640 uint32_t *arraySizes = (uint32_t *)malloc(fieldCount * sizeof(uint32_t)); 641 642 for(int i = 0; i < fieldCount; i ++) { 643 ids[i] = (RsElement)jIds[i]; 644 arraySizes[i] = (uint32_t)jArraySizes[i]; 645 } 646 647 AutoJavaStringArrayToUTF8 names(_env, _names, fieldCount); 648 649 const char **nameArray = names.c_str(); 650 size_t *sizeArray = names.c_str_len(); 651 652 jlong id = (jlong)(uintptr_t)dispatchTab.ElementCreate2((RsContext)con, (RsElement *)ids, 653 fieldCount, nameArray, 654 fieldCount * sizeof(size_t), sizeArray, 655 (const uint32_t *)arraySizes, fieldCount); 656 657 free(ids); 658 free(arraySizes); 659 _env->ReleaseLongArrayElements(_ids, jIds, JNI_ABORT); 660 _env->ReleaseIntArrayElements(_arraySizes, jArraySizes, JNI_ABORT); 661 return id; 662} 663 664 665 666 667static void 668nElementGetSubElements(JNIEnv *_env, jobject _this, jlong con, jlong id, 669 jlongArray _IDs, 670 jobjectArray _names, 671 jintArray _arraySizes) 672{ 673 uint32_t dataSize = _env->GetArrayLength(_IDs); 674 LOG_API("nElementGetSubElements, con(%p)", (RsContext)con); 675 676 uintptr_t *ids = (uintptr_t *)malloc(dataSize * sizeof(uintptr_t)); 677 const char **names = (const char **)malloc((uint32_t)dataSize * sizeof(const char *)); 678 uint32_t *arraySizes = (uint32_t *)malloc((uint32_t)dataSize * sizeof(uint32_t)); 679 680 dispatchTab.ElementGetSubElements((RsContext)con, (RsElement)id, ids, names, arraySizes, 681 (uint32_t)dataSize); 682 683 for(uint32_t i = 0; i < dataSize; i++) { 684 const jlong id = (jlong)(uintptr_t)ids[i]; 685 const jint arraySize = (jint)arraySizes[i]; 686 _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i])); 687 _env->SetLongArrayRegion(_IDs, i, 1, &id); 688 _env->SetIntArrayRegion(_arraySizes, i, 1, &arraySize); 689 } 690 691 free(ids); 692 free(names); 693 free(arraySizes); 694} 695 696// ----------------------------------- 697 698static jlong 699nTypeCreate(JNIEnv *_env, jobject _this, jlong con, jlong eid, 700 jint dimx, jint dimy, jint dimz, jboolean mips, jboolean faces, jint yuv) 701{ 702 LOG_API("nTypeCreate, con(%p) eid(%p), x(%i), y(%i), z(%i), mips(%i), faces(%i), yuv(%i)", 703 (RsContext)con, eid, dimx, dimy, dimz, mips, faces, yuv); 704 705 return (jlong)(uintptr_t)dispatchTab.TypeCreate((RsContext)con, (RsElement)eid, dimx, dimy, 706 dimz, mips, faces, yuv); 707} 708 709// ----------------------------------- 710 711static jlong 712nAllocationCreateTyped(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mips, jint usage, 713 jlong pointer) 714{ 715 LOG_API("nAllocationCreateTyped, con(%p), type(%p), mip(%i), usage(%i), ptr(%p)", 716 (RsContext)con, (RsElement)type, mips, usage, (void *)pointer); 717 return (jlong)(uintptr_t) dispatchTab.AllocationCreateTyped((RsContext)con, (RsType)type, 718 (RsAllocationMipmapControl)mips, 719 (uint32_t)usage, (uintptr_t)pointer); 720} 721 722static void 723nAllocationSyncAll(JNIEnv *_env, jobject _this, jlong con, jlong a, jint bits) 724{ 725 LOG_API("nAllocationSyncAll, con(%p), a(%p), bits(0x%08x)", (RsContext)con, (RsAllocation)a, bits); 726 dispatchTab.AllocationSyncAll((RsContext)con, (RsAllocation)a, (RsAllocationUsageType)bits); 727} 728 729static void 730nAllocationSetSurface(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject sur) 731{ 732 ioDispatch.sAllocationSetSurface(_env, _this, (RsContext)con, (RsAllocation)alloc, sur, dispatchTab); 733} 734 735static void 736nAllocationIoSend(JNIEnv *_env, jobject _this, jlong con, jlong alloc) 737{ 738 dispatchTab.AllocationIoSend((RsContext)con, (RsAllocation)alloc); 739} 740 741static void 742nAllocationGenerateMipmaps(JNIEnv *_env, jobject _this, jlong con, jlong alloc) 743{ 744 LOG_API("nAllocationGenerateMipmaps, con(%p), a(%p)", (RsContext)con, (RsAllocation)alloc); 745 dispatchTab.AllocationGenerateMipmaps((RsContext)con, (RsAllocation)alloc); 746} 747 748static size_t GetBitmapSize(JNIEnv *env, jobject jbitmap) { 749 AndroidBitmapInfo info; 750 memset(&info, 0, sizeof(info)); 751 AndroidBitmap_getInfo(env, jbitmap, &info); 752 size_t s = info.width * info.height; 753 switch (info.format) { 754 case ANDROID_BITMAP_FORMAT_RGBA_8888: s *= 4; break; 755 case ANDROID_BITMAP_FORMAT_RGB_565: s *= 2; break; 756 case ANDROID_BITMAP_FORMAT_RGBA_4444: s *= 2; break; 757 } 758 return s; 759} 760 761static jlong 762nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip, 763 jobject jbitmap, jint usage) 764{ 765 jlong id = 0; 766 void *pixels = NULL; 767 AndroidBitmap_lockPixels(_env, jbitmap, &pixels); 768 769 if (pixels != NULL) { 770 id = (jlong)(uintptr_t)dispatchTab.AllocationCreateFromBitmap((RsContext)con, 771 (RsType)type, 772 (RsAllocationMipmapControl)mip, 773 pixels, 774 GetBitmapSize(_env, jbitmap), 775 usage); 776 AndroidBitmap_unlockPixels(_env, jbitmap); 777 } 778 return id; 779} 780 781static jlong 782nAllocationCreateBitmapBackedAllocation(JNIEnv *_env, jobject _this, jlong con, jlong type, 783 jint mip, jobject jbitmap, jint usage) 784{ 785 jlong id = 0; 786 void *pixels = NULL; 787 AndroidBitmap_lockPixels(_env, jbitmap, &pixels); 788 789 if (pixels != NULL) { 790 id = (jlong)(uintptr_t)dispatchTab.AllocationCreateTyped((RsContext)con, 791 (RsType)type, 792 (RsAllocationMipmapControl)mip, 793 (uint32_t)usage, 794 (uintptr_t)pixels); 795 AndroidBitmap_unlockPixels(_env, jbitmap); 796 } 797 return id; 798} 799 800static jlong 801nAllocationCubeCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, 802 jint mip, jobject jbitmap, jint usage) 803{ 804 void *pixels = NULL; 805 AndroidBitmap_lockPixels(_env, jbitmap, &pixels); 806 807 jlong id = 0; 808 if (pixels != NULL) { 809 id = (jlong)(uintptr_t)dispatchTab.AllocationCubeCreateFromBitmap((RsContext)con, 810 (RsType)type, 811 (RsAllocationMipmapControl)mip, 812 pixels, 813 GetBitmapSize(_env, jbitmap), 814 usage); 815 AndroidBitmap_unlockPixels(_env, jbitmap); 816 } 817 return id; 818} 819 820static void 821nAllocationCopyFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap) 822{ 823 AndroidBitmapInfo info; 824 memset(&info, 0, sizeof(info)); 825 AndroidBitmap_getInfo(_env, jbitmap, &info); 826 827 void *pixels = NULL; 828 AndroidBitmap_lockPixels(_env, jbitmap, &pixels); 829 830 if (pixels != NULL) { 831 dispatchTab.Allocation2DData((RsContext)con, (RsAllocation)alloc, 0, 0, 0, 832 RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, info.width, 833 info.height, pixels, GetBitmapSize(_env, jbitmap), 0); 834 AndroidBitmap_unlockPixels(_env, jbitmap); 835 } 836} 837 838static void 839nAllocationCopyToBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap) 840{ 841 AndroidBitmapInfo info; 842 memset(&info, 0, sizeof(info)); 843 AndroidBitmap_getInfo(_env, jbitmap, &info); 844 845 void *pixels = NULL; 846 AndroidBitmap_lockPixels(_env, jbitmap, &pixels); 847 848 if (pixels != NULL) { 849 dispatchTab.AllocationCopyToBitmap((RsContext)con, (RsAllocation)alloc, pixels, 850 GetBitmapSize(_env, jbitmap)); 851 AndroidBitmap_unlockPixels(_env, jbitmap); 852 } 853 //bitmap.notifyPixelsChanged(); 854} 855 856// Copies from the Java object data into the Allocation pointed to by _alloc. 857static void 858nAllocationData1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod, 859 jint count, jobject data, jint sizeBytes, jint dataType, jint mSize, 860 jboolean usePadding) 861{ 862 RsAllocation *alloc = (RsAllocation *)_alloc; 863 LOG_API("nAllocation1DData, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), " 864 "dataType(%i)", (RsContext)con, (RsAllocation)alloc, offset, count, sizeBytes, 865 dataType); 866 PER_ARRAY_TYPE(nullptr, dispatchTab.Allocation1DData, true, 867 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes); 868} 869 870 871static void 872nAllocationElementData1D(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint xoff, 873 jint lod, jint compIdx, jbyteArray data, jint sizeBytes) 874{ 875 jint len = _env->GetArrayLength(data); 876 LOG_API("nAllocationElementData1D, con(%p), alloc(%p), xoff(%i), comp(%i), len(%i), " 877 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, compIdx, len, 878 sizeBytes); 879 jbyte *ptr = _env->GetByteArrayElements(data, nullptr); 880 dispatchTab.Allocation1DElementData((RsContext)con, (RsAllocation)alloc, xoff, 881 lod, ptr, sizeBytes, compIdx); 882 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT); 883} 884 885/* 886static void 887nAllocationElementData(JNIEnv *_env, jobject _this, jlong con, jlong alloc, 888 jint xoff, jint yoff, jint zoff, 889 jint lod, jint compIdx, jbyteArray data, jint sizeBytes) 890{ 891 jint len = _env->GetArrayLength(data); 892 LOG_API("nAllocationElementData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), " 893 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len, 894 sizeBytes); 895 jbyte *ptr = _env->GetByteArrayElements(data, nullptr); 896 dispatchTab.AllocationElementData((RsContext)con, (RsAllocation)alloc, 897 xoff, yoff, zoff, 898 lod, ptr, sizeBytes, compIdx); 899 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT); 900} 901*/ 902 903// Copies from the Java object data into the Allocation pointed to by _alloc. 904static void 905nAllocationData2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face, 906 jint w, jint h, jobject data, jint sizeBytes, jint dataType, jint mSize, 907 jboolean usePadding) 908{ 909 RsAllocation *alloc = (RsAllocation *)_alloc; 910 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face; 911 LOG_API("nAllocation2DData, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) " 912 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType); 913 int count = w * h; 914 PER_ARRAY_TYPE(nullptr, dispatchTab.Allocation2DData, true, 915 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0); 916} 917 918static void 919nAllocationData2D_alloc(JNIEnv *_env, jobject _this, jlong con, 920 jlong dstAlloc, jint dstXoff, jint dstYoff, 921 jint dstMip, jint dstFace, 922 jint width, jint height, 923 jlong srcAlloc, jint srcXoff, jint srcYoff, 924 jint srcMip, jint srcFace) 925{ 926 LOG_API("nAllocation2DData_s, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i)," 927 " dstMip(%i), dstFace(%i), width(%i), height(%i)," 928 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i), srcFace(%i)", 929 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip, dstFace, 930 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip, srcFace); 931 932 dispatchTab.AllocationCopy2DRange((RsContext)con, 933 (RsAllocation)dstAlloc, 934 dstXoff, dstYoff, 935 dstMip, dstFace, 936 width, height, 937 (RsAllocation)srcAlloc, 938 srcXoff, srcYoff, 939 srcMip, srcFace); 940} 941 942// Copies from the Java object data into the Allocation pointed to by _alloc. 943static void 944nAllocationData3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod, 945 jint w, jint h, jint d, jobject data, jint sizeBytes, jint dataType, 946 jint mSize, jboolean usePadding) 947{ 948 RsAllocation *alloc = (RsAllocation *)_alloc; 949 LOG_API("nAllocation3DData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i)," 950 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, 951 lod, w, h, d, sizeBytes); 952 int count = w * h * d; 953 PER_ARRAY_TYPE(nullptr, dispatchTab.Allocation3DData, true, 954 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0); 955} 956 957static void 958nAllocationData3D_alloc(JNIEnv *_env, jobject _this, jlong con, 959 jlong dstAlloc, jint dstXoff, jint dstYoff, jint dstZoff, 960 jint dstMip, 961 jint width, jint height, jint depth, 962 jlong srcAlloc, jint srcXoff, jint srcYoff, jint srcZoff, 963 jint srcMip) 964{ 965 LOG_API("nAllocationData3D_alloc, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i)," 966 " dstMip(%i), width(%i), height(%i)," 967 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i)", 968 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip, dstFace, 969 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip, srcFace); 970 971 dispatchTab.AllocationCopy3DRange((RsContext)con, 972 (RsAllocation)dstAlloc, 973 dstXoff, dstYoff, dstZoff, dstMip, 974 width, height, depth, 975 (RsAllocation)srcAlloc, 976 srcXoff, srcYoff, srcZoff, srcMip); 977} 978 979// Copies from the Allocation pointed to by _alloc into the Java object data. 980static void 981nAllocationRead(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jobject data, jint dataType, 982 jint mSize, jboolean usePadding) 983{ 984 RsAllocation *alloc = (RsAllocation *)_alloc; 985 LOG_API("nAllocationRead, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc); 986 int count = 0; 987 PER_ARRAY_TYPE(0, dispatchTab.AllocationRead, false, 988 (RsContext)con, alloc, ptr, len * typeBytes); 989} 990 991// Copies from the Allocation pointed to by _alloc into the Java object data. 992static void 993nAllocationRead1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod, 994 jint count, jobject data, jint sizeBytes, jint dataType, 995 jint mSize, jboolean usePadding) 996{ 997 RsAllocation *alloc = (RsAllocation *)_alloc; 998 LOG_API("nAllocation1DRead, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), " 999 "dataType(%i)", (RsContext)con, alloc, offset, count, sizeBytes, dataType); 1000 PER_ARRAY_TYPE(0, dispatchTab.Allocation1DRead, false, 1001 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes); 1002} 1003 1004// Copies from the Element in the Allocation pointed to by _alloc into the Java array data. 1005/* 1006static void 1007nAllocationElementRead(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, 1008 jint xoff, jint yoff, jint zoff, 1009 jint lod, jint compIdx, jobject data, jint sizeBytes) 1010{ 1011 jint len = _env->GetArrayLength(data); 1012 LOG_API("nAllocationElementRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), " 1013 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len, 1014 sizeBytes); 1015 jbyte *ptr = _env->GetByteArrayElements(data, nullptr); 1016 dispatchTab.AllocationElementRead((RsContext)con, (RsAllocation)alloc, 1017 xoff, yoff, zoff, 1018 lod, ptr, sizeBytes, compIdx); 1019 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT); 1020} 1021*/ 1022 1023// Copies from the Allocation pointed to by _alloc into the Java object data. 1024static void 1025nAllocationRead2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face, 1026 jint w, jint h, jobject data, jint sizeBytes, jint dataType, 1027 jint mSize, jboolean usePadding) 1028{ 1029 RsAllocation *alloc = (RsAllocation *)_alloc; 1030 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face; 1031 LOG_API("nAllocation2DRead, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) " 1032 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType); 1033 int count = w * h; 1034 PER_ARRAY_TYPE(0, dispatchTab.Allocation2DRead, false, 1035 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0); 1036} 1037 1038// Copies from the Allocation pointed to by _alloc into the Java object data. 1039/* 1040static void 1041nAllocationRead3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod, 1042 jint w, jint h, jint d, jobject data, int sizeBytes, int dataType, 1043 jint mSize, jboolean usePadding) 1044{ 1045 RsAllocation *alloc = (RsAllocation *)_alloc; 1046 LOG_API("nAllocation3DRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i)," 1047 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, 1048 lod, w, h, d, sizeBytes); 1049 int count = w * h * d; 1050 PER_ARRAY_TYPE(nullptr, dispatchTab.Allocation3DRead, false, 1051 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0); 1052} 1053*/ 1054 1055static jlong 1056nAllocationGetType(JNIEnv *_env, jobject _this, jlong con, jlong a) 1057{ 1058 LOG_API("nAllocationGetType, con(%p), a(%p)", (RsContext)con, (RsAllocation)a); 1059 return (jlong)(uintptr_t) dispatchTab.AllocationGetType((RsContext)con, (RsAllocation)a); 1060} 1061 1062static void 1063nAllocationResize1D(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint dimX) 1064{ 1065 LOG_API("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", (RsContext)con, 1066 (RsAllocation)alloc, dimX); 1067 dispatchTab.AllocationResize1D((RsContext)con, (RsAllocation)alloc, dimX); 1068} 1069 1070// ----------------------------------- 1071 1072static void 1073nScriptBindAllocation(JNIEnv *_env, jobject _this, jlong con, jlong script, jlong alloc, jint slot, jboolean mUseInc) 1074{ 1075 LOG_API("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", 1076 (RsContext)con, (RsScript)script, (RsAllocation)alloc, slot); 1077 if (mUseInc) { 1078 dispatchTabInc.ScriptBindAllocation((RsContext)con, (RsScript)script, (RsAllocation)alloc, slot); 1079 } else { 1080 dispatchTab.ScriptBindAllocation((RsContext)con, (RsScript)script, (RsAllocation)alloc, slot); 1081 } 1082} 1083 1084static void 1085nScriptSetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jint val, jboolean mUseInc) 1086{ 1087 LOG_API("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", (RsContext)con, 1088 (void *)script, slot, val); 1089 if (mUseInc) { 1090 dispatchTabInc.ScriptSetVarI((RsContext)con, (RsScript)script, slot, val); 1091 } else { 1092 dispatchTab.ScriptSetVarI((RsContext)con, (RsScript)script, slot, val); 1093 } 1094} 1095 1096static void 1097nScriptSetVarObj(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val, jboolean mUseInc) 1098{ 1099 LOG_API("nScriptSetVarObj, con(%p), s(%p), slot(%i), val(%i)", (RsContext)con, 1100 (void *)script, slot, val); 1101 if (mUseInc) { 1102 dispatchTabInc.ScriptSetVarObj((RsContext)con, (RsScript)script, slot, (RsObjectBase)val); 1103 } else { 1104 dispatchTab.ScriptSetVarObj((RsContext)con, (RsScript)script, slot, (RsObjectBase)val); 1105 } 1106} 1107 1108static void 1109nScriptSetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val, jboolean mUseInc) 1110{ 1111 LOG_API("nScriptSetVarJ, con(%p), s(%p), slot(%i), val(%lli)", (RsContext)con, 1112 (void *)script, slot, val); 1113 if (mUseInc) { 1114 dispatchTabInc.ScriptSetVarJ((RsContext)con, (RsScript)script, slot, val); 1115 } else { 1116 dispatchTab.ScriptSetVarJ((RsContext)con, (RsScript)script, slot, val); 1117 } 1118} 1119 1120static void 1121nScriptSetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, float val, jboolean mUseInc) 1122{ 1123 LOG_API("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", (RsContext)con, 1124 (void *)script, slot, val); 1125 if (mUseInc) { 1126 dispatchTabInc.ScriptSetVarF((RsContext)con, (RsScript)script, slot, val); 1127 } else { 1128 dispatchTab.ScriptSetVarF((RsContext)con, (RsScript)script, slot, val); 1129 } 1130} 1131 1132static void 1133nScriptSetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, double val, jboolean mUseInc) 1134{ 1135 LOG_API("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", (RsContext)con, 1136 (void *)script, slot, val); 1137 if (mUseInc) { 1138 dispatchTabInc.ScriptSetVarD((RsContext)con, (RsScript)script, slot, val); 1139 } else { 1140 dispatchTab.ScriptSetVarD((RsContext)con, (RsScript)script, slot, val); 1141 } 1142} 1143 1144static void 1145nScriptSetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data, jboolean mUseInc) 1146{ 1147 LOG_API("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot); 1148 jint len = _env->GetArrayLength(data); 1149 jbyte *ptr = _env->GetByteArrayElements(data, NULL); 1150 if (mUseInc) { 1151 dispatchTabInc.ScriptSetVarV((RsContext)con, (RsScript)script, slot, ptr, len); 1152 } else { 1153 dispatchTab.ScriptSetVarV((RsContext)con, (RsScript)script, slot, ptr, len); 1154 } 1155 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT); 1156} 1157 1158static void 1159nScriptSetVarVE(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data, 1160 jlong elem, jintArray dims, jboolean mUseInc) 1161{ 1162 LOG_API("nScriptSetVarVE, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot); 1163 jint len = _env->GetArrayLength(data); 1164 jbyte *ptr = _env->GetByteArrayElements(data, NULL); 1165 jint dimsLen = _env->GetArrayLength(dims) * sizeof(int); 1166 jint *dimsPtr = _env->GetIntArrayElements(dims, NULL); 1167 if (mUseInc) { 1168 dispatchTabInc.ScriptSetVarVE((RsContext)con, (RsScript)script, slot, ptr, len, (RsElement)elem, 1169 (const uint32_t *)dimsPtr, dimsLen); 1170 } else { 1171 dispatchTab.ScriptSetVarVE((RsContext)con, (RsScript)script, slot, ptr, len, (RsElement)elem, 1172 (const uint32_t *)dimsPtr, dimsLen); 1173 } 1174 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT); 1175 _env->ReleaseIntArrayElements(dims, dimsPtr, JNI_ABORT); 1176} 1177 1178 1179static void 1180nScriptSetTimeZone(JNIEnv *_env, jobject _this, jlong con, jlong script, jbyteArray timeZone, jboolean mUseInc) 1181{ 1182 LOG_API("nScriptCSetTimeZone, con(%p), s(%p), timeZone(%s)", (RsContext)con, 1183 (void *)script, (const char *)timeZone); 1184 1185 jint length = _env->GetArrayLength(timeZone); 1186 jbyte* timeZone_ptr; 1187 timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0); 1188 if (mUseInc) { 1189 dispatchTabInc.ScriptSetTimeZone((RsContext)con, (RsScript)script, (const char *)timeZone_ptr, length); 1190 } else { 1191 dispatchTab.ScriptSetTimeZone((RsContext)con, (RsScript)script, (const char *)timeZone_ptr, length); 1192 } 1193 1194 if (timeZone_ptr) { 1195 _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0); 1196 } 1197} 1198 1199static void 1200nScriptInvoke(JNIEnv *_env, jobject _this, jlong con, jlong obj, jint slot, jboolean mUseInc) 1201{ 1202 LOG_API("nScriptInvoke, con(%p), script(%p)", (RsContext)con, (void *)obj); 1203 if (mUseInc) { 1204 dispatchTabInc.ScriptInvoke((RsContext)con, (RsScript)obj, slot); 1205 } else { 1206 dispatchTab.ScriptInvoke((RsContext)con, (RsScript)obj, slot); 1207 } 1208} 1209 1210static void 1211nScriptInvokeV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data, jboolean mUseInc) 1212{ 1213 LOG_API("nScriptInvokeV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot); 1214 jint len = _env->GetArrayLength(data); 1215 jbyte *ptr = _env->GetByteArrayElements(data, NULL); 1216 if (mUseInc) { 1217 dispatchTabInc.ScriptInvokeV((RsContext)con, (RsScript)script, slot, ptr, len); 1218 } else { 1219 dispatchTab.ScriptInvokeV((RsContext)con, (RsScript)script, slot, ptr, len); 1220 } 1221 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT); 1222} 1223 1224static void 1225nScriptForEach(JNIEnv *_env, jobject _this, jlong con, jlong incCon, 1226 jlong script, jint slot, jlong ain, jlong aout, jboolean mUseInc) 1227{ 1228 LOG_API("nScriptForEach, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot); 1229 if (mUseInc) { 1230 dispatchTab.ContextFinish((RsContext)con); 1231 dispatchTabInc.ScriptForEach((RsContext)incCon, (RsScript)script, slot, 1232 (RsAllocation)ain, (RsAllocation)aout, 1233 NULL, 0, NULL, 0); 1234 } else { 1235 dispatchTab.ScriptForEach((RsContext)con, (RsScript)script, slot, 1236 (RsAllocation)ain, (RsAllocation)aout, 1237 NULL, 0, NULL, 0); 1238 } 1239} 1240static void 1241nScriptForEachV(JNIEnv *_env, jobject _this, jlong con, jlong incCon, 1242 jlong script, jint slot, jlong ain, jlong aout, jbyteArray params, jboolean mUseInc) 1243{ 1244 LOG_API("nScriptForEach, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot); 1245 jint len = _env->GetArrayLength(params); 1246 jbyte *ptr = _env->GetByteArrayElements(params, NULL); 1247 if (mUseInc) { 1248 dispatchTab.ContextFinish((RsContext)con); 1249 dispatchTabInc.ScriptForEach((RsContext)incCon, (RsScript)script, slot, 1250 (RsAllocation)ain, (RsAllocation)aout, 1251 ptr, len, NULL, 0); 1252 } else { 1253 dispatchTab.ScriptForEach((RsContext)con, (RsScript)script, slot, 1254 (RsAllocation)ain, (RsAllocation)aout, 1255 ptr, len, NULL, 0); 1256 } 1257 _env->ReleaseByteArrayElements(params, ptr, JNI_ABORT); 1258} 1259 1260static void 1261nScriptForEachClipped(JNIEnv *_env, jobject _this, jlong con, jlong incCon, 1262 jlong script, jint slot, jlong ain, jlong aout, 1263 jint xstart, jint xend, 1264 jint ystart, jint yend, jint zstart, jint zend, jboolean mUseInc) 1265{ 1266 LOG_API("nScriptForEachClipped, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot); 1267 RsScriptCall sc; 1268 sc.xStart = xstart; 1269 sc.xEnd = xend; 1270 sc.yStart = ystart; 1271 sc.yEnd = yend; 1272 sc.zStart = zstart; 1273 sc.zEnd = zend; 1274 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE; 1275 sc.arrayStart = 0; 1276 sc.arrayEnd = 0; 1277 if (mUseInc) { 1278 dispatchTab.ContextFinish((RsContext)con); 1279 dispatchTabInc.ScriptForEach((RsContext)incCon, (RsScript)script, slot, 1280 (RsAllocation)ain, (RsAllocation)aout, 1281 NULL, 0, &sc, sizeof(sc)); 1282 } else { 1283 dispatchTab.ScriptForEach((RsContext)con, (RsScript)script, slot, 1284 (RsAllocation)ain, (RsAllocation)aout, 1285 NULL, 0, &sc, sizeof(sc)); 1286 } 1287} 1288 1289static void 1290nScriptForEachClippedV(JNIEnv *_env, jobject _this, jlong con, jlong incCon, 1291 jlong script, jint slot, jlong ain, jlong aout, 1292 jbyteArray params, jint xstart, jint xend, 1293 jint ystart, jint yend, jint zstart, jint zend, jboolean mUseInc) 1294{ 1295 LOG_API("nScriptForEachClipped, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot); 1296 jint len = _env->GetArrayLength(params); 1297 jbyte *ptr = _env->GetByteArrayElements(params, NULL); 1298 RsScriptCall sc; 1299 sc.xStart = xstart; 1300 sc.xEnd = xend; 1301 sc.yStart = ystart; 1302 sc.yEnd = yend; 1303 sc.zStart = zstart; 1304 sc.zEnd = zend; 1305 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE; 1306 sc.arrayStart = 0; 1307 sc.arrayEnd = 0; 1308 if (mUseInc) { 1309 dispatchTab.ContextFinish((RsContext)con); 1310 dispatchTabInc.ScriptForEach((RsContext)incCon, (RsScript)script, slot, 1311 (RsAllocation)ain, (RsAllocation)aout, 1312 ptr, len, &sc, sizeof(sc)); 1313 } else { 1314 dispatchTab.ScriptForEach((RsContext)con, (RsScript)script, slot, 1315 (RsAllocation)ain, (RsAllocation)aout, 1316 ptr, len, &sc, sizeof(sc)); 1317 } 1318 _env->ReleaseByteArrayElements(params, ptr, JNI_ABORT); 1319} 1320 1321// ----------------------------------- 1322 1323static jlong 1324nScriptCCreate(JNIEnv *_env, jobject _this, jlong con, 1325 jstring resName, jstring cacheDir, 1326 jbyteArray scriptRef, jint length) 1327{ 1328 LOG_API("nScriptCCreate, con(%p)", (RsContext)con); 1329 1330 AutoJavaStringToUTF8 resNameUTF(_env, resName); 1331 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir); 1332 jlong ret = 0; 1333 jbyte* script_ptr = NULL; 1334 jint _exception = 0; 1335 jint remaining; 1336 if (!scriptRef) { 1337 _exception = 1; 1338 //jniThrowException(_env, "java/lang/IllegalArgumentException", "script == null"); 1339 goto exit; 1340 } 1341 if (length < 0) { 1342 _exception = 1; 1343 //jniThrowException(_env, "java/lang/IllegalArgumentException", "length < 0"); 1344 goto exit; 1345 } 1346 remaining = _env->GetArrayLength(scriptRef); 1347 if (remaining < length) { 1348 _exception = 1; 1349 //jniThrowException(_env, "java/lang/IllegalArgumentException", 1350 // "length > script.length - offset"); 1351 goto exit; 1352 } 1353 script_ptr = (jbyte *) 1354 _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0); 1355 1356 //rsScriptCSetText(con, (const char *)script_ptr, length); 1357 1358 ret = (jlong)(uintptr_t)dispatchTab.ScriptCCreate((RsContext)con, 1359 resNameUTF.c_str(), resNameUTF.length(), 1360 cacheDirUTF.c_str(), cacheDirUTF.length(), 1361 (const char *)script_ptr, length); 1362 1363exit: 1364 if (script_ptr) { 1365 _env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr, 1366 _exception ? JNI_ABORT: 0); 1367 } 1368 1369 return (jlong)(uintptr_t)ret; 1370} 1371 1372static jlong 1373nScriptIntrinsicCreate(JNIEnv *_env, jobject _this, jlong con, jint id, jlong eid, jboolean mUseInc) 1374{ 1375 LOG_API("nScriptIntrinsicCreate, con(%p) id(%i) element(%p)", (RsContext)con, id, (void *)eid); 1376 if (mUseInc) { 1377 return (jlong)(uintptr_t)dispatchTabInc.ScriptIntrinsicCreate((RsContext)con, id, (RsElement)eid); 1378 } else { 1379 return (jlong)(uintptr_t)dispatchTab.ScriptIntrinsicCreate((RsContext)con, id, (RsElement)eid); 1380 } 1381} 1382 1383static jlong 1384nScriptKernelIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot, jint sig, jboolean mUseInc) 1385{ 1386 LOG_API("nScriptKernelIDCreate, con(%p) script(%p), slot(%i), sig(%i)", (RsContext)con, 1387 (void *)sid, slot, sig); 1388 if (mUseInc) { 1389 return (jlong)(uintptr_t)dispatchTabInc.ScriptKernelIDCreate((RsContext)con, (RsScript)sid, 1390 slot, sig); 1391 } else { 1392 return (jlong)(uintptr_t)dispatchTab.ScriptKernelIDCreate((RsContext)con, (RsScript)sid, 1393 slot, sig); 1394 } 1395} 1396 1397static jlong 1398nScriptInvokeIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot) 1399{ 1400 LOG_API("nScriptInvokeIDCreate, con(%p) script(%p), slot(%i), sig(%i)", con, 1401 (void *)sid, slot); 1402 return (jlong)dispatchTab.ScriptInvokeIDCreate((RsContext)con, (RsScript)sid, slot); 1403} 1404 1405static jlong 1406nScriptFieldIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot, jboolean mUseInc) 1407{ 1408 LOG_API("nScriptFieldIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con, (void *)sid, slot); 1409 if (mUseInc) { 1410 return (jlong)(uintptr_t)dispatchTabInc.ScriptFieldIDCreate((RsContext)con, (RsScript)sid, slot); 1411 } else { 1412 return (jlong)(uintptr_t)dispatchTab.ScriptFieldIDCreate((RsContext)con, (RsScript)sid, slot); 1413 } 1414} 1415 1416static jlong 1417nScriptGroupCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _kernels, jlongArray _src, 1418 jlongArray _dstk, jlongArray _dstf, jlongArray _types) 1419{ 1420 LOG_API("nScriptGroupCreate, con(%p)", (RsContext)con); 1421 1422 jint kernelsLen = _env->GetArrayLength(_kernels); 1423 jlong *jKernelsPtr = _env->GetLongArrayElements(_kernels, nullptr); 1424 RsScriptKernelID* kernelsPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * kernelsLen); 1425 for(int i = 0; i < kernelsLen; ++i) { 1426 kernelsPtr[i] = (RsScriptKernelID)jKernelsPtr[i]; 1427 } 1428 1429 jint srcLen = _env->GetArrayLength(_src); 1430 jlong *jSrcPtr = _env->GetLongArrayElements(_src, nullptr); 1431 RsScriptKernelID* srcPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * srcLen); 1432 for(int i = 0; i < srcLen; ++i) { 1433 srcPtr[i] = (RsScriptKernelID)jSrcPtr[i]; 1434 } 1435 1436 jint dstkLen = _env->GetArrayLength(_dstk); 1437 jlong *jDstkPtr = _env->GetLongArrayElements(_dstk, nullptr); 1438 RsScriptKernelID* dstkPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstkLen); 1439 for(int i = 0; i < dstkLen; ++i) { 1440 dstkPtr[i] = (RsScriptKernelID)jDstkPtr[i]; 1441 } 1442 1443 jint dstfLen = _env->GetArrayLength(_dstf); 1444 jlong *jDstfPtr = _env->GetLongArrayElements(_dstf, nullptr); 1445 RsScriptKernelID* dstfPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstfLen); 1446 for(int i = 0; i < dstfLen; ++i) { 1447 dstfPtr[i] = (RsScriptKernelID)jDstfPtr[i]; 1448 } 1449 1450 jint typesLen = _env->GetArrayLength(_types); 1451 jlong *jTypesPtr = _env->GetLongArrayElements(_types, nullptr); 1452 RsType* typesPtr = (RsType*) malloc(sizeof(RsType) * typesLen); 1453 for(int i = 0; i < typesLen; ++i) { 1454 typesPtr[i] = (RsType)jTypesPtr[i]; 1455 } 1456 1457 jlong id = (jlong)(uintptr_t) dispatchTab.ScriptGroupCreate((RsContext)con, 1458 (RsScriptKernelID *)kernelsPtr, kernelsLen * sizeof(RsScriptKernelID), 1459 (RsScriptKernelID *)srcPtr, srcLen * sizeof(RsScriptKernelID), 1460 (RsScriptKernelID *)dstkPtr, dstkLen * sizeof(RsScriptKernelID), 1461 (RsScriptFieldID *)dstfPtr, dstfLen * sizeof(RsScriptKernelID), 1462 (RsType *)typesPtr, typesLen * sizeof(RsType)); 1463 1464 free(kernelsPtr); 1465 free(srcPtr); 1466 free(dstkPtr); 1467 free(dstfPtr); 1468 free(typesPtr); 1469 _env->ReleaseLongArrayElements(_kernels, jKernelsPtr, 0); 1470 _env->ReleaseLongArrayElements(_src, jSrcPtr, 0); 1471 _env->ReleaseLongArrayElements(_dstk, jDstkPtr, 0); 1472 _env->ReleaseLongArrayElements(_dstf, jDstfPtr, 0); 1473 _env->ReleaseLongArrayElements(_types, jTypesPtr, 0); 1474 return id; 1475} 1476 1477static void 1478nScriptGroupSetInput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc) 1479{ 1480 LOG_API("nScriptGroupSetInput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con, 1481 (void *)gid, (void *)kid, (void *)alloc); 1482 dispatchTab.ScriptGroupSetInput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, 1483 (RsAllocation)alloc); 1484} 1485 1486static void 1487nScriptGroupSetOutput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc) 1488{ 1489 LOG_API("nScriptGroupSetOutput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con, 1490 (void *)gid, (void *)kid, (void *)alloc); 1491 dispatchTab.ScriptGroupSetOutput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, 1492 (RsAllocation)alloc); 1493} 1494 1495static void 1496nScriptGroupExecute(JNIEnv *_env, jobject _this, jlong con, jlong gid) 1497{ 1498 LOG_API("nScriptGroupSetOutput, con(%p) group(%p)", (RsContext)con, (void *)gid); 1499 dispatchTab.ScriptGroupExecute((RsContext)con, (RsScriptGroup)gid); 1500} 1501 1502// --------------------------------------------------------------------------- 1503 1504static jlong 1505nSamplerCreate(JNIEnv *_env, jobject _this, jlong con, jint magFilter, jint minFilter, 1506 jint wrapS, jint wrapT, jint wrapR, jfloat aniso) 1507{ 1508 LOG_API("nSamplerCreate, con(%p)", (RsContext)con); 1509 return (jlong)(uintptr_t)dispatchTab.SamplerCreate((RsContext)con, 1510 (RsSamplerValue)magFilter, 1511 (RsSamplerValue)minFilter, 1512 (RsSamplerValue)wrapS, 1513 (RsSamplerValue)wrapT, 1514 (RsSamplerValue)wrapR, 1515 aniso); 1516} 1517 1518static jint 1519nSystemGetPointerSize(JNIEnv *_env, jobject _this) { 1520 return (jint)sizeof(void*); 1521} 1522 1523// --------------------------------------------------------------------------- 1524// For Incremental Intrinsic Support 1525static bool nIncLoadSO() { 1526 void* handle = NULL; 1527 handle = dlopen("libRSSupport.so", RTLD_LAZY | RTLD_LOCAL); 1528 if (handle == NULL) { 1529 LOG_API("couldn't dlopen %s, %s", filename, dlerror()); 1530 return false; 1531 } 1532 1533 if (loadSymbols(handle, dispatchTabInc) == false) { 1534 LOG_API("%s init failed!", filename); 1535 return false; 1536 } 1537 LOG_API("Successfully loaded %s", filename); 1538 return true; 1539} 1540 1541// ----------------------------------- 1542// To create/destroy a dummy context 1543static void 1544nIncObjDestroy(JNIEnv *_env, jobject _this, jlong con, jlong obj) 1545{ 1546 LOG_API("nObjDestroy, con(%p) obj(%p)", (RsContext)con, (void *)obj); 1547 dispatchTabInc.ObjDestroy((RsContext)con, (void *)obj); 1548} 1549 1550 1551static jlong 1552nIncDeviceCreate(JNIEnv *_env, jobject _this) 1553{ 1554 LOG_API("nDeviceCreate"); 1555 return (jlong)(uintptr_t)dispatchTabInc.DeviceCreate(); 1556} 1557 1558static void 1559nIncDeviceDestroy(JNIEnv *_env, jobject _this, jlong dev) 1560{ 1561 LOG_API("nDeviceDestroy"); 1562 return dispatchTabInc.DeviceDestroy((RsDevice)dev); 1563} 1564 1565static jlong 1566nIncContextCreate(JNIEnv *_env, jobject _this, jlong dev, jint ver, jint sdkVer, jint ct) 1567{ 1568 LOG_API("nContextCreate"); 1569 //The compat context for incremental support will be synchronous. 1570 return (jlong)(uintptr_t)dispatchTabInc.ContextCreate((RsDevice)dev, ver, sdkVer, 1571 (RsContextType)ct, 1572 RS_CONTEXT_SYNCHRONOUS); 1573} 1574 1575static void 1576nIncContextFinish(JNIEnv *_env, jobject _this, jlong con) 1577{ 1578 LOG_API("nContextFinish, con(%p)", (RsContext)con); 1579 dispatchTabInc.ContextFinish((RsContext)con); 1580} 1581 1582static void 1583nIncContextDestroy(JNIEnv *_env, jobject _this, jlong con) 1584{ 1585 LOG_API("nContextDestroy, con(%p)", (RsContext)con); 1586 dispatchTabInc.ContextDestroy((RsContext)con); 1587} 1588 1589// ----------------------------------- 1590// Create dummy Element 1591static jlong 1592nIncElementCreate(JNIEnv *_env, jobject _this, jlong con, jlong type, jint kind, jboolean norm, jint size) 1593{ 1594 LOG_API("nElementCreate, con(%p), type(%i), kind(%i), norm(%i), size(%i)", (RsContext)con, 1595 type, kind, norm, size); 1596 return (jlong)(uintptr_t)dispatchTabInc.ElementCreate((RsContext)con, (RsDataType)type, 1597 (RsDataKind)kind, norm, size); 1598} 1599// ----------------------------------- 1600// Create dummy Type 1601static jlong 1602nIncTypeCreate(JNIEnv *_env, jobject _this, jlong con, jlong eid, 1603 jint dimx, jint dimy, jint dimz, jboolean mips, jboolean faces, jint yuv) 1604{ 1605 LOG_API("nTypeCreate, con(%p) eid(%p), x(%i), y(%i), z(%i), mips(%i), faces(%i), yuv(%i)", 1606 incCon, eid, dimx, dimy, dimz, mips, faces, yuv); 1607 1608 return (jlong)(uintptr_t)dispatchTabInc.TypeCreate((RsContext)con, (RsElement)eid, dimx, dimy, 1609 dimz, mips, faces, yuv); 1610} 1611 1612// ----------------------------------- 1613// Create Allocation from pointer 1614static jlong 1615nIncAllocationCreateTyped(JNIEnv *_env, jobject _this, jlong con, jlong incCon, jlong alloc, jlong type) 1616{ 1617 LOG_API("nAllocationCreateTyped, con(%p), type(%p), mip(%i), usage(%i), ptr(%p)", 1618 incCon, (RsElement)type, mips, usage, (void *)pointer); 1619 size_t strideIn; 1620 void* pIn = NULL; 1621 RsAllocation ainI = NULL; 1622 if (alloc != 0) { 1623 pIn = dispatchTab.AllocationGetPointer((RsContext)con, (RsAllocation)alloc, 0, 1624 RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, 0, 0, 1625 &strideIn, sizeof(size_t)); 1626 ainI = dispatchTabInc.AllocationCreateTyped((RsContext)incCon, (RsType)type, 1627 RS_ALLOCATION_MIPMAP_NONE, 1628 RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_SHARED, 1629 (uintptr_t)pIn); 1630 } 1631 return (jlong)(uintptr_t) ainI; 1632} 1633 1634// --------------------------------------------------------------------------- 1635 1636 1637static const char *classPathName = "android/support/v8/renderscript/RenderScript"; 1638 1639static JNINativeMethod methods[] = { 1640{"nLoadSO", "(ZI)Z", (bool*)nLoadSO }, 1641{"nLoadIOSO", "()Z", (bool*)nLoadIOSO }, 1642{"nDeviceCreate", "()J", (void*)nDeviceCreate }, 1643{"nDeviceDestroy", "(J)V", (void*)nDeviceDestroy }, 1644{"nDeviceSetConfig", "(JII)V", (void*)nDeviceSetConfig }, 1645{"nContextGetUserMessage", "(J[I)I", (void*)nContextGetUserMessage }, 1646{"nContextGetErrorMessage", "(J)Ljava/lang/String;", (void*)nContextGetErrorMessage }, 1647{"nContextPeekMessage", "(J[I)I", (void*)nContextPeekMessage }, 1648{"nContextInitToClient", "(J)V", (void*)nContextInitToClient }, 1649{"nContextDeinitToClient", "(J)V", (void*)nContextDeinitToClient }, 1650 1651 1652// All methods below are thread protected in java. 1653{"rsnContextCreate", "(JIIILjava/lang/String;)J", (void*)nContextCreate }, 1654{"rsnContextFinish", "(J)V", (void*)nContextFinish }, 1655{"rsnContextSetPriority", "(JI)V", (void*)nContextSetPriority }, 1656{"rsnContextDestroy", "(J)V", (void*)nContextDestroy }, 1657{"rsnContextDump", "(JI)V", (void*)nContextDump }, 1658{"rsnContextSendMessage", "(JI[I)V", (void*)nContextSendMessage }, 1659{"rsnClosureCreate", "(JJJ[J[J[I[J[J)J", (void*)nClosureCreate }, 1660{"rsnInvokeClosureCreate", "(JJ[B[J[J[I)J", (void*)nInvokeClosureCreate }, 1661{"rsnClosureSetArg", "(JJIJI)V", (void*)nClosureSetArg }, 1662{"rsnClosureSetGlobal", "(JJJJI)V", (void*)nClosureSetGlobal }, 1663{"rsnObjDestroy", "(JJ)V", (void*)nObjDestroy }, 1664 1665{"rsnElementCreate", "(JJIZI)J", (void*)nElementCreate }, 1666{"rsnElementCreate2", "(J[J[Ljava/lang/String;[I)J", (void*)nElementCreate2 }, 1667{"rsnElementGetSubElements", "(JJ[J[Ljava/lang/String;[I)V", (void*)nElementGetSubElements }, 1668 1669{"rsnTypeCreate", "(JJIIIZZI)J", (void*)nTypeCreate }, 1670 1671{"rsnAllocationCreateTyped", "(JJIIJ)J", (void*)nAllocationCreateTyped }, 1672{"rsnAllocationCreateFromBitmap", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateFromBitmap }, 1673{"rsnAllocationCreateBitmapBackedAllocation", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateBitmapBackedAllocation }, 1674{"rsnAllocationCubeCreateFromBitmap","(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCubeCreateFromBitmap }, 1675 1676{"rsnAllocationCopyFromBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyFromBitmap }, 1677{"rsnAllocationCopyToBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyToBitmap }, 1678 1679{"rsnAllocationSyncAll", "(JJI)V", (void*)nAllocationSyncAll }, 1680{"rsnAllocationSetSurface", "(JJLandroid/view/Surface;)V", (void*)nAllocationSetSurface }, 1681{"rsnAllocationIoSend", "(JJ)V", (void*)nAllocationIoSend }, 1682{"rsnAllocationData1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData1D }, 1683{"rsnAllocationElementData1D", "(JJIII[BI)V", (void*)nAllocationElementData1D }, 1684//{"rsnAllocationElementData", "(JJIIIII[BI)V", (void*)nAllocationElementData }, 1685{"rsnAllocationData2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData2D }, 1686{"rsnAllocationData2D", "(JJIIIIIIJIIII)V", (void*)nAllocationData2D_alloc }, 1687{"rsnAllocationData3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData3D }, 1688{"rsnAllocationData3D", "(JJIIIIIIIJIIII)V", (void*)nAllocationData3D_alloc }, 1689{"rsnAllocationRead", "(JJLjava/lang/Object;IIZ)V", (void*)nAllocationRead }, 1690{"rsnAllocationRead1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead1D }, 1691//{"rsnAllocationElementRead", "(JJIIIII[BI)V", (void*)nAllocationElementRead }, 1692{"rsnAllocationRead2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead2D }, 1693//{"rsnAllocationRead3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead3D }, 1694{"rsnAllocationGetType", "(JJ)J", (void*)nAllocationGetType}, 1695{"rsnAllocationResize1D", "(JJI)V", (void*)nAllocationResize1D }, 1696{"rsnAllocationGenerateMipmaps", "(JJ)V", (void*)nAllocationGenerateMipmaps }, 1697 1698{"rsnScriptBindAllocation", "(JJJIZ)V", (void*)nScriptBindAllocation }, 1699{"rsnScriptSetTimeZone", "(JJ[BZ)V", (void*)nScriptSetTimeZone }, 1700{"rsnScriptInvoke", "(JJIZ)V", (void*)nScriptInvoke }, 1701{"rsnScriptInvokeV", "(JJI[BZ)V", (void*)nScriptInvokeV }, 1702{"rsnScriptForEach", "(JJJIJJZ)V", (void*)nScriptForEach }, 1703{"rsnScriptForEach", "(JJJIJJ[BZ)V", (void*)nScriptForEachV }, 1704{"rsnScriptForEachClipped", "(JJJIJJIIIIIIZ)V", (void*)nScriptForEachClipped }, 1705{"rsnScriptForEachClipped", "(JJJIJJ[BIIIIIIZ)V", (void*)nScriptForEachClippedV }, 1706{"rsnScriptSetVarI", "(JJIIZ)V", (void*)nScriptSetVarI }, 1707{"rsnScriptSetVarJ", "(JJIJZ)V", (void*)nScriptSetVarJ }, 1708{"rsnScriptSetVarF", "(JJIFZ)V", (void*)nScriptSetVarF }, 1709{"rsnScriptSetVarD", "(JJIDZ)V", (void*)nScriptSetVarD }, 1710{"rsnScriptSetVarV", "(JJI[BZ)V", (void*)nScriptSetVarV }, 1711{"rsnScriptSetVarVE", "(JJI[BJ[IZ)V", (void*)nScriptSetVarVE }, 1712{"rsnScriptSetVarObj", "(JJIJZ)V", (void*)nScriptSetVarObj }, 1713 1714{"rsnScriptCCreate", "(JLjava/lang/String;Ljava/lang/String;[BI)J", (void*)nScriptCCreate }, 1715{"rsnScriptIntrinsicCreate", "(JIJZ)J", (void*)nScriptIntrinsicCreate }, 1716{"rsnScriptKernelIDCreate", "(JJIIZ)J", (void*)nScriptKernelIDCreate }, 1717{"rsnScriptInvokeIDCreate", "(JJI)J", (void*)nScriptInvokeIDCreate }, 1718{"rsnScriptFieldIDCreate", "(JJIZ)J", (void*)nScriptFieldIDCreate }, 1719{"rsnScriptGroupCreate", "(J[J[J[J[J[J)J", (void*)nScriptGroupCreate }, 1720{"rsnScriptGroup2Create", "(JLjava/lang/String;[J)J", (void*)nScriptGroup2Create }, 1721{"rsnScriptGroupSetInput", "(JJJJ)V", (void*)nScriptGroupSetInput }, 1722{"rsnScriptGroupSetOutput", "(JJJJ)V", (void*)nScriptGroupSetOutput }, 1723{"rsnScriptGroupExecute", "(JJ)V", (void*)nScriptGroupExecute }, 1724{"rsnScriptGroup2Execute", "(JJ)V", (void*)nScriptGroup2Execute }, 1725 1726{"rsnSamplerCreate", "(JIIIIIF)J", (void*)nSamplerCreate }, 1727 1728{"rsnSystemGetPointerSize", "()I", (void*)nSystemGetPointerSize }, 1729 1730// Entry points for Inc libRSSupport 1731{"nIncLoadSO", "()Z", (bool*)nIncLoadSO }, 1732{"nIncDeviceCreate", "()J", (void*)nIncDeviceCreate }, 1733{"nIncDeviceDestroy", "(J)V", (void*)nIncDeviceDestroy }, 1734{"rsnIncContextCreate", "(JIII)J", (void*)nIncContextCreate }, 1735{"rsnIncContextFinish", "(J)V", (void*)nIncContextFinish }, 1736{"rsnIncContextDestroy", "(J)V", (void*)nIncContextDestroy }, 1737{"rsnIncObjDestroy", "(JJ)V", (void*)nIncObjDestroy }, 1738{"rsnIncElementCreate", "(JJIZI)J", (void*)nIncElementCreate }, 1739{"rsnIncTypeCreate", "(JJIIIZZI)J", (void*)nIncTypeCreate }, 1740{"rsnIncAllocationCreateTyped", "(JJJJ)J", (void*)nIncAllocationCreateTyped }, 1741}; 1742 1743// --------------------------------------------------------------------------- 1744 1745jint JNI_OnLoad(JavaVM* vm, void* reserved) 1746{ 1747 JNIEnv* env = NULL; 1748 jclass clazz = NULL; 1749 jint result = -1; 1750 1751 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) { 1752 // __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, 1753 // "ERROR: GetEnv failed\n"); 1754 goto bail; 1755 } 1756 if (env == NULL) { 1757 // __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "ERROR: env == NULL"); 1758 goto bail; 1759 } 1760 1761 clazz = env->FindClass(classPathName); 1762 if (clazz == NULL) { 1763 goto bail; 1764 } 1765 1766 if (env->RegisterNatives(clazz, methods, NELEM(methods)) < 0) { 1767 // __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, 1768 // "ERROR: MediaPlayer native registration failed\n"); 1769 goto bail; 1770 } 1771 1772 /* success -- return valid version number */ 1773 result = JNI_VERSION_1_4; 1774 1775bail: 1776 return result; 1777} 1778