android_renderscript_RenderScript.cpp revision eeca435dc6134a285b9bbb832cd6a1a88f34e85f
1/*
2 * Copyright (C) 2006 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 <surfaceflinger/Surface.h>
27
28#include <core/SkBitmap.h>
29#include <core/SkPixelRef.h>
30#include <core/SkStream.h>
31#include <core/SkTemplates.h>
32#include <images/SkImageDecoder.h>
33
34#include <utils/Asset.h>
35#include <utils/ResourceTypes.h>
36
37#include "jni.h"
38#include "JNIHelp.h"
39#include "android_runtime/AndroidRuntime.h"
40#include "android_runtime/android_view_Surface.h"
41
42#include <RenderScript.h>
43#include <RenderScriptEnv.h>
44
45//#define LOG_API LOGE
46#define LOG_API(...)
47
48using namespace android;
49
50// ---------------------------------------------------------------------------
51
52static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
53{
54    jclass npeClazz = env->FindClass(exc);
55    env->ThrowNew(npeClazz, msg);
56}
57
58static jfieldID gContextId = 0;
59static jfieldID gNativeBitmapID = 0;
60static jfieldID gTypeNativeCache = 0;
61
62static void _nInit(JNIEnv *_env, jclass _this)
63{
64    gContextId             = _env->GetFieldID(_this, "mContext", "I");
65
66    jclass bitmapClass = _env->FindClass("android/graphics/Bitmap");
67    gNativeBitmapID = _env->GetFieldID(bitmapClass, "mNativeBitmap", "I");
68}
69
70// ---------------------------------------------------------------------------
71
72static void
73nContextFinish(JNIEnv *_env, jobject _this, RsContext con)
74{
75    LOG_API("nContextFinish, con(%p)", con);
76    rsContextFinish(con);
77}
78
79static void
80nAssignName(JNIEnv *_env, jobject _this, RsContext con, jint obj, jbyteArray str)
81{
82    LOG_API("nAssignName, con(%p), obj(%p)", con, (void *)obj);
83    jint len = _env->GetArrayLength(str);
84    jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
85    rsAssignName(con, (void *)obj, (const char *)cptr, len);
86    _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
87}
88
89static jstring
90nGetName(JNIEnv *_env, jobject _this, RsContext con, jint obj)
91{
92    LOG_API("nGetName, con(%p), obj(%p)", con, (void *)obj);
93    const char *name = NULL;
94    rsaGetName(con, (void *)obj, &name);
95    if(name == NULL || strlen(name) == 0) {
96        return NULL;
97    }
98    return _env->NewStringUTF(name);
99}
100
101static void
102nObjDestroy(JNIEnv *_env, jobject _this, RsContext con, jint obj)
103{
104    LOG_API("nObjDestroy, con(%p) obj(%p)", con, (void *)obj);
105    rsObjDestroy(con, (void *)obj);
106}
107
108// ---------------------------------------------------------------------------
109
110static jint
111nDeviceCreate(JNIEnv *_env, jobject _this)
112{
113    LOG_API("nDeviceCreate");
114    return (jint)rsDeviceCreate();
115}
116
117static void
118nDeviceDestroy(JNIEnv *_env, jobject _this, jint dev)
119{
120    LOG_API("nDeviceDestroy");
121    return rsDeviceDestroy((RsDevice)dev);
122}
123
124static void
125nDeviceSetConfig(JNIEnv *_env, jobject _this, jint dev, jint p, jint value)
126{
127    LOG_API("nDeviceSetConfig  dev(%p), param(%i), value(%i)", (void *)dev, p, value);
128    return rsDeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value);
129}
130
131static jint
132nContextCreate(JNIEnv *_env, jobject _this, jint dev, jint ver)
133{
134    LOG_API("nContextCreate");
135    return (jint)rsContextCreate((RsDevice)dev, ver);
136}
137
138static jint
139nContextCreateGL(JNIEnv *_env, jobject _this, jint dev, jint ver,
140                 int colorMin, int colorPref,
141                 int alphaMin, int alphaPref,
142                 int depthMin, int depthPref,
143                 int stencilMin, int stencilPref,
144                 int samplesMin, int samplesPref, float samplesQ)
145{
146    RsSurfaceConfig sc;
147    sc.alphaMin = alphaMin;
148    sc.alphaPref = alphaPref;
149    sc.colorMin = colorMin;
150    sc.colorPref = colorPref;
151    sc.depthMin = depthMin;
152    sc.depthPref = depthPref;
153    sc.samplesMin = samplesMin;
154    sc.samplesPref = samplesPref;
155    sc.samplesQ = samplesQ;
156
157    LOG_API("nContextCreateGL");
158    return (jint)rsContextCreateGL((RsDevice)dev, ver, sc);
159}
160
161static void
162nContextSetPriority(JNIEnv *_env, jobject _this, RsContext con, jint p)
163{
164    LOG_API("ContextSetPriority, con(%p), priority(%i)", con, p);
165    rsContextSetPriority(con, p);
166}
167
168
169
170static void
171nContextSetSurface(JNIEnv *_env, jobject _this, RsContext con, jint width, jint height, jobject wnd)
172{
173    LOG_API("nContextSetSurface, con(%p), width(%i), height(%i), surface(%p)", con, width, height, (Surface *)wnd);
174
175    Surface * window = NULL;
176    if (wnd == NULL) {
177
178    } else {
179        window = (Surface*) android_Surface_getNativeWindow(_env, wnd).get();
180    }
181
182    rsContextSetSurface(con, width, height, window);
183}
184
185static void
186nContextDestroy(JNIEnv *_env, jobject _this, RsContext con)
187{
188    LOG_API("nContextDestroy, con(%p)", con);
189    rsContextDestroy(con);
190}
191
192static void
193nContextDump(JNIEnv *_env, jobject _this, RsContext con, jint bits)
194{
195    LOG_API("nContextDump, con(%p)  bits(%i)", (RsContext)con, bits);
196    rsContextDump((RsContext)con, bits);
197}
198
199static void
200nContextPause(JNIEnv *_env, jobject _this, RsContext con)
201{
202    LOG_API("nContextPause, con(%p)", con);
203    rsContextPause(con);
204}
205
206static void
207nContextResume(JNIEnv *_env, jobject _this, RsContext con)
208{
209    LOG_API("nContextResume, con(%p)", con);
210    rsContextResume(con);
211}
212
213
214static jstring
215nContextGetErrorMessage(JNIEnv *_env, jobject _this, RsContext con)
216{
217    LOG_API("nContextGetErrorMessage, con(%p)", con);
218    char buf[1024];
219
220    size_t receiveLen;
221    uint32_t subID;
222    int id = rsContextGetMessage(con, buf, &receiveLen, &subID, sizeof(buf), true);
223    if (!id && receiveLen) {
224        LOGV("message receive buffer too small.  %i", receiveLen);
225    }
226    return _env->NewStringUTF(buf);
227}
228
229static void
230nContextGetUserMessage(JNIEnv *_env, jobject _this, RsContext con, jintArray data)
231{
232    jint len = _env->GetArrayLength(data);
233    LOG_API("nContextGetMessage, con(%p), len(%i)", con, len);
234    jint *ptr = _env->GetIntArrayElements(data, NULL);
235    size_t receiveLen;
236    uint32_t subID;
237    int id = rsContextGetMessage(con, ptr, &receiveLen, &subID, len * 4, true);
238    if (!id && receiveLen) {
239        LOGV("message receive buffer too small.  %i", receiveLen);
240    }
241    _env->ReleaseIntArrayElements(data, ptr, 0);
242}
243
244static jint
245nContextPeekMessage(JNIEnv *_env, jobject _this, RsContext con, jintArray auxData, jboolean wait)
246{
247    LOG_API("nContextPeekMessage, con(%p)", con);
248    jint *auxDataPtr = _env->GetIntArrayElements(auxData, NULL);
249    size_t receiveLen;
250    uint32_t subID;
251    int id = rsContextPeekMessage(con, &receiveLen, &subID, wait);
252    auxDataPtr[0] = (jint)subID;
253    auxDataPtr[1] = (jint)receiveLen;
254    _env->ReleaseIntArrayElements(auxData, auxDataPtr, 0);
255    return id;
256}
257
258static void nContextInitToClient(JNIEnv *_env, jobject _this, RsContext con)
259{
260    LOG_API("nContextInitToClient, con(%p)", con);
261    rsContextInitToClient(con);
262}
263
264static void nContextDeinitToClient(JNIEnv *_env, jobject _this, RsContext con)
265{
266    LOG_API("nContextDeinitToClient, con(%p)", con);
267    rsContextDeinitToClient(con);
268}
269
270
271static jint
272nElementCreate(JNIEnv *_env, jobject _this, RsContext con, jint type, jint kind, jboolean norm, jint size)
273{
274    LOG_API("nElementCreate, con(%p), type(%i), kind(%i), norm(%i), size(%i)", con, type, kind, norm, size);
275    return (jint)rsElementCreate(con, (RsDataType)type, (RsDataKind)kind, norm, size);
276}
277
278static jint
279nElementCreate2(JNIEnv *_env, jobject _this, RsContext con, jintArray _ids, jobjectArray _names, jintArray _arraySizes)
280{
281    int fieldCount = _env->GetArrayLength(_ids);
282    LOG_API("nElementCreate2, con(%p)", con);
283
284    jint *ids = _env->GetIntArrayElements(_ids, NULL);
285    jint *arraySizes = _env->GetIntArrayElements(_arraySizes, NULL);
286    const char ** nameArray = (const char **)calloc(fieldCount, sizeof(char *));
287    size_t* sizeArray = (size_t*)calloc(fieldCount, sizeof(size_t));
288
289    for (int ct=0; ct < fieldCount; ct++) {
290        jstring s = (jstring)_env->GetObjectArrayElement(_names, ct);
291        nameArray[ct] = _env->GetStringUTFChars(s, NULL);
292        sizeArray[ct] = _env->GetStringUTFLength(s);
293    }
294    jint id = (jint)rsElementCreate2(con, fieldCount, (RsElement *)ids, nameArray, sizeArray, (const uint32_t *)arraySizes);
295    for (int ct=0; ct < fieldCount; ct++) {
296        jstring s = (jstring)_env->GetObjectArrayElement(_names, ct);
297        _env->ReleaseStringUTFChars(s, nameArray[ct]);
298    }
299    _env->ReleaseIntArrayElements(_ids, ids, JNI_ABORT);
300    _env->ReleaseIntArrayElements(_arraySizes, arraySizes, JNI_ABORT);
301    free(nameArray);
302    free(sizeArray);
303    return (jint)id;
304}
305
306static void
307nElementGetNativeData(JNIEnv *_env, jobject _this, RsContext con, jint id, jintArray _elementData)
308{
309    int dataSize = _env->GetArrayLength(_elementData);
310    LOG_API("nElementGetNativeData, con(%p)", con);
311
312    // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
313    assert(dataSize == 5);
314
315    uint32_t elementData[5];
316    rsaElementGetNativeData(con, (RsElement)id, elementData, dataSize);
317
318    for(jint i = 0; i < dataSize; i ++) {
319        _env->SetIntArrayRegion(_elementData, i, 1, (const jint*)&elementData[i]);
320    }
321}
322
323
324static void
325nElementGetSubElements(JNIEnv *_env, jobject _this, RsContext con, jint id, jintArray _IDs, jobjectArray _names)
326{
327    int dataSize = _env->GetArrayLength(_IDs);
328    LOG_API("nElementGetSubElements, con(%p)", con);
329
330    uint32_t *ids = (uint32_t *)malloc((uint32_t)dataSize * sizeof(uint32_t));
331    const char **names = (const char **)malloc((uint32_t)dataSize * sizeof(const char *));
332
333    rsaElementGetSubElements(con, (RsElement)id, ids, names, (uint32_t)dataSize);
334
335    for(jint i = 0; i < dataSize; i++) {
336        _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
337        _env->SetIntArrayRegion(_IDs, i, 1, (const jint*)&ids[i]);
338    }
339
340    free(ids);
341    free(names);
342}
343
344// -----------------------------------
345
346static int
347nTypeCreate(JNIEnv *_env, jobject _this, RsContext con, RsElement eid,
348            jint dimx, jint dimy, jint dimz, jboolean mips, jboolean faces)
349{
350    LOG_API("nTypeCreate, con(%p) eid(%p), x(%i), y(%i), z(%i), mips(%i), faces(%i)",
351            con, eid, dimx, dimy, dimz, mips, faces);
352
353    jint id = (jint)rsaTypeCreate(con, (RsElement)eid, dimx, dimy, dimz, mips, faces);
354    return (jint)id;
355}
356
357static void
358nTypeGetNativeData(JNIEnv *_env, jobject _this, RsContext con, jint id, jintArray _typeData)
359{
360    // We are packing 6 items: mDimX; mDimY; mDimZ;
361    // mDimLOD; mDimFaces; mElement; into typeData
362    int elementCount = _env->GetArrayLength(_typeData);
363
364    assert(elementCount == 6);
365    LOG_API("nTypeCreate, con(%p)", con);
366
367    uint32_t typeData[6];
368    rsaTypeGetNativeData(con, (RsType)id, typeData, 6);
369
370    for(jint i = 0; i < elementCount; i ++) {
371        _env->SetIntArrayRegion(_typeData, i, 1, (const jint*)&typeData[i]);
372    }
373}
374
375// -----------------------------------
376
377static jint
378nAllocationCreateTyped(JNIEnv *_env, jobject _this, RsContext con, jint type, jint mips, jint usage)
379{
380    LOG_API("nAllocationCreateTyped, con(%p), type(%p), mip(%i), usage(%i)", con, (RsElement)type, mips, usage);
381    return (jint) rsaAllocationCreateTyped(con, (RsType)type, (RsAllocationMipmapControl)mips, (uint32_t)usage);
382}
383
384static void
385nAllocationUploadToTexture(JNIEnv *_env, jobject _this, RsContext con, jint a, jboolean genMip, jint mip)
386{
387    LOG_API("nAllocationUploadToTexture, con(%p), a(%p), genMip(%i), mip(%i)", con, (RsAllocation)a, genMip, mip);
388    rsAllocationUploadToTexture(con, (RsAllocation)a, genMip, mip);
389}
390
391static void
392nAllocationUploadToBufferObject(JNIEnv *_env, jobject _this, RsContext con, jint a)
393{
394    LOG_API("nAllocationUploadToBufferObject, con(%p), a(%p)", con, (RsAllocation)a);
395    rsAllocationUploadToBufferObject(con, (RsAllocation)a);
396}
397
398static void
399nAllocationSyncAll(JNIEnv *_env, jobject _this, RsContext con, jint a, jint bits)
400{
401    LOG_API("nAllocationSyncAll, con(%p), a(%p), bits(0x%08x)", con, (RsAllocation)a, bits);
402    rsAllocationSyncAll(con, (RsAllocation)a, (RsAllocationUsageType)bits);
403}
404
405static int
406nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, RsContext con, jint type, jint mip, jobject jbitmap, jint usage)
407{
408    SkBitmap const * nativeBitmap =
409            (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID);
410    const SkBitmap& bitmap(*nativeBitmap);
411
412    bitmap.lockPixels();
413    const void* ptr = bitmap.getPixels();
414    jint id = (jint)rsaAllocationCreateFromBitmap(con, (RsType)type, (RsAllocationMipmapControl)mip, ptr, usage);
415    bitmap.unlockPixels();
416    return id;
417}
418
419static int
420nAllocationCubeCreateFromBitmap(JNIEnv *_env, jobject _this, RsContext con, jint type, jint mip, jobject jbitmap, jint usage)
421{
422    SkBitmap const * nativeBitmap =
423            (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID);
424    const SkBitmap& bitmap(*nativeBitmap);
425
426    bitmap.lockPixels();
427    const void* ptr = bitmap.getPixels();
428    jint id = (jint)rsaAllocationCubeCreateFromBitmap(con, (RsType)type, (RsAllocationMipmapControl)mip, ptr, usage);
429    bitmap.unlockPixels();
430    return id;
431}
432
433static void
434nAllocationCopyFromBitmap(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jobject jbitmap)
435{
436    SkBitmap const * nativeBitmap =
437            (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID);
438    const SkBitmap& bitmap(*nativeBitmap);
439
440    bitmap.lockPixels();
441    const void* ptr = bitmap.getPixels();
442    rsAllocationCopyFromBitmap(con, (RsAllocation)alloc, ptr, bitmap.getSize());
443    bitmap.unlockPixels();
444}
445
446static void
447nAllocationCopyToBitmap(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jobject jbitmap)
448{
449    SkBitmap const * nativeBitmap =
450            (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID);
451    const SkBitmap& bitmap(*nativeBitmap);
452
453    bitmap.lockPixels();
454    void* ptr = bitmap.getPixels();
455    rsAllocationCopyToBitmap(con, (RsAllocation)alloc, ptr, bitmap.getSize());
456    bitmap.unlockPixels();
457}
458
459static void ReleaseBitmapCallback(void *bmp)
460{
461    SkBitmap const * nativeBitmap = (SkBitmap const *)bmp;
462    nativeBitmap->unlockPixels();
463}
464
465
466static void
467nAllocationSubData1D_i(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint count, jintArray data, int sizeBytes)
468{
469    jint len = _env->GetArrayLength(data);
470    LOG_API("nAllocation1DSubData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
471    jint *ptr = _env->GetIntArrayElements(data, NULL);
472    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
473    _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
474}
475
476static void
477nAllocationSubData1D_s(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint count, jshortArray data, int sizeBytes)
478{
479    jint len = _env->GetArrayLength(data);
480    LOG_API("nAllocation1DSubData_s, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
481    jshort *ptr = _env->GetShortArrayElements(data, NULL);
482    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
483    _env->ReleaseShortArrayElements(data, ptr, JNI_ABORT);
484}
485
486static void
487nAllocationSubData1D_b(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint count, jbyteArray data, int sizeBytes)
488{
489    jint len = _env->GetArrayLength(data);
490    LOG_API("nAllocation1DSubData_b, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
491    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
492    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
493    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
494}
495
496static void
497nAllocationSubData1D_f(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint count, jfloatArray data, int sizeBytes)
498{
499    jint len = _env->GetArrayLength(data);
500    LOG_API("nAllocation1DSubData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
501    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
502    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
503    _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
504}
505
506static void
507//    native void rsnAllocationSubElementData1D(int con, int id, int xoff, int compIdx, byte[] d, int sizeBytes);
508nAllocationSubElementData1D(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint compIdx, jbyteArray data, int sizeBytes)
509{
510    jint len = _env->GetArrayLength(data);
511    LOG_API("nAllocationSubElementData1D, con(%p), alloc(%p), offset(%i), comp(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, compIdx, len, sizeBytes);
512    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
513    rsAllocation1DSubElementData(con, (RsAllocation)alloc, offset, ptr, compIdx, sizeBytes);
514    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
515}
516
517static void
518nAllocationSubData2D_i(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint w, jint h, jintArray data, int sizeBytes)
519{
520    jint len = _env->GetArrayLength(data);
521    LOG_API("nAllocation2DSubData_i, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)", con, (RsAllocation)alloc, xoff, yoff, w, h, len);
522    jint *ptr = _env->GetIntArrayElements(data, NULL);
523    rsAllocation2DSubData(con, (RsAllocation)alloc, xoff, yoff, w, h, ptr, sizeBytes);
524    _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
525}
526
527static void
528nAllocationSubData2D_f(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint w, jint h, jfloatArray data, int sizeBytes)
529{
530    jint len = _env->GetArrayLength(data);
531    LOG_API("nAllocation2DSubData_i, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)", con, (RsAllocation)alloc, xoff, yoff, w, h, len);
532    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
533    rsAllocation2DSubData(con, (RsAllocation)alloc, xoff, yoff, w, h, ptr, sizeBytes);
534    _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
535}
536
537static void
538nAllocationRead_i(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jintArray data)
539{
540    jint len = _env->GetArrayLength(data);
541    LOG_API("nAllocationRead_i, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
542    jint *ptr = _env->GetIntArrayElements(data, NULL);
543    rsAllocationRead(con, (RsAllocation)alloc, ptr);
544    _env->ReleaseIntArrayElements(data, ptr, 0);
545}
546
547static void
548nAllocationRead_f(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jfloatArray data)
549{
550    jint len = _env->GetArrayLength(data);
551    LOG_API("nAllocationRead_f, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
552    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
553    rsAllocationRead(con, (RsAllocation)alloc, ptr);
554    _env->ReleaseFloatArrayElements(data, ptr, 0);
555}
556
557static jint
558nAllocationGetType(JNIEnv *_env, jobject _this, RsContext con, jint a)
559{
560    LOG_API("nAllocationGetType, con(%p), a(%p)", con, (RsAllocation)a);
561    return (jint) rsaAllocationGetType(con, (RsAllocation)a);
562}
563
564static void
565nAllocationResize1D(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint dimX)
566{
567    LOG_API("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", con, (RsAllocation)alloc, dimX);
568    rsAllocationResize1D(con, (RsAllocation)alloc, dimX);
569}
570
571static void
572nAllocationResize2D(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint dimX, jint dimY)
573{
574    LOG_API("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i), sizeY(%i)", con, (RsAllocation)alloc, dimX, dimY);
575    rsAllocationResize2D(con, (RsAllocation)alloc, dimX, dimY);
576}
577
578// -----------------------------------
579
580static int
581nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, RsContext con, jint native_asset)
582{
583    LOGV("______nFileA3D %u", (uint32_t) native_asset);
584
585    Asset* asset = reinterpret_cast<Asset*>(native_asset);
586
587    jint id = (jint)rsaFileA3DCreateFromAssetStream(con, asset->getBuffer(false), asset->getLength());
588    return id;
589}
590
591static int
592nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, RsContext con, jint fileA3D)
593{
594    int32_t numEntries = 0;
595    rsaFileA3DGetNumIndexEntries(con, &numEntries, (RsFile)fileA3D);
596    return numEntries;
597}
598
599static void
600nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, RsContext con, jint fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
601{
602    LOGV("______nFileA3D %u", (uint32_t) fileA3D);
603    RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
604
605    rsaFileA3DGetIndexEntries(con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
606
607    for(jint i = 0; i < numEntries; i ++) {
608        _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
609        _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
610    }
611
612    free(fileEntries);
613}
614
615static int
616nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, RsContext con, jint fileA3D, jint index)
617{
618    LOGV("______nFileA3D %u", (uint32_t) fileA3D);
619    jint id = (jint)rsaFileA3DGetEntryByIndex(con, (uint32_t)index, (RsFile)fileA3D);
620    return id;
621}
622
623// -----------------------------------
624
625static int
626nFontCreateFromFile(JNIEnv *_env, jobject _this, RsContext con, jstring fileName, jfloat fontSize, jint dpi)
627{
628    const char* fileNameUTF = _env->GetStringUTFChars(fileName, NULL);
629
630    jint id = (jint)rsFontCreateFromFile(con, fileNameUTF, fontSize, dpi);
631    return id;
632}
633
634
635// -----------------------------------
636
637static void
638nAdapter1DBindAllocation(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint alloc)
639{
640    LOG_API("nAdapter1DBindAllocation, con(%p), adapter(%p), alloc(%p)", con, (RsAdapter1D)adapter, (RsAllocation)alloc);
641    rsAdapter1DBindAllocation(con, (RsAdapter1D)adapter, (RsAllocation)alloc);
642}
643
644static void
645nAdapter1DSetConstraint(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint dim, jint value)
646{
647    LOG_API("nAdapter1DSetConstraint, con(%p), adapter(%p), dim(%i), value(%i)", con, (RsAdapter1D)adapter, dim, value);
648    rsAdapter1DSetConstraint(con, (RsAdapter1D)adapter, (RsDimension)dim, value);
649}
650
651static void
652nAdapter1DData_i(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jintArray data)
653{
654    jint len = _env->GetArrayLength(data);
655    LOG_API("nAdapter1DData_i, con(%p), adapter(%p), len(%i)", con, (RsAdapter1D)adapter, len);
656    jint *ptr = _env->GetIntArrayElements(data, NULL);
657    rsAdapter1DData(con, (RsAdapter1D)adapter, ptr);
658    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
659}
660
661static void
662nAdapter1DSubData_i(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint offset, jint count, jintArray data)
663{
664    jint len = _env->GetArrayLength(data);
665    LOG_API("nAdapter1DSubData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAdapter1D)adapter, offset, count, len);
666    jint *ptr = _env->GetIntArrayElements(data, NULL);
667    rsAdapter1DSubData(con, (RsAdapter1D)adapter, offset, count, ptr);
668    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
669}
670
671static void
672nAdapter1DData_f(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jfloatArray data)
673{
674    jint len = _env->GetArrayLength(data);
675    LOG_API("nAdapter1DData_f, con(%p), adapter(%p), len(%i)", con, (RsAdapter1D)adapter, len);
676    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
677    rsAdapter1DData(con, (RsAdapter1D)adapter, ptr);
678    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
679}
680
681static void
682nAdapter1DSubData_f(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint offset, jint count, jfloatArray data)
683{
684    jint len = _env->GetArrayLength(data);
685    LOG_API("nAdapter1DSubData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAdapter1D)adapter, offset, count, len);
686    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
687    rsAdapter1DSubData(con, (RsAdapter1D)adapter, offset, count, ptr);
688    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
689}
690
691static jint
692nAdapter1DCreate(JNIEnv *_env, jobject _this, RsContext con)
693{
694    LOG_API("nAdapter1DCreate, con(%p)", con);
695    return (jint)rsAdapter1DCreate(con);
696}
697
698// -----------------------------------
699
700static void
701nAdapter2DBindAllocation(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint alloc)
702{
703    LOG_API("nAdapter2DBindAllocation, con(%p), adapter(%p), alloc(%p)", con, (RsAdapter2D)adapter, (RsAllocation)alloc);
704    rsAdapter2DBindAllocation(con, (RsAdapter2D)adapter, (RsAllocation)alloc);
705}
706
707static void
708nAdapter2DSetConstraint(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint dim, jint value)
709{
710    LOG_API("nAdapter2DSetConstraint, con(%p), adapter(%p), dim(%i), value(%i)", con, (RsAdapter2D)adapter, dim, value);
711    rsAdapter2DSetConstraint(con, (RsAdapter2D)adapter, (RsDimension)dim, value);
712}
713
714static void
715nAdapter2DData_i(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jintArray data)
716{
717    jint len = _env->GetArrayLength(data);
718    LOG_API("nAdapter2DData_i, con(%p), adapter(%p), len(%i)", con, (RsAdapter2D)adapter, len);
719    jint *ptr = _env->GetIntArrayElements(data, NULL);
720    rsAdapter2DData(con, (RsAdapter2D)adapter, ptr);
721    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
722}
723
724static void
725nAdapter2DData_f(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jfloatArray data)
726{
727    jint len = _env->GetArrayLength(data);
728    LOG_API("nAdapter2DData_f, con(%p), adapter(%p), len(%i)", con, (RsAdapter2D)adapter, len);
729    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
730    rsAdapter2DData(con, (RsAdapter2D)adapter, ptr);
731    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
732}
733
734static void
735nAdapter2DSubData_i(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint xoff, jint yoff, jint w, jint h, jintArray data)
736{
737    jint len = _env->GetArrayLength(data);
738    LOG_API("nAdapter2DSubData_i, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)",
739            con, (RsAdapter2D)adapter, xoff, yoff, w, h, len);
740    jint *ptr = _env->GetIntArrayElements(data, NULL);
741    rsAdapter2DSubData(con, (RsAdapter2D)adapter, xoff, yoff, w, h, ptr);
742    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
743}
744
745static void
746nAdapter2DSubData_f(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint xoff, jint yoff, jint w, jint h, jfloatArray data)
747{
748    jint len = _env->GetArrayLength(data);
749    LOG_API("nAdapter2DSubData_f, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)",
750            con, (RsAdapter2D)adapter, xoff, yoff, w, h, len);
751    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
752    rsAdapter2DSubData(con, (RsAdapter1D)adapter, xoff, yoff, w, h, ptr);
753    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
754}
755
756static jint
757nAdapter2DCreate(JNIEnv *_env, jobject _this, RsContext con)
758{
759    LOG_API("nAdapter2DCreate, con(%p)", con);
760    return (jint)rsAdapter2DCreate(con);
761}
762
763// -----------------------------------
764
765static void
766nScriptBindAllocation(JNIEnv *_env, jobject _this, RsContext con, jint script, jint alloc, jint slot)
767{
768    LOG_API("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", con, (RsScript)script, (RsAllocation)alloc, slot);
769    rsScriptBindAllocation(con, (RsScript)script, (RsAllocation)alloc, slot);
770}
771
772static void
773nScriptSetVarI(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jint val)
774{
775    LOG_API("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", con, (void *)script, slot, val);
776    rsScriptSetVarI(con, (RsScript)script, slot, val);
777}
778
779static void
780nScriptSetVarObj(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jint val)
781{
782    LOG_API("nScriptSetVarObj, con(%p), s(%p), slot(%i), val(%i)", con, (void *)script, slot, val);
783    rsScriptSetVarObj(con, (RsScript)script, slot, (RsObjectBase)val);
784}
785
786static void
787nScriptSetVarJ(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jlong val)
788{
789    LOG_API("nScriptSetVarJ, con(%p), s(%p), slot(%i), val(%lli)", con, (void *)script, slot, val);
790    rsScriptSetVarJ(con, (RsScript)script, slot, val);
791}
792
793static void
794nScriptSetVarF(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, float val)
795{
796    LOG_API("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", con, (void *)script, slot, val);
797    rsScriptSetVarF(con, (RsScript)script, slot, val);
798}
799
800static void
801nScriptSetVarD(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, double val)
802{
803    LOG_API("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", con, (void *)script, slot, val);
804    rsScriptSetVarD(con, (RsScript)script, slot, val);
805}
806
807static void
808nScriptSetVarV(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jbyteArray data)
809{
810    LOG_API("nScriptSetVarV, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
811    jint len = _env->GetArrayLength(data);
812    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
813    rsScriptSetVarV(con, (RsScript)script, slot, ptr, len);
814    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
815}
816
817
818static void
819nScriptSetTimeZone(JNIEnv *_env, jobject _this, RsContext con, jint script, jbyteArray timeZone)
820{
821    LOG_API("nScriptCSetTimeZone, con(%p), s(%p), timeZone(%s)", con, (void *)script, (const char *)timeZone);
822
823    jint length = _env->GetArrayLength(timeZone);
824    jbyte* timeZone_ptr;
825    timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
826
827    rsScriptSetTimeZone(con, (RsScript)script, (const char *)timeZone_ptr, length);
828
829    if (timeZone_ptr) {
830        _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
831    }
832}
833
834static void
835nScriptInvoke(JNIEnv *_env, jobject _this, RsContext con, jint obj, jint slot)
836{
837    LOG_API("nScriptInvoke, con(%p), script(%p)", con, (void *)obj);
838    rsScriptInvoke(con, (RsScript)obj, slot);
839}
840
841static void
842nScriptInvokeV(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jbyteArray data)
843{
844    LOG_API("nScriptInvokeV, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
845    jint len = _env->GetArrayLength(data);
846    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
847    rsScriptInvokeV(con, (RsScript)script, slot, ptr, len);
848    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
849}
850
851
852// -----------------------------------
853
854static void
855nScriptCBegin(JNIEnv *_env, jobject _this, RsContext con)
856{
857    LOG_API("nScriptCBegin, con(%p)", con);
858    rsScriptCBegin(con);
859}
860
861static void
862nScriptCSetScript(JNIEnv *_env, jobject _this, RsContext con, jbyteArray scriptRef,
863                  jint offset, jint length)
864{
865    LOG_API("!!! nScriptCSetScript, con(%p)", con);
866    jint _exception = 0;
867    jint remaining;
868    jbyte* script_base = 0;
869    jbyte* script_ptr;
870    if (!scriptRef) {
871        _exception = 1;
872        //_env->ThrowNew(IAEClass, "script == null");
873        goto exit;
874    }
875    if (offset < 0) {
876        _exception = 1;
877        //_env->ThrowNew(IAEClass, "offset < 0");
878        goto exit;
879    }
880    if (length < 0) {
881        _exception = 1;
882        //_env->ThrowNew(IAEClass, "length < 0");
883        goto exit;
884    }
885    remaining = _env->GetArrayLength(scriptRef) - offset;
886    if (remaining < length) {
887        _exception = 1;
888        //_env->ThrowNew(IAEClass, "length > script.length - offset");
889        goto exit;
890    }
891    script_base = (jbyte *)
892        _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
893    script_ptr = script_base + offset;
894
895    rsScriptCSetText(con, (const char *)script_ptr, length);
896
897exit:
898    if (script_base) {
899        _env->ReleasePrimitiveArrayCritical(scriptRef, script_base,
900                _exception ? JNI_ABORT: 0);
901    }
902}
903
904static jint
905nScriptCCreate(JNIEnv *_env, jobject _this, RsContext con, jstring packageName, jstring resName, jstring cacheDir)
906{
907    LOG_API("nScriptCCreate, con(%p)", con);
908    const char* packageNameUTF = _env->GetStringUTFChars(packageName, NULL);
909    const char* resNameUTF = _env->GetStringUTFChars(resName, NULL);
910    const char* cacheDirUTF = _env->GetStringUTFChars(cacheDir, NULL);
911    jint i = (jint)rsScriptCCreate(con, packageNameUTF, resNameUTF, cacheDirUTF);
912    _env->ReleaseStringUTFChars(packageName, packageNameUTF);
913    _env->ReleaseStringUTFChars(resName, resNameUTF);
914    _env->ReleaseStringUTFChars(cacheDir, cacheDirUTF);
915    return i;
916}
917
918// ---------------------------------------------------------------------------
919
920static void
921nProgramStoreBegin(JNIEnv *_env, jobject _this, RsContext con, jint in, jint out)
922{
923    LOG_API("nProgramStoreBegin, con(%p), in(%p), out(%p)", con, (RsElement)in, (RsElement)out);
924    rsProgramStoreBegin(con, (RsElement)in, (RsElement)out);
925}
926
927static void
928nProgramStoreDepthFunc(JNIEnv *_env, jobject _this, RsContext con, jint func)
929{
930    LOG_API("nProgramStoreDepthFunc, con(%p), func(%i)", con, func);
931    rsProgramStoreDepthFunc(con, (RsDepthFunc)func);
932}
933
934static void
935nProgramStoreDepthMask(JNIEnv *_env, jobject _this, RsContext con, jboolean enable)
936{
937    LOG_API("nProgramStoreDepthMask, con(%p), enable(%i)", con, enable);
938    rsProgramStoreDepthMask(con, enable);
939}
940
941static void
942nProgramStoreColorMask(JNIEnv *_env, jobject _this, RsContext con, jboolean r, jboolean g, jboolean b, jboolean a)
943{
944    LOG_API("nProgramStoreColorMask, con(%p), r(%i), g(%i), b(%i), a(%i)", con, r, g, b, a);
945    rsProgramStoreColorMask(con, r, g, b, a);
946}
947
948static void
949nProgramStoreBlendFunc(JNIEnv *_env, jobject _this, RsContext con, int src, int dst)
950{
951    LOG_API("nProgramStoreBlendFunc, con(%p), src(%i), dst(%i)", con, src, dst);
952    rsProgramStoreBlendFunc(con, (RsBlendSrcFunc)src, (RsBlendDstFunc)dst);
953}
954
955static void
956nProgramStoreDither(JNIEnv *_env, jobject _this, RsContext con, jboolean enable)
957{
958    LOG_API("nProgramStoreDither, con(%p), enable(%i)", con, enable);
959    rsProgramStoreDither(con, enable);
960}
961
962static jint
963nProgramStoreCreate(JNIEnv *_env, jobject _this, RsContext con)
964{
965    LOG_API("nProgramStoreCreate, con(%p)", con);
966    return (jint)rsProgramStoreCreate(con);
967}
968
969// ---------------------------------------------------------------------------
970
971static void
972nProgramBindConstants(JNIEnv *_env, jobject _this, RsContext con, jint vpv, jint slot, jint a)
973{
974    LOG_API("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", con, (RsProgramVertex)vpv, slot, (RsAllocation)a);
975    rsProgramBindConstants(con, (RsProgram)vpv, slot, (RsAllocation)a);
976}
977
978static void
979nProgramBindTexture(JNIEnv *_env, jobject _this, RsContext con, jint vpf, jint slot, jint a)
980{
981    LOG_API("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
982    rsProgramBindTexture(con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
983}
984
985static void
986nProgramBindSampler(JNIEnv *_env, jobject _this, RsContext con, jint vpf, jint slot, jint a)
987{
988    LOG_API("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsSampler)a);
989    rsProgramBindSampler(con, (RsProgramFragment)vpf, slot, (RsSampler)a);
990}
991
992// ---------------------------------------------------------------------------
993
994static jint
995nProgramFragmentCreate(JNIEnv *_env, jobject _this, RsContext con, jstring shader, jintArray params)
996{
997    const char* shaderUTF = _env->GetStringUTFChars(shader, NULL);
998    jint shaderLen = _env->GetStringUTFLength(shader);
999    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1000    jint paramLen = _env->GetArrayLength(params);
1001
1002    LOG_API("nProgramFragmentCreate, con(%p), shaderLen(%i), paramLen(%i)", con, shaderLen, paramLen);
1003
1004    jint ret = (jint)rsProgramFragmentCreate(con, shaderUTF, shaderLen, (uint32_t *)paramPtr, paramLen);
1005    _env->ReleaseStringUTFChars(shader, shaderUTF);
1006    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1007    return ret;
1008}
1009
1010
1011// ---------------------------------------------------------------------------
1012
1013static jint
1014nProgramVertexCreate(JNIEnv *_env, jobject _this, RsContext con, jstring shader, jintArray params)
1015{
1016    const char* shaderUTF = _env->GetStringUTFChars(shader, NULL);
1017    jint shaderLen = _env->GetStringUTFLength(shader);
1018    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1019    jint paramLen = _env->GetArrayLength(params);
1020
1021    LOG_API("nProgramVertexCreate, con(%p), shaderLen(%i), paramLen(%i)", con, shaderLen, paramLen);
1022
1023    jint ret = (jint)rsProgramVertexCreate(con, shaderUTF, shaderLen, (uint32_t *)paramPtr, paramLen);
1024    _env->ReleaseStringUTFChars(shader, shaderUTF);
1025    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1026    return ret;
1027}
1028
1029// ---------------------------------------------------------------------------
1030
1031static jint
1032nProgramRasterCreate(JNIEnv *_env, jobject _this, RsContext con, jboolean pointSmooth, jboolean lineSmooth, jboolean pointSprite)
1033{
1034    LOG_API("nProgramRasterCreate, con(%p), pointSmooth(%i), lineSmooth(%i), pointSprite(%i)",
1035            con, pointSmooth, lineSmooth, pointSprite);
1036    return (jint)rsProgramRasterCreate(con, pointSmooth, lineSmooth, pointSprite);
1037}
1038
1039static void
1040nProgramRasterSetLineWidth(JNIEnv *_env, jobject _this, RsContext con, jint vpr, jfloat v)
1041{
1042    LOG_API("nProgramRasterSetLineWidth, con(%p), vpf(%p), value(%f)", con, (RsProgramRaster)vpr, v);
1043    rsProgramRasterSetLineWidth(con, (RsProgramRaster)vpr, v);
1044}
1045
1046static void
1047nProgramRasterSetCullMode(JNIEnv *_env, jobject _this, RsContext con, jint vpr, jint v)
1048{
1049    LOG_API("nProgramRasterSetCullMode, con(%p), vpf(%p), value(%i)", con, (RsProgramRaster)vpr, v);
1050    rsProgramRasterSetCullMode(con, (RsProgramRaster)vpr, (RsCullMode)v);
1051}
1052
1053
1054// ---------------------------------------------------------------------------
1055
1056static void
1057nContextBindRootScript(JNIEnv *_env, jobject _this, RsContext con, jint script)
1058{
1059    LOG_API("nContextBindRootScript, con(%p), script(%p)", con, (RsScript)script);
1060    rsContextBindRootScript(con, (RsScript)script);
1061}
1062
1063static void
1064nContextBindProgramStore(JNIEnv *_env, jobject _this, RsContext con, jint pfs)
1065{
1066    LOG_API("nContextBindProgramStore, con(%p), pfs(%p)", con, (RsProgramStore)pfs);
1067    rsContextBindProgramStore(con, (RsProgramStore)pfs);
1068}
1069
1070static void
1071nContextBindProgramFragment(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1072{
1073    LOG_API("nContextBindProgramFragment, con(%p), pf(%p)", con, (RsProgramFragment)pf);
1074    rsContextBindProgramFragment(con, (RsProgramFragment)pf);
1075}
1076
1077static void
1078nContextBindProgramVertex(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1079{
1080    LOG_API("nContextBindProgramVertex, con(%p), pf(%p)", con, (RsProgramVertex)pf);
1081    rsContextBindProgramVertex(con, (RsProgramVertex)pf);
1082}
1083
1084static void
1085nContextBindProgramRaster(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1086{
1087    LOG_API("nContextBindProgramRaster, con(%p), pf(%p)", con, (RsProgramRaster)pf);
1088    rsContextBindProgramRaster(con, (RsProgramRaster)pf);
1089}
1090
1091
1092// ---------------------------------------------------------------------------
1093
1094static void
1095nSamplerBegin(JNIEnv *_env, jobject _this, RsContext con)
1096{
1097    LOG_API("nSamplerBegin, con(%p)", con);
1098    rsSamplerBegin(con);
1099}
1100
1101static void
1102nSamplerSet(JNIEnv *_env, jobject _this, RsContext con, jint p, jint v)
1103{
1104    LOG_API("nSamplerSet, con(%p), param(%i), value(%i)", con, p, v);
1105    rsSamplerSet(con, (RsSamplerParam)p, (RsSamplerValue)v);
1106}
1107
1108static void
1109nSamplerSet2(JNIEnv *_env, jobject _this, RsContext con, jint p, jfloat v)
1110{
1111    LOG_API("nSamplerSet2, con(%p), param(%i), value(%f)", con, p, v);
1112    rsSamplerSet2(con, (RsSamplerParam)p, v);
1113}
1114
1115static jint
1116nSamplerCreate(JNIEnv *_env, jobject _this, RsContext con)
1117{
1118    LOG_API("nSamplerCreate, con(%p)", con);
1119    return (jint)rsSamplerCreate(con);
1120}
1121
1122// ---------------------------------------------------------------------------
1123
1124static jint
1125nMeshCreate(JNIEnv *_env, jobject _this, RsContext con, jint vtxCount, jint idxCount)
1126{
1127    LOG_API("nMeshCreate, con(%p), vtxCount(%i), idxCount(%i)", con, vtxCount, idxCount);
1128    int id = (int)rsMeshCreate(con, vtxCount, idxCount);
1129    return id;
1130}
1131
1132static void
1133nMeshBindVertex(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jint alloc, jint slot)
1134{
1135    LOG_API("nMeshBindVertex, con(%p), Mesh(%p), Alloc(%p), slot(%i)", con, (RsMesh)mesh, (RsAllocation)alloc, slot);
1136    rsMeshBindVertex(con, (RsMesh)mesh, (RsAllocation)alloc, slot);
1137}
1138
1139static void
1140nMeshBindIndex(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jint alloc, jint primID, jint slot)
1141{
1142    LOG_API("nMeshBindIndex, con(%p), Mesh(%p), Alloc(%p)", con, (RsMesh)mesh, (RsAllocation)alloc);
1143    rsMeshBindIndex(con, (RsMesh)mesh, (RsAllocation)alloc, primID, slot);
1144}
1145
1146static void
1147nMeshInitVertexAttribs(JNIEnv *_env, jobject _this, RsContext con, jint mesh)
1148{
1149    LOG_API("nMeshInitVertexAttribs, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1150    rsMeshInitVertexAttribs(con, (RsMesh)mesh);
1151}
1152
1153
1154static jint
1155nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, RsContext con, jint mesh)
1156{
1157    LOG_API("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1158    jint vtxCount = 0;
1159    rsaMeshGetVertexBufferCount(con, (RsMesh)mesh, &vtxCount);
1160    return vtxCount;
1161}
1162
1163static jint
1164nMeshGetIndexCount(JNIEnv *_env, jobject _this, RsContext con, jint mesh)
1165{
1166    LOG_API("nMeshGetIndexCount, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1167    jint idxCount = 0;
1168    rsaMeshGetIndexCount(con, (RsMesh)mesh, &idxCount);
1169    return idxCount;
1170}
1171
1172static void
1173nMeshGetVertices(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jintArray _ids, int numVtxIDs)
1174{
1175    LOG_API("nMeshGetVertices, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1176
1177    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
1178    rsaMeshGetVertices(con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
1179
1180    for(jint i = 0; i < numVtxIDs; i ++) {
1181        _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&allocs[i]);
1182    }
1183
1184    free(allocs);
1185}
1186
1187static void
1188nMeshGetIndices(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jintArray _idxIds, jintArray _primitives, int numIndices)
1189{
1190    LOG_API("nMeshGetVertices, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1191
1192    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
1193    uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
1194
1195    rsaMeshGetIndices(con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
1196
1197    for(jint i = 0; i < numIndices; i ++) {
1198        _env->SetIntArrayRegion(_idxIds, i, 1, (const jint*)&allocs[i]);
1199        _env->SetIntArrayRegion(_primitives, i, 1, (const jint*)&prims[i]);
1200    }
1201
1202    free(allocs);
1203    free(prims);
1204}
1205
1206// ---------------------------------------------------------------------------
1207
1208
1209static const char *classPathName = "android/renderscript/RenderScript";
1210
1211static JNINativeMethod methods[] = {
1212{"_nInit",                         "()V",                                     (void*)_nInit },
1213
1214{"nDeviceCreate",                  "()I",                                     (void*)nDeviceCreate },
1215{"nDeviceDestroy",                 "(I)V",                                    (void*)nDeviceDestroy },
1216{"nDeviceSetConfig",               "(III)V",                                  (void*)nDeviceSetConfig },
1217{"nContextGetUserMessage",         "(I[I)V",                                  (void*)nContextGetUserMessage },
1218{"nContextGetErrorMessage",        "(I)Ljava/lang/String;",                   (void*)nContextGetErrorMessage },
1219{"nContextPeekMessage",            "(I[IZ)I",                                 (void*)nContextPeekMessage },
1220
1221{"nContextInitToClient",           "(I)V",                                    (void*)nContextInitToClient },
1222{"nContextDeinitToClient",         "(I)V",                                    (void*)nContextDeinitToClient },
1223
1224
1225// All methods below are thread protected in java.
1226{"rsnContextCreate",                 "(II)I",                                 (void*)nContextCreate },
1227{"rsnContextCreateGL",               "(IIIIIIIIIIIIF)I",                      (void*)nContextCreateGL },
1228{"rsnContextFinish",                 "(I)V",                                  (void*)nContextFinish },
1229{"rsnContextSetPriority",            "(II)V",                                 (void*)nContextSetPriority },
1230{"rsnContextSetSurface",             "(IIILandroid/view/Surface;)V",          (void*)nContextSetSurface },
1231{"rsnContextDestroy",                "(I)V",                                  (void*)nContextDestroy },
1232{"rsnContextDump",                   "(II)V",                                 (void*)nContextDump },
1233{"rsnContextPause",                  "(I)V",                                  (void*)nContextPause },
1234{"rsnContextResume",                 "(I)V",                                  (void*)nContextResume },
1235{"rsnAssignName",                    "(II[B)V",                               (void*)nAssignName },
1236{"rsnGetName",                       "(II)Ljava/lang/String;",                (void*)nGetName },
1237{"rsnObjDestroy",                    "(II)V",                                 (void*)nObjDestroy },
1238
1239{"rsnFileA3DCreateFromAssetStream",  "(II)I",                                 (void*)nFileA3DCreateFromAssetStream },
1240{"rsnFileA3DGetNumIndexEntries",     "(II)I",                                 (void*)nFileA3DGetNumIndexEntries },
1241{"rsnFileA3DGetIndexEntries",        "(III[I[Ljava/lang/String;)V",           (void*)nFileA3DGetIndexEntries },
1242{"rsnFileA3DGetEntryByIndex",        "(III)I",                                (void*)nFileA3DGetEntryByIndex },
1243
1244{"rsnFontCreateFromFile",            "(ILjava/lang/String;FI)I",              (void*)nFontCreateFromFile },
1245
1246{"rsnElementCreate",                 "(IIIZI)I",                              (void*)nElementCreate },
1247{"rsnElementCreate2",                "(I[I[Ljava/lang/String;[I)I",           (void*)nElementCreate2 },
1248{"rsnElementGetNativeData",          "(II[I)V",                               (void*)nElementGetNativeData },
1249{"rsnElementGetSubElements",         "(II[I[Ljava/lang/String;)V",            (void*)nElementGetSubElements },
1250
1251{"rsnTypeCreate",                    "(IIIIIZZ)I",                            (void*)nTypeCreate },
1252{"rsnTypeGetNativeData",             "(II[I)V",                               (void*)nTypeGetNativeData },
1253
1254{"rsnAllocationCreateTyped",         "(IIII)I",                               (void*)nAllocationCreateTyped },
1255{"rsnAllocationCreateFromBitmap",    "(IIILandroid/graphics/Bitmap;I)I",      (void*)nAllocationCreateFromBitmap },
1256{"rsnAllocationCubeCreateFromBitmap","(IIILandroid/graphics/Bitmap;I)I",      (void*)nAllocationCubeCreateFromBitmap },
1257
1258{"rsnAllocationCopyFromBitmap",      "(IILandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyFromBitmap },
1259{"rsnAllocationCopyToBitmap",        "(IILandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyToBitmap },
1260
1261{"rsnAllocationUploadToTexture",     "(IIZI)V",                               (void*)nAllocationUploadToTexture },
1262{"rsnAllocationUploadToBufferObject","(II)V",                                 (void*)nAllocationUploadToBufferObject },
1263{"rsnAllocationSyncAll",             "(III)V",                                (void*)nAllocationSyncAll },
1264{"rsnAllocationSubData1D",           "(IIII[II)V",                            (void*)nAllocationSubData1D_i },
1265{"rsnAllocationSubData1D",           "(IIII[SI)V",                            (void*)nAllocationSubData1D_s },
1266{"rsnAllocationSubData1D",           "(IIII[BI)V",                            (void*)nAllocationSubData1D_b },
1267{"rsnAllocationSubData1D",           "(IIII[FI)V",                            (void*)nAllocationSubData1D_f },
1268{"rsnAllocationSubElementData1D",    "(IIII[BI)V",                            (void*)nAllocationSubElementData1D },
1269{"rsnAllocationSubData2D",           "(IIIIII[II)V",                          (void*)nAllocationSubData2D_i },
1270{"rsnAllocationSubData2D",           "(IIIIII[FI)V",                          (void*)nAllocationSubData2D_f },
1271{"rsnAllocationRead",                "(II[I)V",                               (void*)nAllocationRead_i },
1272{"rsnAllocationRead",                "(II[F)V",                               (void*)nAllocationRead_f },
1273{"rsnAllocationGetType",             "(II)I",                                 (void*)nAllocationGetType},
1274{"rsnAllocationResize1D",            "(III)V",                                (void*)nAllocationResize1D },
1275{"rsnAllocationResize2D",            "(IIII)V",                               (void*)nAllocationResize2D },
1276
1277{"rsnAdapter1DBindAllocation",       "(III)V",                                (void*)nAdapter1DBindAllocation },
1278{"rsnAdapter1DSetConstraint",        "(IIII)V",                               (void*)nAdapter1DSetConstraint },
1279{"rsnAdapter1DData",                 "(II[I)V",                               (void*)nAdapter1DData_i },
1280{"rsnAdapter1DData",                 "(II[F)V",                               (void*)nAdapter1DData_f },
1281{"rsnAdapter1DSubData",              "(IIII[I)V",                             (void*)nAdapter1DSubData_i },
1282{"rsnAdapter1DSubData",              "(IIII[F)V",                             (void*)nAdapter1DSubData_f },
1283{"rsnAdapter1DCreate",               "(I)I",                                  (void*)nAdapter1DCreate },
1284
1285{"rsnAdapter2DBindAllocation",       "(III)V",                                (void*)nAdapter2DBindAllocation },
1286{"rsnAdapter2DSetConstraint",        "(IIII)V",                               (void*)nAdapter2DSetConstraint },
1287{"rsnAdapter2DData",                 "(II[I)V",                               (void*)nAdapter2DData_i },
1288{"rsnAdapter2DData",                 "(II[F)V",                               (void*)nAdapter2DData_f },
1289{"rsnAdapter2DSubData",              "(IIIIII[I)V",                           (void*)nAdapter2DSubData_i },
1290{"rsnAdapter2DSubData",              "(IIIIII[F)V",                           (void*)nAdapter2DSubData_f },
1291{"rsnAdapter2DCreate",               "(I)I",                                  (void*)nAdapter2DCreate },
1292
1293{"rsnScriptBindAllocation",          "(IIII)V",                               (void*)nScriptBindAllocation },
1294{"rsnScriptSetTimeZone",             "(II[B)V",                               (void*)nScriptSetTimeZone },
1295{"rsnScriptInvoke",                  "(III)V",                                (void*)nScriptInvoke },
1296{"rsnScriptInvokeV",                 "(III[B)V",                              (void*)nScriptInvokeV },
1297{"rsnScriptSetVarI",                 "(IIII)V",                               (void*)nScriptSetVarI },
1298{"rsnScriptSetVarJ",                 "(IIIJ)V",                               (void*)nScriptSetVarJ },
1299{"rsnScriptSetVarF",                 "(IIIF)V",                               (void*)nScriptSetVarF },
1300{"rsnScriptSetVarD",                 "(IIID)V",                               (void*)nScriptSetVarD },
1301{"rsnScriptSetVarV",                 "(III[B)V",                              (void*)nScriptSetVarV },
1302{"rsnScriptSetVarObj",               "(IIII)V",                               (void*)nScriptSetVarObj },
1303
1304{"rsnScriptCBegin",                  "(I)V",                                  (void*)nScriptCBegin },
1305{"rsnScriptCSetScript",              "(I[BII)V",                              (void*)nScriptCSetScript },
1306{"rsnScriptCCreate",                 "(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)I",  (void*)nScriptCCreate },
1307
1308{"rsnProgramStoreBegin",             "(III)V",                                (void*)nProgramStoreBegin },
1309{"rsnProgramStoreDepthFunc",         "(II)V",                                 (void*)nProgramStoreDepthFunc },
1310{"rsnProgramStoreDepthMask",         "(IZ)V",                                 (void*)nProgramStoreDepthMask },
1311{"rsnProgramStoreColorMask",         "(IZZZZ)V",                              (void*)nProgramStoreColorMask },
1312{"rsnProgramStoreBlendFunc",         "(III)V",                                (void*)nProgramStoreBlendFunc },
1313{"rsnProgramStoreDither",            "(IZ)V",                                 (void*)nProgramStoreDither },
1314{"rsnProgramStoreCreate",            "(I)I",                                  (void*)nProgramStoreCreate },
1315
1316{"rsnProgramBindConstants",          "(IIII)V",                               (void*)nProgramBindConstants },
1317{"rsnProgramBindTexture",            "(IIII)V",                               (void*)nProgramBindTexture },
1318{"rsnProgramBindSampler",            "(IIII)V",                               (void*)nProgramBindSampler },
1319
1320{"rsnProgramFragmentCreate",        "(ILjava/lang/String;[I)I",               (void*)nProgramFragmentCreate },
1321
1322{"rsnProgramRasterCreate",           "(IZZZ)I",                               (void*)nProgramRasterCreate },
1323{"rsnProgramRasterSetLineWidth",     "(IIF)V",                                (void*)nProgramRasterSetLineWidth },
1324{"rsnProgramRasterSetCullMode",      "(III)V",                                (void*)nProgramRasterSetCullMode },
1325
1326{"rsnProgramVertexCreate",          "(ILjava/lang/String;[I)I",               (void*)nProgramVertexCreate },
1327
1328{"rsnContextBindRootScript",         "(II)V",                                 (void*)nContextBindRootScript },
1329{"rsnContextBindProgramStore",       "(II)V",                                 (void*)nContextBindProgramStore },
1330{"rsnContextBindProgramFragment",    "(II)V",                                 (void*)nContextBindProgramFragment },
1331{"rsnContextBindProgramVertex",      "(II)V",                                 (void*)nContextBindProgramVertex },
1332{"rsnContextBindProgramRaster",      "(II)V",                                 (void*)nContextBindProgramRaster },
1333
1334{"rsnSamplerBegin",                  "(I)V",                                  (void*)nSamplerBegin },
1335{"rsnSamplerSet",                    "(III)V",                                (void*)nSamplerSet },
1336{"rsnSamplerSet2",                   "(IIF)V",                                (void*)nSamplerSet2 },
1337{"rsnSamplerCreate",                 "(I)I",                                  (void*)nSamplerCreate },
1338
1339{"rsnMeshCreate",                    "(III)I",                                (void*)nMeshCreate },
1340{"rsnMeshBindVertex",                "(IIII)V",                               (void*)nMeshBindVertex },
1341{"rsnMeshBindIndex",                 "(IIIII)V",                              (void*)nMeshBindIndex },
1342{"rsnMeshInitVertexAttribs",         "(II)V",                                 (void*)nMeshInitVertexAttribs },
1343
1344{"rsnMeshGetVertexBufferCount",      "(II)I",                                 (void*)nMeshGetVertexBufferCount },
1345{"rsnMeshGetIndexCount",             "(II)I",                                 (void*)nMeshGetIndexCount },
1346{"rsnMeshGetVertices",               "(II[II)V",                              (void*)nMeshGetVertices },
1347{"rsnMeshGetIndices",                "(II[I[II)V",                            (void*)nMeshGetIndices },
1348
1349};
1350
1351static int registerFuncs(JNIEnv *_env)
1352{
1353    return android::AndroidRuntime::registerNativeMethods(
1354            _env, classPathName, methods, NELEM(methods));
1355}
1356
1357// ---------------------------------------------------------------------------
1358
1359jint JNI_OnLoad(JavaVM* vm, void* reserved)
1360{
1361    JNIEnv* env = NULL;
1362    jint result = -1;
1363
1364    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
1365        LOGE("ERROR: GetEnv failed\n");
1366        goto bail;
1367    }
1368    assert(env != NULL);
1369
1370    if (registerFuncs(env) < 0) {
1371        LOGE("ERROR: MediaPlayer native registration failed\n");
1372        goto bail;
1373    }
1374
1375    /* success -- return valid version number */
1376    result = JNI_VERSION_1_4;
1377
1378bail:
1379    return result;
1380}
1381