android_renderscript_RenderScript.cpp revision 25207df658d6a8a3e885c7017fcc25702363583c
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
1029// Copies from the Java array data into the Allocation pointed to by alloc.
1030static void
1031//    native void rsnAllocationElementData1D(long con, long id, int xoff, int compIdx, byte[] d, int sizeBytes);
1032nAllocationElementData1D(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint offset, jint lod,
1033                         jint compIdx, jbyteArray data, jint sizeBytes)
1034{
1035    jint len = _env->GetArrayLength(data);
1036    if (kLogApi) {
1037        ALOGD("nAllocationElementData1D, con(%p), alloc(%p), offset(%i), comp(%i), len(%i), "
1038              "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, offset, compIdx, len,
1039              sizeBytes);
1040    }
1041    jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
1042    rsAllocation1DElementData((RsContext)con, (RsAllocation)alloc, offset, lod, ptr, sizeBytes, compIdx);
1043    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1044}
1045
1046// Copies from the Java object data into the Allocation pointed to by _alloc.
1047static void
1048nAllocationData2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
1049                  jint w, jint h, jobject data, jint sizeBytes, jint dataType)
1050{
1051    RsAllocation *alloc = (RsAllocation *)_alloc;
1052    RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
1053    if (kLogApi) {
1054        ALOGD("nAllocation2DData, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1055              "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1056    }
1057    PER_ARRAY_TYPE(nullptr, rsAllocation2DData, true, (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
1058}
1059
1060// Copies from the Allocation pointed to by srcAlloc into the Allocation
1061// pointed to by dstAlloc.
1062static void
1063nAllocationData2D_alloc(JNIEnv *_env, jobject _this, jlong con,
1064                        jlong dstAlloc, jint dstXoff, jint dstYoff,
1065                        jint dstMip, jint dstFace,
1066                        jint width, jint height,
1067                        jlong srcAlloc, jint srcXoff, jint srcYoff,
1068                        jint srcMip, jint srcFace)
1069{
1070    if (kLogApi) {
1071        ALOGD("nAllocation2DData_s, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1072              " dstMip(%i), dstFace(%i), width(%i), height(%i),"
1073              " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i), srcFace(%i)",
1074              (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip, dstFace,
1075              width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
1076    }
1077
1078    rsAllocationCopy2DRange((RsContext)con,
1079                            (RsAllocation)dstAlloc,
1080                            dstXoff, dstYoff,
1081                            dstMip, dstFace,
1082                            width, height,
1083                            (RsAllocation)srcAlloc,
1084                            srcXoff, srcYoff,
1085                            srcMip, srcFace);
1086}
1087
1088// Copies from the Java object data into the Allocation pointed to by _alloc.
1089static void
1090nAllocationData3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
1091                    jint w, jint h, jint d, jobject data, int sizeBytes, int dataType)
1092{
1093    RsAllocation *alloc = (RsAllocation *)_alloc;
1094    if (kLogApi) {
1095        ALOGD("nAllocation3DData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1096              " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1097              lod, w, h, d, sizeBytes);
1098    }
1099    PER_ARRAY_TYPE(nullptr, rsAllocation3DData, true, (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
1100}
1101
1102// Copies from the Allocation pointed to by srcAlloc into the Allocation
1103// pointed to by dstAlloc.
1104static void
1105nAllocationData3D_alloc(JNIEnv *_env, jobject _this, jlong con,
1106                        jlong dstAlloc, jint dstXoff, jint dstYoff, jint dstZoff,
1107                        jint dstMip,
1108                        jint width, jint height, jint depth,
1109                        jlong srcAlloc, jint srcXoff, jint srcYoff, jint srcZoff,
1110                        jint srcMip)
1111{
1112    if (kLogApi) {
1113        ALOGD("nAllocationData3D_alloc, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1114              " dstMip(%i), width(%i), height(%i),"
1115              " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i)",
1116              (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip,
1117              width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip);
1118    }
1119
1120    rsAllocationCopy3DRange((RsContext)con,
1121                            (RsAllocation)dstAlloc,
1122                            dstXoff, dstYoff, dstZoff, dstMip,
1123                            width, height, depth,
1124                            (RsAllocation)srcAlloc,
1125                            srcXoff, srcYoff, srcZoff, srcMip);
1126}
1127
1128
1129// Copies from the Allocation pointed to by _alloc into the Java object data.
1130static void
1131nAllocationRead(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jobject data, int dataType)
1132{
1133    RsAllocation *alloc = (RsAllocation *)_alloc;
1134    if (kLogApi) {
1135        ALOGD("nAllocationRead, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
1136    }
1137    PER_ARRAY_TYPE(0, rsAllocationRead, false, (RsContext)con, alloc, ptr, len * typeBytes);
1138}
1139
1140// Copies from the Allocation pointed to by _alloc into the Java object data.
1141static void
1142nAllocationRead1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
1143                  jint count, jobject data, int sizeBytes, int dataType)
1144{
1145    RsAllocation *alloc = (RsAllocation *)_alloc;
1146    if (kLogApi) {
1147        ALOGD("nAllocation1DRead, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1148              "dataType(%i)", (RsContext)con, alloc, offset, count, sizeBytes, dataType);
1149    }
1150    PER_ARRAY_TYPE(0, rsAllocation1DRead, false, (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
1151}
1152
1153// Copies from the Allocation pointed to by _alloc into the Java object data.
1154static void
1155nAllocationRead2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
1156                  jint w, jint h, jobject data, int sizeBytes, int dataType)
1157{
1158    RsAllocation *alloc = (RsAllocation *)_alloc;
1159    RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
1160    if (kLogApi) {
1161        ALOGD("nAllocation2DRead, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1162              "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1163    }
1164    PER_ARRAY_TYPE(0, rsAllocation2DRead, false, (RsContext)con, alloc, xoff, yoff, lod, face, w, h,
1165                   ptr, sizeBytes, 0);
1166}
1167
1168static jlong
1169nAllocationGetType(JNIEnv *_env, jobject _this, jlong con, jlong a)
1170{
1171    if (kLogApi) {
1172        ALOGD("nAllocationGetType, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1173    }
1174    return (jlong)(uintptr_t) rsaAllocationGetType((RsContext)con, (RsAllocation)a);
1175}
1176
1177static void
1178nAllocationResize1D(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint dimX)
1179{
1180    if (kLogApi) {
1181        ALOGD("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", (RsContext)con,
1182              (RsAllocation)alloc, dimX);
1183    }
1184    rsAllocationResize1D((RsContext)con, (RsAllocation)alloc, dimX);
1185}
1186
1187
1188static jlong
1189nAllocationAdapterCreate(JNIEnv *_env, jobject _this, jlong con, jlong basealloc, jlong type)
1190{
1191    if (kLogApi) {
1192        ALOGD("nAllocationAdapterCreate, con(%p), base(%p), type(%p)",
1193              (RsContext)con, (RsAllocation)basealloc, (RsElement)type);
1194    }
1195    return (jlong)(uintptr_t) rsAllocationAdapterCreate((RsContext)con, (RsType)type,
1196                                                        (RsAllocation)basealloc);
1197
1198}
1199
1200static void
1201nAllocationAdapterOffset(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1202                        jint x, jint y, jint z, jint face, jint lod,
1203                        jint a1, jint a2, jint a3, jint a4)
1204{
1205    uint32_t params[] = {
1206        (uint32_t)x, (uint32_t)y, (uint32_t)z, (uint32_t)face,
1207        (uint32_t)lod, (uint32_t)a1, (uint32_t)a2, (uint32_t)a3, (uint32_t)a4
1208    };
1209    if (kLogApi) {
1210        ALOGD("nAllocationAdapterOffset, con(%p), alloc(%p), x(%i), y(%i), z(%i), face(%i), lod(%i), arrays(%i %i %i %i)",
1211              (RsContext)con, (RsAllocation)alloc, x, y, z, face, lod, a1, a2, a3, a4);
1212    }
1213    rsAllocationAdapterOffset((RsContext)con, (RsAllocation)alloc,
1214                              params, sizeof(params));
1215}
1216
1217
1218// -----------------------------------
1219
1220static jlong
1221nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con, jlong native_asset)
1222{
1223    Asset* asset = reinterpret_cast<Asset*>(native_asset);
1224    ALOGV("______nFileA3D %p", asset);
1225
1226    jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromMemory((RsContext)con, asset->getBuffer(false), asset->getLength());
1227    return id;
1228}
1229
1230static jlong
1231nFileA3DCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path)
1232{
1233    AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
1234    if (mgr == nullptr) {
1235        return 0;
1236    }
1237
1238    AutoJavaStringToUTF8 str(_env, _path);
1239    Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
1240    if (asset == nullptr) {
1241        return 0;
1242    }
1243
1244    jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromAsset((RsContext)con, asset);
1245    return id;
1246}
1247
1248static jlong
1249nFileA3DCreateFromFile(JNIEnv *_env, jobject _this, jlong con, jstring fileName)
1250{
1251    AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
1252    jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromFile((RsContext)con, fileNameUTF.c_str());
1253
1254    return id;
1255}
1256
1257static jint
1258nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D)
1259{
1260    int32_t numEntries = 0;
1261    rsaFileA3DGetNumIndexEntries((RsContext)con, &numEntries, (RsFile)fileA3D);
1262    return (jint)numEntries;
1263}
1264
1265static void
1266nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
1267{
1268    ALOGV("______nFileA3D %p", (RsFile) fileA3D);
1269    RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
1270
1271    rsaFileA3DGetIndexEntries((RsContext)con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
1272
1273    for(jint i = 0; i < numEntries; i ++) {
1274        _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
1275        _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
1276    }
1277
1278    free(fileEntries);
1279}
1280
1281static jlong
1282nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint index)
1283{
1284    ALOGV("______nFileA3D %p", (RsFile) fileA3D);
1285    jlong id = (jlong)(uintptr_t)rsaFileA3DGetEntryByIndex((RsContext)con, (uint32_t)index, (RsFile)fileA3D);
1286    return id;
1287}
1288
1289// -----------------------------------
1290
1291static jlong
1292nFontCreateFromFile(JNIEnv *_env, jobject _this, jlong con,
1293                    jstring fileName, jfloat fontSize, jint dpi)
1294{
1295    AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
1296    jlong id = (jlong)(uintptr_t)rsFontCreateFromFile((RsContext)con,
1297                                         fileNameUTF.c_str(), fileNameUTF.length(),
1298                                         fontSize, dpi);
1299
1300    return id;
1301}
1302
1303static jlong
1304nFontCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con,
1305                           jstring name, jfloat fontSize, jint dpi, jlong native_asset)
1306{
1307    Asset* asset = reinterpret_cast<Asset*>(native_asset);
1308    AutoJavaStringToUTF8 nameUTF(_env, name);
1309
1310    jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
1311                                           nameUTF.c_str(), nameUTF.length(),
1312                                           fontSize, dpi,
1313                                           asset->getBuffer(false), asset->getLength());
1314    return id;
1315}
1316
1317static jlong
1318nFontCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path,
1319                     jfloat fontSize, jint dpi)
1320{
1321    AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
1322    if (mgr == nullptr) {
1323        return 0;
1324    }
1325
1326    AutoJavaStringToUTF8 str(_env, _path);
1327    Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
1328    if (asset == nullptr) {
1329        return 0;
1330    }
1331
1332    jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
1333                                           str.c_str(), str.length(),
1334                                           fontSize, dpi,
1335                                           asset->getBuffer(false), asset->getLength());
1336    delete asset;
1337    return id;
1338}
1339
1340// -----------------------------------
1341
1342static void
1343nScriptBindAllocation(JNIEnv *_env, jobject _this, jlong con, jlong script, jlong alloc, jint slot)
1344{
1345    if (kLogApi) {
1346        ALOGD("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", (RsContext)con,
1347              (RsScript)script, (RsAllocation)alloc, slot);
1348    }
1349    rsScriptBindAllocation((RsContext)con, (RsScript)script, (RsAllocation)alloc, slot);
1350}
1351
1352static void
1353nScriptSetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jint val)
1354{
1355    if (kLogApi) {
1356        ALOGD("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", (RsContext)con, (void *)script,
1357              slot, val);
1358    }
1359    rsScriptSetVarI((RsContext)con, (RsScript)script, slot, val);
1360}
1361
1362static jint
1363nScriptGetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
1364{
1365    if (kLogApi) {
1366        ALOGD("nScriptGetVarI, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1367    }
1368    int value = 0;
1369    rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
1370    return value;
1371}
1372
1373static void
1374nScriptSetVarObj(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
1375{
1376    if (kLogApi) {
1377        ALOGD("nScriptSetVarObj, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
1378              slot, val);
1379    }
1380    rsScriptSetVarObj((RsContext)con, (RsScript)script, slot, (RsObjectBase)val);
1381}
1382
1383static void
1384nScriptSetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
1385{
1386    if (kLogApi) {
1387        ALOGD("nScriptSetVarJ, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
1388              slot, val);
1389    }
1390    rsScriptSetVarJ((RsContext)con, (RsScript)script, slot, val);
1391}
1392
1393static jlong
1394nScriptGetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
1395{
1396    if (kLogApi) {
1397        ALOGD("nScriptGetVarJ, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1398    }
1399    jlong value = 0;
1400    rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
1401    return value;
1402}
1403
1404static void
1405nScriptSetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, float val)
1406{
1407    if (kLogApi) {
1408        ALOGD("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", (RsContext)con, (void *)script,
1409              slot, val);
1410    }
1411    rsScriptSetVarF((RsContext)con, (RsScript)script, slot, val);
1412}
1413
1414static jfloat
1415nScriptGetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
1416{
1417    if (kLogApi) {
1418        ALOGD("nScriptGetVarF, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1419    }
1420    jfloat value = 0;
1421    rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
1422    return value;
1423}
1424
1425static void
1426nScriptSetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, double val)
1427{
1428    if (kLogApi) {
1429        ALOGD("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", (RsContext)con, (void *)script,
1430              slot, val);
1431    }
1432    rsScriptSetVarD((RsContext)con, (RsScript)script, slot, val);
1433}
1434
1435static jdouble
1436nScriptGetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
1437{
1438    if (kLogApi) {
1439        ALOGD("nScriptGetVarD, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1440    }
1441    jdouble value = 0;
1442    rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
1443    return value;
1444}
1445
1446static void
1447nScriptSetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
1448{
1449    if (kLogApi) {
1450        ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1451    }
1452    jint len = _env->GetArrayLength(data);
1453    jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
1454    rsScriptSetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
1455    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1456}
1457
1458static void
1459nScriptGetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
1460{
1461    if (kLogApi) {
1462        ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1463    }
1464    jint len = _env->GetArrayLength(data);
1465    jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
1466    rsScriptGetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
1467    _env->ReleaseByteArrayElements(data, ptr, 0);
1468}
1469
1470static void
1471nScriptSetVarVE(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data,
1472                jlong elem, jintArray dims)
1473{
1474    if (kLogApi) {
1475        ALOGD("nScriptSetVarVE, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1476    }
1477    jint len = _env->GetArrayLength(data);
1478    jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
1479    jint dimsLen = _env->GetArrayLength(dims) * sizeof(int);
1480    jint *dimsPtr = _env->GetIntArrayElements(dims, nullptr);
1481    rsScriptSetVarVE((RsContext)con, (RsScript)script, slot, ptr, len, (RsElement)elem,
1482                     (const uint32_t*) dimsPtr, dimsLen);
1483    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1484    _env->ReleaseIntArrayElements(dims, dimsPtr, JNI_ABORT);
1485}
1486
1487
1488static void
1489nScriptSetTimeZone(JNIEnv *_env, jobject _this, jlong con, jlong script, jbyteArray timeZone)
1490{
1491    if (kLogApi) {
1492        ALOGD("nScriptCSetTimeZone, con(%p), s(%p)", (RsContext)con, (void *)script);
1493    }
1494
1495    jint length = _env->GetArrayLength(timeZone);
1496    jbyte* timeZone_ptr;
1497    timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
1498
1499    rsScriptSetTimeZone((RsContext)con, (RsScript)script, (const char *)timeZone_ptr, length);
1500
1501    if (timeZone_ptr) {
1502        _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
1503    }
1504}
1505
1506static void
1507nScriptInvoke(JNIEnv *_env, jobject _this, jlong con, jlong obj, jint slot)
1508{
1509    if (kLogApi) {
1510        ALOGD("nScriptInvoke, con(%p), script(%p)", (RsContext)con, (void *)obj);
1511    }
1512    rsScriptInvoke((RsContext)con, (RsScript)obj, slot);
1513}
1514
1515static void
1516nScriptInvokeV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
1517{
1518    if (kLogApi) {
1519        ALOGD("nScriptInvokeV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1520    }
1521    jint len = _env->GetArrayLength(data);
1522    jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
1523    rsScriptInvokeV((RsContext)con, (RsScript)script, slot, ptr, len);
1524    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1525}
1526
1527static void
1528nScriptForEach(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
1529               jlongArray ains, jlong aout, jbyteArray params,
1530               jintArray limits)
1531{
1532    if (kLogApi) {
1533        ALOGD("nScriptForEach, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1534    }
1535
1536    jint   in_len = 0;
1537    jlong *in_ptr = nullptr;
1538
1539    RsAllocation *in_allocs = nullptr;
1540
1541    if (ains != nullptr) {
1542        in_len = _env->GetArrayLength(ains);
1543        in_ptr = _env->GetLongArrayElements(ains, nullptr);
1544
1545        if (sizeof(RsAllocation) == sizeof(jlong)) {
1546            in_allocs = (RsAllocation*)in_ptr;
1547
1548        } else {
1549            // Convert from 64-bit jlong types to the native pointer type.
1550
1551            in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
1552
1553            for (int index = in_len; --index >= 0;) {
1554                in_allocs[index] = (RsAllocation)in_ptr[index];
1555            }
1556        }
1557    }
1558
1559    jint   param_len = 0;
1560    jbyte *param_ptr = nullptr;
1561
1562    if (params != nullptr) {
1563        param_len = _env->GetArrayLength(params);
1564        param_ptr = _env->GetByteArrayElements(params, nullptr);
1565    }
1566
1567    RsScriptCall sc, *sca = nullptr;
1568    uint32_t sc_size = 0;
1569
1570    jint  limit_len = 0;
1571    jint *limit_ptr = nullptr;
1572
1573    if (limits != nullptr) {
1574        limit_len = _env->GetArrayLength(limits);
1575        limit_ptr = _env->GetIntArrayElements(limits, nullptr);
1576
1577        assert(limit_len == 6);
1578        UNUSED(limit_len);  // As the assert might not be compiled.
1579
1580        sc.xStart     = limit_ptr[0];
1581        sc.xEnd       = limit_ptr[1];
1582        sc.yStart     = limit_ptr[2];
1583        sc.yEnd       = limit_ptr[3];
1584        sc.zStart     = limit_ptr[4];
1585        sc.zEnd       = limit_ptr[5];
1586        sc.strategy   = RS_FOR_EACH_STRATEGY_DONT_CARE;
1587        sc.arrayStart = 0;
1588        sc.arrayEnd = 0;
1589        sc.array2Start = 0;
1590        sc.array2End = 0;
1591        sc.array3Start = 0;
1592        sc.array3End = 0;
1593        sc.array4Start = 0;
1594        sc.array4End = 0;
1595
1596        sca = &sc;
1597    }
1598
1599    rsScriptForEachMulti((RsContext)con, (RsScript)script, slot,
1600                         in_allocs, in_len, (RsAllocation)aout,
1601                         param_ptr, param_len, sca, sc_size);
1602
1603    if (ains != nullptr) {
1604        _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
1605    }
1606
1607    if (params != nullptr) {
1608        _env->ReleaseByteArrayElements(params, param_ptr, JNI_ABORT);
1609    }
1610
1611    if (limits != nullptr) {
1612        _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
1613    }
1614}
1615
1616// -----------------------------------
1617
1618static jlong
1619nScriptCCreate(JNIEnv *_env, jobject _this, jlong con,
1620               jstring resName, jstring cacheDir,
1621               jbyteArray scriptRef, jint length)
1622{
1623    if (kLogApi) {
1624        ALOGD("nScriptCCreate, con(%p)", (RsContext)con);
1625    }
1626
1627    AutoJavaStringToUTF8 resNameUTF(_env, resName);
1628    AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
1629    jlong ret = 0;
1630    jbyte* script_ptr = nullptr;
1631    jint _exception = 0;
1632    jint remaining;
1633    if (!scriptRef) {
1634        _exception = 1;
1635        //jniThrowException(_env, "java/lang/IllegalArgumentException", "script == null");
1636        goto exit;
1637    }
1638    if (length < 0) {
1639        _exception = 1;
1640        //jniThrowException(_env, "java/lang/IllegalArgumentException", "length < 0");
1641        goto exit;
1642    }
1643    remaining = _env->GetArrayLength(scriptRef);
1644    if (remaining < length) {
1645        _exception = 1;
1646        //jniThrowException(_env, "java/lang/IllegalArgumentException",
1647        //        "length > script.length - offset");
1648        goto exit;
1649    }
1650    script_ptr = (jbyte *)
1651        _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
1652
1653    //rsScriptCSetText((RsContext)con, (const char *)script_ptr, length);
1654
1655    ret = (jlong)(uintptr_t)rsScriptCCreate((RsContext)con,
1656                                resNameUTF.c_str(), resNameUTF.length(),
1657                                cacheDirUTF.c_str(), cacheDirUTF.length(),
1658                                (const char *)script_ptr, length);
1659
1660exit:
1661    if (script_ptr) {
1662        _env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr,
1663                _exception ? JNI_ABORT: 0);
1664    }
1665
1666    return (jlong)(uintptr_t)ret;
1667}
1668
1669static jlong
1670nScriptIntrinsicCreate(JNIEnv *_env, jobject _this, jlong con, jint id, jlong eid)
1671{
1672    if (kLogApi) {
1673        ALOGD("nScriptIntrinsicCreate, con(%p) id(%i) element(%p)", (RsContext)con, id,
1674              (void *)eid);
1675    }
1676    return (jlong)(uintptr_t)rsScriptIntrinsicCreate((RsContext)con, id, (RsElement)eid);
1677}
1678
1679static jlong
1680nScriptKernelIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot, jint sig)
1681{
1682    if (kLogApi) {
1683        ALOGD("nScriptKernelIDCreate, con(%p) script(%p), slot(%i), sig(%i)", (RsContext)con,
1684              (void *)sid, slot, sig);
1685    }
1686    return (jlong)(uintptr_t)rsScriptKernelIDCreate((RsContext)con, (RsScript)sid, slot, sig);
1687}
1688
1689static jlong
1690nScriptInvokeIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
1691{
1692    if (kLogApi) {
1693        ALOGD("nScriptInvokeIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con,
1694              (void *)sid, slot);
1695    }
1696    return (jlong)(uintptr_t)rsScriptInvokeIDCreate((RsContext)con, (RsScript)sid, slot);
1697}
1698
1699static jlong
1700nScriptFieldIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
1701{
1702    if (kLogApi) {
1703        ALOGD("nScriptFieldIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con, (void *)sid,
1704              slot);
1705    }
1706    return (jlong)(uintptr_t)rsScriptFieldIDCreate((RsContext)con, (RsScript)sid, slot);
1707}
1708
1709static jlong
1710nScriptGroupCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _kernels, jlongArray _src,
1711    jlongArray _dstk, jlongArray _dstf, jlongArray _types)
1712{
1713    if (kLogApi) {
1714        ALOGD("nScriptGroupCreate, con(%p)", (RsContext)con);
1715    }
1716
1717    jint kernelsLen = _env->GetArrayLength(_kernels);
1718    jlong *jKernelsPtr = _env->GetLongArrayElements(_kernels, nullptr);
1719    RsScriptKernelID* kernelsPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * kernelsLen);
1720    for(int i = 0; i < kernelsLen; ++i) {
1721        kernelsPtr[i] = (RsScriptKernelID)jKernelsPtr[i];
1722    }
1723
1724    jint srcLen = _env->GetArrayLength(_src);
1725    jlong *jSrcPtr = _env->GetLongArrayElements(_src, nullptr);
1726    RsScriptKernelID* srcPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * srcLen);
1727    for(int i = 0; i < srcLen; ++i) {
1728        srcPtr[i] = (RsScriptKernelID)jSrcPtr[i];
1729    }
1730
1731    jint dstkLen = _env->GetArrayLength(_dstk);
1732    jlong *jDstkPtr = _env->GetLongArrayElements(_dstk, nullptr);
1733    RsScriptKernelID* dstkPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstkLen);
1734    for(int i = 0; i < dstkLen; ++i) {
1735        dstkPtr[i] = (RsScriptKernelID)jDstkPtr[i];
1736    }
1737
1738    jint dstfLen = _env->GetArrayLength(_dstf);
1739    jlong *jDstfPtr = _env->GetLongArrayElements(_dstf, nullptr);
1740    RsScriptKernelID* dstfPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstfLen);
1741    for(int i = 0; i < dstfLen; ++i) {
1742        dstfPtr[i] = (RsScriptKernelID)jDstfPtr[i];
1743    }
1744
1745    jint typesLen = _env->GetArrayLength(_types);
1746    jlong *jTypesPtr = _env->GetLongArrayElements(_types, nullptr);
1747    RsType* typesPtr = (RsType*) malloc(sizeof(RsType) * typesLen);
1748    for(int i = 0; i < typesLen; ++i) {
1749        typesPtr[i] = (RsType)jTypesPtr[i];
1750    }
1751
1752    jlong id = (jlong)(uintptr_t)rsScriptGroupCreate((RsContext)con,
1753                               (RsScriptKernelID *)kernelsPtr, kernelsLen * sizeof(RsScriptKernelID),
1754                               (RsScriptKernelID *)srcPtr, srcLen * sizeof(RsScriptKernelID),
1755                               (RsScriptKernelID *)dstkPtr, dstkLen * sizeof(RsScriptKernelID),
1756                               (RsScriptFieldID *)dstfPtr, dstfLen * sizeof(RsScriptKernelID),
1757                               (RsType *)typesPtr, typesLen * sizeof(RsType));
1758
1759    free(kernelsPtr);
1760    free(srcPtr);
1761    free(dstkPtr);
1762    free(dstfPtr);
1763    free(typesPtr);
1764    _env->ReleaseLongArrayElements(_kernels, jKernelsPtr, 0);
1765    _env->ReleaseLongArrayElements(_src, jSrcPtr, 0);
1766    _env->ReleaseLongArrayElements(_dstk, jDstkPtr, 0);
1767    _env->ReleaseLongArrayElements(_dstf, jDstfPtr, 0);
1768    _env->ReleaseLongArrayElements(_types, jTypesPtr, 0);
1769    return id;
1770}
1771
1772static void
1773nScriptGroupSetInput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
1774{
1775    if (kLogApi) {
1776        ALOGD("nScriptGroupSetInput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
1777              (void *)gid, (void *)kid, (void *)alloc);
1778    }
1779    rsScriptGroupSetInput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
1780}
1781
1782static void
1783nScriptGroupSetOutput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
1784{
1785    if (kLogApi) {
1786        ALOGD("nScriptGroupSetOutput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
1787              (void *)gid, (void *)kid, (void *)alloc);
1788    }
1789    rsScriptGroupSetOutput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
1790}
1791
1792static void
1793nScriptGroupExecute(JNIEnv *_env, jobject _this, jlong con, jlong gid)
1794{
1795    if (kLogApi) {
1796        ALOGD("nScriptGroupSetOutput, con(%p) group(%p)", (RsContext)con, (void *)gid);
1797    }
1798    rsScriptGroupExecute((RsContext)con, (RsScriptGroup)gid);
1799}
1800
1801// ---------------------------------------------------------------------------
1802
1803static jlong
1804nProgramStoreCreate(JNIEnv *_env, jobject _this, jlong con,
1805                    jboolean colorMaskR, jboolean colorMaskG, jboolean colorMaskB, jboolean colorMaskA,
1806                    jboolean depthMask, jboolean ditherEnable,
1807                    jint srcFunc, jint destFunc,
1808                    jint depthFunc)
1809{
1810    if (kLogApi) {
1811        ALOGD("nProgramStoreCreate, con(%p)", (RsContext)con);
1812    }
1813    return (jlong)(uintptr_t)rsProgramStoreCreate((RsContext)con, colorMaskR, colorMaskG, colorMaskB, colorMaskA,
1814                                      depthMask, ditherEnable, (RsBlendSrcFunc)srcFunc,
1815                                      (RsBlendDstFunc)destFunc, (RsDepthFunc)depthFunc);
1816}
1817
1818// ---------------------------------------------------------------------------
1819
1820static void
1821nProgramBindConstants(JNIEnv *_env, jobject _this, jlong con, jlong vpv, jint slot, jlong a)
1822{
1823    if (kLogApi) {
1824        ALOGD("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", (RsContext)con,
1825              (RsProgramVertex)vpv, slot, (RsAllocation)a);
1826    }
1827    rsProgramBindConstants((RsContext)con, (RsProgram)vpv, slot, (RsAllocation)a);
1828}
1829
1830static void
1831nProgramBindTexture(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
1832{
1833    if (kLogApi) {
1834        ALOGD("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
1835              (RsProgramFragment)vpf, slot, (RsAllocation)a);
1836    }
1837    rsProgramBindTexture((RsContext)con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
1838}
1839
1840static void
1841nProgramBindSampler(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
1842{
1843    if (kLogApi) {
1844        ALOGD("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
1845              (RsProgramFragment)vpf, slot, (RsSampler)a);
1846    }
1847    rsProgramBindSampler((RsContext)con, (RsProgramFragment)vpf, slot, (RsSampler)a);
1848}
1849
1850// ---------------------------------------------------------------------------
1851
1852static jlong
1853nProgramFragmentCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
1854                       jobjectArray texNames, jlongArray params)
1855{
1856    AutoJavaStringToUTF8 shaderUTF(_env, shader);
1857    jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
1858    jint paramLen = _env->GetArrayLength(params);
1859
1860    int texCount = _env->GetArrayLength(texNames);
1861    AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
1862    const char ** nameArray = names.c_str();
1863    size_t* sizeArray = names.c_str_len();
1864
1865    if (kLogApi) {
1866        ALOGD("nProgramFragmentCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
1867    }
1868
1869    uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
1870    for(int i = 0; i < paramLen; ++i) {
1871        paramPtr[i] = (uintptr_t)jParamPtr[i];
1872    }
1873    jlong ret = (jlong)(uintptr_t)rsProgramFragmentCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
1874                                             nameArray, texCount, sizeArray,
1875                                             paramPtr, paramLen);
1876
1877    free(paramPtr);
1878    _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
1879    return ret;
1880}
1881
1882
1883// ---------------------------------------------------------------------------
1884
1885static jlong
1886nProgramVertexCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
1887                     jobjectArray texNames, jlongArray params)
1888{
1889    AutoJavaStringToUTF8 shaderUTF(_env, shader);
1890    jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
1891    jint paramLen = _env->GetArrayLength(params);
1892
1893    if (kLogApi) {
1894        ALOGD("nProgramVertexCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
1895    }
1896
1897    int texCount = _env->GetArrayLength(texNames);
1898    AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
1899    const char ** nameArray = names.c_str();
1900    size_t* sizeArray = names.c_str_len();
1901
1902    uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
1903    for(int i = 0; i < paramLen; ++i) {
1904        paramPtr[i] = (uintptr_t)jParamPtr[i];
1905    }
1906
1907    jlong ret = (jlong)(uintptr_t)rsProgramVertexCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
1908                                           nameArray, texCount, sizeArray,
1909                                           paramPtr, paramLen);
1910
1911    free(paramPtr);
1912    _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
1913    return ret;
1914}
1915
1916// ---------------------------------------------------------------------------
1917
1918static jlong
1919nProgramRasterCreate(JNIEnv *_env, jobject _this, jlong con, jboolean pointSprite, jint cull)
1920{
1921    if (kLogApi) {
1922        ALOGD("nProgramRasterCreate, con(%p), pointSprite(%i), cull(%i)", (RsContext)con,
1923              pointSprite, cull);
1924    }
1925    return (jlong)(uintptr_t)rsProgramRasterCreate((RsContext)con, pointSprite, (RsCullMode)cull);
1926}
1927
1928
1929// ---------------------------------------------------------------------------
1930
1931static void
1932nContextBindRootScript(JNIEnv *_env, jobject _this, jlong con, jlong script)
1933{
1934    if (kLogApi) {
1935        ALOGD("nContextBindRootScript, con(%p), script(%p)", (RsContext)con, (RsScript)script);
1936    }
1937    rsContextBindRootScript((RsContext)con, (RsScript)script);
1938}
1939
1940static void
1941nContextBindProgramStore(JNIEnv *_env, jobject _this, jlong con, jlong pfs)
1942{
1943    if (kLogApi) {
1944        ALOGD("nContextBindProgramStore, con(%p), pfs(%p)", (RsContext)con, (RsProgramStore)pfs);
1945    }
1946    rsContextBindProgramStore((RsContext)con, (RsProgramStore)pfs);
1947}
1948
1949static void
1950nContextBindProgramFragment(JNIEnv *_env, jobject _this, jlong con, jlong pf)
1951{
1952    if (kLogApi) {
1953        ALOGD("nContextBindProgramFragment, con(%p), pf(%p)", (RsContext)con,
1954              (RsProgramFragment)pf);
1955    }
1956    rsContextBindProgramFragment((RsContext)con, (RsProgramFragment)pf);
1957}
1958
1959static void
1960nContextBindProgramVertex(JNIEnv *_env, jobject _this, jlong con, jlong pf)
1961{
1962    if (kLogApi) {
1963        ALOGD("nContextBindProgramVertex, con(%p), pf(%p)", (RsContext)con, (RsProgramVertex)pf);
1964    }
1965    rsContextBindProgramVertex((RsContext)con, (RsProgramVertex)pf);
1966}
1967
1968static void
1969nContextBindProgramRaster(JNIEnv *_env, jobject _this, jlong con, jlong pf)
1970{
1971    if (kLogApi) {
1972        ALOGD("nContextBindProgramRaster, con(%p), pf(%p)", (RsContext)con, (RsProgramRaster)pf);
1973    }
1974    rsContextBindProgramRaster((RsContext)con, (RsProgramRaster)pf);
1975}
1976
1977
1978// ---------------------------------------------------------------------------
1979
1980static jlong
1981nSamplerCreate(JNIEnv *_env, jobject _this, jlong con, jint magFilter, jint minFilter,
1982               jint wrapS, jint wrapT, jint wrapR, jfloat aniso)
1983{
1984    if (kLogApi) {
1985        ALOGD("nSamplerCreate, con(%p)", (RsContext)con);
1986    }
1987    return (jlong)(uintptr_t)rsSamplerCreate((RsContext)con,
1988                                 (RsSamplerValue)magFilter,
1989                                 (RsSamplerValue)minFilter,
1990                                 (RsSamplerValue)wrapS,
1991                                 (RsSamplerValue)wrapT,
1992                                 (RsSamplerValue)wrapR,
1993                                 aniso);
1994}
1995
1996// ---------------------------------------------------------------------------
1997
1998static jlong
1999nPathCreate(JNIEnv *_env, jobject _this, jlong con, jint prim, jboolean isStatic, jlong _vtx, jlong _loop, jfloat q) {
2000    if (kLogApi) {
2001        ALOGD("nPathCreate, con(%p)", (RsContext)con);
2002    }
2003
2004    jlong id = (jlong)(uintptr_t)rsPathCreate((RsContext)con, (RsPathPrimitive)prim, isStatic,
2005                                   (RsAllocation)_vtx,
2006                                   (RsAllocation)_loop, q);
2007    return id;
2008}
2009
2010static jlong
2011nMeshCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _vtx, jlongArray _idx, jintArray _prim)
2012{
2013    if (kLogApi) {
2014        ALOGD("nMeshCreate, con(%p)", (RsContext)con);
2015    }
2016
2017    jint vtxLen = _env->GetArrayLength(_vtx);
2018    jlong *jVtxPtr = _env->GetLongArrayElements(_vtx, nullptr);
2019    RsAllocation* vtxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * vtxLen);
2020    for(int i = 0; i < vtxLen; ++i) {
2021        vtxPtr[i] = (RsAllocation)(uintptr_t)jVtxPtr[i];
2022    }
2023
2024    jint idxLen = _env->GetArrayLength(_idx);
2025    jlong *jIdxPtr = _env->GetLongArrayElements(_idx, nullptr);
2026    RsAllocation* idxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * idxLen);
2027    for(int i = 0; i < idxLen; ++i) {
2028        idxPtr[i] = (RsAllocation)(uintptr_t)jIdxPtr[i];
2029    }
2030
2031    jint primLen = _env->GetArrayLength(_prim);
2032    jint *primPtr = _env->GetIntArrayElements(_prim, nullptr);
2033
2034    jlong id = (jlong)(uintptr_t)rsMeshCreate((RsContext)con,
2035                               (RsAllocation *)vtxPtr, vtxLen,
2036                               (RsAllocation *)idxPtr, idxLen,
2037                               (uint32_t *)primPtr, primLen);
2038
2039    free(vtxPtr);
2040    free(idxPtr);
2041    _env->ReleaseLongArrayElements(_vtx, jVtxPtr, 0);
2042    _env->ReleaseLongArrayElements(_idx, jIdxPtr, 0);
2043    _env->ReleaseIntArrayElements(_prim, primPtr, 0);
2044    return id;
2045}
2046
2047static jint
2048nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
2049{
2050    if (kLogApi) {
2051        ALOGD("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2052    }
2053    jint vtxCount = 0;
2054    rsaMeshGetVertexBufferCount((RsContext)con, (RsMesh)mesh, &vtxCount);
2055    return vtxCount;
2056}
2057
2058static jint
2059nMeshGetIndexCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
2060{
2061    if (kLogApi) {
2062        ALOGD("nMeshGetIndexCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2063    }
2064    jint idxCount = 0;
2065    rsaMeshGetIndexCount((RsContext)con, (RsMesh)mesh, &idxCount);
2066    return idxCount;
2067}
2068
2069static void
2070nMeshGetVertices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _ids, jint numVtxIDs)
2071{
2072    if (kLogApi) {
2073        ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2074    }
2075
2076    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
2077    rsaMeshGetVertices((RsContext)con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
2078
2079    for(jint i = 0; i < numVtxIDs; i ++) {
2080        const jlong alloc = (jlong)(uintptr_t)allocs[i];
2081        _env->SetLongArrayRegion(_ids, i, 1, &alloc);
2082    }
2083
2084    free(allocs);
2085}
2086
2087static void
2088nMeshGetIndices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _idxIds, jintArray _primitives, jint numIndices)
2089{
2090    if (kLogApi) {
2091        ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2092    }
2093
2094    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
2095    uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
2096
2097    rsaMeshGetIndices((RsContext)con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
2098
2099    for(jint i = 0; i < numIndices; i ++) {
2100        const jlong alloc = (jlong)(uintptr_t)allocs[i];
2101        const jint prim = (jint)prims[i];
2102        _env->SetLongArrayRegion(_idxIds, i, 1, &alloc);
2103        _env->SetIntArrayRegion(_primitives, i, 1, &prim);
2104    }
2105
2106    free(allocs);
2107    free(prims);
2108}
2109
2110static jint
2111nSystemGetPointerSize(JNIEnv *_env, jobject _this) {
2112    return (jint)sizeof(void*);
2113}
2114
2115
2116// ---------------------------------------------------------------------------
2117
2118
2119static const char *classPathName = "android/renderscript/RenderScript";
2120
2121static JNINativeMethod methods[] = {
2122{"_nInit",                         "()V",                                     (void*)_nInit },
2123
2124{"nDeviceCreate",                  "()J",                                     (void*)nDeviceCreate },
2125{"nDeviceDestroy",                 "(J)V",                                    (void*)nDeviceDestroy },
2126{"nDeviceSetConfig",               "(JII)V",                                  (void*)nDeviceSetConfig },
2127{"nContextGetUserMessage",         "(J[I)I",                                  (void*)nContextGetUserMessage },
2128{"nContextGetErrorMessage",        "(J)Ljava/lang/String;",                   (void*)nContextGetErrorMessage },
2129{"nContextPeekMessage",            "(J[I)I",                                  (void*)nContextPeekMessage },
2130
2131{"nContextInitToClient",           "(J)V",                                    (void*)nContextInitToClient },
2132{"nContextDeinitToClient",         "(J)V",                                    (void*)nContextDeinitToClient },
2133
2134
2135// All methods below are thread protected in java.
2136{"rsnContextCreate",                 "(JIII)J",                               (void*)nContextCreate },
2137{"rsnContextCreateGL",               "(JIIIIIIIIIIIIFI)J",                    (void*)nContextCreateGL },
2138{"rsnContextFinish",                 "(J)V",                                  (void*)nContextFinish },
2139{"rsnContextSetPriority",            "(JI)V",                                 (void*)nContextSetPriority },
2140{"rsnContextSetSurface",             "(JIILandroid/view/Surface;)V",          (void*)nContextSetSurface },
2141{"rsnContextDestroy",                "(J)V",                                  (void*)nContextDestroy },
2142{"rsnContextDump",                   "(JI)V",                                 (void*)nContextDump },
2143{"rsnContextPause",                  "(J)V",                                  (void*)nContextPause },
2144{"rsnContextResume",                 "(J)V",                                  (void*)nContextResume },
2145{"rsnContextSendMessage",            "(JI[I)V",                               (void*)nContextSendMessage },
2146{"rsnClosureCreate",                 "(JJJ[J[J[I[J[J)J",                      (void*)nClosureCreate },
2147{"rsnInvokeClosureCreate",           "(JJ[B[J[J[I)J",                         (void*)nInvokeClosureCreate },
2148{"rsnClosureSetArg",                 "(JJIJI)V",                              (void*)nClosureSetArg },
2149{"rsnClosureSetGlobal",              "(JJJJI)V",                              (void*)nClosureSetGlobal },
2150{"rsnAssignName",                    "(JJ[B)V",                               (void*)nAssignName },
2151{"rsnGetName",                       "(JJ)Ljava/lang/String;",                (void*)nGetName },
2152{"rsnObjDestroy",                    "(JJ)V",                                 (void*)nObjDestroy },
2153
2154{"rsnFileA3DCreateFromFile",         "(JLjava/lang/String;)J",                (void*)nFileA3DCreateFromFile },
2155{"rsnFileA3DCreateFromAssetStream",  "(JJ)J",                                 (void*)nFileA3DCreateFromAssetStream },
2156{"rsnFileA3DCreateFromAsset",        "(JLandroid/content/res/AssetManager;Ljava/lang/String;)J",            (void*)nFileA3DCreateFromAsset },
2157{"rsnFileA3DGetNumIndexEntries",     "(JJ)I",                                 (void*)nFileA3DGetNumIndexEntries },
2158{"rsnFileA3DGetIndexEntries",        "(JJI[I[Ljava/lang/String;)V",           (void*)nFileA3DGetIndexEntries },
2159{"rsnFileA3DGetEntryByIndex",        "(JJI)J",                                (void*)nFileA3DGetEntryByIndex },
2160
2161{"rsnFontCreateFromFile",            "(JLjava/lang/String;FI)J",              (void*)nFontCreateFromFile },
2162{"rsnFontCreateFromAssetStream",     "(JLjava/lang/String;FIJ)J",             (void*)nFontCreateFromAssetStream },
2163{"rsnFontCreateFromAsset",        "(JLandroid/content/res/AssetManager;Ljava/lang/String;FI)J",            (void*)nFontCreateFromAsset },
2164
2165{"rsnElementCreate",                 "(JJIZI)J",                              (void*)nElementCreate },
2166{"rsnElementCreate2",                "(J[J[Ljava/lang/String;[I)J",           (void*)nElementCreate2 },
2167{"rsnElementGetNativeData",          "(JJ[I)V",                               (void*)nElementGetNativeData },
2168{"rsnElementGetSubElements",         "(JJ[J[Ljava/lang/String;[I)V",          (void*)nElementGetSubElements },
2169
2170{"rsnTypeCreate",                    "(JJIIIZZI)J",                           (void*)nTypeCreate },
2171{"rsnTypeGetNativeData",             "(JJ[J)V",                               (void*)nTypeGetNativeData },
2172
2173{"rsnAllocationCreateTyped",         "(JJIIJ)J",                               (void*)nAllocationCreateTyped },
2174{"rsnAllocationCreateFromBitmap",    "(JJILandroid/graphics/Bitmap;I)J",      (void*)nAllocationCreateFromBitmap },
2175{"rsnAllocationCreateBitmapBackedAllocation",    "(JJILandroid/graphics/Bitmap;I)J",      (void*)nAllocationCreateBitmapBackedAllocation },
2176{"rsnAllocationCubeCreateFromBitmap","(JJILandroid/graphics/Bitmap;I)J",      (void*)nAllocationCubeCreateFromBitmap },
2177
2178{"rsnAllocationCopyFromBitmap",      "(JJLandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyFromBitmap },
2179{"rsnAllocationCopyToBitmap",        "(JJLandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyToBitmap },
2180
2181{"rsnAllocationSyncAll",             "(JJI)V",                                (void*)nAllocationSyncAll },
2182{"rsnAllocationGetSurface",          "(JJ)Landroid/view/Surface;",            (void*)nAllocationGetSurface },
2183{"rsnAllocationSetSurface",          "(JJLandroid/view/Surface;)V",           (void*)nAllocationSetSurface },
2184{"rsnAllocationIoSend",              "(JJ)V",                                 (void*)nAllocationIoSend },
2185{"rsnAllocationIoReceive",           "(JJ)V",                                 (void*)nAllocationIoReceive },
2186{"rsnAllocationData1D",              "(JJIIILjava/lang/Object;II)V",          (void*)nAllocationData1D },
2187{"rsnAllocationElementData1D",       "(JJIII[BI)V",                           (void*)nAllocationElementData1D },
2188{"rsnAllocationData2D",              "(JJIIIIIILjava/lang/Object;II)V",       (void*)nAllocationData2D },
2189{"rsnAllocationData2D",              "(JJIIIIIIJIIII)V",                      (void*)nAllocationData2D_alloc },
2190{"rsnAllocationData3D",              "(JJIIIIIIILjava/lang/Object;II)V",      (void*)nAllocationData3D },
2191{"rsnAllocationData3D",              "(JJIIIIIIIJIIII)V",                     (void*)nAllocationData3D_alloc },
2192{"rsnAllocationRead",                "(JJLjava/lang/Object;I)V",              (void*)nAllocationRead },
2193{"rsnAllocationRead1D",              "(JJIIILjava/lang/Object;II)V",          (void*)nAllocationRead1D },
2194{"rsnAllocationRead2D",              "(JJIIIIIILjava/lang/Object;II)V",       (void*)nAllocationRead2D },
2195{"rsnAllocationGetType",             "(JJ)J",                                 (void*)nAllocationGetType},
2196{"rsnAllocationResize1D",            "(JJI)V",                                (void*)nAllocationResize1D },
2197{"rsnAllocationGenerateMipmaps",     "(JJ)V",                                 (void*)nAllocationGenerateMipmaps },
2198
2199{"rsnAllocationAdapterCreate",       "(JJJ)J",                                (void*)nAllocationAdapterCreate },
2200{"rsnAllocationAdapterOffset",       "(JJIIIIIIIII)V",                        (void*)nAllocationAdapterOffset },
2201
2202{"rsnScriptBindAllocation",          "(JJJI)V",                               (void*)nScriptBindAllocation },
2203{"rsnScriptSetTimeZone",             "(JJ[B)V",                               (void*)nScriptSetTimeZone },
2204{"rsnScriptInvoke",                  "(JJI)V",                                (void*)nScriptInvoke },
2205{"rsnScriptInvokeV",                 "(JJI[B)V",                              (void*)nScriptInvokeV },
2206
2207{"rsnScriptForEach",                 "(JJI[JJ[B[I)V",                         (void*)nScriptForEach },
2208
2209{"rsnScriptSetVarI",                 "(JJII)V",                               (void*)nScriptSetVarI },
2210{"rsnScriptGetVarI",                 "(JJI)I",                                (void*)nScriptGetVarI },
2211{"rsnScriptSetVarJ",                 "(JJIJ)V",                               (void*)nScriptSetVarJ },
2212{"rsnScriptGetVarJ",                 "(JJI)J",                                (void*)nScriptGetVarJ },
2213{"rsnScriptSetVarF",                 "(JJIF)V",                               (void*)nScriptSetVarF },
2214{"rsnScriptGetVarF",                 "(JJI)F",                                (void*)nScriptGetVarF },
2215{"rsnScriptSetVarD",                 "(JJID)V",                               (void*)nScriptSetVarD },
2216{"rsnScriptGetVarD",                 "(JJI)D",                                (void*)nScriptGetVarD },
2217{"rsnScriptSetVarV",                 "(JJI[B)V",                              (void*)nScriptSetVarV },
2218{"rsnScriptGetVarV",                 "(JJI[B)V",                              (void*)nScriptGetVarV },
2219{"rsnScriptSetVarVE",                "(JJI[BJ[I)V",                           (void*)nScriptSetVarVE },
2220{"rsnScriptSetVarObj",               "(JJIJ)V",                               (void*)nScriptSetVarObj },
2221
2222{"rsnScriptCCreate",                 "(JLjava/lang/String;Ljava/lang/String;[BI)J",  (void*)nScriptCCreate },
2223{"rsnScriptIntrinsicCreate",         "(JIJ)J",                                (void*)nScriptIntrinsicCreate },
2224{"rsnScriptKernelIDCreate",          "(JJII)J",                               (void*)nScriptKernelIDCreate },
2225{"rsnScriptInvokeIDCreate",          "(JJI)J",                                (void*)nScriptInvokeIDCreate },
2226{"rsnScriptFieldIDCreate",           "(JJI)J",                                (void*)nScriptFieldIDCreate },
2227{"rsnScriptGroupCreate",             "(J[J[J[J[J[J)J",                        (void*)nScriptGroupCreate },
2228{"rsnScriptGroup2Create",            "(JLjava/lang/String;[J)J",               (void*)nScriptGroup2Create },
2229{"rsnScriptGroupSetInput",           "(JJJJ)V",                               (void*)nScriptGroupSetInput },
2230{"rsnScriptGroupSetOutput",          "(JJJJ)V",                               (void*)nScriptGroupSetOutput },
2231{"rsnScriptGroupExecute",            "(JJ)V",                                 (void*)nScriptGroupExecute },
2232{"rsnScriptGroup2Execute",           "(JJ)V",                                 (void*)nScriptGroup2Execute },
2233
2234{"rsnScriptIntrinsicBLAS_Single",    "(JJIIIIIIIIIFJJFJIIII)V",               (void*)nScriptIntrinsicBLAS_Single },
2235{"rsnScriptIntrinsicBLAS_Double",    "(JJIIIIIIIIIDJJDJIIII)V",               (void*)nScriptIntrinsicBLAS_Double },
2236{"rsnScriptIntrinsicBLAS_Complex",   "(JJIIIIIIIIIFFJJFFJIIII)V",             (void*)nScriptIntrinsicBLAS_Complex },
2237{"rsnScriptIntrinsicBLAS_Z",         "(JJIIIIIIIIIDDJJDDJIIII)V",             (void*)nScriptIntrinsicBLAS_Z },
2238
2239{"rsnProgramStoreCreate",            "(JZZZZZZIII)J",                         (void*)nProgramStoreCreate },
2240
2241{"rsnProgramBindConstants",          "(JJIJ)V",                               (void*)nProgramBindConstants },
2242{"rsnProgramBindTexture",            "(JJIJ)V",                               (void*)nProgramBindTexture },
2243{"rsnProgramBindSampler",            "(JJIJ)V",                               (void*)nProgramBindSampler },
2244
2245{"rsnProgramFragmentCreate",         "(JLjava/lang/String;[Ljava/lang/String;[J)J",              (void*)nProgramFragmentCreate },
2246{"rsnProgramRasterCreate",           "(JZI)J",                                (void*)nProgramRasterCreate },
2247{"rsnProgramVertexCreate",           "(JLjava/lang/String;[Ljava/lang/String;[J)J",              (void*)nProgramVertexCreate },
2248
2249{"rsnContextBindRootScript",         "(JJ)V",                                 (void*)nContextBindRootScript },
2250{"rsnContextBindProgramStore",       "(JJ)V",                                 (void*)nContextBindProgramStore },
2251{"rsnContextBindProgramFragment",    "(JJ)V",                                 (void*)nContextBindProgramFragment },
2252{"rsnContextBindProgramVertex",      "(JJ)V",                                 (void*)nContextBindProgramVertex },
2253{"rsnContextBindProgramRaster",      "(JJ)V",                                 (void*)nContextBindProgramRaster },
2254
2255{"rsnSamplerCreate",                 "(JIIIIIF)J",                            (void*)nSamplerCreate },
2256
2257{"rsnPathCreate",                    "(JIZJJF)J",                             (void*)nPathCreate },
2258{"rsnMeshCreate",                    "(J[J[J[I)J",                            (void*)nMeshCreate },
2259
2260{"rsnMeshGetVertexBufferCount",      "(JJ)I",                                 (void*)nMeshGetVertexBufferCount },
2261{"rsnMeshGetIndexCount",             "(JJ)I",                                 (void*)nMeshGetIndexCount },
2262{"rsnMeshGetVertices",               "(JJ[JI)V",                              (void*)nMeshGetVertices },
2263{"rsnMeshGetIndices",                "(JJ[J[II)V",                            (void*)nMeshGetIndices },
2264
2265{"rsnSystemGetPointerSize",          "()I",                                   (void*)nSystemGetPointerSize },
2266};
2267
2268static int registerFuncs(JNIEnv *_env)
2269{
2270    return android::AndroidRuntime::registerNativeMethods(
2271            _env, classPathName, methods, NELEM(methods));
2272}
2273
2274// ---------------------------------------------------------------------------
2275
2276jint JNI_OnLoad(JavaVM* vm, void* reserved)
2277{
2278    JNIEnv* env = nullptr;
2279    jint result = -1;
2280
2281    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
2282        ALOGE("ERROR: GetEnv failed\n");
2283        goto bail;
2284    }
2285    assert(env != nullptr);
2286
2287    if (registerFuncs(env) < 0) {
2288        ALOGE("ERROR: Renderscript native registration failed\n");
2289        goto bail;
2290    }
2291
2292    /* success -- return valid version number */
2293    result = JNI_VERSION_1_4;
2294
2295bail:
2296    return result;
2297}
2298