android_renderscript_RenderScript.cpp revision 5edc608a0749ed4b7074b5c1243043eb722c3c31
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
590static void
591nAllocationResize1D(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint dimX)
592{
593    LOG_API("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", con, (RsAllocation)alloc, dimX);
594    rsAllocationResize1D(con, (RsAllocation)alloc, dimX);
595}
596
597static void
598nAllocationResize2D(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint dimX, jint dimY)
599{
600    LOG_API("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i), sizeY(%i)", con, (RsAllocation)alloc, dimX, dimY);
601    rsAllocationResize2D(con, (RsAllocation)alloc, dimX, dimY);
602}
603
604// -----------------------------------
605
606static int
607nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, RsContext con, jint native_asset)
608{
609    LOGV("______nFileA3D %u", (uint32_t) native_asset);
610
611    Asset* asset = reinterpret_cast<Asset*>(native_asset);
612
613    jint id = (jint)rsFileA3DCreateFromAssetStream(con, asset->getBuffer(false), asset->getLength());
614    return id;
615}
616
617static int
618nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, RsContext con, jint fileA3D)
619{
620    int32_t numEntries = 0;
621    rsFileA3DGetNumIndexEntries(con, &numEntries, (RsFile)fileA3D);
622    return numEntries;
623}
624
625static void
626nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, RsContext con, jint fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
627{
628    LOGV("______nFileA3D %u", (uint32_t) fileA3D);
629    RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
630
631    rsFileA3DGetIndexEntries(con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
632
633    for(jint i = 0; i < numEntries; i ++) {
634        _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
635        _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
636    }
637
638    free(fileEntries);
639}
640
641static int
642nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, RsContext con, jint fileA3D, jint index)
643{
644    LOGV("______nFileA3D %u", (uint32_t) fileA3D);
645    jint id = (jint)rsFileA3DGetEntryByIndex(con, (uint32_t)index, (RsFile)fileA3D);
646    return id;
647}
648
649// -----------------------------------
650
651static int
652nFontCreateFromFile(JNIEnv *_env, jobject _this, RsContext con, jstring fileName, jint fontSize, jint dpi)
653{
654    const char* fileNameUTF = _env->GetStringUTFChars(fileName, NULL);
655
656    jint id = (jint)rsFontCreateFromFile(con, fileNameUTF, fontSize, dpi);
657    return id;
658}
659
660
661// -----------------------------------
662
663static void
664nAdapter1DBindAllocation(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint alloc)
665{
666    LOG_API("nAdapter1DBindAllocation, con(%p), adapter(%p), alloc(%p)", con, (RsAdapter1D)adapter, (RsAllocation)alloc);
667    rsAdapter1DBindAllocation(con, (RsAdapter1D)adapter, (RsAllocation)alloc);
668}
669
670static void
671nAdapter1DSetConstraint(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint dim, jint value)
672{
673    LOG_API("nAdapter1DSetConstraint, con(%p), adapter(%p), dim(%i), value(%i)", con, (RsAdapter1D)adapter, dim, value);
674    rsAdapter1DSetConstraint(con, (RsAdapter1D)adapter, (RsDimension)dim, value);
675}
676
677static void
678nAdapter1DData_i(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jintArray data)
679{
680    jint len = _env->GetArrayLength(data);
681    LOG_API("nAdapter1DData_i, con(%p), adapter(%p), len(%i)", con, (RsAdapter1D)adapter, len);
682    jint *ptr = _env->GetIntArrayElements(data, NULL);
683    rsAdapter1DData(con, (RsAdapter1D)adapter, ptr);
684    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
685}
686
687static void
688nAdapter1DSubData_i(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint offset, jint count, jintArray data)
689{
690    jint len = _env->GetArrayLength(data);
691    LOG_API("nAdapter1DSubData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAdapter1D)adapter, offset, count, len);
692    jint *ptr = _env->GetIntArrayElements(data, NULL);
693    rsAdapter1DSubData(con, (RsAdapter1D)adapter, offset, count, ptr);
694    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
695}
696
697static void
698nAdapter1DData_f(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jfloatArray data)
699{
700    jint len = _env->GetArrayLength(data);
701    LOG_API("nAdapter1DData_f, con(%p), adapter(%p), len(%i)", con, (RsAdapter1D)adapter, len);
702    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
703    rsAdapter1DData(con, (RsAdapter1D)adapter, ptr);
704    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
705}
706
707static void
708nAdapter1DSubData_f(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint offset, jint count, jfloatArray data)
709{
710    jint len = _env->GetArrayLength(data);
711    LOG_API("nAdapter1DSubData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAdapter1D)adapter, offset, count, len);
712    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
713    rsAdapter1DSubData(con, (RsAdapter1D)adapter, offset, count, ptr);
714    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
715}
716
717static jint
718nAdapter1DCreate(JNIEnv *_env, jobject _this, RsContext con)
719{
720    LOG_API("nAdapter1DCreate, con(%p)", con);
721    return (jint)rsAdapter1DCreate(con);
722}
723
724// -----------------------------------
725
726static void
727nAdapter2DBindAllocation(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint alloc)
728{
729    LOG_API("nAdapter2DBindAllocation, con(%p), adapter(%p), alloc(%p)", con, (RsAdapter2D)adapter, (RsAllocation)alloc);
730    rsAdapter2DBindAllocation(con, (RsAdapter2D)adapter, (RsAllocation)alloc);
731}
732
733static void
734nAdapter2DSetConstraint(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint dim, jint value)
735{
736    LOG_API("nAdapter2DSetConstraint, con(%p), adapter(%p), dim(%i), value(%i)", con, (RsAdapter2D)adapter, dim, value);
737    rsAdapter2DSetConstraint(con, (RsAdapter2D)adapter, (RsDimension)dim, value);
738}
739
740static void
741nAdapter2DData_i(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jintArray data)
742{
743    jint len = _env->GetArrayLength(data);
744    LOG_API("nAdapter2DData_i, con(%p), adapter(%p), len(%i)", con, (RsAdapter2D)adapter, len);
745    jint *ptr = _env->GetIntArrayElements(data, NULL);
746    rsAdapter2DData(con, (RsAdapter2D)adapter, ptr);
747    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
748}
749
750static void
751nAdapter2DData_f(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jfloatArray data)
752{
753    jint len = _env->GetArrayLength(data);
754    LOG_API("nAdapter2DData_f, con(%p), adapter(%p), len(%i)", con, (RsAdapter2D)adapter, len);
755    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
756    rsAdapter2DData(con, (RsAdapter2D)adapter, ptr);
757    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
758}
759
760static void
761nAdapter2DSubData_i(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint xoff, jint yoff, jint w, jint h, jintArray data)
762{
763    jint len = _env->GetArrayLength(data);
764    LOG_API("nAdapter2DSubData_i, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)",
765            con, (RsAdapter2D)adapter, xoff, yoff, w, h, len);
766    jint *ptr = _env->GetIntArrayElements(data, NULL);
767    rsAdapter2DSubData(con, (RsAdapter2D)adapter, xoff, yoff, w, h, ptr);
768    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
769}
770
771static void
772nAdapter2DSubData_f(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint xoff, jint yoff, jint w, jint h, jfloatArray data)
773{
774    jint len = _env->GetArrayLength(data);
775    LOG_API("nAdapter2DSubData_f, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)",
776            con, (RsAdapter2D)adapter, xoff, yoff, w, h, len);
777    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
778    rsAdapter2DSubData(con, (RsAdapter1D)adapter, xoff, yoff, w, h, ptr);
779    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
780}
781
782static jint
783nAdapter2DCreate(JNIEnv *_env, jobject _this, RsContext con)
784{
785    LOG_API("nAdapter2DCreate, con(%p)", con);
786    return (jint)rsAdapter2DCreate(con);
787}
788
789// -----------------------------------
790
791static void
792nScriptBindAllocation(JNIEnv *_env, jobject _this, RsContext con, jint script, jint alloc, jint slot)
793{
794    LOG_API("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", con, (RsScript)script, (RsAllocation)alloc, slot);
795    rsScriptBindAllocation(con, (RsScript)script, (RsAllocation)alloc, slot);
796}
797
798static void
799nScriptSetVarI(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jint val)
800{
801    LOG_API("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", con, (void *)script, slot, val);
802    rsScriptSetVarI(con, (RsScript)script, slot, val);
803}
804
805static void
806nScriptSetVarF(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, float val)
807{
808    LOG_API("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", con, (void *)script, slot, val);
809    rsScriptSetVarF(con, (RsScript)script, slot, val);
810}
811
812static void
813nScriptSetVarD(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, double val)
814{
815    LOG_API("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", con, (void *)script, slot, val);
816    rsScriptSetVarD(con, (RsScript)script, slot, val);
817}
818
819static void
820nScriptSetVarV(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jbyteArray data)
821{
822    LOG_API("nScriptSetVarV, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
823    jint len = _env->GetArrayLength(data);
824    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
825    rsScriptSetVarV(con, (RsScript)script, slot, ptr, len);
826    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
827}
828
829
830static void
831nScriptSetTimeZone(JNIEnv *_env, jobject _this, RsContext con, jint script, jbyteArray timeZone)
832{
833    LOG_API("nScriptCSetTimeZone, con(%p), s(%p), timeZone(%s)", con, (void *)script, (const char *)timeZone);
834
835    jint length = _env->GetArrayLength(timeZone);
836    jbyte* timeZone_ptr;
837    timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
838
839    rsScriptSetTimeZone(con, (RsScript)script, (const char *)timeZone_ptr, length);
840
841    if (timeZone_ptr) {
842        _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
843    }
844}
845
846static void
847nScriptInvoke(JNIEnv *_env, jobject _this, RsContext con, jint obj, jint slot)
848{
849    LOG_API("nScriptInvoke, con(%p), script(%p)", con, (void *)obj);
850    rsScriptInvoke(con, (RsScript)obj, slot);
851}
852
853static void
854nScriptInvokeV(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jbyteArray data)
855{
856    LOG_API("nScriptInvokeV, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
857    jint len = _env->GetArrayLength(data);
858    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
859    rsScriptInvokeV(con, (RsScript)script, slot, ptr, len);
860    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
861}
862
863
864// -----------------------------------
865
866static void
867nScriptCBegin(JNIEnv *_env, jobject _this, RsContext con)
868{
869    LOG_API("nScriptCBegin, con(%p)", con);
870    rsScriptCBegin(con);
871}
872
873static void
874nScriptCSetScript(JNIEnv *_env, jobject _this, RsContext con, jbyteArray scriptRef,
875                  jint offset, jint length)
876{
877    LOG_API("!!! nScriptCSetScript, con(%p)", con);
878    jint _exception = 0;
879    jint remaining;
880    jbyte* script_base = 0;
881    jbyte* script_ptr;
882    if (!scriptRef) {
883        _exception = 1;
884        //_env->ThrowNew(IAEClass, "script == null");
885        goto exit;
886    }
887    if (offset < 0) {
888        _exception = 1;
889        //_env->ThrowNew(IAEClass, "offset < 0");
890        goto exit;
891    }
892    if (length < 0) {
893        _exception = 1;
894        //_env->ThrowNew(IAEClass, "length < 0");
895        goto exit;
896    }
897    remaining = _env->GetArrayLength(scriptRef) - offset;
898    if (remaining < length) {
899        _exception = 1;
900        //_env->ThrowNew(IAEClass, "length > script.length - offset");
901        goto exit;
902    }
903    script_base = (jbyte *)
904        _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
905    script_ptr = script_base + offset;
906
907    rsScriptCSetText(con, (const char *)script_ptr, length);
908
909exit:
910    if (script_base) {
911        _env->ReleasePrimitiveArrayCritical(scriptRef, script_base,
912                _exception ? JNI_ABORT: 0);
913    }
914}
915
916static jint
917nScriptCCreate(JNIEnv *_env, jobject _this, RsContext con)
918{
919    LOG_API("nScriptCCreate, con(%p)", con);
920    return (jint)rsScriptCCreate(con);
921}
922
923// ---------------------------------------------------------------------------
924
925static void
926nProgramStoreBegin(JNIEnv *_env, jobject _this, RsContext con, jint in, jint out)
927{
928    LOG_API("nProgramStoreBegin, con(%p), in(%p), out(%p)", con, (RsElement)in, (RsElement)out);
929    rsProgramStoreBegin(con, (RsElement)in, (RsElement)out);
930}
931
932static void
933nProgramStoreDepthFunc(JNIEnv *_env, jobject _this, RsContext con, jint func)
934{
935    LOG_API("nProgramStoreDepthFunc, con(%p), func(%i)", con, func);
936    rsProgramStoreDepthFunc(con, (RsDepthFunc)func);
937}
938
939static void
940nProgramStoreDepthMask(JNIEnv *_env, jobject _this, RsContext con, jboolean enable)
941{
942    LOG_API("nProgramStoreDepthMask, con(%p), enable(%i)", con, enable);
943    rsProgramStoreDepthMask(con, enable);
944}
945
946static void
947nProgramStoreColorMask(JNIEnv *_env, jobject _this, RsContext con, jboolean r, jboolean g, jboolean b, jboolean a)
948{
949    LOG_API("nProgramStoreColorMask, con(%p), r(%i), g(%i), b(%i), a(%i)", con, r, g, b, a);
950    rsProgramStoreColorMask(con, r, g, b, a);
951}
952
953static void
954nProgramStoreBlendFunc(JNIEnv *_env, jobject _this, RsContext con, int src, int dst)
955{
956    LOG_API("nProgramStoreBlendFunc, con(%p), src(%i), dst(%i)", con, src, dst);
957    rsProgramStoreBlendFunc(con, (RsBlendSrcFunc)src, (RsBlendDstFunc)dst);
958}
959
960static void
961nProgramStoreDither(JNIEnv *_env, jobject _this, RsContext con, jboolean enable)
962{
963    LOG_API("nProgramStoreDither, con(%p), enable(%i)", con, enable);
964    rsProgramStoreDither(con, enable);
965}
966
967static jint
968nProgramStoreCreate(JNIEnv *_env, jobject _this, RsContext con)
969{
970    LOG_API("nProgramStoreCreate, con(%p)", con);
971    return (jint)rsProgramStoreCreate(con);
972}
973
974// ---------------------------------------------------------------------------
975
976static void
977nProgramBindConstants(JNIEnv *_env, jobject _this, RsContext con, jint vpv, jint slot, jint a)
978{
979    LOG_API("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", con, (RsProgramVertex)vpv, slot, (RsAllocation)a);
980    rsProgramBindConstants(con, (RsProgram)vpv, slot, (RsAllocation)a);
981}
982
983static void
984nProgramBindTexture(JNIEnv *_env, jobject _this, RsContext con, jint vpf, jint slot, jint a)
985{
986    LOG_API("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
987    rsProgramBindTexture(con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
988}
989
990static void
991nProgramBindSampler(JNIEnv *_env, jobject _this, RsContext con, jint vpf, jint slot, jint a)
992{
993    LOG_API("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsSampler)a);
994    rsProgramBindSampler(con, (RsProgramFragment)vpf, slot, (RsSampler)a);
995}
996
997// ---------------------------------------------------------------------------
998
999static jint
1000nProgramFragmentCreate(JNIEnv *_env, jobject _this, RsContext con, jstring shader, jintArray params)
1001{
1002    const char* shaderUTF = _env->GetStringUTFChars(shader, NULL);
1003    jint shaderLen = _env->GetStringUTFLength(shader);
1004    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1005    jint paramLen = _env->GetArrayLength(params);
1006
1007    LOG_API("nProgramFragmentCreate, con(%p), shaderLen(%i), paramLen(%i)", con, shaderLen, paramLen);
1008
1009    jint ret = (jint)rsProgramFragmentCreate(con, shaderUTF, shaderLen, (uint32_t *)paramPtr, paramLen);
1010    _env->ReleaseStringUTFChars(shader, shaderUTF);
1011    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1012    return ret;
1013}
1014
1015
1016// ---------------------------------------------------------------------------
1017
1018static jint
1019nProgramVertexCreate(JNIEnv *_env, jobject _this, RsContext con, jstring shader, jintArray params)
1020{
1021    const char* shaderUTF = _env->GetStringUTFChars(shader, NULL);
1022    jint shaderLen = _env->GetStringUTFLength(shader);
1023    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1024    jint paramLen = _env->GetArrayLength(params);
1025
1026    LOG_API("nProgramVertexCreate, con(%p), shaderLen(%i), paramLen(%i)", con, shaderLen, paramLen);
1027
1028    jint ret = (jint)rsProgramVertexCreate(con, shaderUTF, shaderLen, (uint32_t *)paramPtr, paramLen);
1029    _env->ReleaseStringUTFChars(shader, shaderUTF);
1030    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1031    return ret;
1032}
1033
1034// ---------------------------------------------------------------------------
1035
1036static jint
1037nProgramRasterCreate(JNIEnv *_env, jobject _this, RsContext con, jboolean pointSmooth, jboolean lineSmooth, jboolean pointSprite)
1038{
1039    LOG_API("nProgramRasterCreate, con(%p), pointSmooth(%i), lineSmooth(%i), pointSprite(%i)",
1040            con, pointSmooth, lineSmooth, pointSprite);
1041    return (jint)rsProgramRasterCreate(con, pointSmooth, lineSmooth, pointSprite);
1042}
1043
1044static void
1045nProgramRasterSetLineWidth(JNIEnv *_env, jobject _this, RsContext con, jint vpr, jfloat v)
1046{
1047    LOG_API("nProgramRasterSetLineWidth, con(%p), vpf(%p), value(%f)", con, (RsProgramRaster)vpr, v);
1048    rsProgramRasterSetLineWidth(con, (RsProgramRaster)vpr, v);
1049}
1050
1051static void
1052nProgramRasterSetCullMode(JNIEnv *_env, jobject _this, RsContext con, jint vpr, jint v)
1053{
1054    LOG_API("nProgramRasterSetCullMode, con(%p), vpf(%p), value(%i)", con, (RsProgramRaster)vpr, v);
1055    rsProgramRasterSetCullMode(con, (RsProgramRaster)vpr, (RsCullMode)v);
1056}
1057
1058
1059// ---------------------------------------------------------------------------
1060
1061static void
1062nContextBindRootScript(JNIEnv *_env, jobject _this, RsContext con, jint script)
1063{
1064    LOG_API("nContextBindRootScript, con(%p), script(%p)", con, (RsScript)script);
1065    rsContextBindRootScript(con, (RsScript)script);
1066}
1067
1068static void
1069nContextBindProgramStore(JNIEnv *_env, jobject _this, RsContext con, jint pfs)
1070{
1071    LOG_API("nContextBindProgramStore, con(%p), pfs(%p)", con, (RsProgramStore)pfs);
1072    rsContextBindProgramStore(con, (RsProgramStore)pfs);
1073}
1074
1075static void
1076nContextBindProgramFragment(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1077{
1078    LOG_API("nContextBindProgramFragment, con(%p), pf(%p)", con, (RsProgramFragment)pf);
1079    rsContextBindProgramFragment(con, (RsProgramFragment)pf);
1080}
1081
1082static void
1083nContextBindProgramVertex(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1084{
1085    LOG_API("nContextBindProgramVertex, con(%p), pf(%p)", con, (RsProgramVertex)pf);
1086    rsContextBindProgramVertex(con, (RsProgramVertex)pf);
1087}
1088
1089static void
1090nContextBindProgramRaster(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1091{
1092    LOG_API("nContextBindProgramRaster, con(%p), pf(%p)", con, (RsProgramRaster)pf);
1093    rsContextBindProgramRaster(con, (RsProgramRaster)pf);
1094}
1095
1096
1097// ---------------------------------------------------------------------------
1098
1099static void
1100nSamplerBegin(JNIEnv *_env, jobject _this, RsContext con)
1101{
1102    LOG_API("nSamplerBegin, con(%p)", con);
1103    rsSamplerBegin(con);
1104}
1105
1106static void
1107nSamplerSet(JNIEnv *_env, jobject _this, RsContext con, jint p, jint v)
1108{
1109    LOG_API("nSamplerSet, con(%p), param(%i), value(%i)", con, p, v);
1110    rsSamplerSet(con, (RsSamplerParam)p, (RsSamplerValue)v);
1111}
1112
1113static void
1114nSamplerSet2(JNIEnv *_env, jobject _this, RsContext con, jint p, jfloat v)
1115{
1116    LOG_API("nSamplerSet2, con(%p), param(%i), value(%f)", con, p, v);
1117    rsSamplerSet2(con, (RsSamplerParam)p, v);
1118}
1119
1120static jint
1121nSamplerCreate(JNIEnv *_env, jobject _this, RsContext con)
1122{
1123    LOG_API("nSamplerCreate, con(%p)", con);
1124    return (jint)rsSamplerCreate(con);
1125}
1126
1127// ---------------------------------------------------------------------------
1128
1129static jint
1130nMeshCreate(JNIEnv *_env, jobject _this, RsContext con, jint vtxCount, jint idxCount)
1131{
1132    LOG_API("nMeshCreate, con(%p), vtxCount(%i), idxCount(%i)", con, vtxCount, idxCount);
1133    int id = (int)rsMeshCreate(con, vtxCount, idxCount);
1134    return id;
1135}
1136
1137static void
1138nMeshBindVertex(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jint alloc, jint slot)
1139{
1140    LOG_API("nMeshBindVertex, con(%p), Mesh(%p), Alloc(%p), slot(%i)", con, (RsMesh)mesh, (RsAllocation)alloc, slot);
1141    rsMeshBindVertex(con, (RsMesh)mesh, (RsAllocation)alloc, slot);
1142}
1143
1144static void
1145nMeshBindIndex(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jint alloc, jint primID, jint slot)
1146{
1147    LOG_API("nMeshBindIndex, con(%p), Mesh(%p), Alloc(%p)", con, (RsMesh)mesh, (RsAllocation)alloc);
1148    rsMeshBindIndex(con, (RsMesh)mesh, (RsAllocation)alloc, primID, slot);
1149}
1150
1151static jint
1152nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, RsContext con, jint mesh)
1153{
1154    LOG_API("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1155    jint vtxCount = 0;
1156    rsMeshGetVertexBufferCount(con, (RsMesh)mesh, &vtxCount);
1157    return vtxCount;
1158}
1159
1160static jint
1161nMeshGetIndexCount(JNIEnv *_env, jobject _this, RsContext con, jint mesh)
1162{
1163    LOG_API("nMeshGetIndexCount, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1164    jint idxCount = 0;
1165    rsMeshGetIndexCount(con, (RsMesh)mesh, &idxCount);
1166    return idxCount;
1167}
1168
1169static void
1170nMeshGetVertices(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jintArray _ids, int numVtxIDs)
1171{
1172    LOG_API("nMeshGetVertices, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1173
1174    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
1175    rsMeshGetVertices(con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
1176
1177    for(jint i = 0; i < numVtxIDs; i ++) {
1178        _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&allocs[i]);
1179    }
1180
1181    free(allocs);
1182}
1183
1184static void
1185nMeshGetIndices(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jintArray _idxIds, jintArray _primitives, int numIndices)
1186{
1187    LOG_API("nMeshGetVertices, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1188
1189    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
1190    uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
1191
1192    rsMeshGetIndices(con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
1193
1194    for(jint i = 0; i < numIndices; i ++) {
1195        _env->SetIntArrayRegion(_idxIds, i, 1, (const jint*)&allocs[i]);
1196        _env->SetIntArrayRegion(_primitives, i, 1, (const jint*)&prims[i]);
1197    }
1198
1199    free(allocs);
1200    free(prims);
1201}
1202
1203// ---------------------------------------------------------------------------
1204
1205
1206static const char *classPathName = "android/renderscript/RenderScript";
1207
1208static JNINativeMethod methods[] = {
1209{"_nInit",                         "()V",                                  (void*)_nInit },
1210{"nInitElements",                  "(IIII)V",                              (void*)nInitElements },
1211
1212{"nDeviceCreate",                  "()I",                                  (void*)nDeviceCreate },
1213{"nDeviceDestroy",                 "(I)V",                                 (void*)nDeviceDestroy },
1214{"nDeviceSetConfig",               "(III)V",                               (void*)nDeviceSetConfig },
1215{"nContextGetMessage",             "(I[IZ)I",                               (void*)nContextGetMessage },
1216{"nContextInitToClient",           "(I)V",                                  (void*)nContextInitToClient },
1217{"nContextDeinitToClient",         "(I)V",                                  (void*)nContextDeinitToClient },
1218
1219
1220// All methods below are thread protected in java.
1221{"rsnContextCreate",                 "(II)I",                                 (void*)nContextCreate },
1222{"rsnContextCreateGL",               "(IIZ)I",                                (void*)nContextCreateGL },
1223{"rsnContextFinish",                 "(I)V",                                  (void*)nContextFinish },
1224{"rsnContextSetPriority",            "(II)V",                                 (void*)nContextSetPriority },
1225{"rsnContextSetSurface",             "(IIILandroid/view/Surface;)V",          (void*)nContextSetSurface },
1226{"rsnContextDestroy",                "(I)V",                                  (void*)nContextDestroy },
1227{"rsnContextDump",                   "(II)V",                                 (void*)nContextDump },
1228{"rsnContextPause",                  "(I)V",                                  (void*)nContextPause },
1229{"rsnContextResume",                 "(I)V",                                  (void*)nContextResume },
1230{"rsnAssignName",                    "(II[B)V",                               (void*)nAssignName },
1231{"rsnGetName",                       "(II)Ljava/lang/String;",                (void*)nGetName },
1232{"rsnObjDestroy",                    "(II)V",                                 (void*)nObjDestroy },
1233
1234{"rsnFileOpen",                      "(I[B)I",                                (void*)nFileOpen },
1235{"rsnFileA3DCreateFromAssetStream",  "(II)I",                                 (void*)nFileA3DCreateFromAssetStream },
1236{"rsnFileA3DGetNumIndexEntries",     "(II)I",                                 (void*)nFileA3DGetNumIndexEntries },
1237{"rsnFileA3DGetIndexEntries",        "(III[I[Ljava/lang/String;)V",           (void*)nFileA3DGetIndexEntries },
1238{"rsnFileA3DGetEntryByIndex",        "(III)I",                                (void*)nFileA3DGetEntryByIndex },
1239
1240{"rsnFontCreateFromFile",            "(ILjava/lang/String;II)I",              (void*)nFontCreateFromFile },
1241
1242{"rsnElementCreate",                 "(IIIZI)I",                              (void*)nElementCreate },
1243{"rsnElementCreate2",                "(I[I[Ljava/lang/String;[I)I",           (void*)nElementCreate2 },
1244{"rsnElementGetNativeData",          "(II[I)V",                               (void*)nElementGetNativeData },
1245{"rsnElementGetSubElements",         "(II[I[Ljava/lang/String;)V",            (void*)nElementGetSubElements },
1246
1247{"rsnTypeBegin",                     "(II)V",                                 (void*)nTypeBegin },
1248{"rsnTypeAdd",                       "(III)V",                                (void*)nTypeAdd },
1249{"rsnTypeCreate",                    "(I)I",                                  (void*)nTypeCreate },
1250{"rsnTypeGetNativeData",             "(II[I)V",                               (void*)nTypeGetNativeData },
1251
1252{"rsnAllocationCreateTyped",         "(II)I",                                 (void*)nAllocationCreateTyped },
1253{"rsnAllocationCreateFromBitmap",    "(IIZLandroid/graphics/Bitmap;)I",       (void*)nAllocationCreateFromBitmap },
1254{"rsnAllocationCreateBitmapRef",     "(IILandroid/graphics/Bitmap;)I",        (void*)nAllocationCreateBitmapRef },
1255{"rsnAllocationCreateFromBitmapBoxed","(IIZLandroid/graphics/Bitmap;)I",      (void*)nAllocationCreateFromBitmapBoxed },
1256{"rsnAllocationCreateFromAssetStream","(IIZI)I",                              (void*)nAllocationCreateFromAssetStream },
1257{"rsnAllocationUploadToTexture",     "(IIZI)V",                               (void*)nAllocationUploadToTexture },
1258{"rsnAllocationUploadToBufferObject","(II)V",                                 (void*)nAllocationUploadToBufferObject },
1259{"rsnAllocationSubData1D",           "(IIII[II)V",                            (void*)nAllocationSubData1D_i },
1260{"rsnAllocationSubData1D",           "(IIII[SI)V",                            (void*)nAllocationSubData1D_s },
1261{"rsnAllocationSubData1D",           "(IIII[BI)V",                            (void*)nAllocationSubData1D_b },
1262{"rsnAllocationSubData1D",           "(IIII[FI)V",                            (void*)nAllocationSubData1D_f },
1263{"rsnAllocationSubElementData1D",    "(IIII[BI)V",                            (void*)nAllocationSubElementData1D },
1264{"rsnAllocationSubData2D",           "(IIIIII[II)V",                          (void*)nAllocationSubData2D_i },
1265{"rsnAllocationSubData2D",           "(IIIIII[FI)V",                          (void*)nAllocationSubData2D_f },
1266{"rsnAllocationRead",                "(II[I)V",                               (void*)nAllocationRead_i },
1267{"rsnAllocationRead",                "(II[F)V",                               (void*)nAllocationRead_f },
1268{"rsnAllocationGetType",             "(II)I",                                 (void*)nAllocationGetType},
1269{"rsnAllocationResize1D",            "(III)V",                                (void*)nAllocationResize1D },
1270{"rsnAllocationResize2D",            "(IIII)V",                               (void*)nAllocationResize2D },
1271
1272{"rsnAdapter1DBindAllocation",       "(III)V",                                (void*)nAdapter1DBindAllocation },
1273{"rsnAdapter1DSetConstraint",        "(IIII)V",                               (void*)nAdapter1DSetConstraint },
1274{"rsnAdapter1DData",                 "(II[I)V",                               (void*)nAdapter1DData_i },
1275{"rsnAdapter1DData",                 "(II[F)V",                               (void*)nAdapter1DData_f },
1276{"rsnAdapter1DSubData",              "(IIII[I)V",                             (void*)nAdapter1DSubData_i },
1277{"rsnAdapter1DSubData",              "(IIII[F)V",                             (void*)nAdapter1DSubData_f },
1278{"rsnAdapter1DCreate",               "(I)I",                                  (void*)nAdapter1DCreate },
1279
1280{"rsnAdapter2DBindAllocation",       "(III)V",                                (void*)nAdapter2DBindAllocation },
1281{"rsnAdapter2DSetConstraint",        "(IIII)V",                               (void*)nAdapter2DSetConstraint },
1282{"rsnAdapter2DData",                 "(II[I)V",                               (void*)nAdapter2DData_i },
1283{"rsnAdapter2DData",                 "(II[F)V",                               (void*)nAdapter2DData_f },
1284{"rsnAdapter2DSubData",              "(IIIIII[I)V",                           (void*)nAdapter2DSubData_i },
1285{"rsnAdapter2DSubData",              "(IIIIII[F)V",                           (void*)nAdapter2DSubData_f },
1286{"rsnAdapter2DCreate",               "(I)I",                                  (void*)nAdapter2DCreate },
1287
1288{"rsnScriptBindAllocation",          "(IIII)V",                               (void*)nScriptBindAllocation },
1289{"rsnScriptSetTimeZone",             "(II[B)V",                               (void*)nScriptSetTimeZone },
1290{"rsnScriptInvoke",                  "(III)V",                                (void*)nScriptInvoke },
1291{"rsnScriptInvokeV",                 "(III[B)V",                              (void*)nScriptInvokeV },
1292{"rsnScriptSetVarI",                 "(IIII)V",                               (void*)nScriptSetVarI },
1293{"rsnScriptSetVarF",                 "(IIIF)V",                               (void*)nScriptSetVarF },
1294{"rsnScriptSetVarD",                 "(IIID)V",                               (void*)nScriptSetVarD },
1295{"rsnScriptSetVarV",                 "(III[B)V",                              (void*)nScriptSetVarV },
1296
1297{"rsnScriptCBegin",                  "(I)V",                                  (void*)nScriptCBegin },
1298{"rsnScriptCSetScript",              "(I[BII)V",                              (void*)nScriptCSetScript },
1299{"rsnScriptCCreate",                 "(I)I",                                  (void*)nScriptCCreate },
1300
1301{"rsnProgramStoreBegin",             "(III)V",                                (void*)nProgramStoreBegin },
1302{"rsnProgramStoreDepthFunc",         "(II)V",                                 (void*)nProgramStoreDepthFunc },
1303{"rsnProgramStoreDepthMask",         "(IZ)V",                                 (void*)nProgramStoreDepthMask },
1304{"rsnProgramStoreColorMask",         "(IZZZZ)V",                              (void*)nProgramStoreColorMask },
1305{"rsnProgramStoreBlendFunc",         "(III)V",                                (void*)nProgramStoreBlendFunc },
1306{"rsnProgramStoreDither",            "(IZ)V",                                 (void*)nProgramStoreDither },
1307{"rsnProgramStoreCreate",            "(I)I",                                  (void*)nProgramStoreCreate },
1308
1309{"rsnProgramBindConstants",          "(IIII)V",                               (void*)nProgramBindConstants },
1310{"rsnProgramBindTexture",            "(IIII)V",                               (void*)nProgramBindTexture },
1311{"rsnProgramBindSampler",            "(IIII)V",                               (void*)nProgramBindSampler },
1312
1313{"rsnProgramFragmentCreate",        "(ILjava/lang/String;[I)I",               (void*)nProgramFragmentCreate },
1314
1315{"rsnProgramRasterCreate",           "(IZZZ)I",                               (void*)nProgramRasterCreate },
1316{"rsnProgramRasterSetLineWidth",     "(IIF)V",                                (void*)nProgramRasterSetLineWidth },
1317{"rsnProgramRasterSetCullMode",      "(III)V",                                (void*)nProgramRasterSetCullMode },
1318
1319{"rsnProgramVertexCreate",          "(ILjava/lang/String;[I)I",               (void*)nProgramVertexCreate },
1320
1321{"rsnContextBindRootScript",         "(II)V",                                 (void*)nContextBindRootScript },
1322{"rsnContextBindProgramStore",       "(II)V",                                 (void*)nContextBindProgramStore },
1323{"rsnContextBindProgramFragment",    "(II)V",                                 (void*)nContextBindProgramFragment },
1324{"rsnContextBindProgramVertex",      "(II)V",                                 (void*)nContextBindProgramVertex },
1325{"rsnContextBindProgramRaster",      "(II)V",                                 (void*)nContextBindProgramRaster },
1326
1327{"rsnSamplerBegin",                  "(I)V",                                  (void*)nSamplerBegin },
1328{"rsnSamplerSet",                    "(III)V",                                (void*)nSamplerSet },
1329{"rsnSamplerSet2",                   "(IIF)V",                                (void*)nSamplerSet2 },
1330{"rsnSamplerCreate",                 "(I)I",                                  (void*)nSamplerCreate },
1331
1332{"rsnMeshCreate",                    "(III)I",                                (void*)nMeshCreate },
1333{"rsnMeshBindVertex",                "(IIII)V",                               (void*)nMeshBindVertex },
1334{"rsnMeshBindIndex",                 "(IIIII)V",                              (void*)nMeshBindIndex },
1335
1336{"rsnMeshGetVertexBufferCount",      "(II)I",                                 (void*)nMeshGetVertexBufferCount },
1337{"rsnMeshGetIndexCount",             "(II)I",                                 (void*)nMeshGetIndexCount },
1338{"rsnMeshGetVertices",               "(II[II)V",                              (void*)nMeshGetVertices },
1339{"rsnMeshGetIndices",                "(II[I[II)V",                            (void*)nMeshGetIndices },
1340
1341};
1342
1343static int registerFuncs(JNIEnv *_env)
1344{
1345    return android::AndroidRuntime::registerNativeMethods(
1346            _env, classPathName, methods, NELEM(methods));
1347}
1348
1349// ---------------------------------------------------------------------------
1350
1351jint JNI_OnLoad(JavaVM* vm, void* reserved)
1352{
1353    JNIEnv* env = NULL;
1354    jint result = -1;
1355
1356    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
1357        LOGE("ERROR: GetEnv failed\n");
1358        goto bail;
1359    }
1360    assert(env != NULL);
1361
1362    if (registerFuncs(env) < 0) {
1363        LOGE("ERROR: MediaPlayer native registration failed\n");
1364        goto bail;
1365    }
1366
1367    /* success -- return valid version number */
1368    result = JNI_VERSION_1_4;
1369
1370bail:
1371    return result;
1372}
1373
1374