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