android_renderscript_RenderScript.cpp revision 4d3399337d18ef04116bc8a2e5799274655d0c30
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
1022static void
1023nScriptCAddDefineI32(JNIEnv *_env, jobject _this, jstring name, jint value)
1024{
1025    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1026    const char* n = _env->GetStringUTFChars(name, NULL);
1027    LOG_API("nScriptCAddDefineI32, con(%p) name(%s) value(%d)", con, n, value);
1028    rsScriptCSetDefineI32(con, n, value);
1029    _env->ReleaseStringUTFChars(name, n);
1030}
1031
1032static void
1033nScriptCAddDefineF(JNIEnv *_env, jobject _this, jstring name, jfloat value)
1034{
1035    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1036    const char* n = _env->GetStringUTFChars(name, NULL);
1037    LOG_API("nScriptCAddDefineF, con(%p) name(%s) value(%f)", con, n, value);
1038    rsScriptCSetDefineF(con, n, value);
1039    _env->ReleaseStringUTFChars(name, n);
1040}
1041
1042// ---------------------------------------------------------------------------
1043
1044static void
1045nProgramFragmentStoreBegin(JNIEnv *_env, jobject _this, jint in, jint out)
1046{
1047    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1048    LOG_API("nProgramFragmentStoreBegin, con(%p), in(%p), out(%p)", con, (RsElement)in, (RsElement)out);
1049    rsProgramFragmentStoreBegin(con, (RsElement)in, (RsElement)out);
1050}
1051
1052static void
1053nProgramFragmentStoreDepthFunc(JNIEnv *_env, jobject _this, jint func)
1054{
1055    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1056    LOG_API("nProgramFragmentStoreDepthFunc, con(%p), func(%i)", con, func);
1057    rsProgramFragmentStoreDepthFunc(con, (RsDepthFunc)func);
1058}
1059
1060static void
1061nProgramFragmentStoreDepthMask(JNIEnv *_env, jobject _this, jboolean enable)
1062{
1063    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1064    LOG_API("nProgramFragmentStoreDepthMask, con(%p), enable(%i)", con, enable);
1065    rsProgramFragmentStoreDepthMask(con, enable);
1066}
1067
1068static void
1069nProgramFragmentStoreColorMask(JNIEnv *_env, jobject _this, jboolean r, jboolean g, jboolean b, jboolean a)
1070{
1071    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1072    LOG_API("nProgramFragmentStoreColorMask, con(%p), r(%i), g(%i), b(%i), a(%i)", con, r, g, b, a);
1073    rsProgramFragmentStoreColorMask(con, r, g, b, a);
1074}
1075
1076static void
1077nProgramFragmentStoreBlendFunc(JNIEnv *_env, jobject _this, int src, int dst)
1078{
1079    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1080    LOG_API("nProgramFragmentStoreBlendFunc, con(%p), src(%i), dst(%i)", con, src, dst);
1081    rsProgramFragmentStoreBlendFunc(con, (RsBlendSrcFunc)src, (RsBlendDstFunc)dst);
1082}
1083
1084static void
1085nProgramFragmentStoreDither(JNIEnv *_env, jobject _this, jboolean enable)
1086{
1087    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1088    LOG_API("nProgramFragmentStoreDither, con(%p), enable(%i)", con, enable);
1089    rsProgramFragmentStoreDither(con, enable);
1090}
1091
1092static jint
1093nProgramFragmentStoreCreate(JNIEnv *_env, jobject _this)
1094{
1095    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1096    LOG_API("nProgramFragmentStoreCreate, con(%p)", con);
1097
1098    return (jint)rsProgramFragmentStoreCreate(con);
1099}
1100
1101// ---------------------------------------------------------------------------
1102
1103static void
1104nProgramBindConstants(JNIEnv *_env, jobject _this, jint vpv, jint slot, jint a)
1105{
1106    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1107    LOG_API("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", con, (RsProgramVertex)vpv, slot, (RsAllocation)a);
1108    rsProgramBindConstants(con, (RsProgram)vpv, slot, (RsAllocation)a);
1109}
1110
1111static void
1112nProgramBindTexture(JNIEnv *_env, jobject _this, jint vpf, jint slot, jint a)
1113{
1114    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1115    LOG_API("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
1116    rsProgramBindTexture(con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
1117}
1118
1119static void
1120nProgramBindSampler(JNIEnv *_env, jobject _this, jint vpf, jint slot, jint a)
1121{
1122    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1123    LOG_API("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsSampler)a);
1124    rsProgramBindSampler(con, (RsProgramFragment)vpf, slot, (RsSampler)a);
1125}
1126
1127// ---------------------------------------------------------------------------
1128
1129static jint
1130nProgramFragmentCreate(JNIEnv *_env, jobject _this, jintArray params)
1131{
1132    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1133    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1134    jint paramLen = _env->GetArrayLength(params);
1135
1136    LOG_API("nProgramFragmentCreate, con(%p), paramLen(%i)", con, paramLen);
1137
1138    jint ret = (jint)rsProgramFragmentCreate(con, (uint32_t *)paramPtr, paramLen);
1139    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1140    return ret;
1141}
1142
1143static jint
1144nProgramFragmentCreate2(JNIEnv *_env, jobject _this, jstring shader, jintArray params)
1145{
1146    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1147    const char* shaderUTF = _env->GetStringUTFChars(shader, NULL);
1148    jint shaderLen = _env->GetStringUTFLength(shader);
1149    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1150    jint paramLen = _env->GetArrayLength(params);
1151
1152    LOG_API("nProgramFragmentCreate2, con(%p), shaderLen(%i), paramLen(%i)", con, shaderLen, paramLen);
1153
1154    jint ret = (jint)rsProgramFragmentCreate2(con, shaderUTF, shaderLen, (uint32_t *)paramPtr, paramLen);
1155    _env->ReleaseStringUTFChars(shader, shaderUTF);
1156    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1157    return ret;
1158}
1159
1160
1161// ---------------------------------------------------------------------------
1162
1163static jint
1164nProgramVertexCreate(JNIEnv *_env, jobject _this, jboolean texMat)
1165{
1166    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1167    LOG_API("nProgramVertexCreate, con(%p), texMat(%i)", con, texMat);
1168    return (jint)rsProgramVertexCreate(con, texMat);
1169}
1170
1171static jint
1172nProgramVertexCreate2(JNIEnv *_env, jobject _this, jstring shader, jintArray params)
1173{
1174    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1175    const char* shaderUTF = _env->GetStringUTFChars(shader, NULL);
1176    jint shaderLen = _env->GetStringUTFLength(shader);
1177    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1178    jint paramLen = _env->GetArrayLength(params);
1179
1180    LOG_API("nProgramVertexCreate2, con(%p), shaderLen(%i), paramLen(%i)", con, shaderLen, paramLen);
1181
1182    jint ret = (jint)rsProgramVertexCreate2(con, shaderUTF, shaderLen, (uint32_t *)paramPtr, paramLen);
1183    _env->ReleaseStringUTFChars(shader, shaderUTF);
1184    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1185    return ret;
1186}
1187
1188// ---------------------------------------------------------------------------
1189
1190static jint
1191nProgramRasterCreate(JNIEnv *_env, jobject _this, jint in, jint out,
1192                     jboolean pointSmooth, jboolean lineSmooth, jboolean pointSprite)
1193{
1194    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1195    LOG_API("nProgramRasterCreate, con(%p), in(%p), out(%p), pointSmooth(%i), lineSmooth(%i), pointSprite(%i)",
1196            con, (RsElement)in, (RsElement)out, pointSmooth, lineSmooth, pointSprite);
1197    return (jint)rsProgramRasterCreate(con, (RsElement)in, (RsElement)out, pointSmooth, lineSmooth, pointSprite);
1198}
1199
1200static void
1201nProgramRasterSetPointSize(JNIEnv *_env, jobject _this, jint vpr, jfloat v)
1202{
1203    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1204    LOG_API("nProgramRasterSetPointSize, con(%p), vpf(%p), value(%f)", con, (RsProgramRaster)vpr, v);
1205    rsProgramRasterSetPointSize(con, (RsProgramFragment)vpr, v);
1206}
1207
1208static void
1209nProgramRasterSetLineWidth(JNIEnv *_env, jobject _this, jint vpr, jfloat v)
1210{
1211    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1212    LOG_API("nProgramRasterSetLineWidth, con(%p), vpf(%p), value(%f)", con, (RsProgramRaster)vpr, v);
1213    rsProgramRasterSetLineWidth(con, (RsProgramFragment)vpr, v);
1214}
1215
1216
1217// ---------------------------------------------------------------------------
1218
1219static void
1220nContextBindRootScript(JNIEnv *_env, jobject _this, jint script)
1221{
1222    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1223    LOG_API("nContextBindRootScript, con(%p), script(%p)", con, (RsScript)script);
1224    rsContextBindRootScript(con, (RsScript)script);
1225}
1226
1227static void
1228nContextBindProgramFragmentStore(JNIEnv *_env, jobject _this, jint pfs)
1229{
1230    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1231    LOG_API("nContextBindProgramFragmentStore, con(%p), pfs(%p)", con, (RsProgramFragmentStore)pfs);
1232    rsContextBindProgramFragmentStore(con, (RsProgramFragmentStore)pfs);
1233}
1234
1235static void
1236nContextBindProgramFragment(JNIEnv *_env, jobject _this, jint pf)
1237{
1238    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1239    LOG_API("nContextBindProgramFragment, con(%p), pf(%p)", con, (RsProgramFragment)pf);
1240    rsContextBindProgramFragment(con, (RsProgramFragment)pf);
1241}
1242
1243static void
1244nContextBindProgramVertex(JNIEnv *_env, jobject _this, jint pf)
1245{
1246    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1247    LOG_API("nContextBindProgramVertex, con(%p), pf(%p)", con, (RsProgramVertex)pf);
1248    rsContextBindProgramVertex(con, (RsProgramVertex)pf);
1249}
1250
1251static void
1252nContextBindProgramRaster(JNIEnv *_env, jobject _this, jint pf)
1253{
1254    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1255    LOG_API("nContextBindProgramRaster, con(%p), pf(%p)", con, (RsProgramRaster)pf);
1256    rsContextBindProgramRaster(con, (RsProgramRaster)pf);
1257}
1258
1259
1260// ---------------------------------------------------------------------------
1261
1262static void
1263nSamplerBegin(JNIEnv *_env, jobject _this)
1264{
1265    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1266    LOG_API("nSamplerBegin, con(%p)", con);
1267    rsSamplerBegin(con);
1268}
1269
1270static void
1271nSamplerSet(JNIEnv *_env, jobject _this, jint p, jint v)
1272{
1273    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1274    LOG_API("nSamplerSet, con(%p), param(%i), value(%i)", con, p, v);
1275    rsSamplerSet(con, (RsSamplerParam)p, (RsSamplerValue)v);
1276}
1277
1278static jint
1279nSamplerCreate(JNIEnv *_env, jobject _this)
1280{
1281    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1282    LOG_API("nSamplerCreate, con(%p)", con);
1283    return (jint)rsSamplerCreate(con);
1284}
1285
1286// ---------------------------------------------------------------------------
1287
1288static void
1289nLightBegin(JNIEnv *_env, jobject _this)
1290{
1291    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1292    LOG_API("nLightBegin, con(%p)", con);
1293    rsLightBegin(con);
1294}
1295
1296static void
1297nLightSetIsMono(JNIEnv *_env, jobject _this, jboolean isMono)
1298{
1299    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1300    LOG_API("nLightSetIsMono, con(%p), isMono(%i)", con, isMono);
1301    rsLightSetMonochromatic(con, isMono);
1302}
1303
1304static void
1305nLightSetIsLocal(JNIEnv *_env, jobject _this, jboolean isLocal)
1306{
1307    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1308    LOG_API("nLightSetIsLocal, con(%p), isLocal(%i)", con, isLocal);
1309    rsLightSetLocal(con, isLocal);
1310}
1311
1312static jint
1313nLightCreate(JNIEnv *_env, jobject _this)
1314{
1315    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1316    LOG_API("nLightCreate, con(%p)", con);
1317    return (jint)rsLightCreate(con);
1318}
1319
1320static void
1321nLightSetColor(JNIEnv *_env, jobject _this, jint light, float r, float g, float b)
1322{
1323    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1324    LOG_API("nLightSetColor, con(%p), light(%p), r(%f), g(%f), b(%f)", con, (RsLight)light, r, g, b);
1325    rsLightSetColor(con, (RsLight)light, r, g, b);
1326}
1327
1328static void
1329nLightSetPosition(JNIEnv *_env, jobject _this, jint light, float x, float y, float z)
1330{
1331    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1332    LOG_API("nLightSetPosition, con(%p), light(%p), x(%f), y(%f), z(%f)", con, (RsLight)light, x, y, z);
1333    rsLightSetPosition(con, (RsLight)light, x, y, z);
1334}
1335
1336// ---------------------------------------------------------------------------
1337
1338static jint
1339nSimpleMeshCreate(JNIEnv *_env, jobject _this, jint batchID, jint indexID, jintArray vtxIDs, jint primID)
1340{
1341    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1342    jint len = _env->GetArrayLength(vtxIDs);
1343    LOG_API("nSimpleMeshCreate, con(%p), batchID(%i), indexID(%i), vtxIDs.len(%i), primID(%i)",
1344            con, batchID, indexID, len, primID);
1345    jint *ptr = _env->GetIntArrayElements(vtxIDs, NULL);
1346    int id = (int)rsSimpleMeshCreate(con, (void *)batchID, (void *)indexID, (void **)ptr, len, primID);
1347    _env->ReleaseIntArrayElements(vtxIDs, ptr, 0/*JNI_ABORT*/);
1348    return id;
1349}
1350
1351static void
1352nSimpleMeshBindVertex(JNIEnv *_env, jobject _this, jint s, jint alloc, jint slot)
1353{
1354    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1355    LOG_API("nSimpleMeshBindVertex, con(%p), SimpleMesh(%p), Alloc(%p), slot(%i)", con, (RsSimpleMesh)s, (RsAllocation)alloc, slot);
1356    rsSimpleMeshBindVertex(con, (RsSimpleMesh)s, (RsAllocation)alloc, slot);
1357}
1358
1359static void
1360nSimpleMeshBindIndex(JNIEnv *_env, jobject _this, jint s, jint alloc)
1361{
1362    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1363    LOG_API("nSimpleMeshBindIndex, con(%p), SimpleMesh(%p), Alloc(%p)", con, (RsSimpleMesh)s, (RsAllocation)alloc);
1364    rsSimpleMeshBindIndex(con, (RsSimpleMesh)s, (RsAllocation)alloc);
1365}
1366
1367// ---------------------------------------------------------------------------
1368
1369
1370static const char *classPathName = "android/renderscript/RenderScript";
1371
1372static JNINativeMethod methods[] = {
1373{"_nInit",                         "()V",                                  (void*)_nInit },
1374{"nInitElements",                  "(IIII)V",                              (void*)nInitElements },
1375
1376{"nDeviceCreate",                  "()I",                                  (void*)nDeviceCreate },
1377{"nDeviceDestroy",                 "(I)V",                                 (void*)nDeviceDestroy },
1378{"nDeviceSetConfig",               "(III)V",                               (void*)nDeviceSetConfig },
1379{"nContextCreate",                 "(II)I",                                (void*)nContextCreate },
1380{"nContextCreateGL",               "(IIZ)I",                               (void*)nContextCreateGL },
1381{"nContextSetPriority",            "(I)V",                                 (void*)nContextSetPriority },
1382{"nContextSetSurface",             "(IILandroid/view/Surface;)V",          (void*)nContextSetSurface },
1383{"nContextDestroy",                "(I)V",                                 (void*)nContextDestroy },
1384{"nContextDump",                   "(I)V",                                 (void*)nContextDump },
1385{"nContextPause",                  "()V",                                  (void*)nContextPause },
1386{"nContextResume",                 "()V",                                  (void*)nContextResume },
1387{"nAssignName",                    "(I[B)V",                               (void*)nAssignName },
1388{"nObjDestroy",                    "(I)V",                                 (void*)nObjDestroy },
1389{"nObjDestroyOOB",                 "(I)V",                                 (void*)nObjDestroyOOB },
1390{"nContextGetMessage",             "([IZ)I",                               (void*)nContextGetMessage },
1391{"nContextInitToClient",           "()V",                                  (void*)nContextInitToClient },
1392{"nContextDeinitToClient",         "()V",                                  (void*)nContextDeinitToClient },
1393
1394{"nFileOpen",                      "([B)I",                                (void*)nFileOpen },
1395
1396{"nElementCreate",                 "(IIZI)I",                              (void*)nElementCreate },
1397{"nElementCreate2",                "([I[Ljava/lang/String;)I",             (void*)nElementCreate2 },
1398
1399{"nTypeBegin",                     "(I)V",                                 (void*)nTypeBegin },
1400{"nTypeAdd",                       "(II)V",                                (void*)nTypeAdd },
1401{"nTypeCreate",                    "()I",                                  (void*)nTypeCreate },
1402{"nTypeFinalDestroy",              "(Landroid/renderscript/Type;)V",       (void*)nTypeFinalDestroy },
1403{"nTypeSetupFields",               "(Landroid/renderscript/Type;[I[I[Ljava/lang/reflect/Field;)V", (void*)nTypeSetupFields },
1404
1405{"nAllocationCreateTyped",         "(I)I",                                 (void*)nAllocationCreateTyped },
1406{"nAllocationCreateFromBitmap",    "(IZLandroid/graphics/Bitmap;)I",       (void*)nAllocationCreateFromBitmap },
1407{"nAllocationCreateBitmapRef",     "(ILandroid/graphics/Bitmap;)I",        (void*)nAllocationCreateBitmapRef },
1408{"nAllocationCreateFromBitmapBoxed","(IZLandroid/graphics/Bitmap;)I",      (void*)nAllocationCreateFromBitmapBoxed },
1409{"nAllocationCreateFromAssetStream","(IZI)I",                              (void*)nAllocationCreateFromAssetStream },
1410{"nAllocationUploadToTexture",     "(IZI)V",                               (void*)nAllocationUploadToTexture },
1411{"nAllocationUploadToBufferObject","(I)V",                                 (void*)nAllocationUploadToBufferObject },
1412{"nAllocationSubData1D",           "(III[II)V",                            (void*)nAllocationSubData1D_i },
1413{"nAllocationSubData1D",           "(III[SI)V",                            (void*)nAllocationSubData1D_s },
1414{"nAllocationSubData1D",           "(III[BI)V",                            (void*)nAllocationSubData1D_b },
1415{"nAllocationSubData1D",           "(III[FI)V",                            (void*)nAllocationSubData1D_f },
1416{"nAllocationSubData2D",           "(IIIII[II)V",                          (void*)nAllocationSubData2D_i },
1417{"nAllocationSubData2D",           "(IIIII[FI)V",                          (void*)nAllocationSubData2D_f },
1418{"nAllocationRead",                "(I[I)V",                               (void*)nAllocationRead_i },
1419{"nAllocationRead",                "(I[F)V",                               (void*)nAllocationRead_f },
1420{"nAllocationSubDataFromObject",   "(ILandroid/renderscript/Type;ILjava/lang/Object;)V",   (void*)nAllocationSubDataFromObject },
1421{"nAllocationSubReadFromObject",   "(ILandroid/renderscript/Type;ILjava/lang/Object;)V",   (void*)nAllocationSubReadFromObject },
1422
1423{"nAdapter1DBindAllocation",       "(II)V",                                (void*)nAdapter1DBindAllocation },
1424{"nAdapter1DSetConstraint",        "(III)V",                               (void*)nAdapter1DSetConstraint },
1425{"nAdapter1DData",                 "(I[I)V",                               (void*)nAdapter1DData_i },
1426{"nAdapter1DData",                 "(I[F)V",                               (void*)nAdapter1DData_f },
1427{"nAdapter1DSubData",              "(III[I)V",                             (void*)nAdapter1DSubData_i },
1428{"nAdapter1DSubData",              "(III[F)V",                             (void*)nAdapter1DSubData_f },
1429{"nAdapter1DCreate",               "()I",                                  (void*)nAdapter1DCreate },
1430
1431{"nAdapter2DBindAllocation",       "(II)V",                                (void*)nAdapter2DBindAllocation },
1432{"nAdapter2DSetConstraint",        "(III)V",                               (void*)nAdapter2DSetConstraint },
1433{"nAdapter2DData",                 "(I[I)V",                               (void*)nAdapter2DData_i },
1434{"nAdapter2DData",                 "(I[F)V",                               (void*)nAdapter2DData_f },
1435{"nAdapter2DSubData",              "(IIIII[I)V",                           (void*)nAdapter2DSubData_i },
1436{"nAdapter2DSubData",              "(IIIII[F)V",                           (void*)nAdapter2DSubData_f },
1437{"nAdapter2DCreate",               "()I",                                  (void*)nAdapter2DCreate },
1438
1439{"nScriptBindAllocation",          "(III)V",                               (void*)nScriptBindAllocation },
1440{"nScriptSetClearColor",           "(IFFFF)V",                             (void*)nScriptSetClearColor },
1441{"nScriptSetClearDepth",           "(IF)V",                                (void*)nScriptSetClearDepth },
1442{"nScriptSetClearStencil",         "(II)V",                                (void*)nScriptSetClearStencil },
1443{"nScriptSetTimeZone",             "(I[B)V",                               (void*)nScriptSetTimeZone },
1444{"nScriptSetRoot",                 "(Z)V",                                 (void*)nScriptSetRoot },
1445{"nScriptInvoke",                  "(II)V",                                (void*)nScriptInvoke },
1446{"nScriptInvokeData",              "(II)V",                                (void*)nScriptInvokeData },
1447{"nScriptInvokeV",                 "(II[B)V",                              (void*)nScriptInvokeV },
1448{"nScriptSetVarI",                 "(III)V",                               (void*)nScriptSetVarI },
1449{"nScriptSetVarF",                 "(IIF)V",                               (void*)nScriptSetVarF },
1450{"nScriptSetVarV",                 "(II[B)V",                              (void*)nScriptSetVarV },
1451
1452{"nScriptCBegin",                  "()V",                                  (void*)nScriptCBegin },
1453{"nScriptCSetScript",              "([BII)V",                              (void*)nScriptCSetScript },
1454{"nScriptCCreate",                 "()I",                                  (void*)nScriptCCreate },
1455
1456{"nProgramFragmentStoreBegin",     "(II)V",                                (void*)nProgramFragmentStoreBegin },
1457{"nProgramFragmentStoreDepthFunc", "(I)V",                                 (void*)nProgramFragmentStoreDepthFunc },
1458{"nProgramFragmentStoreDepthMask", "(Z)V",                                 (void*)nProgramFragmentStoreDepthMask },
1459{"nProgramFragmentStoreColorMask", "(ZZZZ)V",                              (void*)nProgramFragmentStoreColorMask },
1460{"nProgramFragmentStoreBlendFunc", "(II)V",                                (void*)nProgramFragmentStoreBlendFunc },
1461{"nProgramFragmentStoreDither",    "(Z)V",                                 (void*)nProgramFragmentStoreDither },
1462{"nProgramFragmentStoreCreate",    "()I",                                  (void*)nProgramFragmentStoreCreate },
1463
1464{"nProgramBindConstants",          "(III)V",                               (void*)nProgramBindConstants },
1465{"nProgramBindTexture",            "(III)V",                               (void*)nProgramBindTexture },
1466{"nProgramBindSampler",            "(III)V",                               (void*)nProgramBindSampler },
1467
1468{"nProgramFragmentCreate",         "([I)I",                                (void*)nProgramFragmentCreate },
1469{"nProgramFragmentCreate2",        "(Ljava/lang/String;[I)I",              (void*)nProgramFragmentCreate2 },
1470
1471{"nProgramRasterCreate",           "(IIZZZ)I",                             (void*)nProgramRasterCreate },
1472{"nProgramRasterSetPointSize",     "(IF)V",                                (void*)nProgramRasterSetPointSize },
1473{"nProgramRasterSetLineWidth",     "(IF)V",                                (void*)nProgramRasterSetLineWidth },
1474
1475{"nProgramVertexCreate",           "(Z)I",                                 (void*)nProgramVertexCreate },
1476{"nProgramVertexCreate2",          "(Ljava/lang/String;[I)I",              (void*)nProgramVertexCreate2 },
1477
1478{"nLightBegin",                    "()V",                                  (void*)nLightBegin },
1479{"nLightSetIsMono",                "(Z)V",                                 (void*)nLightSetIsMono },
1480{"nLightSetIsLocal",               "(Z)V",                                 (void*)nLightSetIsLocal },
1481{"nLightCreate",                   "()I",                                  (void*)nLightCreate },
1482{"nLightSetColor",                 "(IFFF)V",                              (void*)nLightSetColor },
1483{"nLightSetPosition",              "(IFFF)V",                              (void*)nLightSetPosition },
1484
1485{"nContextBindRootScript",         "(I)V",                                 (void*)nContextBindRootScript },
1486{"nContextBindProgramFragmentStore","(I)V",                                (void*)nContextBindProgramFragmentStore },
1487{"nContextBindProgramFragment",    "(I)V",                                 (void*)nContextBindProgramFragment },
1488{"nContextBindProgramVertex",      "(I)V",                                 (void*)nContextBindProgramVertex },
1489{"nContextBindProgramRaster",      "(I)V",                                 (void*)nContextBindProgramRaster },
1490
1491{"nSamplerBegin",                  "()V",                                  (void*)nSamplerBegin },
1492{"nSamplerSet",                    "(II)V",                                (void*)nSamplerSet },
1493{"nSamplerCreate",                 "()I",                                  (void*)nSamplerCreate },
1494
1495{"nSimpleMeshCreate",              "(II[II)I",                             (void*)nSimpleMeshCreate },
1496{"nSimpleMeshBindVertex",          "(III)V",                               (void*)nSimpleMeshBindVertex },
1497{"nSimpleMeshBindIndex",           "(II)V",                                (void*)nSimpleMeshBindIndex },
1498
1499};
1500
1501static int registerFuncs(JNIEnv *_env)
1502{
1503    return android::AndroidRuntime::registerNativeMethods(
1504            _env, classPathName, methods, NELEM(methods));
1505}
1506
1507// ---------------------------------------------------------------------------
1508
1509jint JNI_OnLoad(JavaVM* vm, void* reserved)
1510{
1511    JNIEnv* env = NULL;
1512    jint result = -1;
1513
1514    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
1515        LOGE("ERROR: GetEnv failed\n");
1516        goto bail;
1517    }
1518    assert(env != NULL);
1519
1520    if (registerFuncs(env) < 0) {
1521        LOGE("ERROR: MediaPlayer native registration failed\n");
1522        goto bail;
1523    }
1524
1525    /* success -- return valid version number */
1526    result = JNI_VERSION_1_4;
1527
1528bail:
1529    return result;
1530}
1531