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