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