android_renderscript_RenderScript.cpp revision 70d4e5024298f71edb3b04867e05568f5495b4ce
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#include "android_runtime/android_view_Surface.h"
41
42#include <RenderScript.h>
43#include <RenderScriptEnv.h>
44
45//#define LOG_API LOGE
46#define LOG_API(...)
47
48using namespace android;
49
50// ---------------------------------------------------------------------------
51
52static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
53{
54    jclass npeClazz = env->FindClass(exc);
55    env->ThrowNew(npeClazz, msg);
56}
57
58static jfieldID gContextId = 0;
59static jfieldID gNativeBitmapID = 0;
60static jfieldID gTypeNativeCache = 0;
61
62static RsElement g_A_8 = NULL;
63static RsElement g_RGBA_4444 = NULL;
64static RsElement g_RGBA_8888 = NULL;
65static RsElement g_RGB_565 = NULL;
66
67static void _nInit(JNIEnv *_env, jclass _this)
68{
69    gContextId             = _env->GetFieldID(_this, "mContext", "I");
70
71    jclass bitmapClass = _env->FindClass("android/graphics/Bitmap");
72    gNativeBitmapID = _env->GetFieldID(bitmapClass, "mNativeBitmap", "I");
73}
74
75static void nInitElements(JNIEnv *_env, jobject _this, jint a8, jint rgba4444, jint rgba8888, jint rgb565)
76{
77    g_A_8 = reinterpret_cast<RsElement>(a8);
78    g_RGBA_4444 = reinterpret_cast<RsElement>(rgba4444);
79    g_RGBA_8888 = reinterpret_cast<RsElement>(rgba8888);
80    g_RGB_565 = reinterpret_cast<RsElement>(rgb565);
81}
82
83// ---------------------------------------------------------------------------
84
85static void
86nContextFinish(JNIEnv *_env, jobject _this, RsContext con)
87{
88    LOG_API("nContextFinish, con(%p)", con);
89    rsContextFinish(con);
90}
91
92static void
93nAssignName(JNIEnv *_env, jobject _this, RsContext con, jint obj, jbyteArray str)
94{
95    LOG_API("nAssignName, con(%p), obj(%p)", con, (void *)obj);
96    jint len = _env->GetArrayLength(str);
97    jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
98    rsAssignName(con, (void *)obj, (const char *)cptr, len);
99    _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
100}
101
102static jstring
103nGetName(JNIEnv *_env, jobject _this, RsContext con, jint obj)
104{
105    LOG_API("nGetName, con(%p), obj(%p)", con, (void *)obj);
106    const char *name = NULL;
107    rsGetName(con, (void *)obj, &name);
108    return _env->NewStringUTF(name);
109}
110
111static void
112nObjDestroy(JNIEnv *_env, jobject _this, RsContext con, jint obj)
113{
114    LOG_API("nObjDestroy, con(%p) obj(%p)", con, (void *)obj);
115    rsObjDestroy(con, (void *)obj);
116}
117
118
119static jint
120nFileOpen(JNIEnv *_env, jobject _this, RsContext con, jbyteArray str)
121{
122    LOG_API("nFileOpen, con(%p)", con);
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, RsContext con, jint p)
169{
170    LOG_API("ContextSetPriority, con(%p), priority(%i)", con, p);
171    rsContextSetPriority(con, p);
172}
173
174
175
176static void
177nContextSetSurface(JNIEnv *_env, jobject _this, RsContext con, jint width, jint height, jobject wnd)
178{
179    LOG_API("nContextSetSurface, con(%p), width(%i), height(%i), surface(%p)", con, width, height, (Surface *)wnd);
180
181    Surface * window = NULL;
182    if (wnd == NULL) {
183
184    } else {
185        window = (Surface*) android_Surface_getNativeWindow(_env, wnd).get();
186    }
187
188    rsContextSetSurface(con, width, height, window);
189}
190
191static void
192nContextDestroy(JNIEnv *_env, jobject _this, RsContext con)
193{
194    LOG_API("nContextDestroy, con(%p)", con);
195    rsContextDestroy(con);
196}
197
198static void
199nContextDump(JNIEnv *_env, jobject _this, RsContext con, jint bits)
200{
201    LOG_API("nContextDump, con(%p)  bits(%i)", (RsContext)con, bits);
202    rsContextDump((RsContext)con, bits);
203}
204
205static void
206nContextPause(JNIEnv *_env, jobject _this, RsContext con)
207{
208    LOG_API("nContextPause, con(%p)", con);
209    rsContextPause(con);
210}
211
212static void
213nContextResume(JNIEnv *_env, jobject _this, RsContext con)
214{
215    LOG_API("nContextResume, con(%p)", con);
216    rsContextResume(con);
217}
218
219static jint
220nContextGetMessage(JNIEnv *_env, jobject _this, RsContext con, jintArray data, jboolean wait)
221{
222    jint len = _env->GetArrayLength(data);
223    LOG_API("nContextGetMessage, con(%p), len(%i)", con, len);
224    jint *ptr = _env->GetIntArrayElements(data, NULL);
225    size_t receiveLen;
226    int id = rsContextGetMessage(con, ptr, &receiveLen, len * 4, wait);
227    if (!id && receiveLen) {
228        LOGV("message receive buffer too small.  %i", receiveLen);
229        *ptr = (jint)receiveLen;
230    }
231    _env->ReleaseIntArrayElements(data, ptr, 0);
232    return id;
233}
234
235static void nContextInitToClient(JNIEnv *_env, jobject _this, RsContext con)
236{
237    LOG_API("nContextInitToClient, con(%p)", con);
238    rsContextInitToClient(con);
239}
240
241static void nContextDeinitToClient(JNIEnv *_env, jobject _this, RsContext con)
242{
243    LOG_API("nContextDeinitToClient, con(%p)", con);
244    rsContextDeinitToClient(con);
245}
246
247
248static jint
249nElementCreate(JNIEnv *_env, jobject _this, RsContext con, jint type, jint kind, jboolean norm, jint size)
250{
251    LOG_API("nElementCreate, con(%p), type(%i), kind(%i), norm(%i), size(%i)", con, type, kind, norm, size);
252    return (jint)rsElementCreate(con, (RsDataType)type, (RsDataKind)kind, norm, size);
253}
254
255static jint
256nElementCreate2(JNIEnv *_env, jobject _this, RsContext con, jintArray _ids, jobjectArray _names, jintArray _arraySizes)
257{
258    int fieldCount = _env->GetArrayLength(_ids);
259    LOG_API("nElementCreate2, con(%p)", con);
260
261    jint *ids = _env->GetIntArrayElements(_ids, NULL);
262    jint *arraySizes = _env->GetIntArrayElements(_arraySizes, NULL);
263    const char ** nameArray = (const char **)calloc(fieldCount, sizeof(char *));
264    size_t* sizeArray = (size_t*)calloc(fieldCount, sizeof(size_t));
265
266    for (int ct=0; ct < fieldCount; ct++) {
267        jstring s = (jstring)_env->GetObjectArrayElement(_names, ct);
268        nameArray[ct] = _env->GetStringUTFChars(s, NULL);
269        sizeArray[ct] = _env->GetStringUTFLength(s);
270    }
271    jint id = (jint)rsElementCreate2(con, fieldCount, (RsElement *)ids, nameArray, sizeArray, (const uint32_t *)arraySizes);
272    for (int ct=0; ct < fieldCount; ct++) {
273        jstring s = (jstring)_env->GetObjectArrayElement(_names, ct);
274        _env->ReleaseStringUTFChars(s, nameArray[ct]);
275    }
276    _env->ReleaseIntArrayElements(_ids, ids, JNI_ABORT);
277    _env->ReleaseIntArrayElements(_arraySizes, arraySizes, JNI_ABORT);
278    free(nameArray);
279    free(sizeArray);
280    return (jint)id;
281}
282
283static void
284nElementGetNativeData(JNIEnv *_env, jobject _this, RsContext con, jint id, jintArray _elementData)
285{
286    int dataSize = _env->GetArrayLength(_elementData);
287    LOG_API("nElementGetNativeData, con(%p)", con);
288
289    // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
290    assert(dataSize == 5);
291
292    uint32_t elementData[5];
293    rsElementGetNativeData(con, (RsElement)id, elementData, dataSize);
294
295    for(jint i = 0; i < dataSize; i ++) {
296        _env->SetIntArrayRegion(_elementData, i, 1, (const jint*)&elementData[i]);
297    }
298}
299
300
301static void
302nElementGetSubElements(JNIEnv *_env, jobject _this, RsContext con, jint id, jintArray _IDs, jobjectArray _names)
303{
304    int dataSize = _env->GetArrayLength(_IDs);
305    LOG_API("nElementGetSubElements, con(%p)", con);
306
307    uint32_t *ids = (uint32_t *)malloc((uint32_t)dataSize * sizeof(uint32_t));
308    const char **names = (const char **)malloc((uint32_t)dataSize * sizeof(const char *));
309
310    rsElementGetSubElements(con, (RsElement)id, ids, names, (uint32_t)dataSize);
311
312    for(jint i = 0; i < dataSize; i ++) {
313        _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
314        _env->SetIntArrayRegion(_IDs, i, 1, (const jint*)&ids[i]);
315    }
316
317    free(ids);
318    free(names);
319}
320
321// -----------------------------------
322
323static void
324nTypeBegin(JNIEnv *_env, jobject _this, RsContext con, jint eID)
325{
326    LOG_API("nTypeBegin, con(%p) e(%p)", con, (RsElement)eID);
327    rsTypeBegin(con, (RsElement)eID);
328}
329
330static void
331nTypeAdd(JNIEnv *_env, jobject _this, RsContext con, jint dim, jint val)
332{
333    LOG_API("nTypeAdd, con(%p) dim(%i), val(%i)", con, dim, val);
334    rsTypeAdd(con, (RsDimension)dim, val);
335}
336
337static jint
338nTypeCreate(JNIEnv *_env, jobject _this, RsContext con)
339{
340    LOG_API("nTypeCreate, con(%p)", con);
341    return (jint)rsTypeCreate(con);
342}
343
344static void
345nTypeGetNativeData(JNIEnv *_env, jobject _this, RsContext con, jint id, jintArray _typeData)
346{
347    // We are packing 6 items: mDimX; mDimY; mDimZ;
348    // mDimLOD; mDimFaces; mElement; into typeData
349    int elementCount = _env->GetArrayLength(_typeData);
350
351    assert(elementCount == 6);
352    LOG_API("nTypeCreate, con(%p)", con);
353
354    uint32_t typeData[6];
355    rsTypeGetNativeData(con, (RsType)id, typeData, 6);
356
357    for(jint i = 0; i < elementCount; i ++) {
358        _env->SetIntArrayRegion(_typeData, i, 1, (const jint*)&typeData[i]);
359    }
360}
361
362// -----------------------------------
363
364static jint
365nAllocationCreateTyped(JNIEnv *_env, jobject _this, RsContext con, jint e)
366{
367    LOG_API("nAllocationCreateTyped, con(%p), e(%p)", con, (RsElement)e);
368    return (jint) rsAllocationCreateTyped(con, (RsElement)e);
369}
370
371static void
372nAllocationUploadToTexture(JNIEnv *_env, jobject _this, RsContext con, jint a, jboolean genMip, jint mip)
373{
374    LOG_API("nAllocationUploadToTexture, con(%p), a(%p), genMip(%i), mip(%i)", con, (RsAllocation)a, genMip, mip);
375    rsAllocationUploadToTexture(con, (RsAllocation)a, genMip, mip);
376}
377
378static void
379nAllocationUploadToBufferObject(JNIEnv *_env, jobject _this, RsContext con, jint a)
380{
381    LOG_API("nAllocationUploadToBufferObject, con(%p), a(%p)", con, (RsAllocation)a);
382    rsAllocationUploadToBufferObject(con, (RsAllocation)a);
383}
384
385static RsElement SkBitmapToPredefined(SkBitmap::Config cfg)
386{
387    switch (cfg) {
388    case SkBitmap::kA8_Config:
389        return g_A_8;
390    case SkBitmap::kARGB_4444_Config:
391        return g_RGBA_4444;
392    case SkBitmap::kARGB_8888_Config:
393        return g_RGBA_8888;
394    case SkBitmap::kRGB_565_Config:
395        return g_RGB_565;
396
397    default:
398        break;
399    }
400    // If we don't have a conversion mark it as a user type.
401    LOGE("Unsupported bitmap type");
402    return NULL;
403}
404
405static int
406nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, RsContext con, jint dstFmt, jboolean genMips, jobject jbitmap)
407{
408    SkBitmap const * nativeBitmap =
409            (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID);
410    const SkBitmap& bitmap(*nativeBitmap);
411    SkBitmap::Config config = bitmap.getConfig();
412
413    RsElement e = SkBitmapToPredefined(config);
414    if (e) {
415        bitmap.lockPixels();
416        const int w = bitmap.width();
417        const int h = bitmap.height();
418        const void* ptr = bitmap.getPixels();
419        jint id = (jint)rsAllocationCreateFromBitmap(con, w, h, (RsElement)dstFmt, e, genMips, ptr);
420        bitmap.unlockPixels();
421        return id;
422    }
423    return 0;
424}
425
426static void ReleaseBitmapCallback(void *bmp)
427{
428    SkBitmap const * nativeBitmap = (SkBitmap const *)bmp;
429    nativeBitmap->unlockPixels();
430}
431
432static int
433nAllocationCreateBitmapRef(JNIEnv *_env, jobject _this, RsContext con, jint type, jobject jbitmap)
434{
435    SkBitmap * nativeBitmap =
436            (SkBitmap *)_env->GetIntField(jbitmap, gNativeBitmapID);
437
438
439    nativeBitmap->lockPixels();
440    void* ptr = nativeBitmap->getPixels();
441    jint id = (jint)rsAllocationCreateBitmapRef(con, (RsType)type, ptr, nativeBitmap, ReleaseBitmapCallback);
442    return id;
443}
444
445static int
446nAllocationCreateFromAssetStream(JNIEnv *_env, jobject _this, RsContext con, jint dstFmt, jboolean genMips, jint native_asset)
447{
448    Asset* asset = reinterpret_cast<Asset*>(native_asset);
449    SkBitmap bitmap;
450    SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
451            &bitmap, SkBitmap::kNo_Config, SkImageDecoder::kDecodePixels_Mode);
452
453    SkBitmap::Config config = bitmap.getConfig();
454
455    RsElement e = SkBitmapToPredefined(config);
456
457    if (e) {
458        bitmap.lockPixels();
459        const int w = bitmap.width();
460        const int h = bitmap.height();
461        const void* ptr = bitmap.getPixels();
462        jint id = (jint)rsAllocationCreateFromBitmap(con, w, h, (RsElement)dstFmt, e, genMips, ptr);
463        bitmap.unlockPixels();
464        return id;
465    }
466    return 0;
467}
468
469static int
470nAllocationCreateFromBitmapBoxed(JNIEnv *_env, jobject _this, RsContext con, jint dstFmt, jboolean genMips, jobject jbitmap)
471{
472    SkBitmap const * nativeBitmap =
473            (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID);
474    const SkBitmap& bitmap(*nativeBitmap);
475    SkBitmap::Config config = bitmap.getConfig();
476
477    RsElement e = SkBitmapToPredefined(config);
478
479    if (e) {
480        bitmap.lockPixels();
481        const int w = bitmap.width();
482        const int h = bitmap.height();
483        const void* ptr = bitmap.getPixels();
484        jint id = (jint)rsAllocationCreateFromBitmapBoxed(con, w, h, (RsElement)dstFmt, e, genMips, ptr);
485        bitmap.unlockPixels();
486        return id;
487    }
488    return 0;
489}
490
491
492static void
493nAllocationSubData1D_i(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint count, jintArray data, int sizeBytes)
494{
495    jint len = _env->GetArrayLength(data);
496    LOG_API("nAllocation1DSubData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
497    jint *ptr = _env->GetIntArrayElements(data, NULL);
498    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
499    _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
500}
501
502static void
503nAllocationSubData1D_s(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint count, jshortArray data, int sizeBytes)
504{
505    jint len = _env->GetArrayLength(data);
506    LOG_API("nAllocation1DSubData_s, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
507    jshort *ptr = _env->GetShortArrayElements(data, NULL);
508    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
509    _env->ReleaseShortArrayElements(data, ptr, JNI_ABORT);
510}
511
512static void
513nAllocationSubData1D_b(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint count, jbyteArray data, int sizeBytes)
514{
515    jint len = _env->GetArrayLength(data);
516    LOG_API("nAllocation1DSubData_b, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
517    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
518    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
519    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
520}
521
522static void
523nAllocationSubData1D_f(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint count, jfloatArray data, int sizeBytes)
524{
525    jint len = _env->GetArrayLength(data);
526    LOG_API("nAllocation1DSubData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
527    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
528    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
529    _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
530}
531
532static void
533//    native void rsnAllocationSubElementData1D(int con, int id, int xoff, int compIdx, byte[] d, int sizeBytes);
534nAllocationSubElementData1D(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint compIdx, jbyteArray data, int sizeBytes)
535{
536    jint len = _env->GetArrayLength(data);
537    LOG_API("nAllocationSubElementData1D, con(%p), alloc(%p), offset(%i), comp(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, compIdx, len, sizeBytes);
538    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
539    rsAllocation1DSubElementData(con, (RsAllocation)alloc, offset, ptr, compIdx, sizeBytes);
540    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
541}
542
543static void
544nAllocationSubData2D_i(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint w, jint h, jintArray data, int sizeBytes)
545{
546    jint len = _env->GetArrayLength(data);
547    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);
548    jint *ptr = _env->GetIntArrayElements(data, NULL);
549    rsAllocation2DSubData(con, (RsAllocation)alloc, xoff, yoff, w, h, ptr, sizeBytes);
550    _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
551}
552
553static void
554nAllocationSubData2D_f(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint w, jint h, jfloatArray data, int sizeBytes)
555{
556    jint len = _env->GetArrayLength(data);
557    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);
558    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
559    rsAllocation2DSubData(con, (RsAllocation)alloc, xoff, yoff, w, h, ptr, sizeBytes);
560    _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
561}
562
563static void
564nAllocationRead_i(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jintArray data)
565{
566    jint len = _env->GetArrayLength(data);
567    LOG_API("nAllocationRead_i, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
568    jint *ptr = _env->GetIntArrayElements(data, NULL);
569    rsAllocationRead(con, (RsAllocation)alloc, ptr);
570    _env->ReleaseIntArrayElements(data, ptr, 0);
571}
572
573static void
574nAllocationRead_f(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jfloatArray data)
575{
576    jint len = _env->GetArrayLength(data);
577    LOG_API("nAllocationRead_f, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
578    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
579    rsAllocationRead(con, (RsAllocation)alloc, ptr);
580    _env->ReleaseFloatArrayElements(data, ptr, 0);
581}
582
583static jint
584nAllocationGetType(JNIEnv *_env, jobject _this, RsContext con, jint a)
585{
586    LOG_API("nAllocationGetType, con(%p), a(%p)", con, (RsAllocation)a);
587    return (jint) rsAllocationGetType(con, (RsAllocation)a);
588}
589
590// -----------------------------------
591
592static int
593nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, RsContext con, jint native_asset)
594{
595    LOGV("______nFileA3D %u", (uint32_t) native_asset);
596
597    Asset* asset = reinterpret_cast<Asset*>(native_asset);
598
599    jint id = (jint)rsFileA3DCreateFromAssetStream(con, asset->getBuffer(false), asset->getLength());
600    return id;
601}
602
603static int
604nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, RsContext con, jint fileA3D)
605{
606    int32_t numEntries = 0;
607    rsFileA3DGetNumIndexEntries(con, &numEntries, (RsFile)fileA3D);
608    return numEntries;
609}
610
611static void
612nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, RsContext con, jint fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
613{
614    LOGV("______nFileA3D %u", (uint32_t) fileA3D);
615    RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
616
617    rsFileA3DGetIndexEntries(con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
618
619    for(jint i = 0; i < numEntries; i ++) {
620        _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
621        _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
622    }
623
624    free(fileEntries);
625}
626
627static int
628nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, RsContext con, jint fileA3D, jint index)
629{
630    LOGV("______nFileA3D %u", (uint32_t) fileA3D);
631    jint id = (jint)rsFileA3DGetEntryByIndex(con, (uint32_t)index, (RsFile)fileA3D);
632    return id;
633}
634
635// -----------------------------------
636
637static int
638nFontCreateFromFile(JNIEnv *_env, jobject _this, RsContext con, jstring fileName, jint fontSize, jint dpi)
639{
640    const char* fileNameUTF = _env->GetStringUTFChars(fileName, NULL);
641
642    jint id = (jint)rsFontCreateFromFile(con, fileNameUTF, fontSize, dpi);
643    return id;
644}
645
646
647// -----------------------------------
648
649static void
650nAdapter1DBindAllocation(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint alloc)
651{
652    LOG_API("nAdapter1DBindAllocation, con(%p), adapter(%p), alloc(%p)", con, (RsAdapter1D)adapter, (RsAllocation)alloc);
653    rsAdapter1DBindAllocation(con, (RsAdapter1D)adapter, (RsAllocation)alloc);
654}
655
656static void
657nAdapter1DSetConstraint(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint dim, jint value)
658{
659    LOG_API("nAdapter1DSetConstraint, con(%p), adapter(%p), dim(%i), value(%i)", con, (RsAdapter1D)adapter, dim, value);
660    rsAdapter1DSetConstraint(con, (RsAdapter1D)adapter, (RsDimension)dim, value);
661}
662
663static void
664nAdapter1DData_i(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jintArray data)
665{
666    jint len = _env->GetArrayLength(data);
667    LOG_API("nAdapter1DData_i, con(%p), adapter(%p), len(%i)", con, (RsAdapter1D)adapter, len);
668    jint *ptr = _env->GetIntArrayElements(data, NULL);
669    rsAdapter1DData(con, (RsAdapter1D)adapter, ptr);
670    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
671}
672
673static void
674nAdapter1DSubData_i(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint offset, jint count, jintArray data)
675{
676    jint len = _env->GetArrayLength(data);
677    LOG_API("nAdapter1DSubData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAdapter1D)adapter, offset, count, len);
678    jint *ptr = _env->GetIntArrayElements(data, NULL);
679    rsAdapter1DSubData(con, (RsAdapter1D)adapter, offset, count, ptr);
680    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
681}
682
683static void
684nAdapter1DData_f(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jfloatArray data)
685{
686    jint len = _env->GetArrayLength(data);
687    LOG_API("nAdapter1DData_f, con(%p), adapter(%p), len(%i)", con, (RsAdapter1D)adapter, len);
688    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
689    rsAdapter1DData(con, (RsAdapter1D)adapter, ptr);
690    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
691}
692
693static void
694nAdapter1DSubData_f(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint offset, jint count, jfloatArray data)
695{
696    jint len = _env->GetArrayLength(data);
697    LOG_API("nAdapter1DSubData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAdapter1D)adapter, offset, count, len);
698    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
699    rsAdapter1DSubData(con, (RsAdapter1D)adapter, offset, count, ptr);
700    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
701}
702
703static jint
704nAdapter1DCreate(JNIEnv *_env, jobject _this, RsContext con)
705{
706    LOG_API("nAdapter1DCreate, con(%p)", con);
707    return (jint)rsAdapter1DCreate(con);
708}
709
710// -----------------------------------
711
712static void
713nAdapter2DBindAllocation(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint alloc)
714{
715    LOG_API("nAdapter2DBindAllocation, con(%p), adapter(%p), alloc(%p)", con, (RsAdapter2D)adapter, (RsAllocation)alloc);
716    rsAdapter2DBindAllocation(con, (RsAdapter2D)adapter, (RsAllocation)alloc);
717}
718
719static void
720nAdapter2DSetConstraint(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint dim, jint value)
721{
722    LOG_API("nAdapter2DSetConstraint, con(%p), adapter(%p), dim(%i), value(%i)", con, (RsAdapter2D)adapter, dim, value);
723    rsAdapter2DSetConstraint(con, (RsAdapter2D)adapter, (RsDimension)dim, value);
724}
725
726static void
727nAdapter2DData_i(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jintArray data)
728{
729    jint len = _env->GetArrayLength(data);
730    LOG_API("nAdapter2DData_i, con(%p), adapter(%p), len(%i)", con, (RsAdapter2D)adapter, len);
731    jint *ptr = _env->GetIntArrayElements(data, NULL);
732    rsAdapter2DData(con, (RsAdapter2D)adapter, ptr);
733    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
734}
735
736static void
737nAdapter2DData_f(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jfloatArray data)
738{
739    jint len = _env->GetArrayLength(data);
740    LOG_API("nAdapter2DData_f, con(%p), adapter(%p), len(%i)", con, (RsAdapter2D)adapter, len);
741    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
742    rsAdapter2DData(con, (RsAdapter2D)adapter, ptr);
743    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
744}
745
746static void
747nAdapter2DSubData_i(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint xoff, jint yoff, jint w, jint h, jintArray data)
748{
749    jint len = _env->GetArrayLength(data);
750    LOG_API("nAdapter2DSubData_i, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)",
751            con, (RsAdapter2D)adapter, xoff, yoff, w, h, len);
752    jint *ptr = _env->GetIntArrayElements(data, NULL);
753    rsAdapter2DSubData(con, (RsAdapter2D)adapter, xoff, yoff, w, h, ptr);
754    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
755}
756
757static void
758nAdapter2DSubData_f(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint xoff, jint yoff, jint w, jint h, jfloatArray data)
759{
760    jint len = _env->GetArrayLength(data);
761    LOG_API("nAdapter2DSubData_f, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)",
762            con, (RsAdapter2D)adapter, xoff, yoff, w, h, len);
763    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
764    rsAdapter2DSubData(con, (RsAdapter1D)adapter, xoff, yoff, w, h, ptr);
765    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
766}
767
768static jint
769nAdapter2DCreate(JNIEnv *_env, jobject _this, RsContext con)
770{
771    LOG_API("nAdapter2DCreate, con(%p)", con);
772    return (jint)rsAdapter2DCreate(con);
773}
774
775// -----------------------------------
776
777static void
778nScriptBindAllocation(JNIEnv *_env, jobject _this, RsContext con, jint script, jint alloc, jint slot)
779{
780    LOG_API("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", con, (RsScript)script, (RsAllocation)alloc, slot);
781    rsScriptBindAllocation(con, (RsScript)script, (RsAllocation)alloc, slot);
782}
783
784static void
785nScriptSetVarI(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jint val)
786{
787    LOG_API("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i), b(%f), a(%f)", con, (void *)script, slot, val);
788    rsScriptSetVarI(con, (RsScript)script, slot, val);
789}
790
791static void
792nScriptSetVarF(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, float val)
793{
794    LOG_API("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i), b(%f), a(%f)", con, (void *)script, slot, val);
795    rsScriptSetVarF(con, (RsScript)script, slot, val);
796}
797
798static void
799nScriptSetVarV(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jbyteArray data)
800{
801    LOG_API("nScriptSetVarV, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
802    jint len = _env->GetArrayLength(data);
803    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
804    rsScriptSetVarV(con, (RsScript)script, slot, ptr, len);
805    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
806}
807
808
809static void
810nScriptSetTimeZone(JNIEnv *_env, jobject _this, RsContext con, jint script, jbyteArray timeZone)
811{
812    LOG_API("nScriptCSetTimeZone, con(%p), s(%p), timeZone(%s)", con, (void *)script, (const char *)timeZone);
813
814    jint length = _env->GetArrayLength(timeZone);
815    jbyte* timeZone_ptr;
816    timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
817
818    rsScriptSetTimeZone(con, (RsScript)script, (const char *)timeZone_ptr, length);
819
820    if (timeZone_ptr) {
821        _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
822    }
823}
824
825static void
826nScriptInvoke(JNIEnv *_env, jobject _this, RsContext con, jint obj, jint slot)
827{
828    LOG_API("nScriptInvoke, con(%p), script(%p)", con, (void *)obj);
829    rsScriptInvoke(con, (RsScript)obj, slot);
830}
831
832static void
833nScriptInvokeV(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jbyteArray data)
834{
835    LOG_API("nScriptInvokeV, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
836    jint len = _env->GetArrayLength(data);
837    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
838    rsScriptInvokeV(con, (RsScript)script, slot, ptr, len);
839    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
840}
841
842
843// -----------------------------------
844
845static void
846nScriptCBegin(JNIEnv *_env, jobject _this, RsContext con)
847{
848    LOG_API("nScriptCBegin, con(%p)", con);
849    rsScriptCBegin(con);
850}
851
852static void
853nScriptCSetScript(JNIEnv *_env, jobject _this, RsContext con, jbyteArray scriptRef,
854                  jint offset, jint length)
855{
856    LOG_API("!!! nScriptCSetScript, con(%p)", con);
857    jint _exception = 0;
858    jint remaining;
859    jbyte* script_base = 0;
860    jbyte* script_ptr;
861    if (!scriptRef) {
862        _exception = 1;
863        //_env->ThrowNew(IAEClass, "script == null");
864        goto exit;
865    }
866    if (offset < 0) {
867        _exception = 1;
868        //_env->ThrowNew(IAEClass, "offset < 0");
869        goto exit;
870    }
871    if (length < 0) {
872        _exception = 1;
873        //_env->ThrowNew(IAEClass, "length < 0");
874        goto exit;
875    }
876    remaining = _env->GetArrayLength(scriptRef) - offset;
877    if (remaining < length) {
878        _exception = 1;
879        //_env->ThrowNew(IAEClass, "length > script.length - offset");
880        goto exit;
881    }
882    script_base = (jbyte *)
883        _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
884    script_ptr = script_base + offset;
885
886    rsScriptCSetText(con, (const char *)script_ptr, length);
887
888exit:
889    if (script_base) {
890        _env->ReleasePrimitiveArrayCritical(scriptRef, script_base,
891                _exception ? JNI_ABORT: 0);
892    }
893}
894
895static jint
896nScriptCCreate(JNIEnv *_env, jobject _this, RsContext con)
897{
898    LOG_API("nScriptCCreate, con(%p)", con);
899    return (jint)rsScriptCCreate(con);
900}
901
902// ---------------------------------------------------------------------------
903
904static void
905nProgramStoreBegin(JNIEnv *_env, jobject _this, RsContext con, jint in, jint out)
906{
907    LOG_API("nProgramStoreBegin, con(%p), in(%p), out(%p)", con, (RsElement)in, (RsElement)out);
908    rsProgramStoreBegin(con, (RsElement)in, (RsElement)out);
909}
910
911static void
912nProgramStoreDepthFunc(JNIEnv *_env, jobject _this, RsContext con, jint func)
913{
914    LOG_API("nProgramStoreDepthFunc, con(%p), func(%i)", con, func);
915    rsProgramStoreDepthFunc(con, (RsDepthFunc)func);
916}
917
918static void
919nProgramStoreDepthMask(JNIEnv *_env, jobject _this, RsContext con, jboolean enable)
920{
921    LOG_API("nProgramStoreDepthMask, con(%p), enable(%i)", con, enable);
922    rsProgramStoreDepthMask(con, enable);
923}
924
925static void
926nProgramStoreColorMask(JNIEnv *_env, jobject _this, RsContext con, jboolean r, jboolean g, jboolean b, jboolean a)
927{
928    LOG_API("nProgramStoreColorMask, con(%p), r(%i), g(%i), b(%i), a(%i)", con, r, g, b, a);
929    rsProgramStoreColorMask(con, r, g, b, a);
930}
931
932static void
933nProgramStoreBlendFunc(JNIEnv *_env, jobject _this, RsContext con, int src, int dst)
934{
935    LOG_API("nProgramStoreBlendFunc, con(%p), src(%i), dst(%i)", con, src, dst);
936    rsProgramStoreBlendFunc(con, (RsBlendSrcFunc)src, (RsBlendDstFunc)dst);
937}
938
939static void
940nProgramStoreDither(JNIEnv *_env, jobject _this, RsContext con, jboolean enable)
941{
942    LOG_API("nProgramStoreDither, con(%p), enable(%i)", con, enable);
943    rsProgramStoreDither(con, enable);
944}
945
946static jint
947nProgramStoreCreate(JNIEnv *_env, jobject _this, RsContext con)
948{
949    LOG_API("nProgramStoreCreate, con(%p)", con);
950    return (jint)rsProgramStoreCreate(con);
951}
952
953// ---------------------------------------------------------------------------
954
955static void
956nProgramBindConstants(JNIEnv *_env, jobject _this, RsContext con, jint vpv, jint slot, jint a)
957{
958    LOG_API("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", con, (RsProgramVertex)vpv, slot, (RsAllocation)a);
959    rsProgramBindConstants(con, (RsProgram)vpv, slot, (RsAllocation)a);
960}
961
962static void
963nProgramBindTexture(JNIEnv *_env, jobject _this, RsContext con, jint vpf, jint slot, jint a)
964{
965    LOG_API("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
966    rsProgramBindTexture(con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
967}
968
969static void
970nProgramBindSampler(JNIEnv *_env, jobject _this, RsContext con, jint vpf, jint slot, jint a)
971{
972    LOG_API("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsSampler)a);
973    rsProgramBindSampler(con, (RsProgramFragment)vpf, slot, (RsSampler)a);
974}
975
976// ---------------------------------------------------------------------------
977
978static jint
979nProgramFragmentCreate(JNIEnv *_env, jobject _this, RsContext con, jintArray params)
980{
981    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
982    jint paramLen = _env->GetArrayLength(params);
983
984    LOG_API("nProgramFragmentCreate, con(%p), paramLen(%i)", con, paramLen);
985
986    jint ret = (jint)rsProgramFragmentCreate(con, (uint32_t *)paramPtr, paramLen);
987    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
988    return ret;
989}
990
991static jint
992nProgramFragmentCreate2(JNIEnv *_env, jobject _this, RsContext con, jstring shader, jintArray params)
993{
994    const char* shaderUTF = _env->GetStringUTFChars(shader, NULL);
995    jint shaderLen = _env->GetStringUTFLength(shader);
996    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
997    jint paramLen = _env->GetArrayLength(params);
998
999    LOG_API("nProgramFragmentCreate2, con(%p), shaderLen(%i), paramLen(%i)", con, shaderLen, paramLen);
1000
1001    jint ret = (jint)rsProgramFragmentCreate2(con, shaderUTF, shaderLen, (uint32_t *)paramPtr, paramLen);
1002    _env->ReleaseStringUTFChars(shader, shaderUTF);
1003    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1004    return ret;
1005}
1006
1007
1008// ---------------------------------------------------------------------------
1009
1010static jint
1011nProgramVertexCreate(JNIEnv *_env, jobject _this, RsContext con, jboolean texMat)
1012{
1013    LOG_API("nProgramVertexCreate, con(%p), texMat(%i)", con, texMat);
1014    return (jint)rsProgramVertexCreate(con, texMat);
1015}
1016
1017static jint
1018nProgramVertexCreate2(JNIEnv *_env, jobject _this, RsContext con, jstring shader, jintArray params)
1019{
1020    const char* shaderUTF = _env->GetStringUTFChars(shader, NULL);
1021    jint shaderLen = _env->GetStringUTFLength(shader);
1022    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1023    jint paramLen = _env->GetArrayLength(params);
1024
1025    LOG_API("nProgramVertexCreate2, con(%p), shaderLen(%i), paramLen(%i)", con, shaderLen, paramLen);
1026
1027    jint ret = (jint)rsProgramVertexCreate2(con, shaderUTF, shaderLen, (uint32_t *)paramPtr, paramLen);
1028    _env->ReleaseStringUTFChars(shader, shaderUTF);
1029    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1030    return ret;
1031}
1032
1033// ---------------------------------------------------------------------------
1034
1035static jint
1036nProgramRasterCreate(JNIEnv *_env, jobject _this, RsContext con, jboolean pointSmooth, jboolean lineSmooth, jboolean pointSprite)
1037{
1038    LOG_API("nProgramRasterCreate, con(%p), pointSmooth(%i), lineSmooth(%i), pointSprite(%i)",
1039            con, pointSmooth, lineSmooth, pointSprite);
1040    return (jint)rsProgramRasterCreate(con, pointSmooth, lineSmooth, pointSprite);
1041}
1042
1043static void
1044nProgramRasterSetLineWidth(JNIEnv *_env, jobject _this, RsContext con, jint vpr, jfloat v)
1045{
1046    LOG_API("nProgramRasterSetLineWidth, con(%p), vpf(%p), value(%f)", con, (RsProgramRaster)vpr, v);
1047    rsProgramRasterSetLineWidth(con, (RsProgramRaster)vpr, v);
1048}
1049
1050static void
1051nProgramRasterSetCullMode(JNIEnv *_env, jobject _this, RsContext con, jint vpr, jint v)
1052{
1053    LOG_API("nProgramRasterSetCullMode, con(%p), vpf(%p), value(%i)", con, (RsProgramRaster)vpr, v);
1054    rsProgramRasterSetCullMode(con, (RsProgramRaster)vpr, (RsCullMode)v);
1055}
1056
1057
1058// ---------------------------------------------------------------------------
1059
1060static void
1061nContextBindRootScript(JNIEnv *_env, jobject _this, RsContext con, jint script)
1062{
1063    LOG_API("nContextBindRootScript, con(%p), script(%p)", con, (RsScript)script);
1064    rsContextBindRootScript(con, (RsScript)script);
1065}
1066
1067static void
1068nContextBindProgramStore(JNIEnv *_env, jobject _this, RsContext con, jint pfs)
1069{
1070    LOG_API("nContextBindProgramStore, con(%p), pfs(%p)", con, (RsProgramStore)pfs);
1071    rsContextBindProgramStore(con, (RsProgramStore)pfs);
1072}
1073
1074static void
1075nContextBindProgramFragment(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1076{
1077    LOG_API("nContextBindProgramFragment, con(%p), pf(%p)", con, (RsProgramFragment)pf);
1078    rsContextBindProgramFragment(con, (RsProgramFragment)pf);
1079}
1080
1081static void
1082nContextBindProgramVertex(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1083{
1084    LOG_API("nContextBindProgramVertex, con(%p), pf(%p)", con, (RsProgramVertex)pf);
1085    rsContextBindProgramVertex(con, (RsProgramVertex)pf);
1086}
1087
1088static void
1089nContextBindProgramRaster(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1090{
1091    LOG_API("nContextBindProgramRaster, con(%p), pf(%p)", con, (RsProgramRaster)pf);
1092    rsContextBindProgramRaster(con, (RsProgramRaster)pf);
1093}
1094
1095
1096// ---------------------------------------------------------------------------
1097
1098static void
1099nSamplerBegin(JNIEnv *_env, jobject _this, RsContext con)
1100{
1101    LOG_API("nSamplerBegin, con(%p)", con);
1102    rsSamplerBegin(con);
1103}
1104
1105static void
1106nSamplerSet(JNIEnv *_env, jobject _this, RsContext con, jint p, jint v)
1107{
1108    LOG_API("nSamplerSet, con(%p), param(%i), value(%i)", con, p, v);
1109    rsSamplerSet(con, (RsSamplerParam)p, (RsSamplerValue)v);
1110}
1111
1112static jint
1113nSamplerCreate(JNIEnv *_env, jobject _this, RsContext con)
1114{
1115    LOG_API("nSamplerCreate, con(%p)", con);
1116    return (jint)rsSamplerCreate(con);
1117}
1118
1119// ---------------------------------------------------------------------------
1120
1121static jint
1122nMeshCreate(JNIEnv *_env, jobject _this, RsContext con, jint vtxCount, jint idxCount)
1123{
1124    LOG_API("nMeshCreate, con(%p), vtxCount(%i), idxCount(%i)", con, vtxCount, idxCount);
1125    int id = (int)rsMeshCreate(con, vtxCount, idxCount);
1126    return id;
1127}
1128
1129static void
1130nMeshBindVertex(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jint alloc, jint slot)
1131{
1132    LOG_API("nMeshBindVertex, con(%p), Mesh(%p), Alloc(%p), slot(%i)", con, (RsMesh)mesh, (RsAllocation)alloc, slot);
1133    rsMeshBindVertex(con, (RsMesh)mesh, (RsAllocation)alloc, slot);
1134}
1135
1136static void
1137nMeshBindIndex(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jint alloc, jint primID, jint slot)
1138{
1139    LOG_API("nMeshBindIndex, con(%p), Mesh(%p), Alloc(%p)", con, (RsMesh)mesh, (RsAllocation)alloc);
1140    rsMeshBindIndex(con, (RsMesh)mesh, (RsAllocation)alloc, primID, slot);
1141}
1142
1143static jint
1144nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, RsContext con, jint mesh)
1145{
1146    LOG_API("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1147    jint vtxCount = 0;
1148    rsMeshGetVertexBufferCount(con, (RsMesh)mesh, &vtxCount);
1149    return vtxCount;
1150}
1151
1152static jint
1153nMeshGetIndexCount(JNIEnv *_env, jobject _this, RsContext con, jint mesh)
1154{
1155    LOG_API("nMeshGetIndexCount, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1156    jint idxCount = 0;
1157    rsMeshGetIndexCount(con, (RsMesh)mesh, &idxCount);
1158    return idxCount;
1159}
1160
1161static void
1162nMeshGetVertices(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jintArray _ids, int numVtxIDs)
1163{
1164    LOG_API("nMeshGetVertices, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1165
1166    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
1167    rsMeshGetVertices(con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
1168
1169    for(jint i = 0; i < numVtxIDs; i ++) {
1170        _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&allocs[i]);
1171    }
1172
1173    free(allocs);
1174}
1175
1176static void
1177nMeshGetIndices(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jintArray _idxIds, jintArray _primitives, int numIndices)
1178{
1179    LOG_API("nMeshGetVertices, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1180
1181    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
1182    uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
1183
1184    rsMeshGetIndices(con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
1185
1186    for(jint i = 0; i < numIndices; i ++) {
1187        _env->SetIntArrayRegion(_idxIds, i, 1, (const jint*)&allocs[i]);
1188        _env->SetIntArrayRegion(_primitives, i, 1, (const jint*)&prims[i]);
1189    }
1190
1191    free(allocs);
1192    free(prims);
1193}
1194
1195// ---------------------------------------------------------------------------
1196
1197
1198static const char *classPathName = "android/renderscript/RenderScript";
1199
1200static JNINativeMethod methods[] = {
1201{"_nInit",                         "()V",                                  (void*)_nInit },
1202{"nInitElements",                  "(IIII)V",                              (void*)nInitElements },
1203
1204{"nDeviceCreate",                  "()I",                                  (void*)nDeviceCreate },
1205{"nDeviceDestroy",                 "(I)V",                                 (void*)nDeviceDestroy },
1206{"nDeviceSetConfig",               "(III)V",                               (void*)nDeviceSetConfig },
1207{"nContextGetMessage",             "(I[IZ)I",                               (void*)nContextGetMessage },
1208{"nContextInitToClient",           "(I)V",                                  (void*)nContextInitToClient },
1209{"nContextDeinitToClient",         "(I)V",                                  (void*)nContextDeinitToClient },
1210
1211
1212// All methods below are thread protected in java.
1213{"rsnContextCreate",                 "(II)I",                                (void*)nContextCreate },
1214{"rsnContextCreateGL",               "(IIZ)I",                               (void*)nContextCreateGL },
1215{"rsnContextFinish",                 "(I)V",                                  (void*)nContextFinish },
1216{"rsnContextSetPriority",            "(II)V",                                 (void*)nContextSetPriority },
1217{"rsnContextSetSurface",             "(IIILandroid/view/Surface;)V",          (void*)nContextSetSurface },
1218{"rsnContextDestroy",                "(I)V",                                 (void*)nContextDestroy },
1219{"rsnContextDump",                   "(II)V",                                 (void*)nContextDump },
1220{"rsnContextPause",                  "(I)V",                                  (void*)nContextPause },
1221{"rsnContextResume",                 "(I)V",                                  (void*)nContextResume },
1222{"rsnAssignName",                    "(II[B)V",                               (void*)nAssignName },
1223{"rsnGetName",                       "(II)Ljava/lang/String;",               (void*)nGetName },
1224{"rsnObjDestroy",                    "(II)V",                                 (void*)nObjDestroy },
1225
1226{"rsnFileOpen",                      "(I[B)I",                                (void*)nFileOpen },
1227{"rsnFileA3DCreateFromAssetStream",  "(II)I",                                 (void*)nFileA3DCreateFromAssetStream },
1228{"rsnFileA3DGetNumIndexEntries",     "(II)I",                                 (void*)nFileA3DGetNumIndexEntries },
1229{"rsnFileA3DGetIndexEntries",        "(III[I[Ljava/lang/String;)V",          (void*)nFileA3DGetIndexEntries },
1230{"rsnFileA3DGetEntryByIndex",        "(III)I",                                (void*)nFileA3DGetEntryByIndex },
1231
1232{"rsnFontCreateFromFile",            "(ILjava/lang/String;II)I",             (void*)nFontCreateFromFile },
1233
1234{"rsnElementCreate",                 "(IIIZI)I",                              (void*)nElementCreate },
1235{"rsnElementCreate2",                "(I[I[Ljava/lang/String;[I)I",           (void*)nElementCreate2 },
1236{"rsnElementGetNativeData",          "(II[I)V",                               (void*)nElementGetNativeData },
1237{"rsnElementGetSubElements",         "(II[I[Ljava/lang/String;)V",           (void*)nElementGetSubElements },
1238
1239{"rsnTypeBegin",                     "(II)V",                                 (void*)nTypeBegin },
1240{"rsnTypeAdd",                       "(III)V",                                (void*)nTypeAdd },
1241{"rsnTypeCreate",                    "(I)I",                                  (void*)nTypeCreate },
1242{"rsnTypeGetNativeData",             "(II[I)V",                                (void*)nTypeGetNativeData },
1243
1244{"rsnAllocationCreateTyped",         "(II)I",                                 (void*)nAllocationCreateTyped },
1245{"rsnAllocationCreateFromBitmap",    "(IIZLandroid/graphics/Bitmap;)I",       (void*)nAllocationCreateFromBitmap },
1246{"rsnAllocationCreateBitmapRef",     "(IILandroid/graphics/Bitmap;)I",        (void*)nAllocationCreateBitmapRef },
1247{"rsnAllocationCreateFromBitmapBoxed","(IIZLandroid/graphics/Bitmap;)I",      (void*)nAllocationCreateFromBitmapBoxed },
1248{"rsnAllocationCreateFromAssetStream","(IIZI)I",                              (void*)nAllocationCreateFromAssetStream },
1249{"rsnAllocationUploadToTexture",     "(IIZI)V",                               (void*)nAllocationUploadToTexture },
1250{"rsnAllocationUploadToBufferObject","(II)V",                                 (void*)nAllocationUploadToBufferObject },
1251{"rsnAllocationSubData1D",           "(IIII[II)V",                            (void*)nAllocationSubData1D_i },
1252{"rsnAllocationSubData1D",           "(IIII[SI)V",                            (void*)nAllocationSubData1D_s },
1253{"rsnAllocationSubData1D",           "(IIII[BI)V",                            (void*)nAllocationSubData1D_b },
1254{"rsnAllocationSubData1D",           "(IIII[FI)V",                            (void*)nAllocationSubData1D_f },
1255{"rsnAllocationSubElementData1D",    "(IIII[BI)V",                            (void*)nAllocationSubElementData1D },
1256{"rsnAllocationSubData2D",           "(IIIIII[II)V",                          (void*)nAllocationSubData2D_i },
1257{"rsnAllocationSubData2D",           "(IIIIII[FI)V",                          (void*)nAllocationSubData2D_f },
1258{"rsnAllocationRead",                "(II[I)V",                               (void*)nAllocationRead_i },
1259{"rsnAllocationRead",                "(II[F)V",                               (void*)nAllocationRead_f },
1260{"rsnAllocationGetType",             "(II)I",                                 (void*)nAllocationGetType},
1261
1262{"rsnAdapter1DBindAllocation",       "(III)V",                                (void*)nAdapter1DBindAllocation },
1263{"rsnAdapter1DSetConstraint",        "(IIII)V",                               (void*)nAdapter1DSetConstraint },
1264{"rsnAdapter1DData",                 "(II[I)V",                               (void*)nAdapter1DData_i },
1265{"rsnAdapter1DData",                 "(II[F)V",                               (void*)nAdapter1DData_f },
1266{"rsnAdapter1DSubData",              "(IIII[I)V",                             (void*)nAdapter1DSubData_i },
1267{"rsnAdapter1DSubData",              "(IIII[F)V",                             (void*)nAdapter1DSubData_f },
1268{"rsnAdapter1DCreate",               "(I)I",                                  (void*)nAdapter1DCreate },
1269
1270{"rsnAdapter2DBindAllocation",       "(III)V",                                (void*)nAdapter2DBindAllocation },
1271{"rsnAdapter2DSetConstraint",        "(IIII)V",                               (void*)nAdapter2DSetConstraint },
1272{"rsnAdapter2DData",                 "(II[I)V",                               (void*)nAdapter2DData_i },
1273{"rsnAdapter2DData",                 "(II[F)V",                               (void*)nAdapter2DData_f },
1274{"rsnAdapter2DSubData",              "(IIIIII[I)V",                           (void*)nAdapter2DSubData_i },
1275{"rsnAdapter2DSubData",              "(IIIIII[F)V",                           (void*)nAdapter2DSubData_f },
1276{"rsnAdapter2DCreate",               "(I)I",                                  (void*)nAdapter2DCreate },
1277
1278{"rsnScriptBindAllocation",          "(IIII)V",                               (void*)nScriptBindAllocation },
1279{"rsnScriptSetTimeZone",             "(II[B)V",                               (void*)nScriptSetTimeZone },
1280{"rsnScriptInvoke",                  "(III)V",                                (void*)nScriptInvoke },
1281{"rsnScriptInvokeV",                 "(III[B)V",                              (void*)nScriptInvokeV },
1282{"rsnScriptSetVarI",                 "(IIII)V",                               (void*)nScriptSetVarI },
1283{"rsnScriptSetVarF",                 "(IIIF)V",                               (void*)nScriptSetVarF },
1284{"rsnScriptSetVarV",                 "(III[B)V",                              (void*)nScriptSetVarV },
1285
1286{"rsnScriptCBegin",                  "(I)V",                                  (void*)nScriptCBegin },
1287{"rsnScriptCSetScript",              "(I[BII)V",                              (void*)nScriptCSetScript },
1288{"rsnScriptCCreate",                 "(I)I",                                  (void*)nScriptCCreate },
1289
1290{"rsnProgramStoreBegin",             "(III)V",                                (void*)nProgramStoreBegin },
1291{"rsnProgramStoreDepthFunc",         "(II)V",                                 (void*)nProgramStoreDepthFunc },
1292{"rsnProgramStoreDepthMask",         "(IZ)V",                                 (void*)nProgramStoreDepthMask },
1293{"rsnProgramStoreColorMask",         "(IZZZZ)V",                              (void*)nProgramStoreColorMask },
1294{"rsnProgramStoreBlendFunc",         "(III)V",                                (void*)nProgramStoreBlendFunc },
1295{"rsnProgramStoreDither",            "(IZ)V",                                 (void*)nProgramStoreDither },
1296{"rsnProgramStoreCreate",            "(I)I",                                  (void*)nProgramStoreCreate },
1297
1298{"rsnProgramBindConstants",          "(IIII)V",                               (void*)nProgramBindConstants },
1299{"rsnProgramBindTexture",            "(IIII)V",                               (void*)nProgramBindTexture },
1300{"rsnProgramBindSampler",            "(IIII)V",                               (void*)nProgramBindSampler },
1301
1302{"rsnProgramFragmentCreate",         "(I[I)I",                                (void*)nProgramFragmentCreate },
1303{"rsnProgramFragmentCreate2",        "(ILjava/lang/String;[I)I",              (void*)nProgramFragmentCreate2 },
1304
1305{"rsnProgramRasterCreate",           "(IZZZ)I",                             (void*)nProgramRasterCreate },
1306{"rsnProgramRasterSetLineWidth",     "(IIF)V",                                (void*)nProgramRasterSetLineWidth },
1307{"rsnProgramRasterSetCullMode",      "(III)V",                                (void*)nProgramRasterSetCullMode },
1308
1309{"rsnProgramVertexCreate",           "(IZ)I",                                 (void*)nProgramVertexCreate },
1310{"rsnProgramVertexCreate2",          "(ILjava/lang/String;[I)I",              (void*)nProgramVertexCreate2 },
1311
1312{"rsnContextBindRootScript",         "(II)V",                                 (void*)nContextBindRootScript },
1313{"rsnContextBindProgramStore",       "(II)V",                                (void*)nContextBindProgramStore },
1314{"rsnContextBindProgramFragment",    "(II)V",                                 (void*)nContextBindProgramFragment },
1315{"rsnContextBindProgramVertex",      "(II)V",                                 (void*)nContextBindProgramVertex },
1316{"rsnContextBindProgramRaster",      "(II)V",                                 (void*)nContextBindProgramRaster },
1317
1318{"rsnSamplerBegin",                  "(I)V",                                  (void*)nSamplerBegin },
1319{"rsnSamplerSet",                    "(III)V",                                (void*)nSamplerSet },
1320{"rsnSamplerCreate",                 "(I)I",                                  (void*)nSamplerCreate },
1321
1322{"rsnMeshCreate",                    "(III)I",                                (void*)nMeshCreate },
1323{"rsnMeshBindVertex",                "(IIII)V",                               (void*)nMeshBindVertex },
1324{"rsnMeshBindIndex",                 "(IIIII)V",                              (void*)nMeshBindIndex },
1325
1326{"rsnMeshGetVertexBufferCount",      "(II)I",                                 (void*)nMeshGetVertexBufferCount },
1327{"rsnMeshGetIndexCount",             "(II)I",                                 (void*)nMeshGetIndexCount },
1328{"rsnMeshGetVertices",               "(II[II)V",                             (void*)nMeshGetVertices },
1329{"rsnMeshGetIndices",                "(II[I[II)V",                            (void*)nMeshGetIndices },
1330
1331};
1332
1333static int registerFuncs(JNIEnv *_env)
1334{
1335    return android::AndroidRuntime::registerNativeMethods(
1336            _env, classPathName, methods, NELEM(methods));
1337}
1338
1339// ---------------------------------------------------------------------------
1340
1341jint JNI_OnLoad(JavaVM* vm, void* reserved)
1342{
1343    JNIEnv* env = NULL;
1344    jint result = -1;
1345
1346    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
1347        LOGE("ERROR: GetEnv failed\n");
1348        goto bail;
1349    }
1350    assert(env != NULL);
1351
1352    if (registerFuncs(env) < 0) {
1353        LOGE("ERROR: MediaPlayer native registration failed\n");
1354        goto bail;
1355    }
1356
1357    /* success -- return valid version number */
1358    result = JNI_VERSION_1_4;
1359
1360bail:
1361    return result;
1362}
1363
1364