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