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