android_renderscript_RenderScript.cpp revision 704ff64b099406bb328898a7443921f22dbffd6d
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 <ui/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, "mSurface", "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, jint mip)
448{
449    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
450    LOG_API("nAllocationUploadToTexture, con(%p), a(%p), mip(%i)", con, (RsAllocation)a, mip);
451    rsAllocationUploadToTexture(con, (RsAllocation)a, 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 int
505nAllocationCreateFromAssetStream(JNIEnv *_env, jobject _this, jint dstFmt, jboolean genMips, jint native_asset)
506{
507    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
508
509    Asset* asset = reinterpret_cast<Asset*>(native_asset);
510    SkBitmap bitmap;
511    SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
512            &bitmap, SkBitmap::kNo_Config, SkImageDecoder::kDecodePixels_Mode);
513
514    SkBitmap::Config config = bitmap.getConfig();
515
516    RsElement e = SkBitmapToPredefined(config);
517
518    if (e) {
519        bitmap.lockPixels();
520        const int w = bitmap.width();
521        const int h = bitmap.height();
522        const void* ptr = bitmap.getPixels();
523        jint id = (jint)rsAllocationCreateFromBitmap(con, w, h, (RsElement)dstFmt, e, genMips, ptr);
524        bitmap.unlockPixels();
525        return id;
526    }
527    return 0;
528}
529
530static int
531nAllocationCreateFromBitmapBoxed(JNIEnv *_env, jobject _this, jint dstFmt, jboolean genMips, jobject jbitmap)
532{
533    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
534    SkBitmap const * nativeBitmap =
535            (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID);
536    const SkBitmap& bitmap(*nativeBitmap);
537    SkBitmap::Config config = bitmap.getConfig();
538
539    RsElement e = SkBitmapToPredefined(config);
540
541    if (e) {
542        bitmap.lockPixels();
543        const int w = bitmap.width();
544        const int h = bitmap.height();
545        const void* ptr = bitmap.getPixels();
546        jint id = (jint)rsAllocationCreateFromBitmapBoxed(con, w, h, (RsElement)dstFmt, e, genMips, ptr);
547        bitmap.unlockPixels();
548        return id;
549    }
550    return 0;
551}
552
553
554static void
555nAllocationSubData1D_i(JNIEnv *_env, jobject _this, jint alloc, jint offset, jint count, jintArray data, int sizeBytes)
556{
557    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
558    jint len = _env->GetArrayLength(data);
559    LOG_API("nAllocation1DSubData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
560    jint *ptr = _env->GetIntArrayElements(data, NULL);
561    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
562    _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
563}
564
565static void
566nAllocationSubData1D_s(JNIEnv *_env, jobject _this, jint alloc, jint offset, jint count, jshortArray data, int sizeBytes)
567{
568    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
569    jint len = _env->GetArrayLength(data);
570    LOG_API("nAllocation1DSubData_s, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
571    jshort *ptr = _env->GetShortArrayElements(data, NULL);
572    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
573    _env->ReleaseShortArrayElements(data, ptr, JNI_ABORT);
574}
575
576static void
577nAllocationSubData1D_b(JNIEnv *_env, jobject _this, jint alloc, jint offset, jint count, jbyteArray data, int sizeBytes)
578{
579    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
580    jint len = _env->GetArrayLength(data);
581    LOG_API("nAllocation1DSubData_b, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
582    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
583    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
584    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
585}
586
587static void
588nAllocationSubData1D_f(JNIEnv *_env, jobject _this, jint alloc, jint offset, jint count, jfloatArray data, int sizeBytes)
589{
590    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
591    jint len = _env->GetArrayLength(data);
592    LOG_API("nAllocation1DSubData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
593    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
594    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
595    _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
596}
597
598static void
599nAllocationSubData2D_i(JNIEnv *_env, jobject _this, jint alloc, jint xoff, jint yoff, jint w, jint h, jintArray data, int sizeBytes)
600{
601    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
602    jint len = _env->GetArrayLength(data);
603    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);
604    jint *ptr = _env->GetIntArrayElements(data, NULL);
605    rsAllocation2DSubData(con, (RsAllocation)alloc, xoff, yoff, w, h, ptr, sizeBytes);
606    _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
607}
608
609static void
610nAllocationSubData2D_f(JNIEnv *_env, jobject _this, jint alloc, jint xoff, jint yoff, jint w, jint h, jfloatArray data, int sizeBytes)
611{
612    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
613    jint len = _env->GetArrayLength(data);
614    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);
615    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
616    rsAllocation2DSubData(con, (RsAllocation)alloc, xoff, yoff, w, h, ptr, sizeBytes);
617    _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
618}
619
620static void
621nAllocationRead_i(JNIEnv *_env, jobject _this, jint alloc, jintArray data)
622{
623    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
624    jint len = _env->GetArrayLength(data);
625    LOG_API("nAllocationRead_i, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
626    jint *ptr = _env->GetIntArrayElements(data, NULL);
627    rsAllocationRead(con, (RsAllocation)alloc, ptr);
628    _env->ReleaseIntArrayElements(data, ptr, 0);
629}
630
631static void
632nAllocationRead_f(JNIEnv *_env, jobject _this, jint alloc, jfloatArray data)
633{
634    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
635    jint len = _env->GetArrayLength(data);
636    LOG_API("nAllocationRead_f, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
637    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
638    rsAllocationRead(con, (RsAllocation)alloc, ptr);
639    _env->ReleaseFloatArrayElements(data, ptr, 0);
640}
641
642
643//{"nAllocationDataFromObject",      "(ILandroid/renderscript/Type;Ljava/lang/Object;)V",   (void*)nAllocationDataFromObject },
644static void
645nAllocationSubDataFromObject(JNIEnv *_env, jobject _this, jint alloc, jobject _type, jint offset, jobject _o)
646{
647    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
648    LOG_API("nAllocationDataFromObject con(%p), alloc(%p)", con, (RsAllocation)alloc);
649
650    const TypeCache *tc = (TypeCache *)_env->GetIntField(_type, gTypeNativeCache);
651
652    void * bufAlloc = malloc(tc->size);
653    void * buf = bufAlloc;
654    for (int ct=0; ct < tc->fieldCount; ct++) {
655        const TypeFieldCache *tfc = &tc->fields[ct];
656        buf = tfc->ptr(_env, _o, tfc->field, buf);
657    }
658    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, 1, bufAlloc, tc->size);
659    free(bufAlloc);
660}
661
662static void
663nAllocationSubReadFromObject(JNIEnv *_env, jobject _this, jint alloc, jobject _type, jint offset, jobject _o)
664{
665    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
666    LOG_API("nAllocationReadFromObject con(%p), alloc(%p)", con, (RsAllocation)alloc);
667
668    assert(offset == 0);
669
670    const TypeCache *tc = (TypeCache *)_env->GetIntField(_type, gTypeNativeCache);
671
672    void * bufAlloc = malloc(tc->size);
673    void * buf = bufAlloc;
674    rsAllocationRead(con, (RsAllocation)alloc, bufAlloc);
675
676    for (int ct=0; ct < tc->fieldCount; ct++) {
677        const TypeFieldCache *tfc = &tc->fields[ct];
678        buf = tfc->readPtr(_env, _o, tfc->field, buf);
679    }
680    free(bufAlloc);
681}
682
683
684// -----------------------------------
685
686static void
687nAdapter1DBindAllocation(JNIEnv *_env, jobject _this, jint adapter, jint alloc)
688{
689    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
690    LOG_API("nAdapter1DBindAllocation, con(%p), adapter(%p), alloc(%p)", con, (RsAdapter1D)adapter, (RsAllocation)alloc);
691    rsAdapter1DBindAllocation(con, (RsAdapter1D)adapter, (RsAllocation)alloc);
692}
693
694static void
695nAdapter1DSetConstraint(JNIEnv *_env, jobject _this, jint adapter, jint dim, jint value)
696{
697    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
698    LOG_API("nAdapter1DSetConstraint, con(%p), adapter(%p), dim(%i), value(%i)", con, (RsAdapter1D)adapter, dim, value);
699    rsAdapter1DSetConstraint(con, (RsAdapter1D)adapter, (RsDimension)dim, value);
700}
701
702static void
703nAdapter1DData_i(JNIEnv *_env, jobject _this, jint adapter, jintArray data)
704{
705    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
706    jint len = _env->GetArrayLength(data);
707    LOG_API("nAdapter1DData_i, con(%p), adapter(%p), len(%i)", con, (RsAdapter1D)adapter, len);
708    jint *ptr = _env->GetIntArrayElements(data, NULL);
709    rsAdapter1DData(con, (RsAdapter1D)adapter, ptr);
710    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
711}
712
713static void
714nAdapter1DSubData_i(JNIEnv *_env, jobject _this, jint adapter, jint offset, jint count, jintArray data)
715{
716    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
717    jint len = _env->GetArrayLength(data);
718    LOG_API("nAdapter1DSubData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAdapter1D)adapter, offset, count, len);
719    jint *ptr = _env->GetIntArrayElements(data, NULL);
720    rsAdapter1DSubData(con, (RsAdapter1D)adapter, offset, count, ptr);
721    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
722}
723
724static void
725nAdapter1DData_f(JNIEnv *_env, jobject _this, jint adapter, jfloatArray data)
726{
727    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
728    jint len = _env->GetArrayLength(data);
729    LOG_API("nAdapter1DData_f, con(%p), adapter(%p), len(%i)", con, (RsAdapter1D)adapter, len);
730    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
731    rsAdapter1DData(con, (RsAdapter1D)adapter, ptr);
732    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
733}
734
735static void
736nAdapter1DSubData_f(JNIEnv *_env, jobject _this, jint adapter, jint offset, jint count, jfloatArray data)
737{
738    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
739    jint len = _env->GetArrayLength(data);
740    LOG_API("nAdapter1DSubData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAdapter1D)adapter, offset, count, len);
741    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
742    rsAdapter1DSubData(con, (RsAdapter1D)adapter, offset, count, ptr);
743    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
744}
745
746static jint
747nAdapter1DCreate(JNIEnv *_env, jobject _this)
748{
749    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
750    LOG_API("nAdapter1DCreate, con(%p)", con);
751    return (jint)rsAdapter1DCreate(con);
752}
753
754// -----------------------------------
755
756static void
757nAdapter2DBindAllocation(JNIEnv *_env, jobject _this, jint adapter, jint alloc)
758{
759    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
760    LOG_API("nAdapter2DBindAllocation, con(%p), adapter(%p), alloc(%p)", con, (RsAdapter2D)adapter, (RsAllocation)alloc);
761    rsAdapter2DBindAllocation(con, (RsAdapter2D)adapter, (RsAllocation)alloc);
762}
763
764static void
765nAdapter2DSetConstraint(JNIEnv *_env, jobject _this, jint adapter, jint dim, jint value)
766{
767    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
768    LOG_API("nAdapter2DSetConstraint, con(%p), adapter(%p), dim(%i), value(%i)", con, (RsAdapter2D)adapter, dim, value);
769    rsAdapter2DSetConstraint(con, (RsAdapter2D)adapter, (RsDimension)dim, value);
770}
771
772static void
773nAdapter2DData_i(JNIEnv *_env, jobject _this, jint adapter, jintArray data)
774{
775    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
776    jint len = _env->GetArrayLength(data);
777    LOG_API("nAdapter2DData_i, con(%p), adapter(%p), len(%i)", con, (RsAdapter2D)adapter, len);
778    jint *ptr = _env->GetIntArrayElements(data, NULL);
779    rsAdapter2DData(con, (RsAdapter2D)adapter, ptr);
780    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
781}
782
783static void
784nAdapter2DData_f(JNIEnv *_env, jobject _this, jint adapter, jfloatArray data)
785{
786    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
787    jint len = _env->GetArrayLength(data);
788    LOG_API("nAdapter2DData_f, con(%p), adapter(%p), len(%i)", con, (RsAdapter2D)adapter, len);
789    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
790    rsAdapter2DData(con, (RsAdapter2D)adapter, ptr);
791    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
792}
793
794static void
795nAdapter2DSubData_i(JNIEnv *_env, jobject _this, jint adapter, jint xoff, jint yoff, jint w, jint h, jintArray data)
796{
797    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
798    jint len = _env->GetArrayLength(data);
799    LOG_API("nAdapter2DSubData_i, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)",
800            con, (RsAdapter2D)adapter, xoff, yoff, w, h, len);
801    jint *ptr = _env->GetIntArrayElements(data, NULL);
802    rsAdapter2DSubData(con, (RsAdapter2D)adapter, xoff, yoff, w, h, ptr);
803    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
804}
805
806static void
807nAdapter2DSubData_f(JNIEnv *_env, jobject _this, jint adapter, jint xoff, jint yoff, jint w, jint h, jfloatArray data)
808{
809    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
810    jint len = _env->GetArrayLength(data);
811    LOG_API("nAdapter2DSubData_f, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)",
812            con, (RsAdapter2D)adapter, xoff, yoff, w, h, len);
813    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
814    rsAdapter2DSubData(con, (RsAdapter1D)adapter, xoff, yoff, w, h, ptr);
815    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
816}
817
818static jint
819nAdapter2DCreate(JNIEnv *_env, jobject _this)
820{
821    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
822    LOG_API("nAdapter2DCreate, con(%p)", con);
823    return (jint)rsAdapter2DCreate(con);
824}
825
826// -----------------------------------
827
828static void
829nScriptBindAllocation(JNIEnv *_env, jobject _this, jint script, jint alloc, jint slot)
830{
831    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
832    LOG_API("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", con, (RsScript)script, (RsAllocation)alloc, slot);
833    rsScriptBindAllocation(con, (RsScript)script, (RsAllocation)alloc, slot);
834}
835
836static void
837nScriptSetClearColor(JNIEnv *_env, jobject _this, jint script, jfloat r, jfloat g, jfloat b, jfloat a)
838{
839    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
840    LOG_API("nScriptSetClearColor, con(%p), s(%p), r(%f), g(%f), b(%f), a(%f)", con, (void *)script, r, g, b, a);
841    rsScriptSetClearColor(con, (RsScript)script, r, g, b, a);
842}
843
844static void
845nScriptSetClearDepth(JNIEnv *_env, jobject _this, jint script, jfloat d)
846{
847    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
848    LOG_API("nScriptCSetClearDepth, con(%p), s(%p), depth(%f)", con, (void *)script, d);
849    rsScriptSetClearDepth(con, (RsScript)script, d);
850}
851
852static void
853nScriptSetClearStencil(JNIEnv *_env, jobject _this, jint script, jint stencil)
854{
855    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
856    LOG_API("nScriptCSetClearStencil, con(%p), s(%p), stencil(%i)", con, (void *)script, stencil);
857    rsScriptSetClearStencil(con, (RsScript)script, stencil);
858}
859
860static void
861nScriptSetTimeZone(JNIEnv *_env, jobject _this, jint script, jbyteArray timeZone)
862{
863    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
864    LOG_API("nScriptCSetTimeZone, con(%p), s(%p), timeZone(%s)", con, (void *)script, (const char *)timeZone);
865
866    jint length = _env->GetArrayLength(timeZone);
867    jbyte* timeZone_ptr;
868    timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
869
870    rsScriptSetTimeZone(con, (RsScript)script, (const char *)timeZone_ptr, length);
871
872    if (timeZone_ptr) {
873        _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
874    }
875}
876
877static void
878nScriptSetType(JNIEnv *_env, jobject _this, jint type, jboolean writable, jstring _str, jint slot)
879{
880    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
881    LOG_API("nScriptCAddType, con(%p), type(%p), writable(%i), slot(%i)", con, (RsType)type, writable, slot);
882    const char* n = NULL;
883    if (_str) {
884        n = _env->GetStringUTFChars(_str, NULL);
885    }
886    rsScriptSetType(con, (RsType)type, slot, writable, n);
887    if (n) {
888        _env->ReleaseStringUTFChars(_str, n);
889    }
890}
891
892static void
893nScriptSetInvoke(JNIEnv *_env, jobject _this, jstring _str, jint slot)
894{
895    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
896    LOG_API("nScriptSetInvoke, con(%p)", con);
897    const char* n = NULL;
898    if (_str) {
899        n = _env->GetStringUTFChars(_str, NULL);
900    }
901    rsScriptSetInvoke(con, n, slot);
902    if (n) {
903        _env->ReleaseStringUTFChars(_str, n);
904    }
905}
906
907static void
908nScriptInvoke(JNIEnv *_env, jobject _this, jint obj, jint slot)
909{
910    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
911    LOG_API("nScriptInvoke, con(%p), script(%p)", con, (void *)obj);
912    rsScriptInvoke(con, (RsScript)obj, slot);
913}
914
915static void
916nScriptSetRoot(JNIEnv *_env, jobject _this, jboolean isRoot)
917{
918    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
919    LOG_API("nScriptCSetRoot, con(%p), isRoot(%i)", con, isRoot);
920    rsScriptSetRoot(con, isRoot);
921}
922
923// -----------------------------------
924
925static void
926nScriptCBegin(JNIEnv *_env, jobject _this)
927{
928    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
929    LOG_API("nScriptCBegin, con(%p)", con);
930    rsScriptCBegin(con);
931}
932
933static void
934nScriptCSetScript(JNIEnv *_env, jobject _this, jbyteArray scriptRef,
935                  jint offset, jint length)
936{
937    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
938    LOG_API("!!! nScriptCSetScript, con(%p)", con);
939    jint _exception = 0;
940    jint remaining;
941    jbyte* script_base = 0;
942    jbyte* script_ptr;
943    if (!scriptRef) {
944        _exception = 1;
945        //_env->ThrowNew(IAEClass, "script == null");
946        goto exit;
947    }
948    if (offset < 0) {
949        _exception = 1;
950        //_env->ThrowNew(IAEClass, "offset < 0");
951        goto exit;
952    }
953    if (length < 0) {
954        _exception = 1;
955        //_env->ThrowNew(IAEClass, "length < 0");
956        goto exit;
957    }
958    remaining = _env->GetArrayLength(scriptRef) - offset;
959    if (remaining < length) {
960        _exception = 1;
961        //_env->ThrowNew(IAEClass, "length > script.length - offset");
962        goto exit;
963    }
964    script_base = (jbyte *)
965        _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
966    script_ptr = script_base + offset;
967
968    rsScriptCSetText(con, (const char *)script_ptr, length);
969
970exit:
971    if (script_base) {
972        _env->ReleasePrimitiveArrayCritical(scriptRef, script_base,
973                _exception ? JNI_ABORT: 0);
974    }
975}
976
977static jint
978nScriptCCreate(JNIEnv *_env, jobject _this)
979{
980    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
981    LOG_API("nScriptCCreate, con(%p)", con);
982    return (jint)rsScriptCCreate(con);
983}
984
985static void
986nScriptCAddDefineI32(JNIEnv *_env, jobject _this, jstring name, jint value)
987{
988    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
989    const char* n = _env->GetStringUTFChars(name, NULL);
990    LOG_API("nScriptCAddDefineI32, con(%p) name(%s) value(%d)", con, n, value);
991    rsScriptCSetDefineI32(con, n, value);
992    _env->ReleaseStringUTFChars(name, n);
993}
994
995static void
996nScriptCAddDefineF(JNIEnv *_env, jobject _this, jstring name, jfloat value)
997{
998    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
999    const char* n = _env->GetStringUTFChars(name, NULL);
1000    LOG_API("nScriptCAddDefineF, con(%p) name(%s) value(%f)", con, n, value);
1001    rsScriptCSetDefineF(con, n, value);
1002    _env->ReleaseStringUTFChars(name, n);
1003}
1004
1005// ---------------------------------------------------------------------------
1006
1007static void
1008nProgramFragmentStoreBegin(JNIEnv *_env, jobject _this, jint in, jint out)
1009{
1010    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1011    LOG_API("nProgramFragmentStoreBegin, con(%p), in(%p), out(%p)", con, (RsElement)in, (RsElement)out);
1012    rsProgramFragmentStoreBegin(con, (RsElement)in, (RsElement)out);
1013}
1014
1015static void
1016nProgramFragmentStoreDepthFunc(JNIEnv *_env, jobject _this, jint func)
1017{
1018    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1019    LOG_API("nProgramFragmentStoreDepthFunc, con(%p), func(%i)", con, func);
1020    rsProgramFragmentStoreDepthFunc(con, (RsDepthFunc)func);
1021}
1022
1023static void
1024nProgramFragmentStoreDepthMask(JNIEnv *_env, jobject _this, jboolean enable)
1025{
1026    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1027    LOG_API("nProgramFragmentStoreDepthMask, con(%p), enable(%i)", con, enable);
1028    rsProgramFragmentStoreDepthMask(con, enable);
1029}
1030
1031static void
1032nProgramFragmentStoreColorMask(JNIEnv *_env, jobject _this, jboolean r, jboolean g, jboolean b, jboolean a)
1033{
1034    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1035    LOG_API("nProgramFragmentStoreColorMask, con(%p), r(%i), g(%i), b(%i), a(%i)", con, r, g, b, a);
1036    rsProgramFragmentStoreColorMask(con, r, g, b, a);
1037}
1038
1039static void
1040nProgramFragmentStoreBlendFunc(JNIEnv *_env, jobject _this, int src, int dst)
1041{
1042    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1043    LOG_API("nProgramFragmentStoreBlendFunc, con(%p), src(%i), dst(%i)", con, src, dst);
1044    rsProgramFragmentStoreBlendFunc(con, (RsBlendSrcFunc)src, (RsBlendDstFunc)dst);
1045}
1046
1047static void
1048nProgramFragmentStoreDither(JNIEnv *_env, jobject _this, jboolean enable)
1049{
1050    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1051    LOG_API("nProgramFragmentStoreDither, con(%p), enable(%i)", con, enable);
1052    rsProgramFragmentStoreDither(con, enable);
1053}
1054
1055static jint
1056nProgramFragmentStoreCreate(JNIEnv *_env, jobject _this)
1057{
1058    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1059    LOG_API("nProgramFragmentStoreCreate, con(%p)", con);
1060
1061    return (jint)rsProgramFragmentStoreCreate(con);
1062}
1063
1064// ---------------------------------------------------------------------------
1065
1066static void
1067nProgramBindConstants(JNIEnv *_env, jobject _this, jint vpv, jint slot, jint a)
1068{
1069    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1070    LOG_API("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", con, (RsProgramVertex)vpv, slot, (RsAllocation)a);
1071    rsProgramBindConstants(con, (RsProgram)vpv, slot, (RsAllocation)a);
1072}
1073
1074static void
1075nProgramBindTexture(JNIEnv *_env, jobject _this, jint vpf, jint slot, jint a)
1076{
1077    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1078    LOG_API("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
1079    rsProgramBindTexture(con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
1080}
1081
1082static void
1083nProgramBindSampler(JNIEnv *_env, jobject _this, jint vpf, jint slot, jint a)
1084{
1085    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1086    LOG_API("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsSampler)a);
1087    rsProgramBindSampler(con, (RsProgramFragment)vpf, slot, (RsSampler)a);
1088}
1089
1090// ---------------------------------------------------------------------------
1091
1092static jint
1093nProgramFragmentCreate(JNIEnv *_env, jobject _this, jintArray params)
1094{
1095    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1096    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1097    jint paramLen = _env->GetArrayLength(params);
1098
1099    LOG_API("nProgramFragmentCreate, con(%p), paramLen(%i)", con, paramLen);
1100
1101    jint ret = (jint)rsProgramFragmentCreate(con, (uint32_t *)paramPtr, paramLen);
1102    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1103    return ret;
1104}
1105
1106static jint
1107nProgramFragmentCreate2(JNIEnv *_env, jobject _this, jstring shader, jintArray params)
1108{
1109    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1110    const char* shaderUTF = _env->GetStringUTFChars(shader, NULL);
1111    jint shaderLen = _env->GetStringUTFLength(shader);
1112    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1113    jint paramLen = _env->GetArrayLength(params);
1114
1115    LOG_API("nProgramFragmentCreate2, con(%p), shaderLen(%i), paramLen(%i)", con, shaderLen, paramLen);
1116
1117    jint ret = (jint)rsProgramFragmentCreate2(con, shaderUTF, shaderLen, (uint32_t *)paramPtr, paramLen);
1118    _env->ReleaseStringUTFChars(shader, shaderUTF);
1119    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1120    return ret;
1121}
1122
1123
1124// ---------------------------------------------------------------------------
1125
1126static jint
1127nProgramVertexCreate(JNIEnv *_env, jobject _this, jboolean texMat)
1128{
1129    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1130    LOG_API("nProgramVertexCreate, con(%p), texMat(%i)", con, texMat);
1131    return (jint)rsProgramVertexCreate(con, texMat);
1132}
1133
1134static jint
1135nProgramVertexCreate2(JNIEnv *_env, jobject _this, jstring shader, jintArray params)
1136{
1137    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1138    const char* shaderUTF = _env->GetStringUTFChars(shader, NULL);
1139    jint shaderLen = _env->GetStringUTFLength(shader);
1140    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1141    jint paramLen = _env->GetArrayLength(params);
1142
1143    LOG_API("nProgramVertexCreate2, con(%p), shaderLen(%i), paramLen(%i)", con, shaderLen, paramLen);
1144
1145    jint ret = (jint)rsProgramVertexCreate2(con, shaderUTF, shaderLen, (uint32_t *)paramPtr, paramLen);
1146    _env->ReleaseStringUTFChars(shader, shaderUTF);
1147    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1148    return ret;
1149}
1150
1151// ---------------------------------------------------------------------------
1152
1153static jint
1154nProgramRasterCreate(JNIEnv *_env, jobject _this, jint in, jint out,
1155                     jboolean pointSmooth, jboolean lineSmooth, jboolean pointSprite)
1156{
1157    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1158    LOG_API("nProgramRasterCreate, con(%p), in(%p), out(%p), pointSmooth(%i), lineSmooth(%i), pointSprite(%i)",
1159            con, (RsElement)in, (RsElement)out, pointSmooth, lineSmooth, pointSprite);
1160    return (jint)rsProgramRasterCreate(con, (RsElement)in, (RsElement)out, pointSmooth, lineSmooth, pointSprite);
1161}
1162
1163static void
1164nProgramRasterSetPointSize(JNIEnv *_env, jobject _this, jint vpr, jfloat v)
1165{
1166    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1167    LOG_API("nProgramRasterSetPointSize, con(%p), vpf(%p), value(%f)", con, (RsProgramRaster)vpr, v);
1168    rsProgramRasterSetPointSize(con, (RsProgramFragment)vpr, v);
1169}
1170
1171static void
1172nProgramRasterSetLineWidth(JNIEnv *_env, jobject _this, jint vpr, jfloat v)
1173{
1174    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1175    LOG_API("nProgramRasterSetLineWidth, con(%p), vpf(%p), value(%f)", con, (RsProgramRaster)vpr, v);
1176    rsProgramRasterSetLineWidth(con, (RsProgramFragment)vpr, v);
1177}
1178
1179
1180// ---------------------------------------------------------------------------
1181
1182static void
1183nContextBindRootScript(JNIEnv *_env, jobject _this, jint script)
1184{
1185    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1186    LOG_API("nContextBindRootScript, con(%p), script(%p)", con, (RsScript)script);
1187    rsContextBindRootScript(con, (RsScript)script);
1188}
1189
1190static void
1191nContextBindProgramFragmentStore(JNIEnv *_env, jobject _this, jint pfs)
1192{
1193    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1194    LOG_API("nContextBindProgramFragmentStore, con(%p), pfs(%p)", con, (RsProgramFragmentStore)pfs);
1195    rsContextBindProgramFragmentStore(con, (RsProgramFragmentStore)pfs);
1196}
1197
1198static void
1199nContextBindProgramFragment(JNIEnv *_env, jobject _this, jint pf)
1200{
1201    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1202    LOG_API("nContextBindProgramFragment, con(%p), pf(%p)", con, (RsProgramFragment)pf);
1203    rsContextBindProgramFragment(con, (RsProgramFragment)pf);
1204}
1205
1206static void
1207nContextBindProgramVertex(JNIEnv *_env, jobject _this, jint pf)
1208{
1209    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1210    LOG_API("nContextBindProgramVertex, con(%p), pf(%p)", con, (RsProgramVertex)pf);
1211    rsContextBindProgramVertex(con, (RsProgramVertex)pf);
1212}
1213
1214static void
1215nContextBindProgramRaster(JNIEnv *_env, jobject _this, jint pf)
1216{
1217    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1218    LOG_API("nContextBindProgramRaster, con(%p), pf(%p)", con, (RsProgramRaster)pf);
1219    rsContextBindProgramRaster(con, (RsProgramRaster)pf);
1220}
1221
1222
1223// ---------------------------------------------------------------------------
1224
1225static void
1226nSamplerBegin(JNIEnv *_env, jobject _this)
1227{
1228    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1229    LOG_API("nSamplerBegin, con(%p)", con);
1230    rsSamplerBegin(con);
1231}
1232
1233static void
1234nSamplerSet(JNIEnv *_env, jobject _this, jint p, jint v)
1235{
1236    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1237    LOG_API("nSamplerSet, con(%p), param(%i), value(%i)", con, p, v);
1238    rsSamplerSet(con, (RsSamplerParam)p, (RsSamplerValue)v);
1239}
1240
1241static jint
1242nSamplerCreate(JNIEnv *_env, jobject _this)
1243{
1244    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1245    LOG_API("nSamplerCreate, con(%p)", con);
1246    return (jint)rsSamplerCreate(con);
1247}
1248
1249// ---------------------------------------------------------------------------
1250
1251static void
1252nLightBegin(JNIEnv *_env, jobject _this)
1253{
1254    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1255    LOG_API("nLightBegin, con(%p)", con);
1256    rsLightBegin(con);
1257}
1258
1259static void
1260nLightSetIsMono(JNIEnv *_env, jobject _this, jboolean isMono)
1261{
1262    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1263    LOG_API("nLightSetIsMono, con(%p), isMono(%i)", con, isMono);
1264    rsLightSetMonochromatic(con, isMono);
1265}
1266
1267static void
1268nLightSetIsLocal(JNIEnv *_env, jobject _this, jboolean isLocal)
1269{
1270    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1271    LOG_API("nLightSetIsLocal, con(%p), isLocal(%i)", con, isLocal);
1272    rsLightSetLocal(con, isLocal);
1273}
1274
1275static jint
1276nLightCreate(JNIEnv *_env, jobject _this)
1277{
1278    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1279    LOG_API("nLightCreate, con(%p)", con);
1280    return (jint)rsLightCreate(con);
1281}
1282
1283static void
1284nLightSetColor(JNIEnv *_env, jobject _this, jint light, float r, float g, float b)
1285{
1286    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1287    LOG_API("nLightSetColor, con(%p), light(%p), r(%f), g(%f), b(%f)", con, (RsLight)light, r, g, b);
1288    rsLightSetColor(con, (RsLight)light, r, g, b);
1289}
1290
1291static void
1292nLightSetPosition(JNIEnv *_env, jobject _this, jint light, float x, float y, float z)
1293{
1294    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1295    LOG_API("nLightSetPosition, con(%p), light(%p), x(%f), y(%f), z(%f)", con, (RsLight)light, x, y, z);
1296    rsLightSetPosition(con, (RsLight)light, x, y, z);
1297}
1298
1299// ---------------------------------------------------------------------------
1300
1301static jint
1302nSimpleMeshCreate(JNIEnv *_env, jobject _this, jint batchID, jint indexID, jintArray vtxIDs, jint primID)
1303{
1304    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1305    jint len = _env->GetArrayLength(vtxIDs);
1306    LOG_API("nSimpleMeshCreate, con(%p), batchID(%i), indexID(%i), vtxIDs.len(%i), primID(%i)",
1307            con, batchID, indexID, len, primID);
1308    jint *ptr = _env->GetIntArrayElements(vtxIDs, NULL);
1309    int id = (int)rsSimpleMeshCreate(con, (void *)batchID, (void *)indexID, (void **)ptr, len, primID);
1310    _env->ReleaseIntArrayElements(vtxIDs, ptr, 0/*JNI_ABORT*/);
1311    return id;
1312}
1313
1314static void
1315nSimpleMeshBindVertex(JNIEnv *_env, jobject _this, jint s, jint alloc, jint slot)
1316{
1317    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1318    LOG_API("nSimpleMeshBindVertex, con(%p), SimpleMesh(%p), Alloc(%p), slot(%i)", con, (RsSimpleMesh)s, (RsAllocation)alloc, slot);
1319    rsSimpleMeshBindVertex(con, (RsSimpleMesh)s, (RsAllocation)alloc, slot);
1320}
1321
1322static void
1323nSimpleMeshBindIndex(JNIEnv *_env, jobject _this, jint s, jint alloc)
1324{
1325    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1326    LOG_API("nSimpleMeshBindIndex, con(%p), SimpleMesh(%p), Alloc(%p)", con, (RsSimpleMesh)s, (RsAllocation)alloc);
1327    rsSimpleMeshBindIndex(con, (RsSimpleMesh)s, (RsAllocation)alloc);
1328}
1329
1330// ---------------------------------------------------------------------------
1331
1332
1333static const char *classPathName = "android/renderscript/RenderScript";
1334
1335static JNINativeMethod methods[] = {
1336{"_nInit",                         "()V",                                  (void*)_nInit },
1337{"nInitElements",                  "(IIII)V",                              (void*)nInitElements },
1338
1339{"nDeviceCreate",                  "()I",                                  (void*)nDeviceCreate },
1340{"nDeviceDestroy",                 "(I)V",                                 (void*)nDeviceDestroy },
1341{"nDeviceSetConfig",               "(III)V",                               (void*)nDeviceSetConfig },
1342{"nContextCreate",                 "(II)I",                                (void*)nContextCreate },
1343{"nContextCreateGL",               "(IIZ)I",                               (void*)nContextCreateGL },
1344{"nContextSetPriority",            "(I)V",                                 (void*)nContextSetPriority },
1345{"nContextSetSurface",             "(IILandroid/view/Surface;)V",          (void*)nContextSetSurface },
1346{"nContextDestroy",                "(I)V",                                 (void*)nContextDestroy },
1347{"nContextDump",                   "(I)V",                                 (void*)nContextDump },
1348{"nContextPause",                  "()V",                                  (void*)nContextPause },
1349{"nContextResume",                 "()V",                                  (void*)nContextResume },
1350{"nAssignName",                    "(I[B)V",                               (void*)nAssignName },
1351{"nObjDestroy",                    "(I)V",                                 (void*)nObjDestroy },
1352{"nObjDestroyOOB",                 "(I)V",                                 (void*)nObjDestroyOOB },
1353{"nContextGetMessage",             "([IZ)I",                               (void*)nContextGetMessage },
1354{"nContextInitToClient",           "()V",                                  (void*)nContextInitToClient },
1355{"nContextDeinitToClient",         "()V",                                  (void*)nContextDeinitToClient },
1356
1357{"nFileOpen",                      "([B)I",                                (void*)nFileOpen },
1358
1359{"nElementCreate",                 "(IIZI)I",                              (void*)nElementCreate },
1360{"nElementCreate2",                "([I[Ljava/lang/String;)I",             (void*)nElementCreate2 },
1361
1362{"nTypeBegin",                     "(I)V",                                 (void*)nTypeBegin },
1363{"nTypeAdd",                       "(II)V",                                (void*)nTypeAdd },
1364{"nTypeCreate",                    "()I",                                  (void*)nTypeCreate },
1365{"nTypeFinalDestroy",              "(Landroid/renderscript/Type;)V",       (void*)nTypeFinalDestroy },
1366{"nTypeSetupFields",               "(Landroid/renderscript/Type;[I[I[Ljava/lang/reflect/Field;)V", (void*)nTypeSetupFields },
1367
1368{"nAllocationCreateTyped",         "(I)I",                                 (void*)nAllocationCreateTyped },
1369{"nAllocationCreateFromBitmap",    "(IZLandroid/graphics/Bitmap;)I",       (void*)nAllocationCreateFromBitmap },
1370{"nAllocationCreateFromBitmapBoxed","(IZLandroid/graphics/Bitmap;)I",      (void*)nAllocationCreateFromBitmapBoxed },
1371{"nAllocationCreateFromAssetStream","(IZI)I",                              (void*)nAllocationCreateFromAssetStream },
1372{"nAllocationUploadToTexture",     "(II)V",                                (void*)nAllocationUploadToTexture },
1373{"nAllocationUploadToBufferObject","(I)V",                                 (void*)nAllocationUploadToBufferObject },
1374{"nAllocationSubData1D",           "(III[II)V",                            (void*)nAllocationSubData1D_i },
1375{"nAllocationSubData1D",           "(III[SI)V",                            (void*)nAllocationSubData1D_s },
1376{"nAllocationSubData1D",           "(III[BI)V",                            (void*)nAllocationSubData1D_b },
1377{"nAllocationSubData1D",           "(III[FI)V",                            (void*)nAllocationSubData1D_f },
1378{"nAllocationSubData2D",           "(IIIII[II)V",                          (void*)nAllocationSubData2D_i },
1379{"nAllocationSubData2D",           "(IIIII[FI)V",                          (void*)nAllocationSubData2D_f },
1380{"nAllocationRead",                "(I[I)V",                               (void*)nAllocationRead_i },
1381{"nAllocationRead",                "(I[F)V",                               (void*)nAllocationRead_f },
1382{"nAllocationSubDataFromObject",   "(ILandroid/renderscript/Type;ILjava/lang/Object;)V",   (void*)nAllocationSubDataFromObject },
1383{"nAllocationSubReadFromObject",   "(ILandroid/renderscript/Type;ILjava/lang/Object;)V",   (void*)nAllocationSubReadFromObject },
1384
1385{"nAdapter1DBindAllocation",       "(II)V",                                (void*)nAdapter1DBindAllocation },
1386{"nAdapter1DSetConstraint",        "(III)V",                               (void*)nAdapter1DSetConstraint },
1387{"nAdapter1DData",                 "(I[I)V",                               (void*)nAdapter1DData_i },
1388{"nAdapter1DData",                 "(I[F)V",                               (void*)nAdapter1DData_f },
1389{"nAdapter1DSubData",              "(III[I)V",                             (void*)nAdapter1DSubData_i },
1390{"nAdapter1DSubData",              "(III[F)V",                             (void*)nAdapter1DSubData_f },
1391{"nAdapter1DCreate",               "()I",                                  (void*)nAdapter1DCreate },
1392
1393{"nAdapter2DBindAllocation",       "(II)V",                                (void*)nAdapter2DBindAllocation },
1394{"nAdapter2DSetConstraint",        "(III)V",                               (void*)nAdapter2DSetConstraint },
1395{"nAdapter2DData",                 "(I[I)V",                               (void*)nAdapter2DData_i },
1396{"nAdapter2DData",                 "(I[F)V",                               (void*)nAdapter2DData_f },
1397{"nAdapter2DSubData",              "(IIIII[I)V",                           (void*)nAdapter2DSubData_i },
1398{"nAdapter2DSubData",              "(IIIII[F)V",                           (void*)nAdapter2DSubData_f },
1399{"nAdapter2DCreate",               "()I",                                  (void*)nAdapter2DCreate },
1400
1401{"nScriptBindAllocation",          "(III)V",                               (void*)nScriptBindAllocation },
1402{"nScriptSetClearColor",           "(IFFFF)V",                             (void*)nScriptSetClearColor },
1403{"nScriptSetClearDepth",           "(IF)V",                                (void*)nScriptSetClearDepth },
1404{"nScriptSetClearStencil",         "(II)V",                                (void*)nScriptSetClearStencil },
1405{"nScriptSetTimeZone",             "(I[B)V",                               (void*)nScriptSetTimeZone },
1406{"nScriptSetType",                 "(IZLjava/lang/String;I)V",             (void*)nScriptSetType },
1407{"nScriptSetRoot",                 "(Z)V",                                 (void*)nScriptSetRoot },
1408{"nScriptSetInvokable",            "(Ljava/lang/String;I)V",               (void*)nScriptSetInvoke },
1409{"nScriptInvoke",                  "(II)V",                                (void*)nScriptInvoke },
1410
1411{"nScriptCBegin",                  "()V",                                  (void*)nScriptCBegin },
1412{"nScriptCSetScript",              "([BII)V",                              (void*)nScriptCSetScript },
1413{"nScriptCCreate",                 "()I",                                  (void*)nScriptCCreate },
1414{"nScriptCAddDefineI32",           "(Ljava/lang/String;I)V",               (void*)nScriptCAddDefineI32 },
1415{"nScriptCAddDefineF",             "(Ljava/lang/String;F)V",               (void*)nScriptCAddDefineF },
1416
1417{"nProgramFragmentStoreBegin",     "(II)V",                                (void*)nProgramFragmentStoreBegin },
1418{"nProgramFragmentStoreDepthFunc", "(I)V",                                 (void*)nProgramFragmentStoreDepthFunc },
1419{"nProgramFragmentStoreDepthMask", "(Z)V",                                 (void*)nProgramFragmentStoreDepthMask },
1420{"nProgramFragmentStoreColorMask", "(ZZZZ)V",                              (void*)nProgramFragmentStoreColorMask },
1421{"nProgramFragmentStoreBlendFunc", "(II)V",                                (void*)nProgramFragmentStoreBlendFunc },
1422{"nProgramFragmentStoreDither",    "(Z)V",                                 (void*)nProgramFragmentStoreDither },
1423{"nProgramFragmentStoreCreate",    "()I",                                  (void*)nProgramFragmentStoreCreate },
1424
1425{"nProgramBindConstants",          "(III)V",                               (void*)nProgramBindConstants },
1426{"nProgramBindTexture",            "(III)V",                               (void*)nProgramBindTexture },
1427{"nProgramBindSampler",            "(III)V",                               (void*)nProgramBindSampler },
1428
1429{"nProgramFragmentCreate",         "([I)I",                                (void*)nProgramFragmentCreate },
1430{"nProgramFragmentCreate2",        "(Ljava/lang/String;[I)I",              (void*)nProgramFragmentCreate2 },
1431
1432{"nProgramRasterCreate",           "(IIZZZ)I",                             (void*)nProgramRasterCreate },
1433{"nProgramRasterSetPointSize",     "(IF)V",                                (void*)nProgramRasterSetPointSize },
1434{"nProgramRasterSetLineWidth",     "(IF)V",                                (void*)nProgramRasterSetLineWidth },
1435
1436{"nProgramVertexCreate",           "(Z)I",                                 (void*)nProgramVertexCreate },
1437{"nProgramVertexCreate2",          "(Ljava/lang/String;[I)I",              (void*)nProgramVertexCreate2 },
1438
1439{"nLightBegin",                    "()V",                                  (void*)nLightBegin },
1440{"nLightSetIsMono",                "(Z)V",                                 (void*)nLightSetIsMono },
1441{"nLightSetIsLocal",               "(Z)V",                                 (void*)nLightSetIsLocal },
1442{"nLightCreate",                   "()I",                                  (void*)nLightCreate },
1443{"nLightSetColor",                 "(IFFF)V",                              (void*)nLightSetColor },
1444{"nLightSetPosition",              "(IFFF)V",                              (void*)nLightSetPosition },
1445
1446{"nContextBindRootScript",         "(I)V",                                 (void*)nContextBindRootScript },
1447{"nContextBindProgramFragmentStore","(I)V",                                (void*)nContextBindProgramFragmentStore },
1448{"nContextBindProgramFragment",    "(I)V",                                 (void*)nContextBindProgramFragment },
1449{"nContextBindProgramVertex",      "(I)V",                                 (void*)nContextBindProgramVertex },
1450{"nContextBindProgramRaster",      "(I)V",                                 (void*)nContextBindProgramRaster },
1451
1452{"nSamplerBegin",                  "()V",                                  (void*)nSamplerBegin },
1453{"nSamplerSet",                    "(II)V",                                (void*)nSamplerSet },
1454{"nSamplerCreate",                 "()I",                                  (void*)nSamplerCreate },
1455
1456{"nSimpleMeshCreate",              "(II[II)I",                             (void*)nSimpleMeshCreate },
1457{"nSimpleMeshBindVertex",          "(III)V",                               (void*)nSimpleMeshBindVertex },
1458{"nSimpleMeshBindIndex",           "(II)V",                                (void*)nSimpleMeshBindIndex },
1459
1460};
1461
1462static int registerFuncs(JNIEnv *_env)
1463{
1464    return android::AndroidRuntime::registerNativeMethods(
1465            _env, classPathName, methods, NELEM(methods));
1466}
1467
1468// ---------------------------------------------------------------------------
1469
1470jint JNI_OnLoad(JavaVM* vm, void* reserved)
1471{
1472    JNIEnv* env = NULL;
1473    jint result = -1;
1474
1475    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
1476        LOGE("ERROR: GetEnv failed\n");
1477        goto bail;
1478    }
1479    assert(env != NULL);
1480
1481    if (registerFuncs(env) < 0) {
1482        LOGE("ERROR: MediaPlayer native registration failed\n");
1483        goto bail;
1484    }
1485
1486    /* success -- return valid version number */
1487    result = JNI_VERSION_1_4;
1488
1489bail:
1490    return result;
1491}
1492