android_renderscript_RenderScript.cpp revision 96ed4cfa62dd09aafb3f9da01e047661b4fe3c95
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
41#include <RenderScript.h>
42#include <RenderScriptEnv.h>
43
44//#define LOG_API LOGE
45#define LOG_API(...)
46
47using namespace android;
48
49// ---------------------------------------------------------------------------
50
51static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
52{
53    jclass npeClazz = env->FindClass(exc);
54    env->ThrowNew(npeClazz, msg);
55}
56
57static jfieldID gContextId = 0;
58static jfieldID gNativeBitmapID = 0;
59static jfieldID gTypeNativeCache = 0;
60
61static RsElement g_A_8 = NULL;
62static RsElement g_RGBA_4444 = NULL;
63static RsElement g_RGBA_8888 = NULL;
64static RsElement g_RGB_565 = NULL;
65
66static void _nInit(JNIEnv *_env, jclass _this)
67{
68    gContextId             = _env->GetFieldID(_this, "mContext", "I");
69
70    jclass bitmapClass = _env->FindClass("android/graphics/Bitmap");
71    gNativeBitmapID = _env->GetFieldID(bitmapClass, "mNativeBitmap", "I");
72
73    jclass typeClass = _env->FindClass("android/renderscript/Type");
74    gTypeNativeCache = _env->GetFieldID(typeClass, "mNativeCache", "I");
75}
76
77static void nInitElements(JNIEnv *_env, jobject _this, jint a8, jint rgba4444, jint rgba8888, jint rgb565)
78{
79    g_A_8 = reinterpret_cast<RsElement>(a8);
80    g_RGBA_4444 = reinterpret_cast<RsElement>(rgba4444);
81    g_RGBA_8888 = reinterpret_cast<RsElement>(rgba8888);
82    g_RGB_565 = reinterpret_cast<RsElement>(rgb565);
83}
84
85// ---------------------------------------------------------------------------
86
87static void
88nContextFinish(JNIEnv *_env, jobject _this)
89{
90    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
91    LOG_API("nContextFinish, con(%p)", con);
92    rsContextFinish(con);
93}
94
95static void
96nAssignName(JNIEnv *_env, jobject _this, jint obj, jbyteArray str)
97{
98    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
99    LOG_API("nAssignName, con(%p), obj(%p)", con, (void *)obj);
100
101    jint len = _env->GetArrayLength(str);
102    jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
103    rsAssignName(con, (void *)obj, (const char *)cptr, len);
104    _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
105}
106
107static void
108nObjDestroy(JNIEnv *_env, jobject _this, jint obj)
109{
110    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
111    LOG_API("nObjDestroy, con(%p) obj(%p)", con, (void *)obj);
112    rsObjDestroy(con, (void *)obj);
113}
114
115static void
116nObjDestroyOOB(JNIEnv *_env, jobject _this, jint obj)
117{
118    // This function only differs from nObjDestroy in that it calls the
119    // special Out Of Band version of ObjDestroy which is thread safe.
120    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
121    LOG_API("nObjDestroyOOB, con(%p) obj(%p)", con, (void *)obj);
122    rsObjDestroyOOB(con, (void *)obj);
123}
124
125static jint
126nFileOpen(JNIEnv *_env, jobject _this, jbyteArray str)
127{
128    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
129    LOG_API("nFileOpen, con(%p)", con);
130
131    jint len = _env->GetArrayLength(str);
132    jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
133    jint ret = (jint)rsFileOpen(con, (const char *)cptr, len);
134    _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
135    return ret;
136}
137
138// ---------------------------------------------------------------------------
139
140static jint
141nDeviceCreate(JNIEnv *_env, jobject _this)
142{
143    LOG_API("nDeviceCreate");
144    return (jint)rsDeviceCreate();
145}
146
147static void
148nDeviceDestroy(JNIEnv *_env, jobject _this, jint dev)
149{
150    LOG_API("nDeviceDestroy");
151    return rsDeviceDestroy((RsDevice)dev);
152}
153
154static void
155nDeviceSetConfig(JNIEnv *_env, jobject _this, jint dev, jint p, jint value)
156{
157    LOG_API("nDeviceSetConfig  dev(%p), param(%i), value(%i)", (void *)dev, p, value);
158    return rsDeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value);
159}
160
161static jint
162nContextCreate(JNIEnv *_env, jobject _this, jint dev, jint ver)
163{
164    LOG_API("nContextCreate");
165    return (jint)rsContextCreate((RsDevice)dev, ver);
166}
167
168static jint
169nContextCreateGL(JNIEnv *_env, jobject _this, jint dev, jint ver, jboolean useDepth)
170{
171    LOG_API("nContextCreateGL");
172    return (jint)rsContextCreateGL((RsDevice)dev, ver, useDepth);
173}
174
175static void
176nContextSetPriority(JNIEnv *_env, jobject _this, jint p)
177{
178    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
179    LOG_API("ContextSetPriority, con(%p), priority(%i)", con, p);
180    rsContextSetPriority(con, p);
181}
182
183
184
185static void
186nContextSetSurface(JNIEnv *_env, jobject _this, jint width, jint height, jobject wnd)
187{
188    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
189    LOG_API("nContextSetSurface, con(%p), width(%i), height(%i), surface(%p)", con, width, height, (Surface *)wnd);
190
191    Surface * window = NULL;
192    if (wnd == NULL) {
193
194    } else {
195        jclass surface_class = _env->FindClass("android/view/Surface");
196        jfieldID surfaceFieldID = _env->GetFieldID(surface_class, ANDROID_VIEW_SURFACE_JNI_ID, "I");
197        window = (Surface*)_env->GetIntField(wnd, surfaceFieldID);
198    }
199
200    rsContextSetSurface(con, width, height, window);
201}
202
203static void
204nContextDestroy(JNIEnv *_env, jobject _this, jint con)
205{
206    LOG_API("nContextDestroy, con(%p)", (RsContext)con);
207    rsContextDestroy((RsContext)con);
208}
209
210static void
211nContextDump(JNIEnv *_env, jobject _this, jint bits)
212{
213    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
214    LOG_API("nContextDump, con(%p)  bits(%i)", (RsContext)con, bits);
215    rsContextDump((RsContext)con, bits);
216}
217
218static void
219nContextPause(JNIEnv *_env, jobject _this)
220{
221    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
222    LOG_API("nContextPause, con(%p)", con);
223    rsContextPause(con);
224}
225
226static void
227nContextResume(JNIEnv *_env, jobject _this)
228{
229    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
230    LOG_API("nContextResume, con(%p)", con);
231    rsContextResume(con);
232}
233
234static jint
235nContextGetMessage(JNIEnv *_env, jobject _this, jintArray data, jboolean wait)
236{
237    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
238    jint len = _env->GetArrayLength(data);
239    LOG_API("nContextGetMessage, con(%p), len(%i)", con, len);
240    jint *ptr = _env->GetIntArrayElements(data, NULL);
241    size_t receiveLen;
242    int id = rsContextGetMessage(con, ptr, &receiveLen, len * 4, wait);
243    if (!id && receiveLen) {
244        LOGE("message receive buffer too small.  %i", receiveLen);
245    }
246    _env->ReleaseIntArrayElements(data, ptr, 0);
247    return id;
248}
249
250static void nContextInitToClient(JNIEnv *_env, jobject _this)
251{
252    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
253    LOG_API("nContextInitToClient, con(%p)", con);
254    rsContextInitToClient(con);
255}
256
257static void nContextDeinitToClient(JNIEnv *_env, jobject _this)
258{
259    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
260    LOG_API("nContextDeinitToClient, con(%p)", con);
261    rsContextDeinitToClient(con);
262}
263
264
265static jint
266nElementCreate(JNIEnv *_env, jobject _this, jint type, jint kind, jboolean norm, jint size)
267{
268    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
269    LOG_API("nElementCreate, con(%p), type(%i), kind(%i), norm(%i), size(%i)", con, type, kind, norm, size);
270    return (jint)rsElementCreate(con, (RsDataType)type, (RsDataKind)kind, norm, size);
271}
272
273static jint
274nElementCreate2(JNIEnv *_env, jobject _this, jintArray _ids, jobjectArray _names)
275{
276    int fieldCount = _env->GetArrayLength(_ids);
277    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
278    LOG_API("nElementCreate2, con(%p)", con);
279
280    jint *ids = _env->GetIntArrayElements(_ids, NULL);
281    const char ** nameArray = (const char **)calloc(fieldCount, sizeof(char *));
282    size_t* sizeArray = (size_t*)calloc(fieldCount, sizeof(size_t));
283
284    for (int ct=0; ct < fieldCount; ct++) {
285        jstring s = (jstring)_env->GetObjectArrayElement(_names, ct);
286        nameArray[ct] = _env->GetStringUTFChars(s, NULL);
287        sizeArray[ct] = _env->GetStringUTFLength(s);
288    }
289    jint id = (jint)rsElementCreate2(con, fieldCount, (RsElement *)ids, nameArray, sizeArray);
290    for (int ct=0; ct < fieldCount; ct++) {
291        jstring s = (jstring)_env->GetObjectArrayElement(_names, ct);
292        _env->ReleaseStringUTFChars(s, nameArray[ct]);
293    }
294    _env->ReleaseIntArrayElements(_ids, ids, JNI_ABORT);
295    free(nameArray);
296    free(sizeArray);
297    return (jint)id;
298}
299
300// -----------------------------------
301
302static void
303nTypeBegin(JNIEnv *_env, jobject _this, jint eID)
304{
305    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
306    LOG_API("nTypeBegin, con(%p) e(%p)", con, (RsElement)eID);
307    rsTypeBegin(con, (RsElement)eID);
308}
309
310static void
311nTypeAdd(JNIEnv *_env, jobject _this, jint dim, jint val)
312{
313    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
314    LOG_API("nTypeAdd, con(%p) dim(%i), val(%i)", con, dim, val);
315    rsTypeAdd(con, (RsDimension)dim, val);
316}
317
318static jint
319nTypeCreate(JNIEnv *_env, jobject _this)
320{
321    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
322    LOG_API("nTypeCreate, con(%p)", con);
323    return (jint)rsTypeCreate(con);
324}
325
326static void * SF_LoadInt(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
327{
328    ((int32_t *)buffer)[0] = _env->GetIntField(_obj, _field);
329    return ((uint8_t *)buffer) + 4;
330}
331
332static void * SF_LoadShort(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
333{
334    ((int16_t *)buffer)[0] = _env->GetShortField(_obj, _field);
335    return ((uint8_t *)buffer) + 2;
336}
337
338static void * SF_LoadByte(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
339{
340    ((int8_t *)buffer)[0] = _env->GetByteField(_obj, _field);
341    return ((uint8_t *)buffer) + 1;
342}
343
344static void * SF_LoadFloat(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
345{
346    ((float *)buffer)[0] = _env->GetFloatField(_obj, _field);
347    return ((uint8_t *)buffer) + 4;
348}
349
350static void * SF_SaveInt(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
351{
352    _env->SetIntField(_obj, _field, ((int32_t *)buffer)[0]);
353    return ((uint8_t *)buffer) + 4;
354}
355
356static void * SF_SaveShort(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
357{
358    _env->SetShortField(_obj, _field, ((int16_t *)buffer)[0]);
359    return ((uint8_t *)buffer) + 2;
360}
361
362static void * SF_SaveByte(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
363{
364    _env->SetByteField(_obj, _field, ((int8_t *)buffer)[0]);
365    return ((uint8_t *)buffer) + 1;
366}
367
368static void * SF_SaveFloat(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
369{
370    _env->SetFloatField(_obj, _field, ((float *)buffer)[0]);
371    return ((uint8_t *)buffer) + 4;
372}
373
374struct TypeFieldCache {
375    jfieldID field;
376    int bits;
377    void * (*ptr)(JNIEnv *, jobject, jfieldID, void *buffer);
378    void * (*readPtr)(JNIEnv *, jobject, jfieldID, void *buffer);
379};
380
381struct TypeCache {
382    int fieldCount;
383    int size;
384    TypeFieldCache fields[1];
385};
386
387//{"nTypeFinalDestroy",              "(Landroid/renderscript/Type;)V",       (void*)nTypeFinalDestroy },
388static void
389nTypeFinalDestroy(JNIEnv *_env, jobject _this, jobject _type)
390{
391    TypeCache *tc = (TypeCache *)_env->GetIntField(_type, gTypeNativeCache);
392    free(tc);
393}
394
395// native void nTypeSetupFields(Type t, int[] types, int[] bits, Field[] IDs);
396static void
397nTypeSetupFields(JNIEnv *_env, jobject _this, jobject _type, jintArray _types, jintArray _bits, jobjectArray _IDs)
398{
399    int fieldCount = _env->GetArrayLength(_types);
400    size_t structSize = sizeof(TypeCache) + (sizeof(TypeFieldCache) * (fieldCount-1));
401    TypeCache *tc = (TypeCache *)malloc(structSize);
402    memset(tc, 0, structSize);
403
404    TypeFieldCache *tfc = &tc->fields[0];
405    tc->fieldCount = fieldCount;
406    _env->SetIntField(_type, gTypeNativeCache, (jint)tc);
407
408    jint *fType = _env->GetIntArrayElements(_types, NULL);
409    jint *fBits = _env->GetIntArrayElements(_bits, NULL);
410    for (int ct=0; ct < fieldCount; ct++) {
411        jobject field = _env->GetObjectArrayElement(_IDs, ct);
412        tfc[ct].field = _env->FromReflectedField(field);
413        tfc[ct].bits = fBits[ct];
414
415        switch(fType[ct]) {
416        case RS_TYPE_FLOAT_32:
417            tfc[ct].ptr = SF_LoadFloat;
418            tfc[ct].readPtr = SF_SaveFloat;
419            break;
420        case RS_TYPE_UNSIGNED_32:
421        case RS_TYPE_SIGNED_32:
422            tfc[ct].ptr = SF_LoadInt;
423            tfc[ct].readPtr = SF_SaveInt;
424            break;
425        case RS_TYPE_UNSIGNED_16:
426        case RS_TYPE_SIGNED_16:
427            tfc[ct].ptr = SF_LoadShort;
428            tfc[ct].readPtr = SF_SaveShort;
429            break;
430        case RS_TYPE_UNSIGNED_8:
431        case RS_TYPE_SIGNED_8:
432            tfc[ct].ptr = SF_LoadByte;
433            tfc[ct].readPtr = SF_SaveByte;
434            break;
435        }
436        tc->size += 4;
437    }
438
439    _env->ReleaseIntArrayElements(_types, fType, JNI_ABORT);
440    _env->ReleaseIntArrayElements(_bits, fBits, JNI_ABORT);
441}
442
443
444// -----------------------------------
445
446static jint
447nAllocationCreateTyped(JNIEnv *_env, jobject _this, jint e)
448{
449    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
450    LOG_API("nAllocationCreateTyped, con(%p), e(%p)", con, (RsElement)e);
451    return (jint) rsAllocationCreateTyped(con, (RsElement)e);
452}
453
454static void
455nAllocationUploadToTexture(JNIEnv *_env, jobject _this, jint a, jboolean genMip, jint mip)
456{
457    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
458    LOG_API("nAllocationUploadToTexture, con(%p), a(%p), genMip(%i), mip(%i)", con, (RsAllocation)a, genMip, mip);
459    rsAllocationUploadToTexture(con, (RsAllocation)a, genMip, mip);
460}
461
462static void
463nAllocationUploadToBufferObject(JNIEnv *_env, jobject _this, jint a)
464{
465    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
466    LOG_API("nAllocationUploadToBufferObject, con(%p), a(%p)", con, (RsAllocation)a);
467    rsAllocationUploadToBufferObject(con, (RsAllocation)a);
468}
469
470static RsElement SkBitmapToPredefined(SkBitmap::Config cfg)
471{
472    switch (cfg) {
473    case SkBitmap::kA8_Config:
474        return g_A_8;
475    case SkBitmap::kARGB_4444_Config:
476        return g_RGBA_4444;
477    case SkBitmap::kARGB_8888_Config:
478        return g_RGBA_8888;
479    case SkBitmap::kRGB_565_Config:
480        return g_RGB_565;
481
482    default:
483        break;
484    }
485    // If we don't have a conversion mark it as a user type.
486    LOGE("Unsupported bitmap type");
487    return NULL;
488}
489
490static int
491nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, jint dstFmt, jboolean genMips, jobject jbitmap)
492{
493    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
494    SkBitmap const * nativeBitmap =
495            (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID);
496    const SkBitmap& bitmap(*nativeBitmap);
497    SkBitmap::Config config = bitmap.getConfig();
498
499    RsElement e = SkBitmapToPredefined(config);
500    if (e) {
501        bitmap.lockPixels();
502        const int w = bitmap.width();
503        const int h = bitmap.height();
504        const void* ptr = bitmap.getPixels();
505        jint id = (jint)rsAllocationCreateFromBitmap(con, w, h, (RsElement)dstFmt, e, genMips, ptr);
506        bitmap.unlockPixels();
507        return id;
508    }
509    return 0;
510}
511
512static void ReleaseBitmapCallback(void *bmp)
513{
514    SkBitmap const * nativeBitmap = (SkBitmap const *)bmp;
515    nativeBitmap->unlockPixels();
516}
517
518static int
519nAllocationCreateBitmapRef(JNIEnv *_env, jobject _this, jint type, jobject jbitmap)
520{
521    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
522    SkBitmap * nativeBitmap =
523            (SkBitmap *)_env->GetIntField(jbitmap, gNativeBitmapID);
524
525
526    nativeBitmap->lockPixels();
527    void* ptr = nativeBitmap->getPixels();
528    jint id = (jint)rsAllocationCreateBitmapRef(con, (RsType)type, ptr, nativeBitmap, ReleaseBitmapCallback);
529    return id;
530}
531
532static int
533nAllocationCreateFromAssetStream(JNIEnv *_env, jobject _this, jint dstFmt, jboolean genMips, jint native_asset)
534{
535    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
536
537    Asset* asset = reinterpret_cast<Asset*>(native_asset);
538    SkBitmap bitmap;
539    SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
540            &bitmap, SkBitmap::kNo_Config, SkImageDecoder::kDecodePixels_Mode);
541
542    SkBitmap::Config config = bitmap.getConfig();
543
544    RsElement e = SkBitmapToPredefined(config);
545
546    if (e) {
547        bitmap.lockPixels();
548        const int w = bitmap.width();
549        const int h = bitmap.height();
550        const void* ptr = bitmap.getPixels();
551        jint id = (jint)rsAllocationCreateFromBitmap(con, w, h, (RsElement)dstFmt, e, genMips, ptr);
552        bitmap.unlockPixels();
553        return id;
554    }
555    return 0;
556}
557
558static int
559nAllocationCreateFromBitmapBoxed(JNIEnv *_env, jobject _this, jint dstFmt, jboolean genMips, jobject jbitmap)
560{
561    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
562    SkBitmap const * nativeBitmap =
563            (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID);
564    const SkBitmap& bitmap(*nativeBitmap);
565    SkBitmap::Config config = bitmap.getConfig();
566
567    RsElement e = SkBitmapToPredefined(config);
568
569    if (e) {
570        bitmap.lockPixels();
571        const int w = bitmap.width();
572        const int h = bitmap.height();
573        const void* ptr = bitmap.getPixels();
574        jint id = (jint)rsAllocationCreateFromBitmapBoxed(con, w, h, (RsElement)dstFmt, e, genMips, ptr);
575        bitmap.unlockPixels();
576        return id;
577    }
578    return 0;
579}
580
581
582static void
583nAllocationSubData1D_i(JNIEnv *_env, jobject _this, jint alloc, jint offset, jint count, jintArray data, int sizeBytes)
584{
585    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
586    jint len = _env->GetArrayLength(data);
587    LOG_API("nAllocation1DSubData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
588    jint *ptr = _env->GetIntArrayElements(data, NULL);
589    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
590    _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
591}
592
593static void
594nAllocationSubData1D_s(JNIEnv *_env, jobject _this, jint alloc, jint offset, jint count, jshortArray data, int sizeBytes)
595{
596    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
597    jint len = _env->GetArrayLength(data);
598    LOG_API("nAllocation1DSubData_s, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
599    jshort *ptr = _env->GetShortArrayElements(data, NULL);
600    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
601    _env->ReleaseShortArrayElements(data, ptr, JNI_ABORT);
602}
603
604static void
605nAllocationSubData1D_b(JNIEnv *_env, jobject _this, jint alloc, jint offset, jint count, jbyteArray data, int sizeBytes)
606{
607    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
608    jint len = _env->GetArrayLength(data);
609    LOG_API("nAllocation1DSubData_b, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
610    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
611    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
612    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
613}
614
615static void
616nAllocationSubData1D_f(JNIEnv *_env, jobject _this, jint alloc, jint offset, jint count, jfloatArray data, int sizeBytes)
617{
618    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
619    jint len = _env->GetArrayLength(data);
620    LOG_API("nAllocation1DSubData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
621    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
622    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
623    _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
624}
625
626static void
627nAllocationSubData2D_i(JNIEnv *_env, jobject _this, jint alloc, jint xoff, jint yoff, jint w, jint h, jintArray data, int sizeBytes)
628{
629    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
630    jint len = _env->GetArrayLength(data);
631    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);
632    jint *ptr = _env->GetIntArrayElements(data, NULL);
633    rsAllocation2DSubData(con, (RsAllocation)alloc, xoff, yoff, w, h, ptr, sizeBytes);
634    _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
635}
636
637static void
638nAllocationSubData2D_f(JNIEnv *_env, jobject _this, jint alloc, jint xoff, jint yoff, jint w, jint h, jfloatArray data, int sizeBytes)
639{
640    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
641    jint len = _env->GetArrayLength(data);
642    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);
643    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
644    rsAllocation2DSubData(con, (RsAllocation)alloc, xoff, yoff, w, h, ptr, sizeBytes);
645    _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
646}
647
648static void
649nAllocationRead_i(JNIEnv *_env, jobject _this, jint alloc, jintArray data)
650{
651    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
652    jint len = _env->GetArrayLength(data);
653    LOG_API("nAllocationRead_i, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
654    jint *ptr = _env->GetIntArrayElements(data, NULL);
655    rsAllocationRead(con, (RsAllocation)alloc, ptr);
656    _env->ReleaseIntArrayElements(data, ptr, 0);
657}
658
659static void
660nAllocationRead_f(JNIEnv *_env, jobject _this, jint alloc, jfloatArray data)
661{
662    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
663    jint len = _env->GetArrayLength(data);
664    LOG_API("nAllocationRead_f, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
665    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
666    rsAllocationRead(con, (RsAllocation)alloc, ptr);
667    _env->ReleaseFloatArrayElements(data, ptr, 0);
668}
669
670
671//{"nAllocationDataFromObject",      "(ILandroid/renderscript/Type;Ljava/lang/Object;)V",   (void*)nAllocationDataFromObject },
672static void
673nAllocationSubDataFromObject(JNIEnv *_env, jobject _this, jint alloc, jobject _type, jint offset, jobject _o)
674{
675    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
676    LOG_API("nAllocationDataFromObject con(%p), alloc(%p)", con, (RsAllocation)alloc);
677
678    const TypeCache *tc = (TypeCache *)_env->GetIntField(_type, gTypeNativeCache);
679
680    void * bufAlloc = malloc(tc->size);
681    void * buf = bufAlloc;
682    for (int ct=0; ct < tc->fieldCount; ct++) {
683        const TypeFieldCache *tfc = &tc->fields[ct];
684        buf = tfc->ptr(_env, _o, tfc->field, buf);
685    }
686    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, 1, bufAlloc, tc->size);
687    free(bufAlloc);
688}
689
690static void
691nAllocationSubReadFromObject(JNIEnv *_env, jobject _this, jint alloc, jobject _type, jint offset, jobject _o)
692{
693    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
694    LOG_API("nAllocationReadFromObject con(%p), alloc(%p)", con, (RsAllocation)alloc);
695
696    assert(offset == 0);
697
698    const TypeCache *tc = (TypeCache *)_env->GetIntField(_type, gTypeNativeCache);
699
700    void * bufAlloc = malloc(tc->size);
701    void * buf = bufAlloc;
702    rsAllocationRead(con, (RsAllocation)alloc, bufAlloc);
703
704    for (int ct=0; ct < tc->fieldCount; ct++) {
705        const TypeFieldCache *tfc = &tc->fields[ct];
706        buf = tfc->readPtr(_env, _o, tfc->field, buf);
707    }
708    free(bufAlloc);
709}
710
711// -----------------------------------
712
713static int
714nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, jint native_asset)
715{
716    LOGV("______nFileA3D %u", (uint32_t) native_asset);
717    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
718
719    Asset* asset = reinterpret_cast<Asset*>(native_asset);
720
721    jint id = (jint)rsFileA3DCreateFromAssetStream(con, asset->getBuffer(false), asset->getLength());
722    return id;
723}
724
725static int
726nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, jint fileA3D)
727{
728    LOGV("______nFileA3D %u", (uint32_t) fileA3D);
729    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
730
731    int32_t numEntries = 0;
732    rsFileA3DGetNumIndexEntries(con, &numEntries, (RsFile)fileA3D);
733    LOGV("______nFileA3D NumEntries %u", (uint32_t) numEntries);
734    return numEntries;
735}
736
737static void
738nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, jint fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
739{
740    LOGV("______nFileA3D %u", (uint32_t) fileA3D);
741    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
742
743    RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
744
745    rsFileA3DGetIndexEntries(con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
746
747    for(jint i = 0; i < numEntries; i ++) {
748        _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
749        _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
750    }
751
752    free(fileEntries);
753}
754
755static int
756nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, jint fileA3D, jint index)
757{
758    LOGV("______nFileA3D %u", (uint32_t) fileA3D);
759    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
760
761    jint id = (jint)rsFileA3DGetEntryByIndex(con, (uint32_t)index, (RsFile)fileA3D);
762    return id;
763}
764
765// -----------------------------------
766
767static void
768nAdapter1DBindAllocation(JNIEnv *_env, jobject _this, jint adapter, jint alloc)
769{
770    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
771    LOG_API("nAdapter1DBindAllocation, con(%p), adapter(%p), alloc(%p)", con, (RsAdapter1D)adapter, (RsAllocation)alloc);
772    rsAdapter1DBindAllocation(con, (RsAdapter1D)adapter, (RsAllocation)alloc);
773}
774
775static void
776nAdapter1DSetConstraint(JNIEnv *_env, jobject _this, jint adapter, jint dim, jint value)
777{
778    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
779    LOG_API("nAdapter1DSetConstraint, con(%p), adapter(%p), dim(%i), value(%i)", con, (RsAdapter1D)adapter, dim, value);
780    rsAdapter1DSetConstraint(con, (RsAdapter1D)adapter, (RsDimension)dim, value);
781}
782
783static void
784nAdapter1DData_i(JNIEnv *_env, jobject _this, jint adapter, jintArray data)
785{
786    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
787    jint len = _env->GetArrayLength(data);
788    LOG_API("nAdapter1DData_i, con(%p), adapter(%p), len(%i)", con, (RsAdapter1D)adapter, len);
789    jint *ptr = _env->GetIntArrayElements(data, NULL);
790    rsAdapter1DData(con, (RsAdapter1D)adapter, ptr);
791    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
792}
793
794static void
795nAdapter1DSubData_i(JNIEnv *_env, jobject _this, jint adapter, jint offset, jint count, jintArray data)
796{
797    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
798    jint len = _env->GetArrayLength(data);
799    LOG_API("nAdapter1DSubData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAdapter1D)adapter, offset, count, len);
800    jint *ptr = _env->GetIntArrayElements(data, NULL);
801    rsAdapter1DSubData(con, (RsAdapter1D)adapter, offset, count, ptr);
802    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
803}
804
805static void
806nAdapter1DData_f(JNIEnv *_env, jobject _this, jint adapter, jfloatArray data)
807{
808    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
809    jint len = _env->GetArrayLength(data);
810    LOG_API("nAdapter1DData_f, con(%p), adapter(%p), len(%i)", con, (RsAdapter1D)adapter, len);
811    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
812    rsAdapter1DData(con, (RsAdapter1D)adapter, ptr);
813    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
814}
815
816static void
817nAdapter1DSubData_f(JNIEnv *_env, jobject _this, jint adapter, jint offset, jint count, jfloatArray data)
818{
819    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
820    jint len = _env->GetArrayLength(data);
821    LOG_API("nAdapter1DSubData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAdapter1D)adapter, offset, count, len);
822    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
823    rsAdapter1DSubData(con, (RsAdapter1D)adapter, offset, count, ptr);
824    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
825}
826
827static jint
828nAdapter1DCreate(JNIEnv *_env, jobject _this)
829{
830    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
831    LOG_API("nAdapter1DCreate, con(%p)", con);
832    return (jint)rsAdapter1DCreate(con);
833}
834
835// -----------------------------------
836
837static void
838nAdapter2DBindAllocation(JNIEnv *_env, jobject _this, jint adapter, jint alloc)
839{
840    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
841    LOG_API("nAdapter2DBindAllocation, con(%p), adapter(%p), alloc(%p)", con, (RsAdapter2D)adapter, (RsAllocation)alloc);
842    rsAdapter2DBindAllocation(con, (RsAdapter2D)adapter, (RsAllocation)alloc);
843}
844
845static void
846nAdapter2DSetConstraint(JNIEnv *_env, jobject _this, jint adapter, jint dim, jint value)
847{
848    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
849    LOG_API("nAdapter2DSetConstraint, con(%p), adapter(%p), dim(%i), value(%i)", con, (RsAdapter2D)adapter, dim, value);
850    rsAdapter2DSetConstraint(con, (RsAdapter2D)adapter, (RsDimension)dim, value);
851}
852
853static void
854nAdapter2DData_i(JNIEnv *_env, jobject _this, jint adapter, jintArray data)
855{
856    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
857    jint len = _env->GetArrayLength(data);
858    LOG_API("nAdapter2DData_i, con(%p), adapter(%p), len(%i)", con, (RsAdapter2D)adapter, len);
859    jint *ptr = _env->GetIntArrayElements(data, NULL);
860    rsAdapter2DData(con, (RsAdapter2D)adapter, ptr);
861    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
862}
863
864static void
865nAdapter2DData_f(JNIEnv *_env, jobject _this, jint adapter, jfloatArray data)
866{
867    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
868    jint len = _env->GetArrayLength(data);
869    LOG_API("nAdapter2DData_f, con(%p), adapter(%p), len(%i)", con, (RsAdapter2D)adapter, len);
870    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
871    rsAdapter2DData(con, (RsAdapter2D)adapter, ptr);
872    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
873}
874
875static void
876nAdapter2DSubData_i(JNIEnv *_env, jobject _this, jint adapter, jint xoff, jint yoff, jint w, jint h, jintArray data)
877{
878    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
879    jint len = _env->GetArrayLength(data);
880    LOG_API("nAdapter2DSubData_i, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)",
881            con, (RsAdapter2D)adapter, xoff, yoff, w, h, len);
882    jint *ptr = _env->GetIntArrayElements(data, NULL);
883    rsAdapter2DSubData(con, (RsAdapter2D)adapter, xoff, yoff, w, h, ptr);
884    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
885}
886
887static void
888nAdapter2DSubData_f(JNIEnv *_env, jobject _this, jint adapter, jint xoff, jint yoff, jint w, jint h, jfloatArray data)
889{
890    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
891    jint len = _env->GetArrayLength(data);
892    LOG_API("nAdapter2DSubData_f, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)",
893            con, (RsAdapter2D)adapter, xoff, yoff, w, h, len);
894    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
895    rsAdapter2DSubData(con, (RsAdapter1D)adapter, xoff, yoff, w, h, ptr);
896    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
897}
898
899static jint
900nAdapter2DCreate(JNIEnv *_env, jobject _this)
901{
902    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
903    LOG_API("nAdapter2DCreate, con(%p)", con);
904    return (jint)rsAdapter2DCreate(con);
905}
906
907// -----------------------------------
908
909static void
910nScriptBindAllocation(JNIEnv *_env, jobject _this, jint script, jint alloc, jint slot)
911{
912    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
913    LOG_API("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", con, (RsScript)script, (RsAllocation)alloc, slot);
914    rsScriptBindAllocation(con, (RsScript)script, (RsAllocation)alloc, slot);
915}
916
917static void
918nScriptSetVarI(JNIEnv *_env, jobject _this, jint script, jint slot, jint val)
919{
920    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
921    LOG_API("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i), b(%f), a(%f)", con, (void *)script, slot, val);
922    rsScriptSetVarI(con, (RsScript)script, slot, val);
923}
924
925static void
926nScriptSetVarF(JNIEnv *_env, jobject _this, jint script, jint slot, float val)
927{
928    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
929    LOG_API("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i), b(%f), a(%f)", con, (void *)script, slot, val);
930    rsScriptSetVarF(con, (RsScript)script, slot, val);
931}
932
933static void
934nScriptSetVarV(JNIEnv *_env, jobject _this, jint script, jint slot, jbyteArray data)
935{
936    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
937    LOG_API("nScriptSetVarV, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
938    jint len = _env->GetArrayLength(data);
939    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
940    rsScriptSetVarV(con, (RsScript)script, slot, ptr, len);
941    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
942}
943
944
945static void
946nScriptSetTimeZone(JNIEnv *_env, jobject _this, jint script, jbyteArray timeZone)
947{
948    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
949    LOG_API("nScriptCSetTimeZone, con(%p), s(%p), timeZone(%s)", con, (void *)script, (const char *)timeZone);
950
951    jint length = _env->GetArrayLength(timeZone);
952    jbyte* timeZone_ptr;
953    timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
954
955    rsScriptSetTimeZone(con, (RsScript)script, (const char *)timeZone_ptr, length);
956
957    if (timeZone_ptr) {
958        _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
959    }
960}
961
962static void
963nScriptInvoke(JNIEnv *_env, jobject _this, jint obj, jint slot)
964{
965    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
966    LOG_API("nScriptInvoke, con(%p), script(%p)", con, (void *)obj);
967    rsScriptInvoke(con, (RsScript)obj, slot);
968}
969
970static void
971nScriptInvokeV(JNIEnv *_env, jobject _this, jint script, jint slot, jbyteArray data)
972{
973    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
974    LOG_API("nScriptInvokeV, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
975    jint len = _env->GetArrayLength(data);
976    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
977    rsScriptInvokeV(con, (RsScript)script, slot, ptr, len);
978    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
979}
980
981
982// -----------------------------------
983
984static void
985nScriptCBegin(JNIEnv *_env, jobject _this)
986{
987    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
988    LOG_API("nScriptCBegin, con(%p)", con);
989    rsScriptCBegin(con);
990}
991
992static void
993nScriptCSetScript(JNIEnv *_env, jobject _this, jbyteArray scriptRef,
994                  jint offset, jint length)
995{
996    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
997    LOG_API("!!! nScriptCSetScript, con(%p)", con);
998    jint _exception = 0;
999    jint remaining;
1000    jbyte* script_base = 0;
1001    jbyte* script_ptr;
1002    if (!scriptRef) {
1003        _exception = 1;
1004        //_env->ThrowNew(IAEClass, "script == null");
1005        goto exit;
1006    }
1007    if (offset < 0) {
1008        _exception = 1;
1009        //_env->ThrowNew(IAEClass, "offset < 0");
1010        goto exit;
1011    }
1012    if (length < 0) {
1013        _exception = 1;
1014        //_env->ThrowNew(IAEClass, "length < 0");
1015        goto exit;
1016    }
1017    remaining = _env->GetArrayLength(scriptRef) - offset;
1018    if (remaining < length) {
1019        _exception = 1;
1020        //_env->ThrowNew(IAEClass, "length > script.length - offset");
1021        goto exit;
1022    }
1023    script_base = (jbyte *)
1024        _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
1025    script_ptr = script_base + offset;
1026
1027    rsScriptCSetText(con, (const char *)script_ptr, length);
1028
1029exit:
1030    if (script_base) {
1031        _env->ReleasePrimitiveArrayCritical(scriptRef, script_base,
1032                _exception ? JNI_ABORT: 0);
1033    }
1034}
1035
1036static jint
1037nScriptCCreate(JNIEnv *_env, jobject _this)
1038{
1039    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1040    LOG_API("nScriptCCreate, con(%p)", con);
1041    return (jint)rsScriptCCreate(con);
1042}
1043
1044// ---------------------------------------------------------------------------
1045
1046static void
1047nProgramStoreBegin(JNIEnv *_env, jobject _this, jint in, jint out)
1048{
1049    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1050    LOG_API("nProgramStoreBegin, con(%p), in(%p), out(%p)", con, (RsElement)in, (RsElement)out);
1051    rsProgramStoreBegin(con, (RsElement)in, (RsElement)out);
1052}
1053
1054static void
1055nProgramStoreDepthFunc(JNIEnv *_env, jobject _this, jint func)
1056{
1057    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1058    LOG_API("nProgramStoreDepthFunc, con(%p), func(%i)", con, func);
1059    rsProgramStoreDepthFunc(con, (RsDepthFunc)func);
1060}
1061
1062static void
1063nProgramStoreDepthMask(JNIEnv *_env, jobject _this, jboolean enable)
1064{
1065    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1066    LOG_API("nProgramStoreDepthMask, con(%p), enable(%i)", con, enable);
1067    rsProgramStoreDepthMask(con, enable);
1068}
1069
1070static void
1071nProgramStoreColorMask(JNIEnv *_env, jobject _this, jboolean r, jboolean g, jboolean b, jboolean a)
1072{
1073    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1074    LOG_API("nProgramStoreColorMask, con(%p), r(%i), g(%i), b(%i), a(%i)", con, r, g, b, a);
1075    rsProgramStoreColorMask(con, r, g, b, a);
1076}
1077
1078static void
1079nProgramStoreBlendFunc(JNIEnv *_env, jobject _this, int src, int dst)
1080{
1081    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1082    LOG_API("nProgramStoreBlendFunc, con(%p), src(%i), dst(%i)", con, src, dst);
1083    rsProgramStoreBlendFunc(con, (RsBlendSrcFunc)src, (RsBlendDstFunc)dst);
1084}
1085
1086static void
1087nProgramStoreDither(JNIEnv *_env, jobject _this, jboolean enable)
1088{
1089    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1090    LOG_API("nProgramStoreDither, con(%p), enable(%i)", con, enable);
1091    rsProgramStoreDither(con, enable);
1092}
1093
1094static jint
1095nProgramStoreCreate(JNIEnv *_env, jobject _this)
1096{
1097    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1098    LOG_API("nProgramStoreCreate, con(%p)", con);
1099
1100    return (jint)rsProgramStoreCreate(con);
1101}
1102
1103// ---------------------------------------------------------------------------
1104
1105static void
1106nProgramBindConstants(JNIEnv *_env, jobject _this, jint vpv, jint slot, jint a)
1107{
1108    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1109    LOG_API("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", con, (RsProgramVertex)vpv, slot, (RsAllocation)a);
1110    rsProgramBindConstants(con, (RsProgram)vpv, slot, (RsAllocation)a);
1111}
1112
1113static void
1114nProgramBindTexture(JNIEnv *_env, jobject _this, jint vpf, jint slot, jint a)
1115{
1116    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1117    LOG_API("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
1118    rsProgramBindTexture(con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
1119}
1120
1121static void
1122nProgramBindSampler(JNIEnv *_env, jobject _this, jint vpf, jint slot, jint a)
1123{
1124    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1125    LOG_API("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsSampler)a);
1126    rsProgramBindSampler(con, (RsProgramFragment)vpf, slot, (RsSampler)a);
1127}
1128
1129// ---------------------------------------------------------------------------
1130
1131static jint
1132nProgramFragmentCreate(JNIEnv *_env, jobject _this, jintArray params)
1133{
1134    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1135    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1136    jint paramLen = _env->GetArrayLength(params);
1137
1138    LOG_API("nProgramFragmentCreate, con(%p), paramLen(%i)", con, paramLen);
1139
1140    jint ret = (jint)rsProgramFragmentCreate(con, (uint32_t *)paramPtr, paramLen);
1141    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1142    return ret;
1143}
1144
1145static jint
1146nProgramFragmentCreate2(JNIEnv *_env, jobject _this, jstring shader, jintArray params)
1147{
1148    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1149    const char* shaderUTF = _env->GetStringUTFChars(shader, NULL);
1150    jint shaderLen = _env->GetStringUTFLength(shader);
1151    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1152    jint paramLen = _env->GetArrayLength(params);
1153
1154    LOG_API("nProgramFragmentCreate2, con(%p), shaderLen(%i), paramLen(%i)", con, shaderLen, paramLen);
1155
1156    jint ret = (jint)rsProgramFragmentCreate2(con, shaderUTF, shaderLen, (uint32_t *)paramPtr, paramLen);
1157    _env->ReleaseStringUTFChars(shader, shaderUTF);
1158    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1159    return ret;
1160}
1161
1162
1163// ---------------------------------------------------------------------------
1164
1165static jint
1166nProgramVertexCreate(JNIEnv *_env, jobject _this, jboolean texMat)
1167{
1168    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1169    LOG_API("nProgramVertexCreate, con(%p), texMat(%i)", con, texMat);
1170    return (jint)rsProgramVertexCreate(con, texMat);
1171}
1172
1173static jint
1174nProgramVertexCreate2(JNIEnv *_env, jobject _this, jstring shader, jintArray params)
1175{
1176    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1177    const char* shaderUTF = _env->GetStringUTFChars(shader, NULL);
1178    jint shaderLen = _env->GetStringUTFLength(shader);
1179    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1180    jint paramLen = _env->GetArrayLength(params);
1181
1182    LOG_API("nProgramVertexCreate2, con(%p), shaderLen(%i), paramLen(%i)", con, shaderLen, paramLen);
1183
1184    jint ret = (jint)rsProgramVertexCreate2(con, shaderUTF, shaderLen, (uint32_t *)paramPtr, paramLen);
1185    _env->ReleaseStringUTFChars(shader, shaderUTF);
1186    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1187    return ret;
1188}
1189
1190// ---------------------------------------------------------------------------
1191
1192static jint
1193nProgramRasterCreate(JNIEnv *_env, jobject _this, jint in, jint out,
1194                     jboolean pointSmooth, jboolean lineSmooth, jboolean pointSprite)
1195{
1196    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1197    LOG_API("nProgramRasterCreate, con(%p), in(%p), out(%p), pointSmooth(%i), lineSmooth(%i), pointSprite(%i)",
1198            con, (RsElement)in, (RsElement)out, pointSmooth, lineSmooth, pointSprite);
1199    return (jint)rsProgramRasterCreate(con, (RsElement)in, (RsElement)out, pointSmooth, lineSmooth, pointSprite);
1200}
1201
1202static void
1203nProgramRasterSetPointSize(JNIEnv *_env, jobject _this, jint vpr, jfloat v)
1204{
1205    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1206    LOG_API("nProgramRasterSetPointSize, con(%p), vpf(%p), value(%f)", con, (RsProgramRaster)vpr, v);
1207    rsProgramRasterSetPointSize(con, (RsProgramFragment)vpr, v);
1208}
1209
1210static void
1211nProgramRasterSetLineWidth(JNIEnv *_env, jobject _this, jint vpr, jfloat v)
1212{
1213    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1214    LOG_API("nProgramRasterSetLineWidth, con(%p), vpf(%p), value(%f)", con, (RsProgramRaster)vpr, v);
1215    rsProgramRasterSetLineWidth(con, (RsProgramFragment)vpr, v);
1216}
1217
1218
1219// ---------------------------------------------------------------------------
1220
1221static void
1222nContextBindRootScript(JNIEnv *_env, jobject _this, jint script)
1223{
1224    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1225    LOG_API("nContextBindRootScript, con(%p), script(%p)", con, (RsScript)script);
1226    rsContextBindRootScript(con, (RsScript)script);
1227}
1228
1229static void
1230nContextBindProgramStore(JNIEnv *_env, jobject _this, jint pfs)
1231{
1232    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1233    LOG_API("nContextBindProgramStore, con(%p), pfs(%p)", con, (RsProgramStore)pfs);
1234    rsContextBindProgramStore(con, (RsProgramStore)pfs);
1235}
1236
1237static void
1238nContextBindProgramFragment(JNIEnv *_env, jobject _this, jint pf)
1239{
1240    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1241    LOG_API("nContextBindProgramFragment, con(%p), pf(%p)", con, (RsProgramFragment)pf);
1242    rsContextBindProgramFragment(con, (RsProgramFragment)pf);
1243}
1244
1245static void
1246nContextBindProgramVertex(JNIEnv *_env, jobject _this, jint pf)
1247{
1248    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1249    LOG_API("nContextBindProgramVertex, con(%p), pf(%p)", con, (RsProgramVertex)pf);
1250    rsContextBindProgramVertex(con, (RsProgramVertex)pf);
1251}
1252
1253static void
1254nContextBindProgramRaster(JNIEnv *_env, jobject _this, jint pf)
1255{
1256    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1257    LOG_API("nContextBindProgramRaster, con(%p), pf(%p)", con, (RsProgramRaster)pf);
1258    rsContextBindProgramRaster(con, (RsProgramRaster)pf);
1259}
1260
1261
1262// ---------------------------------------------------------------------------
1263
1264static void
1265nSamplerBegin(JNIEnv *_env, jobject _this)
1266{
1267    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1268    LOG_API("nSamplerBegin, con(%p)", con);
1269    rsSamplerBegin(con);
1270}
1271
1272static void
1273nSamplerSet(JNIEnv *_env, jobject _this, jint p, jint v)
1274{
1275    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1276    LOG_API("nSamplerSet, con(%p), param(%i), value(%i)", con, p, v);
1277    rsSamplerSet(con, (RsSamplerParam)p, (RsSamplerValue)v);
1278}
1279
1280static jint
1281nSamplerCreate(JNIEnv *_env, jobject _this)
1282{
1283    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1284    LOG_API("nSamplerCreate, con(%p)", con);
1285    return (jint)rsSamplerCreate(con);
1286}
1287
1288// ---------------------------------------------------------------------------
1289
1290static void
1291nLightBegin(JNIEnv *_env, jobject _this)
1292{
1293    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1294    LOG_API("nLightBegin, con(%p)", con);
1295    rsLightBegin(con);
1296}
1297
1298static void
1299nLightSetIsMono(JNIEnv *_env, jobject _this, jboolean isMono)
1300{
1301    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1302    LOG_API("nLightSetIsMono, con(%p), isMono(%i)", con, isMono);
1303    rsLightSetMonochromatic(con, isMono);
1304}
1305
1306static void
1307nLightSetIsLocal(JNIEnv *_env, jobject _this, jboolean isLocal)
1308{
1309    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1310    LOG_API("nLightSetIsLocal, con(%p), isLocal(%i)", con, isLocal);
1311    rsLightSetLocal(con, isLocal);
1312}
1313
1314static jint
1315nLightCreate(JNIEnv *_env, jobject _this)
1316{
1317    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1318    LOG_API("nLightCreate, con(%p)", con);
1319    return (jint)rsLightCreate(con);
1320}
1321
1322static void
1323nLightSetColor(JNIEnv *_env, jobject _this, jint light, float r, float g, float b)
1324{
1325    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1326    LOG_API("nLightSetColor, con(%p), light(%p), r(%f), g(%f), b(%f)", con, (RsLight)light, r, g, b);
1327    rsLightSetColor(con, (RsLight)light, r, g, b);
1328}
1329
1330static void
1331nLightSetPosition(JNIEnv *_env, jobject _this, jint light, float x, float y, float z)
1332{
1333    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1334    LOG_API("nLightSetPosition, con(%p), light(%p), x(%f), y(%f), z(%f)", con, (RsLight)light, x, y, z);
1335    rsLightSetPosition(con, (RsLight)light, x, y, z);
1336}
1337
1338// ---------------------------------------------------------------------------
1339
1340static jint
1341nSimpleMeshCreate(JNIEnv *_env, jobject _this, jint batchID, jint indexID, jintArray vtxIDs, jint primID)
1342{
1343    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1344    jint len = _env->GetArrayLength(vtxIDs);
1345    LOG_API("nSimpleMeshCreate, con(%p), batchID(%i), indexID(%i), vtxIDs.len(%i), primID(%i)",
1346            con, batchID, indexID, len, primID);
1347    jint *ptr = _env->GetIntArrayElements(vtxIDs, NULL);
1348    int id = (int)rsSimpleMeshCreate(con, (void *)batchID, (void *)indexID, (void **)ptr, len, primID);
1349    _env->ReleaseIntArrayElements(vtxIDs, ptr, 0/*JNI_ABORT*/);
1350    return id;
1351}
1352
1353static void
1354nSimpleMeshBindVertex(JNIEnv *_env, jobject _this, jint s, jint alloc, jint slot)
1355{
1356    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1357    LOG_API("nSimpleMeshBindVertex, con(%p), SimpleMesh(%p), Alloc(%p), slot(%i)", con, (RsSimpleMesh)s, (RsAllocation)alloc, slot);
1358    rsSimpleMeshBindVertex(con, (RsSimpleMesh)s, (RsAllocation)alloc, slot);
1359}
1360
1361static void
1362nSimpleMeshBindIndex(JNIEnv *_env, jobject _this, jint s, jint alloc)
1363{
1364    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1365    LOG_API("nSimpleMeshBindIndex, con(%p), SimpleMesh(%p), Alloc(%p)", con, (RsSimpleMesh)s, (RsAllocation)alloc);
1366    rsSimpleMeshBindIndex(con, (RsSimpleMesh)s, (RsAllocation)alloc);
1367}
1368
1369// ---------------------------------------------------------------------------
1370
1371
1372static const char *classPathName = "android/renderscript/RenderScript";
1373
1374static JNINativeMethod methods[] = {
1375{"_nInit",                         "()V",                                  (void*)_nInit },
1376{"nInitElements",                  "(IIII)V",                              (void*)nInitElements },
1377
1378{"nDeviceCreate",                  "()I",                                  (void*)nDeviceCreate },
1379{"nDeviceDestroy",                 "(I)V",                                 (void*)nDeviceDestroy },
1380{"nDeviceSetConfig",               "(III)V",                               (void*)nDeviceSetConfig },
1381{"nContextCreate",                 "(II)I",                                (void*)nContextCreate },
1382{"nContextCreateGL",               "(IIZ)I",                               (void*)nContextCreateGL },
1383{"nContextFinish",                 "()V",                                  (void*)nContextFinish },
1384{"nContextSetPriority",            "(I)V",                                 (void*)nContextSetPriority },
1385{"nContextSetSurface",             "(IILandroid/view/Surface;)V",          (void*)nContextSetSurface },
1386{"nContextDestroy",                "(I)V",                                 (void*)nContextDestroy },
1387{"nContextDump",                   "(I)V",                                 (void*)nContextDump },
1388{"nContextPause",                  "()V",                                  (void*)nContextPause },
1389{"nContextResume",                 "()V",                                  (void*)nContextResume },
1390{"nAssignName",                    "(I[B)V",                               (void*)nAssignName },
1391{"nObjDestroy",                    "(I)V",                                 (void*)nObjDestroy },
1392{"nObjDestroyOOB",                 "(I)V",                                 (void*)nObjDestroyOOB },
1393{"nContextGetMessage",             "([IZ)I",                               (void*)nContextGetMessage },
1394{"nContextInitToClient",           "()V",                                  (void*)nContextInitToClient },
1395{"nContextDeinitToClient",         "()V",                                  (void*)nContextDeinitToClient },
1396
1397{"nFileOpen",                      "([B)I",                                (void*)nFileOpen },
1398
1399{"nElementCreate",                 "(IIZI)I",                              (void*)nElementCreate },
1400{"nElementCreate2",                "([I[Ljava/lang/String;)I",             (void*)nElementCreate2 },
1401
1402{"nTypeBegin",                     "(I)V",                                 (void*)nTypeBegin },
1403{"nTypeAdd",                       "(II)V",                                (void*)nTypeAdd },
1404{"nTypeCreate",                    "()I",                                  (void*)nTypeCreate },
1405{"nTypeFinalDestroy",              "(Landroid/renderscript/Type;)V",       (void*)nTypeFinalDestroy },
1406{"nTypeSetupFields",               "(Landroid/renderscript/Type;[I[I[Ljava/lang/reflect/Field;)V", (void*)nTypeSetupFields },
1407
1408{"nAllocationCreateTyped",         "(I)I",                                 (void*)nAllocationCreateTyped },
1409{"nAllocationCreateFromBitmap",    "(IZLandroid/graphics/Bitmap;)I",       (void*)nAllocationCreateFromBitmap },
1410{"nAllocationCreateBitmapRef",     "(ILandroid/graphics/Bitmap;)I",        (void*)nAllocationCreateBitmapRef },
1411{"nAllocationCreateFromBitmapBoxed","(IZLandroid/graphics/Bitmap;)I",      (void*)nAllocationCreateFromBitmapBoxed },
1412{"nAllocationCreateFromAssetStream","(IZI)I",                              (void*)nAllocationCreateFromAssetStream },
1413{"nAllocationUploadToTexture",     "(IZI)V",                               (void*)nAllocationUploadToTexture },
1414{"nAllocationUploadToBufferObject","(I)V",                                 (void*)nAllocationUploadToBufferObject },
1415{"nAllocationSubData1D",           "(III[II)V",                            (void*)nAllocationSubData1D_i },
1416{"nAllocationSubData1D",           "(III[SI)V",                            (void*)nAllocationSubData1D_s },
1417{"nAllocationSubData1D",           "(III[BI)V",                            (void*)nAllocationSubData1D_b },
1418{"nAllocationSubData1D",           "(III[FI)V",                            (void*)nAllocationSubData1D_f },
1419{"nAllocationSubData2D",           "(IIIII[II)V",                          (void*)nAllocationSubData2D_i },
1420{"nAllocationSubData2D",           "(IIIII[FI)V",                          (void*)nAllocationSubData2D_f },
1421{"nAllocationRead",                "(I[I)V",                               (void*)nAllocationRead_i },
1422{"nAllocationRead",                "(I[F)V",                               (void*)nAllocationRead_f },
1423{"nAllocationSubDataFromObject",   "(ILandroid/renderscript/Type;ILjava/lang/Object;)V",   (void*)nAllocationSubDataFromObject },
1424{"nAllocationSubReadFromObject",   "(ILandroid/renderscript/Type;ILjava/lang/Object;)V",   (void*)nAllocationSubReadFromObject },
1425
1426{"nAdapter1DBindAllocation",       "(II)V",                                (void*)nAdapter1DBindAllocation },
1427{"nAdapter1DSetConstraint",        "(III)V",                               (void*)nAdapter1DSetConstraint },
1428{"nAdapter1DData",                 "(I[I)V",                               (void*)nAdapter1DData_i },
1429{"nAdapter1DData",                 "(I[F)V",                               (void*)nAdapter1DData_f },
1430{"nAdapter1DSubData",              "(III[I)V",                             (void*)nAdapter1DSubData_i },
1431{"nAdapter1DSubData",              "(III[F)V",                             (void*)nAdapter1DSubData_f },
1432{"nAdapter1DCreate",               "()I",                                  (void*)nAdapter1DCreate },
1433
1434{"nAdapter2DBindAllocation",       "(II)V",                                (void*)nAdapter2DBindAllocation },
1435{"nAdapter2DSetConstraint",        "(III)V",                               (void*)nAdapter2DSetConstraint },
1436{"nAdapter2DData",                 "(I[I)V",                               (void*)nAdapter2DData_i },
1437{"nAdapter2DData",                 "(I[F)V",                               (void*)nAdapter2DData_f },
1438{"nAdapter2DSubData",              "(IIIII[I)V",                           (void*)nAdapter2DSubData_i },
1439{"nAdapter2DSubData",              "(IIIII[F)V",                           (void*)nAdapter2DSubData_f },
1440{"nAdapter2DCreate",               "()I",                                  (void*)nAdapter2DCreate },
1441
1442{"nScriptBindAllocation",          "(III)V",                               (void*)nScriptBindAllocation },
1443{"nScriptSetTimeZone",             "(I[B)V",                               (void*)nScriptSetTimeZone },
1444{"nScriptInvoke",                  "(II)V",                                (void*)nScriptInvoke },
1445{"nScriptInvokeV",                 "(II[B)V",                              (void*)nScriptInvokeV },
1446{"nScriptSetVarI",                 "(III)V",                               (void*)nScriptSetVarI },
1447{"nScriptSetVarF",                 "(IIF)V",                               (void*)nScriptSetVarF },
1448{"nScriptSetVarV",                 "(II[B)V",                              (void*)nScriptSetVarV },
1449
1450{"nScriptCBegin",                  "()V",                                  (void*)nScriptCBegin },
1451{"nScriptCSetScript",              "([BII)V",                              (void*)nScriptCSetScript },
1452{"nScriptCCreate",                 "()I",                                  (void*)nScriptCCreate },
1453
1454{"nProgramStoreBegin",             "(II)V",                                (void*)nProgramStoreBegin },
1455{"nProgramStoreDepthFunc",         "(I)V",                                 (void*)nProgramStoreDepthFunc },
1456{"nProgramStoreDepthMask",         "(Z)V",                                 (void*)nProgramStoreDepthMask },
1457{"nProgramStoreColorMask",         "(ZZZZ)V",                              (void*)nProgramStoreColorMask },
1458{"nProgramStoreBlendFunc",         "(II)V",                                (void*)nProgramStoreBlendFunc },
1459{"nProgramStoreDither",            "(Z)V",                                 (void*)nProgramStoreDither },
1460{"nProgramStoreCreate",            "()I",                                  (void*)nProgramStoreCreate },
1461
1462{"nProgramBindConstants",          "(III)V",                               (void*)nProgramBindConstants },
1463{"nProgramBindTexture",            "(III)V",                               (void*)nProgramBindTexture },
1464{"nProgramBindSampler",            "(III)V",                               (void*)nProgramBindSampler },
1465
1466{"nProgramFragmentCreate",         "([I)I",                                (void*)nProgramFragmentCreate },
1467{"nProgramFragmentCreate2",        "(Ljava/lang/String;[I)I",              (void*)nProgramFragmentCreate2 },
1468
1469{"nProgramRasterCreate",           "(IIZZZ)I",                             (void*)nProgramRasterCreate },
1470{"nProgramRasterSetPointSize",     "(IF)V",                                (void*)nProgramRasterSetPointSize },
1471{"nProgramRasterSetLineWidth",     "(IF)V",                                (void*)nProgramRasterSetLineWidth },
1472
1473{"nProgramVertexCreate",           "(Z)I",                                 (void*)nProgramVertexCreate },
1474{"nProgramVertexCreate2",          "(Ljava/lang/String;[I)I",              (void*)nProgramVertexCreate2 },
1475
1476{"nLightBegin",                    "()V",                                  (void*)nLightBegin },
1477{"nLightSetIsMono",                "(Z)V",                                 (void*)nLightSetIsMono },
1478{"nLightSetIsLocal",               "(Z)V",                                 (void*)nLightSetIsLocal },
1479{"nLightCreate",                   "()I",                                  (void*)nLightCreate },
1480{"nLightSetColor",                 "(IFFF)V",                              (void*)nLightSetColor },
1481{"nLightSetPosition",              "(IFFF)V",                              (void*)nLightSetPosition },
1482
1483{"nContextBindRootScript",         "(I)V",                                 (void*)nContextBindRootScript },
1484{"nContextBindProgramStore",       "(I)V",                                (void*)nContextBindProgramStore },
1485{"nContextBindProgramFragment",    "(I)V",                                 (void*)nContextBindProgramFragment },
1486{"nContextBindProgramVertex",      "(I)V",                                 (void*)nContextBindProgramVertex },
1487{"nContextBindProgramRaster",      "(I)V",                                 (void*)nContextBindProgramRaster },
1488
1489{"nSamplerBegin",                  "()V",                                  (void*)nSamplerBegin },
1490{"nSamplerSet",                    "(II)V",                                (void*)nSamplerSet },
1491{"nSamplerCreate",                 "()I",                                  (void*)nSamplerCreate },
1492
1493{"nSimpleMeshCreate",              "(II[II)I",                             (void*)nSimpleMeshCreate },
1494{"nSimpleMeshBindVertex",          "(III)V",                               (void*)nSimpleMeshBindVertex },
1495{"nSimpleMeshBindIndex",           "(II)V",                                (void*)nSimpleMeshBindIndex },
1496
1497{"nFileA3DCreateFromAssetStream", "(I)I",                                 (void*)nFileA3DCreateFromAssetStream },
1498{"nFileA3DGetNumIndexEntries",     "(I)I",                                 (void*)nFileA3DGetNumIndexEntries },
1499{"nFileA3DGetIndexEntries",        "(II[I[Ljava/lang/String;)V",          (void*)nFileA3DGetIndexEntries },
1500{"nFileA3DGetEntryByIndex",        "(II)I",                                (void*)nFileA3DGetEntryByIndex },
1501
1502};
1503
1504static int registerFuncs(JNIEnv *_env)
1505{
1506    return android::AndroidRuntime::registerNativeMethods(
1507            _env, classPathName, methods, NELEM(methods));
1508}
1509
1510// ---------------------------------------------------------------------------
1511
1512jint JNI_OnLoad(JavaVM* vm, void* reserved)
1513{
1514    JNIEnv* env = NULL;
1515    jint result = -1;
1516
1517    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
1518        LOGE("ERROR: GetEnv failed\n");
1519        goto bail;
1520    }
1521    assert(env != NULL);
1522
1523    if (registerFuncs(env) < 0) {
1524        LOGE("ERROR: MediaPlayer native registration failed\n");
1525        goto bail;
1526    }
1527
1528    /* success -- return valid version number */
1529    result = JNI_VERSION_1_4;
1530
1531bail:
1532    return result;
1533}
1534