android_renderscript_RenderScript.cpp revision 7ff53fa0d42795da55b65468e07df82029deda79
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
313nAssignName(JNIEnv *_env, jobject _this, jlong con, jlong obj, jbyteArray str)
314{
315    if (kLogApi) {
316        ALOGD("nAssignName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
317    }
318    jint len = _env->GetArrayLength(str);
319    jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
320    rsAssignName((RsContext)con, (void *)obj, (const char *)cptr, len);
321    _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
322}
323
324static jstring
325nGetName(JNIEnv *_env, jobject _this, jlong con, jlong obj)
326{
327    if (kLogApi) {
328        ALOGD("nGetName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
329    }
330    const char *name = nullptr;
331    rsaGetName((RsContext)con, (void *)obj, &name);
332    if(name == nullptr || strlen(name) == 0) {
333        return nullptr;
334    }
335    return _env->NewStringUTF(name);
336}
337
338static void
339nObjDestroy(JNIEnv *_env, jobject _this, jlong con, jlong obj)
340{
341    if (kLogApi) {
342        ALOGD("nObjDestroy, con(%p) obj(%p)", (RsContext)con, (void *)obj);
343    }
344    rsObjDestroy((RsContext)con, (void *)obj);
345}
346
347// ---------------------------------------------------------------------------
348
349static jlong
350nDeviceCreate(JNIEnv *_env, jobject _this)
351{
352    if (kLogApi) {
353        ALOGD("nDeviceCreate");
354    }
355    return (jlong)(uintptr_t)rsDeviceCreate();
356}
357
358static void
359nDeviceDestroy(JNIEnv *_env, jobject _this, jlong dev)
360{
361    if (kLogApi) {
362        ALOGD("nDeviceDestroy");
363    }
364    return rsDeviceDestroy((RsDevice)dev);
365}
366
367static void
368nDeviceSetConfig(JNIEnv *_env, jobject _this, jlong dev, jint p, jint value)
369{
370    if (kLogApi) {
371        ALOGD("nDeviceSetConfig  dev(%p), param(%i), value(%i)", (void *)dev, p, value);
372    }
373    return rsDeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value);
374}
375
376static jlong
377nContextCreate(JNIEnv *_env, jobject _this, jlong dev, jint flags, jint sdkVer, jint contextType)
378{
379    if (kLogApi) {
380        ALOGD("nContextCreate");
381    }
382    return (jlong)(uintptr_t)rsContextCreate((RsDevice)dev, 0, sdkVer, (RsContextType)contextType, flags);
383}
384
385static jlong
386nContextCreateGL(JNIEnv *_env, jobject _this, jlong dev, jint ver, jint sdkVer,
387                 jint colorMin, jint colorPref,
388                 jint alphaMin, jint alphaPref,
389                 jint depthMin, jint depthPref,
390                 jint stencilMin, jint stencilPref,
391                 jint samplesMin, jint samplesPref, jfloat samplesQ,
392                 jint dpi)
393{
394    RsSurfaceConfig sc;
395    sc.alphaMin = alphaMin;
396    sc.alphaPref = alphaPref;
397    sc.colorMin = colorMin;
398    sc.colorPref = colorPref;
399    sc.depthMin = depthMin;
400    sc.depthPref = depthPref;
401    sc.samplesMin = samplesMin;
402    sc.samplesPref = samplesPref;
403    sc.samplesQ = samplesQ;
404
405    if (kLogApi) {
406        ALOGD("nContextCreateGL");
407    }
408    return (jlong)(uintptr_t)rsContextCreateGL((RsDevice)dev, ver, sdkVer, sc, dpi);
409}
410
411static void
412nContextSetPriority(JNIEnv *_env, jobject _this, jlong con, jint p)
413{
414    if (kLogApi) {
415        ALOGD("ContextSetPriority, con(%p), priority(%i)", (RsContext)con, p);
416    }
417    rsContextSetPriority((RsContext)con, p);
418}
419
420
421
422static void
423nContextSetSurface(JNIEnv *_env, jobject _this, jlong con, jint width, jint height, jobject wnd)
424{
425    if (kLogApi) {
426        ALOGD("nContextSetSurface, con(%p), width(%i), height(%i), surface(%p)", (RsContext)con,
427              width, height, (Surface *)wnd);
428    }
429
430    ANativeWindow * window = nullptr;
431    if (wnd == nullptr) {
432
433    } else {
434        window = android_view_Surface_getNativeWindow(_env, wnd).get();
435    }
436
437    rsContextSetSurface((RsContext)con, width, height, window);
438}
439
440static void
441nContextDestroy(JNIEnv *_env, jobject _this, jlong con)
442{
443    if (kLogApi) {
444        ALOGD("nContextDestroy, con(%p)", (RsContext)con);
445    }
446    rsContextDestroy((RsContext)con);
447}
448
449static void
450nContextDump(JNIEnv *_env, jobject _this, jlong con, jint bits)
451{
452    if (kLogApi) {
453        ALOGD("nContextDump, con(%p)  bits(%i)", (RsContext)con, bits);
454    }
455    rsContextDump((RsContext)con, bits);
456}
457
458static void
459nContextPause(JNIEnv *_env, jobject _this, jlong con)
460{
461    if (kLogApi) {
462        ALOGD("nContextPause, con(%p)", (RsContext)con);
463    }
464    rsContextPause((RsContext)con);
465}
466
467static void
468nContextResume(JNIEnv *_env, jobject _this, jlong con)
469{
470    if (kLogApi) {
471        ALOGD("nContextResume, con(%p)", (RsContext)con);
472    }
473    rsContextResume((RsContext)con);
474}
475
476
477static jstring
478nContextGetErrorMessage(JNIEnv *_env, jobject _this, jlong con)
479{
480    if (kLogApi) {
481        ALOGD("nContextGetErrorMessage, con(%p)", (RsContext)con);
482    }
483    char buf[1024];
484
485    size_t receiveLen;
486    uint32_t subID;
487    int id = rsContextGetMessage((RsContext)con,
488                                 buf, sizeof(buf),
489                                 &receiveLen, sizeof(receiveLen),
490                                 &subID, sizeof(subID));
491    if (!id && receiveLen) {
492        ALOGV("message receive buffer too small.  %zu", receiveLen);
493    }
494    return _env->NewStringUTF(buf);
495}
496
497static jint
498nContextGetUserMessage(JNIEnv *_env, jobject _this, jlong con, jintArray data)
499{
500    jint len = _env->GetArrayLength(data);
501    if (kLogApi) {
502        ALOGD("nContextGetMessage, con(%p), len(%i)", (RsContext)con, len);
503    }
504    jint *ptr = _env->GetIntArrayElements(data, nullptr);
505    size_t receiveLen;
506    uint32_t subID;
507    int id = rsContextGetMessage((RsContext)con,
508                                 ptr, len * 4,
509                                 &receiveLen, sizeof(receiveLen),
510                                 &subID, sizeof(subID));
511    if (!id && receiveLen) {
512        ALOGV("message receive buffer too small.  %zu", receiveLen);
513    }
514    _env->ReleaseIntArrayElements(data, ptr, 0);
515    return (jint)id;
516}
517
518static jint
519nContextPeekMessage(JNIEnv *_env, jobject _this, jlong con, jintArray auxData)
520{
521    if (kLogApi) {
522        ALOGD("nContextPeekMessage, con(%p)", (RsContext)con);
523    }
524    jint *auxDataPtr = _env->GetIntArrayElements(auxData, nullptr);
525    size_t receiveLen;
526    uint32_t subID;
527    int id = rsContextPeekMessage((RsContext)con, &receiveLen, sizeof(receiveLen),
528                                  &subID, sizeof(subID));
529    auxDataPtr[0] = (jint)subID;
530    auxDataPtr[1] = (jint)receiveLen;
531    _env->ReleaseIntArrayElements(auxData, auxDataPtr, 0);
532    return (jint)id;
533}
534
535static void nContextInitToClient(JNIEnv *_env, jobject _this, jlong con)
536{
537    if (kLogApi) {
538        ALOGD("nContextInitToClient, con(%p)", (RsContext)con);
539    }
540    rsContextInitToClient((RsContext)con);
541}
542
543static void nContextDeinitToClient(JNIEnv *_env, jobject _this, jlong con)
544{
545    if (kLogApi) {
546        ALOGD("nContextDeinitToClient, con(%p)", (RsContext)con);
547    }
548    rsContextDeinitToClient((RsContext)con);
549}
550
551static void
552nContextSendMessage(JNIEnv *_env, jobject _this, jlong con, jint id, jintArray data)
553{
554    jint *ptr = nullptr;
555    jint len = 0;
556    if (data) {
557        len = _env->GetArrayLength(data);
558        ptr = _env->GetIntArrayElements(data, nullptr);
559    }
560    if (kLogApi) {
561        ALOGD("nContextSendMessage, con(%p), id(%i), len(%i)", (RsContext)con, id, len);
562    }
563    rsContextSendMessage((RsContext)con, id, (const uint8_t *)ptr, len * sizeof(int));
564    if (data) {
565        _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
566    }
567}
568
569
570
571static jlong
572nElementCreate(JNIEnv *_env, jobject _this, jlong con, jlong type, jint kind, jboolean norm,
573               jint size)
574{
575    if (kLogApi) {
576        ALOGD("nElementCreate, con(%p), type(%" PRId64 "), kind(%i), norm(%i), size(%i)", (RsContext)con,
577              type, kind, norm, size);
578    }
579    return (jlong)(uintptr_t)rsElementCreate((RsContext)con, (RsDataType)type, (RsDataKind)kind,
580                                             norm, size);
581}
582
583static jlong
584nElementCreate2(JNIEnv *_env, jobject _this, jlong con,
585                jlongArray _ids, jobjectArray _names, jintArray _arraySizes)
586{
587    int fieldCount = _env->GetArrayLength(_ids);
588    if (kLogApi) {
589        ALOGD("nElementCreate2, con(%p)", (RsContext)con);
590    }
591
592    jlong *jIds = _env->GetLongArrayElements(_ids, nullptr);
593    jint *jArraySizes = _env->GetIntArrayElements(_arraySizes, nullptr);
594
595    RsElement *ids = (RsElement*)malloc(fieldCount * sizeof(RsElement));
596    uint32_t *arraySizes = (uint32_t *)malloc(fieldCount * sizeof(uint32_t));
597
598    for(int i = 0; i < fieldCount; i ++) {
599        ids[i] = (RsElement)jIds[i];
600        arraySizes[i] = (uint32_t)jArraySizes[i];
601    }
602
603    AutoJavaStringArrayToUTF8 names(_env, _names, fieldCount);
604
605    const char **nameArray = names.c_str();
606    size_t *sizeArray = names.c_str_len();
607
608    jlong id = (jlong)(uintptr_t)rsElementCreate2((RsContext)con,
609                                     (const RsElement *)ids, fieldCount,
610                                     nameArray, fieldCount * sizeof(size_t),  sizeArray,
611                                     (const uint32_t *)arraySizes, fieldCount);
612
613    free(ids);
614    free(arraySizes);
615    _env->ReleaseLongArrayElements(_ids, jIds, JNI_ABORT);
616    _env->ReleaseIntArrayElements(_arraySizes, jArraySizes, JNI_ABORT);
617
618    return (jlong)(uintptr_t)id;
619}
620
621static void
622nElementGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jintArray _elementData)
623{
624    int dataSize = _env->GetArrayLength(_elementData);
625    if (kLogApi) {
626        ALOGD("nElementGetNativeData, con(%p)", (RsContext)con);
627    }
628
629    // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
630    assert(dataSize == 5);
631
632    uintptr_t elementData[5];
633    rsaElementGetNativeData((RsContext)con, (RsElement)id, elementData, dataSize);
634
635    for(jint i = 0; i < dataSize; i ++) {
636        const jint data = (jint)elementData[i];
637        _env->SetIntArrayRegion(_elementData, i, 1, &data);
638    }
639}
640
641
642static void
643nElementGetSubElements(JNIEnv *_env, jobject _this, jlong con, jlong id,
644                       jlongArray _IDs,
645                       jobjectArray _names,
646                       jintArray _arraySizes)
647{
648    uint32_t dataSize = _env->GetArrayLength(_IDs);
649    if (kLogApi) {
650        ALOGD("nElementGetSubElements, con(%p)", (RsContext)con);
651    }
652
653    uintptr_t *ids = (uintptr_t*)malloc(dataSize * sizeof(uintptr_t));
654    const char **names = (const char **)malloc(dataSize * sizeof(const char *));
655    uint32_t *arraySizes = (uint32_t *)malloc(dataSize * sizeof(uint32_t));
656
657    rsaElementGetSubElements((RsContext)con, (RsElement)id, ids, names, arraySizes,
658                             (uint32_t)dataSize);
659
660    for(uint32_t i = 0; i < dataSize; i++) {
661        const jlong id = (jlong)(uintptr_t)ids[i];
662        const jint arraySize = (jint)arraySizes[i];
663        _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
664        _env->SetLongArrayRegion(_IDs, i, 1, &id);
665        _env->SetIntArrayRegion(_arraySizes, i, 1, &arraySize);
666    }
667
668    free(ids);
669    free(names);
670    free(arraySizes);
671}
672
673// -----------------------------------
674
675static jlong
676nTypeCreate(JNIEnv *_env, jobject _this, jlong con, jlong eid,
677            jint dimx, jint dimy, jint dimz, jboolean mips, jboolean faces, jint yuv)
678{
679    if (kLogApi) {
680        ALOGD("nTypeCreate, con(%p) eid(%p), x(%i), y(%i), z(%i), mips(%i), faces(%i), yuv(%i)",
681              (RsContext)con, (void*)eid, dimx, dimy, dimz, mips, faces, yuv);
682    }
683
684    return (jlong)(uintptr_t)rsTypeCreate((RsContext)con, (RsElement)eid, dimx, dimy, dimz, mips,
685                                          faces, yuv);
686}
687
688static void
689nTypeGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jlongArray _typeData)
690{
691    // We are packing 6 items: mDimX; mDimY; mDimZ;
692    // mDimLOD; mDimFaces; mElement; into typeData
693    int elementCount = _env->GetArrayLength(_typeData);
694
695    assert(elementCount == 6);
696    if (kLogApi) {
697        ALOGD("nTypeGetNativeData, con(%p)", (RsContext)con);
698    }
699
700    uintptr_t typeData[6];
701    rsaTypeGetNativeData((RsContext)con, (RsType)id, typeData, 6);
702
703    for(jint i = 0; i < elementCount; i ++) {
704        const jlong data = (jlong)(uintptr_t)typeData[i];
705        _env->SetLongArrayRegion(_typeData, i, 1, &data);
706    }
707}
708
709// -----------------------------------
710
711static jlong
712nAllocationCreateTyped(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mips, jint usage,
713                       jlong pointer)
714{
715    if (kLogApi) {
716        ALOGD("nAllocationCreateTyped, con(%p), type(%p), mip(%i), usage(%i), ptr(%p)",
717              (RsContext)con, (RsElement)type, mips, usage, (void *)pointer);
718    }
719    return (jlong)(uintptr_t) rsAllocationCreateTyped((RsContext)con, (RsType)type,
720                                                      (RsAllocationMipmapControl)mips,
721                                                      (uint32_t)usage, (uintptr_t)pointer);
722}
723
724static void
725nAllocationSyncAll(JNIEnv *_env, jobject _this, jlong con, jlong a, jint bits)
726{
727    if (kLogApi) {
728        ALOGD("nAllocationSyncAll, con(%p), a(%p), bits(0x%08x)", (RsContext)con, (RsAllocation)a,
729              bits);
730    }
731    rsAllocationSyncAll((RsContext)con, (RsAllocation)a, (RsAllocationUsageType)bits);
732}
733
734static jobject
735nAllocationGetSurface(JNIEnv *_env, jobject _this, jlong con, jlong a)
736{
737    if (kLogApi) {
738        ALOGD("nAllocationGetSurface, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
739    }
740
741    IGraphicBufferProducer *v = (IGraphicBufferProducer *)rsAllocationGetSurface((RsContext)con,
742                                                                                 (RsAllocation)a);
743    sp<IGraphicBufferProducer> bp = v;
744    v->decStrong(nullptr);
745
746    jobject o = android_view_Surface_createFromIGraphicBufferProducer(_env, bp);
747    return o;
748}
749
750static void
751nAllocationSetSurface(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject sur)
752{
753    if (kLogApi) {
754        ALOGD("nAllocationSetSurface, con(%p), alloc(%p), surface(%p)", (RsContext)con,
755              (RsAllocation)alloc, (Surface *)sur);
756    }
757
758    sp<Surface> s;
759    if (sur != 0) {
760        s = android_view_Surface_getSurface(_env, sur);
761    }
762
763    rsAllocationSetSurface((RsContext)con, (RsAllocation)alloc,
764                           static_cast<ANativeWindow *>(s.get()));
765}
766
767static void
768nAllocationIoSend(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
769{
770    if (kLogApi) {
771        ALOGD("nAllocationIoSend, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
772    }
773    rsAllocationIoSend((RsContext)con, (RsAllocation)alloc);
774}
775
776static void
777nAllocationIoReceive(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
778{
779    if (kLogApi) {
780        ALOGD("nAllocationIoReceive, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
781    }
782    rsAllocationIoReceive((RsContext)con, (RsAllocation)alloc);
783}
784
785
786static void
787nAllocationGenerateMipmaps(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
788{
789    if (kLogApi) {
790        ALOGD("nAllocationGenerateMipmaps, con(%p), a(%p)", (RsContext)con, (RsAllocation)alloc);
791    }
792    rsAllocationGenerateMipmaps((RsContext)con, (RsAllocation)alloc);
793}
794
795static jlong
796nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
797                            jobject jbitmap, jint usage)
798{
799    SkBitmap const * nativeBitmap =
800            (SkBitmap const *)_env->GetLongField(jbitmap, gNativeBitmapID);
801    const SkBitmap& bitmap(*nativeBitmap);
802
803    bitmap.lockPixels();
804    const void* ptr = bitmap.getPixels();
805    jlong id = (jlong)(uintptr_t)rsAllocationCreateFromBitmap((RsContext)con,
806                                                  (RsType)type, (RsAllocationMipmapControl)mip,
807                                                  ptr, bitmap.getSize(), usage);
808    bitmap.unlockPixels();
809    return id;
810}
811
812static jlong
813nAllocationCreateBitmapBackedAllocation(JNIEnv *_env, jobject _this, jlong con, jlong type,
814                                        jint mip, jobject jbitmap, jint usage)
815{
816    SkBitmap const * nativeBitmap =
817            (SkBitmap const *)_env->GetLongField(jbitmap, gNativeBitmapID);
818    const SkBitmap& bitmap(*nativeBitmap);
819
820    bitmap.lockPixels();
821    const void* ptr = bitmap.getPixels();
822    jlong id = (jlong)(uintptr_t)rsAllocationCreateTyped((RsContext)con,
823                                            (RsType)type, (RsAllocationMipmapControl)mip,
824                                            (uint32_t)usage, (uintptr_t)ptr);
825    bitmap.unlockPixels();
826    return id;
827}
828
829static jlong
830nAllocationCubeCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
831                                jobject jbitmap, jint usage)
832{
833    SkBitmap const * nativeBitmap =
834            (SkBitmap const *)_env->GetLongField(jbitmap, gNativeBitmapID);
835    const SkBitmap& bitmap(*nativeBitmap);
836
837    bitmap.lockPixels();
838    const void* ptr = bitmap.getPixels();
839    jlong id = (jlong)(uintptr_t)rsAllocationCubeCreateFromBitmap((RsContext)con,
840                                                      (RsType)type, (RsAllocationMipmapControl)mip,
841                                                      ptr, bitmap.getSize(), usage);
842    bitmap.unlockPixels();
843    return id;
844}
845
846static void
847nAllocationCopyFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
848{
849    SkBitmap const * nativeBitmap =
850            (SkBitmap const *)_env->GetLongField(jbitmap, gNativeBitmapID);
851    const SkBitmap& bitmap(*nativeBitmap);
852    int w = bitmap.width();
853    int h = bitmap.height();
854
855    bitmap.lockPixels();
856    const void* ptr = bitmap.getPixels();
857    rsAllocation2DData((RsContext)con, (RsAllocation)alloc, 0, 0,
858                       0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
859                       w, h, ptr, bitmap.getSize(), 0);
860    bitmap.unlockPixels();
861}
862
863static void
864nAllocationCopyToBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
865{
866    SkBitmap const * nativeBitmap =
867            (SkBitmap const *)_env->GetLongField(jbitmap, gNativeBitmapID);
868    const SkBitmap& bitmap(*nativeBitmap);
869
870    bitmap.lockPixels();
871    void* ptr = bitmap.getPixels();
872    rsAllocationCopyToBitmap((RsContext)con, (RsAllocation)alloc, ptr, bitmap.getSize());
873    bitmap.unlockPixels();
874    bitmap.notifyPixelsChanged();
875}
876
877// Copies from the Java object data into the Allocation pointed to by _alloc.
878static void
879nAllocationData1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
880                  jint count, jobject data, jint sizeBytes, jint dataType)
881{
882    RsAllocation *alloc = (RsAllocation *)_alloc;
883    if (kLogApi) {
884        ALOGD("nAllocation1DData, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
885              "dataType(%i)", (RsContext)con, (RsAllocation)alloc, offset, count, sizeBytes,
886              dataType);
887    }
888    PER_ARRAY_TYPE(nullptr, rsAllocation1DData, true, (RsContext)con, alloc, offset, lod, count,
889                   ptr, sizeBytes);
890}
891
892// Copies from the Java array data into the Allocation pointed to by alloc.
893static void
894//    native void rsnAllocationElementData1D(long con, long id, int xoff, int compIdx, byte[] d, int sizeBytes);
895nAllocationElementData1D(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint offset, jint lod,
896                         jint compIdx, jbyteArray data, jint sizeBytes)
897{
898    jint len = _env->GetArrayLength(data);
899    if (kLogApi) {
900        ALOGD("nAllocationElementData1D, con(%p), alloc(%p), offset(%i), comp(%i), len(%i), "
901              "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, offset, compIdx, len,
902              sizeBytes);
903    }
904    jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
905    rsAllocation1DElementData((RsContext)con, (RsAllocation)alloc, offset, lod, ptr, sizeBytes, compIdx);
906    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
907}
908
909// Copies from the Java object data into the Allocation pointed to by _alloc.
910static void
911nAllocationData2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
912                  jint w, jint h, jobject data, jint sizeBytes, jint dataType)
913{
914    RsAllocation *alloc = (RsAllocation *)_alloc;
915    RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
916    if (kLogApi) {
917        ALOGD("nAllocation2DData, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
918              "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
919    }
920    PER_ARRAY_TYPE(nullptr, rsAllocation2DData, true, (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
921}
922
923// Copies from the Allocation pointed to by srcAlloc into the Allocation
924// pointed to by dstAlloc.
925static void
926nAllocationData2D_alloc(JNIEnv *_env, jobject _this, jlong con,
927                        jlong dstAlloc, jint dstXoff, jint dstYoff,
928                        jint dstMip, jint dstFace,
929                        jint width, jint height,
930                        jlong srcAlloc, jint srcXoff, jint srcYoff,
931                        jint srcMip, jint srcFace)
932{
933    if (kLogApi) {
934        ALOGD("nAllocation2DData_s, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
935              " dstMip(%i), dstFace(%i), width(%i), height(%i),"
936              " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i), srcFace(%i)",
937              (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip, dstFace,
938              width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
939    }
940
941    rsAllocationCopy2DRange((RsContext)con,
942                            (RsAllocation)dstAlloc,
943                            dstXoff, dstYoff,
944                            dstMip, dstFace,
945                            width, height,
946                            (RsAllocation)srcAlloc,
947                            srcXoff, srcYoff,
948                            srcMip, srcFace);
949}
950
951// Copies from the Java object data into the Allocation pointed to by _alloc.
952static void
953nAllocationData3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
954                    jint w, jint h, jint d, jobject data, int sizeBytes, int dataType)
955{
956    RsAllocation *alloc = (RsAllocation *)_alloc;
957    if (kLogApi) {
958        ALOGD("nAllocation3DData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
959              " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
960              lod, w, h, d, sizeBytes);
961    }
962    PER_ARRAY_TYPE(nullptr, rsAllocation3DData, true, (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
963}
964
965// Copies from the Allocation pointed to by srcAlloc into the Allocation
966// pointed to by dstAlloc.
967static void
968nAllocationData3D_alloc(JNIEnv *_env, jobject _this, jlong con,
969                        jlong dstAlloc, jint dstXoff, jint dstYoff, jint dstZoff,
970                        jint dstMip,
971                        jint width, jint height, jint depth,
972                        jlong srcAlloc, jint srcXoff, jint srcYoff, jint srcZoff,
973                        jint srcMip)
974{
975    if (kLogApi) {
976        ALOGD("nAllocationData3D_alloc, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
977              " dstMip(%i), width(%i), height(%i),"
978              " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i)",
979              (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip,
980              width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip);
981    }
982
983    rsAllocationCopy3DRange((RsContext)con,
984                            (RsAllocation)dstAlloc,
985                            dstXoff, dstYoff, dstZoff, dstMip,
986                            width, height, depth,
987                            (RsAllocation)srcAlloc,
988                            srcXoff, srcYoff, srcZoff, srcMip);
989}
990
991
992// Copies from the Allocation pointed to by _alloc into the Java object data.
993static void
994nAllocationRead(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jobject data, int dataType)
995{
996    RsAllocation *alloc = (RsAllocation *)_alloc;
997    if (kLogApi) {
998        ALOGD("nAllocationRead, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
999    }
1000    PER_ARRAY_TYPE(0, rsAllocationRead, false, (RsContext)con, alloc, ptr, len * typeBytes);
1001}
1002
1003// Copies from the Allocation pointed to by _alloc into the Java object data.
1004static void
1005nAllocationRead1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
1006                  jint count, jobject data, int sizeBytes, int dataType)
1007{
1008    RsAllocation *alloc = (RsAllocation *)_alloc;
1009    if (kLogApi) {
1010        ALOGD("nAllocation1DRead, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1011              "dataType(%i)", (RsContext)con, alloc, offset, count, sizeBytes, dataType);
1012    }
1013    PER_ARRAY_TYPE(0, rsAllocation1DRead, false, (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
1014}
1015
1016// Copies from the Allocation pointed to by _alloc into the Java object data.
1017static void
1018nAllocationRead2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
1019                  jint w, jint h, jobject data, int sizeBytes, int dataType)
1020{
1021    RsAllocation *alloc = (RsAllocation *)_alloc;
1022    RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
1023    if (kLogApi) {
1024        ALOGD("nAllocation2DRead, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1025              "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1026    }
1027    PER_ARRAY_TYPE(0, rsAllocation2DRead, false, (RsContext)con, alloc, xoff, yoff, lod, face, w, h,
1028                   ptr, sizeBytes, 0);
1029}
1030
1031static jlong
1032nAllocationGetType(JNIEnv *_env, jobject _this, jlong con, jlong a)
1033{
1034    if (kLogApi) {
1035        ALOGD("nAllocationGetType, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1036    }
1037    return (jlong)(uintptr_t) rsaAllocationGetType((RsContext)con, (RsAllocation)a);
1038}
1039
1040static void
1041nAllocationResize1D(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint dimX)
1042{
1043    if (kLogApi) {
1044        ALOGD("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", (RsContext)con,
1045              (RsAllocation)alloc, dimX);
1046    }
1047    rsAllocationResize1D((RsContext)con, (RsAllocation)alloc, dimX);
1048}
1049
1050// -----------------------------------
1051
1052static jlong
1053nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con, jlong native_asset)
1054{
1055    Asset* asset = reinterpret_cast<Asset*>(native_asset);
1056    ALOGV("______nFileA3D %p", asset);
1057
1058    jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromMemory((RsContext)con, asset->getBuffer(false), asset->getLength());
1059    return id;
1060}
1061
1062static jlong
1063nFileA3DCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path)
1064{
1065    AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
1066    if (mgr == nullptr) {
1067        return 0;
1068    }
1069
1070    AutoJavaStringToUTF8 str(_env, _path);
1071    Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
1072    if (asset == nullptr) {
1073        return 0;
1074    }
1075
1076    jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromAsset((RsContext)con, asset);
1077    return id;
1078}
1079
1080static jlong
1081nFileA3DCreateFromFile(JNIEnv *_env, jobject _this, jlong con, jstring fileName)
1082{
1083    AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
1084    jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromFile((RsContext)con, fileNameUTF.c_str());
1085
1086    return id;
1087}
1088
1089static jint
1090nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D)
1091{
1092    int32_t numEntries = 0;
1093    rsaFileA3DGetNumIndexEntries((RsContext)con, &numEntries, (RsFile)fileA3D);
1094    return (jint)numEntries;
1095}
1096
1097static void
1098nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
1099{
1100    ALOGV("______nFileA3D %p", (RsFile) fileA3D);
1101    RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
1102
1103    rsaFileA3DGetIndexEntries((RsContext)con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
1104
1105    for(jint i = 0; i < numEntries; i ++) {
1106        _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
1107        _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
1108    }
1109
1110    free(fileEntries);
1111}
1112
1113static jlong
1114nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint index)
1115{
1116    ALOGV("______nFileA3D %p", (RsFile) fileA3D);
1117    jlong id = (jlong)(uintptr_t)rsaFileA3DGetEntryByIndex((RsContext)con, (uint32_t)index, (RsFile)fileA3D);
1118    return id;
1119}
1120
1121// -----------------------------------
1122
1123static jlong
1124nFontCreateFromFile(JNIEnv *_env, jobject _this, jlong con,
1125                    jstring fileName, jfloat fontSize, jint dpi)
1126{
1127    AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
1128    jlong id = (jlong)(uintptr_t)rsFontCreateFromFile((RsContext)con,
1129                                         fileNameUTF.c_str(), fileNameUTF.length(),
1130                                         fontSize, dpi);
1131
1132    return id;
1133}
1134
1135static jlong
1136nFontCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con,
1137                           jstring name, jfloat fontSize, jint dpi, jlong native_asset)
1138{
1139    Asset* asset = reinterpret_cast<Asset*>(native_asset);
1140    AutoJavaStringToUTF8 nameUTF(_env, name);
1141
1142    jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
1143                                           nameUTF.c_str(), nameUTF.length(),
1144                                           fontSize, dpi,
1145                                           asset->getBuffer(false), asset->getLength());
1146    return id;
1147}
1148
1149static jlong
1150nFontCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path,
1151                     jfloat fontSize, jint dpi)
1152{
1153    AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
1154    if (mgr == nullptr) {
1155        return 0;
1156    }
1157
1158    AutoJavaStringToUTF8 str(_env, _path);
1159    Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
1160    if (asset == nullptr) {
1161        return 0;
1162    }
1163
1164    jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
1165                                           str.c_str(), str.length(),
1166                                           fontSize, dpi,
1167                                           asset->getBuffer(false), asset->getLength());
1168    delete asset;
1169    return id;
1170}
1171
1172// -----------------------------------
1173
1174static void
1175nScriptBindAllocation(JNIEnv *_env, jobject _this, jlong con, jlong script, jlong alloc, jint slot)
1176{
1177    if (kLogApi) {
1178        ALOGD("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", (RsContext)con,
1179              (RsScript)script, (RsAllocation)alloc, slot);
1180    }
1181    rsScriptBindAllocation((RsContext)con, (RsScript)script, (RsAllocation)alloc, slot);
1182}
1183
1184static void
1185nScriptSetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jint val)
1186{
1187    if (kLogApi) {
1188        ALOGD("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", (RsContext)con, (void *)script,
1189              slot, val);
1190    }
1191    rsScriptSetVarI((RsContext)con, (RsScript)script, slot, val);
1192}
1193
1194static jint
1195nScriptGetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
1196{
1197    if (kLogApi) {
1198        ALOGD("nScriptGetVarI, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1199    }
1200    int value = 0;
1201    rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
1202    return value;
1203}
1204
1205static void
1206nScriptSetVarObj(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
1207{
1208    if (kLogApi) {
1209        ALOGD("nScriptSetVarObj, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
1210              slot, val);
1211    }
1212    rsScriptSetVarObj((RsContext)con, (RsScript)script, slot, (RsObjectBase)val);
1213}
1214
1215static void
1216nScriptSetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
1217{
1218    if (kLogApi) {
1219        ALOGD("nScriptSetVarJ, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
1220              slot, val);
1221    }
1222    rsScriptSetVarJ((RsContext)con, (RsScript)script, slot, val);
1223}
1224
1225static jlong
1226nScriptGetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
1227{
1228    if (kLogApi) {
1229        ALOGD("nScriptGetVarJ, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1230    }
1231    jlong value = 0;
1232    rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
1233    return value;
1234}
1235
1236static void
1237nScriptSetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, float val)
1238{
1239    if (kLogApi) {
1240        ALOGD("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", (RsContext)con, (void *)script,
1241              slot, val);
1242    }
1243    rsScriptSetVarF((RsContext)con, (RsScript)script, slot, val);
1244}
1245
1246static jfloat
1247nScriptGetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
1248{
1249    if (kLogApi) {
1250        ALOGD("nScriptGetVarF, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1251    }
1252    jfloat value = 0;
1253    rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
1254    return value;
1255}
1256
1257static void
1258nScriptSetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, double val)
1259{
1260    if (kLogApi) {
1261        ALOGD("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", (RsContext)con, (void *)script,
1262              slot, val);
1263    }
1264    rsScriptSetVarD((RsContext)con, (RsScript)script, slot, val);
1265}
1266
1267static jdouble
1268nScriptGetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
1269{
1270    if (kLogApi) {
1271        ALOGD("nScriptGetVarD, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1272    }
1273    jdouble value = 0;
1274    rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
1275    return value;
1276}
1277
1278static void
1279nScriptSetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
1280{
1281    if (kLogApi) {
1282        ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1283    }
1284    jint len = _env->GetArrayLength(data);
1285    jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
1286    rsScriptSetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
1287    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1288}
1289
1290static void
1291nScriptGetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
1292{
1293    if (kLogApi) {
1294        ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1295    }
1296    jint len = _env->GetArrayLength(data);
1297    jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
1298    rsScriptGetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
1299    _env->ReleaseByteArrayElements(data, ptr, 0);
1300}
1301
1302static void
1303nScriptSetVarVE(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data,
1304                jlong elem, jintArray dims)
1305{
1306    if (kLogApi) {
1307        ALOGD("nScriptSetVarVE, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1308    }
1309    jint len = _env->GetArrayLength(data);
1310    jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
1311    jint dimsLen = _env->GetArrayLength(dims) * sizeof(int);
1312    jint *dimsPtr = _env->GetIntArrayElements(dims, nullptr);
1313    rsScriptSetVarVE((RsContext)con, (RsScript)script, slot, ptr, len, (RsElement)elem,
1314                     (const uint32_t*) dimsPtr, dimsLen);
1315    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1316    _env->ReleaseIntArrayElements(dims, dimsPtr, JNI_ABORT);
1317}
1318
1319
1320static void
1321nScriptSetTimeZone(JNIEnv *_env, jobject _this, jlong con, jlong script, jbyteArray timeZone)
1322{
1323    if (kLogApi) {
1324        ALOGD("nScriptCSetTimeZone, con(%p), s(%p)", (RsContext)con, (void *)script);
1325    }
1326
1327    jint length = _env->GetArrayLength(timeZone);
1328    jbyte* timeZone_ptr;
1329    timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
1330
1331    rsScriptSetTimeZone((RsContext)con, (RsScript)script, (const char *)timeZone_ptr, length);
1332
1333    if (timeZone_ptr) {
1334        _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
1335    }
1336}
1337
1338static void
1339nScriptInvoke(JNIEnv *_env, jobject _this, jlong con, jlong obj, jint slot)
1340{
1341    if (kLogApi) {
1342        ALOGD("nScriptInvoke, con(%p), script(%p)", (RsContext)con, (void *)obj);
1343    }
1344    rsScriptInvoke((RsContext)con, (RsScript)obj, slot);
1345}
1346
1347static void
1348nScriptInvokeV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
1349{
1350    if (kLogApi) {
1351        ALOGD("nScriptInvokeV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1352    }
1353    jint len = _env->GetArrayLength(data);
1354    jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
1355    rsScriptInvokeV((RsContext)con, (RsScript)script, slot, ptr, len);
1356    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1357}
1358
1359static void
1360nScriptForEach(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
1361               jlongArray ains, jlong aout, jbyteArray params,
1362               jintArray limits)
1363{
1364    if (kLogApi) {
1365        ALOGD("nScriptForEach, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1366    }
1367
1368    jint   in_len = 0;
1369    jlong *in_ptr = nullptr;
1370
1371    RsAllocation *in_allocs = nullptr;
1372
1373    if (ains != nullptr) {
1374        in_len = _env->GetArrayLength(ains);
1375        in_ptr = _env->GetLongArrayElements(ains, nullptr);
1376
1377        if (sizeof(RsAllocation) == sizeof(jlong)) {
1378            in_allocs = (RsAllocation*)in_ptr;
1379
1380        } else {
1381            // Convert from 64-bit jlong types to the native pointer type.
1382
1383            in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
1384
1385            for (int index = in_len; --index >= 0;) {
1386                in_allocs[index] = (RsAllocation)in_ptr[index];
1387            }
1388        }
1389    }
1390
1391    jint   param_len = 0;
1392    jbyte *param_ptr = nullptr;
1393
1394    if (params != nullptr) {
1395        param_len = _env->GetArrayLength(params);
1396        param_ptr = _env->GetByteArrayElements(params, nullptr);
1397    }
1398
1399    RsScriptCall sc, *sca = nullptr;
1400    uint32_t sc_size = 0;
1401
1402    jint  limit_len = 0;
1403    jint *limit_ptr = nullptr;
1404
1405    if (limits != nullptr) {
1406        limit_len = _env->GetArrayLength(limits);
1407        limit_ptr = _env->GetIntArrayElements(limits, nullptr);
1408
1409        assert(limit_len == 6);
1410        UNUSED(limit_len);  // As the assert might not be compiled.
1411
1412        sc.xStart     = limit_ptr[0];
1413        sc.xEnd       = limit_ptr[1];
1414        sc.yStart     = limit_ptr[2];
1415        sc.yEnd       = limit_ptr[3];
1416        sc.zStart     = limit_ptr[4];
1417        sc.zEnd       = limit_ptr[5];
1418        sc.strategy   = RS_FOR_EACH_STRATEGY_DONT_CARE;
1419        sc.arrayStart = 0;
1420        sc.arrayEnd = 0;
1421        sc.array2Start = 0;
1422        sc.array2End = 0;
1423        sc.array3Start = 0;
1424        sc.array3End = 0;
1425        sc.array4Start = 0;
1426        sc.array4End = 0;
1427
1428        sca = &sc;
1429    }
1430
1431    rsScriptForEachMulti((RsContext)con, (RsScript)script, slot,
1432                         in_allocs, in_len, (RsAllocation)aout,
1433                         param_ptr, param_len, sca, sc_size);
1434
1435    if (ains != nullptr) {
1436        _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
1437    }
1438
1439    if (params != nullptr) {
1440        _env->ReleaseByteArrayElements(params, param_ptr, JNI_ABORT);
1441    }
1442
1443    if (limits != nullptr) {
1444        _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
1445    }
1446}
1447
1448// -----------------------------------
1449
1450static jlong
1451nScriptCCreate(JNIEnv *_env, jobject _this, jlong con,
1452               jstring resName, jstring cacheDir,
1453               jbyteArray scriptRef, jint length)
1454{
1455    if (kLogApi) {
1456        ALOGD("nScriptCCreate, con(%p)", (RsContext)con);
1457    }
1458
1459    AutoJavaStringToUTF8 resNameUTF(_env, resName);
1460    AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
1461    jlong ret = 0;
1462    jbyte* script_ptr = nullptr;
1463    jint _exception = 0;
1464    jint remaining;
1465    if (!scriptRef) {
1466        _exception = 1;
1467        //jniThrowException(_env, "java/lang/IllegalArgumentException", "script == null");
1468        goto exit;
1469    }
1470    if (length < 0) {
1471        _exception = 1;
1472        //jniThrowException(_env, "java/lang/IllegalArgumentException", "length < 0");
1473        goto exit;
1474    }
1475    remaining = _env->GetArrayLength(scriptRef);
1476    if (remaining < length) {
1477        _exception = 1;
1478        //jniThrowException(_env, "java/lang/IllegalArgumentException",
1479        //        "length > script.length - offset");
1480        goto exit;
1481    }
1482    script_ptr = (jbyte *)
1483        _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
1484
1485    //rsScriptCSetText((RsContext)con, (const char *)script_ptr, length);
1486
1487    ret = (jlong)(uintptr_t)rsScriptCCreate((RsContext)con,
1488                                resNameUTF.c_str(), resNameUTF.length(),
1489                                cacheDirUTF.c_str(), cacheDirUTF.length(),
1490                                (const char *)script_ptr, length);
1491
1492exit:
1493    if (script_ptr) {
1494        _env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr,
1495                _exception ? JNI_ABORT: 0);
1496    }
1497
1498    return (jlong)(uintptr_t)ret;
1499}
1500
1501static jlong
1502nScriptIntrinsicCreate(JNIEnv *_env, jobject _this, jlong con, jint id, jlong eid)
1503{
1504    if (kLogApi) {
1505        ALOGD("nScriptIntrinsicCreate, con(%p) id(%i) element(%p)", (RsContext)con, id,
1506              (void *)eid);
1507    }
1508    return (jlong)(uintptr_t)rsScriptIntrinsicCreate((RsContext)con, id, (RsElement)eid);
1509}
1510
1511static jlong
1512nScriptKernelIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot, jint sig)
1513{
1514    if (kLogApi) {
1515        ALOGD("nScriptKernelIDCreate, con(%p) script(%p), slot(%i), sig(%i)", (RsContext)con,
1516              (void *)sid, slot, sig);
1517    }
1518    return (jlong)(uintptr_t)rsScriptKernelIDCreate((RsContext)con, (RsScript)sid, slot, sig);
1519}
1520
1521static jlong
1522nScriptInvokeIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
1523{
1524    if (kLogApi) {
1525        ALOGD("nScriptInvokeIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con,
1526              (void *)sid, slot);
1527    }
1528    return (jlong)(uintptr_t)rsScriptInvokeIDCreate((RsContext)con, (RsScript)sid, slot);
1529}
1530
1531static jlong
1532nScriptFieldIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
1533{
1534    if (kLogApi) {
1535        ALOGD("nScriptFieldIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con, (void *)sid,
1536              slot);
1537    }
1538    return (jlong)(uintptr_t)rsScriptFieldIDCreate((RsContext)con, (RsScript)sid, slot);
1539}
1540
1541static jlong
1542nScriptGroupCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _kernels, jlongArray _src,
1543    jlongArray _dstk, jlongArray _dstf, jlongArray _types)
1544{
1545    if (kLogApi) {
1546        ALOGD("nScriptGroupCreate, con(%p)", (RsContext)con);
1547    }
1548
1549    jint kernelsLen = _env->GetArrayLength(_kernels);
1550    jlong *jKernelsPtr = _env->GetLongArrayElements(_kernels, nullptr);
1551    RsScriptKernelID* kernelsPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * kernelsLen);
1552    for(int i = 0; i < kernelsLen; ++i) {
1553        kernelsPtr[i] = (RsScriptKernelID)jKernelsPtr[i];
1554    }
1555
1556    jint srcLen = _env->GetArrayLength(_src);
1557    jlong *jSrcPtr = _env->GetLongArrayElements(_src, nullptr);
1558    RsScriptKernelID* srcPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * srcLen);
1559    for(int i = 0; i < srcLen; ++i) {
1560        srcPtr[i] = (RsScriptKernelID)jSrcPtr[i];
1561    }
1562
1563    jint dstkLen = _env->GetArrayLength(_dstk);
1564    jlong *jDstkPtr = _env->GetLongArrayElements(_dstk, nullptr);
1565    RsScriptKernelID* dstkPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstkLen);
1566    for(int i = 0; i < dstkLen; ++i) {
1567        dstkPtr[i] = (RsScriptKernelID)jDstkPtr[i];
1568    }
1569
1570    jint dstfLen = _env->GetArrayLength(_dstf);
1571    jlong *jDstfPtr = _env->GetLongArrayElements(_dstf, nullptr);
1572    RsScriptKernelID* dstfPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstfLen);
1573    for(int i = 0; i < dstfLen; ++i) {
1574        dstfPtr[i] = (RsScriptKernelID)jDstfPtr[i];
1575    }
1576
1577    jint typesLen = _env->GetArrayLength(_types);
1578    jlong *jTypesPtr = _env->GetLongArrayElements(_types, nullptr);
1579    RsType* typesPtr = (RsType*) malloc(sizeof(RsType) * typesLen);
1580    for(int i = 0; i < typesLen; ++i) {
1581        typesPtr[i] = (RsType)jTypesPtr[i];
1582    }
1583
1584    jlong id = (jlong)(uintptr_t)rsScriptGroupCreate((RsContext)con,
1585                               (RsScriptKernelID *)kernelsPtr, kernelsLen * sizeof(RsScriptKernelID),
1586                               (RsScriptKernelID *)srcPtr, srcLen * sizeof(RsScriptKernelID),
1587                               (RsScriptKernelID *)dstkPtr, dstkLen * sizeof(RsScriptKernelID),
1588                               (RsScriptFieldID *)dstfPtr, dstfLen * sizeof(RsScriptKernelID),
1589                               (RsType *)typesPtr, typesLen * sizeof(RsType));
1590
1591    free(kernelsPtr);
1592    free(srcPtr);
1593    free(dstkPtr);
1594    free(dstfPtr);
1595    free(typesPtr);
1596    _env->ReleaseLongArrayElements(_kernels, jKernelsPtr, 0);
1597    _env->ReleaseLongArrayElements(_src, jSrcPtr, 0);
1598    _env->ReleaseLongArrayElements(_dstk, jDstkPtr, 0);
1599    _env->ReleaseLongArrayElements(_dstf, jDstfPtr, 0);
1600    _env->ReleaseLongArrayElements(_types, jTypesPtr, 0);
1601    return id;
1602}
1603
1604static void
1605nScriptGroupSetInput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
1606{
1607    if (kLogApi) {
1608        ALOGD("nScriptGroupSetInput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
1609              (void *)gid, (void *)kid, (void *)alloc);
1610    }
1611    rsScriptGroupSetInput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
1612}
1613
1614static void
1615nScriptGroupSetOutput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
1616{
1617    if (kLogApi) {
1618        ALOGD("nScriptGroupSetOutput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
1619              (void *)gid, (void *)kid, (void *)alloc);
1620    }
1621    rsScriptGroupSetOutput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
1622}
1623
1624static void
1625nScriptGroupExecute(JNIEnv *_env, jobject _this, jlong con, jlong gid)
1626{
1627    if (kLogApi) {
1628        ALOGD("nScriptGroupSetOutput, con(%p) group(%p)", (RsContext)con, (void *)gid);
1629    }
1630    rsScriptGroupExecute((RsContext)con, (RsScriptGroup)gid);
1631}
1632
1633// ---------------------------------------------------------------------------
1634
1635static jlong
1636nProgramStoreCreate(JNIEnv *_env, jobject _this, jlong con,
1637                    jboolean colorMaskR, jboolean colorMaskG, jboolean colorMaskB, jboolean colorMaskA,
1638                    jboolean depthMask, jboolean ditherEnable,
1639                    jint srcFunc, jint destFunc,
1640                    jint depthFunc)
1641{
1642    if (kLogApi) {
1643        ALOGD("nProgramStoreCreate, con(%p)", (RsContext)con);
1644    }
1645    return (jlong)(uintptr_t)rsProgramStoreCreate((RsContext)con, colorMaskR, colorMaskG, colorMaskB, colorMaskA,
1646                                      depthMask, ditherEnable, (RsBlendSrcFunc)srcFunc,
1647                                      (RsBlendDstFunc)destFunc, (RsDepthFunc)depthFunc);
1648}
1649
1650// ---------------------------------------------------------------------------
1651
1652static void
1653nProgramBindConstants(JNIEnv *_env, jobject _this, jlong con, jlong vpv, jint slot, jlong a)
1654{
1655    if (kLogApi) {
1656        ALOGD("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", (RsContext)con,
1657              (RsProgramVertex)vpv, slot, (RsAllocation)a);
1658    }
1659    rsProgramBindConstants((RsContext)con, (RsProgram)vpv, slot, (RsAllocation)a);
1660}
1661
1662static void
1663nProgramBindTexture(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
1664{
1665    if (kLogApi) {
1666        ALOGD("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
1667              (RsProgramFragment)vpf, slot, (RsAllocation)a);
1668    }
1669    rsProgramBindTexture((RsContext)con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
1670}
1671
1672static void
1673nProgramBindSampler(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
1674{
1675    if (kLogApi) {
1676        ALOGD("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
1677              (RsProgramFragment)vpf, slot, (RsSampler)a);
1678    }
1679    rsProgramBindSampler((RsContext)con, (RsProgramFragment)vpf, slot, (RsSampler)a);
1680}
1681
1682// ---------------------------------------------------------------------------
1683
1684static jlong
1685nProgramFragmentCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
1686                       jobjectArray texNames, jlongArray params)
1687{
1688    AutoJavaStringToUTF8 shaderUTF(_env, shader);
1689    jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
1690    jint paramLen = _env->GetArrayLength(params);
1691
1692    int texCount = _env->GetArrayLength(texNames);
1693    AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
1694    const char ** nameArray = names.c_str();
1695    size_t* sizeArray = names.c_str_len();
1696
1697    if (kLogApi) {
1698        ALOGD("nProgramFragmentCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
1699    }
1700
1701    uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
1702    for(int i = 0; i < paramLen; ++i) {
1703        paramPtr[i] = (uintptr_t)jParamPtr[i];
1704    }
1705    jlong ret = (jlong)(uintptr_t)rsProgramFragmentCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
1706                                             nameArray, texCount, sizeArray,
1707                                             paramPtr, paramLen);
1708
1709    free(paramPtr);
1710    _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
1711    return ret;
1712}
1713
1714
1715// ---------------------------------------------------------------------------
1716
1717static jlong
1718nProgramVertexCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
1719                     jobjectArray texNames, jlongArray params)
1720{
1721    AutoJavaStringToUTF8 shaderUTF(_env, shader);
1722    jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
1723    jint paramLen = _env->GetArrayLength(params);
1724
1725    if (kLogApi) {
1726        ALOGD("nProgramVertexCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
1727    }
1728
1729    int texCount = _env->GetArrayLength(texNames);
1730    AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
1731    const char ** nameArray = names.c_str();
1732    size_t* sizeArray = names.c_str_len();
1733
1734    uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
1735    for(int i = 0; i < paramLen; ++i) {
1736        paramPtr[i] = (uintptr_t)jParamPtr[i];
1737    }
1738
1739    jlong ret = (jlong)(uintptr_t)rsProgramVertexCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
1740                                           nameArray, texCount, sizeArray,
1741                                           paramPtr, paramLen);
1742
1743    free(paramPtr);
1744    _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
1745    return ret;
1746}
1747
1748// ---------------------------------------------------------------------------
1749
1750static jlong
1751nProgramRasterCreate(JNIEnv *_env, jobject _this, jlong con, jboolean pointSprite, jint cull)
1752{
1753    if (kLogApi) {
1754        ALOGD("nProgramRasterCreate, con(%p), pointSprite(%i), cull(%i)", (RsContext)con,
1755              pointSprite, cull);
1756    }
1757    return (jlong)(uintptr_t)rsProgramRasterCreate((RsContext)con, pointSprite, (RsCullMode)cull);
1758}
1759
1760
1761// ---------------------------------------------------------------------------
1762
1763static void
1764nContextBindRootScript(JNIEnv *_env, jobject _this, jlong con, jlong script)
1765{
1766    if (kLogApi) {
1767        ALOGD("nContextBindRootScript, con(%p), script(%p)", (RsContext)con, (RsScript)script);
1768    }
1769    rsContextBindRootScript((RsContext)con, (RsScript)script);
1770}
1771
1772static void
1773nContextBindProgramStore(JNIEnv *_env, jobject _this, jlong con, jlong pfs)
1774{
1775    if (kLogApi) {
1776        ALOGD("nContextBindProgramStore, con(%p), pfs(%p)", (RsContext)con, (RsProgramStore)pfs);
1777    }
1778    rsContextBindProgramStore((RsContext)con, (RsProgramStore)pfs);
1779}
1780
1781static void
1782nContextBindProgramFragment(JNIEnv *_env, jobject _this, jlong con, jlong pf)
1783{
1784    if (kLogApi) {
1785        ALOGD("nContextBindProgramFragment, con(%p), pf(%p)", (RsContext)con,
1786              (RsProgramFragment)pf);
1787    }
1788    rsContextBindProgramFragment((RsContext)con, (RsProgramFragment)pf);
1789}
1790
1791static void
1792nContextBindProgramVertex(JNIEnv *_env, jobject _this, jlong con, jlong pf)
1793{
1794    if (kLogApi) {
1795        ALOGD("nContextBindProgramVertex, con(%p), pf(%p)", (RsContext)con, (RsProgramVertex)pf);
1796    }
1797    rsContextBindProgramVertex((RsContext)con, (RsProgramVertex)pf);
1798}
1799
1800static void
1801nContextBindProgramRaster(JNIEnv *_env, jobject _this, jlong con, jlong pf)
1802{
1803    if (kLogApi) {
1804        ALOGD("nContextBindProgramRaster, con(%p), pf(%p)", (RsContext)con, (RsProgramRaster)pf);
1805    }
1806    rsContextBindProgramRaster((RsContext)con, (RsProgramRaster)pf);
1807}
1808
1809
1810// ---------------------------------------------------------------------------
1811
1812static jlong
1813nSamplerCreate(JNIEnv *_env, jobject _this, jlong con, jint magFilter, jint minFilter,
1814               jint wrapS, jint wrapT, jint wrapR, jfloat aniso)
1815{
1816    if (kLogApi) {
1817        ALOGD("nSamplerCreate, con(%p)", (RsContext)con);
1818    }
1819    return (jlong)(uintptr_t)rsSamplerCreate((RsContext)con,
1820                                 (RsSamplerValue)magFilter,
1821                                 (RsSamplerValue)minFilter,
1822                                 (RsSamplerValue)wrapS,
1823                                 (RsSamplerValue)wrapT,
1824                                 (RsSamplerValue)wrapR,
1825                                 aniso);
1826}
1827
1828// ---------------------------------------------------------------------------
1829
1830static jlong
1831nPathCreate(JNIEnv *_env, jobject _this, jlong con, jint prim, jboolean isStatic, jlong _vtx, jlong _loop, jfloat q) {
1832    if (kLogApi) {
1833        ALOGD("nPathCreate, con(%p)", (RsContext)con);
1834    }
1835
1836    jlong id = (jlong)(uintptr_t)rsPathCreate((RsContext)con, (RsPathPrimitive)prim, isStatic,
1837                                   (RsAllocation)_vtx,
1838                                   (RsAllocation)_loop, q);
1839    return id;
1840}
1841
1842static jlong
1843nMeshCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _vtx, jlongArray _idx, jintArray _prim)
1844{
1845    if (kLogApi) {
1846        ALOGD("nMeshCreate, con(%p)", (RsContext)con);
1847    }
1848
1849    jint vtxLen = _env->GetArrayLength(_vtx);
1850    jlong *jVtxPtr = _env->GetLongArrayElements(_vtx, nullptr);
1851    RsAllocation* vtxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * vtxLen);
1852    for(int i = 0; i < vtxLen; ++i) {
1853        vtxPtr[i] = (RsAllocation)(uintptr_t)jVtxPtr[i];
1854    }
1855
1856    jint idxLen = _env->GetArrayLength(_idx);
1857    jlong *jIdxPtr = _env->GetLongArrayElements(_idx, nullptr);
1858    RsAllocation* idxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * idxLen);
1859    for(int i = 0; i < idxLen; ++i) {
1860        idxPtr[i] = (RsAllocation)(uintptr_t)jIdxPtr[i];
1861    }
1862
1863    jint primLen = _env->GetArrayLength(_prim);
1864    jint *primPtr = _env->GetIntArrayElements(_prim, nullptr);
1865
1866    jlong id = (jlong)(uintptr_t)rsMeshCreate((RsContext)con,
1867                               (RsAllocation *)vtxPtr, vtxLen,
1868                               (RsAllocation *)idxPtr, idxLen,
1869                               (uint32_t *)primPtr, primLen);
1870
1871    free(vtxPtr);
1872    free(idxPtr);
1873    _env->ReleaseLongArrayElements(_vtx, jVtxPtr, 0);
1874    _env->ReleaseLongArrayElements(_idx, jIdxPtr, 0);
1875    _env->ReleaseIntArrayElements(_prim, primPtr, 0);
1876    return id;
1877}
1878
1879static jint
1880nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
1881{
1882    if (kLogApi) {
1883        ALOGD("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
1884    }
1885    jint vtxCount = 0;
1886    rsaMeshGetVertexBufferCount((RsContext)con, (RsMesh)mesh, &vtxCount);
1887    return vtxCount;
1888}
1889
1890static jint
1891nMeshGetIndexCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
1892{
1893    if (kLogApi) {
1894        ALOGD("nMeshGetIndexCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
1895    }
1896    jint idxCount = 0;
1897    rsaMeshGetIndexCount((RsContext)con, (RsMesh)mesh, &idxCount);
1898    return idxCount;
1899}
1900
1901static void
1902nMeshGetVertices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _ids, jint numVtxIDs)
1903{
1904    if (kLogApi) {
1905        ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
1906    }
1907
1908    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
1909    rsaMeshGetVertices((RsContext)con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
1910
1911    for(jint i = 0; i < numVtxIDs; i ++) {
1912        const jlong alloc = (jlong)(uintptr_t)allocs[i];
1913        _env->SetLongArrayRegion(_ids, i, 1, &alloc);
1914    }
1915
1916    free(allocs);
1917}
1918
1919static void
1920nMeshGetIndices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _idxIds, jintArray _primitives, jint numIndices)
1921{
1922    if (kLogApi) {
1923        ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
1924    }
1925
1926    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
1927    uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
1928
1929    rsaMeshGetIndices((RsContext)con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
1930
1931    for(jint i = 0; i < numIndices; i ++) {
1932        const jlong alloc = (jlong)(uintptr_t)allocs[i];
1933        const jint prim = (jint)prims[i];
1934        _env->SetLongArrayRegion(_idxIds, i, 1, &alloc);
1935        _env->SetIntArrayRegion(_primitives, i, 1, &prim);
1936    }
1937
1938    free(allocs);
1939    free(prims);
1940}
1941
1942static jint
1943nSystemGetPointerSize(JNIEnv *_env, jobject _this) {
1944    return (jint)sizeof(void*);
1945}
1946
1947
1948// ---------------------------------------------------------------------------
1949
1950
1951static const char *classPathName = "android/renderscript/RenderScript";
1952
1953static JNINativeMethod methods[] = {
1954{"_nInit",                         "()V",                                     (void*)_nInit },
1955
1956{"nDeviceCreate",                  "()J",                                     (void*)nDeviceCreate },
1957{"nDeviceDestroy",                 "(J)V",                                    (void*)nDeviceDestroy },
1958{"nDeviceSetConfig",               "(JII)V",                                  (void*)nDeviceSetConfig },
1959{"nContextGetUserMessage",         "(J[I)I",                                  (void*)nContextGetUserMessage },
1960{"nContextGetErrorMessage",        "(J)Ljava/lang/String;",                   (void*)nContextGetErrorMessage },
1961{"nContextPeekMessage",            "(J[I)I",                                  (void*)nContextPeekMessage },
1962
1963{"nContextInitToClient",           "(J)V",                                    (void*)nContextInitToClient },
1964{"nContextDeinitToClient",         "(J)V",                                    (void*)nContextDeinitToClient },
1965
1966
1967// All methods below are thread protected in java.
1968{"rsnContextCreate",                 "(JIII)J",                               (void*)nContextCreate },
1969{"rsnContextCreateGL",               "(JIIIIIIIIIIIIFI)J",                    (void*)nContextCreateGL },
1970{"rsnContextFinish",                 "(J)V",                                  (void*)nContextFinish },
1971{"rsnContextSetPriority",            "(JI)V",                                 (void*)nContextSetPriority },
1972{"rsnContextSetSurface",             "(JIILandroid/view/Surface;)V",          (void*)nContextSetSurface },
1973{"rsnContextDestroy",                "(J)V",                                  (void*)nContextDestroy },
1974{"rsnContextDump",                   "(JI)V",                                 (void*)nContextDump },
1975{"rsnContextPause",                  "(J)V",                                  (void*)nContextPause },
1976{"rsnContextResume",                 "(J)V",                                  (void*)nContextResume },
1977{"rsnContextSendMessage",            "(JI[I)V",                               (void*)nContextSendMessage },
1978{"rsnClosureCreate",                 "(JJJ[J[J[I[J[J)J",                      (void*)nClosureCreate },
1979{"rsnInvokeClosureCreate",           "(JJ[B[J[J[I)J",                         (void*)nInvokeClosureCreate },
1980{"rsnClosureSetArg",                 "(JJIJI)V",                              (void*)nClosureSetArg },
1981{"rsnClosureSetGlobal",              "(JJJJI)V",                              (void*)nClosureSetGlobal },
1982{"rsnAssignName",                    "(JJ[B)V",                               (void*)nAssignName },
1983{"rsnGetName",                       "(JJ)Ljava/lang/String;",                (void*)nGetName },
1984{"rsnObjDestroy",                    "(JJ)V",                                 (void*)nObjDestroy },
1985
1986{"rsnFileA3DCreateFromFile",         "(JLjava/lang/String;)J",                (void*)nFileA3DCreateFromFile },
1987{"rsnFileA3DCreateFromAssetStream",  "(JJ)J",                                 (void*)nFileA3DCreateFromAssetStream },
1988{"rsnFileA3DCreateFromAsset",        "(JLandroid/content/res/AssetManager;Ljava/lang/String;)J",            (void*)nFileA3DCreateFromAsset },
1989{"rsnFileA3DGetNumIndexEntries",     "(JJ)I",                                 (void*)nFileA3DGetNumIndexEntries },
1990{"rsnFileA3DGetIndexEntries",        "(JJI[I[Ljava/lang/String;)V",           (void*)nFileA3DGetIndexEntries },
1991{"rsnFileA3DGetEntryByIndex",        "(JJI)J",                                (void*)nFileA3DGetEntryByIndex },
1992
1993{"rsnFontCreateFromFile",            "(JLjava/lang/String;FI)J",              (void*)nFontCreateFromFile },
1994{"rsnFontCreateFromAssetStream",     "(JLjava/lang/String;FIJ)J",             (void*)nFontCreateFromAssetStream },
1995{"rsnFontCreateFromAsset",        "(JLandroid/content/res/AssetManager;Ljava/lang/String;FI)J",            (void*)nFontCreateFromAsset },
1996
1997{"rsnElementCreate",                 "(JJIZI)J",                              (void*)nElementCreate },
1998{"rsnElementCreate2",                "(J[J[Ljava/lang/String;[I)J",           (void*)nElementCreate2 },
1999{"rsnElementGetNativeData",          "(JJ[I)V",                               (void*)nElementGetNativeData },
2000{"rsnElementGetSubElements",         "(JJ[J[Ljava/lang/String;[I)V",          (void*)nElementGetSubElements },
2001
2002{"rsnTypeCreate",                    "(JJIIIZZI)J",                           (void*)nTypeCreate },
2003{"rsnTypeGetNativeData",             "(JJ[J)V",                               (void*)nTypeGetNativeData },
2004
2005{"rsnAllocationCreateTyped",         "(JJIIJ)J",                               (void*)nAllocationCreateTyped },
2006{"rsnAllocationCreateFromBitmap",    "(JJILandroid/graphics/Bitmap;I)J",      (void*)nAllocationCreateFromBitmap },
2007{"rsnAllocationCreateBitmapBackedAllocation",    "(JJILandroid/graphics/Bitmap;I)J",      (void*)nAllocationCreateBitmapBackedAllocation },
2008{"rsnAllocationCubeCreateFromBitmap","(JJILandroid/graphics/Bitmap;I)J",      (void*)nAllocationCubeCreateFromBitmap },
2009
2010{"rsnAllocationCopyFromBitmap",      "(JJLandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyFromBitmap },
2011{"rsnAllocationCopyToBitmap",        "(JJLandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyToBitmap },
2012
2013{"rsnAllocationSyncAll",             "(JJI)V",                                (void*)nAllocationSyncAll },
2014{"rsnAllocationGetSurface",          "(JJ)Landroid/view/Surface;",            (void*)nAllocationGetSurface },
2015{"rsnAllocationSetSurface",          "(JJLandroid/view/Surface;)V",           (void*)nAllocationSetSurface },
2016{"rsnAllocationIoSend",              "(JJ)V",                                 (void*)nAllocationIoSend },
2017{"rsnAllocationIoReceive",           "(JJ)V",                                 (void*)nAllocationIoReceive },
2018{"rsnAllocationData1D",              "(JJIIILjava/lang/Object;II)V",          (void*)nAllocationData1D },
2019{"rsnAllocationElementData1D",       "(JJIII[BI)V",                           (void*)nAllocationElementData1D },
2020{"rsnAllocationData2D",              "(JJIIIIIILjava/lang/Object;II)V",       (void*)nAllocationData2D },
2021{"rsnAllocationData2D",              "(JJIIIIIIJIIII)V",                      (void*)nAllocationData2D_alloc },
2022{"rsnAllocationData3D",              "(JJIIIIIIILjava/lang/Object;II)V",      (void*)nAllocationData3D },
2023{"rsnAllocationData3D",              "(JJIIIIIIIJIIII)V",                     (void*)nAllocationData3D_alloc },
2024{"rsnAllocationRead",                "(JJLjava/lang/Object;I)V",              (void*)nAllocationRead },
2025{"rsnAllocationRead1D",              "(JJIIILjava/lang/Object;II)V",          (void*)nAllocationRead1D },
2026{"rsnAllocationRead2D",              "(JJIIIIIILjava/lang/Object;II)V",       (void*)nAllocationRead2D },
2027{"rsnAllocationGetType",             "(JJ)J",                                 (void*)nAllocationGetType},
2028{"rsnAllocationResize1D",            "(JJI)V",                                (void*)nAllocationResize1D },
2029{"rsnAllocationGenerateMipmaps",     "(JJ)V",                                 (void*)nAllocationGenerateMipmaps },
2030
2031{"rsnScriptBindAllocation",          "(JJJI)V",                               (void*)nScriptBindAllocation },
2032{"rsnScriptSetTimeZone",             "(JJ[B)V",                               (void*)nScriptSetTimeZone },
2033{"rsnScriptInvoke",                  "(JJI)V",                                (void*)nScriptInvoke },
2034{"rsnScriptInvokeV",                 "(JJI[B)V",                              (void*)nScriptInvokeV },
2035
2036{"rsnScriptForEach",                 "(JJI[JJ[B[I)V",                         (void*)nScriptForEach },
2037
2038{"rsnScriptSetVarI",                 "(JJII)V",                               (void*)nScriptSetVarI },
2039{"rsnScriptGetVarI",                 "(JJI)I",                                (void*)nScriptGetVarI },
2040{"rsnScriptSetVarJ",                 "(JJIJ)V",                               (void*)nScriptSetVarJ },
2041{"rsnScriptGetVarJ",                 "(JJI)J",                                (void*)nScriptGetVarJ },
2042{"rsnScriptSetVarF",                 "(JJIF)V",                               (void*)nScriptSetVarF },
2043{"rsnScriptGetVarF",                 "(JJI)F",                                (void*)nScriptGetVarF },
2044{"rsnScriptSetVarD",                 "(JJID)V",                               (void*)nScriptSetVarD },
2045{"rsnScriptGetVarD",                 "(JJI)D",                                (void*)nScriptGetVarD },
2046{"rsnScriptSetVarV",                 "(JJI[B)V",                              (void*)nScriptSetVarV },
2047{"rsnScriptGetVarV",                 "(JJI[B)V",                              (void*)nScriptGetVarV },
2048{"rsnScriptSetVarVE",                "(JJI[BJ[I)V",                           (void*)nScriptSetVarVE },
2049{"rsnScriptSetVarObj",               "(JJIJ)V",                               (void*)nScriptSetVarObj },
2050
2051{"rsnScriptCCreate",                 "(JLjava/lang/String;Ljava/lang/String;[BI)J",  (void*)nScriptCCreate },
2052{"rsnScriptIntrinsicCreate",         "(JIJ)J",                                (void*)nScriptIntrinsicCreate },
2053{"rsnScriptKernelIDCreate",          "(JJII)J",                               (void*)nScriptKernelIDCreate },
2054{"rsnScriptInvokeIDCreate",          "(JJI)J",                                (void*)nScriptInvokeIDCreate },
2055{"rsnScriptFieldIDCreate",           "(JJI)J",                                (void*)nScriptFieldIDCreate },
2056{"rsnScriptGroupCreate",             "(J[J[J[J[J[J)J",                        (void*)nScriptGroupCreate },
2057{"rsnScriptGroup2Create",            "(JLjava/lang/String;[J)J",               (void*)nScriptGroup2Create },
2058{"rsnScriptGroupSetInput",           "(JJJJ)V",                               (void*)nScriptGroupSetInput },
2059{"rsnScriptGroupSetOutput",          "(JJJJ)V",                               (void*)nScriptGroupSetOutput },
2060{"rsnScriptGroupExecute",            "(JJ)V",                                 (void*)nScriptGroupExecute },
2061{"rsnScriptGroup2Execute",           "(JJ)V",                                 (void*)nScriptGroup2Execute },
2062
2063{"rsnProgramStoreCreate",            "(JZZZZZZIII)J",                         (void*)nProgramStoreCreate },
2064
2065{"rsnProgramBindConstants",          "(JJIJ)V",                               (void*)nProgramBindConstants },
2066{"rsnProgramBindTexture",            "(JJIJ)V",                               (void*)nProgramBindTexture },
2067{"rsnProgramBindSampler",            "(JJIJ)V",                               (void*)nProgramBindSampler },
2068
2069{"rsnProgramFragmentCreate",         "(JLjava/lang/String;[Ljava/lang/String;[J)J",              (void*)nProgramFragmentCreate },
2070{"rsnProgramRasterCreate",           "(JZI)J",                                (void*)nProgramRasterCreate },
2071{"rsnProgramVertexCreate",           "(JLjava/lang/String;[Ljava/lang/String;[J)J",              (void*)nProgramVertexCreate },
2072
2073{"rsnContextBindRootScript",         "(JJ)V",                                 (void*)nContextBindRootScript },
2074{"rsnContextBindProgramStore",       "(JJ)V",                                 (void*)nContextBindProgramStore },
2075{"rsnContextBindProgramFragment",    "(JJ)V",                                 (void*)nContextBindProgramFragment },
2076{"rsnContextBindProgramVertex",      "(JJ)V",                                 (void*)nContextBindProgramVertex },
2077{"rsnContextBindProgramRaster",      "(JJ)V",                                 (void*)nContextBindProgramRaster },
2078
2079{"rsnSamplerCreate",                 "(JIIIIIF)J",                            (void*)nSamplerCreate },
2080
2081{"rsnPathCreate",                    "(JIZJJF)J",                             (void*)nPathCreate },
2082{"rsnMeshCreate",                    "(J[J[J[I)J",                            (void*)nMeshCreate },
2083
2084{"rsnMeshGetVertexBufferCount",      "(JJ)I",                                 (void*)nMeshGetVertexBufferCount },
2085{"rsnMeshGetIndexCount",             "(JJ)I",                                 (void*)nMeshGetIndexCount },
2086{"rsnMeshGetVertices",               "(JJ[JI)V",                              (void*)nMeshGetVertices },
2087{"rsnMeshGetIndices",                "(JJ[J[II)V",                            (void*)nMeshGetIndices },
2088
2089{"rsnSystemGetPointerSize",          "()I",                                   (void*)nSystemGetPointerSize },
2090};
2091
2092static int registerFuncs(JNIEnv *_env)
2093{
2094    return android::AndroidRuntime::registerNativeMethods(
2095            _env, classPathName, methods, NELEM(methods));
2096}
2097
2098// ---------------------------------------------------------------------------
2099
2100jint JNI_OnLoad(JavaVM* vm, void* reserved)
2101{
2102    JNIEnv* env = nullptr;
2103    jint result = -1;
2104
2105    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
2106        ALOGE("ERROR: GetEnv failed\n");
2107        goto bail;
2108    }
2109    assert(env != nullptr);
2110
2111    if (registerFuncs(env) < 0) {
2112        ALOGE("ERROR: Renderscript native registration failed\n");
2113        goto bail;
2114    }
2115
2116    /* success -- return valid version number */
2117    result = JNI_VERSION_1_4;
2118
2119bail:
2120    return result;
2121}
2122