android_renderscript_RenderScript.cpp revision 11e4317555fdabeecf28fe8a70bfecb216224ad1
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
26#include <SkBitmap.h>
27
28#include <androidfw/Asset.h>
29#include <androidfw/AssetManager.h>
30#include <androidfw/ResourceTypes.h>
31
32#include "jni.h"
33#include "JNIHelp.h"
34#include "android_runtime/AndroidRuntime.h"
35#include "android_runtime/android_view_Surface.h"
36#include "android_runtime/android_util_AssetManager.h"
37
38#include <rs.h>
39#include <rsEnv.h>
40#include <gui/Surface.h>
41#include <gui/GLConsumer.h>
42#include <gui/Surface.h>
43#include <android_runtime/android_graphics_SurfaceTexture.h>
44
45//#define LOG_API ALOGE
46#define LOG_API(...)
47
48using namespace android;
49
50#define PER_ARRAY_TYPE(flag, fnc, ...) {                                                \
51    jint len = 0;                                                                       \
52    void *ptr = NULL;                                                                   \
53    size_t typeBytes = 0;                                                               \
54    switch(dataType) {                                                                  \
55    case RS_TYPE_FLOAT_32:                                                              \
56        len = _env->GetArrayLength((jfloatArray)data);                                  \
57        ptr = _env->GetFloatArrayElements((jfloatArray)data, flag);                     \
58        typeBytes = 4;                                                                  \
59        fnc(__VA_ARGS__);                                                               \
60        _env->ReleaseFloatArrayElements((jfloatArray)data, (jfloat *)ptr, JNI_ABORT);   \
61        return;                                                                         \
62    case RS_TYPE_FLOAT_64:                                                              \
63        len = _env->GetArrayLength((jdoubleArray)data);                                 \
64        ptr = _env->GetDoubleArrayElements((jdoubleArray)data, flag);                   \
65        typeBytes = 8;                                                                  \
66        fnc(__VA_ARGS__);                                                               \
67        _env->ReleaseDoubleArrayElements((jdoubleArray)data, (jdouble *)ptr, JNI_ABORT);\
68        return;                                                                         \
69    case RS_TYPE_SIGNED_8:                                                              \
70    case RS_TYPE_UNSIGNED_8:                                                            \
71        len = _env->GetArrayLength((jbyteArray)data);                                   \
72        ptr = _env->GetByteArrayElements((jbyteArray)data, flag);                       \
73        typeBytes = 1;                                                                  \
74        fnc(__VA_ARGS__);                                                               \
75        _env->ReleaseByteArrayElements((jbyteArray)data, (jbyte*)ptr, JNI_ABORT);       \
76        return;                                                                         \
77    case RS_TYPE_SIGNED_16:                                                             \
78    case RS_TYPE_UNSIGNED_16:                                                           \
79        len = _env->GetArrayLength((jshortArray)data);                                  \
80        ptr = _env->GetShortArrayElements((jshortArray)data, flag);                     \
81        typeBytes = 2;                                                                  \
82        fnc(__VA_ARGS__);                                                               \
83        _env->ReleaseShortArrayElements((jshortArray)data, (jshort *)ptr, JNI_ABORT);   \
84        return;                                                                         \
85    case RS_TYPE_SIGNED_32:                                                             \
86    case RS_TYPE_UNSIGNED_32:                                                           \
87        len = _env->GetArrayLength((jintArray)data);                                    \
88        ptr = _env->GetIntArrayElements((jintArray)data, flag);                         \
89        typeBytes = 4;                                                                  \
90        fnc(__VA_ARGS__);                                                               \
91        _env->ReleaseIntArrayElements((jintArray)data, (jint *)ptr, JNI_ABORT);         \
92        return;                                                                         \
93    case RS_TYPE_SIGNED_64:                                                             \
94    case RS_TYPE_UNSIGNED_64:                                                           \
95        len = _env->GetArrayLength((jlongArray)data);                                   \
96        ptr = _env->GetLongArrayElements((jlongArray)data, flag);                       \
97        typeBytes = 8;                                                                  \
98        fnc(__VA_ARGS__);                                                               \
99        _env->ReleaseLongArrayElements((jlongArray)data, (jlong *)ptr, JNI_ABORT);      \
100        return;                                                                         \
101    default:                                                                            \
102        break;                                                                          \
103    }                                                                                   \
104}
105
106
107class AutoJavaStringToUTF8 {
108public:
109    AutoJavaStringToUTF8(JNIEnv* env, jstring str) : fEnv(env), fJStr(str) {
110        fCStr = env->GetStringUTFChars(str, NULL);
111        fLength = env->GetStringUTFLength(str);
112    }
113    ~AutoJavaStringToUTF8() {
114        fEnv->ReleaseStringUTFChars(fJStr, fCStr);
115    }
116    const char* c_str() const { return fCStr; }
117    jsize length() const { return fLength; }
118
119private:
120    JNIEnv*     fEnv;
121    jstring     fJStr;
122    const char* fCStr;
123    jsize       fLength;
124};
125
126class AutoJavaStringArrayToUTF8 {
127public:
128    AutoJavaStringArrayToUTF8(JNIEnv* env, jobjectArray strings, jsize stringsLength)
129    : mEnv(env), mStrings(strings), mStringsLength(stringsLength) {
130        mCStrings = NULL;
131        mSizeArray = NULL;
132        if (stringsLength > 0) {
133            mCStrings = (const char **)calloc(stringsLength, sizeof(char *));
134            mSizeArray = (size_t*)calloc(stringsLength, sizeof(size_t));
135            for (jsize ct = 0; ct < stringsLength; ct ++) {
136                jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
137                mCStrings[ct] = mEnv->GetStringUTFChars(s, NULL);
138                mSizeArray[ct] = mEnv->GetStringUTFLength(s);
139            }
140        }
141    }
142    ~AutoJavaStringArrayToUTF8() {
143        for (jsize ct=0; ct < mStringsLength; ct++) {
144            jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
145            mEnv->ReleaseStringUTFChars(s, mCStrings[ct]);
146        }
147        free(mCStrings);
148        free(mSizeArray);
149    }
150    const char **c_str() const { return mCStrings; }
151    size_t *c_str_len() const { return mSizeArray; }
152    jsize length() const { return mStringsLength; }
153
154private:
155    JNIEnv      *mEnv;
156    jobjectArray mStrings;
157    const char **mCStrings;
158    size_t      *mSizeArray;
159    jsize        mStringsLength;
160};
161
162// ---------------------------------------------------------------------------
163
164static jfieldID gContextId = 0;
165static jfieldID gNativeBitmapID = 0;
166static jfieldID gTypeNativeCache = 0;
167
168static void _nInit(JNIEnv *_env, jclass _this)
169{
170    gContextId             = _env->GetFieldID(_this, "mContext", "J");
171
172    jclass bitmapClass = _env->FindClass("android/graphics/Bitmap");
173    gNativeBitmapID = _env->GetFieldID(bitmapClass, "mNativeBitmap", "J");
174}
175
176// ---------------------------------------------------------------------------
177
178static void
179nContextFinish(JNIEnv *_env, jobject _this, jlong con)
180{
181    LOG_API("nContextFinish, con(%p)", (RsContext)con);
182    rsContextFinish((RsContext)con);
183}
184
185static void
186nAssignName(JNIEnv *_env, jobject _this, jlong con, jlong obj, jbyteArray str)
187{
188    LOG_API("nAssignName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
189    jint len = _env->GetArrayLength(str);
190    jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
191    rsAssignName((RsContext)con, (void *)obj, (const char *)cptr, len);
192    _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
193}
194
195static jstring
196nGetName(JNIEnv *_env, jobject _this, jlong con, jlong obj)
197{
198    LOG_API("nGetName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
199    const char *name = NULL;
200    rsaGetName((RsContext)con, (void *)obj, &name);
201    if(name == NULL || strlen(name) == 0) {
202        return NULL;
203    }
204    return _env->NewStringUTF(name);
205}
206
207static void
208nObjDestroy(JNIEnv *_env, jobject _this, jlong con, jlong obj)
209{
210    LOG_API("nObjDestroy, con(%p) obj(%p)", (RsContext)con, (void *)obj);
211    rsObjDestroy((RsContext)con, (void *)obj);
212}
213
214// ---------------------------------------------------------------------------
215
216static jlong
217nDeviceCreate(JNIEnv *_env, jobject _this)
218{
219    LOG_API("nDeviceCreate");
220    return (jlong)rsDeviceCreate();
221}
222
223static void
224nDeviceDestroy(JNIEnv *_env, jobject _this, jlong dev)
225{
226    LOG_API("nDeviceDestroy");
227    return rsDeviceDestroy((RsDevice)dev);
228}
229
230static void
231nDeviceSetConfig(JNIEnv *_env, jobject _this, jlong dev, jint p, jint value)
232{
233    LOG_API("nDeviceSetConfig  dev(%p), param(%i), value(%i)", (void *)dev, p, value);
234    return rsDeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value);
235}
236
237static jlong
238nContextCreate(JNIEnv *_env, jobject _this, jlong dev, jint ver, jint sdkVer, jint ct)
239{
240    LOG_API("nContextCreate");
241    return (jlong)rsContextCreate((RsDevice)dev, ver, sdkVer, (RsContextType)ct, 0);
242}
243
244static jlong
245nContextCreateGL(JNIEnv *_env, jobject _this, jlong dev, jint ver, jint sdkVer,
246                 jint colorMin, jint colorPref,
247                 jint alphaMin, jint alphaPref,
248                 jint depthMin, jint depthPref,
249                 jint stencilMin, jint stencilPref,
250                 jint samplesMin, jint samplesPref, jfloat samplesQ,
251                 jint dpi)
252{
253    RsSurfaceConfig sc;
254    sc.alphaMin = alphaMin;
255    sc.alphaPref = alphaPref;
256    sc.colorMin = colorMin;
257    sc.colorPref = colorPref;
258    sc.depthMin = depthMin;
259    sc.depthPref = depthPref;
260    sc.samplesMin = samplesMin;
261    sc.samplesPref = samplesPref;
262    sc.samplesQ = samplesQ;
263
264    LOG_API("nContextCreateGL");
265    return (jlong)rsContextCreateGL((RsDevice)dev, ver, sdkVer, sc, dpi);
266}
267
268static void
269nContextSetPriority(JNIEnv *_env, jobject _this, jlong con, jint p)
270{
271    LOG_API("ContextSetPriority, con(%p), priority(%i)", (RsContext)con, p);
272    rsContextSetPriority((RsContext)con, p);
273}
274
275
276
277static void
278nContextSetSurface(JNIEnv *_env, jobject _this, jlong con, jint width, jint height, jobject wnd)
279{
280    LOG_API("nContextSetSurface, con(%p), width(%i), height(%i), surface(%p)", (RsContext)con, width, height, (Surface *)wnd);
281
282    ANativeWindow * window = NULL;
283    if (wnd == NULL) {
284
285    } else {
286        window = android_view_Surface_getNativeWindow(_env, wnd).get();
287    }
288
289    rsContextSetSurface((RsContext)con, width, height, window);
290}
291
292static void
293nContextDestroy(JNIEnv *_env, jobject _this, jlong con)
294{
295    LOG_API("nContextDestroy, con(%p)", (RsContext)con);
296    rsContextDestroy((RsContext)con);
297}
298
299static void
300nContextDump(JNIEnv *_env, jobject _this, jlong con, jint bits)
301{
302    LOG_API("nContextDump, con(%p)  bits(%i)", (RsContext)con, bits);
303    rsContextDump((RsContext)con, bits);
304}
305
306static void
307nContextPause(JNIEnv *_env, jobject _this, jlong con)
308{
309    LOG_API("nContextPause, con(%p)", (RsContext)con);
310    rsContextPause((RsContext)con);
311}
312
313static void
314nContextResume(JNIEnv *_env, jobject _this, jlong con)
315{
316    LOG_API("nContextResume, con(%p)", (RsContext)con);
317    rsContextResume((RsContext)con);
318}
319
320
321static jstring
322nContextGetErrorMessage(JNIEnv *_env, jobject _this, jlong con)
323{
324    LOG_API("nContextGetErrorMessage, con(%p)", (RsContext)con);
325    char buf[1024];
326
327    size_t receiveLen;
328    uint32_t subID;
329    int id = rsContextGetMessage((RsContext)con,
330                                 buf, sizeof(buf),
331                                 &receiveLen, sizeof(receiveLen),
332                                 &subID, sizeof(subID));
333    if (!id && receiveLen) {
334        ALOGV("message receive buffer too small.  %i", receiveLen);
335    }
336    return _env->NewStringUTF(buf);
337}
338
339static jint
340nContextGetUserMessage(JNIEnv *_env, jobject _this, jlong con, jintArray data)
341{
342    jint len = _env->GetArrayLength(data);
343    LOG_API("nContextGetMessage, con(%p), len(%i)", (RsContext)con, len);
344    jint *ptr = _env->GetIntArrayElements(data, NULL);
345    size_t receiveLen;
346    uint32_t subID;
347    int id = rsContextGetMessage((RsContext)con,
348                                 ptr, len * 4,
349                                 &receiveLen, sizeof(receiveLen),
350                                 &subID, sizeof(subID));
351    if (!id && receiveLen) {
352        ALOGV("message receive buffer too small.  %i", receiveLen);
353    }
354    _env->ReleaseIntArrayElements(data, ptr, 0);
355    return (jint)id;
356}
357
358static jint
359nContextPeekMessage(JNIEnv *_env, jobject _this, jlong con, jintArray auxData)
360{
361    LOG_API("nContextPeekMessage, con(%p)", (RsContext)con);
362    jint *auxDataPtr = _env->GetIntArrayElements(auxData, NULL);
363    size_t receiveLen;
364    uint32_t subID;
365    int id = rsContextPeekMessage((RsContext)con, &receiveLen, sizeof(receiveLen),
366                                  &subID, sizeof(subID));
367    auxDataPtr[0] = (jint)subID;
368    auxDataPtr[1] = (jint)receiveLen;
369    _env->ReleaseIntArrayElements(auxData, auxDataPtr, 0);
370    return (jint)id;
371}
372
373static void nContextInitToClient(JNIEnv *_env, jobject _this, jlong con)
374{
375    LOG_API("nContextInitToClient, con(%p)", (RsContext)con);
376    rsContextInitToClient((RsContext)con);
377}
378
379static void nContextDeinitToClient(JNIEnv *_env, jobject _this, jlong con)
380{
381    LOG_API("nContextDeinitToClient, con(%p)", (RsContext)con);
382    rsContextDeinitToClient((RsContext)con);
383}
384
385static void
386nContextSendMessage(JNIEnv *_env, jobject _this, jlong con, jint id, jintArray data)
387{
388    jint *ptr = NULL;
389    jint len = 0;
390    if (data) {
391        len = _env->GetArrayLength(data);
392        ptr = _env->GetIntArrayElements(data, NULL);
393    }
394    LOG_API("nContextSendMessage, con(%p), id(%i), len(%i)", (RsContext)con, id, len);
395    rsContextSendMessage((RsContext)con, id, (const uint8_t *)ptr, len * sizeof(int));
396    if (data) {
397        _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
398    }
399}
400
401
402
403static jlong
404nElementCreate(JNIEnv *_env, jobject _this, jlong con, jlong type, jint kind, jboolean norm, jint size)
405{
406    LOG_API("nElementCreate, con(%p), type(%i), kind(%i), norm(%i), size(%i)", (RsContext)con, type, kind, norm, size);
407    return (jlong)rsElementCreate((RsContext)con, (RsDataType)type, (RsDataKind)kind, norm, size);
408}
409
410static jlong
411nElementCreate2(JNIEnv *_env, jobject _this, jlong con,
412                jlongArray _ids, jobjectArray _names, jintArray _arraySizes)
413{
414    int fieldCount = _env->GetArrayLength(_ids);
415    LOG_API("nElementCreate2, con(%p)", (RsContext)con);
416
417    jlong *jIds = _env->GetLongArrayElements(_ids, NULL);
418    jint *jArraySizes = _env->GetIntArrayElements(_arraySizes, NULL);
419
420    RsElement *ids = (RsElement*)malloc(fieldCount * sizeof(RsElement));
421    uint32_t *arraySizes = (uint32_t *)malloc(fieldCount * sizeof(uint32_t));
422
423    for(int i = 0; i < fieldCount; i ++) {
424        ids[i] = (RsElement)jIds[i];
425        arraySizes[i] = (uint32_t)jArraySizes[i];
426    }
427
428    AutoJavaStringArrayToUTF8 names(_env, _names, fieldCount);
429
430    const char **nameArray = names.c_str();
431    size_t *sizeArray = names.c_str_len();
432
433    jlong id = (jlong)rsElementCreate2((RsContext)con,
434                                     (const RsElement *)ids, fieldCount,
435                                     nameArray, fieldCount * sizeof(size_t),  sizeArray,
436                                     (const uint32_t *)arraySizes, fieldCount);
437
438    free(ids);
439    free(arraySizes);
440    _env->ReleaseLongArrayElements(_ids, jIds, JNI_ABORT);
441    _env->ReleaseIntArrayElements(_arraySizes, jArraySizes, JNI_ABORT);
442
443    return (jlong)id;
444}
445
446static void
447nElementGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jintArray _elementData)
448{
449    int dataSize = _env->GetArrayLength(_elementData);
450    LOG_API("nElementGetNativeData, con(%p)", (RsContext)con);
451
452    // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
453    assert(dataSize == 5);
454
455    uint32_t elementData[5];
456    rsaElementGetNativeData((RsContext)con, (RsElement)id, elementData, dataSize);
457
458    for(jint i = 0; i < dataSize; i ++) {
459        const jint data = (jint)elementData[i];
460        _env->SetIntArrayRegion(_elementData, i, 1, &data);
461    }
462}
463
464
465static void
466nElementGetSubElements(JNIEnv *_env, jobject _this, jlong con, jlong id,
467                       jlongArray _IDs,
468                       jobjectArray _names,
469                       jintArray _arraySizes)
470{
471    uint32_t dataSize = _env->GetArrayLength(_IDs);
472    LOG_API("nElementGetSubElements, con(%p)", (RsContext)con);
473
474    uintptr_t *ids = (uintptr_t*)malloc(dataSize * sizeof(uintptr_t));
475    const char **names = (const char **)malloc(dataSize * sizeof(const char *));
476    size_t *arraySizes = (size_t *)malloc(dataSize * sizeof(size_t));
477
478    rsaElementGetSubElements((RsContext)con, (RsElement)id, ids, names, arraySizes, (uint32_t)dataSize);
479
480    for(uint32_t i = 0; i < dataSize; i++) {
481        const jlong id = (jlong)ids[i];
482        const jint arraySize = (jint)arraySizes[i];
483        _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
484        _env->SetLongArrayRegion(_IDs, i, 1, &id);
485        _env->SetIntArrayRegion(_arraySizes, i, 1, &arraySize);
486    }
487
488    free(ids);
489    free(names);
490    free(arraySizes);
491}
492
493// -----------------------------------
494
495static jlong
496nTypeCreate(JNIEnv *_env, jobject _this, jlong con, jlong eid,
497            jint dimx, jint dimy, jint dimz, jboolean mips, jboolean faces, jint yuv)
498{
499    LOG_API("nTypeCreate, con(%p) eid(%p), x(%i), y(%i), z(%i), mips(%i), faces(%i), yuv(%i)",
500            (RsContext)con, eid, dimx, dimy, dimz, mips, faces, yuv);
501
502    return (jlong)rsTypeCreate((RsContext)con, (RsElement)eid, dimx, dimy, dimz, mips, faces, yuv);
503}
504
505static void
506nTypeGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jlongArray _typeData)
507{
508    // We are packing 6 items: mDimX; mDimY; mDimZ;
509    // mDimLOD; mDimFaces; mElement; into typeData
510    int elementCount = _env->GetArrayLength(_typeData);
511
512    assert(elementCount == 6);
513    LOG_API("nTypeGetNativeData, con(%p)", (RsContext)con);
514
515    uintptr_t typeData[6];
516    rsaTypeGetNativeData((RsContext)con, (RsType)id, typeData, 6);
517
518    for(jint i = 0; i < elementCount; i ++) {
519        const jlong data = (jlong)typeData[i];
520        _env->SetLongArrayRegion(_typeData, i, 1, &data);
521    }
522}
523
524// -----------------------------------
525
526static jlong
527nAllocationCreateTyped(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mips, jint usage, jlong pointer)
528{
529    LOG_API("nAllocationCreateTyped, con(%p), type(%p), mip(%i), usage(%i), ptr(%p)", (RsContext)con, (RsElement)type, mips, usage, (void *)pointer);
530    return (jlong) rsAllocationCreateTyped((RsContext)con, (RsType)type, (RsAllocationMipmapControl)mips, (uint32_t)usage, (uintptr_t)pointer);
531}
532
533static void
534nAllocationSyncAll(JNIEnv *_env, jobject _this, jlong con, jlong a, jint bits)
535{
536    LOG_API("nAllocationSyncAll, con(%p), a(%p), bits(0x%08x)", (RsContext)con, (RsAllocation)a, bits);
537    rsAllocationSyncAll((RsContext)con, (RsAllocation)a, (RsAllocationUsageType)bits);
538}
539
540static jobject
541nAllocationGetSurface(JNIEnv *_env, jobject _this, jlong con, jlong a)
542{
543    LOG_API("nAllocationGetSurface, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
544
545    IGraphicBufferProducer *v = (IGraphicBufferProducer *)rsAllocationGetSurface((RsContext)con, (RsAllocation)a);
546    sp<IGraphicBufferProducer> bp = v;
547    v->decStrong(NULL);
548
549    jobject o = android_view_Surface_createFromIGraphicBufferProducer(_env, bp);
550    return o;
551}
552
553static void
554nAllocationSetSurface(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject sur)
555{
556    LOG_API("nAllocationSetSurface, con(%p), alloc(%p), surface(%p)",
557            (RsContext)con, (RsAllocation)alloc, (Surface *)sur);
558
559    sp<Surface> s;
560    if (sur != 0) {
561        s = android_view_Surface_getSurface(_env, sur);
562    }
563
564    rsAllocationSetSurface((RsContext)con, (RsAllocation)alloc, static_cast<ANativeWindow *>(s.get()));
565}
566
567static void
568nAllocationIoSend(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
569{
570    LOG_API("nAllocationIoSend, con(%p), alloc(%p)", (RsContext)con, alloc);
571    rsAllocationIoSend((RsContext)con, (RsAllocation)alloc);
572}
573
574static void
575nAllocationIoReceive(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
576{
577    LOG_API("nAllocationIoReceive, con(%p), alloc(%p)", (RsContext)con, alloc);
578    rsAllocationIoReceive((RsContext)con, (RsAllocation)alloc);
579}
580
581
582static void
583nAllocationGenerateMipmaps(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
584{
585    LOG_API("nAllocationGenerateMipmaps, con(%p), a(%p)", (RsContext)con, (RsAllocation)alloc);
586    rsAllocationGenerateMipmaps((RsContext)con, (RsAllocation)alloc);
587}
588
589static jlong
590nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip, jobject jbitmap, jint usage)
591{
592    SkBitmap const * nativeBitmap =
593            (SkBitmap const *)_env->GetLongField(jbitmap, gNativeBitmapID);
594    const SkBitmap& bitmap(*nativeBitmap);
595
596    bitmap.lockPixels();
597    const void* ptr = bitmap.getPixels();
598    jlong id = (jlong)rsAllocationCreateFromBitmap((RsContext)con,
599                                                  (RsType)type, (RsAllocationMipmapControl)mip,
600                                                  ptr, bitmap.getSize(), usage);
601    bitmap.unlockPixels();
602    return id;
603}
604
605static jlong
606nAllocationCreateBitmapBackedAllocation(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip, jobject jbitmap, jint usage)
607{
608    SkBitmap const * nativeBitmap =
609            (SkBitmap const *)_env->GetLongField(jbitmap, gNativeBitmapID);
610    const SkBitmap& bitmap(*nativeBitmap);
611
612    bitmap.lockPixels();
613    const void* ptr = bitmap.getPixels();
614    jlong id = (jlong)rsAllocationCreateTyped((RsContext)con,
615                                            (RsType)type, (RsAllocationMipmapControl)mip,
616                                            (uint32_t)usage, (uintptr_t)ptr);
617    bitmap.unlockPixels();
618    return id;
619}
620
621static jlong
622nAllocationCubeCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip, jobject jbitmap, jint usage)
623{
624    SkBitmap const * nativeBitmap =
625            (SkBitmap const *)_env->GetLongField(jbitmap, gNativeBitmapID);
626    const SkBitmap& bitmap(*nativeBitmap);
627
628    bitmap.lockPixels();
629    const void* ptr = bitmap.getPixels();
630    jlong id = (jlong)rsAllocationCubeCreateFromBitmap((RsContext)con,
631                                                      (RsType)type, (RsAllocationMipmapControl)mip,
632                                                      ptr, bitmap.getSize(), usage);
633    bitmap.unlockPixels();
634    return id;
635}
636
637static void
638nAllocationCopyFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
639{
640    SkBitmap const * nativeBitmap =
641            (SkBitmap const *)_env->GetLongField(jbitmap, gNativeBitmapID);
642    const SkBitmap& bitmap(*nativeBitmap);
643    int w = bitmap.width();
644    int h = bitmap.height();
645
646    bitmap.lockPixels();
647    const void* ptr = bitmap.getPixels();
648    rsAllocation2DData((RsContext)con, (RsAllocation)alloc, 0, 0,
649                       0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
650                       w, h, ptr, bitmap.getSize(), 0);
651    bitmap.unlockPixels();
652}
653
654static void
655nAllocationCopyToBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
656{
657    SkBitmap const * nativeBitmap =
658            (SkBitmap const *)_env->GetLongField(jbitmap, gNativeBitmapID);
659    const SkBitmap& bitmap(*nativeBitmap);
660
661    bitmap.lockPixels();
662    void* ptr = bitmap.getPixels();
663    rsAllocationCopyToBitmap((RsContext)con, (RsAllocation)alloc, ptr, bitmap.getSize());
664    bitmap.unlockPixels();
665    bitmap.notifyPixelsChanged();
666}
667
668static void ReleaseBitmapCallback(void *bmp)
669{
670    SkBitmap const * nativeBitmap = (SkBitmap const *)bmp;
671    nativeBitmap->unlockPixels();
672}
673
674
675static void
676nAllocationData1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
677                  jint count, jobject data, jint sizeBytes, jint dataType)
678{
679    RsAllocation *alloc = (RsAllocation *)_alloc;
680    LOG_API("nAllocation1DData, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), dataType(%i)",
681            (RsContext)con, (RsAllocation)alloc, offset, count, sizeBytes, dataType);
682    PER_ARRAY_TYPE(NULL, rsAllocation1DData, (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
683}
684
685static void
686//    native void rsnAllocationElementData1D(long con, long id, int xoff, int compIdx, byte[] d, int sizeBytes);
687nAllocationElementData1D(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint offset, jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
688{
689    jint len = _env->GetArrayLength(data);
690    LOG_API("nAllocationElementData1D, con(%p), alloc(%p), offset(%i), comp(%i), len(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, offset, compIdx, len, sizeBytes);
691    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
692    rsAllocation1DElementData((RsContext)con, (RsAllocation)alloc, offset, lod, ptr, sizeBytes, compIdx);
693    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
694}
695
696static void
697nAllocationData2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
698                  jint w, jint h, jobject data, jint sizeBytes, jint dataType)
699{
700    RsAllocation *alloc = (RsAllocation *)_alloc;
701    RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
702    LOG_API("nAllocation2DData, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) type(%i)",
703            (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
704    PER_ARRAY_TYPE(NULL, rsAllocation2DData, (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
705}
706
707static void
708nAllocationData2D_alloc(JNIEnv *_env, jobject _this, jlong con,
709                        jlong dstAlloc, jint dstXoff, jint dstYoff,
710                        jint dstMip, jint dstFace,
711                        jint width, jint height,
712                        jlong srcAlloc, jint srcXoff, jint srcYoff,
713                        jint srcMip, jint srcFace)
714{
715    LOG_API("nAllocation2DData_s, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
716            " dstMip(%i), dstFace(%i), width(%i), height(%i),"
717            " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i), srcFace(%i)",
718            (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip, dstFace,
719            width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
720
721    rsAllocationCopy2DRange((RsContext)con,
722                            (RsAllocation)dstAlloc,
723                            dstXoff, dstYoff,
724                            dstMip, dstFace,
725                            width, height,
726                            (RsAllocation)srcAlloc,
727                            srcXoff, srcYoff,
728                            srcMip, srcFace);
729}
730
731static void
732nAllocationData3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
733                    jint w, jint h, jint d, jobject data, int sizeBytes, int dataType)
734{
735    RsAllocation *alloc = (RsAllocation *)_alloc;
736    LOG_API("nAllocation3DData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i), h(%i), d(%i), sizeBytes(%i)",
737            (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, lod, w, h, d, sizeBytes);
738    PER_ARRAY_TYPE(NULL, rsAllocation3DData, (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
739}
740
741static void
742nAllocationData3D_alloc(JNIEnv *_env, jobject _this, jlong con,
743                        jlong dstAlloc, jint dstXoff, jint dstYoff, jint dstZoff,
744                        jint dstMip,
745                        jint width, jint height, jint depth,
746                        jlong srcAlloc, jint srcXoff, jint srcYoff, jint srcZoff,
747                        jint srcMip)
748{
749    LOG_API("nAllocationData3D_alloc, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
750            " dstMip(%i), width(%i), height(%i),"
751            " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i)",
752            (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip,
753            width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip);
754
755    rsAllocationCopy3DRange((RsContext)con,
756                            (RsAllocation)dstAlloc,
757                            dstXoff, dstYoff, dstZoff, dstMip,
758                            width, height, depth,
759                            (RsAllocation)srcAlloc,
760                            srcXoff, srcYoff, srcZoff, srcMip);
761}
762
763
764static void
765nAllocationRead(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jobject data, int dataType)
766{
767    RsAllocation *alloc = (RsAllocation *)_alloc;
768    LOG_API("nAllocationRead, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
769    PER_ARRAY_TYPE(0, rsAllocationRead, (RsContext)con, alloc, ptr, len * typeBytes);
770}
771
772static void
773nAllocationRead1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
774                  jint count, jobject data, int sizeBytes, int dataType)
775{
776    RsAllocation *alloc = (RsAllocation *)_alloc;
777    LOG_API("nAllocation1DRead, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), dataType(%i)",
778            (RsContext)con, alloc, offset, count, sizeBytes, dataType);
779    PER_ARRAY_TYPE(0, rsAllocation1DRead, (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
780}
781
782static void
783nAllocationRead2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
784                  jint w, jint h, jobject data, int sizeBytes, int dataType)
785{
786    RsAllocation *alloc = (RsAllocation *)_alloc;
787    RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
788    LOG_API("nAllocation2DRead, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) type(%i)",
789            (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
790    PER_ARRAY_TYPE(0, rsAllocation2DRead, (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
791}
792
793static jlong
794nAllocationGetType(JNIEnv *_env, jobject _this, jlong con, jlong a)
795{
796    LOG_API("nAllocationGetType, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
797    return (jlong) rsaAllocationGetType((RsContext)con, (RsAllocation)a);
798}
799
800static void
801nAllocationResize1D(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint dimX)
802{
803    LOG_API("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", (RsContext)con, (RsAllocation)alloc, dimX);
804    rsAllocationResize1D((RsContext)con, (RsAllocation)alloc, dimX);
805}
806
807// -----------------------------------
808
809static jlong
810nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con, jlong native_asset)
811{
812    Asset* asset = reinterpret_cast<Asset*>(native_asset);
813    ALOGV("______nFileA3D %p", asset);
814
815    jlong id = (jlong)rsaFileA3DCreateFromMemory((RsContext)con, asset->getBuffer(false), asset->getLength());
816    return id;
817}
818
819static jlong
820nFileA3DCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path)
821{
822    AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
823    if (mgr == NULL) {
824        return 0;
825    }
826
827    AutoJavaStringToUTF8 str(_env, _path);
828    Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
829    if (asset == NULL) {
830        return 0;
831    }
832
833    jlong id = (jlong)rsaFileA3DCreateFromAsset((RsContext)con, asset);
834    return id;
835}
836
837static jlong
838nFileA3DCreateFromFile(JNIEnv *_env, jobject _this, jlong con, jstring fileName)
839{
840    AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
841    jlong id = (jlong)rsaFileA3DCreateFromFile((RsContext)con, fileNameUTF.c_str());
842
843    return id;
844}
845
846static jint
847nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D)
848{
849    int32_t numEntries = 0;
850    rsaFileA3DGetNumIndexEntries((RsContext)con, &numEntries, (RsFile)fileA3D);
851    return (jint)numEntries;
852}
853
854static void
855nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
856{
857    ALOGV("______nFileA3D %p", (RsFile) fileA3D);
858    RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
859
860    rsaFileA3DGetIndexEntries((RsContext)con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
861
862    for(jint i = 0; i < numEntries; i ++) {
863        _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
864        _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
865    }
866
867    free(fileEntries);
868}
869
870static jlong
871nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint index)
872{
873    ALOGV("______nFileA3D %p", (RsFile) fileA3D);
874    jlong id = (jlong)rsaFileA3DGetEntryByIndex((RsContext)con, (uint32_t)index, (RsFile)fileA3D);
875    return id;
876}
877
878// -----------------------------------
879
880static jlong
881nFontCreateFromFile(JNIEnv *_env, jobject _this, jlong con,
882                    jstring fileName, jfloat fontSize, jint dpi)
883{
884    AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
885    jlong id = (jlong)rsFontCreateFromFile((RsContext)con,
886                                         fileNameUTF.c_str(), fileNameUTF.length(),
887                                         fontSize, dpi);
888
889    return id;
890}
891
892static jlong
893nFontCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con,
894                           jstring name, jfloat fontSize, jint dpi, jlong native_asset)
895{
896    Asset* asset = reinterpret_cast<Asset*>(native_asset);
897    AutoJavaStringToUTF8 nameUTF(_env, name);
898
899    jlong id = (jlong)rsFontCreateFromMemory((RsContext)con,
900                                           nameUTF.c_str(), nameUTF.length(),
901                                           fontSize, dpi,
902                                           asset->getBuffer(false), asset->getLength());
903    return id;
904}
905
906static jlong
907nFontCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path,
908                     jfloat fontSize, jint dpi)
909{
910    AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
911    if (mgr == NULL) {
912        return 0;
913    }
914
915    AutoJavaStringToUTF8 str(_env, _path);
916    Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
917    if (asset == NULL) {
918        return 0;
919    }
920
921    jlong id = (jlong)rsFontCreateFromMemory((RsContext)con,
922                                           str.c_str(), str.length(),
923                                           fontSize, dpi,
924                                           asset->getBuffer(false), asset->getLength());
925    delete asset;
926    return id;
927}
928
929// -----------------------------------
930
931static void
932nScriptBindAllocation(JNIEnv *_env, jobject _this, jlong con, jlong script, jlong alloc, jint slot)
933{
934    LOG_API("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", (RsContext)con, (RsScript)script, (RsAllocation)alloc, slot);
935    rsScriptBindAllocation((RsContext)con, (RsScript)script, (RsAllocation)alloc, slot);
936}
937
938static void
939nScriptSetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jint val)
940{
941    LOG_API("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", (RsContext)con, (void *)script, slot, val);
942    rsScriptSetVarI((RsContext)con, (RsScript)script, slot, val);
943}
944
945static jint
946nScriptGetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
947{
948    LOG_API("nScriptGetVarI, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
949    int value = 0;
950    rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
951    return value;
952}
953
954static void
955nScriptSetVarObj(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
956{
957    LOG_API("nScriptSetVarObj, con(%p), s(%p), slot(%i), val(%i)", (RsContext)con, (void *)script, slot, val);
958    rsScriptSetVarObj((RsContext)con, (RsScript)script, slot, (RsObjectBase)val);
959}
960
961static void
962nScriptSetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
963{
964    LOG_API("nScriptSetVarJ, con(%p), s(%p), slot(%i), val(%lli)", (RsContext)con, (void *)script, slot, val);
965    rsScriptSetVarJ((RsContext)con, (RsScript)script, slot, val);
966}
967
968static jlong
969nScriptGetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
970{
971    LOG_API("nScriptGetVarJ, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
972    jlong value = 0;
973    rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
974    return value;
975}
976
977static void
978nScriptSetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, float val)
979{
980    LOG_API("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", (RsContext)con, (void *)script, slot, val);
981    rsScriptSetVarF((RsContext)con, (RsScript)script, slot, val);
982}
983
984static jfloat
985nScriptGetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
986{
987    LOG_API("nScriptGetVarF, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
988    jfloat value = 0;
989    rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
990    return value;
991}
992
993static void
994nScriptSetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, double val)
995{
996    LOG_API("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", (RsContext)con, (void *)script, slot, val);
997    rsScriptSetVarD((RsContext)con, (RsScript)script, slot, val);
998}
999
1000static jdouble
1001nScriptGetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
1002{
1003    LOG_API("nScriptGetVarD, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1004    jdouble value = 0;
1005    rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
1006    return value;
1007}
1008
1009static void
1010nScriptSetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
1011{
1012    LOG_API("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1013    jint len = _env->GetArrayLength(data);
1014    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
1015    rsScriptSetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
1016    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1017}
1018
1019static void
1020nScriptGetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
1021{
1022    LOG_API("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1023    jint len = _env->GetArrayLength(data);
1024    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
1025    rsScriptGetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
1026    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1027}
1028
1029static void
1030nScriptSetVarVE(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data, jlong elem, jintArray dims)
1031{
1032    LOG_API("nScriptSetVarVE, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1033    jint len = _env->GetArrayLength(data);
1034    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
1035    jint dimsLen = _env->GetArrayLength(dims) * sizeof(int);
1036    jint *dimsPtr = _env->GetIntArrayElements(dims, NULL);
1037    rsScriptSetVarVE((RsContext)con, (RsScript)script, slot, ptr, len, (RsElement)elem,
1038                     (const size_t*) dimsPtr, dimsLen);
1039    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1040    _env->ReleaseIntArrayElements(dims, dimsPtr, JNI_ABORT);
1041}
1042
1043
1044static void
1045nScriptSetTimeZone(JNIEnv *_env, jobject _this, jlong con, jlong script, jbyteArray timeZone)
1046{
1047    LOG_API("nScriptCSetTimeZone, con(%p), s(%p)", (RsContext)con, (void *)script);
1048
1049    jint length = _env->GetArrayLength(timeZone);
1050    jbyte* timeZone_ptr;
1051    timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
1052
1053    rsScriptSetTimeZone((RsContext)con, (RsScript)script, (const char *)timeZone_ptr, length);
1054
1055    if (timeZone_ptr) {
1056        _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
1057    }
1058}
1059
1060static void
1061nScriptInvoke(JNIEnv *_env, jobject _this, jlong con, jlong obj, jint slot)
1062{
1063    LOG_API("nScriptInvoke, con(%p), script(%p)", (RsContext)con, (void *)obj);
1064    rsScriptInvoke((RsContext)con, (RsScript)obj, slot);
1065}
1066
1067static void
1068nScriptInvokeV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
1069{
1070    LOG_API("nScriptInvokeV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1071    jint len = _env->GetArrayLength(data);
1072    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
1073    rsScriptInvokeV((RsContext)con, (RsScript)script, slot, ptr, len);
1074    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1075}
1076
1077static void
1078nScriptForEach(JNIEnv *_env, jobject _this, jlong con,
1079               jlong script, jint slot, jlong ain, jlong aout)
1080{
1081    LOG_API("nScriptForEach, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1082    rsScriptForEach((RsContext)con, (RsScript)script, slot, (RsAllocation)ain, (RsAllocation)aout, NULL, 0, NULL, 0);
1083}
1084static void
1085nScriptForEachV(JNIEnv *_env, jobject _this, jlong con,
1086                jlong script, jint slot, jlong ain, jlong aout, jbyteArray params)
1087{
1088    LOG_API("nScriptForEach, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1089    jint len = _env->GetArrayLength(params);
1090    jbyte *ptr = _env->GetByteArrayElements(params, NULL);
1091    rsScriptForEach((RsContext)con, (RsScript)script, slot, (RsAllocation)ain, (RsAllocation)aout, ptr, len, NULL, 0);
1092    _env->ReleaseByteArrayElements(params, ptr, JNI_ABORT);
1093}
1094
1095static void
1096nScriptForEachClipped(JNIEnv *_env, jobject _this, jlong con,
1097                      jlong script, jint slot, jlong ain, jlong aout,
1098                      jint xstart, jint xend,
1099                      jint ystart, jint yend, jint zstart, jint zend)
1100{
1101    LOG_API("nScriptForEachClipped, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1102    RsScriptCall sc;
1103    sc.xStart = xstart;
1104    sc.xEnd = xend;
1105    sc.yStart = ystart;
1106    sc.yEnd = yend;
1107    sc.zStart = zstart;
1108    sc.zEnd = zend;
1109    sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
1110    sc.arrayStart = 0;
1111    sc.arrayEnd = 0;
1112    rsScriptForEach((RsContext)con, (RsScript)script, slot, (RsAllocation)ain, (RsAllocation)aout, NULL, 0, &sc, sizeof(sc));
1113}
1114
1115static void
1116nScriptForEachClippedV(JNIEnv *_env, jobject _this, jlong con,
1117                       jlong script, jint slot, jlong ain, jlong aout,
1118                       jbyteArray params, jint xstart, jint xend,
1119                       jint ystart, jint yend, jint zstart, jint zend)
1120{
1121    LOG_API("nScriptForEachClipped, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1122    jint len = _env->GetArrayLength(params);
1123    jbyte *ptr = _env->GetByteArrayElements(params, NULL);
1124    RsScriptCall sc;
1125    sc.xStart = xstart;
1126    sc.xEnd = xend;
1127    sc.yStart = ystart;
1128    sc.yEnd = yend;
1129    sc.zStart = zstart;
1130    sc.zEnd = zend;
1131    sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
1132    sc.arrayStart = 0;
1133    sc.arrayEnd = 0;
1134    rsScriptForEach((RsContext)con, (RsScript)script, slot, (RsAllocation)ain, (RsAllocation)aout, ptr, len, &sc, sizeof(sc));
1135    _env->ReleaseByteArrayElements(params, ptr, JNI_ABORT);
1136}
1137
1138// -----------------------------------
1139
1140static jlong
1141nScriptCCreate(JNIEnv *_env, jobject _this, jlong con,
1142               jstring resName, jstring cacheDir,
1143               jbyteArray scriptRef, jint length)
1144{
1145    LOG_API("nScriptCCreate, con(%p)", (RsContext)con);
1146
1147    AutoJavaStringToUTF8 resNameUTF(_env, resName);
1148    AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
1149    jlong ret = 0;
1150    jbyte* script_ptr = NULL;
1151    jint _exception = 0;
1152    jint remaining;
1153    if (!scriptRef) {
1154        _exception = 1;
1155        //jniThrowException(_env, "java/lang/IllegalArgumentException", "script == null");
1156        goto exit;
1157    }
1158    if (length < 0) {
1159        _exception = 1;
1160        //jniThrowException(_env, "java/lang/IllegalArgumentException", "length < 0");
1161        goto exit;
1162    }
1163    remaining = _env->GetArrayLength(scriptRef);
1164    if (remaining < length) {
1165        _exception = 1;
1166        //jniThrowException(_env, "java/lang/IllegalArgumentException",
1167        //        "length > script.length - offset");
1168        goto exit;
1169    }
1170    script_ptr = (jbyte *)
1171        _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
1172
1173    //rsScriptCSetText((RsContext)con, (const char *)script_ptr, length);
1174
1175    ret = (jlong)rsScriptCCreate((RsContext)con,
1176                                resNameUTF.c_str(), resNameUTF.length(),
1177                                cacheDirUTF.c_str(), cacheDirUTF.length(),
1178                                (const char *)script_ptr, length);
1179
1180exit:
1181    if (script_ptr) {
1182        _env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr,
1183                _exception ? JNI_ABORT: 0);
1184    }
1185
1186    return (jlong)ret;
1187}
1188
1189static jlong
1190nScriptIntrinsicCreate(JNIEnv *_env, jobject _this, jlong con, jint id, jlong eid)
1191{
1192    LOG_API("nScriptIntrinsicCreate, con(%p) id(%i) element(%p)", (RsContext)con, id, (void *)eid);
1193    return (jlong)rsScriptIntrinsicCreate((RsContext)con, id, (RsElement)eid);
1194}
1195
1196static jlong
1197nScriptKernelIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot, jint sig)
1198{
1199    LOG_API("nScriptKernelIDCreate, con(%p) script(%p), slot(%i), sig(%i)", (RsContext)con, (void *)sid, slot, sig);
1200    return (jlong)rsScriptKernelIDCreate((RsContext)con, (RsScript)sid, slot, sig);
1201}
1202
1203static jlong
1204nScriptFieldIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
1205{
1206    LOG_API("nScriptFieldIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con, (void *)sid, slot);
1207    return (jlong)rsScriptFieldIDCreate((RsContext)con, (RsScript)sid, slot);
1208}
1209
1210static jlong
1211nScriptGroupCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _kernels, jlongArray _src,
1212    jlongArray _dstk, jlongArray _dstf, jlongArray _types)
1213{
1214    LOG_API("nScriptGroupCreate, con(%p)", (RsContext)con);
1215
1216    jint kernelsLen = _env->GetArrayLength(_kernels);
1217    jlong *jKernelsPtr = _env->GetLongArrayElements(_kernels, NULL);
1218    RsScriptKernelID* kernelsPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * kernelsLen);
1219    for(int i = 0; i < kernelsLen; ++i) {
1220        kernelsPtr[i] = (RsScriptKernelID)jKernelsPtr[i];
1221    }
1222
1223    jint srcLen = _env->GetArrayLength(_src);
1224    jlong *jSrcPtr = _env->GetLongArrayElements(_src, NULL);
1225    RsScriptKernelID* srcPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * srcLen);
1226    for(int i = 0; i < srcLen; ++i) {
1227        srcPtr[i] = (RsScriptKernelID)jSrcPtr[i];
1228    }
1229
1230    jint dstkLen = _env->GetArrayLength(_dstk);
1231    jlong *jDstkPtr = _env->GetLongArrayElements(_dstk, NULL);
1232    RsScriptKernelID* dstkPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstkLen);
1233    for(int i = 0; i < dstkLen; ++i) {
1234        dstkPtr[i] = (RsScriptKernelID)jDstkPtr[i];
1235    }
1236
1237    jint dstfLen = _env->GetArrayLength(_dstf);
1238    jlong *jDstfPtr = _env->GetLongArrayElements(_dstf, NULL);
1239    RsScriptKernelID* dstfPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstfLen);
1240    for(int i = 0; i < dstfLen; ++i) {
1241        dstfPtr[i] = (RsScriptKernelID)jDstfPtr[i];
1242    }
1243
1244    jint typesLen = _env->GetArrayLength(_types);
1245    jlong *jTypesPtr = _env->GetLongArrayElements(_types, NULL);
1246    RsType* typesPtr = (RsType*) malloc(sizeof(RsType) * typesLen);
1247    for(int i = 0; i < typesLen; ++i) {
1248        typesPtr[i] = (RsType)jTypesPtr[i];
1249    }
1250
1251    jlong id = (jlong)rsScriptGroupCreate((RsContext)con,
1252                               (RsScriptKernelID *)kernelsPtr, kernelsLen * sizeof(RsScriptKernelID),
1253                               (RsScriptKernelID *)srcPtr, srcLen * sizeof(RsScriptKernelID),
1254                               (RsScriptKernelID *)dstkPtr, dstkLen * sizeof(RsScriptKernelID),
1255                               (RsScriptFieldID *)dstfPtr, dstfLen * sizeof(RsScriptKernelID),
1256                               (RsType *)typesPtr, typesLen * sizeof(RsType));
1257
1258    free(kernelsPtr);
1259    free(srcPtr);
1260    free(dstkPtr);
1261    free(dstfPtr);
1262    free(typesPtr);
1263    _env->ReleaseLongArrayElements(_kernels, jKernelsPtr, 0);
1264    _env->ReleaseLongArrayElements(_src, jSrcPtr, 0);
1265    _env->ReleaseLongArrayElements(_dstk, jDstkPtr, 0);
1266    _env->ReleaseLongArrayElements(_dstf, jDstfPtr, 0);
1267    _env->ReleaseLongArrayElements(_types, jTypesPtr, 0);
1268    return id;
1269}
1270
1271static void
1272nScriptGroupSetInput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
1273{
1274    LOG_API("nScriptGroupSetInput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
1275        (void *)gid, (void *)kid, (void *)alloc);
1276    rsScriptGroupSetInput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
1277}
1278
1279static void
1280nScriptGroupSetOutput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
1281{
1282    LOG_API("nScriptGroupSetOutput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
1283        (void *)gid, (void *)kid, (void *)alloc);
1284    rsScriptGroupSetOutput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
1285}
1286
1287static void
1288nScriptGroupExecute(JNIEnv *_env, jobject _this, jlong con, jlong gid)
1289{
1290    LOG_API("nScriptGroupSetOutput, con(%p) group(%p)", (RsContext)con, (void *)gid);
1291    rsScriptGroupExecute((RsContext)con, (RsScriptGroup)gid);
1292}
1293
1294// ---------------------------------------------------------------------------
1295
1296static jlong
1297nProgramStoreCreate(JNIEnv *_env, jobject _this, jlong con,
1298                    jboolean colorMaskR, jboolean colorMaskG, jboolean colorMaskB, jboolean colorMaskA,
1299                    jboolean depthMask, jboolean ditherEnable,
1300                    jint srcFunc, jint destFunc,
1301                    jint depthFunc)
1302{
1303    LOG_API("nProgramStoreCreate, con(%p)", (RsContext)con);
1304    return (jlong)rsProgramStoreCreate((RsContext)con, colorMaskR, colorMaskG, colorMaskB, colorMaskA,
1305                                      depthMask, ditherEnable, (RsBlendSrcFunc)srcFunc,
1306                                      (RsBlendDstFunc)destFunc, (RsDepthFunc)depthFunc);
1307}
1308
1309// ---------------------------------------------------------------------------
1310
1311static void
1312nProgramBindConstants(JNIEnv *_env, jobject _this, jlong con, jlong vpv, jint slot, jlong a)
1313{
1314    LOG_API("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", (RsContext)con, (RsProgramVertex)vpv, slot, (RsAllocation)a);
1315    rsProgramBindConstants((RsContext)con, (RsProgram)vpv, slot, (RsAllocation)a);
1316}
1317
1318static void
1319nProgramBindTexture(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
1320{
1321    LOG_API("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
1322    rsProgramBindTexture((RsContext)con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
1323}
1324
1325static void
1326nProgramBindSampler(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
1327{
1328    LOG_API("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con, (RsProgramFragment)vpf, slot, (RsSampler)a);
1329    rsProgramBindSampler((RsContext)con, (RsProgramFragment)vpf, slot, (RsSampler)a);
1330}
1331
1332// ---------------------------------------------------------------------------
1333
1334static jlong
1335nProgramFragmentCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
1336                       jobjectArray texNames, jlongArray params)
1337{
1338    AutoJavaStringToUTF8 shaderUTF(_env, shader);
1339    jlong *jParamPtr = _env->GetLongArrayElements(params, NULL);
1340    jint paramLen = _env->GetArrayLength(params);
1341
1342    int texCount = _env->GetArrayLength(texNames);
1343    AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
1344    const char ** nameArray = names.c_str();
1345    size_t* sizeArray = names.c_str_len();
1346
1347    LOG_API("nProgramFragmentCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
1348
1349    uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
1350    for(int i = 0; i < paramLen; ++i) {
1351        paramPtr[i] = (uintptr_t)jParamPtr[i];
1352    }
1353    jlong ret = (jlong)rsProgramFragmentCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
1354                                             nameArray, texCount, sizeArray,
1355                                             paramPtr, paramLen);
1356
1357    free(paramPtr);
1358    _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
1359    return ret;
1360}
1361
1362
1363// ---------------------------------------------------------------------------
1364
1365static jlong
1366nProgramVertexCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
1367                     jobjectArray texNames, jlongArray params)
1368{
1369    AutoJavaStringToUTF8 shaderUTF(_env, shader);
1370    jlong *jParamPtr = _env->GetLongArrayElements(params, NULL);
1371    jint paramLen = _env->GetArrayLength(params);
1372
1373    LOG_API("nProgramVertexCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
1374
1375    int texCount = _env->GetArrayLength(texNames);
1376    AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
1377    const char ** nameArray = names.c_str();
1378    size_t* sizeArray = names.c_str_len();
1379
1380    uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
1381    for(int i = 0; i < paramLen; ++i) {
1382        paramPtr[i] = (uintptr_t)jParamPtr[i];
1383    }
1384
1385    jlong ret = (jlong)rsProgramVertexCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
1386                                           nameArray, texCount, sizeArray,
1387                                           paramPtr, paramLen);
1388
1389    free(paramPtr);
1390    _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
1391    return ret;
1392}
1393
1394// ---------------------------------------------------------------------------
1395
1396static jlong
1397nProgramRasterCreate(JNIEnv *_env, jobject _this, jlong con, jboolean pointSprite, jint cull)
1398{
1399    LOG_API("nProgramRasterCreate, con(%p), pointSprite(%i), cull(%i)", (RsContext)con, pointSprite, cull);
1400    return (jlong)rsProgramRasterCreate((RsContext)con, pointSprite, (RsCullMode)cull);
1401}
1402
1403
1404// ---------------------------------------------------------------------------
1405
1406static void
1407nContextBindRootScript(JNIEnv *_env, jobject _this, jlong con, jint script)
1408{
1409    LOG_API("nContextBindRootScript, con(%p), script(%p)", (RsContext)con, (RsScript)script);
1410    rsContextBindRootScript((RsContext)con, (RsScript)script);
1411}
1412
1413static void
1414nContextBindProgramStore(JNIEnv *_env, jobject _this, jlong con, jint pfs)
1415{
1416    LOG_API("nContextBindProgramStore, con(%p), pfs(%p)", (RsContext)con, (RsProgramStore)pfs);
1417    rsContextBindProgramStore((RsContext)con, (RsProgramStore)pfs);
1418}
1419
1420static void
1421nContextBindProgramFragment(JNIEnv *_env, jobject _this, jlong con, jint pf)
1422{
1423    LOG_API("nContextBindProgramFragment, con(%p), pf(%p)", (RsContext)con, (RsProgramFragment)pf);
1424    rsContextBindProgramFragment((RsContext)con, (RsProgramFragment)pf);
1425}
1426
1427static void
1428nContextBindProgramVertex(JNIEnv *_env, jobject _this, jlong con, jint pf)
1429{
1430    LOG_API("nContextBindProgramVertex, con(%p), pf(%p)", (RsContext)con, (RsProgramVertex)pf);
1431    rsContextBindProgramVertex((RsContext)con, (RsProgramVertex)pf);
1432}
1433
1434static void
1435nContextBindProgramRaster(JNIEnv *_env, jobject _this, jlong con, jint pf)
1436{
1437    LOG_API("nContextBindProgramRaster, con(%p), pf(%p)", (RsContext)con, (RsProgramRaster)pf);
1438    rsContextBindProgramRaster((RsContext)con, (RsProgramRaster)pf);
1439}
1440
1441
1442// ---------------------------------------------------------------------------
1443
1444static jlong
1445nSamplerCreate(JNIEnv *_env, jobject _this, jlong con, jint magFilter, jint minFilter,
1446               jint wrapS, jint wrapT, jint wrapR, jfloat aniso)
1447{
1448    LOG_API("nSamplerCreate, con(%p)", (RsContext)con);
1449    return (jlong)rsSamplerCreate((RsContext)con,
1450                                 (RsSamplerValue)magFilter,
1451                                 (RsSamplerValue)minFilter,
1452                                 (RsSamplerValue)wrapS,
1453                                 (RsSamplerValue)wrapT,
1454                                 (RsSamplerValue)wrapR,
1455                                 aniso);
1456}
1457
1458// ---------------------------------------------------------------------------
1459
1460static jlong
1461nPathCreate(JNIEnv *_env, jobject _this, jlong con, jint prim, jboolean isStatic, jlong _vtx, jlong _loop, jfloat q) {
1462    LOG_API("nPathCreate, con(%p)", (RsContext)con);
1463
1464    jlong id = (jlong)rsPathCreate((RsContext)con, (RsPathPrimitive)prim, isStatic,
1465                                   (RsAllocation)_vtx,
1466                                   (RsAllocation)_loop, q);
1467    return id;
1468}
1469
1470static jlong
1471nMeshCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _vtx, jlongArray _idx, jintArray _prim)
1472{
1473    LOG_API("nMeshCreate, con(%p)", (RsContext)con);
1474
1475    jint vtxLen = _env->GetArrayLength(_vtx);
1476    jlong *jVtxPtr = _env->GetLongArrayElements(_vtx, NULL);
1477    RsAllocation* vtxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * vtxLen);
1478    for(int i = 0; i < vtxLen; ++i) {
1479        vtxPtr[i] = (RsAllocation)(uintptr_t)jVtxPtr[i];
1480    }
1481
1482    jint idxLen = _env->GetArrayLength(_idx);
1483    jlong *jIdxPtr = _env->GetLongArrayElements(_idx, NULL);
1484    RsAllocation* idxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * idxLen);
1485    for(int i = 0; i < idxLen; ++i) {
1486        idxPtr[i] = (RsAllocation)(uintptr_t)jIdxPtr[i];
1487    }
1488
1489    jint primLen = _env->GetArrayLength(_prim);
1490    jint *primPtr = _env->GetIntArrayElements(_prim, NULL);
1491
1492    jlong id = (jlong)rsMeshCreate((RsContext)con,
1493                               (RsAllocation *)vtxPtr, vtxLen,
1494                               (RsAllocation *)idxPtr, idxLen,
1495                               (uint32_t *)primPtr, primLen);
1496
1497    free(vtxPtr);
1498    free(idxPtr);
1499    _env->ReleaseLongArrayElements(_vtx, jVtxPtr, 0);
1500    _env->ReleaseLongArrayElements(_idx, jIdxPtr, 0);
1501    _env->ReleaseIntArrayElements(_prim, primPtr, 0);
1502    return id;
1503}
1504
1505static jint
1506nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
1507{
1508    LOG_API("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
1509    jint vtxCount = 0;
1510    rsaMeshGetVertexBufferCount((RsContext)con, (RsMesh)mesh, &vtxCount);
1511    return vtxCount;
1512}
1513
1514static jint
1515nMeshGetIndexCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
1516{
1517    LOG_API("nMeshGetIndexCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
1518    jint idxCount = 0;
1519    rsaMeshGetIndexCount((RsContext)con, (RsMesh)mesh, &idxCount);
1520    return idxCount;
1521}
1522
1523static void
1524nMeshGetVertices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _ids, jint numVtxIDs)
1525{
1526    LOG_API("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
1527
1528    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
1529    rsaMeshGetVertices((RsContext)con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
1530
1531    for(jint i = 0; i < numVtxIDs; i ++) {
1532        const jlong alloc = (jlong)allocs[i];
1533        _env->SetLongArrayRegion(_ids, i, 1, &alloc);
1534    }
1535
1536    free(allocs);
1537}
1538
1539static void
1540nMeshGetIndices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _idxIds, jintArray _primitives, jint numIndices)
1541{
1542    LOG_API("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
1543
1544    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
1545    uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
1546
1547    rsaMeshGetIndices((RsContext)con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
1548
1549    for(jint i = 0; i < numIndices; i ++) {
1550        const jlong alloc = (jlong)allocs[i];
1551        const jint prim = (jint)prims[i];
1552        _env->SetLongArrayRegion(_idxIds, i, 1, &alloc);
1553        _env->SetIntArrayRegion(_primitives, i, 1, &prim);
1554    }
1555
1556    free(allocs);
1557    free(prims);
1558}
1559
1560// ---------------------------------------------------------------------------
1561
1562
1563static const char *classPathName = "android/renderscript/RenderScript";
1564
1565static JNINativeMethod methods[] = {
1566{"_nInit",                         "()V",                                     (void*)_nInit },
1567
1568{"nDeviceCreate",                  "()J",                                     (void*)nDeviceCreate },
1569{"nDeviceDestroy",                 "(J)V",                                    (void*)nDeviceDestroy },
1570{"nDeviceSetConfig",               "(JII)V",                                  (void*)nDeviceSetConfig },
1571{"nContextGetUserMessage",         "(J[I)I",                                  (void*)nContextGetUserMessage },
1572{"nContextGetErrorMessage",        "(J)Ljava/lang/String;",                   (void*)nContextGetErrorMessage },
1573{"nContextPeekMessage",            "(J[I)I",                                  (void*)nContextPeekMessage },
1574
1575{"nContextInitToClient",           "(J)V",                                    (void*)nContextInitToClient },
1576{"nContextDeinitToClient",         "(J)V",                                    (void*)nContextDeinitToClient },
1577
1578
1579// All methods below are thread protected in java.
1580{"rsnContextCreate",                 "(JIII)J",                               (void*)nContextCreate },
1581{"rsnContextCreateGL",               "(JIIIIIIIIIIIIFI)J",                    (void*)nContextCreateGL },
1582{"rsnContextFinish",                 "(J)V",                                  (void*)nContextFinish },
1583{"rsnContextSetPriority",            "(JI)V",                                 (void*)nContextSetPriority },
1584{"rsnContextSetSurface",             "(JIILandroid/view/Surface;)V",          (void*)nContextSetSurface },
1585{"rsnContextDestroy",                "(J)V",                                  (void*)nContextDestroy },
1586{"rsnContextDump",                   "(JI)V",                                 (void*)nContextDump },
1587{"rsnContextPause",                  "(J)V",                                  (void*)nContextPause },
1588{"rsnContextResume",                 "(J)V",                                  (void*)nContextResume },
1589{"rsnContextSendMessage",            "(JI[I)V",                               (void*)nContextSendMessage },
1590{"rsnAssignName",                    "(JJ[B)V",                               (void*)nAssignName },
1591{"rsnGetName",                       "(JJ)Ljava/lang/String;",                (void*)nGetName },
1592{"rsnObjDestroy",                    "(JJ)V",                                 (void*)nObjDestroy },
1593
1594{"rsnFileA3DCreateFromFile",         "(JLjava/lang/String;)J",                (void*)nFileA3DCreateFromFile },
1595{"rsnFileA3DCreateFromAssetStream",  "(JJ)J",                                 (void*)nFileA3DCreateFromAssetStream },
1596{"rsnFileA3DCreateFromAsset",        "(JLandroid/content/res/AssetManager;Ljava/lang/String;)J",            (void*)nFileA3DCreateFromAsset },
1597{"rsnFileA3DGetNumIndexEntries",     "(JJ)I",                                 (void*)nFileA3DGetNumIndexEntries },
1598{"rsnFileA3DGetIndexEntries",        "(JJI[I[Ljava/lang/String;)V",           (void*)nFileA3DGetIndexEntries },
1599{"rsnFileA3DGetEntryByIndex",        "(JJI)J",                                (void*)nFileA3DGetEntryByIndex },
1600
1601{"rsnFontCreateFromFile",            "(JLjava/lang/String;FI)J",              (void*)nFontCreateFromFile },
1602{"rsnFontCreateFromAssetStream",     "(JLjava/lang/String;FIJ)J",             (void*)nFontCreateFromAssetStream },
1603{"rsnFontCreateFromAsset",        "(JLandroid/content/res/AssetManager;Ljava/lang/String;FI)J",            (void*)nFontCreateFromAsset },
1604
1605{"rsnElementCreate",                 "(JJIZI)J",                              (void*)nElementCreate },
1606{"rsnElementCreate2",                "(J[J[Ljava/lang/String;[I)J",           (void*)nElementCreate2 },
1607{"rsnElementGetNativeData",          "(JJ[I)V",                               (void*)nElementGetNativeData },
1608{"rsnElementGetSubElements",         "(JJ[J[Ljava/lang/String;[I)V",          (void*)nElementGetSubElements },
1609
1610{"rsnTypeCreate",                    "(JJIIIZZI)J",                           (void*)nTypeCreate },
1611{"rsnTypeGetNativeData",             "(JJ[J)V",                               (void*)nTypeGetNativeData },
1612
1613{"rsnAllocationCreateTyped",         "(JJIIJ)J",                               (void*)nAllocationCreateTyped },
1614{"rsnAllocationCreateFromBitmap",    "(JJILandroid/graphics/Bitmap;I)J",      (void*)nAllocationCreateFromBitmap },
1615{"rsnAllocationCreateBitmapBackedAllocation",    "(JJILandroid/graphics/Bitmap;I)J",      (void*)nAllocationCreateBitmapBackedAllocation },
1616{"rsnAllocationCubeCreateFromBitmap","(JJILandroid/graphics/Bitmap;I)J",      (void*)nAllocationCubeCreateFromBitmap },
1617
1618{"rsnAllocationCopyFromBitmap",      "(JJLandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyFromBitmap },
1619{"rsnAllocationCopyToBitmap",        "(JJLandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyToBitmap },
1620
1621{"rsnAllocationSyncAll",             "(JJI)V",                                (void*)nAllocationSyncAll },
1622{"rsnAllocationGetSurface",          "(JJ)Landroid/view/Surface;",            (void*)nAllocationGetSurface },
1623{"rsnAllocationSetSurface",          "(JJLandroid/view/Surface;)V",           (void*)nAllocationSetSurface },
1624{"rsnAllocationIoSend",              "(JJ)V",                                 (void*)nAllocationIoSend },
1625{"rsnAllocationIoReceive",           "(JJ)V",                                 (void*)nAllocationIoReceive },
1626{"rsnAllocationData1D",              "(JJIIILjava/lang/Object;II)V",          (void*)nAllocationData1D },
1627{"rsnAllocationElementData1D",       "(JJIII[BI)V",                           (void*)nAllocationElementData1D },
1628{"rsnAllocationData2D",              "(JJIIIIIILjava/lang/Object;II)V",       (void*)nAllocationData2D },
1629{"rsnAllocationData2D",              "(JJIIIIIIJIIII)V",                      (void*)nAllocationData2D_alloc },
1630{"rsnAllocationData3D",              "(JJIIIIIIILjava/lang/Object;II)V",      (void*)nAllocationData3D },
1631{"rsnAllocationData3D",              "(JJIIIIIIIJIIII)V",                     (void*)nAllocationData3D_alloc },
1632{"rsnAllocationRead",                "(JJLjava/lang/Object;I)V",              (void*)nAllocationRead },
1633{"rsnAllocationRead1D",              "(JJIIILjava/lang/Object;II)V",          (void*)nAllocationRead1D },
1634{"rsnAllocationRead2D",              "(JJIIIIIILjava/lang/Object;II)V",       (void*)nAllocationRead2D },
1635{"rsnAllocationGetType",             "(JJ)J",                                 (void*)nAllocationGetType},
1636{"rsnAllocationResize1D",            "(JJI)V",                                (void*)nAllocationResize1D },
1637{"rsnAllocationGenerateMipmaps",     "(JJ)V",                                 (void*)nAllocationGenerateMipmaps },
1638
1639{"rsnScriptBindAllocation",          "(JJJI)V",                               (void*)nScriptBindAllocation },
1640{"rsnScriptSetTimeZone",             "(JJ[B)V",                               (void*)nScriptSetTimeZone },
1641{"rsnScriptInvoke",                  "(JJI)V",                                (void*)nScriptInvoke },
1642{"rsnScriptInvokeV",                 "(JJI[B)V",                              (void*)nScriptInvokeV },
1643{"rsnScriptForEach",                 "(JJIJJ)V",                              (void*)nScriptForEach },
1644{"rsnScriptForEach",                 "(JJIJJ[B)V",                            (void*)nScriptForEachV },
1645{"rsnScriptForEachClipped",          "(JJIJJIIIIII)V",                        (void*)nScriptForEachClipped },
1646{"rsnScriptForEachClipped",          "(JJIJJ[BIIIIII)V",                      (void*)nScriptForEachClippedV },
1647{"rsnScriptSetVarI",                 "(JJII)V",                               (void*)nScriptSetVarI },
1648{"rsnScriptGetVarI",                 "(JJI)I",                                (void*)nScriptGetVarI },
1649{"rsnScriptSetVarJ",                 "(JJIJ)V",                               (void*)nScriptSetVarJ },
1650{"rsnScriptGetVarJ",                 "(JJI)J",                                (void*)nScriptGetVarJ },
1651{"rsnScriptSetVarF",                 "(JJIF)V",                               (void*)nScriptSetVarF },
1652{"rsnScriptGetVarF",                 "(JJI)F",                                (void*)nScriptGetVarF },
1653{"rsnScriptSetVarD",                 "(JJID)V",                               (void*)nScriptSetVarD },
1654{"rsnScriptGetVarD",                 "(JJI)D",                                (void*)nScriptGetVarD },
1655{"rsnScriptSetVarV",                 "(JJI[B)V",                              (void*)nScriptSetVarV },
1656{"rsnScriptGetVarV",                 "(JJI[B)V",                              (void*)nScriptGetVarV },
1657{"rsnScriptSetVarVE",                "(JJI[BJ[I)V",                           (void*)nScriptSetVarVE },
1658{"rsnScriptSetVarObj",               "(JJIJ)V",                               (void*)nScriptSetVarObj },
1659
1660{"rsnScriptCCreate",                 "(JLjava/lang/String;Ljava/lang/String;[BI)J",  (void*)nScriptCCreate },
1661{"rsnScriptIntrinsicCreate",         "(JIJ)J",                                (void*)nScriptIntrinsicCreate },
1662{"rsnScriptKernelIDCreate",          "(JJII)J",                               (void*)nScriptKernelIDCreate },
1663{"rsnScriptFieldIDCreate",           "(JJI)J",                                (void*)nScriptFieldIDCreate },
1664{"rsnScriptGroupCreate",             "(J[J[J[J[J[J)J",                        (void*)nScriptGroupCreate },
1665{"rsnScriptGroupSetInput",           "(JJJJ)V",                               (void*)nScriptGroupSetInput },
1666{"rsnScriptGroupSetOutput",          "(JJJJ)V",                               (void*)nScriptGroupSetOutput },
1667{"rsnScriptGroupExecute",            "(JJ)V",                                 (void*)nScriptGroupExecute },
1668
1669{"rsnProgramStoreCreate",            "(JZZZZZZIII)J",                         (void*)nProgramStoreCreate },
1670
1671{"rsnProgramBindConstants",          "(JJIJ)V",                               (void*)nProgramBindConstants },
1672{"rsnProgramBindTexture",            "(JJIJ)V",                               (void*)nProgramBindTexture },
1673{"rsnProgramBindSampler",            "(JJIJ)V",                               (void*)nProgramBindSampler },
1674
1675{"rsnProgramFragmentCreate",         "(JLjava/lang/String;[Ljava/lang/String;[J)J",              (void*)nProgramFragmentCreate },
1676{"rsnProgramRasterCreate",           "(JZI)J",                                (void*)nProgramRasterCreate },
1677{"rsnProgramVertexCreate",           "(JLjava/lang/String;[Ljava/lang/String;[J)J",              (void*)nProgramVertexCreate },
1678
1679{"rsnContextBindRootScript",         "(JI)V",                                 (void*)nContextBindRootScript },
1680{"rsnContextBindProgramStore",       "(JI)V",                                 (void*)nContextBindProgramStore },
1681{"rsnContextBindProgramFragment",    "(JI)V",                                 (void*)nContextBindProgramFragment },
1682{"rsnContextBindProgramVertex",      "(JI)V",                                 (void*)nContextBindProgramVertex },
1683{"rsnContextBindProgramRaster",      "(JI)V",                                 (void*)nContextBindProgramRaster },
1684
1685{"rsnSamplerCreate",                 "(JIIIIIF)J",                            (void*)nSamplerCreate },
1686
1687{"rsnPathCreate",                    "(JIZJJF)J",                             (void*)nPathCreate },
1688{"rsnMeshCreate",                    "(J[J[J[I)J",                            (void*)nMeshCreate },
1689
1690{"rsnMeshGetVertexBufferCount",      "(JJ)I",                                 (void*)nMeshGetVertexBufferCount },
1691{"rsnMeshGetIndexCount",             "(JJ)I",                                 (void*)nMeshGetIndexCount },
1692{"rsnMeshGetVertices",               "(JJ[JI)V",                              (void*)nMeshGetVertices },
1693{"rsnMeshGetIndices",                "(JJ[J[II)V",                            (void*)nMeshGetIndices },
1694
1695};
1696
1697static int registerFuncs(JNIEnv *_env)
1698{
1699    return android::AndroidRuntime::registerNativeMethods(
1700            _env, classPathName, methods, NELEM(methods));
1701}
1702
1703// ---------------------------------------------------------------------------
1704
1705jint JNI_OnLoad(JavaVM* vm, void* reserved)
1706{
1707    JNIEnv* env = NULL;
1708    jint result = -1;
1709
1710    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
1711        ALOGE("ERROR: GetEnv failed\n");
1712        goto bail;
1713    }
1714    assert(env != NULL);
1715
1716    if (registerFuncs(env) < 0) {
1717        ALOGE("ERROR: Renderscript native registration failed\n");
1718        goto bail;
1719    }
1720
1721    /* success -- return valid version number */
1722    result = JNI_VERSION_1_4;
1723
1724bail:
1725    return result;
1726}
1727