android_renderscript_RenderScript.cpp revision d4b23b54445b13dacaafad97d100999abb36ea6f
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, jint 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 resName)
906{
907    LOG_API("nScriptCCreate, con(%p)", con);
908    const char* resNameUTF = _env->GetStringUTFChars(resName, NULL);
909    return (jint)rsScriptCCreate(con, resNameUTF);
910}
911
912// ---------------------------------------------------------------------------
913
914static void
915nProgramStoreBegin(JNIEnv *_env, jobject _this, RsContext con, jint in, jint out)
916{
917    LOG_API("nProgramStoreBegin, con(%p), in(%p), out(%p)", con, (RsElement)in, (RsElement)out);
918    rsProgramStoreBegin(con, (RsElement)in, (RsElement)out);
919}
920
921static void
922nProgramStoreDepthFunc(JNIEnv *_env, jobject _this, RsContext con, jint func)
923{
924    LOG_API("nProgramStoreDepthFunc, con(%p), func(%i)", con, func);
925    rsProgramStoreDepthFunc(con, (RsDepthFunc)func);
926}
927
928static void
929nProgramStoreDepthMask(JNIEnv *_env, jobject _this, RsContext con, jboolean enable)
930{
931    LOG_API("nProgramStoreDepthMask, con(%p), enable(%i)", con, enable);
932    rsProgramStoreDepthMask(con, enable);
933}
934
935static void
936nProgramStoreColorMask(JNIEnv *_env, jobject _this, RsContext con, jboolean r, jboolean g, jboolean b, jboolean a)
937{
938    LOG_API("nProgramStoreColorMask, con(%p), r(%i), g(%i), b(%i), a(%i)", con, r, g, b, a);
939    rsProgramStoreColorMask(con, r, g, b, a);
940}
941
942static void
943nProgramStoreBlendFunc(JNIEnv *_env, jobject _this, RsContext con, int src, int dst)
944{
945    LOG_API("nProgramStoreBlendFunc, con(%p), src(%i), dst(%i)", con, src, dst);
946    rsProgramStoreBlendFunc(con, (RsBlendSrcFunc)src, (RsBlendDstFunc)dst);
947}
948
949static void
950nProgramStoreDither(JNIEnv *_env, jobject _this, RsContext con, jboolean enable)
951{
952    LOG_API("nProgramStoreDither, con(%p), enable(%i)", con, enable);
953    rsProgramStoreDither(con, enable);
954}
955
956static jint
957nProgramStoreCreate(JNIEnv *_env, jobject _this, RsContext con)
958{
959    LOG_API("nProgramStoreCreate, con(%p)", con);
960    return (jint)rsProgramStoreCreate(con);
961}
962
963// ---------------------------------------------------------------------------
964
965static void
966nProgramBindConstants(JNIEnv *_env, jobject _this, RsContext con, jint vpv, jint slot, jint a)
967{
968    LOG_API("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", con, (RsProgramVertex)vpv, slot, (RsAllocation)a);
969    rsProgramBindConstants(con, (RsProgram)vpv, slot, (RsAllocation)a);
970}
971
972static void
973nProgramBindTexture(JNIEnv *_env, jobject _this, RsContext con, jint vpf, jint slot, jint a)
974{
975    LOG_API("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
976    rsProgramBindTexture(con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
977}
978
979static void
980nProgramBindSampler(JNIEnv *_env, jobject _this, RsContext con, jint vpf, jint slot, jint a)
981{
982    LOG_API("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsSampler)a);
983    rsProgramBindSampler(con, (RsProgramFragment)vpf, slot, (RsSampler)a);
984}
985
986// ---------------------------------------------------------------------------
987
988static jint
989nProgramFragmentCreate(JNIEnv *_env, jobject _this, RsContext con, jstring shader, jintArray params)
990{
991    const char* shaderUTF = _env->GetStringUTFChars(shader, NULL);
992    jint shaderLen = _env->GetStringUTFLength(shader);
993    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
994    jint paramLen = _env->GetArrayLength(params);
995
996    LOG_API("nProgramFragmentCreate, con(%p), shaderLen(%i), paramLen(%i)", con, shaderLen, paramLen);
997
998    jint ret = (jint)rsProgramFragmentCreate(con, shaderUTF, shaderLen, (uint32_t *)paramPtr, paramLen);
999    _env->ReleaseStringUTFChars(shader, shaderUTF);
1000    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1001    return ret;
1002}
1003
1004
1005// ---------------------------------------------------------------------------
1006
1007static jint
1008nProgramVertexCreate(JNIEnv *_env, jobject _this, RsContext con, jstring shader, jintArray params)
1009{
1010    const char* shaderUTF = _env->GetStringUTFChars(shader, NULL);
1011    jint shaderLen = _env->GetStringUTFLength(shader);
1012    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1013    jint paramLen = _env->GetArrayLength(params);
1014
1015    LOG_API("nProgramVertexCreate, con(%p), shaderLen(%i), paramLen(%i)", con, shaderLen, paramLen);
1016
1017    jint ret = (jint)rsProgramVertexCreate(con, shaderUTF, shaderLen, (uint32_t *)paramPtr, paramLen);
1018    _env->ReleaseStringUTFChars(shader, shaderUTF);
1019    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1020    return ret;
1021}
1022
1023// ---------------------------------------------------------------------------
1024
1025static jint
1026nProgramRasterCreate(JNIEnv *_env, jobject _this, RsContext con, jboolean pointSmooth, jboolean lineSmooth, jboolean pointSprite)
1027{
1028    LOG_API("nProgramRasterCreate, con(%p), pointSmooth(%i), lineSmooth(%i), pointSprite(%i)",
1029            con, pointSmooth, lineSmooth, pointSprite);
1030    return (jint)rsProgramRasterCreate(con, pointSmooth, lineSmooth, pointSprite);
1031}
1032
1033static void
1034nProgramRasterSetLineWidth(JNIEnv *_env, jobject _this, RsContext con, jint vpr, jfloat v)
1035{
1036    LOG_API("nProgramRasterSetLineWidth, con(%p), vpf(%p), value(%f)", con, (RsProgramRaster)vpr, v);
1037    rsProgramRasterSetLineWidth(con, (RsProgramRaster)vpr, v);
1038}
1039
1040static void
1041nProgramRasterSetCullMode(JNIEnv *_env, jobject _this, RsContext con, jint vpr, jint v)
1042{
1043    LOG_API("nProgramRasterSetCullMode, con(%p), vpf(%p), value(%i)", con, (RsProgramRaster)vpr, v);
1044    rsProgramRasterSetCullMode(con, (RsProgramRaster)vpr, (RsCullMode)v);
1045}
1046
1047
1048// ---------------------------------------------------------------------------
1049
1050static void
1051nContextBindRootScript(JNIEnv *_env, jobject _this, RsContext con, jint script)
1052{
1053    LOG_API("nContextBindRootScript, con(%p), script(%p)", con, (RsScript)script);
1054    rsContextBindRootScript(con, (RsScript)script);
1055}
1056
1057static void
1058nContextBindProgramStore(JNIEnv *_env, jobject _this, RsContext con, jint pfs)
1059{
1060    LOG_API("nContextBindProgramStore, con(%p), pfs(%p)", con, (RsProgramStore)pfs);
1061    rsContextBindProgramStore(con, (RsProgramStore)pfs);
1062}
1063
1064static void
1065nContextBindProgramFragment(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1066{
1067    LOG_API("nContextBindProgramFragment, con(%p), pf(%p)", con, (RsProgramFragment)pf);
1068    rsContextBindProgramFragment(con, (RsProgramFragment)pf);
1069}
1070
1071static void
1072nContextBindProgramVertex(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1073{
1074    LOG_API("nContextBindProgramVertex, con(%p), pf(%p)", con, (RsProgramVertex)pf);
1075    rsContextBindProgramVertex(con, (RsProgramVertex)pf);
1076}
1077
1078static void
1079nContextBindProgramRaster(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1080{
1081    LOG_API("nContextBindProgramRaster, con(%p), pf(%p)", con, (RsProgramRaster)pf);
1082    rsContextBindProgramRaster(con, (RsProgramRaster)pf);
1083}
1084
1085
1086// ---------------------------------------------------------------------------
1087
1088static void
1089nSamplerBegin(JNIEnv *_env, jobject _this, RsContext con)
1090{
1091    LOG_API("nSamplerBegin, con(%p)", con);
1092    rsSamplerBegin(con);
1093}
1094
1095static void
1096nSamplerSet(JNIEnv *_env, jobject _this, RsContext con, jint p, jint v)
1097{
1098    LOG_API("nSamplerSet, con(%p), param(%i), value(%i)", con, p, v);
1099    rsSamplerSet(con, (RsSamplerParam)p, (RsSamplerValue)v);
1100}
1101
1102static void
1103nSamplerSet2(JNIEnv *_env, jobject _this, RsContext con, jint p, jfloat v)
1104{
1105    LOG_API("nSamplerSet2, con(%p), param(%i), value(%f)", con, p, v);
1106    rsSamplerSet2(con, (RsSamplerParam)p, v);
1107}
1108
1109static jint
1110nSamplerCreate(JNIEnv *_env, jobject _this, RsContext con)
1111{
1112    LOG_API("nSamplerCreate, con(%p)", con);
1113    return (jint)rsSamplerCreate(con);
1114}
1115
1116// ---------------------------------------------------------------------------
1117
1118static jint
1119nMeshCreate(JNIEnv *_env, jobject _this, RsContext con, jint vtxCount, jint idxCount)
1120{
1121    LOG_API("nMeshCreate, con(%p), vtxCount(%i), idxCount(%i)", con, vtxCount, idxCount);
1122    int id = (int)rsMeshCreate(con, vtxCount, idxCount);
1123    return id;
1124}
1125
1126static void
1127nMeshBindVertex(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jint alloc, jint slot)
1128{
1129    LOG_API("nMeshBindVertex, con(%p), Mesh(%p), Alloc(%p), slot(%i)", con, (RsMesh)mesh, (RsAllocation)alloc, slot);
1130    rsMeshBindVertex(con, (RsMesh)mesh, (RsAllocation)alloc, slot);
1131}
1132
1133static void
1134nMeshBindIndex(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jint alloc, jint primID, jint slot)
1135{
1136    LOG_API("nMeshBindIndex, con(%p), Mesh(%p), Alloc(%p)", con, (RsMesh)mesh, (RsAllocation)alloc);
1137    rsMeshBindIndex(con, (RsMesh)mesh, (RsAllocation)alloc, primID, slot);
1138}
1139
1140static void
1141nMeshInitVertexAttribs(JNIEnv *_env, jobject _this, RsContext con, jint mesh)
1142{
1143    LOG_API("nMeshInitVertexAttribs, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1144    rsMeshInitVertexAttribs(con, (RsMesh)mesh);
1145}
1146
1147
1148static jint
1149nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, RsContext con, jint mesh)
1150{
1151    LOG_API("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1152    jint vtxCount = 0;
1153    rsaMeshGetVertexBufferCount(con, (RsMesh)mesh, &vtxCount);
1154    return vtxCount;
1155}
1156
1157static jint
1158nMeshGetIndexCount(JNIEnv *_env, jobject _this, RsContext con, jint mesh)
1159{
1160    LOG_API("nMeshGetIndexCount, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1161    jint idxCount = 0;
1162    rsaMeshGetIndexCount(con, (RsMesh)mesh, &idxCount);
1163    return idxCount;
1164}
1165
1166static void
1167nMeshGetVertices(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jintArray _ids, int numVtxIDs)
1168{
1169    LOG_API("nMeshGetVertices, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1170
1171    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
1172    rsaMeshGetVertices(con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
1173
1174    for(jint i = 0; i < numVtxIDs; i ++) {
1175        _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&allocs[i]);
1176    }
1177
1178    free(allocs);
1179}
1180
1181static void
1182nMeshGetIndices(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jintArray _idxIds, jintArray _primitives, int numIndices)
1183{
1184    LOG_API("nMeshGetVertices, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1185
1186    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
1187    uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
1188
1189    rsaMeshGetIndices(con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
1190
1191    for(jint i = 0; i < numIndices; i ++) {
1192        _env->SetIntArrayRegion(_idxIds, i, 1, (const jint*)&allocs[i]);
1193        _env->SetIntArrayRegion(_primitives, i, 1, (const jint*)&prims[i]);
1194    }
1195
1196    free(allocs);
1197    free(prims);
1198}
1199
1200// ---------------------------------------------------------------------------
1201
1202
1203static const char *classPathName = "android/renderscript/RenderScript";
1204
1205static JNINativeMethod methods[] = {
1206{"_nInit",                         "()V",                                     (void*)_nInit },
1207
1208{"nDeviceCreate",                  "()I",                                     (void*)nDeviceCreate },
1209{"nDeviceDestroy",                 "(I)V",                                    (void*)nDeviceDestroy },
1210{"nDeviceSetConfig",               "(III)V",                                  (void*)nDeviceSetConfig },
1211{"nContextGetUserMessage",         "(I[I)V",                                  (void*)nContextGetUserMessage },
1212{"nContextGetErrorMessage",        "(I)Ljava/lang/String;",                   (void*)nContextGetErrorMessage },
1213{"nContextPeekMessage",            "(I[IZ)I",                                 (void*)nContextPeekMessage },
1214
1215{"nContextInitToClient",           "(I)V",                                    (void*)nContextInitToClient },
1216{"nContextDeinitToClient",         "(I)V",                                    (void*)nContextDeinitToClient },
1217
1218
1219// All methods below are thread protected in java.
1220{"rsnContextCreate",                 "(II)I",                                 (void*)nContextCreate },
1221{"rsnContextCreateGL",               "(IIIIIIIIIIIIF)I",                      (void*)nContextCreateGL },
1222{"rsnContextFinish",                 "(I)V",                                  (void*)nContextFinish },
1223{"rsnContextSetPriority",            "(II)V",                                 (void*)nContextSetPriority },
1224{"rsnContextSetSurface",             "(IIILandroid/view/Surface;)V",          (void*)nContextSetSurface },
1225{"rsnContextDestroy",                "(I)V",                                  (void*)nContextDestroy },
1226{"rsnContextDump",                   "(II)V",                                 (void*)nContextDump },
1227{"rsnContextPause",                  "(I)V",                                  (void*)nContextPause },
1228{"rsnContextResume",                 "(I)V",                                  (void*)nContextResume },
1229{"rsnAssignName",                    "(II[B)V",                               (void*)nAssignName },
1230{"rsnGetName",                       "(II)Ljava/lang/String;",                (void*)nGetName },
1231{"rsnObjDestroy",                    "(II)V",                                 (void*)nObjDestroy },
1232
1233{"rsnFileA3DCreateFromAssetStream",  "(II)I",                                 (void*)nFileA3DCreateFromAssetStream },
1234{"rsnFileA3DGetNumIndexEntries",     "(II)I",                                 (void*)nFileA3DGetNumIndexEntries },
1235{"rsnFileA3DGetIndexEntries",        "(III[I[Ljava/lang/String;)V",           (void*)nFileA3DGetIndexEntries },
1236{"rsnFileA3DGetEntryByIndex",        "(III)I",                                (void*)nFileA3DGetEntryByIndex },
1237
1238{"rsnFontCreateFromFile",            "(ILjava/lang/String;II)I",              (void*)nFontCreateFromFile },
1239
1240{"rsnElementCreate",                 "(IIIZI)I",                              (void*)nElementCreate },
1241{"rsnElementCreate2",                "(I[I[Ljava/lang/String;[I)I",           (void*)nElementCreate2 },
1242{"rsnElementGetNativeData",          "(II[I)V",                               (void*)nElementGetNativeData },
1243{"rsnElementGetSubElements",         "(II[I[Ljava/lang/String;)V",            (void*)nElementGetSubElements },
1244
1245{"rsnTypeCreate",                    "(IIIIIZZ)I",                            (void*)nTypeCreate },
1246{"rsnTypeGetNativeData",             "(II[I)V",                               (void*)nTypeGetNativeData },
1247
1248{"rsnAllocationCreateTyped",         "(IIII)I",                               (void*)nAllocationCreateTyped },
1249{"rsnAllocationCreateFromBitmap",    "(IIILandroid/graphics/Bitmap;I)I",      (void*)nAllocationCreateFromBitmap },
1250{"rsnAllocationCubeCreateFromBitmap","(IIILandroid/graphics/Bitmap;I)I",      (void*)nAllocationCubeCreateFromBitmap },
1251
1252{"rsnAllocationCopyFromBitmap",      "(IILandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyFromBitmap },
1253{"rsnAllocationCopyToBitmap",        "(IILandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyToBitmap },
1254
1255{"rsnAllocationUploadToTexture",     "(IIZI)V",                               (void*)nAllocationUploadToTexture },
1256{"rsnAllocationUploadToBufferObject","(II)V",                                 (void*)nAllocationUploadToBufferObject },
1257{"rsnAllocationSyncAll",             "(III)V",                                (void*)nAllocationSyncAll },
1258{"rsnAllocationSubData1D",           "(IIII[II)V",                            (void*)nAllocationSubData1D_i },
1259{"rsnAllocationSubData1D",           "(IIII[SI)V",                            (void*)nAllocationSubData1D_s },
1260{"rsnAllocationSubData1D",           "(IIII[BI)V",                            (void*)nAllocationSubData1D_b },
1261{"rsnAllocationSubData1D",           "(IIII[FI)V",                            (void*)nAllocationSubData1D_f },
1262{"rsnAllocationSubElementData1D",    "(IIII[BI)V",                            (void*)nAllocationSubElementData1D },
1263{"rsnAllocationSubData2D",           "(IIIIII[II)V",                          (void*)nAllocationSubData2D_i },
1264{"rsnAllocationSubData2D",           "(IIIIII[FI)V",                          (void*)nAllocationSubData2D_f },
1265{"rsnAllocationRead",                "(II[I)V",                               (void*)nAllocationRead_i },
1266{"rsnAllocationRead",                "(II[F)V",                               (void*)nAllocationRead_f },
1267{"rsnAllocationGetType",             "(II)I",                                 (void*)nAllocationGetType},
1268{"rsnAllocationResize1D",            "(III)V",                                (void*)nAllocationResize1D },
1269{"rsnAllocationResize2D",            "(IIII)V",                               (void*)nAllocationResize2D },
1270
1271{"rsnAdapter1DBindAllocation",       "(III)V",                                (void*)nAdapter1DBindAllocation },
1272{"rsnAdapter1DSetConstraint",        "(IIII)V",                               (void*)nAdapter1DSetConstraint },
1273{"rsnAdapter1DData",                 "(II[I)V",                               (void*)nAdapter1DData_i },
1274{"rsnAdapter1DData",                 "(II[F)V",                               (void*)nAdapter1DData_f },
1275{"rsnAdapter1DSubData",              "(IIII[I)V",                             (void*)nAdapter1DSubData_i },
1276{"rsnAdapter1DSubData",              "(IIII[F)V",                             (void*)nAdapter1DSubData_f },
1277{"rsnAdapter1DCreate",               "(I)I",                                  (void*)nAdapter1DCreate },
1278
1279{"rsnAdapter2DBindAllocation",       "(III)V",                                (void*)nAdapter2DBindAllocation },
1280{"rsnAdapter2DSetConstraint",        "(IIII)V",                               (void*)nAdapter2DSetConstraint },
1281{"rsnAdapter2DData",                 "(II[I)V",                               (void*)nAdapter2DData_i },
1282{"rsnAdapter2DData",                 "(II[F)V",                               (void*)nAdapter2DData_f },
1283{"rsnAdapter2DSubData",              "(IIIIII[I)V",                           (void*)nAdapter2DSubData_i },
1284{"rsnAdapter2DSubData",              "(IIIIII[F)V",                           (void*)nAdapter2DSubData_f },
1285{"rsnAdapter2DCreate",               "(I)I",                                  (void*)nAdapter2DCreate },
1286
1287{"rsnScriptBindAllocation",          "(IIII)V",                               (void*)nScriptBindAllocation },
1288{"rsnScriptSetTimeZone",             "(II[B)V",                               (void*)nScriptSetTimeZone },
1289{"rsnScriptInvoke",                  "(III)V",                                (void*)nScriptInvoke },
1290{"rsnScriptInvokeV",                 "(III[B)V",                              (void*)nScriptInvokeV },
1291{"rsnScriptSetVarI",                 "(IIII)V",                               (void*)nScriptSetVarI },
1292{"rsnScriptSetVarJ",                 "(IIIJ)V",                               (void*)nScriptSetVarJ },
1293{"rsnScriptSetVarF",                 "(IIIF)V",                               (void*)nScriptSetVarF },
1294{"rsnScriptSetVarD",                 "(IIID)V",                               (void*)nScriptSetVarD },
1295{"rsnScriptSetVarV",                 "(III[B)V",                              (void*)nScriptSetVarV },
1296{"rsnScriptSetVarObj",               "(IIII)V",                               (void*)nScriptSetVarObj },
1297
1298{"rsnScriptCBegin",                  "(I)V",                                  (void*)nScriptCBegin },
1299{"rsnScriptCSetScript",              "(I[BII)V",                              (void*)nScriptCSetScript },
1300{"rsnScriptCCreate",                 "(ILjava/lang/String;)I",                (void*)nScriptCCreate },
1301
1302{"rsnProgramStoreBegin",             "(III)V",                                (void*)nProgramStoreBegin },
1303{"rsnProgramStoreDepthFunc",         "(II)V",                                 (void*)nProgramStoreDepthFunc },
1304{"rsnProgramStoreDepthMask",         "(IZ)V",                                 (void*)nProgramStoreDepthMask },
1305{"rsnProgramStoreColorMask",         "(IZZZZ)V",                              (void*)nProgramStoreColorMask },
1306{"rsnProgramStoreBlendFunc",         "(III)V",                                (void*)nProgramStoreBlendFunc },
1307{"rsnProgramStoreDither",            "(IZ)V",                                 (void*)nProgramStoreDither },
1308{"rsnProgramStoreCreate",            "(I)I",                                  (void*)nProgramStoreCreate },
1309
1310{"rsnProgramBindConstants",          "(IIII)V",                               (void*)nProgramBindConstants },
1311{"rsnProgramBindTexture",            "(IIII)V",                               (void*)nProgramBindTexture },
1312{"rsnProgramBindSampler",            "(IIII)V",                               (void*)nProgramBindSampler },
1313
1314{"rsnProgramFragmentCreate",        "(ILjava/lang/String;[I)I",               (void*)nProgramFragmentCreate },
1315
1316{"rsnProgramRasterCreate",           "(IZZZ)I",                               (void*)nProgramRasterCreate },
1317{"rsnProgramRasterSetLineWidth",     "(IIF)V",                                (void*)nProgramRasterSetLineWidth },
1318{"rsnProgramRasterSetCullMode",      "(III)V",                                (void*)nProgramRasterSetCullMode },
1319
1320{"rsnProgramVertexCreate",          "(ILjava/lang/String;[I)I",               (void*)nProgramVertexCreate },
1321
1322{"rsnContextBindRootScript",         "(II)V",                                 (void*)nContextBindRootScript },
1323{"rsnContextBindProgramStore",       "(II)V",                                 (void*)nContextBindProgramStore },
1324{"rsnContextBindProgramFragment",    "(II)V",                                 (void*)nContextBindProgramFragment },
1325{"rsnContextBindProgramVertex",      "(II)V",                                 (void*)nContextBindProgramVertex },
1326{"rsnContextBindProgramRaster",      "(II)V",                                 (void*)nContextBindProgramRaster },
1327
1328{"rsnSamplerBegin",                  "(I)V",                                  (void*)nSamplerBegin },
1329{"rsnSamplerSet",                    "(III)V",                                (void*)nSamplerSet },
1330{"rsnSamplerSet2",                   "(IIF)V",                                (void*)nSamplerSet2 },
1331{"rsnSamplerCreate",                 "(I)I",                                  (void*)nSamplerCreate },
1332
1333{"rsnMeshCreate",                    "(III)I",                                (void*)nMeshCreate },
1334{"rsnMeshBindVertex",                "(IIII)V",                               (void*)nMeshBindVertex },
1335{"rsnMeshBindIndex",                 "(IIIII)V",                              (void*)nMeshBindIndex },
1336{"rsnMeshInitVertexAttribs",         "(II)V",                                 (void*)nMeshInitVertexAttribs },
1337
1338{"rsnMeshGetVertexBufferCount",      "(II)I",                                 (void*)nMeshGetVertexBufferCount },
1339{"rsnMeshGetIndexCount",             "(II)I",                                 (void*)nMeshGetIndexCount },
1340{"rsnMeshGetVertices",               "(II[II)V",                              (void*)nMeshGetVertices },
1341{"rsnMeshGetIndices",                "(II[I[II)V",                            (void*)nMeshGetIndices },
1342
1343};
1344
1345static int registerFuncs(JNIEnv *_env)
1346{
1347    return android::AndroidRuntime::registerNativeMethods(
1348            _env, classPathName, methods, NELEM(methods));
1349}
1350
1351// ---------------------------------------------------------------------------
1352
1353jint JNI_OnLoad(JavaVM* vm, void* reserved)
1354{
1355    JNIEnv* env = NULL;
1356    jint result = -1;
1357
1358    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
1359        LOGE("ERROR: GetEnv failed\n");
1360        goto bail;
1361    }
1362    assert(env != NULL);
1363
1364    if (registerFuncs(env) < 0) {
1365        LOGE("ERROR: MediaPlayer native registration failed\n");
1366        goto bail;
1367    }
1368
1369    /* success -- return valid version number */
1370    result = JNI_VERSION_1_4;
1371
1372bail:
1373    return result;
1374}
1375
1376