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