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