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