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