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