android_renderscript_RenderScript.cpp revision b89aaacb2ca9d062e0a17a32e3d4dbf3f6948a17
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)", 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("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", con, (void *)script, slot, val);
795    rsScriptSetVarF(con, (RsScript)script, slot, val);
796}
797
798static void
799nScriptSetVarD(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, double val)
800{
801    LOG_API("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", con, (void *)script, slot, val);
802    rsScriptSetVarD(con, (RsScript)script, slot, val);
803}
804
805static void
806nScriptSetVarV(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jbyteArray data)
807{
808    LOG_API("nScriptSetVarV, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
809    jint len = _env->GetArrayLength(data);
810    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
811    rsScriptSetVarV(con, (RsScript)script, slot, ptr, len);
812    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
813}
814
815
816static void
817nScriptSetTimeZone(JNIEnv *_env, jobject _this, RsContext con, jint script, jbyteArray timeZone)
818{
819    LOG_API("nScriptCSetTimeZone, con(%p), s(%p), timeZone(%s)", con, (void *)script, (const char *)timeZone);
820
821    jint length = _env->GetArrayLength(timeZone);
822    jbyte* timeZone_ptr;
823    timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
824
825    rsScriptSetTimeZone(con, (RsScript)script, (const char *)timeZone_ptr, length);
826
827    if (timeZone_ptr) {
828        _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
829    }
830}
831
832static void
833nScriptInvoke(JNIEnv *_env, jobject _this, RsContext con, jint obj, jint slot)
834{
835    LOG_API("nScriptInvoke, con(%p), script(%p)", con, (void *)obj);
836    rsScriptInvoke(con, (RsScript)obj, slot);
837}
838
839static void
840nScriptInvokeV(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jbyteArray data)
841{
842    LOG_API("nScriptInvokeV, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
843    jint len = _env->GetArrayLength(data);
844    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
845    rsScriptInvokeV(con, (RsScript)script, slot, ptr, len);
846    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
847}
848
849
850// -----------------------------------
851
852static void
853nScriptCBegin(JNIEnv *_env, jobject _this, RsContext con)
854{
855    LOG_API("nScriptCBegin, con(%p)", con);
856    rsScriptCBegin(con);
857}
858
859static void
860nScriptCSetScript(JNIEnv *_env, jobject _this, RsContext con, jbyteArray scriptRef,
861                  jint offset, jint length)
862{
863    LOG_API("!!! nScriptCSetScript, con(%p)", con);
864    jint _exception = 0;
865    jint remaining;
866    jbyte* script_base = 0;
867    jbyte* script_ptr;
868    if (!scriptRef) {
869        _exception = 1;
870        //_env->ThrowNew(IAEClass, "script == null");
871        goto exit;
872    }
873    if (offset < 0) {
874        _exception = 1;
875        //_env->ThrowNew(IAEClass, "offset < 0");
876        goto exit;
877    }
878    if (length < 0) {
879        _exception = 1;
880        //_env->ThrowNew(IAEClass, "length < 0");
881        goto exit;
882    }
883    remaining = _env->GetArrayLength(scriptRef) - offset;
884    if (remaining < length) {
885        _exception = 1;
886        //_env->ThrowNew(IAEClass, "length > script.length - offset");
887        goto exit;
888    }
889    script_base = (jbyte *)
890        _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
891    script_ptr = script_base + offset;
892
893    rsScriptCSetText(con, (const char *)script_ptr, length);
894
895exit:
896    if (script_base) {
897        _env->ReleasePrimitiveArrayCritical(scriptRef, script_base,
898                _exception ? JNI_ABORT: 0);
899    }
900}
901
902static jint
903nScriptCCreate(JNIEnv *_env, jobject _this, RsContext con)
904{
905    LOG_API("nScriptCCreate, con(%p)", con);
906    return (jint)rsScriptCCreate(con);
907}
908
909// ---------------------------------------------------------------------------
910
911static void
912nProgramStoreBegin(JNIEnv *_env, jobject _this, RsContext con, jint in, jint out)
913{
914    LOG_API("nProgramStoreBegin, con(%p), in(%p), out(%p)", con, (RsElement)in, (RsElement)out);
915    rsProgramStoreBegin(con, (RsElement)in, (RsElement)out);
916}
917
918static void
919nProgramStoreDepthFunc(JNIEnv *_env, jobject _this, RsContext con, jint func)
920{
921    LOG_API("nProgramStoreDepthFunc, con(%p), func(%i)", con, func);
922    rsProgramStoreDepthFunc(con, (RsDepthFunc)func);
923}
924
925static void
926nProgramStoreDepthMask(JNIEnv *_env, jobject _this, RsContext con, jboolean enable)
927{
928    LOG_API("nProgramStoreDepthMask, con(%p), enable(%i)", con, enable);
929    rsProgramStoreDepthMask(con, enable);
930}
931
932static void
933nProgramStoreColorMask(JNIEnv *_env, jobject _this, RsContext con, jboolean r, jboolean g, jboolean b, jboolean a)
934{
935    LOG_API("nProgramStoreColorMask, con(%p), r(%i), g(%i), b(%i), a(%i)", con, r, g, b, a);
936    rsProgramStoreColorMask(con, r, g, b, a);
937}
938
939static void
940nProgramStoreBlendFunc(JNIEnv *_env, jobject _this, RsContext con, int src, int dst)
941{
942    LOG_API("nProgramStoreBlendFunc, con(%p), src(%i), dst(%i)", con, src, dst);
943    rsProgramStoreBlendFunc(con, (RsBlendSrcFunc)src, (RsBlendDstFunc)dst);
944}
945
946static void
947nProgramStoreDither(JNIEnv *_env, jobject _this, RsContext con, jboolean enable)
948{
949    LOG_API("nProgramStoreDither, con(%p), enable(%i)", con, enable);
950    rsProgramStoreDither(con, enable);
951}
952
953static jint
954nProgramStoreCreate(JNIEnv *_env, jobject _this, RsContext con)
955{
956    LOG_API("nProgramStoreCreate, con(%p)", con);
957    return (jint)rsProgramStoreCreate(con);
958}
959
960// ---------------------------------------------------------------------------
961
962static void
963nProgramBindConstants(JNIEnv *_env, jobject _this, RsContext con, jint vpv, jint slot, jint a)
964{
965    LOG_API("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", con, (RsProgramVertex)vpv, slot, (RsAllocation)a);
966    rsProgramBindConstants(con, (RsProgram)vpv, slot, (RsAllocation)a);
967}
968
969static void
970nProgramBindTexture(JNIEnv *_env, jobject _this, RsContext con, jint vpf, jint slot, jint a)
971{
972    LOG_API("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
973    rsProgramBindTexture(con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
974}
975
976static void
977nProgramBindSampler(JNIEnv *_env, jobject _this, RsContext con, jint vpf, jint slot, jint a)
978{
979    LOG_API("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsSampler)a);
980    rsProgramBindSampler(con, (RsProgramFragment)vpf, slot, (RsSampler)a);
981}
982
983// ---------------------------------------------------------------------------
984
985static jint
986nProgramFragmentCreate(JNIEnv *_env, jobject _this, RsContext con, jstring shader, jintArray params)
987{
988    const char* shaderUTF = _env->GetStringUTFChars(shader, NULL);
989    jint shaderLen = _env->GetStringUTFLength(shader);
990    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
991    jint paramLen = _env->GetArrayLength(params);
992
993    LOG_API("nProgramFragmentCreate, con(%p), shaderLen(%i), paramLen(%i)", con, shaderLen, paramLen);
994
995    jint ret = (jint)rsProgramFragmentCreate(con, shaderUTF, shaderLen, (uint32_t *)paramPtr, paramLen);
996    _env->ReleaseStringUTFChars(shader, shaderUTF);
997    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
998    return ret;
999}
1000
1001
1002// ---------------------------------------------------------------------------
1003
1004static jint
1005nProgramVertexCreate(JNIEnv *_env, jobject _this, RsContext con, jstring shader, jintArray params)
1006{
1007    const char* shaderUTF = _env->GetStringUTFChars(shader, NULL);
1008    jint shaderLen = _env->GetStringUTFLength(shader);
1009    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1010    jint paramLen = _env->GetArrayLength(params);
1011
1012    LOG_API("nProgramVertexCreate, con(%p), shaderLen(%i), paramLen(%i)", con, shaderLen, paramLen);
1013
1014    jint ret = (jint)rsProgramVertexCreate(con, shaderUTF, shaderLen, (uint32_t *)paramPtr, paramLen);
1015    _env->ReleaseStringUTFChars(shader, shaderUTF);
1016    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1017    return ret;
1018}
1019
1020// ---------------------------------------------------------------------------
1021
1022static jint
1023nProgramRasterCreate(JNIEnv *_env, jobject _this, RsContext con, jboolean pointSmooth, jboolean lineSmooth, jboolean pointSprite)
1024{
1025    LOG_API("nProgramRasterCreate, con(%p), pointSmooth(%i), lineSmooth(%i), pointSprite(%i)",
1026            con, pointSmooth, lineSmooth, pointSprite);
1027    return (jint)rsProgramRasterCreate(con, pointSmooth, lineSmooth, pointSprite);
1028}
1029
1030static void
1031nProgramRasterSetLineWidth(JNIEnv *_env, jobject _this, RsContext con, jint vpr, jfloat v)
1032{
1033    LOG_API("nProgramRasterSetLineWidth, con(%p), vpf(%p), value(%f)", con, (RsProgramRaster)vpr, v);
1034    rsProgramRasterSetLineWidth(con, (RsProgramRaster)vpr, v);
1035}
1036
1037static void
1038nProgramRasterSetCullMode(JNIEnv *_env, jobject _this, RsContext con, jint vpr, jint v)
1039{
1040    LOG_API("nProgramRasterSetCullMode, con(%p), vpf(%p), value(%i)", con, (RsProgramRaster)vpr, v);
1041    rsProgramRasterSetCullMode(con, (RsProgramRaster)vpr, (RsCullMode)v);
1042}
1043
1044
1045// ---------------------------------------------------------------------------
1046
1047static void
1048nContextBindRootScript(JNIEnv *_env, jobject _this, RsContext con, jint script)
1049{
1050    LOG_API("nContextBindRootScript, con(%p), script(%p)", con, (RsScript)script);
1051    rsContextBindRootScript(con, (RsScript)script);
1052}
1053
1054static void
1055nContextBindProgramStore(JNIEnv *_env, jobject _this, RsContext con, jint pfs)
1056{
1057    LOG_API("nContextBindProgramStore, con(%p), pfs(%p)", con, (RsProgramStore)pfs);
1058    rsContextBindProgramStore(con, (RsProgramStore)pfs);
1059}
1060
1061static void
1062nContextBindProgramFragment(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1063{
1064    LOG_API("nContextBindProgramFragment, con(%p), pf(%p)", con, (RsProgramFragment)pf);
1065    rsContextBindProgramFragment(con, (RsProgramFragment)pf);
1066}
1067
1068static void
1069nContextBindProgramVertex(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1070{
1071    LOG_API("nContextBindProgramVertex, con(%p), pf(%p)", con, (RsProgramVertex)pf);
1072    rsContextBindProgramVertex(con, (RsProgramVertex)pf);
1073}
1074
1075static void
1076nContextBindProgramRaster(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1077{
1078    LOG_API("nContextBindProgramRaster, con(%p), pf(%p)", con, (RsProgramRaster)pf);
1079    rsContextBindProgramRaster(con, (RsProgramRaster)pf);
1080}
1081
1082
1083// ---------------------------------------------------------------------------
1084
1085static void
1086nSamplerBegin(JNIEnv *_env, jobject _this, RsContext con)
1087{
1088    LOG_API("nSamplerBegin, con(%p)", con);
1089    rsSamplerBegin(con);
1090}
1091
1092static void
1093nSamplerSet(JNIEnv *_env, jobject _this, RsContext con, jint p, jint v)
1094{
1095    LOG_API("nSamplerSet, con(%p), param(%i), value(%i)", con, p, v);
1096    rsSamplerSet(con, (RsSamplerParam)p, (RsSamplerValue)v);
1097}
1098
1099static jint
1100nSamplerCreate(JNIEnv *_env, jobject _this, RsContext con)
1101{
1102    LOG_API("nSamplerCreate, con(%p)", con);
1103    return (jint)rsSamplerCreate(con);
1104}
1105
1106// ---------------------------------------------------------------------------
1107
1108static jint
1109nMeshCreate(JNIEnv *_env, jobject _this, RsContext con, jint vtxCount, jint idxCount)
1110{
1111    LOG_API("nMeshCreate, con(%p), vtxCount(%i), idxCount(%i)", con, vtxCount, idxCount);
1112    int id = (int)rsMeshCreate(con, vtxCount, idxCount);
1113    return id;
1114}
1115
1116static void
1117nMeshBindVertex(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jint alloc, jint slot)
1118{
1119    LOG_API("nMeshBindVertex, con(%p), Mesh(%p), Alloc(%p), slot(%i)", con, (RsMesh)mesh, (RsAllocation)alloc, slot);
1120    rsMeshBindVertex(con, (RsMesh)mesh, (RsAllocation)alloc, slot);
1121}
1122
1123static void
1124nMeshBindIndex(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jint alloc, jint primID, jint slot)
1125{
1126    LOG_API("nMeshBindIndex, con(%p), Mesh(%p), Alloc(%p)", con, (RsMesh)mesh, (RsAllocation)alloc);
1127    rsMeshBindIndex(con, (RsMesh)mesh, (RsAllocation)alloc, primID, slot);
1128}
1129
1130static jint
1131nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, RsContext con, jint mesh)
1132{
1133    LOG_API("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1134    jint vtxCount = 0;
1135    rsMeshGetVertexBufferCount(con, (RsMesh)mesh, &vtxCount);
1136    return vtxCount;
1137}
1138
1139static jint
1140nMeshGetIndexCount(JNIEnv *_env, jobject _this, RsContext con, jint mesh)
1141{
1142    LOG_API("nMeshGetIndexCount, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1143    jint idxCount = 0;
1144    rsMeshGetIndexCount(con, (RsMesh)mesh, &idxCount);
1145    return idxCount;
1146}
1147
1148static void
1149nMeshGetVertices(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jintArray _ids, int numVtxIDs)
1150{
1151    LOG_API("nMeshGetVertices, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1152
1153    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
1154    rsMeshGetVertices(con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
1155
1156    for(jint i = 0; i < numVtxIDs; i ++) {
1157        _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&allocs[i]);
1158    }
1159
1160    free(allocs);
1161}
1162
1163static void
1164nMeshGetIndices(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jintArray _idxIds, jintArray _primitives, int numIndices)
1165{
1166    LOG_API("nMeshGetVertices, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1167
1168    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
1169    uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
1170
1171    rsMeshGetIndices(con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
1172
1173    for(jint i = 0; i < numIndices; i ++) {
1174        _env->SetIntArrayRegion(_idxIds, i, 1, (const jint*)&allocs[i]);
1175        _env->SetIntArrayRegion(_primitives, i, 1, (const jint*)&prims[i]);
1176    }
1177
1178    free(allocs);
1179    free(prims);
1180}
1181
1182// ---------------------------------------------------------------------------
1183
1184
1185static const char *classPathName = "android/renderscript/RenderScript";
1186
1187static JNINativeMethod methods[] = {
1188{"_nInit",                         "()V",                                  (void*)_nInit },
1189{"nInitElements",                  "(IIII)V",                              (void*)nInitElements },
1190
1191{"nDeviceCreate",                  "()I",                                  (void*)nDeviceCreate },
1192{"nDeviceDestroy",                 "(I)V",                                 (void*)nDeviceDestroy },
1193{"nDeviceSetConfig",               "(III)V",                               (void*)nDeviceSetConfig },
1194{"nContextGetMessage",             "(I[IZ)I",                               (void*)nContextGetMessage },
1195{"nContextInitToClient",           "(I)V",                                  (void*)nContextInitToClient },
1196{"nContextDeinitToClient",         "(I)V",                                  (void*)nContextDeinitToClient },
1197
1198
1199// All methods below are thread protected in java.
1200{"rsnContextCreate",                 "(II)I",                                 (void*)nContextCreate },
1201{"rsnContextCreateGL",               "(IIZ)I",                                (void*)nContextCreateGL },
1202{"rsnContextFinish",                 "(I)V",                                  (void*)nContextFinish },
1203{"rsnContextSetPriority",            "(II)V",                                 (void*)nContextSetPriority },
1204{"rsnContextSetSurface",             "(IIILandroid/view/Surface;)V",          (void*)nContextSetSurface },
1205{"rsnContextDestroy",                "(I)V",                                  (void*)nContextDestroy },
1206{"rsnContextDump",                   "(II)V",                                 (void*)nContextDump },
1207{"rsnContextPause",                  "(I)V",                                  (void*)nContextPause },
1208{"rsnContextResume",                 "(I)V",                                  (void*)nContextResume },
1209{"rsnAssignName",                    "(II[B)V",                               (void*)nAssignName },
1210{"rsnGetName",                       "(II)Ljava/lang/String;",                (void*)nGetName },
1211{"rsnObjDestroy",                    "(II)V",                                 (void*)nObjDestroy },
1212
1213{"rsnFileOpen",                      "(I[B)I",                                (void*)nFileOpen },
1214{"rsnFileA3DCreateFromAssetStream",  "(II)I",                                 (void*)nFileA3DCreateFromAssetStream },
1215{"rsnFileA3DGetNumIndexEntries",     "(II)I",                                 (void*)nFileA3DGetNumIndexEntries },
1216{"rsnFileA3DGetIndexEntries",        "(III[I[Ljava/lang/String;)V",           (void*)nFileA3DGetIndexEntries },
1217{"rsnFileA3DGetEntryByIndex",        "(III)I",                                (void*)nFileA3DGetEntryByIndex },
1218
1219{"rsnFontCreateFromFile",            "(ILjava/lang/String;II)I",              (void*)nFontCreateFromFile },
1220
1221{"rsnElementCreate",                 "(IIIZI)I",                              (void*)nElementCreate },
1222{"rsnElementCreate2",                "(I[I[Ljava/lang/String;[I)I",           (void*)nElementCreate2 },
1223{"rsnElementGetNativeData",          "(II[I)V",                               (void*)nElementGetNativeData },
1224{"rsnElementGetSubElements",         "(II[I[Ljava/lang/String;)V",            (void*)nElementGetSubElements },
1225
1226{"rsnTypeBegin",                     "(II)V",                                 (void*)nTypeBegin },
1227{"rsnTypeAdd",                       "(III)V",                                (void*)nTypeAdd },
1228{"rsnTypeCreate",                    "(I)I",                                  (void*)nTypeCreate },
1229{"rsnTypeGetNativeData",             "(II[I)V",                               (void*)nTypeGetNativeData },
1230
1231{"rsnAllocationCreateTyped",         "(II)I",                                 (void*)nAllocationCreateTyped },
1232{"rsnAllocationCreateFromBitmap",    "(IIZLandroid/graphics/Bitmap;)I",       (void*)nAllocationCreateFromBitmap },
1233{"rsnAllocationCreateBitmapRef",     "(IILandroid/graphics/Bitmap;)I",        (void*)nAllocationCreateBitmapRef },
1234{"rsnAllocationCreateFromBitmapBoxed","(IIZLandroid/graphics/Bitmap;)I",      (void*)nAllocationCreateFromBitmapBoxed },
1235{"rsnAllocationCreateFromAssetStream","(IIZI)I",                              (void*)nAllocationCreateFromAssetStream },
1236{"rsnAllocationUploadToTexture",     "(IIZI)V",                               (void*)nAllocationUploadToTexture },
1237{"rsnAllocationUploadToBufferObject","(II)V",                                 (void*)nAllocationUploadToBufferObject },
1238{"rsnAllocationSubData1D",           "(IIII[II)V",                            (void*)nAllocationSubData1D_i },
1239{"rsnAllocationSubData1D",           "(IIII[SI)V",                            (void*)nAllocationSubData1D_s },
1240{"rsnAllocationSubData1D",           "(IIII[BI)V",                            (void*)nAllocationSubData1D_b },
1241{"rsnAllocationSubData1D",           "(IIII[FI)V",                            (void*)nAllocationSubData1D_f },
1242{"rsnAllocationSubElementData1D",    "(IIII[BI)V",                            (void*)nAllocationSubElementData1D },
1243{"rsnAllocationSubData2D",           "(IIIIII[II)V",                          (void*)nAllocationSubData2D_i },
1244{"rsnAllocationSubData2D",           "(IIIIII[FI)V",                          (void*)nAllocationSubData2D_f },
1245{"rsnAllocationRead",                "(II[I)V",                               (void*)nAllocationRead_i },
1246{"rsnAllocationRead",                "(II[F)V",                               (void*)nAllocationRead_f },
1247{"rsnAllocationGetType",             "(II)I",                                 (void*)nAllocationGetType},
1248
1249{"rsnAdapter1DBindAllocation",       "(III)V",                                (void*)nAdapter1DBindAllocation },
1250{"rsnAdapter1DSetConstraint",        "(IIII)V",                               (void*)nAdapter1DSetConstraint },
1251{"rsnAdapter1DData",                 "(II[I)V",                               (void*)nAdapter1DData_i },
1252{"rsnAdapter1DData",                 "(II[F)V",                               (void*)nAdapter1DData_f },
1253{"rsnAdapter1DSubData",              "(IIII[I)V",                             (void*)nAdapter1DSubData_i },
1254{"rsnAdapter1DSubData",              "(IIII[F)V",                             (void*)nAdapter1DSubData_f },
1255{"rsnAdapter1DCreate",               "(I)I",                                  (void*)nAdapter1DCreate },
1256
1257{"rsnAdapter2DBindAllocation",       "(III)V",                                (void*)nAdapter2DBindAllocation },
1258{"rsnAdapter2DSetConstraint",        "(IIII)V",                               (void*)nAdapter2DSetConstraint },
1259{"rsnAdapter2DData",                 "(II[I)V",                               (void*)nAdapter2DData_i },
1260{"rsnAdapter2DData",                 "(II[F)V",                               (void*)nAdapter2DData_f },
1261{"rsnAdapter2DSubData",              "(IIIIII[I)V",                           (void*)nAdapter2DSubData_i },
1262{"rsnAdapter2DSubData",              "(IIIIII[F)V",                           (void*)nAdapter2DSubData_f },
1263{"rsnAdapter2DCreate",               "(I)I",                                  (void*)nAdapter2DCreate },
1264
1265{"rsnScriptBindAllocation",          "(IIII)V",                               (void*)nScriptBindAllocation },
1266{"rsnScriptSetTimeZone",             "(II[B)V",                               (void*)nScriptSetTimeZone },
1267{"rsnScriptInvoke",                  "(III)V",                                (void*)nScriptInvoke },
1268{"rsnScriptInvokeV",                 "(III[B)V",                              (void*)nScriptInvokeV },
1269{"rsnScriptSetVarI",                 "(IIII)V",                               (void*)nScriptSetVarI },
1270{"rsnScriptSetVarF",                 "(IIIF)V",                               (void*)nScriptSetVarF },
1271{"rsnScriptSetVarD",                 "(IIID)V",                               (void*)nScriptSetVarD },
1272{"rsnScriptSetVarV",                 "(III[B)V",                              (void*)nScriptSetVarV },
1273
1274{"rsnScriptCBegin",                  "(I)V",                                  (void*)nScriptCBegin },
1275{"rsnScriptCSetScript",              "(I[BII)V",                              (void*)nScriptCSetScript },
1276{"rsnScriptCCreate",                 "(I)I",                                  (void*)nScriptCCreate },
1277
1278{"rsnProgramStoreBegin",             "(III)V",                                (void*)nProgramStoreBegin },
1279{"rsnProgramStoreDepthFunc",         "(II)V",                                 (void*)nProgramStoreDepthFunc },
1280{"rsnProgramStoreDepthMask",         "(IZ)V",                                 (void*)nProgramStoreDepthMask },
1281{"rsnProgramStoreColorMask",         "(IZZZZ)V",                              (void*)nProgramStoreColorMask },
1282{"rsnProgramStoreBlendFunc",         "(III)V",                                (void*)nProgramStoreBlendFunc },
1283{"rsnProgramStoreDither",            "(IZ)V",                                 (void*)nProgramStoreDither },
1284{"rsnProgramStoreCreate",            "(I)I",                                  (void*)nProgramStoreCreate },
1285
1286{"rsnProgramBindConstants",          "(IIII)V",                               (void*)nProgramBindConstants },
1287{"rsnProgramBindTexture",            "(IIII)V",                               (void*)nProgramBindTexture },
1288{"rsnProgramBindSampler",            "(IIII)V",                               (void*)nProgramBindSampler },
1289
1290{"rsnProgramFragmentCreate",        "(ILjava/lang/String;[I)I",               (void*)nProgramFragmentCreate },
1291
1292{"rsnProgramRasterCreate",           "(IZZZ)I",                               (void*)nProgramRasterCreate },
1293{"rsnProgramRasterSetLineWidth",     "(IIF)V",                                (void*)nProgramRasterSetLineWidth },
1294{"rsnProgramRasterSetCullMode",      "(III)V",                                (void*)nProgramRasterSetCullMode },
1295
1296{"rsnProgramVertexCreate",          "(ILjava/lang/String;[I)I",               (void*)nProgramVertexCreate },
1297
1298{"rsnContextBindRootScript",         "(II)V",                                 (void*)nContextBindRootScript },
1299{"rsnContextBindProgramStore",       "(II)V",                                 (void*)nContextBindProgramStore },
1300{"rsnContextBindProgramFragment",    "(II)V",                                 (void*)nContextBindProgramFragment },
1301{"rsnContextBindProgramVertex",      "(II)V",                                 (void*)nContextBindProgramVertex },
1302{"rsnContextBindProgramRaster",      "(II)V",                                 (void*)nContextBindProgramRaster },
1303
1304{"rsnSamplerBegin",                  "(I)V",                                  (void*)nSamplerBegin },
1305{"rsnSamplerSet",                    "(III)V",                                (void*)nSamplerSet },
1306{"rsnSamplerCreate",                 "(I)I",                                  (void*)nSamplerCreate },
1307
1308{"rsnMeshCreate",                    "(III)I",                                (void*)nMeshCreate },
1309{"rsnMeshBindVertex",                "(IIII)V",                               (void*)nMeshBindVertex },
1310{"rsnMeshBindIndex",                 "(IIIII)V",                              (void*)nMeshBindIndex },
1311
1312{"rsnMeshGetVertexBufferCount",      "(II)I",                                 (void*)nMeshGetVertexBufferCount },
1313{"rsnMeshGetIndexCount",             "(II)I",                                 (void*)nMeshGetIndexCount },
1314{"rsnMeshGetVertices",               "(II[II)V",                              (void*)nMeshGetVertices },
1315{"rsnMeshGetIndices",                "(II[I[II)V",                            (void*)nMeshGetIndices },
1316
1317};
1318
1319static int registerFuncs(JNIEnv *_env)
1320{
1321    return android::AndroidRuntime::registerNativeMethods(
1322            _env, classPathName, methods, NELEM(methods));
1323}
1324
1325// ---------------------------------------------------------------------------
1326
1327jint JNI_OnLoad(JavaVM* vm, void* reserved)
1328{
1329    JNIEnv* env = NULL;
1330    jint result = -1;
1331
1332    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
1333        LOGE("ERROR: GetEnv failed\n");
1334        goto bail;
1335    }
1336    assert(env != NULL);
1337
1338    if (registerFuncs(env) < 0) {
1339        LOGE("ERROR: MediaPlayer native registration failed\n");
1340        goto bail;
1341    }
1342
1343    /* success -- return valid version number */
1344    result = JNI_VERSION_1_4;
1345
1346bail:
1347    return result;
1348}
1349
1350