android_renderscript_RenderScript.cpp revision b2915245b74b3b5541b123e38403f8e26426b4b7
1/*
2 * Copyright (C) 2011-2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "libRS_jni"
18
19#include <stdlib.h>
20#include <stdio.h>
21#include <fcntl.h>
22#include <unistd.h>
23#include <math.h>
24#include <utils/misc.h>
25#include <inttypes.h>
26
27#include <androidfw/Asset.h>
28#include <androidfw/AssetManager.h>
29#include <androidfw/ResourceTypes.h>
30
31#include "jni.h"
32#include "JNIHelp.h"
33#include "android_runtime/AndroidRuntime.h"
34#include "android_runtime/android_view_Surface.h"
35#include "android_runtime/android_util_AssetManager.h"
36#include "android/graphics/GraphicsJNI.h"
37
38#include <rs.h>
39#include <rsEnv.h>
40#include <gui/Surface.h>
41#include <gui/GLConsumer.h>
42#include <gui/Surface.h>
43#include <android_runtime/android_graphics_SurfaceTexture.h>
44
45//#define LOG_API ALOGE
46static constexpr bool kLogApi = false;
47
48using namespace android;
49
50template <typename... T>
51void UNUSED(T... t) {}
52
53#define PER_ARRAY_TYPE(flag, fnc, readonly, ...) {                                      \
54    jint len = 0;                                                                       \
55    void *ptr = nullptr;                                                                \
56    size_t typeBytes = 0;                                                               \
57    jint relFlag = 0;                                                                   \
58    if (readonly) {                                                                     \
59        /* The on-release mode should only be JNI_ABORT for read-only accesses. */      \
60        relFlag = JNI_ABORT;                                                            \
61    }                                                                                   \
62    switch(dataType) {                                                                  \
63    case RS_TYPE_FLOAT_32:                                                              \
64        len = _env->GetArrayLength((jfloatArray)data);                                  \
65        ptr = _env->GetFloatArrayElements((jfloatArray)data, flag);                     \
66        typeBytes = 4;                                                                  \
67        fnc(__VA_ARGS__);                                                               \
68        _env->ReleaseFloatArrayElements((jfloatArray)data, (jfloat *)ptr, relFlag);     \
69        return;                                                                         \
70    case RS_TYPE_FLOAT_64:                                                              \
71        len = _env->GetArrayLength((jdoubleArray)data);                                 \
72        ptr = _env->GetDoubleArrayElements((jdoubleArray)data, flag);                   \
73        typeBytes = 8;                                                                  \
74        fnc(__VA_ARGS__);                                                               \
75        _env->ReleaseDoubleArrayElements((jdoubleArray)data, (jdouble *)ptr, relFlag);  \
76        return;                                                                         \
77    case RS_TYPE_SIGNED_8:                                                              \
78    case RS_TYPE_UNSIGNED_8:                                                            \
79        len = _env->GetArrayLength((jbyteArray)data);                                   \
80        ptr = _env->GetByteArrayElements((jbyteArray)data, flag);                       \
81        typeBytes = 1;                                                                  \
82        fnc(__VA_ARGS__);                                                               \
83        _env->ReleaseByteArrayElements((jbyteArray)data, (jbyte*)ptr, relFlag);         \
84        return;                                                                         \
85    case RS_TYPE_SIGNED_16:                                                             \
86    case RS_TYPE_UNSIGNED_16:                                                           \
87        len = _env->GetArrayLength((jshortArray)data);                                  \
88        ptr = _env->GetShortArrayElements((jshortArray)data, flag);                     \
89        typeBytes = 2;                                                                  \
90        fnc(__VA_ARGS__);                                                               \
91        _env->ReleaseShortArrayElements((jshortArray)data, (jshort *)ptr, relFlag);     \
92        return;                                                                         \
93    case RS_TYPE_SIGNED_32:                                                             \
94    case RS_TYPE_UNSIGNED_32:                                                           \
95        len = _env->GetArrayLength((jintArray)data);                                    \
96        ptr = _env->GetIntArrayElements((jintArray)data, flag);                         \
97        typeBytes = 4;                                                                  \
98        fnc(__VA_ARGS__);                                                               \
99        _env->ReleaseIntArrayElements((jintArray)data, (jint *)ptr, relFlag);           \
100        return;                                                                         \
101    case RS_TYPE_SIGNED_64:                                                             \
102    case RS_TYPE_UNSIGNED_64:                                                           \
103        len = _env->GetArrayLength((jlongArray)data);                                   \
104        ptr = _env->GetLongArrayElements((jlongArray)data, flag);                       \
105        typeBytes = 8;                                                                  \
106        fnc(__VA_ARGS__);                                                               \
107        _env->ReleaseLongArrayElements((jlongArray)data, (jlong *)ptr, relFlag);        \
108        return;                                                                         \
109    default:                                                                            \
110        break;                                                                          \
111    }                                                                                   \
112    UNUSED(len, ptr, typeBytes, relFlag);                                               \
113}
114
115
116class AutoJavaStringToUTF8 {
117public:
118    AutoJavaStringToUTF8(JNIEnv* env, jstring str) : fEnv(env), fJStr(str) {
119        fCStr = env->GetStringUTFChars(str, nullptr);
120        fLength = env->GetStringUTFLength(str);
121    }
122    ~AutoJavaStringToUTF8() {
123        fEnv->ReleaseStringUTFChars(fJStr, fCStr);
124    }
125    const char* c_str() const { return fCStr; }
126    jsize length() const { return fLength; }
127
128private:
129    JNIEnv*     fEnv;
130    jstring     fJStr;
131    const char* fCStr;
132    jsize       fLength;
133};
134
135class AutoJavaStringArrayToUTF8 {
136public:
137    AutoJavaStringArrayToUTF8(JNIEnv* env, jobjectArray strings, jsize stringsLength)
138    : mEnv(env), mStrings(strings), mStringsLength(stringsLength) {
139        mCStrings = nullptr;
140        mSizeArray = nullptr;
141        if (stringsLength > 0) {
142            mCStrings = (const char **)calloc(stringsLength, sizeof(char *));
143            mSizeArray = (size_t*)calloc(stringsLength, sizeof(size_t));
144            for (jsize ct = 0; ct < stringsLength; ct ++) {
145                jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
146                mCStrings[ct] = mEnv->GetStringUTFChars(s, nullptr);
147                mSizeArray[ct] = mEnv->GetStringUTFLength(s);
148            }
149        }
150    }
151    ~AutoJavaStringArrayToUTF8() {
152        for (jsize ct=0; ct < mStringsLength; ct++) {
153            jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
154            mEnv->ReleaseStringUTFChars(s, mCStrings[ct]);
155        }
156        free(mCStrings);
157        free(mSizeArray);
158    }
159    const char **c_str() const { return mCStrings; }
160    size_t *c_str_len() const { return mSizeArray; }
161    jsize length() const { return mStringsLength; }
162
163private:
164    JNIEnv      *mEnv;
165    jobjectArray mStrings;
166    const char **mCStrings;
167    size_t      *mSizeArray;
168    jsize        mStringsLength;
169};
170
171// ---------------------------------------------------------------------------
172
173static jfieldID gContextId = 0;
174
175static void _nInit(JNIEnv *_env, jclass _this)
176{
177    gContextId             = _env->GetFieldID(_this, "mContext", "J");
178}
179
180// ---------------------------------------------------------------------------
181
182static void
183nContextFinish(JNIEnv *_env, jobject _this, jlong con)
184{
185    if (kLogApi) {
186        ALOGD("nContextFinish, con(%p)", (RsContext)con);
187    }
188    rsContextFinish((RsContext)con);
189}
190
191static jlong
192nClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong kernelID,
193               jlong returnValue, jlongArray fieldIDArray,
194               jlongArray valueArray, jintArray sizeArray,
195               jlongArray depClosureArray, jlongArray depFieldIDArray) {
196  jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
197  jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
198  RsScriptFieldID* fieldIDs =
199      (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * fieldIDs_length);
200  for (int i = 0; i< fieldIDs_length; i++) {
201    fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
202  }
203
204  jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
205  jsize values_length = _env->GetArrayLength(valueArray);
206  uintptr_t* values = (uintptr_t*)alloca(sizeof(uintptr_t) * values_length);
207  for (int i = 0; i < values_length; i++) {
208    values[i] = (uintptr_t)jValues[i];
209  }
210
211  jint* sizes = _env->GetIntArrayElements(sizeArray, nullptr);
212  jsize sizes_length = _env->GetArrayLength(sizeArray);
213
214  jlong* jDepClosures =
215      _env->GetLongArrayElements(depClosureArray, nullptr);
216  jsize depClosures_length = _env->GetArrayLength(depClosureArray);
217  RsClosure* depClosures =
218      (RsClosure*)alloca(sizeof(RsClosure) * depClosures_length);
219  for (int i = 0; i < depClosures_length; i++) {
220    depClosures[i] = (RsClosure)jDepClosures[i];
221  }
222
223  jlong* jDepFieldIDs =
224      _env->GetLongArrayElements(depFieldIDArray, nullptr);
225  jsize depFieldIDs_length = _env->GetArrayLength(depFieldIDArray);
226  RsScriptFieldID* depFieldIDs =
227      (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * depFieldIDs_length);
228  for (int i = 0; i < depClosures_length; i++) {
229    depFieldIDs[i] = (RsClosure)jDepFieldIDs[i];
230  }
231
232  return (jlong)(uintptr_t)rsClosureCreate(
233      (RsContext)con, (RsScriptKernelID)kernelID, (RsAllocation)returnValue,
234      fieldIDs, (size_t)fieldIDs_length, values, (size_t)values_length,
235      (size_t*)sizes, (size_t)sizes_length,
236      depClosures, (size_t)depClosures_length,
237      depFieldIDs, (size_t)depFieldIDs_length);
238}
239
240static jlong
241nInvokeClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong invokeID,
242                     jbyteArray paramArray, jlongArray fieldIDArray, jlongArray valueArray,
243                     jintArray sizeArray) {
244  jbyte* jParams = _env->GetByteArrayElements(paramArray, nullptr);
245  jsize jParamLength = _env->GetArrayLength(paramArray);
246
247  jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
248  jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
249  RsScriptFieldID* fieldIDs =
250      (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * fieldIDs_length);
251  for (int i = 0; i< fieldIDs_length; i++) {
252    fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
253  }
254
255  jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
256  jsize values_length = _env->GetArrayLength(valueArray);
257  uintptr_t* values = (uintptr_t*)alloca(sizeof(uintptr_t) * values_length);
258  for (int i = 0; i < values_length; i++) {
259    values[i] = (uintptr_t)jValues[i];
260  }
261
262  jint* sizes = _env->GetIntArrayElements(sizeArray, nullptr);
263  jsize sizes_length = _env->GetArrayLength(sizeArray);
264
265  return (jlong)(uintptr_t)rsInvokeClosureCreate(
266      (RsContext)con, (RsScriptInvokeID)invokeID, jParams, jParamLength,
267      fieldIDs, (size_t)fieldIDs_length, values, (size_t)values_length,
268      (size_t*)sizes, (size_t)sizes_length);
269}
270
271static void
272nClosureSetArg(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
273               jint index, jlong value, jint size) {
274  rsClosureSetArg((RsContext)con, (RsClosure)closureID, (uint32_t)index,
275                  (uintptr_t)value, (size_t)size);
276}
277
278static void
279nClosureSetGlobal(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
280                  jlong fieldID, jlong value, jint size) {
281  rsClosureSetGlobal((RsContext)con, (RsClosure)closureID,
282                     (RsScriptFieldID)fieldID, (uintptr_t)value, (size_t)size);
283}
284
285static long
286nScriptGroup2Create(JNIEnv *_env, jobject _this, jlong con,
287                    jstring cacheDir, jlongArray closureArray) {
288  AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
289
290  jlong* jClosures = _env->GetLongArrayElements(closureArray, nullptr);
291  jsize numClosures = _env->GetArrayLength(closureArray);
292  RsClosure* closures = (RsClosure*)alloca(sizeof(RsClosure) * numClosures);
293  for (int i = 0; i < numClosures; i++) {
294    closures[i] = (RsClosure)jClosures[i];
295  }
296
297  return (jlong)(uintptr_t)rsScriptGroup2Create(
298      (RsContext)con, cacheDirUTF.c_str(), cacheDirUTF.length(),
299      closures, numClosures);
300}
301
302static void
303nScriptGroup2Execute(JNIEnv *_env, jobject _this, jlong con, jlong groupID) {
304  rsScriptGroupExecute((RsContext)con, (RsScriptGroup2)groupID);
305}
306
307static void
308nScriptIntrinsicBLAS_Single(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
309                            jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
310                            jfloat alpha, jlong A, jlong B, jfloat beta, jlong C, jint incX, jint incY,
311                            jint KL, jint KU) {
312    RsBlasCall call;
313    memset(&call, 0, sizeof(call));
314    call.func = (RsBlasFunction)func;
315    call.transA = (RsBlasTranspose)TransA;
316    call.transB = (RsBlasTranspose)TransB;
317    call.side = (RsBlasSide)Side;
318    call.uplo = (RsBlasUplo)Uplo;
319    call.diag = (RsBlasDiag)Diag;
320    call.M = M;
321    call.N = N;
322    call.K = K;
323    call.alpha.f = alpha;
324    call.beta.f = beta;
325    call.incX = incX;
326    call.incY = incY;
327    call.KL = KL;
328    call.KU = KU;
329
330    RsAllocation in_allocs[3];
331    in_allocs[0] = (RsAllocation)A;
332    in_allocs[1] = (RsAllocation)B;
333    in_allocs[2] = (RsAllocation)C;
334
335    rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
336                         in_allocs, sizeof(in_allocs), nullptr,
337                         &call, sizeof(call), nullptr, 0);
338}
339
340static void
341nScriptIntrinsicBLAS_Double(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
342                            jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
343                            jdouble alpha, jlong A, jlong B, jdouble beta, jlong C, jint incX, jint incY,
344                            jint KL, jint KU) {
345    RsBlasCall call;
346    memset(&call, 0, sizeof(call));
347    call.func = (RsBlasFunction)func;
348    call.transA = (RsBlasTranspose)TransA;
349    call.transB = (RsBlasTranspose)TransB;
350    call.side = (RsBlasSide)Side;
351    call.uplo = (RsBlasUplo)Uplo;
352    call.diag = (RsBlasDiag)Diag;
353    call.M = M;
354    call.N = N;
355    call.K = K;
356    call.alpha.d = alpha;
357    call.beta.d = beta;
358    call.incX = incX;
359    call.incY = incY;
360    call.KL = KL;
361    call.KU = KU;
362
363    RsAllocation in_allocs[3];
364    in_allocs[0] = (RsAllocation)A;
365    in_allocs[1] = (RsAllocation)B;
366    in_allocs[2] = (RsAllocation)C;
367
368    rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
369                         in_allocs, sizeof(in_allocs), nullptr,
370                         &call, sizeof(call), nullptr, 0);
371}
372
373static void
374nScriptIntrinsicBLAS_Complex(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
375                             jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
376                             jfloat alphaX, jfloat alphaY, jlong A, jlong B, jfloat betaX,
377                             jfloat betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
378    RsBlasCall call;
379    memset(&call, 0, sizeof(call));
380    call.func = (RsBlasFunction)func;
381    call.transA = (RsBlasTranspose)TransA;
382    call.transB = (RsBlasTranspose)TransB;
383    call.side = (RsBlasSide)Side;
384    call.uplo = (RsBlasUplo)Uplo;
385    call.diag = (RsBlasDiag)Diag;
386    call.M = M;
387    call.N = N;
388    call.K = K;
389    call.alpha.c.r = alphaX;
390    call.alpha.c.i = alphaY;
391    call.beta.c.r = betaX;
392    call.beta.c.r = betaY;
393    call.incX = incX;
394    call.incY = incY;
395    call.KL = KL;
396    call.KU = KU;
397
398    RsAllocation in_allocs[3];
399    in_allocs[0] = (RsAllocation)A;
400    in_allocs[1] = (RsAllocation)B;
401    in_allocs[2] = (RsAllocation)C;
402
403    rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
404                         in_allocs, sizeof(in_allocs), nullptr,
405                         &call, sizeof(call), nullptr, 0);
406}
407
408static void
409nScriptIntrinsicBLAS_Z(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
410                       jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
411                       jdouble alphaX, jdouble alphaY, jlong A, jlong B, jdouble betaX,
412                       jdouble betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
413    RsBlasCall call;
414    memset(&call, 0, sizeof(call));
415    call.func = (RsBlasFunction)func;
416    call.transA = (RsBlasTranspose)TransA;
417    call.transB = (RsBlasTranspose)TransB;
418    call.side = (RsBlasSide)Side;
419    call.uplo = (RsBlasUplo)Uplo;
420    call.diag = (RsBlasDiag)Diag;
421    call.M = M;
422    call.N = N;
423    call.K = K;
424    call.alpha.z.r = alphaX;
425    call.alpha.z.i = alphaY;
426    call.beta.z.r = betaX;
427    call.beta.z.r = betaY;
428    call.incX = incX;
429    call.incY = incY;
430    call.KL = KL;
431    call.KU = KU;
432
433    RsAllocation in_allocs[3];
434    in_allocs[0] = (RsAllocation)A;
435    in_allocs[1] = (RsAllocation)B;
436    in_allocs[2] = (RsAllocation)C;
437
438    rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
439                         in_allocs, sizeof(in_allocs), nullptr,
440                         &call, sizeof(call), nullptr, 0);
441}
442
443
444static void
445nAssignName(JNIEnv *_env, jobject _this, jlong con, jlong obj, jbyteArray str)
446{
447    if (kLogApi) {
448        ALOGD("nAssignName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
449    }
450    jint len = _env->GetArrayLength(str);
451    jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
452    rsAssignName((RsContext)con, (void *)obj, (const char *)cptr, len);
453    _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
454}
455
456static jstring
457nGetName(JNIEnv *_env, jobject _this, jlong con, jlong obj)
458{
459    if (kLogApi) {
460        ALOGD("nGetName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
461    }
462    const char *name = nullptr;
463    rsaGetName((RsContext)con, (void *)obj, &name);
464    if(name == nullptr || strlen(name) == 0) {
465        return nullptr;
466    }
467    return _env->NewStringUTF(name);
468}
469
470static void
471nObjDestroy(JNIEnv *_env, jobject _this, jlong con, jlong obj)
472{
473    if (kLogApi) {
474        ALOGD("nObjDestroy, con(%p) obj(%p)", (RsContext)con, (void *)obj);
475    }
476    rsObjDestroy((RsContext)con, (void *)obj);
477}
478
479// ---------------------------------------------------------------------------
480
481static jlong
482nDeviceCreate(JNIEnv *_env, jobject _this)
483{
484    if (kLogApi) {
485        ALOGD("nDeviceCreate");
486    }
487    return (jlong)(uintptr_t)rsDeviceCreate();
488}
489
490static void
491nDeviceDestroy(JNIEnv *_env, jobject _this, jlong dev)
492{
493    if (kLogApi) {
494        ALOGD("nDeviceDestroy");
495    }
496    return rsDeviceDestroy((RsDevice)dev);
497}
498
499static void
500nDeviceSetConfig(JNIEnv *_env, jobject _this, jlong dev, jint p, jint value)
501{
502    if (kLogApi) {
503        ALOGD("nDeviceSetConfig  dev(%p), param(%i), value(%i)", (void *)dev, p, value);
504    }
505    return rsDeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value);
506}
507
508static jlong
509nContextCreate(JNIEnv *_env, jobject _this, jlong dev, jint flags, jint sdkVer, jint contextType)
510{
511    if (kLogApi) {
512        ALOGD("nContextCreate");
513    }
514    return (jlong)(uintptr_t)rsContextCreate((RsDevice)dev, 0, sdkVer, (RsContextType)contextType, flags);
515}
516
517static jlong
518nContextCreateGL(JNIEnv *_env, jobject _this, jlong dev, jint ver, jint sdkVer,
519                 jint colorMin, jint colorPref,
520                 jint alphaMin, jint alphaPref,
521                 jint depthMin, jint depthPref,
522                 jint stencilMin, jint stencilPref,
523                 jint samplesMin, jint samplesPref, jfloat samplesQ,
524                 jint dpi)
525{
526    RsSurfaceConfig sc;
527    sc.alphaMin = alphaMin;
528    sc.alphaPref = alphaPref;
529    sc.colorMin = colorMin;
530    sc.colorPref = colorPref;
531    sc.depthMin = depthMin;
532    sc.depthPref = depthPref;
533    sc.samplesMin = samplesMin;
534    sc.samplesPref = samplesPref;
535    sc.samplesQ = samplesQ;
536
537    if (kLogApi) {
538        ALOGD("nContextCreateGL");
539    }
540    return (jlong)(uintptr_t)rsContextCreateGL((RsDevice)dev, ver, sdkVer, sc, dpi);
541}
542
543static void
544nContextSetPriority(JNIEnv *_env, jobject _this, jlong con, jint p)
545{
546    if (kLogApi) {
547        ALOGD("ContextSetPriority, con(%p), priority(%i)", (RsContext)con, p);
548    }
549    rsContextSetPriority((RsContext)con, p);
550}
551
552
553
554static void
555nContextSetSurface(JNIEnv *_env, jobject _this, jlong con, jint width, jint height, jobject wnd)
556{
557    if (kLogApi) {
558        ALOGD("nContextSetSurface, con(%p), width(%i), height(%i), surface(%p)", (RsContext)con,
559              width, height, (Surface *)wnd);
560    }
561
562    ANativeWindow * window = nullptr;
563    if (wnd == nullptr) {
564
565    } else {
566        window = android_view_Surface_getNativeWindow(_env, wnd).get();
567    }
568
569    rsContextSetSurface((RsContext)con, width, height, window);
570}
571
572static void
573nContextDestroy(JNIEnv *_env, jobject _this, jlong con)
574{
575    if (kLogApi) {
576        ALOGD("nContextDestroy, con(%p)", (RsContext)con);
577    }
578    rsContextDestroy((RsContext)con);
579}
580
581static void
582nContextDump(JNIEnv *_env, jobject _this, jlong con, jint bits)
583{
584    if (kLogApi) {
585        ALOGD("nContextDump, con(%p)  bits(%i)", (RsContext)con, bits);
586    }
587    rsContextDump((RsContext)con, bits);
588}
589
590static void
591nContextPause(JNIEnv *_env, jobject _this, jlong con)
592{
593    if (kLogApi) {
594        ALOGD("nContextPause, con(%p)", (RsContext)con);
595    }
596    rsContextPause((RsContext)con);
597}
598
599static void
600nContextResume(JNIEnv *_env, jobject _this, jlong con)
601{
602    if (kLogApi) {
603        ALOGD("nContextResume, con(%p)", (RsContext)con);
604    }
605    rsContextResume((RsContext)con);
606}
607
608
609static jstring
610nContextGetErrorMessage(JNIEnv *_env, jobject _this, jlong con)
611{
612    if (kLogApi) {
613        ALOGD("nContextGetErrorMessage, con(%p)", (RsContext)con);
614    }
615    char buf[1024];
616
617    size_t receiveLen;
618    uint32_t subID;
619    int id = rsContextGetMessage((RsContext)con,
620                                 buf, sizeof(buf),
621                                 &receiveLen, sizeof(receiveLen),
622                                 &subID, sizeof(subID));
623    if (!id && receiveLen) {
624        ALOGV("message receive buffer too small.  %zu", receiveLen);
625    }
626    return _env->NewStringUTF(buf);
627}
628
629static jint
630nContextGetUserMessage(JNIEnv *_env, jobject _this, jlong con, jintArray data)
631{
632    jint len = _env->GetArrayLength(data);
633    if (kLogApi) {
634        ALOGD("nContextGetMessage, con(%p), len(%i)", (RsContext)con, len);
635    }
636    jint *ptr = _env->GetIntArrayElements(data, nullptr);
637    size_t receiveLen;
638    uint32_t subID;
639    int id = rsContextGetMessage((RsContext)con,
640                                 ptr, len * 4,
641                                 &receiveLen, sizeof(receiveLen),
642                                 &subID, sizeof(subID));
643    if (!id && receiveLen) {
644        ALOGV("message receive buffer too small.  %zu", receiveLen);
645    }
646    _env->ReleaseIntArrayElements(data, ptr, 0);
647    return (jint)id;
648}
649
650static jint
651nContextPeekMessage(JNIEnv *_env, jobject _this, jlong con, jintArray auxData)
652{
653    if (kLogApi) {
654        ALOGD("nContextPeekMessage, con(%p)", (RsContext)con);
655    }
656    jint *auxDataPtr = _env->GetIntArrayElements(auxData, nullptr);
657    size_t receiveLen;
658    uint32_t subID;
659    int id = rsContextPeekMessage((RsContext)con, &receiveLen, sizeof(receiveLen),
660                                  &subID, sizeof(subID));
661    auxDataPtr[0] = (jint)subID;
662    auxDataPtr[1] = (jint)receiveLen;
663    _env->ReleaseIntArrayElements(auxData, auxDataPtr, 0);
664    return (jint)id;
665}
666
667static void nContextInitToClient(JNIEnv *_env, jobject _this, jlong con)
668{
669    if (kLogApi) {
670        ALOGD("nContextInitToClient, con(%p)", (RsContext)con);
671    }
672    rsContextInitToClient((RsContext)con);
673}
674
675static void nContextDeinitToClient(JNIEnv *_env, jobject _this, jlong con)
676{
677    if (kLogApi) {
678        ALOGD("nContextDeinitToClient, con(%p)", (RsContext)con);
679    }
680    rsContextDeinitToClient((RsContext)con);
681}
682
683static void
684nContextSendMessage(JNIEnv *_env, jobject _this, jlong con, jint id, jintArray data)
685{
686    jint *ptr = nullptr;
687    jint len = 0;
688    if (data) {
689        len = _env->GetArrayLength(data);
690        ptr = _env->GetIntArrayElements(data, nullptr);
691    }
692    if (kLogApi) {
693        ALOGD("nContextSendMessage, con(%p), id(%i), len(%i)", (RsContext)con, id, len);
694    }
695    rsContextSendMessage((RsContext)con, id, (const uint8_t *)ptr, len * sizeof(int));
696    if (data) {
697        _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
698    }
699}
700
701
702
703static jlong
704nElementCreate(JNIEnv *_env, jobject _this, jlong con, jlong type, jint kind, jboolean norm,
705               jint size)
706{
707    if (kLogApi) {
708        ALOGD("nElementCreate, con(%p), type(%" PRId64 "), kind(%i), norm(%i), size(%i)", (RsContext)con,
709              type, kind, norm, size);
710    }
711    return (jlong)(uintptr_t)rsElementCreate((RsContext)con, (RsDataType)type, (RsDataKind)kind,
712                                             norm, size);
713}
714
715static jlong
716nElementCreate2(JNIEnv *_env, jobject _this, jlong con,
717                jlongArray _ids, jobjectArray _names, jintArray _arraySizes)
718{
719    int fieldCount = _env->GetArrayLength(_ids);
720    if (kLogApi) {
721        ALOGD("nElementCreate2, con(%p)", (RsContext)con);
722    }
723
724    jlong *jIds = _env->GetLongArrayElements(_ids, nullptr);
725    jint *jArraySizes = _env->GetIntArrayElements(_arraySizes, nullptr);
726
727    RsElement *ids = (RsElement*)malloc(fieldCount * sizeof(RsElement));
728    uint32_t *arraySizes = (uint32_t *)malloc(fieldCount * sizeof(uint32_t));
729
730    for(int i = 0; i < fieldCount; i ++) {
731        ids[i] = (RsElement)jIds[i];
732        arraySizes[i] = (uint32_t)jArraySizes[i];
733    }
734
735    AutoJavaStringArrayToUTF8 names(_env, _names, fieldCount);
736
737    const char **nameArray = names.c_str();
738    size_t *sizeArray = names.c_str_len();
739
740    jlong id = (jlong)(uintptr_t)rsElementCreate2((RsContext)con,
741                                     (const RsElement *)ids, fieldCount,
742                                     nameArray, fieldCount * sizeof(size_t),  sizeArray,
743                                     (const uint32_t *)arraySizes, fieldCount);
744
745    free(ids);
746    free(arraySizes);
747    _env->ReleaseLongArrayElements(_ids, jIds, JNI_ABORT);
748    _env->ReleaseIntArrayElements(_arraySizes, jArraySizes, JNI_ABORT);
749
750    return (jlong)(uintptr_t)id;
751}
752
753static void
754nElementGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jintArray _elementData)
755{
756    int dataSize = _env->GetArrayLength(_elementData);
757    if (kLogApi) {
758        ALOGD("nElementGetNativeData, con(%p)", (RsContext)con);
759    }
760
761    // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
762    assert(dataSize == 5);
763
764    uintptr_t elementData[5];
765    rsaElementGetNativeData((RsContext)con, (RsElement)id, elementData, dataSize);
766
767    for(jint i = 0; i < dataSize; i ++) {
768        const jint data = (jint)elementData[i];
769        _env->SetIntArrayRegion(_elementData, i, 1, &data);
770    }
771}
772
773
774static void
775nElementGetSubElements(JNIEnv *_env, jobject _this, jlong con, jlong id,
776                       jlongArray _IDs,
777                       jobjectArray _names,
778                       jintArray _arraySizes)
779{
780    uint32_t dataSize = _env->GetArrayLength(_IDs);
781    if (kLogApi) {
782        ALOGD("nElementGetSubElements, con(%p)", (RsContext)con);
783    }
784
785    uintptr_t *ids = (uintptr_t*)malloc(dataSize * sizeof(uintptr_t));
786    const char **names = (const char **)malloc(dataSize * sizeof(const char *));
787    uint32_t *arraySizes = (uint32_t *)malloc(dataSize * sizeof(uint32_t));
788
789    rsaElementGetSubElements((RsContext)con, (RsElement)id, ids, names, arraySizes,
790                             (uint32_t)dataSize);
791
792    for(uint32_t i = 0; i < dataSize; i++) {
793        const jlong id = (jlong)(uintptr_t)ids[i];
794        const jint arraySize = (jint)arraySizes[i];
795        _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
796        _env->SetLongArrayRegion(_IDs, i, 1, &id);
797        _env->SetIntArrayRegion(_arraySizes, i, 1, &arraySize);
798    }
799
800    free(ids);
801    free(names);
802    free(arraySizes);
803}
804
805// -----------------------------------
806
807static jlong
808nTypeCreate(JNIEnv *_env, jobject _this, jlong con, jlong eid,
809            jint dimx, jint dimy, jint dimz, jboolean mips, jboolean faces, jint yuv)
810{
811    if (kLogApi) {
812        ALOGD("nTypeCreate, con(%p) eid(%p), x(%i), y(%i), z(%i), mips(%i), faces(%i), yuv(%i)",
813              (RsContext)con, (void*)eid, dimx, dimy, dimz, mips, faces, yuv);
814    }
815
816    return (jlong)(uintptr_t)rsTypeCreate((RsContext)con, (RsElement)eid, dimx, dimy, dimz, mips,
817                                          faces, yuv);
818}
819
820static void
821nTypeGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jlongArray _typeData)
822{
823    // We are packing 6 items: mDimX; mDimY; mDimZ;
824    // mDimLOD; mDimFaces; mElement; into typeData
825    int elementCount = _env->GetArrayLength(_typeData);
826
827    assert(elementCount == 6);
828    if (kLogApi) {
829        ALOGD("nTypeGetNativeData, con(%p)", (RsContext)con);
830    }
831
832    uintptr_t typeData[6];
833    rsaTypeGetNativeData((RsContext)con, (RsType)id, typeData, 6);
834
835    for(jint i = 0; i < elementCount; i ++) {
836        const jlong data = (jlong)(uintptr_t)typeData[i];
837        _env->SetLongArrayRegion(_typeData, i, 1, &data);
838    }
839}
840
841// -----------------------------------
842
843static jlong
844nAllocationCreateTyped(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mips, jint usage,
845                       jlong pointer)
846{
847    if (kLogApi) {
848        ALOGD("nAllocationCreateTyped, con(%p), type(%p), mip(%i), usage(%i), ptr(%p)",
849              (RsContext)con, (RsElement)type, mips, usage, (void *)pointer);
850    }
851    return (jlong)(uintptr_t) rsAllocationCreateTyped((RsContext)con, (RsType)type,
852                                                      (RsAllocationMipmapControl)mips,
853                                                      (uint32_t)usage, (uintptr_t)pointer);
854}
855
856static void
857nAllocationSyncAll(JNIEnv *_env, jobject _this, jlong con, jlong a, jint bits)
858{
859    if (kLogApi) {
860        ALOGD("nAllocationSyncAll, con(%p), a(%p), bits(0x%08x)", (RsContext)con, (RsAllocation)a,
861              bits);
862    }
863    rsAllocationSyncAll((RsContext)con, (RsAllocation)a, (RsAllocationUsageType)bits);
864}
865
866static jobject
867nAllocationGetSurface(JNIEnv *_env, jobject _this, jlong con, jlong a)
868{
869    if (kLogApi) {
870        ALOGD("nAllocationGetSurface, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
871    }
872
873    IGraphicBufferProducer *v = (IGraphicBufferProducer *)rsAllocationGetSurface((RsContext)con,
874                                                                                 (RsAllocation)a);
875    sp<IGraphicBufferProducer> bp = v;
876    v->decStrong(nullptr);
877
878    jobject o = android_view_Surface_createFromIGraphicBufferProducer(_env, bp);
879    return o;
880}
881
882static void
883nAllocationSetSurface(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject sur)
884{
885    if (kLogApi) {
886        ALOGD("nAllocationSetSurface, con(%p), alloc(%p), surface(%p)", (RsContext)con,
887              (RsAllocation)alloc, (Surface *)sur);
888    }
889
890    sp<Surface> s;
891    if (sur != 0) {
892        s = android_view_Surface_getSurface(_env, sur);
893    }
894
895    rsAllocationSetSurface((RsContext)con, (RsAllocation)alloc,
896                           static_cast<ANativeWindow *>(s.get()));
897}
898
899static void
900nAllocationIoSend(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
901{
902    if (kLogApi) {
903        ALOGD("nAllocationIoSend, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
904    }
905    rsAllocationIoSend((RsContext)con, (RsAllocation)alloc);
906}
907
908static void
909nAllocationIoReceive(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
910{
911    if (kLogApi) {
912        ALOGD("nAllocationIoReceive, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
913    }
914    rsAllocationIoReceive((RsContext)con, (RsAllocation)alloc);
915}
916
917
918static void
919nAllocationGenerateMipmaps(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
920{
921    if (kLogApi) {
922        ALOGD("nAllocationGenerateMipmaps, con(%p), a(%p)", (RsContext)con, (RsAllocation)alloc);
923    }
924    rsAllocationGenerateMipmaps((RsContext)con, (RsAllocation)alloc);
925}
926
927static jlong
928nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
929                            jobject jbitmap, jint usage)
930{
931    SkBitmap const * nativeBitmap =
932            GraphicsJNI::getSkBitmap(_env, jbitmap);
933    const SkBitmap& bitmap(*nativeBitmap);
934
935    bitmap.lockPixels();
936    const void* ptr = bitmap.getPixels();
937    jlong id = (jlong)(uintptr_t)rsAllocationCreateFromBitmap((RsContext)con,
938                                                  (RsType)type, (RsAllocationMipmapControl)mip,
939                                                  ptr, bitmap.getSize(), usage);
940    bitmap.unlockPixels();
941    return id;
942}
943
944static jlong
945nAllocationCreateBitmapBackedAllocation(JNIEnv *_env, jobject _this, jlong con, jlong type,
946                                        jint mip, jobject jbitmap, jint usage)
947{
948    SkBitmap const * nativeBitmap =
949            GraphicsJNI::getSkBitmap(_env, jbitmap);
950    const SkBitmap& bitmap(*nativeBitmap);
951
952    bitmap.lockPixels();
953    const void* ptr = bitmap.getPixels();
954    jlong id = (jlong)(uintptr_t)rsAllocationCreateTyped((RsContext)con,
955                                            (RsType)type, (RsAllocationMipmapControl)mip,
956                                            (uint32_t)usage, (uintptr_t)ptr);
957    bitmap.unlockPixels();
958    return id;
959}
960
961static jlong
962nAllocationCubeCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
963                                jobject jbitmap, jint usage)
964{
965    SkBitmap const * nativeBitmap =
966            GraphicsJNI::getSkBitmap(_env, jbitmap);
967    const SkBitmap& bitmap(*nativeBitmap);
968
969    bitmap.lockPixels();
970    const void* ptr = bitmap.getPixels();
971    jlong id = (jlong)(uintptr_t)rsAllocationCubeCreateFromBitmap((RsContext)con,
972                                                      (RsType)type, (RsAllocationMipmapControl)mip,
973                                                      ptr, bitmap.getSize(), usage);
974    bitmap.unlockPixels();
975    return id;
976}
977
978static void
979nAllocationCopyFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
980{
981    SkBitmap const * nativeBitmap =
982            GraphicsJNI::getSkBitmap(_env, jbitmap);
983    const SkBitmap& bitmap(*nativeBitmap);
984    int w = bitmap.width();
985    int h = bitmap.height();
986
987    bitmap.lockPixels();
988    const void* ptr = bitmap.getPixels();
989    rsAllocation2DData((RsContext)con, (RsAllocation)alloc, 0, 0,
990                       0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
991                       w, h, ptr, bitmap.getSize(), 0);
992    bitmap.unlockPixels();
993}
994
995static void
996nAllocationCopyToBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
997{
998    SkBitmap const * nativeBitmap =
999            GraphicsJNI::getSkBitmap(_env, jbitmap);
1000    const SkBitmap& bitmap(*nativeBitmap);
1001
1002    bitmap.lockPixels();
1003    void* ptr = bitmap.getPixels();
1004    rsAllocationCopyToBitmap((RsContext)con, (RsAllocation)alloc, ptr, bitmap.getSize());
1005    bitmap.unlockPixels();
1006    bitmap.notifyPixelsChanged();
1007}
1008
1009// Copies from the Java object data into the Allocation pointed to by _alloc.
1010static void
1011nAllocationData1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
1012                  jint count, jobject data, jint sizeBytes, jint dataType)
1013{
1014    RsAllocation *alloc = (RsAllocation *)_alloc;
1015    if (kLogApi) {
1016        ALOGD("nAllocation1DData, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1017              "dataType(%i)", (RsContext)con, (RsAllocation)alloc, offset, count, sizeBytes,
1018              dataType);
1019    }
1020    PER_ARRAY_TYPE(nullptr, rsAllocation1DData, true, (RsContext)con, alloc, offset, lod, count,
1021                   ptr, sizeBytes);
1022}
1023
1024static void
1025nAllocationElementData(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1026                       jint xoff, jint yoff, jint zoff,
1027                       jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
1028{
1029    jint len = _env->GetArrayLength(data);
1030    if (kLogApi) {
1031        ALOGD("nAllocationElementData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1032              "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
1033              sizeBytes);
1034    }
1035    jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
1036    rsAllocationElementData((RsContext)con, (RsAllocation)alloc,
1037                            xoff, yoff, zoff,
1038                            lod, ptr, sizeBytes, compIdx);
1039    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1040}
1041
1042
1043// Copies from the Java object data into the Allocation pointed to by _alloc.
1044static void
1045nAllocationData2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
1046                  jint w, jint h, jobject data, jint sizeBytes, jint dataType)
1047{
1048    RsAllocation *alloc = (RsAllocation *)_alloc;
1049    RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
1050    if (kLogApi) {
1051        ALOGD("nAllocation2DData, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1052              "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1053    }
1054    PER_ARRAY_TYPE(nullptr, rsAllocation2DData, true, (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
1055}
1056
1057// Copies from the Allocation pointed to by srcAlloc into the Allocation
1058// pointed to by dstAlloc.
1059static void
1060nAllocationData2D_alloc(JNIEnv *_env, jobject _this, jlong con,
1061                        jlong dstAlloc, jint dstXoff, jint dstYoff,
1062                        jint dstMip, jint dstFace,
1063                        jint width, jint height,
1064                        jlong srcAlloc, jint srcXoff, jint srcYoff,
1065                        jint srcMip, jint srcFace)
1066{
1067    if (kLogApi) {
1068        ALOGD("nAllocation2DData_s, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1069              " dstMip(%i), dstFace(%i), width(%i), height(%i),"
1070              " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i), srcFace(%i)",
1071              (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip, dstFace,
1072              width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
1073    }
1074
1075    rsAllocationCopy2DRange((RsContext)con,
1076                            (RsAllocation)dstAlloc,
1077                            dstXoff, dstYoff,
1078                            dstMip, dstFace,
1079                            width, height,
1080                            (RsAllocation)srcAlloc,
1081                            srcXoff, srcYoff,
1082                            srcMip, srcFace);
1083}
1084
1085// Copies from the Java object data into the Allocation pointed to by _alloc.
1086static void
1087nAllocationData3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
1088                    jint w, jint h, jint d, jobject data, int sizeBytes, int dataType)
1089{
1090    RsAllocation *alloc = (RsAllocation *)_alloc;
1091    if (kLogApi) {
1092        ALOGD("nAllocation3DData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1093              " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1094              lod, w, h, d, sizeBytes);
1095    }
1096    PER_ARRAY_TYPE(nullptr, rsAllocation3DData, true, (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
1097}
1098
1099// Copies from the Allocation pointed to by srcAlloc into the Allocation
1100// pointed to by dstAlloc.
1101static void
1102nAllocationData3D_alloc(JNIEnv *_env, jobject _this, jlong con,
1103                        jlong dstAlloc, jint dstXoff, jint dstYoff, jint dstZoff,
1104                        jint dstMip,
1105                        jint width, jint height, jint depth,
1106                        jlong srcAlloc, jint srcXoff, jint srcYoff, jint srcZoff,
1107                        jint srcMip)
1108{
1109    if (kLogApi) {
1110        ALOGD("nAllocationData3D_alloc, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1111              " dstMip(%i), width(%i), height(%i),"
1112              " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i)",
1113              (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip,
1114              width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip);
1115    }
1116
1117    rsAllocationCopy3DRange((RsContext)con,
1118                            (RsAllocation)dstAlloc,
1119                            dstXoff, dstYoff, dstZoff, dstMip,
1120                            width, height, depth,
1121                            (RsAllocation)srcAlloc,
1122                            srcXoff, srcYoff, srcZoff, srcMip);
1123}
1124
1125
1126// Copies from the Allocation pointed to by _alloc into the Java object data.
1127static void
1128nAllocationRead(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jobject data, int dataType)
1129{
1130    RsAllocation *alloc = (RsAllocation *)_alloc;
1131    if (kLogApi) {
1132        ALOGD("nAllocationRead, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
1133    }
1134    PER_ARRAY_TYPE(0, rsAllocationRead, false, (RsContext)con, alloc, ptr, len * typeBytes);
1135}
1136
1137// Copies from the Allocation pointed to by _alloc into the Java object data.
1138static void
1139nAllocationRead1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
1140                  jint count, jobject data, int sizeBytes, int dataType)
1141{
1142    RsAllocation *alloc = (RsAllocation *)_alloc;
1143    if (kLogApi) {
1144        ALOGD("nAllocation1DRead, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1145              "dataType(%i)", (RsContext)con, alloc, offset, count, sizeBytes, dataType);
1146    }
1147    PER_ARRAY_TYPE(0, rsAllocation1DRead, false, (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
1148}
1149
1150// Copies from the Element in the Allocation pointed to by _alloc into the Java array data.
1151static void
1152nAllocationElementRead(JNIEnv *_env, jobject _this, jlong con, jlong _alloc,
1153                       jint xoff, jint yoff, jint zoff,
1154                       jint lod, jint compIdx, jobject data, jint sizeBytes, int dataType)
1155{
1156    RsAllocation *alloc = (RsAllocation *)_alloc;
1157    if (kLogApi) {
1158        ALOGD("nAllocationElementRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), "
1159              "sizeBytes(%i)", (RsContext)con, alloc, xoff, yoff, zoff, compIdx, sizeBytes);
1160    }
1161    PER_ARRAY_TYPE(0, rsAllocationElementRead, false, (RsContext)con, alloc,
1162                   xoff, yoff, zoff, lod, ptr, sizeBytes, compIdx);
1163}
1164
1165// Copies from the Allocation pointed to by _alloc into the Java object data.
1166static void
1167nAllocationRead2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
1168                  jint w, jint h, jobject data, int sizeBytes, int dataType)
1169{
1170    RsAllocation *alloc = (RsAllocation *)_alloc;
1171    RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
1172    if (kLogApi) {
1173        ALOGD("nAllocation2DRead, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1174              "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1175    }
1176    PER_ARRAY_TYPE(0, rsAllocation2DRead, false, (RsContext)con, alloc, xoff, yoff, lod, face, w, h,
1177                   ptr, sizeBytes, 0);
1178}
1179// Copies from the Allocation pointed to by _alloc into the Java object data.
1180static void
1181nAllocationRead3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
1182                  jint w, jint h, jint d, jobject data, int sizeBytes, int dataType)
1183{
1184    RsAllocation *alloc = (RsAllocation *)_alloc;
1185    if (kLogApi) {
1186        ALOGD("nAllocation3DRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1187              " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1188              lod, w, h, d, sizeBytes);
1189    }
1190    PER_ARRAY_TYPE(0, rsAllocation3DRead, false, (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d,
1191                   ptr, sizeBytes, 0);
1192}
1193
1194static jlong
1195nAllocationGetType(JNIEnv *_env, jobject _this, jlong con, jlong a)
1196{
1197    if (kLogApi) {
1198        ALOGD("nAllocationGetType, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1199    }
1200    return (jlong)(uintptr_t) rsaAllocationGetType((RsContext)con, (RsAllocation)a);
1201}
1202
1203static void
1204nAllocationResize1D(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint dimX)
1205{
1206    if (kLogApi) {
1207        ALOGD("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", (RsContext)con,
1208              (RsAllocation)alloc, dimX);
1209    }
1210    rsAllocationResize1D((RsContext)con, (RsAllocation)alloc, dimX);
1211}
1212
1213
1214static jlong
1215nAllocationAdapterCreate(JNIEnv *_env, jobject _this, jlong con, jlong basealloc, jlong type)
1216{
1217    if (kLogApi) {
1218        ALOGD("nAllocationAdapterCreate, con(%p), base(%p), type(%p)",
1219              (RsContext)con, (RsAllocation)basealloc, (RsElement)type);
1220    }
1221    return (jlong)(uintptr_t) rsAllocationAdapterCreate((RsContext)con, (RsType)type,
1222                                                        (RsAllocation)basealloc);
1223
1224}
1225
1226static void
1227nAllocationAdapterOffset(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1228                        jint x, jint y, jint z, jint face, jint lod,
1229                        jint a1, jint a2, jint a3, jint a4)
1230{
1231    uint32_t params[] = {
1232        (uint32_t)x, (uint32_t)y, (uint32_t)z, (uint32_t)face,
1233        (uint32_t)lod, (uint32_t)a1, (uint32_t)a2, (uint32_t)a3, (uint32_t)a4
1234    };
1235    if (kLogApi) {
1236        ALOGD("nAllocationAdapterOffset, con(%p), alloc(%p), x(%i), y(%i), z(%i), face(%i), lod(%i), arrays(%i %i %i %i)",
1237              (RsContext)con, (RsAllocation)alloc, x, y, z, face, lod, a1, a2, a3, a4);
1238    }
1239    rsAllocationAdapterOffset((RsContext)con, (RsAllocation)alloc,
1240                              params, sizeof(params));
1241}
1242
1243
1244// -----------------------------------
1245
1246static jlong
1247nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con, jlong native_asset)
1248{
1249    Asset* asset = reinterpret_cast<Asset*>(native_asset);
1250    ALOGV("______nFileA3D %p", asset);
1251
1252    jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromMemory((RsContext)con, asset->getBuffer(false), asset->getLength());
1253    return id;
1254}
1255
1256static jlong
1257nFileA3DCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path)
1258{
1259    AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
1260    if (mgr == nullptr) {
1261        return 0;
1262    }
1263
1264    AutoJavaStringToUTF8 str(_env, _path);
1265    Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
1266    if (asset == nullptr) {
1267        return 0;
1268    }
1269
1270    jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromAsset((RsContext)con, asset);
1271    return id;
1272}
1273
1274static jlong
1275nFileA3DCreateFromFile(JNIEnv *_env, jobject _this, jlong con, jstring fileName)
1276{
1277    AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
1278    jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromFile((RsContext)con, fileNameUTF.c_str());
1279
1280    return id;
1281}
1282
1283static jint
1284nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D)
1285{
1286    int32_t numEntries = 0;
1287    rsaFileA3DGetNumIndexEntries((RsContext)con, &numEntries, (RsFile)fileA3D);
1288    return (jint)numEntries;
1289}
1290
1291static void
1292nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
1293{
1294    ALOGV("______nFileA3D %p", (RsFile) fileA3D);
1295    RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
1296
1297    rsaFileA3DGetIndexEntries((RsContext)con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
1298
1299    for(jint i = 0; i < numEntries; i ++) {
1300        _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
1301        _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
1302    }
1303
1304    free(fileEntries);
1305}
1306
1307static jlong
1308nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint index)
1309{
1310    ALOGV("______nFileA3D %p", (RsFile) fileA3D);
1311    jlong id = (jlong)(uintptr_t)rsaFileA3DGetEntryByIndex((RsContext)con, (uint32_t)index, (RsFile)fileA3D);
1312    return id;
1313}
1314
1315// -----------------------------------
1316
1317static jlong
1318nFontCreateFromFile(JNIEnv *_env, jobject _this, jlong con,
1319                    jstring fileName, jfloat fontSize, jint dpi)
1320{
1321    AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
1322    jlong id = (jlong)(uintptr_t)rsFontCreateFromFile((RsContext)con,
1323                                         fileNameUTF.c_str(), fileNameUTF.length(),
1324                                         fontSize, dpi);
1325
1326    return id;
1327}
1328
1329static jlong
1330nFontCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con,
1331                           jstring name, jfloat fontSize, jint dpi, jlong native_asset)
1332{
1333    Asset* asset = reinterpret_cast<Asset*>(native_asset);
1334    AutoJavaStringToUTF8 nameUTF(_env, name);
1335
1336    jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
1337                                           nameUTF.c_str(), nameUTF.length(),
1338                                           fontSize, dpi,
1339                                           asset->getBuffer(false), asset->getLength());
1340    return id;
1341}
1342
1343static jlong
1344nFontCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path,
1345                     jfloat fontSize, jint dpi)
1346{
1347    AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
1348    if (mgr == nullptr) {
1349        return 0;
1350    }
1351
1352    AutoJavaStringToUTF8 str(_env, _path);
1353    Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
1354    if (asset == nullptr) {
1355        return 0;
1356    }
1357
1358    jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
1359                                           str.c_str(), str.length(),
1360                                           fontSize, dpi,
1361                                           asset->getBuffer(false), asset->getLength());
1362    delete asset;
1363    return id;
1364}
1365
1366// -----------------------------------
1367
1368static void
1369nScriptBindAllocation(JNIEnv *_env, jobject _this, jlong con, jlong script, jlong alloc, jint slot)
1370{
1371    if (kLogApi) {
1372        ALOGD("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", (RsContext)con,
1373              (RsScript)script, (RsAllocation)alloc, slot);
1374    }
1375    rsScriptBindAllocation((RsContext)con, (RsScript)script, (RsAllocation)alloc, slot);
1376}
1377
1378static void
1379nScriptSetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jint val)
1380{
1381    if (kLogApi) {
1382        ALOGD("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", (RsContext)con, (void *)script,
1383              slot, val);
1384    }
1385    rsScriptSetVarI((RsContext)con, (RsScript)script, slot, val);
1386}
1387
1388static jint
1389nScriptGetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
1390{
1391    if (kLogApi) {
1392        ALOGD("nScriptGetVarI, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1393    }
1394    int value = 0;
1395    rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
1396    return value;
1397}
1398
1399static void
1400nScriptSetVarObj(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
1401{
1402    if (kLogApi) {
1403        ALOGD("nScriptSetVarObj, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
1404              slot, val);
1405    }
1406    rsScriptSetVarObj((RsContext)con, (RsScript)script, slot, (RsObjectBase)val);
1407}
1408
1409static void
1410nScriptSetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
1411{
1412    if (kLogApi) {
1413        ALOGD("nScriptSetVarJ, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
1414              slot, val);
1415    }
1416    rsScriptSetVarJ((RsContext)con, (RsScript)script, slot, val);
1417}
1418
1419static jlong
1420nScriptGetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
1421{
1422    if (kLogApi) {
1423        ALOGD("nScriptGetVarJ, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1424    }
1425    jlong value = 0;
1426    rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
1427    return value;
1428}
1429
1430static void
1431nScriptSetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, float val)
1432{
1433    if (kLogApi) {
1434        ALOGD("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", (RsContext)con, (void *)script,
1435              slot, val);
1436    }
1437    rsScriptSetVarF((RsContext)con, (RsScript)script, slot, val);
1438}
1439
1440static jfloat
1441nScriptGetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
1442{
1443    if (kLogApi) {
1444        ALOGD("nScriptGetVarF, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1445    }
1446    jfloat value = 0;
1447    rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
1448    return value;
1449}
1450
1451static void
1452nScriptSetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, double val)
1453{
1454    if (kLogApi) {
1455        ALOGD("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", (RsContext)con, (void *)script,
1456              slot, val);
1457    }
1458    rsScriptSetVarD((RsContext)con, (RsScript)script, slot, val);
1459}
1460
1461static jdouble
1462nScriptGetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
1463{
1464    if (kLogApi) {
1465        ALOGD("nScriptGetVarD, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1466    }
1467    jdouble value = 0;
1468    rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
1469    return value;
1470}
1471
1472static void
1473nScriptSetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
1474{
1475    if (kLogApi) {
1476        ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1477    }
1478    jint len = _env->GetArrayLength(data);
1479    jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
1480    rsScriptSetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
1481    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1482}
1483
1484static void
1485nScriptGetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
1486{
1487    if (kLogApi) {
1488        ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1489    }
1490    jint len = _env->GetArrayLength(data);
1491    jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
1492    rsScriptGetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
1493    _env->ReleaseByteArrayElements(data, ptr, 0);
1494}
1495
1496static void
1497nScriptSetVarVE(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data,
1498                jlong elem, jintArray dims)
1499{
1500    if (kLogApi) {
1501        ALOGD("nScriptSetVarVE, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1502    }
1503    jint len = _env->GetArrayLength(data);
1504    jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
1505    jint dimsLen = _env->GetArrayLength(dims) * sizeof(int);
1506    jint *dimsPtr = _env->GetIntArrayElements(dims, nullptr);
1507    rsScriptSetVarVE((RsContext)con, (RsScript)script, slot, ptr, len, (RsElement)elem,
1508                     (const uint32_t*) dimsPtr, dimsLen);
1509    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1510    _env->ReleaseIntArrayElements(dims, dimsPtr, JNI_ABORT);
1511}
1512
1513
1514static void
1515nScriptSetTimeZone(JNIEnv *_env, jobject _this, jlong con, jlong script, jbyteArray timeZone)
1516{
1517    if (kLogApi) {
1518        ALOGD("nScriptCSetTimeZone, con(%p), s(%p)", (RsContext)con, (void *)script);
1519    }
1520
1521    jint length = _env->GetArrayLength(timeZone);
1522    jbyte* timeZone_ptr;
1523    timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
1524
1525    rsScriptSetTimeZone((RsContext)con, (RsScript)script, (const char *)timeZone_ptr, length);
1526
1527    if (timeZone_ptr) {
1528        _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
1529    }
1530}
1531
1532static void
1533nScriptInvoke(JNIEnv *_env, jobject _this, jlong con, jlong obj, jint slot)
1534{
1535    if (kLogApi) {
1536        ALOGD("nScriptInvoke, con(%p), script(%p)", (RsContext)con, (void *)obj);
1537    }
1538    rsScriptInvoke((RsContext)con, (RsScript)obj, slot);
1539}
1540
1541static void
1542nScriptInvokeV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
1543{
1544    if (kLogApi) {
1545        ALOGD("nScriptInvokeV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1546    }
1547    jint len = _env->GetArrayLength(data);
1548    jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
1549    rsScriptInvokeV((RsContext)con, (RsScript)script, slot, ptr, len);
1550    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1551}
1552
1553static void
1554nScriptForEach(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
1555               jlongArray ains, jlong aout, jbyteArray params,
1556               jintArray limits)
1557{
1558    if (kLogApi) {
1559        ALOGD("nScriptForEach, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1560    }
1561
1562    jint   in_len = 0;
1563    jlong *in_ptr = nullptr;
1564
1565    RsAllocation *in_allocs = nullptr;
1566
1567    if (ains != nullptr) {
1568        in_len = _env->GetArrayLength(ains);
1569        in_ptr = _env->GetLongArrayElements(ains, nullptr);
1570
1571        if (sizeof(RsAllocation) == sizeof(jlong)) {
1572            in_allocs = (RsAllocation*)in_ptr;
1573
1574        } else {
1575            // Convert from 64-bit jlong types to the native pointer type.
1576
1577            in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
1578
1579            for (int index = in_len; --index >= 0;) {
1580                in_allocs[index] = (RsAllocation)in_ptr[index];
1581            }
1582        }
1583    }
1584
1585    jint   param_len = 0;
1586    jbyte *param_ptr = nullptr;
1587
1588    if (params != nullptr) {
1589        param_len = _env->GetArrayLength(params);
1590        param_ptr = _env->GetByteArrayElements(params, nullptr);
1591    }
1592
1593    RsScriptCall sc, *sca = nullptr;
1594    uint32_t sc_size = 0;
1595
1596    jint  limit_len = 0;
1597    jint *limit_ptr = nullptr;
1598
1599    if (limits != nullptr) {
1600        limit_len = _env->GetArrayLength(limits);
1601        limit_ptr = _env->GetIntArrayElements(limits, nullptr);
1602
1603        assert(limit_len == 6);
1604        UNUSED(limit_len);  // As the assert might not be compiled.
1605
1606        sc.xStart     = limit_ptr[0];
1607        sc.xEnd       = limit_ptr[1];
1608        sc.yStart     = limit_ptr[2];
1609        sc.yEnd       = limit_ptr[3];
1610        sc.zStart     = limit_ptr[4];
1611        sc.zEnd       = limit_ptr[5];
1612        sc.strategy   = RS_FOR_EACH_STRATEGY_DONT_CARE;
1613        sc.arrayStart = 0;
1614        sc.arrayEnd = 0;
1615        sc.array2Start = 0;
1616        sc.array2End = 0;
1617        sc.array3Start = 0;
1618        sc.array3End = 0;
1619        sc.array4Start = 0;
1620        sc.array4End = 0;
1621
1622        sca = &sc;
1623    }
1624
1625    rsScriptForEachMulti((RsContext)con, (RsScript)script, slot,
1626                         in_allocs, in_len, (RsAllocation)aout,
1627                         param_ptr, param_len, sca, sc_size);
1628
1629    if (ains != nullptr) {
1630        _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
1631    }
1632
1633    if (params != nullptr) {
1634        _env->ReleaseByteArrayElements(params, param_ptr, JNI_ABORT);
1635    }
1636
1637    if (limits != nullptr) {
1638        _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
1639    }
1640}
1641
1642// -----------------------------------
1643
1644static jlong
1645nScriptCCreate(JNIEnv *_env, jobject _this, jlong con,
1646               jstring resName, jstring cacheDir,
1647               jbyteArray scriptRef, jint length)
1648{
1649    if (kLogApi) {
1650        ALOGD("nScriptCCreate, con(%p)", (RsContext)con);
1651    }
1652
1653    AutoJavaStringToUTF8 resNameUTF(_env, resName);
1654    AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
1655    jlong ret = 0;
1656    jbyte* script_ptr = nullptr;
1657    jint _exception = 0;
1658    jint remaining;
1659    if (!scriptRef) {
1660        _exception = 1;
1661        //jniThrowException(_env, "java/lang/IllegalArgumentException", "script == null");
1662        goto exit;
1663    }
1664    if (length < 0) {
1665        _exception = 1;
1666        //jniThrowException(_env, "java/lang/IllegalArgumentException", "length < 0");
1667        goto exit;
1668    }
1669    remaining = _env->GetArrayLength(scriptRef);
1670    if (remaining < length) {
1671        _exception = 1;
1672        //jniThrowException(_env, "java/lang/IllegalArgumentException",
1673        //        "length > script.length - offset");
1674        goto exit;
1675    }
1676    script_ptr = (jbyte *)
1677        _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
1678
1679    //rsScriptCSetText((RsContext)con, (const char *)script_ptr, length);
1680
1681    ret = (jlong)(uintptr_t)rsScriptCCreate((RsContext)con,
1682                                resNameUTF.c_str(), resNameUTF.length(),
1683                                cacheDirUTF.c_str(), cacheDirUTF.length(),
1684                                (const char *)script_ptr, length);
1685
1686exit:
1687    if (script_ptr) {
1688        _env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr,
1689                _exception ? JNI_ABORT: 0);
1690    }
1691
1692    return (jlong)(uintptr_t)ret;
1693}
1694
1695static jlong
1696nScriptIntrinsicCreate(JNIEnv *_env, jobject _this, jlong con, jint id, jlong eid)
1697{
1698    if (kLogApi) {
1699        ALOGD("nScriptIntrinsicCreate, con(%p) id(%i) element(%p)", (RsContext)con, id,
1700              (void *)eid);
1701    }
1702    return (jlong)(uintptr_t)rsScriptIntrinsicCreate((RsContext)con, id, (RsElement)eid);
1703}
1704
1705static jlong
1706nScriptKernelIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot, jint sig)
1707{
1708    if (kLogApi) {
1709        ALOGD("nScriptKernelIDCreate, con(%p) script(%p), slot(%i), sig(%i)", (RsContext)con,
1710              (void *)sid, slot, sig);
1711    }
1712    return (jlong)(uintptr_t)rsScriptKernelIDCreate((RsContext)con, (RsScript)sid, slot, sig);
1713}
1714
1715static jlong
1716nScriptInvokeIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
1717{
1718    if (kLogApi) {
1719        ALOGD("nScriptInvokeIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con,
1720              (void *)sid, slot);
1721    }
1722    return (jlong)(uintptr_t)rsScriptInvokeIDCreate((RsContext)con, (RsScript)sid, slot);
1723}
1724
1725static jlong
1726nScriptFieldIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
1727{
1728    if (kLogApi) {
1729        ALOGD("nScriptFieldIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con, (void *)sid,
1730              slot);
1731    }
1732    return (jlong)(uintptr_t)rsScriptFieldIDCreate((RsContext)con, (RsScript)sid, slot);
1733}
1734
1735static jlong
1736nScriptGroupCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _kernels, jlongArray _src,
1737    jlongArray _dstk, jlongArray _dstf, jlongArray _types)
1738{
1739    if (kLogApi) {
1740        ALOGD("nScriptGroupCreate, con(%p)", (RsContext)con);
1741    }
1742
1743    jint kernelsLen = _env->GetArrayLength(_kernels);
1744    jlong *jKernelsPtr = _env->GetLongArrayElements(_kernels, nullptr);
1745    RsScriptKernelID* kernelsPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * kernelsLen);
1746    for(int i = 0; i < kernelsLen; ++i) {
1747        kernelsPtr[i] = (RsScriptKernelID)jKernelsPtr[i];
1748    }
1749
1750    jint srcLen = _env->GetArrayLength(_src);
1751    jlong *jSrcPtr = _env->GetLongArrayElements(_src, nullptr);
1752    RsScriptKernelID* srcPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * srcLen);
1753    for(int i = 0; i < srcLen; ++i) {
1754        srcPtr[i] = (RsScriptKernelID)jSrcPtr[i];
1755    }
1756
1757    jint dstkLen = _env->GetArrayLength(_dstk);
1758    jlong *jDstkPtr = _env->GetLongArrayElements(_dstk, nullptr);
1759    RsScriptKernelID* dstkPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstkLen);
1760    for(int i = 0; i < dstkLen; ++i) {
1761        dstkPtr[i] = (RsScriptKernelID)jDstkPtr[i];
1762    }
1763
1764    jint dstfLen = _env->GetArrayLength(_dstf);
1765    jlong *jDstfPtr = _env->GetLongArrayElements(_dstf, nullptr);
1766    RsScriptKernelID* dstfPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstfLen);
1767    for(int i = 0; i < dstfLen; ++i) {
1768        dstfPtr[i] = (RsScriptKernelID)jDstfPtr[i];
1769    }
1770
1771    jint typesLen = _env->GetArrayLength(_types);
1772    jlong *jTypesPtr = _env->GetLongArrayElements(_types, nullptr);
1773    RsType* typesPtr = (RsType*) malloc(sizeof(RsType) * typesLen);
1774    for(int i = 0; i < typesLen; ++i) {
1775        typesPtr[i] = (RsType)jTypesPtr[i];
1776    }
1777
1778    jlong id = (jlong)(uintptr_t)rsScriptGroupCreate((RsContext)con,
1779                               (RsScriptKernelID *)kernelsPtr, kernelsLen * sizeof(RsScriptKernelID),
1780                               (RsScriptKernelID *)srcPtr, srcLen * sizeof(RsScriptKernelID),
1781                               (RsScriptKernelID *)dstkPtr, dstkLen * sizeof(RsScriptKernelID),
1782                               (RsScriptFieldID *)dstfPtr, dstfLen * sizeof(RsScriptKernelID),
1783                               (RsType *)typesPtr, typesLen * sizeof(RsType));
1784
1785    free(kernelsPtr);
1786    free(srcPtr);
1787    free(dstkPtr);
1788    free(dstfPtr);
1789    free(typesPtr);
1790    _env->ReleaseLongArrayElements(_kernels, jKernelsPtr, 0);
1791    _env->ReleaseLongArrayElements(_src, jSrcPtr, 0);
1792    _env->ReleaseLongArrayElements(_dstk, jDstkPtr, 0);
1793    _env->ReleaseLongArrayElements(_dstf, jDstfPtr, 0);
1794    _env->ReleaseLongArrayElements(_types, jTypesPtr, 0);
1795    return id;
1796}
1797
1798static void
1799nScriptGroupSetInput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
1800{
1801    if (kLogApi) {
1802        ALOGD("nScriptGroupSetInput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
1803              (void *)gid, (void *)kid, (void *)alloc);
1804    }
1805    rsScriptGroupSetInput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
1806}
1807
1808static void
1809nScriptGroupSetOutput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
1810{
1811    if (kLogApi) {
1812        ALOGD("nScriptGroupSetOutput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
1813              (void *)gid, (void *)kid, (void *)alloc);
1814    }
1815    rsScriptGroupSetOutput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
1816}
1817
1818static void
1819nScriptGroupExecute(JNIEnv *_env, jobject _this, jlong con, jlong gid)
1820{
1821    if (kLogApi) {
1822        ALOGD("nScriptGroupSetOutput, con(%p) group(%p)", (RsContext)con, (void *)gid);
1823    }
1824    rsScriptGroupExecute((RsContext)con, (RsScriptGroup)gid);
1825}
1826
1827// ---------------------------------------------------------------------------
1828
1829static jlong
1830nProgramStoreCreate(JNIEnv *_env, jobject _this, jlong con,
1831                    jboolean colorMaskR, jboolean colorMaskG, jboolean colorMaskB, jboolean colorMaskA,
1832                    jboolean depthMask, jboolean ditherEnable,
1833                    jint srcFunc, jint destFunc,
1834                    jint depthFunc)
1835{
1836    if (kLogApi) {
1837        ALOGD("nProgramStoreCreate, con(%p)", (RsContext)con);
1838    }
1839    return (jlong)(uintptr_t)rsProgramStoreCreate((RsContext)con, colorMaskR, colorMaskG, colorMaskB, colorMaskA,
1840                                      depthMask, ditherEnable, (RsBlendSrcFunc)srcFunc,
1841                                      (RsBlendDstFunc)destFunc, (RsDepthFunc)depthFunc);
1842}
1843
1844// ---------------------------------------------------------------------------
1845
1846static void
1847nProgramBindConstants(JNIEnv *_env, jobject _this, jlong con, jlong vpv, jint slot, jlong a)
1848{
1849    if (kLogApi) {
1850        ALOGD("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", (RsContext)con,
1851              (RsProgramVertex)vpv, slot, (RsAllocation)a);
1852    }
1853    rsProgramBindConstants((RsContext)con, (RsProgram)vpv, slot, (RsAllocation)a);
1854}
1855
1856static void
1857nProgramBindTexture(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
1858{
1859    if (kLogApi) {
1860        ALOGD("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
1861              (RsProgramFragment)vpf, slot, (RsAllocation)a);
1862    }
1863    rsProgramBindTexture((RsContext)con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
1864}
1865
1866static void
1867nProgramBindSampler(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
1868{
1869    if (kLogApi) {
1870        ALOGD("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
1871              (RsProgramFragment)vpf, slot, (RsSampler)a);
1872    }
1873    rsProgramBindSampler((RsContext)con, (RsProgramFragment)vpf, slot, (RsSampler)a);
1874}
1875
1876// ---------------------------------------------------------------------------
1877
1878static jlong
1879nProgramFragmentCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
1880                       jobjectArray texNames, jlongArray params)
1881{
1882    AutoJavaStringToUTF8 shaderUTF(_env, shader);
1883    jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
1884    jint paramLen = _env->GetArrayLength(params);
1885
1886    int texCount = _env->GetArrayLength(texNames);
1887    AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
1888    const char ** nameArray = names.c_str();
1889    size_t* sizeArray = names.c_str_len();
1890
1891    if (kLogApi) {
1892        ALOGD("nProgramFragmentCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
1893    }
1894
1895    uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
1896    for(int i = 0; i < paramLen; ++i) {
1897        paramPtr[i] = (uintptr_t)jParamPtr[i];
1898    }
1899    jlong ret = (jlong)(uintptr_t)rsProgramFragmentCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
1900                                             nameArray, texCount, sizeArray,
1901                                             paramPtr, paramLen);
1902
1903    free(paramPtr);
1904    _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
1905    return ret;
1906}
1907
1908
1909// ---------------------------------------------------------------------------
1910
1911static jlong
1912nProgramVertexCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
1913                     jobjectArray texNames, jlongArray params)
1914{
1915    AutoJavaStringToUTF8 shaderUTF(_env, shader);
1916    jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
1917    jint paramLen = _env->GetArrayLength(params);
1918
1919    if (kLogApi) {
1920        ALOGD("nProgramVertexCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
1921    }
1922
1923    int texCount = _env->GetArrayLength(texNames);
1924    AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
1925    const char ** nameArray = names.c_str();
1926    size_t* sizeArray = names.c_str_len();
1927
1928    uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
1929    for(int i = 0; i < paramLen; ++i) {
1930        paramPtr[i] = (uintptr_t)jParamPtr[i];
1931    }
1932
1933    jlong ret = (jlong)(uintptr_t)rsProgramVertexCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
1934                                           nameArray, texCount, sizeArray,
1935                                           paramPtr, paramLen);
1936
1937    free(paramPtr);
1938    _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
1939    return ret;
1940}
1941
1942// ---------------------------------------------------------------------------
1943
1944static jlong
1945nProgramRasterCreate(JNIEnv *_env, jobject _this, jlong con, jboolean pointSprite, jint cull)
1946{
1947    if (kLogApi) {
1948        ALOGD("nProgramRasterCreate, con(%p), pointSprite(%i), cull(%i)", (RsContext)con,
1949              pointSprite, cull);
1950    }
1951    return (jlong)(uintptr_t)rsProgramRasterCreate((RsContext)con, pointSprite, (RsCullMode)cull);
1952}
1953
1954
1955// ---------------------------------------------------------------------------
1956
1957static void
1958nContextBindRootScript(JNIEnv *_env, jobject _this, jlong con, jlong script)
1959{
1960    if (kLogApi) {
1961        ALOGD("nContextBindRootScript, con(%p), script(%p)", (RsContext)con, (RsScript)script);
1962    }
1963    rsContextBindRootScript((RsContext)con, (RsScript)script);
1964}
1965
1966static void
1967nContextBindProgramStore(JNIEnv *_env, jobject _this, jlong con, jlong pfs)
1968{
1969    if (kLogApi) {
1970        ALOGD("nContextBindProgramStore, con(%p), pfs(%p)", (RsContext)con, (RsProgramStore)pfs);
1971    }
1972    rsContextBindProgramStore((RsContext)con, (RsProgramStore)pfs);
1973}
1974
1975static void
1976nContextBindProgramFragment(JNIEnv *_env, jobject _this, jlong con, jlong pf)
1977{
1978    if (kLogApi) {
1979        ALOGD("nContextBindProgramFragment, con(%p), pf(%p)", (RsContext)con,
1980              (RsProgramFragment)pf);
1981    }
1982    rsContextBindProgramFragment((RsContext)con, (RsProgramFragment)pf);
1983}
1984
1985static void
1986nContextBindProgramVertex(JNIEnv *_env, jobject _this, jlong con, jlong pf)
1987{
1988    if (kLogApi) {
1989        ALOGD("nContextBindProgramVertex, con(%p), pf(%p)", (RsContext)con, (RsProgramVertex)pf);
1990    }
1991    rsContextBindProgramVertex((RsContext)con, (RsProgramVertex)pf);
1992}
1993
1994static void
1995nContextBindProgramRaster(JNIEnv *_env, jobject _this, jlong con, jlong pf)
1996{
1997    if (kLogApi) {
1998        ALOGD("nContextBindProgramRaster, con(%p), pf(%p)", (RsContext)con, (RsProgramRaster)pf);
1999    }
2000    rsContextBindProgramRaster((RsContext)con, (RsProgramRaster)pf);
2001}
2002
2003
2004// ---------------------------------------------------------------------------
2005
2006static jlong
2007nSamplerCreate(JNIEnv *_env, jobject _this, jlong con, jint magFilter, jint minFilter,
2008               jint wrapS, jint wrapT, jint wrapR, jfloat aniso)
2009{
2010    if (kLogApi) {
2011        ALOGD("nSamplerCreate, con(%p)", (RsContext)con);
2012    }
2013    return (jlong)(uintptr_t)rsSamplerCreate((RsContext)con,
2014                                 (RsSamplerValue)magFilter,
2015                                 (RsSamplerValue)minFilter,
2016                                 (RsSamplerValue)wrapS,
2017                                 (RsSamplerValue)wrapT,
2018                                 (RsSamplerValue)wrapR,
2019                                 aniso);
2020}
2021
2022// ---------------------------------------------------------------------------
2023
2024static jlong
2025nPathCreate(JNIEnv *_env, jobject _this, jlong con, jint prim, jboolean isStatic, jlong _vtx, jlong _loop, jfloat q) {
2026    if (kLogApi) {
2027        ALOGD("nPathCreate, con(%p)", (RsContext)con);
2028    }
2029
2030    jlong id = (jlong)(uintptr_t)rsPathCreate((RsContext)con, (RsPathPrimitive)prim, isStatic,
2031                                   (RsAllocation)_vtx,
2032                                   (RsAllocation)_loop, q);
2033    return id;
2034}
2035
2036static jlong
2037nMeshCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _vtx, jlongArray _idx, jintArray _prim)
2038{
2039    if (kLogApi) {
2040        ALOGD("nMeshCreate, con(%p)", (RsContext)con);
2041    }
2042
2043    jint vtxLen = _env->GetArrayLength(_vtx);
2044    jlong *jVtxPtr = _env->GetLongArrayElements(_vtx, nullptr);
2045    RsAllocation* vtxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * vtxLen);
2046    for(int i = 0; i < vtxLen; ++i) {
2047        vtxPtr[i] = (RsAllocation)(uintptr_t)jVtxPtr[i];
2048    }
2049
2050    jint idxLen = _env->GetArrayLength(_idx);
2051    jlong *jIdxPtr = _env->GetLongArrayElements(_idx, nullptr);
2052    RsAllocation* idxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * idxLen);
2053    for(int i = 0; i < idxLen; ++i) {
2054        idxPtr[i] = (RsAllocation)(uintptr_t)jIdxPtr[i];
2055    }
2056
2057    jint primLen = _env->GetArrayLength(_prim);
2058    jint *primPtr = _env->GetIntArrayElements(_prim, nullptr);
2059
2060    jlong id = (jlong)(uintptr_t)rsMeshCreate((RsContext)con,
2061                               (RsAllocation *)vtxPtr, vtxLen,
2062                               (RsAllocation *)idxPtr, idxLen,
2063                               (uint32_t *)primPtr, primLen);
2064
2065    free(vtxPtr);
2066    free(idxPtr);
2067    _env->ReleaseLongArrayElements(_vtx, jVtxPtr, 0);
2068    _env->ReleaseLongArrayElements(_idx, jIdxPtr, 0);
2069    _env->ReleaseIntArrayElements(_prim, primPtr, 0);
2070    return id;
2071}
2072
2073static jint
2074nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
2075{
2076    if (kLogApi) {
2077        ALOGD("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2078    }
2079    jint vtxCount = 0;
2080    rsaMeshGetVertexBufferCount((RsContext)con, (RsMesh)mesh, &vtxCount);
2081    return vtxCount;
2082}
2083
2084static jint
2085nMeshGetIndexCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
2086{
2087    if (kLogApi) {
2088        ALOGD("nMeshGetIndexCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2089    }
2090    jint idxCount = 0;
2091    rsaMeshGetIndexCount((RsContext)con, (RsMesh)mesh, &idxCount);
2092    return idxCount;
2093}
2094
2095static void
2096nMeshGetVertices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _ids, jint numVtxIDs)
2097{
2098    if (kLogApi) {
2099        ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2100    }
2101
2102    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
2103    rsaMeshGetVertices((RsContext)con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
2104
2105    for(jint i = 0; i < numVtxIDs; i ++) {
2106        const jlong alloc = (jlong)(uintptr_t)allocs[i];
2107        _env->SetLongArrayRegion(_ids, i, 1, &alloc);
2108    }
2109
2110    free(allocs);
2111}
2112
2113static void
2114nMeshGetIndices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _idxIds, jintArray _primitives, jint numIndices)
2115{
2116    if (kLogApi) {
2117        ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2118    }
2119
2120    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
2121    uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
2122
2123    rsaMeshGetIndices((RsContext)con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
2124
2125    for(jint i = 0; i < numIndices; i ++) {
2126        const jlong alloc = (jlong)(uintptr_t)allocs[i];
2127        const jint prim = (jint)prims[i];
2128        _env->SetLongArrayRegion(_idxIds, i, 1, &alloc);
2129        _env->SetIntArrayRegion(_primitives, i, 1, &prim);
2130    }
2131
2132    free(allocs);
2133    free(prims);
2134}
2135
2136static jint
2137nSystemGetPointerSize(JNIEnv *_env, jobject _this) {
2138    return (jint)sizeof(void*);
2139}
2140
2141
2142// ---------------------------------------------------------------------------
2143
2144
2145static const char *classPathName = "android/renderscript/RenderScript";
2146
2147static JNINativeMethod methods[] = {
2148{"_nInit",                         "()V",                                     (void*)_nInit },
2149
2150{"nDeviceCreate",                  "()J",                                     (void*)nDeviceCreate },
2151{"nDeviceDestroy",                 "(J)V",                                    (void*)nDeviceDestroy },
2152{"nDeviceSetConfig",               "(JII)V",                                  (void*)nDeviceSetConfig },
2153{"nContextGetUserMessage",         "(J[I)I",                                  (void*)nContextGetUserMessage },
2154{"nContextGetErrorMessage",        "(J)Ljava/lang/String;",                   (void*)nContextGetErrorMessage },
2155{"nContextPeekMessage",            "(J[I)I",                                  (void*)nContextPeekMessage },
2156
2157{"nContextInitToClient",           "(J)V",                                    (void*)nContextInitToClient },
2158{"nContextDeinitToClient",         "(J)V",                                    (void*)nContextDeinitToClient },
2159
2160
2161// All methods below are thread protected in java.
2162{"rsnContextCreate",                 "(JIII)J",                               (void*)nContextCreate },
2163{"rsnContextCreateGL",               "(JIIIIIIIIIIIIFI)J",                    (void*)nContextCreateGL },
2164{"rsnContextFinish",                 "(J)V",                                  (void*)nContextFinish },
2165{"rsnContextSetPriority",            "(JI)V",                                 (void*)nContextSetPriority },
2166{"rsnContextSetSurface",             "(JIILandroid/view/Surface;)V",          (void*)nContextSetSurface },
2167{"rsnContextDestroy",                "(J)V",                                  (void*)nContextDestroy },
2168{"rsnContextDump",                   "(JI)V",                                 (void*)nContextDump },
2169{"rsnContextPause",                  "(J)V",                                  (void*)nContextPause },
2170{"rsnContextResume",                 "(J)V",                                  (void*)nContextResume },
2171{"rsnContextSendMessage",            "(JI[I)V",                               (void*)nContextSendMessage },
2172{"rsnClosureCreate",                 "(JJJ[J[J[I[J[J)J",                      (void*)nClosureCreate },
2173{"rsnInvokeClosureCreate",           "(JJ[B[J[J[I)J",                         (void*)nInvokeClosureCreate },
2174{"rsnClosureSetArg",                 "(JJIJI)V",                              (void*)nClosureSetArg },
2175{"rsnClosureSetGlobal",              "(JJJJI)V",                              (void*)nClosureSetGlobal },
2176{"rsnAssignName",                    "(JJ[B)V",                               (void*)nAssignName },
2177{"rsnGetName",                       "(JJ)Ljava/lang/String;",                (void*)nGetName },
2178{"rsnObjDestroy",                    "(JJ)V",                                 (void*)nObjDestroy },
2179
2180{"rsnFileA3DCreateFromFile",         "(JLjava/lang/String;)J",                (void*)nFileA3DCreateFromFile },
2181{"rsnFileA3DCreateFromAssetStream",  "(JJ)J",                                 (void*)nFileA3DCreateFromAssetStream },
2182{"rsnFileA3DCreateFromAsset",        "(JLandroid/content/res/AssetManager;Ljava/lang/String;)J",            (void*)nFileA3DCreateFromAsset },
2183{"rsnFileA3DGetNumIndexEntries",     "(JJ)I",                                 (void*)nFileA3DGetNumIndexEntries },
2184{"rsnFileA3DGetIndexEntries",        "(JJI[I[Ljava/lang/String;)V",           (void*)nFileA3DGetIndexEntries },
2185{"rsnFileA3DGetEntryByIndex",        "(JJI)J",                                (void*)nFileA3DGetEntryByIndex },
2186
2187{"rsnFontCreateFromFile",            "(JLjava/lang/String;FI)J",              (void*)nFontCreateFromFile },
2188{"rsnFontCreateFromAssetStream",     "(JLjava/lang/String;FIJ)J",             (void*)nFontCreateFromAssetStream },
2189{"rsnFontCreateFromAsset",        "(JLandroid/content/res/AssetManager;Ljava/lang/String;FI)J",            (void*)nFontCreateFromAsset },
2190
2191{"rsnElementCreate",                 "(JJIZI)J",                              (void*)nElementCreate },
2192{"rsnElementCreate2",                "(J[J[Ljava/lang/String;[I)J",           (void*)nElementCreate2 },
2193{"rsnElementGetNativeData",          "(JJ[I)V",                               (void*)nElementGetNativeData },
2194{"rsnElementGetSubElements",         "(JJ[J[Ljava/lang/String;[I)V",          (void*)nElementGetSubElements },
2195
2196{"rsnTypeCreate",                    "(JJIIIZZI)J",                           (void*)nTypeCreate },
2197{"rsnTypeGetNativeData",             "(JJ[J)V",                               (void*)nTypeGetNativeData },
2198
2199{"rsnAllocationCreateTyped",         "(JJIIJ)J",                               (void*)nAllocationCreateTyped },
2200{"rsnAllocationCreateFromBitmap",    "(JJILandroid/graphics/Bitmap;I)J",      (void*)nAllocationCreateFromBitmap },
2201{"rsnAllocationCreateBitmapBackedAllocation",    "(JJILandroid/graphics/Bitmap;I)J",      (void*)nAllocationCreateBitmapBackedAllocation },
2202{"rsnAllocationCubeCreateFromBitmap","(JJILandroid/graphics/Bitmap;I)J",      (void*)nAllocationCubeCreateFromBitmap },
2203
2204{"rsnAllocationCopyFromBitmap",      "(JJLandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyFromBitmap },
2205{"rsnAllocationCopyToBitmap",        "(JJLandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyToBitmap },
2206
2207{"rsnAllocationSyncAll",             "(JJI)V",                                (void*)nAllocationSyncAll },
2208{"rsnAllocationGetSurface",          "(JJ)Landroid/view/Surface;",            (void*)nAllocationGetSurface },
2209{"rsnAllocationSetSurface",          "(JJLandroid/view/Surface;)V",           (void*)nAllocationSetSurface },
2210{"rsnAllocationIoSend",              "(JJ)V",                                 (void*)nAllocationIoSend },
2211{"rsnAllocationIoReceive",           "(JJ)V",                                 (void*)nAllocationIoReceive },
2212{"rsnAllocationData1D",              "(JJIIILjava/lang/Object;II)V",          (void*)nAllocationData1D },
2213{"rsnAllocationElementData",         "(JJIIIII[BI)V",                         (void*)nAllocationElementData },
2214{"rsnAllocationData2D",              "(JJIIIIIILjava/lang/Object;II)V",       (void*)nAllocationData2D },
2215{"rsnAllocationData2D",              "(JJIIIIIIJIIII)V",                      (void*)nAllocationData2D_alloc },
2216{"rsnAllocationData3D",              "(JJIIIIIIILjava/lang/Object;II)V",      (void*)nAllocationData3D },
2217{"rsnAllocationData3D",              "(JJIIIIIIIJIIII)V",                     (void*)nAllocationData3D_alloc },
2218{"rsnAllocationRead",                "(JJLjava/lang/Object;I)V",              (void*)nAllocationRead },
2219{"rsnAllocationRead1D",              "(JJIIILjava/lang/Object;II)V",          (void*)nAllocationRead1D },
2220{"rsnAllocationElementRead",         "(JJIIIIILjava/lang/Object;II)V",        (void*)nAllocationElementRead },
2221{"rsnAllocationRead2D",              "(JJIIIIIILjava/lang/Object;II)V",       (void*)nAllocationRead2D },
2222{"rsnAllocationRead3D",              "(JJIIIIIIILjava/lang/Object;II)V",      (void*)nAllocationRead3D },
2223{"rsnAllocationGetType",             "(JJ)J",                                 (void*)nAllocationGetType},
2224{"rsnAllocationResize1D",            "(JJI)V",                                (void*)nAllocationResize1D },
2225{"rsnAllocationGenerateMipmaps",     "(JJ)V",                                 (void*)nAllocationGenerateMipmaps },
2226
2227{"rsnAllocationAdapterCreate",       "(JJJ)J",                                (void*)nAllocationAdapterCreate },
2228{"rsnAllocationAdapterOffset",       "(JJIIIIIIIII)V",                        (void*)nAllocationAdapterOffset },
2229
2230{"rsnScriptBindAllocation",          "(JJJI)V",                               (void*)nScriptBindAllocation },
2231{"rsnScriptSetTimeZone",             "(JJ[B)V",                               (void*)nScriptSetTimeZone },
2232{"rsnScriptInvoke",                  "(JJI)V",                                (void*)nScriptInvoke },
2233{"rsnScriptInvokeV",                 "(JJI[B)V",                              (void*)nScriptInvokeV },
2234
2235{"rsnScriptForEach",                 "(JJI[JJ[B[I)V",                         (void*)nScriptForEach },
2236
2237{"rsnScriptSetVarI",                 "(JJII)V",                               (void*)nScriptSetVarI },
2238{"rsnScriptGetVarI",                 "(JJI)I",                                (void*)nScriptGetVarI },
2239{"rsnScriptSetVarJ",                 "(JJIJ)V",                               (void*)nScriptSetVarJ },
2240{"rsnScriptGetVarJ",                 "(JJI)J",                                (void*)nScriptGetVarJ },
2241{"rsnScriptSetVarF",                 "(JJIF)V",                               (void*)nScriptSetVarF },
2242{"rsnScriptGetVarF",                 "(JJI)F",                                (void*)nScriptGetVarF },
2243{"rsnScriptSetVarD",                 "(JJID)V",                               (void*)nScriptSetVarD },
2244{"rsnScriptGetVarD",                 "(JJI)D",                                (void*)nScriptGetVarD },
2245{"rsnScriptSetVarV",                 "(JJI[B)V",                              (void*)nScriptSetVarV },
2246{"rsnScriptGetVarV",                 "(JJI[B)V",                              (void*)nScriptGetVarV },
2247{"rsnScriptSetVarVE",                "(JJI[BJ[I)V",                           (void*)nScriptSetVarVE },
2248{"rsnScriptSetVarObj",               "(JJIJ)V",                               (void*)nScriptSetVarObj },
2249
2250{"rsnScriptCCreate",                 "(JLjava/lang/String;Ljava/lang/String;[BI)J",  (void*)nScriptCCreate },
2251{"rsnScriptIntrinsicCreate",         "(JIJ)J",                                (void*)nScriptIntrinsicCreate },
2252{"rsnScriptKernelIDCreate",          "(JJII)J",                               (void*)nScriptKernelIDCreate },
2253{"rsnScriptInvokeIDCreate",          "(JJI)J",                                (void*)nScriptInvokeIDCreate },
2254{"rsnScriptFieldIDCreate",           "(JJI)J",                                (void*)nScriptFieldIDCreate },
2255{"rsnScriptGroupCreate",             "(J[J[J[J[J[J)J",                        (void*)nScriptGroupCreate },
2256{"rsnScriptGroup2Create",            "(JLjava/lang/String;[J)J",               (void*)nScriptGroup2Create },
2257{"rsnScriptGroupSetInput",           "(JJJJ)V",                               (void*)nScriptGroupSetInput },
2258{"rsnScriptGroupSetOutput",          "(JJJJ)V",                               (void*)nScriptGroupSetOutput },
2259{"rsnScriptGroupExecute",            "(JJ)V",                                 (void*)nScriptGroupExecute },
2260{"rsnScriptGroup2Execute",           "(JJ)V",                                 (void*)nScriptGroup2Execute },
2261
2262{"rsnScriptIntrinsicBLAS_Single",    "(JJIIIIIIIIIFJJFJIIII)V",               (void*)nScriptIntrinsicBLAS_Single },
2263{"rsnScriptIntrinsicBLAS_Double",    "(JJIIIIIIIIIDJJDJIIII)V",               (void*)nScriptIntrinsicBLAS_Double },
2264{"rsnScriptIntrinsicBLAS_Complex",   "(JJIIIIIIIIIFFJJFFJIIII)V",             (void*)nScriptIntrinsicBLAS_Complex },
2265{"rsnScriptIntrinsicBLAS_Z",         "(JJIIIIIIIIIDDJJDDJIIII)V",             (void*)nScriptIntrinsicBLAS_Z },
2266
2267{"rsnProgramStoreCreate",            "(JZZZZZZIII)J",                         (void*)nProgramStoreCreate },
2268
2269{"rsnProgramBindConstants",          "(JJIJ)V",                               (void*)nProgramBindConstants },
2270{"rsnProgramBindTexture",            "(JJIJ)V",                               (void*)nProgramBindTexture },
2271{"rsnProgramBindSampler",            "(JJIJ)V",                               (void*)nProgramBindSampler },
2272
2273{"rsnProgramFragmentCreate",         "(JLjava/lang/String;[Ljava/lang/String;[J)J",              (void*)nProgramFragmentCreate },
2274{"rsnProgramRasterCreate",           "(JZI)J",                                (void*)nProgramRasterCreate },
2275{"rsnProgramVertexCreate",           "(JLjava/lang/String;[Ljava/lang/String;[J)J",              (void*)nProgramVertexCreate },
2276
2277{"rsnContextBindRootScript",         "(JJ)V",                                 (void*)nContextBindRootScript },
2278{"rsnContextBindProgramStore",       "(JJ)V",                                 (void*)nContextBindProgramStore },
2279{"rsnContextBindProgramFragment",    "(JJ)V",                                 (void*)nContextBindProgramFragment },
2280{"rsnContextBindProgramVertex",      "(JJ)V",                                 (void*)nContextBindProgramVertex },
2281{"rsnContextBindProgramRaster",      "(JJ)V",                                 (void*)nContextBindProgramRaster },
2282
2283{"rsnSamplerCreate",                 "(JIIIIIF)J",                            (void*)nSamplerCreate },
2284
2285{"rsnPathCreate",                    "(JIZJJF)J",                             (void*)nPathCreate },
2286{"rsnMeshCreate",                    "(J[J[J[I)J",                            (void*)nMeshCreate },
2287
2288{"rsnMeshGetVertexBufferCount",      "(JJ)I",                                 (void*)nMeshGetVertexBufferCount },
2289{"rsnMeshGetIndexCount",             "(JJ)I",                                 (void*)nMeshGetIndexCount },
2290{"rsnMeshGetVertices",               "(JJ[JI)V",                              (void*)nMeshGetVertices },
2291{"rsnMeshGetIndices",                "(JJ[J[II)V",                            (void*)nMeshGetIndices },
2292
2293{"rsnSystemGetPointerSize",          "()I",                                   (void*)nSystemGetPointerSize },
2294};
2295
2296static int registerFuncs(JNIEnv *_env)
2297{
2298    return android::AndroidRuntime::registerNativeMethods(
2299            _env, classPathName, methods, NELEM(methods));
2300}
2301
2302// ---------------------------------------------------------------------------
2303
2304jint JNI_OnLoad(JavaVM* vm, void* reserved)
2305{
2306    JNIEnv* env = nullptr;
2307    jint result = -1;
2308
2309    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
2310        ALOGE("ERROR: GetEnv failed\n");
2311        goto bail;
2312    }
2313    assert(env != nullptr);
2314
2315    if (registerFuncs(env) < 0) {
2316        ALOGE("ERROR: Renderscript native registration failed\n");
2317        goto bail;
2318    }
2319
2320    /* success -- return valid version number */
2321    result = JNI_VERSION_1_4;
2322
2323bail:
2324    return result;
2325}
2326