android_renderscript_RenderScript.cpp 104 KB

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