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