android_renderscript_RenderScript.cpp revision dfac814c18f73dd7289f9927edca3e3b6ec6bc00
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
300static void
301nElementGetNativeData(JNIEnv *_env, jobject _this, jint id, jintArray _elementData)
302{
303    int dataSize = _env->GetArrayLength(_elementData);
304    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
305    LOG_API("nElementGetNativeData, con(%p)", con);
306
307    // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
308    assert(dataSize == 5);
309
310    uint32_t elementData[5];
311    rsElementGetNativeData(con, (RsElement)id, elementData, dataSize);
312
313    for(jint i = 0; i < dataSize; i ++) {
314        _env->SetIntArrayRegion(_elementData, i, 1, (const jint*)&elementData[i]);
315    }
316}
317
318
319static void
320nElementGetSubElements(JNIEnv *_env, jobject _this, jint id, jintArray _IDs, jobjectArray _names)
321{
322    int dataSize = _env->GetArrayLength(_IDs);
323    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
324    LOG_API("nElementGetSubElements, con(%p)", con);
325
326    uint32_t *ids = (uint32_t *)malloc((uint32_t)dataSize * sizeof(uint32_t));
327    const char **names = (const char **)malloc((uint32_t)dataSize * sizeof(const char *));
328
329    rsElementGetSubElements(con, (RsElement)id, ids, names, (uint32_t)dataSize);
330
331    for(jint i = 0; i < dataSize; i ++) {
332        _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
333        _env->SetIntArrayRegion(_IDs, i, 1, (const jint*)&ids[i]);
334    }
335
336    free(ids);
337    free(names);
338}
339
340// -----------------------------------
341
342static void
343nTypeBegin(JNIEnv *_env, jobject _this, jint eID)
344{
345    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
346    LOG_API("nTypeBegin, con(%p) e(%p)", con, (RsElement)eID);
347    rsTypeBegin(con, (RsElement)eID);
348}
349
350static void
351nTypeAdd(JNIEnv *_env, jobject _this, jint dim, jint val)
352{
353    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
354    LOG_API("nTypeAdd, con(%p) dim(%i), val(%i)", con, dim, val);
355    rsTypeAdd(con, (RsDimension)dim, val);
356}
357
358static jint
359nTypeCreate(JNIEnv *_env, jobject _this)
360{
361    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
362    LOG_API("nTypeCreate, con(%p)", con);
363    return (jint)rsTypeCreate(con);
364}
365
366static void
367nTypeGetNativeData(JNIEnv *_env, jobject _this, jint id, jintArray _typeData)
368{
369    // We are packing 6 items: mDimX; mDimY; mDimZ;
370    // mDimLOD; mDimFaces; mElement; into typeData
371    int elementCount = _env->GetArrayLength(_typeData);
372
373    assert(elementCount == 6);
374
375    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
376    LOG_API("nTypeCreate, con(%p)", con);
377
378    uint32_t typeData[6];
379    rsTypeGetNativeData(con, (RsType)id, typeData, 6);
380
381    for(jint i = 0; i < elementCount; i ++) {
382        _env->SetIntArrayRegion(_typeData, i, 1, (const jint*)&typeData[i]);
383    }
384}
385
386static void * SF_LoadInt(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
387{
388    ((int32_t *)buffer)[0] = _env->GetIntField(_obj, _field);
389    return ((uint8_t *)buffer) + 4;
390}
391
392static void * SF_LoadShort(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
393{
394    ((int16_t *)buffer)[0] = _env->GetShortField(_obj, _field);
395    return ((uint8_t *)buffer) + 2;
396}
397
398static void * SF_LoadByte(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
399{
400    ((int8_t *)buffer)[0] = _env->GetByteField(_obj, _field);
401    return ((uint8_t *)buffer) + 1;
402}
403
404static void * SF_LoadFloat(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
405{
406    ((float *)buffer)[0] = _env->GetFloatField(_obj, _field);
407    return ((uint8_t *)buffer) + 4;
408}
409
410static void * SF_SaveInt(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
411{
412    _env->SetIntField(_obj, _field, ((int32_t *)buffer)[0]);
413    return ((uint8_t *)buffer) + 4;
414}
415
416static void * SF_SaveShort(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
417{
418    _env->SetShortField(_obj, _field, ((int16_t *)buffer)[0]);
419    return ((uint8_t *)buffer) + 2;
420}
421
422static void * SF_SaveByte(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
423{
424    _env->SetByteField(_obj, _field, ((int8_t *)buffer)[0]);
425    return ((uint8_t *)buffer) + 1;
426}
427
428static void * SF_SaveFloat(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
429{
430    _env->SetFloatField(_obj, _field, ((float *)buffer)[0]);
431    return ((uint8_t *)buffer) + 4;
432}
433
434struct TypeFieldCache {
435    jfieldID field;
436    int bits;
437    void * (*ptr)(JNIEnv *, jobject, jfieldID, void *buffer);
438    void * (*readPtr)(JNIEnv *, jobject, jfieldID, void *buffer);
439};
440
441struct TypeCache {
442    int fieldCount;
443    int size;
444    TypeFieldCache fields[1];
445};
446
447//{"nTypeFinalDestroy",              "(Landroid/renderscript/Type;)V",       (void*)nTypeFinalDestroy },
448static void
449nTypeFinalDestroy(JNIEnv *_env, jobject _this, jobject _type)
450{
451    TypeCache *tc = (TypeCache *)_env->GetIntField(_type, gTypeNativeCache);
452    free(tc);
453}
454
455// native void nTypeSetupFields(Type t, int[] types, int[] bits, Field[] IDs);
456static void
457nTypeSetupFields(JNIEnv *_env, jobject _this, jobject _type, jintArray _types, jintArray _bits, jobjectArray _IDs)
458{
459    int fieldCount = _env->GetArrayLength(_types);
460    size_t structSize = sizeof(TypeCache) + (sizeof(TypeFieldCache) * (fieldCount-1));
461    TypeCache *tc = (TypeCache *)malloc(structSize);
462    memset(tc, 0, structSize);
463
464    TypeFieldCache *tfc = &tc->fields[0];
465    tc->fieldCount = fieldCount;
466    _env->SetIntField(_type, gTypeNativeCache, (jint)tc);
467
468    jint *fType = _env->GetIntArrayElements(_types, NULL);
469    jint *fBits = _env->GetIntArrayElements(_bits, NULL);
470    for (int ct=0; ct < fieldCount; ct++) {
471        jobject field = _env->GetObjectArrayElement(_IDs, ct);
472        tfc[ct].field = _env->FromReflectedField(field);
473        tfc[ct].bits = fBits[ct];
474
475        switch(fType[ct]) {
476        case RS_TYPE_FLOAT_32:
477            tfc[ct].ptr = SF_LoadFloat;
478            tfc[ct].readPtr = SF_SaveFloat;
479            break;
480        case RS_TYPE_UNSIGNED_32:
481        case RS_TYPE_SIGNED_32:
482            tfc[ct].ptr = SF_LoadInt;
483            tfc[ct].readPtr = SF_SaveInt;
484            break;
485        case RS_TYPE_UNSIGNED_16:
486        case RS_TYPE_SIGNED_16:
487            tfc[ct].ptr = SF_LoadShort;
488            tfc[ct].readPtr = SF_SaveShort;
489            break;
490        case RS_TYPE_UNSIGNED_8:
491        case RS_TYPE_SIGNED_8:
492            tfc[ct].ptr = SF_LoadByte;
493            tfc[ct].readPtr = SF_SaveByte;
494            break;
495        }
496        tc->size += 4;
497    }
498
499    _env->ReleaseIntArrayElements(_types, fType, JNI_ABORT);
500    _env->ReleaseIntArrayElements(_bits, fBits, JNI_ABORT);
501}
502
503
504// -----------------------------------
505
506static jint
507nAllocationCreateTyped(JNIEnv *_env, jobject _this, jint e)
508{
509    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
510    LOG_API("nAllocationCreateTyped, con(%p), e(%p)", con, (RsElement)e);
511    return (jint) rsAllocationCreateTyped(con, (RsElement)e);
512}
513
514static void
515nAllocationUploadToTexture(JNIEnv *_env, jobject _this, jint a, jboolean genMip, jint mip)
516{
517    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
518    LOG_API("nAllocationUploadToTexture, con(%p), a(%p), genMip(%i), mip(%i)", con, (RsAllocation)a, genMip, mip);
519    rsAllocationUploadToTexture(con, (RsAllocation)a, genMip, mip);
520}
521
522static void
523nAllocationUploadToBufferObject(JNIEnv *_env, jobject _this, jint a)
524{
525    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
526    LOG_API("nAllocationUploadToBufferObject, con(%p), a(%p)", con, (RsAllocation)a);
527    rsAllocationUploadToBufferObject(con, (RsAllocation)a);
528}
529
530static RsElement SkBitmapToPredefined(SkBitmap::Config cfg)
531{
532    switch (cfg) {
533    case SkBitmap::kA8_Config:
534        return g_A_8;
535    case SkBitmap::kARGB_4444_Config:
536        return g_RGBA_4444;
537    case SkBitmap::kARGB_8888_Config:
538        return g_RGBA_8888;
539    case SkBitmap::kRGB_565_Config:
540        return g_RGB_565;
541
542    default:
543        break;
544    }
545    // If we don't have a conversion mark it as a user type.
546    LOGE("Unsupported bitmap type");
547    return NULL;
548}
549
550static int
551nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, jint dstFmt, jboolean genMips, jobject jbitmap)
552{
553    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
554    SkBitmap const * nativeBitmap =
555            (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID);
556    const SkBitmap& bitmap(*nativeBitmap);
557    SkBitmap::Config config = bitmap.getConfig();
558
559    RsElement e = SkBitmapToPredefined(config);
560    if (e) {
561        bitmap.lockPixels();
562        const int w = bitmap.width();
563        const int h = bitmap.height();
564        const void* ptr = bitmap.getPixels();
565        jint id = (jint)rsAllocationCreateFromBitmap(con, w, h, (RsElement)dstFmt, e, genMips, ptr);
566        bitmap.unlockPixels();
567        return id;
568    }
569    return 0;
570}
571
572static void ReleaseBitmapCallback(void *bmp)
573{
574    SkBitmap const * nativeBitmap = (SkBitmap const *)bmp;
575    nativeBitmap->unlockPixels();
576}
577
578static int
579nAllocationCreateBitmapRef(JNIEnv *_env, jobject _this, jint type, jobject jbitmap)
580{
581    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
582    SkBitmap * nativeBitmap =
583            (SkBitmap *)_env->GetIntField(jbitmap, gNativeBitmapID);
584
585
586    nativeBitmap->lockPixels();
587    void* ptr = nativeBitmap->getPixels();
588    jint id = (jint)rsAllocationCreateBitmapRef(con, (RsType)type, ptr, nativeBitmap, ReleaseBitmapCallback);
589    return id;
590}
591
592static int
593nAllocationCreateFromAssetStream(JNIEnv *_env, jobject _this, jint dstFmt, jboolean genMips, jint native_asset)
594{
595    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
596
597    Asset* asset = reinterpret_cast<Asset*>(native_asset);
598    SkBitmap bitmap;
599    SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
600            &bitmap, SkBitmap::kNo_Config, SkImageDecoder::kDecodePixels_Mode);
601
602    SkBitmap::Config config = bitmap.getConfig();
603
604    RsElement e = SkBitmapToPredefined(config);
605
606    if (e) {
607        bitmap.lockPixels();
608        const int w = bitmap.width();
609        const int h = bitmap.height();
610        const void* ptr = bitmap.getPixels();
611        jint id = (jint)rsAllocationCreateFromBitmap(con, w, h, (RsElement)dstFmt, e, genMips, ptr);
612        bitmap.unlockPixels();
613        return id;
614    }
615    return 0;
616}
617
618static int
619nAllocationCreateFromBitmapBoxed(JNIEnv *_env, jobject _this, jint dstFmt, jboolean genMips, jobject jbitmap)
620{
621    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
622    SkBitmap const * nativeBitmap =
623            (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID);
624    const SkBitmap& bitmap(*nativeBitmap);
625    SkBitmap::Config config = bitmap.getConfig();
626
627    RsElement e = SkBitmapToPredefined(config);
628
629    if (e) {
630        bitmap.lockPixels();
631        const int w = bitmap.width();
632        const int h = bitmap.height();
633        const void* ptr = bitmap.getPixels();
634        jint id = (jint)rsAllocationCreateFromBitmapBoxed(con, w, h, (RsElement)dstFmt, e, genMips, ptr);
635        bitmap.unlockPixels();
636        return id;
637    }
638    return 0;
639}
640
641
642static void
643nAllocationSubData1D_i(JNIEnv *_env, jobject _this, jint alloc, jint offset, jint count, jintArray data, int sizeBytes)
644{
645    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
646    jint len = _env->GetArrayLength(data);
647    LOG_API("nAllocation1DSubData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
648    jint *ptr = _env->GetIntArrayElements(data, NULL);
649    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
650    _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
651}
652
653static void
654nAllocationSubData1D_s(JNIEnv *_env, jobject _this, jint alloc, jint offset, jint count, jshortArray data, int sizeBytes)
655{
656    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
657    jint len = _env->GetArrayLength(data);
658    LOG_API("nAllocation1DSubData_s, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
659    jshort *ptr = _env->GetShortArrayElements(data, NULL);
660    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
661    _env->ReleaseShortArrayElements(data, ptr, JNI_ABORT);
662}
663
664static void
665nAllocationSubData1D_b(JNIEnv *_env, jobject _this, jint alloc, jint offset, jint count, jbyteArray data, int sizeBytes)
666{
667    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
668    jint len = _env->GetArrayLength(data);
669    LOG_API("nAllocation1DSubData_b, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
670    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
671    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
672    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
673}
674
675static void
676nAllocationSubData1D_f(JNIEnv *_env, jobject _this, jint alloc, jint offset, jint count, jfloatArray data, int sizeBytes)
677{
678    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
679    jint len = _env->GetArrayLength(data);
680    LOG_API("nAllocation1DSubData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
681    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
682    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
683    _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
684}
685
686static void
687nAllocationSubData2D_i(JNIEnv *_env, jobject _this, jint alloc, jint xoff, jint yoff, jint w, jint h, jintArray data, int sizeBytes)
688{
689    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
690    jint len = _env->GetArrayLength(data);
691    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);
692    jint *ptr = _env->GetIntArrayElements(data, NULL);
693    rsAllocation2DSubData(con, (RsAllocation)alloc, xoff, yoff, w, h, ptr, sizeBytes);
694    _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
695}
696
697static void
698nAllocationSubData2D_f(JNIEnv *_env, jobject _this, jint alloc, jint xoff, jint yoff, jint w, jint h, jfloatArray data, int sizeBytes)
699{
700    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
701    jint len = _env->GetArrayLength(data);
702    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);
703    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
704    rsAllocation2DSubData(con, (RsAllocation)alloc, xoff, yoff, w, h, ptr, sizeBytes);
705    _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
706}
707
708static void
709nAllocationRead_i(JNIEnv *_env, jobject _this, jint alloc, jintArray data)
710{
711    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
712    jint len = _env->GetArrayLength(data);
713    LOG_API("nAllocationRead_i, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
714    jint *ptr = _env->GetIntArrayElements(data, NULL);
715    rsAllocationRead(con, (RsAllocation)alloc, ptr);
716    _env->ReleaseIntArrayElements(data, ptr, 0);
717}
718
719static void
720nAllocationRead_f(JNIEnv *_env, jobject _this, jint alloc, jfloatArray data)
721{
722    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
723    jint len = _env->GetArrayLength(data);
724    LOG_API("nAllocationRead_f, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
725    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
726    rsAllocationRead(con, (RsAllocation)alloc, ptr);
727    _env->ReleaseFloatArrayElements(data, ptr, 0);
728}
729
730
731//{"nAllocationDataFromObject",      "(ILandroid/renderscript/Type;Ljava/lang/Object;)V",   (void*)nAllocationDataFromObject },
732static void
733nAllocationSubDataFromObject(JNIEnv *_env, jobject _this, jint alloc, jobject _type, jint offset, jobject _o)
734{
735    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
736    LOG_API("nAllocationDataFromObject con(%p), alloc(%p)", con, (RsAllocation)alloc);
737
738    const TypeCache *tc = (TypeCache *)_env->GetIntField(_type, gTypeNativeCache);
739
740    void * bufAlloc = malloc(tc->size);
741    void * buf = bufAlloc;
742    for (int ct=0; ct < tc->fieldCount; ct++) {
743        const TypeFieldCache *tfc = &tc->fields[ct];
744        buf = tfc->ptr(_env, _o, tfc->field, buf);
745    }
746    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, 1, bufAlloc, tc->size);
747    free(bufAlloc);
748}
749
750static void
751nAllocationSubReadFromObject(JNIEnv *_env, jobject _this, jint alloc, jobject _type, jint offset, jobject _o)
752{
753    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
754    LOG_API("nAllocationReadFromObject con(%p), alloc(%p)", con, (RsAllocation)alloc);
755
756    assert(offset == 0);
757
758    const TypeCache *tc = (TypeCache *)_env->GetIntField(_type, gTypeNativeCache);
759
760    void * bufAlloc = malloc(tc->size);
761    void * buf = bufAlloc;
762    rsAllocationRead(con, (RsAllocation)alloc, bufAlloc);
763
764    for (int ct=0; ct < tc->fieldCount; ct++) {
765        const TypeFieldCache *tfc = &tc->fields[ct];
766        buf = tfc->readPtr(_env, _o, tfc->field, buf);
767    }
768    free(bufAlloc);
769}
770
771static jint
772nAllocationGetType(JNIEnv *_env, jobject _this, jint a)
773{
774    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
775    LOG_API("nAllocationGetType, con(%p), a(%p)", con, (RsAllocation)a);
776    return (jint) rsAllocationGetType(con, (RsAllocation)a);
777}
778
779// -----------------------------------
780
781static int
782nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, jint native_asset)
783{
784    LOGV("______nFileA3D %u", (uint32_t) native_asset);
785    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
786
787    Asset* asset = reinterpret_cast<Asset*>(native_asset);
788
789    jint id = (jint)rsFileA3DCreateFromAssetStream(con, asset->getBuffer(false), asset->getLength());
790    return id;
791}
792
793static int
794nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, jint fileA3D)
795{
796    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
797
798    int32_t numEntries = 0;
799    rsFileA3DGetNumIndexEntries(con, &numEntries, (RsFile)fileA3D);
800    return numEntries;
801}
802
803static void
804nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, jint fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
805{
806    LOGV("______nFileA3D %u", (uint32_t) fileA3D);
807    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
808
809    RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
810
811    rsFileA3DGetIndexEntries(con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
812
813    for(jint i = 0; i < numEntries; i ++) {
814        _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
815        _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
816    }
817
818    free(fileEntries);
819}
820
821static int
822nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, jint fileA3D, jint index)
823{
824    LOGV("______nFileA3D %u", (uint32_t) fileA3D);
825    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
826
827    jint id = (jint)rsFileA3DGetEntryByIndex(con, (uint32_t)index, (RsFile)fileA3D);
828    return id;
829}
830
831// -----------------------------------
832
833static int
834nFontCreateFromFile(JNIEnv *_env, jobject _this, jstring fileName, jint fontSize, jint dpi)
835{
836    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
837    const char* fileNameUTF = _env->GetStringUTFChars(fileName, NULL);
838
839    jint id = (jint)rsFontCreateFromFile(con, fileNameUTF, fontSize, dpi);
840    return id;
841}
842
843
844// -----------------------------------
845
846static void
847nAdapter1DBindAllocation(JNIEnv *_env, jobject _this, jint adapter, jint alloc)
848{
849    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
850    LOG_API("nAdapter1DBindAllocation, con(%p), adapter(%p), alloc(%p)", con, (RsAdapter1D)adapter, (RsAllocation)alloc);
851    rsAdapter1DBindAllocation(con, (RsAdapter1D)adapter, (RsAllocation)alloc);
852}
853
854static void
855nAdapter1DSetConstraint(JNIEnv *_env, jobject _this, jint adapter, jint dim, jint value)
856{
857    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
858    LOG_API("nAdapter1DSetConstraint, con(%p), adapter(%p), dim(%i), value(%i)", con, (RsAdapter1D)adapter, dim, value);
859    rsAdapter1DSetConstraint(con, (RsAdapter1D)adapter, (RsDimension)dim, value);
860}
861
862static void
863nAdapter1DData_i(JNIEnv *_env, jobject _this, jint adapter, jintArray data)
864{
865    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
866    jint len = _env->GetArrayLength(data);
867    LOG_API("nAdapter1DData_i, con(%p), adapter(%p), len(%i)", con, (RsAdapter1D)adapter, len);
868    jint *ptr = _env->GetIntArrayElements(data, NULL);
869    rsAdapter1DData(con, (RsAdapter1D)adapter, ptr);
870    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
871}
872
873static void
874nAdapter1DSubData_i(JNIEnv *_env, jobject _this, jint adapter, jint offset, jint count, jintArray data)
875{
876    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
877    jint len = _env->GetArrayLength(data);
878    LOG_API("nAdapter1DSubData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAdapter1D)adapter, offset, count, len);
879    jint *ptr = _env->GetIntArrayElements(data, NULL);
880    rsAdapter1DSubData(con, (RsAdapter1D)adapter, offset, count, ptr);
881    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
882}
883
884static void
885nAdapter1DData_f(JNIEnv *_env, jobject _this, jint adapter, jfloatArray data)
886{
887    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
888    jint len = _env->GetArrayLength(data);
889    LOG_API("nAdapter1DData_f, con(%p), adapter(%p), len(%i)", con, (RsAdapter1D)adapter, len);
890    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
891    rsAdapter1DData(con, (RsAdapter1D)adapter, ptr);
892    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
893}
894
895static void
896nAdapter1DSubData_f(JNIEnv *_env, jobject _this, jint adapter, jint offset, jint count, jfloatArray data)
897{
898    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
899    jint len = _env->GetArrayLength(data);
900    LOG_API("nAdapter1DSubData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAdapter1D)adapter, offset, count, len);
901    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
902    rsAdapter1DSubData(con, (RsAdapter1D)adapter, offset, count, ptr);
903    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
904}
905
906static jint
907nAdapter1DCreate(JNIEnv *_env, jobject _this)
908{
909    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
910    LOG_API("nAdapter1DCreate, con(%p)", con);
911    return (jint)rsAdapter1DCreate(con);
912}
913
914// -----------------------------------
915
916static void
917nAdapter2DBindAllocation(JNIEnv *_env, jobject _this, jint adapter, jint alloc)
918{
919    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
920    LOG_API("nAdapter2DBindAllocation, con(%p), adapter(%p), alloc(%p)", con, (RsAdapter2D)adapter, (RsAllocation)alloc);
921    rsAdapter2DBindAllocation(con, (RsAdapter2D)adapter, (RsAllocation)alloc);
922}
923
924static void
925nAdapter2DSetConstraint(JNIEnv *_env, jobject _this, jint adapter, jint dim, jint value)
926{
927    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
928    LOG_API("nAdapter2DSetConstraint, con(%p), adapter(%p), dim(%i), value(%i)", con, (RsAdapter2D)adapter, dim, value);
929    rsAdapter2DSetConstraint(con, (RsAdapter2D)adapter, (RsDimension)dim, value);
930}
931
932static void
933nAdapter2DData_i(JNIEnv *_env, jobject _this, jint adapter, jintArray data)
934{
935    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
936    jint len = _env->GetArrayLength(data);
937    LOG_API("nAdapter2DData_i, con(%p), adapter(%p), len(%i)", con, (RsAdapter2D)adapter, len);
938    jint *ptr = _env->GetIntArrayElements(data, NULL);
939    rsAdapter2DData(con, (RsAdapter2D)adapter, ptr);
940    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
941}
942
943static void
944nAdapter2DData_f(JNIEnv *_env, jobject _this, jint adapter, jfloatArray data)
945{
946    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
947    jint len = _env->GetArrayLength(data);
948    LOG_API("nAdapter2DData_f, con(%p), adapter(%p), len(%i)", con, (RsAdapter2D)adapter, len);
949    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
950    rsAdapter2DData(con, (RsAdapter2D)adapter, ptr);
951    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
952}
953
954static void
955nAdapter2DSubData_i(JNIEnv *_env, jobject _this, jint adapter, jint xoff, jint yoff, jint w, jint h, jintArray data)
956{
957    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
958    jint len = _env->GetArrayLength(data);
959    LOG_API("nAdapter2DSubData_i, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)",
960            con, (RsAdapter2D)adapter, xoff, yoff, w, h, len);
961    jint *ptr = _env->GetIntArrayElements(data, NULL);
962    rsAdapter2DSubData(con, (RsAdapter2D)adapter, xoff, yoff, w, h, ptr);
963    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
964}
965
966static void
967nAdapter2DSubData_f(JNIEnv *_env, jobject _this, jint adapter, jint xoff, jint yoff, jint w, jint h, jfloatArray data)
968{
969    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
970    jint len = _env->GetArrayLength(data);
971    LOG_API("nAdapter2DSubData_f, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)",
972            con, (RsAdapter2D)adapter, xoff, yoff, w, h, len);
973    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
974    rsAdapter2DSubData(con, (RsAdapter1D)adapter, xoff, yoff, w, h, ptr);
975    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
976}
977
978static jint
979nAdapter2DCreate(JNIEnv *_env, jobject _this)
980{
981    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
982    LOG_API("nAdapter2DCreate, con(%p)", con);
983    return (jint)rsAdapter2DCreate(con);
984}
985
986// -----------------------------------
987
988static void
989nScriptBindAllocation(JNIEnv *_env, jobject _this, jint script, jint alloc, jint slot)
990{
991    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
992    LOG_API("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", con, (RsScript)script, (RsAllocation)alloc, slot);
993    rsScriptBindAllocation(con, (RsScript)script, (RsAllocation)alloc, slot);
994}
995
996static void
997nScriptSetVarI(JNIEnv *_env, jobject _this, jint script, jint slot, jint val)
998{
999    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1000    LOG_API("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i), b(%f), a(%f)", con, (void *)script, slot, val);
1001    rsScriptSetVarI(con, (RsScript)script, slot, val);
1002}
1003
1004static void
1005nScriptSetVarF(JNIEnv *_env, jobject _this, jint script, jint slot, float val)
1006{
1007    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1008    LOG_API("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i), b(%f), a(%f)", con, (void *)script, slot, val);
1009    rsScriptSetVarF(con, (RsScript)script, slot, val);
1010}
1011
1012static void
1013nScriptSetVarV(JNIEnv *_env, jobject _this, jint script, jint slot, jbyteArray data)
1014{
1015    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1016    LOG_API("nScriptSetVarV, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
1017    jint len = _env->GetArrayLength(data);
1018    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
1019    rsScriptSetVarV(con, (RsScript)script, slot, ptr, len);
1020    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1021}
1022
1023
1024static void
1025nScriptSetTimeZone(JNIEnv *_env, jobject _this, jint script, jbyteArray timeZone)
1026{
1027    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1028    LOG_API("nScriptCSetTimeZone, con(%p), s(%p), timeZone(%s)", con, (void *)script, (const char *)timeZone);
1029
1030    jint length = _env->GetArrayLength(timeZone);
1031    jbyte* timeZone_ptr;
1032    timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
1033
1034    rsScriptSetTimeZone(con, (RsScript)script, (const char *)timeZone_ptr, length);
1035
1036    if (timeZone_ptr) {
1037        _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
1038    }
1039}
1040
1041static void
1042nScriptInvoke(JNIEnv *_env, jobject _this, jint obj, jint slot)
1043{
1044    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1045    LOG_API("nScriptInvoke, con(%p), script(%p)", con, (void *)obj);
1046    rsScriptInvoke(con, (RsScript)obj, slot);
1047}
1048
1049static void
1050nScriptInvokeV(JNIEnv *_env, jobject _this, jint script, jint slot, jbyteArray data)
1051{
1052    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1053    LOG_API("nScriptInvokeV, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
1054    jint len = _env->GetArrayLength(data);
1055    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
1056    rsScriptInvokeV(con, (RsScript)script, slot, ptr, len);
1057    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1058}
1059
1060
1061// -----------------------------------
1062
1063static void
1064nScriptCBegin(JNIEnv *_env, jobject _this)
1065{
1066    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1067    LOG_API("nScriptCBegin, con(%p)", con);
1068    rsScriptCBegin(con);
1069}
1070
1071static void
1072nScriptCSetScript(JNIEnv *_env, jobject _this, jbyteArray scriptRef,
1073                  jint offset, jint length)
1074{
1075    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1076    LOG_API("!!! nScriptCSetScript, con(%p)", con);
1077    jint _exception = 0;
1078    jint remaining;
1079    jbyte* script_base = 0;
1080    jbyte* script_ptr;
1081    if (!scriptRef) {
1082        _exception = 1;
1083        //_env->ThrowNew(IAEClass, "script == null");
1084        goto exit;
1085    }
1086    if (offset < 0) {
1087        _exception = 1;
1088        //_env->ThrowNew(IAEClass, "offset < 0");
1089        goto exit;
1090    }
1091    if (length < 0) {
1092        _exception = 1;
1093        //_env->ThrowNew(IAEClass, "length < 0");
1094        goto exit;
1095    }
1096    remaining = _env->GetArrayLength(scriptRef) - offset;
1097    if (remaining < length) {
1098        _exception = 1;
1099        //_env->ThrowNew(IAEClass, "length > script.length - offset");
1100        goto exit;
1101    }
1102    script_base = (jbyte *)
1103        _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
1104    script_ptr = script_base + offset;
1105
1106    rsScriptCSetText(con, (const char *)script_ptr, length);
1107
1108exit:
1109    if (script_base) {
1110        _env->ReleasePrimitiveArrayCritical(scriptRef, script_base,
1111                _exception ? JNI_ABORT: 0);
1112    }
1113}
1114
1115static jint
1116nScriptCCreate(JNIEnv *_env, jobject _this)
1117{
1118    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1119    LOG_API("nScriptCCreate, con(%p)", con);
1120    return (jint)rsScriptCCreate(con);
1121}
1122
1123// ---------------------------------------------------------------------------
1124
1125static void
1126nProgramStoreBegin(JNIEnv *_env, jobject _this, jint in, jint out)
1127{
1128    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1129    LOG_API("nProgramStoreBegin, con(%p), in(%p), out(%p)", con, (RsElement)in, (RsElement)out);
1130    rsProgramStoreBegin(con, (RsElement)in, (RsElement)out);
1131}
1132
1133static void
1134nProgramStoreDepthFunc(JNIEnv *_env, jobject _this, jint func)
1135{
1136    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1137    LOG_API("nProgramStoreDepthFunc, con(%p), func(%i)", con, func);
1138    rsProgramStoreDepthFunc(con, (RsDepthFunc)func);
1139}
1140
1141static void
1142nProgramStoreDepthMask(JNIEnv *_env, jobject _this, jboolean enable)
1143{
1144    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1145    LOG_API("nProgramStoreDepthMask, con(%p), enable(%i)", con, enable);
1146    rsProgramStoreDepthMask(con, enable);
1147}
1148
1149static void
1150nProgramStoreColorMask(JNIEnv *_env, jobject _this, jboolean r, jboolean g, jboolean b, jboolean a)
1151{
1152    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1153    LOG_API("nProgramStoreColorMask, con(%p), r(%i), g(%i), b(%i), a(%i)", con, r, g, b, a);
1154    rsProgramStoreColorMask(con, r, g, b, a);
1155}
1156
1157static void
1158nProgramStoreBlendFunc(JNIEnv *_env, jobject _this, int src, int dst)
1159{
1160    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1161    LOG_API("nProgramStoreBlendFunc, con(%p), src(%i), dst(%i)", con, src, dst);
1162    rsProgramStoreBlendFunc(con, (RsBlendSrcFunc)src, (RsBlendDstFunc)dst);
1163}
1164
1165static void
1166nProgramStoreDither(JNIEnv *_env, jobject _this, jboolean enable)
1167{
1168    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1169    LOG_API("nProgramStoreDither, con(%p), enable(%i)", con, enable);
1170    rsProgramStoreDither(con, enable);
1171}
1172
1173static jint
1174nProgramStoreCreate(JNIEnv *_env, jobject _this)
1175{
1176    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1177    LOG_API("nProgramStoreCreate, con(%p)", con);
1178
1179    return (jint)rsProgramStoreCreate(con);
1180}
1181
1182// ---------------------------------------------------------------------------
1183
1184static void
1185nProgramBindConstants(JNIEnv *_env, jobject _this, jint vpv, jint slot, jint a)
1186{
1187    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1188    LOG_API("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", con, (RsProgramVertex)vpv, slot, (RsAllocation)a);
1189    rsProgramBindConstants(con, (RsProgram)vpv, slot, (RsAllocation)a);
1190}
1191
1192static void
1193nProgramBindTexture(JNIEnv *_env, jobject _this, jint vpf, jint slot, jint a)
1194{
1195    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1196    LOG_API("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
1197    rsProgramBindTexture(con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
1198}
1199
1200static void
1201nProgramBindSampler(JNIEnv *_env, jobject _this, jint vpf, jint slot, jint a)
1202{
1203    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1204    LOG_API("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsSampler)a);
1205    rsProgramBindSampler(con, (RsProgramFragment)vpf, slot, (RsSampler)a);
1206}
1207
1208// ---------------------------------------------------------------------------
1209
1210static jint
1211nProgramFragmentCreate(JNIEnv *_env, jobject _this, jintArray params)
1212{
1213    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1214    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1215    jint paramLen = _env->GetArrayLength(params);
1216
1217    LOG_API("nProgramFragmentCreate, con(%p), paramLen(%i)", con, paramLen);
1218
1219    jint ret = (jint)rsProgramFragmentCreate(con, (uint32_t *)paramPtr, paramLen);
1220    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1221    return ret;
1222}
1223
1224static jint
1225nProgramFragmentCreate2(JNIEnv *_env, jobject _this, jstring shader, jintArray params)
1226{
1227    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1228    const char* shaderUTF = _env->GetStringUTFChars(shader, NULL);
1229    jint shaderLen = _env->GetStringUTFLength(shader);
1230    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1231    jint paramLen = _env->GetArrayLength(params);
1232
1233    LOG_API("nProgramFragmentCreate2, con(%p), shaderLen(%i), paramLen(%i)", con, shaderLen, paramLen);
1234
1235    jint ret = (jint)rsProgramFragmentCreate2(con, shaderUTF, shaderLen, (uint32_t *)paramPtr, paramLen);
1236    _env->ReleaseStringUTFChars(shader, shaderUTF);
1237    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1238    return ret;
1239}
1240
1241
1242// ---------------------------------------------------------------------------
1243
1244static jint
1245nProgramVertexCreate(JNIEnv *_env, jobject _this, jboolean texMat)
1246{
1247    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1248    LOG_API("nProgramVertexCreate, con(%p), texMat(%i)", con, texMat);
1249    return (jint)rsProgramVertexCreate(con, texMat);
1250}
1251
1252static jint
1253nProgramVertexCreate2(JNIEnv *_env, jobject _this, jstring shader, jintArray params)
1254{
1255    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1256    const char* shaderUTF = _env->GetStringUTFChars(shader, NULL);
1257    jint shaderLen = _env->GetStringUTFLength(shader);
1258    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1259    jint paramLen = _env->GetArrayLength(params);
1260
1261    LOG_API("nProgramVertexCreate2, con(%p), shaderLen(%i), paramLen(%i)", con, shaderLen, paramLen);
1262
1263    jint ret = (jint)rsProgramVertexCreate2(con, shaderUTF, shaderLen, (uint32_t *)paramPtr, paramLen);
1264    _env->ReleaseStringUTFChars(shader, shaderUTF);
1265    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1266    return ret;
1267}
1268
1269// ---------------------------------------------------------------------------
1270
1271static jint
1272nProgramRasterCreate(JNIEnv *_env, jobject _this, jboolean pointSmooth, jboolean lineSmooth, jboolean pointSprite)
1273{
1274    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1275    LOG_API("nProgramRasterCreate, con(%p), pointSmooth(%i), lineSmooth(%i), pointSprite(%i)",
1276            con, pointSmooth, lineSmooth, pointSprite);
1277    return (jint)rsProgramRasterCreate(con, pointSmooth, lineSmooth, pointSprite);
1278}
1279
1280static void
1281nProgramRasterSetLineWidth(JNIEnv *_env, jobject _this, jint vpr, jfloat v)
1282{
1283    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1284    LOG_API("nProgramRasterSetLineWidth, con(%p), vpf(%p), value(%f)", con, (RsProgramRaster)vpr, v);
1285    rsProgramRasterSetLineWidth(con, (RsProgramRaster)vpr, v);
1286}
1287
1288static void
1289nProgramRasterSetCullMode(JNIEnv *_env, jobject _this, jint vpr, jint v)
1290{
1291    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1292    LOG_API("nProgramRasterSetCullMode, con(%p), vpf(%p), value(%i)", con, (RsProgramRaster)vpr, v);
1293    rsProgramRasterSetCullMode(con, (RsProgramRaster)vpr, (RsCullMode)v);
1294}
1295
1296
1297// ---------------------------------------------------------------------------
1298
1299static void
1300nContextBindRootScript(JNIEnv *_env, jobject _this, jint script)
1301{
1302    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1303    LOG_API("nContextBindRootScript, con(%p), script(%p)", con, (RsScript)script);
1304    rsContextBindRootScript(con, (RsScript)script);
1305}
1306
1307static void
1308nContextBindProgramStore(JNIEnv *_env, jobject _this, jint pfs)
1309{
1310    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1311    LOG_API("nContextBindProgramStore, con(%p), pfs(%p)", con, (RsProgramStore)pfs);
1312    rsContextBindProgramStore(con, (RsProgramStore)pfs);
1313}
1314
1315static void
1316nContextBindProgramFragment(JNIEnv *_env, jobject _this, jint pf)
1317{
1318    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1319    LOG_API("nContextBindProgramFragment, con(%p), pf(%p)", con, (RsProgramFragment)pf);
1320    rsContextBindProgramFragment(con, (RsProgramFragment)pf);
1321}
1322
1323static void
1324nContextBindProgramVertex(JNIEnv *_env, jobject _this, jint pf)
1325{
1326    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1327    LOG_API("nContextBindProgramVertex, con(%p), pf(%p)", con, (RsProgramVertex)pf);
1328    rsContextBindProgramVertex(con, (RsProgramVertex)pf);
1329}
1330
1331static void
1332nContextBindProgramRaster(JNIEnv *_env, jobject _this, jint pf)
1333{
1334    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1335    LOG_API("nContextBindProgramRaster, con(%p), pf(%p)", con, (RsProgramRaster)pf);
1336    rsContextBindProgramRaster(con, (RsProgramRaster)pf);
1337}
1338
1339
1340// ---------------------------------------------------------------------------
1341
1342static void
1343nSamplerBegin(JNIEnv *_env, jobject _this)
1344{
1345    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1346    LOG_API("nSamplerBegin, con(%p)", con);
1347    rsSamplerBegin(con);
1348}
1349
1350static void
1351nSamplerSet(JNIEnv *_env, jobject _this, jint p, jint v)
1352{
1353    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1354    LOG_API("nSamplerSet, con(%p), param(%i), value(%i)", con, p, v);
1355    rsSamplerSet(con, (RsSamplerParam)p, (RsSamplerValue)v);
1356}
1357
1358static jint
1359nSamplerCreate(JNIEnv *_env, jobject _this)
1360{
1361    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1362    LOG_API("nSamplerCreate, con(%p)", con);
1363    return (jint)rsSamplerCreate(con);
1364}
1365
1366// ---------------------------------------------------------------------------
1367
1368static void
1369nLightBegin(JNIEnv *_env, jobject _this)
1370{
1371    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1372    LOG_API("nLightBegin, con(%p)", con);
1373    rsLightBegin(con);
1374}
1375
1376static void
1377nLightSetIsMono(JNIEnv *_env, jobject _this, jboolean isMono)
1378{
1379    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1380    LOG_API("nLightSetIsMono, con(%p), isMono(%i)", con, isMono);
1381    rsLightSetMonochromatic(con, isMono);
1382}
1383
1384static void
1385nLightSetIsLocal(JNIEnv *_env, jobject _this, jboolean isLocal)
1386{
1387    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1388    LOG_API("nLightSetIsLocal, con(%p), isLocal(%i)", con, isLocal);
1389    rsLightSetLocal(con, isLocal);
1390}
1391
1392static jint
1393nLightCreate(JNIEnv *_env, jobject _this)
1394{
1395    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1396    LOG_API("nLightCreate, con(%p)", con);
1397    return (jint)rsLightCreate(con);
1398}
1399
1400static void
1401nLightSetColor(JNIEnv *_env, jobject _this, jint light, float r, float g, float b)
1402{
1403    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1404    LOG_API("nLightSetColor, con(%p), light(%p), r(%f), g(%f), b(%f)", con, (RsLight)light, r, g, b);
1405    rsLightSetColor(con, (RsLight)light, r, g, b);
1406}
1407
1408static void
1409nLightSetPosition(JNIEnv *_env, jobject _this, jint light, float x, float y, float z)
1410{
1411    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1412    LOG_API("nLightSetPosition, con(%p), light(%p), x(%f), y(%f), z(%f)", con, (RsLight)light, x, y, z);
1413    rsLightSetPosition(con, (RsLight)light, x, y, z);
1414}
1415
1416// ---------------------------------------------------------------------------
1417
1418static jint
1419nMeshCreate(JNIEnv *_env, jobject _this, jint vtxCount, jint idxCount)
1420{
1421    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1422    LOG_API("nMeshCreate, con(%p), vtxCount(%i), idxCount(%i)", con, vtxCount, idxCount);
1423    int id = (int)rsMeshCreate(con, vtxCount, idxCount);
1424    return id;
1425}
1426
1427static void
1428nMeshBindVertex(JNIEnv *_env, jobject _this, jint mesh, jint alloc, jint slot)
1429{
1430    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1431    LOG_API("nMeshBindVertex, con(%p), Mesh(%p), Alloc(%p), slot(%i)", con, (RsMesh)mesh, (RsAllocation)alloc, slot);
1432    rsMeshBindVertex(con, (RsMesh)mesh, (RsAllocation)alloc, slot);
1433}
1434
1435static void
1436nMeshBindIndex(JNIEnv *_env, jobject _this, jint mesh, jint alloc, jint primID, jint slot)
1437{
1438    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1439    LOG_API("nMeshBindIndex, con(%p), Mesh(%p), Alloc(%p)", con, (RsMesh)mesh, (RsAllocation)alloc);
1440    rsMeshBindIndex(con, (RsMesh)mesh, (RsAllocation)alloc, primID, slot);
1441}
1442
1443static jint
1444nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, jint mesh)
1445{
1446    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1447    LOG_API("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1448    jint vtxCount = 0;
1449    rsMeshGetVertexBufferCount(con, (RsMesh)mesh, &vtxCount);
1450    return vtxCount;
1451}
1452
1453static jint
1454nMeshGetIndexCount(JNIEnv *_env, jobject _this, jint mesh)
1455{
1456    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1457    LOG_API("nMeshGetIndexCount, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1458    jint idxCount = 0;
1459    rsMeshGetIndexCount(con, (RsMesh)mesh, &idxCount);
1460    return idxCount;
1461}
1462
1463static void
1464nMeshGetVertices(JNIEnv *_env, jobject _this, jint mesh, jintArray _ids, int numVtxIDs)
1465{
1466    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1467    LOG_API("nMeshGetVertices, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1468
1469    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
1470    rsMeshGetVertices(con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
1471
1472    for(jint i = 0; i < numVtxIDs; i ++) {
1473        _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&allocs[i]);
1474    }
1475
1476    free(allocs);
1477}
1478
1479static void
1480nMeshGetIndices(JNIEnv *_env, jobject _this, jint mesh, jintArray _idxIds, jintArray _primitives, int numIndices)
1481{
1482    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
1483    LOG_API("nMeshGetVertices, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1484
1485    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
1486    uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
1487
1488    rsMeshGetIndices(con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
1489
1490    for(jint i = 0; i < numIndices; i ++) {
1491        _env->SetIntArrayRegion(_idxIds, i, 1, (const jint*)&allocs[i]);
1492        _env->SetIntArrayRegion(_primitives, i, 1, (const jint*)&prims[i]);
1493    }
1494
1495    free(allocs);
1496    free(prims);
1497}
1498
1499// ---------------------------------------------------------------------------
1500
1501
1502static const char *classPathName = "android/renderscript/RenderScript";
1503
1504static JNINativeMethod methods[] = {
1505{"_nInit",                         "()V",                                  (void*)_nInit },
1506{"nInitElements",                  "(IIII)V",                              (void*)nInitElements },
1507
1508{"nDeviceCreate",                  "()I",                                  (void*)nDeviceCreate },
1509{"nDeviceDestroy",                 "(I)V",                                 (void*)nDeviceDestroy },
1510{"nDeviceSetConfig",               "(III)V",                               (void*)nDeviceSetConfig },
1511{"nContextCreate",                 "(II)I",                                (void*)nContextCreate },
1512{"nContextCreateGL",               "(IIZ)I",                               (void*)nContextCreateGL },
1513{"nContextFinish",                 "()V",                                  (void*)nContextFinish },
1514{"nContextSetPriority",            "(I)V",                                 (void*)nContextSetPriority },
1515{"nContextSetSurface",             "(IILandroid/view/Surface;)V",          (void*)nContextSetSurface },
1516{"nContextDestroy",                "(I)V",                                 (void*)nContextDestroy },
1517{"nContextDump",                   "(I)V",                                 (void*)nContextDump },
1518{"nContextPause",                  "()V",                                  (void*)nContextPause },
1519{"nContextResume",                 "()V",                                  (void*)nContextResume },
1520{"nAssignName",                    "(I[B)V",                               (void*)nAssignName },
1521{"nObjDestroy",                    "(I)V",                                 (void*)nObjDestroy },
1522{"nObjDestroyOOB",                 "(I)V",                                 (void*)nObjDestroyOOB },
1523{"nContextGetMessage",             "([IZ)I",                               (void*)nContextGetMessage },
1524{"nContextInitToClient",           "()V",                                  (void*)nContextInitToClient },
1525{"nContextDeinitToClient",         "()V",                                  (void*)nContextDeinitToClient },
1526
1527{"nFileOpen",                      "([B)I",                                (void*)nFileOpen },
1528{"nFileA3DCreateFromAssetStream", "(I)I",                                 (void*)nFileA3DCreateFromAssetStream },
1529{"nFileA3DGetNumIndexEntries",     "(I)I",                                 (void*)nFileA3DGetNumIndexEntries },
1530{"nFileA3DGetIndexEntries",        "(II[I[Ljava/lang/String;)V",          (void*)nFileA3DGetIndexEntries },
1531{"nFileA3DGetEntryByIndex",        "(II)I",                                (void*)nFileA3DGetEntryByIndex },
1532
1533{"nFontCreateFromFile",           "(Ljava/lang/String;II)I",             (void*)nFontCreateFromFile },
1534
1535{"nElementCreate",                 "(IIZI)I",                              (void*)nElementCreate },
1536{"nElementCreate2",                "([I[Ljava/lang/String;)I",             (void*)nElementCreate2 },
1537{"nElementGetNativeData",         "(I[I)V",                               (void*)nElementGetNativeData },
1538{"nElementGetSubElements",        "(I[I[Ljava/lang/String;)V",           (void*)nElementGetSubElements },
1539
1540{"nTypeBegin",                     "(I)V",                                 (void*)nTypeBegin },
1541{"nTypeAdd",                       "(II)V",                                (void*)nTypeAdd },
1542{"nTypeCreate",                    "()I",                                  (void*)nTypeCreate },
1543{"nTypeFinalDestroy",              "(Landroid/renderscript/Type;)V",       (void*)nTypeFinalDestroy },
1544{"nTypeSetupFields",               "(Landroid/renderscript/Type;[I[I[Ljava/lang/reflect/Field;)V", (void*)nTypeSetupFields },
1545{"nTypeGetNativeData",             "(I[I)V",                                (void*)nTypeGetNativeData },
1546
1547{"nAllocationCreateTyped",         "(I)I",                                 (void*)nAllocationCreateTyped },
1548{"nAllocationCreateFromBitmap",    "(IZLandroid/graphics/Bitmap;)I",       (void*)nAllocationCreateFromBitmap },
1549{"nAllocationCreateBitmapRef",     "(ILandroid/graphics/Bitmap;)I",        (void*)nAllocationCreateBitmapRef },
1550{"nAllocationCreateFromBitmapBoxed","(IZLandroid/graphics/Bitmap;)I",      (void*)nAllocationCreateFromBitmapBoxed },
1551{"nAllocationCreateFromAssetStream","(IZI)I",                              (void*)nAllocationCreateFromAssetStream },
1552{"nAllocationUploadToTexture",     "(IZI)V",                               (void*)nAllocationUploadToTexture },
1553{"nAllocationUploadToBufferObject","(I)V",                                 (void*)nAllocationUploadToBufferObject },
1554{"nAllocationSubData1D",           "(III[II)V",                            (void*)nAllocationSubData1D_i },
1555{"nAllocationSubData1D",           "(III[SI)V",                            (void*)nAllocationSubData1D_s },
1556{"nAllocationSubData1D",           "(III[BI)V",                            (void*)nAllocationSubData1D_b },
1557{"nAllocationSubData1D",           "(III[FI)V",                            (void*)nAllocationSubData1D_f },
1558{"nAllocationSubData2D",           "(IIIII[II)V",                          (void*)nAllocationSubData2D_i },
1559{"nAllocationSubData2D",           "(IIIII[FI)V",                          (void*)nAllocationSubData2D_f },
1560{"nAllocationRead",                "(I[I)V",                               (void*)nAllocationRead_i },
1561{"nAllocationRead",                "(I[F)V",                               (void*)nAllocationRead_f },
1562{"nAllocationSubDataFromObject",   "(ILandroid/renderscript/Type;ILjava/lang/Object;)V",   (void*)nAllocationSubDataFromObject },
1563{"nAllocationSubReadFromObject",   "(ILandroid/renderscript/Type;ILjava/lang/Object;)V",   (void*)nAllocationSubReadFromObject },
1564{"nAllocationGetType",              "(I)I",                                 (void*)nAllocationGetType},
1565
1566{"nAdapter1DBindAllocation",       "(II)V",                                (void*)nAdapter1DBindAllocation },
1567{"nAdapter1DSetConstraint",        "(III)V",                               (void*)nAdapter1DSetConstraint },
1568{"nAdapter1DData",                 "(I[I)V",                               (void*)nAdapter1DData_i },
1569{"nAdapter1DData",                 "(I[F)V",                               (void*)nAdapter1DData_f },
1570{"nAdapter1DSubData",              "(III[I)V",                             (void*)nAdapter1DSubData_i },
1571{"nAdapter1DSubData",              "(III[F)V",                             (void*)nAdapter1DSubData_f },
1572{"nAdapter1DCreate",               "()I",                                  (void*)nAdapter1DCreate },
1573
1574{"nAdapter2DBindAllocation",       "(II)V",                                (void*)nAdapter2DBindAllocation },
1575{"nAdapter2DSetConstraint",        "(III)V",                               (void*)nAdapter2DSetConstraint },
1576{"nAdapter2DData",                 "(I[I)V",                               (void*)nAdapter2DData_i },
1577{"nAdapter2DData",                 "(I[F)V",                               (void*)nAdapter2DData_f },
1578{"nAdapter2DSubData",              "(IIIII[I)V",                           (void*)nAdapter2DSubData_i },
1579{"nAdapter2DSubData",              "(IIIII[F)V",                           (void*)nAdapter2DSubData_f },
1580{"nAdapter2DCreate",               "()I",                                  (void*)nAdapter2DCreate },
1581
1582{"nScriptBindAllocation",          "(III)V",                               (void*)nScriptBindAllocation },
1583{"nScriptSetTimeZone",             "(I[B)V",                               (void*)nScriptSetTimeZone },
1584{"nScriptInvoke",                  "(II)V",                                (void*)nScriptInvoke },
1585{"nScriptInvokeV",                 "(II[B)V",                              (void*)nScriptInvokeV },
1586{"nScriptSetVarI",                 "(III)V",                               (void*)nScriptSetVarI },
1587{"nScriptSetVarF",                 "(IIF)V",                               (void*)nScriptSetVarF },
1588{"nScriptSetVarV",                 "(II[B)V",                              (void*)nScriptSetVarV },
1589
1590{"nScriptCBegin",                  "()V",                                  (void*)nScriptCBegin },
1591{"nScriptCSetScript",              "([BII)V",                              (void*)nScriptCSetScript },
1592{"nScriptCCreate",                 "()I",                                  (void*)nScriptCCreate },
1593
1594{"nProgramStoreBegin",             "(II)V",                                (void*)nProgramStoreBegin },
1595{"nProgramStoreDepthFunc",         "(I)V",                                 (void*)nProgramStoreDepthFunc },
1596{"nProgramStoreDepthMask",         "(Z)V",                                 (void*)nProgramStoreDepthMask },
1597{"nProgramStoreColorMask",         "(ZZZZ)V",                              (void*)nProgramStoreColorMask },
1598{"nProgramStoreBlendFunc",         "(II)V",                                (void*)nProgramStoreBlendFunc },
1599{"nProgramStoreDither",            "(Z)V",                                 (void*)nProgramStoreDither },
1600{"nProgramStoreCreate",            "()I",                                  (void*)nProgramStoreCreate },
1601
1602{"nProgramBindConstants",          "(III)V",                               (void*)nProgramBindConstants },
1603{"nProgramBindTexture",            "(III)V",                               (void*)nProgramBindTexture },
1604{"nProgramBindSampler",            "(III)V",                               (void*)nProgramBindSampler },
1605
1606{"nProgramFragmentCreate",         "([I)I",                                (void*)nProgramFragmentCreate },
1607{"nProgramFragmentCreate2",        "(Ljava/lang/String;[I)I",              (void*)nProgramFragmentCreate2 },
1608
1609{"nProgramRasterCreate",           "(ZZZ)I",                             (void*)nProgramRasterCreate },
1610{"nProgramRasterSetLineWidth",     "(IF)V",                                (void*)nProgramRasterSetLineWidth },
1611{"nProgramRasterSetCullMode",      "(II)V",                                (void*)nProgramRasterSetCullMode },
1612
1613{"nProgramVertexCreate",           "(Z)I",                                 (void*)nProgramVertexCreate },
1614{"nProgramVertexCreate2",          "(Ljava/lang/String;[I)I",              (void*)nProgramVertexCreate2 },
1615
1616{"nLightBegin",                    "()V",                                  (void*)nLightBegin },
1617{"nLightSetIsMono",                "(Z)V",                                 (void*)nLightSetIsMono },
1618{"nLightSetIsLocal",               "(Z)V",                                 (void*)nLightSetIsLocal },
1619{"nLightCreate",                   "()I",                                  (void*)nLightCreate },
1620{"nLightSetColor",                 "(IFFF)V",                              (void*)nLightSetColor },
1621{"nLightSetPosition",              "(IFFF)V",                              (void*)nLightSetPosition },
1622
1623{"nContextBindRootScript",         "(I)V",                                 (void*)nContextBindRootScript },
1624{"nContextBindProgramStore",       "(I)V",                                (void*)nContextBindProgramStore },
1625{"nContextBindProgramFragment",    "(I)V",                                 (void*)nContextBindProgramFragment },
1626{"nContextBindProgramVertex",      "(I)V",                                 (void*)nContextBindProgramVertex },
1627{"nContextBindProgramRaster",      "(I)V",                                 (void*)nContextBindProgramRaster },
1628
1629{"nSamplerBegin",                  "()V",                                  (void*)nSamplerBegin },
1630{"nSamplerSet",                    "(II)V",                                (void*)nSamplerSet },
1631{"nSamplerCreate",                 "()I",                                  (void*)nSamplerCreate },
1632
1633{"nMeshCreate",                    "(II)I",                                (void*)nMeshCreate },
1634{"nMeshBindVertex",                "(III)V",                               (void*)nMeshBindVertex },
1635{"nMeshBindIndex",                 "(IIII)V",                              (void*)nMeshBindIndex },
1636
1637{"nMeshGetVertexBufferCount",     "(I)I",                                 (void*)nMeshGetVertexBufferCount },
1638{"nMeshGetIndexCount",             "(I)I",                                 (void*)nMeshGetIndexCount },
1639{"nMeshGetVertices",               "(I[II)V",                             (void*)nMeshGetVertices },
1640{"nMeshGetIndices",                "(I[I[II)V",                            (void*)nMeshGetIndices },
1641
1642};
1643
1644static int registerFuncs(JNIEnv *_env)
1645{
1646    return android::AndroidRuntime::registerNativeMethods(
1647            _env, classPathName, methods, NELEM(methods));
1648}
1649
1650// ---------------------------------------------------------------------------
1651
1652jint JNI_OnLoad(JavaVM* vm, void* reserved)
1653{
1654    JNIEnv* env = NULL;
1655    jint result = -1;
1656
1657    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
1658        LOGE("ERROR: GetEnv failed\n");
1659        goto bail;
1660    }
1661    assert(env != NULL);
1662
1663    if (registerFuncs(env) < 0) {
1664        LOGE("ERROR: MediaPlayer native registration failed\n");
1665        goto bail;
1666    }
1667
1668    /* success -- return valid version number */
1669    result = JNI_VERSION_1_4;
1670
1671bail:
1672    return result;
1673}
1674
1675