android_renderscript_RenderScript.cpp revision 331bf9b14b1c5c1e88f5c4092b6e24fae887fb3b
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/AssetManager.h>
36#include <utils/ResourceTypes.h>
37
38#include "jni.h"
39#include "JNIHelp.h"
40#include "android_runtime/AndroidRuntime.h"
41#include "android_runtime/android_view_Surface.h"
42#include "android_runtime/android_util_AssetManager.h"
43
44#include <RenderScript.h>
45#include <RenderScriptEnv.h>
46
47//#define LOG_API LOGE
48#define LOG_API(...)
49
50using namespace android;
51
52class AutoJavaStringToUTF8 {
53public:
54    AutoJavaStringToUTF8(JNIEnv* env, jstring str) : fEnv(env), fJStr(str)
55    {
56        fCStr = env->GetStringUTFChars(str, NULL);
57        fLength = env->GetStringUTFLength(str);
58    }
59    ~AutoJavaStringToUTF8()
60    {
61        fEnv->ReleaseStringUTFChars(fJStr, fCStr);
62    }
63    const char* c_str() const { return fCStr; }
64    jsize length() const { return fLength; }
65
66private:
67    JNIEnv*     fEnv;
68    jstring     fJStr;
69    const char* fCStr;
70    jsize       fLength;
71};
72
73// ---------------------------------------------------------------------------
74
75static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
76{
77    jclass npeClazz = env->FindClass(exc);
78    env->ThrowNew(npeClazz, msg);
79}
80
81static jfieldID gContextId = 0;
82static jfieldID gNativeBitmapID = 0;
83static jfieldID gTypeNativeCache = 0;
84
85static void _nInit(JNIEnv *_env, jclass _this)
86{
87    gContextId             = _env->GetFieldID(_this, "mContext", "I");
88
89    jclass bitmapClass = _env->FindClass("android/graphics/Bitmap");
90    gNativeBitmapID = _env->GetFieldID(bitmapClass, "mNativeBitmap", "I");
91}
92
93// ---------------------------------------------------------------------------
94
95static void
96nContextFinish(JNIEnv *_env, jobject _this, RsContext con)
97{
98    LOG_API("nContextFinish, con(%p)", con);
99    rsContextFinish(con);
100}
101
102static void
103nAssignName(JNIEnv *_env, jobject _this, RsContext con, jint obj, jbyteArray str)
104{
105    LOG_API("nAssignName, con(%p), obj(%p)", con, (void *)obj);
106    jint len = _env->GetArrayLength(str);
107    jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
108    rsAssignName(con, (void *)obj, (const char *)cptr, len);
109    _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
110}
111
112static jstring
113nGetName(JNIEnv *_env, jobject _this, RsContext con, jint obj)
114{
115    LOG_API("nGetName, con(%p), obj(%p)", con, (void *)obj);
116    const char *name = NULL;
117    rsaGetName(con, (void *)obj, &name);
118    if(name == NULL || strlen(name) == 0) {
119        return NULL;
120    }
121    return _env->NewStringUTF(name);
122}
123
124static void
125nObjDestroy(JNIEnv *_env, jobject _this, RsContext con, jint obj)
126{
127    LOG_API("nObjDestroy, con(%p) obj(%p)", con, (void *)obj);
128    rsObjDestroy(con, (void *)obj);
129}
130
131// ---------------------------------------------------------------------------
132
133static jint
134nDeviceCreate(JNIEnv *_env, jobject _this)
135{
136    LOG_API("nDeviceCreate");
137    return (jint)rsDeviceCreate();
138}
139
140static void
141nDeviceDestroy(JNIEnv *_env, jobject _this, jint dev)
142{
143    LOG_API("nDeviceDestroy");
144    return rsDeviceDestroy((RsDevice)dev);
145}
146
147static void
148nDeviceSetConfig(JNIEnv *_env, jobject _this, jint dev, jint p, jint value)
149{
150    LOG_API("nDeviceSetConfig  dev(%p), param(%i), value(%i)", (void *)dev, p, value);
151    return rsDeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value);
152}
153
154static jint
155nContextCreate(JNIEnv *_env, jobject _this, jint dev, jint ver)
156{
157    LOG_API("nContextCreate");
158    return (jint)rsContextCreate((RsDevice)dev, ver);
159}
160
161static jint
162nContextCreateGL(JNIEnv *_env, jobject _this, jint dev, jint ver,
163                 int colorMin, int colorPref,
164                 int alphaMin, int alphaPref,
165                 int depthMin, int depthPref,
166                 int stencilMin, int stencilPref,
167                 int samplesMin, int samplesPref, float samplesQ,
168                 int dpi)
169{
170    RsSurfaceConfig sc;
171    sc.alphaMin = alphaMin;
172    sc.alphaPref = alphaPref;
173    sc.colorMin = colorMin;
174    sc.colorPref = colorPref;
175    sc.depthMin = depthMin;
176    sc.depthPref = depthPref;
177    sc.samplesMin = samplesMin;
178    sc.samplesPref = samplesPref;
179    sc.samplesQ = samplesQ;
180
181    LOG_API("nContextCreateGL");
182    return (jint)rsContextCreateGL((RsDevice)dev, ver, sc, dpi);
183}
184
185static void
186nContextSetPriority(JNIEnv *_env, jobject _this, RsContext con, jint p)
187{
188    LOG_API("ContextSetPriority, con(%p), priority(%i)", con, p);
189    rsContextSetPriority(con, p);
190}
191
192
193
194static void
195nContextSetSurface(JNIEnv *_env, jobject _this, RsContext con, jint width, jint height, jobject wnd)
196{
197    LOG_API("nContextSetSurface, con(%p), width(%i), height(%i), surface(%p)", con, width, height, (Surface *)wnd);
198
199    Surface * window = NULL;
200    if (wnd == NULL) {
201
202    } else {
203        window = (Surface*) android_Surface_getNativeWindow(_env, wnd).get();
204    }
205
206    rsContextSetSurface(con, width, height, window);
207}
208
209static void
210nContextDestroy(JNIEnv *_env, jobject _this, RsContext con)
211{
212    LOG_API("nContextDestroy, con(%p)", con);
213    rsContextDestroy(con);
214}
215
216static void
217nContextDump(JNIEnv *_env, jobject _this, RsContext con, jint bits)
218{
219    LOG_API("nContextDump, con(%p)  bits(%i)", (RsContext)con, bits);
220    rsContextDump((RsContext)con, bits);
221}
222
223static void
224nContextPause(JNIEnv *_env, jobject _this, RsContext con)
225{
226    LOG_API("nContextPause, con(%p)", con);
227    rsContextPause(con);
228}
229
230static void
231nContextResume(JNIEnv *_env, jobject _this, RsContext con)
232{
233    LOG_API("nContextResume, con(%p)", con);
234    rsContextResume(con);
235}
236
237
238static jstring
239nContextGetErrorMessage(JNIEnv *_env, jobject _this, RsContext con)
240{
241    LOG_API("nContextGetErrorMessage, con(%p)", con);
242    char buf[1024];
243
244    size_t receiveLen;
245    uint32_t subID;
246    int id = rsContextGetMessage(con, buf, &receiveLen, &subID, sizeof(buf), true);
247    if (!id && receiveLen) {
248        LOGV("message receive buffer too small.  %i", receiveLen);
249    }
250    return _env->NewStringUTF(buf);
251}
252
253static void
254nContextGetUserMessage(JNIEnv *_env, jobject _this, RsContext con, jintArray data)
255{
256    jint len = _env->GetArrayLength(data);
257    LOG_API("nContextGetMessage, con(%p), len(%i)", con, len);
258    jint *ptr = _env->GetIntArrayElements(data, NULL);
259    size_t receiveLen;
260    uint32_t subID;
261    int id = rsContextGetMessage(con, ptr, &receiveLen, &subID, len * 4, true);
262    if (!id && receiveLen) {
263        LOGV("message receive buffer too small.  %i", receiveLen);
264    }
265    _env->ReleaseIntArrayElements(data, ptr, 0);
266}
267
268static jint
269nContextPeekMessage(JNIEnv *_env, jobject _this, RsContext con, jintArray auxData, jboolean wait)
270{
271    LOG_API("nContextPeekMessage, con(%p)", con);
272    jint *auxDataPtr = _env->GetIntArrayElements(auxData, NULL);
273    size_t receiveLen;
274    uint32_t subID;
275    int id = rsContextPeekMessage(con, &receiveLen, &subID, wait);
276    auxDataPtr[0] = (jint)subID;
277    auxDataPtr[1] = (jint)receiveLen;
278    _env->ReleaseIntArrayElements(auxData, auxDataPtr, 0);
279    return id;
280}
281
282static void nContextInitToClient(JNIEnv *_env, jobject _this, RsContext con)
283{
284    LOG_API("nContextInitToClient, con(%p)", con);
285    rsContextInitToClient(con);
286}
287
288static void nContextDeinitToClient(JNIEnv *_env, jobject _this, RsContext con)
289{
290    LOG_API("nContextDeinitToClient, con(%p)", con);
291    rsContextDeinitToClient(con);
292}
293
294
295static jint
296nElementCreate(JNIEnv *_env, jobject _this, RsContext con, jint type, jint kind, jboolean norm, jint size)
297{
298    LOG_API("nElementCreate, con(%p), type(%i), kind(%i), norm(%i), size(%i)", con, type, kind, norm, size);
299    return (jint)rsElementCreate(con, (RsDataType)type, (RsDataKind)kind, norm, size);
300}
301
302static jint
303nElementCreate2(JNIEnv *_env, jobject _this, RsContext con, jintArray _ids, jobjectArray _names, jintArray _arraySizes)
304{
305    int fieldCount = _env->GetArrayLength(_ids);
306    LOG_API("nElementCreate2, con(%p)", con);
307
308    jint *ids = _env->GetIntArrayElements(_ids, NULL);
309    jint *arraySizes = _env->GetIntArrayElements(_arraySizes, NULL);
310    const char ** nameArray = (const char **)calloc(fieldCount, sizeof(char *));
311    size_t* sizeArray = (size_t*)calloc(fieldCount, sizeof(size_t));
312
313    for (int ct=0; ct < fieldCount; ct++) {
314        jstring s = (jstring)_env->GetObjectArrayElement(_names, ct);
315        nameArray[ct] = _env->GetStringUTFChars(s, NULL);
316        sizeArray[ct] = _env->GetStringUTFLength(s);
317    }
318    jint id = (jint)rsElementCreate2(con, fieldCount, (RsElement *)ids, nameArray, sizeArray, (const uint32_t *)arraySizes);
319    for (int ct=0; ct < fieldCount; ct++) {
320        jstring s = (jstring)_env->GetObjectArrayElement(_names, ct);
321        _env->ReleaseStringUTFChars(s, nameArray[ct]);
322    }
323    _env->ReleaseIntArrayElements(_ids, ids, JNI_ABORT);
324    _env->ReleaseIntArrayElements(_arraySizes, arraySizes, JNI_ABORT);
325    free(nameArray);
326    free(sizeArray);
327    return (jint)id;
328}
329
330static void
331nElementGetNativeData(JNIEnv *_env, jobject _this, RsContext con, jint id, jintArray _elementData)
332{
333    int dataSize = _env->GetArrayLength(_elementData);
334    LOG_API("nElementGetNativeData, con(%p)", con);
335
336    // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
337    assert(dataSize == 5);
338
339    uint32_t elementData[5];
340    rsaElementGetNativeData(con, (RsElement)id, elementData, dataSize);
341
342    for(jint i = 0; i < dataSize; i ++) {
343        _env->SetIntArrayRegion(_elementData, i, 1, (const jint*)&elementData[i]);
344    }
345}
346
347
348static void
349nElementGetSubElements(JNIEnv *_env, jobject _this, RsContext con, jint id, jintArray _IDs, jobjectArray _names)
350{
351    int dataSize = _env->GetArrayLength(_IDs);
352    LOG_API("nElementGetSubElements, con(%p)", con);
353
354    uint32_t *ids = (uint32_t *)malloc((uint32_t)dataSize * sizeof(uint32_t));
355    const char **names = (const char **)malloc((uint32_t)dataSize * sizeof(const char *));
356
357    rsaElementGetSubElements(con, (RsElement)id, ids, names, (uint32_t)dataSize);
358
359    for(jint i = 0; i < dataSize; i++) {
360        _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
361        _env->SetIntArrayRegion(_IDs, i, 1, (const jint*)&ids[i]);
362    }
363
364    free(ids);
365    free(names);
366}
367
368// -----------------------------------
369
370static int
371nTypeCreate(JNIEnv *_env, jobject _this, RsContext con, RsElement eid,
372            jint dimx, jint dimy, jint dimz, jboolean mips, jboolean faces)
373{
374    LOG_API("nTypeCreate, con(%p) eid(%p), x(%i), y(%i), z(%i), mips(%i), faces(%i)",
375            con, eid, dimx, dimy, dimz, mips, faces);
376
377    jint id = (jint)rsaTypeCreate(con, (RsElement)eid, dimx, dimy, dimz, mips, faces);
378    return (jint)id;
379}
380
381static void
382nTypeGetNativeData(JNIEnv *_env, jobject _this, RsContext con, jint id, jintArray _typeData)
383{
384    // We are packing 6 items: mDimX; mDimY; mDimZ;
385    // mDimLOD; mDimFaces; mElement; into typeData
386    int elementCount = _env->GetArrayLength(_typeData);
387
388    assert(elementCount == 6);
389    LOG_API("nTypeCreate, con(%p)", con);
390
391    uint32_t typeData[6];
392    rsaTypeGetNativeData(con, (RsType)id, typeData, 6);
393
394    for(jint i = 0; i < elementCount; i ++) {
395        _env->SetIntArrayRegion(_typeData, i, 1, (const jint*)&typeData[i]);
396    }
397}
398
399// -----------------------------------
400
401static jint
402nAllocationCreateTyped(JNIEnv *_env, jobject _this, RsContext con, jint type, jint mips, jint usage)
403{
404    LOG_API("nAllocationCreateTyped, con(%p), type(%p), mip(%i), usage(%i)", con, (RsElement)type, mips, usage);
405    return (jint) rsaAllocationCreateTyped(con, (RsType)type, (RsAllocationMipmapControl)mips, (uint32_t)usage);
406}
407
408static void
409nAllocationSyncAll(JNIEnv *_env, jobject _this, RsContext con, jint a, jint bits)
410{
411    LOG_API("nAllocationSyncAll, con(%p), a(%p), bits(0x%08x)", con, (RsAllocation)a, bits);
412    rsAllocationSyncAll(con, (RsAllocation)a, (RsAllocationUsageType)bits);
413}
414
415static void
416nAllocationGenerateMipmaps(JNIEnv *_env, jobject _this, RsContext con, jint alloc)
417{
418    LOG_API("nAllocationGenerateMipmaps, con(%p), a(%p)", con, (RsAllocation)alloc);
419    rsAllocationGenerateMipmaps(con, (RsAllocation)alloc);
420}
421
422static int
423nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, RsContext con, jint type, jint mip, jobject jbitmap, jint usage)
424{
425    SkBitmap const * nativeBitmap =
426            (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID);
427    const SkBitmap& bitmap(*nativeBitmap);
428
429    bitmap.lockPixels();
430    const void* ptr = bitmap.getPixels();
431    jint id = (jint)rsaAllocationCreateFromBitmap(con, (RsType)type, (RsAllocationMipmapControl)mip, ptr, usage);
432    bitmap.unlockPixels();
433    return id;
434}
435
436static int
437nAllocationCubeCreateFromBitmap(JNIEnv *_env, jobject _this, RsContext con, jint type, jint mip, jobject jbitmap, jint usage)
438{
439    SkBitmap const * nativeBitmap =
440            (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID);
441    const SkBitmap& bitmap(*nativeBitmap);
442
443    bitmap.lockPixels();
444    const void* ptr = bitmap.getPixels();
445    jint id = (jint)rsaAllocationCubeCreateFromBitmap(con, (RsType)type, (RsAllocationMipmapControl)mip, ptr, usage);
446    bitmap.unlockPixels();
447    return id;
448}
449
450static void
451nAllocationCopyFromBitmap(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jobject jbitmap)
452{
453    SkBitmap const * nativeBitmap =
454            (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID);
455    const SkBitmap& bitmap(*nativeBitmap);
456    int w = bitmap.width();
457    int h = bitmap.height();
458
459    bitmap.lockPixels();
460    const void* ptr = bitmap.getPixels();
461    rsAllocation2DData(con, (RsAllocation)alloc, 0, 0,
462                       0, RS_ALLOCATION_CUBMAP_FACE_POSITVE_X,
463                       w, h, ptr, bitmap.getSize());
464    bitmap.unlockPixels();
465}
466
467static void
468nAllocationCopyToBitmap(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jobject jbitmap)
469{
470    SkBitmap const * nativeBitmap =
471            (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID);
472    const SkBitmap& bitmap(*nativeBitmap);
473
474    bitmap.lockPixels();
475    void* ptr = bitmap.getPixels();
476    rsAllocationCopyToBitmap(con, (RsAllocation)alloc, ptr, bitmap.getSize());
477    bitmap.unlockPixels();
478}
479
480static void ReleaseBitmapCallback(void *bmp)
481{
482    SkBitmap const * nativeBitmap = (SkBitmap const *)bmp;
483    nativeBitmap->unlockPixels();
484}
485
486
487static void
488nAllocationData1D_i(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint lod, jint count, jintArray data, int sizeBytes)
489{
490    jint len = _env->GetArrayLength(data);
491    LOG_API("nAllocation1DData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
492    jint *ptr = _env->GetIntArrayElements(data, NULL);
493    rsAllocation1DData(con, (RsAllocation)alloc, offset, lod, count, ptr, sizeBytes);
494    _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
495}
496
497static void
498nAllocationData1D_s(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint lod, jint count, jshortArray data, int sizeBytes)
499{
500    jint len = _env->GetArrayLength(data);
501    LOG_API("nAllocation1DData_s, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
502    jshort *ptr = _env->GetShortArrayElements(data, NULL);
503    rsAllocation1DData(con, (RsAllocation)alloc, offset, lod, count, ptr, sizeBytes);
504    _env->ReleaseShortArrayElements(data, ptr, JNI_ABORT);
505}
506
507static void
508nAllocationData1D_b(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint lod, jint count, jbyteArray data, int sizeBytes)
509{
510    jint len = _env->GetArrayLength(data);
511    LOG_API("nAllocation1DData_b, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
512    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
513    rsAllocation1DData(con, (RsAllocation)alloc, offset, lod, count, ptr, sizeBytes);
514    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
515}
516
517static void
518nAllocationData1D_f(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint lod, jint count, jfloatArray data, int sizeBytes)
519{
520    jint len = _env->GetArrayLength(data);
521    LOG_API("nAllocation1DData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
522    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
523    rsAllocation1DData(con, (RsAllocation)alloc, offset, lod, count, ptr, sizeBytes);
524    _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
525}
526
527static void
528//    native void rsnAllocationElementData1D(int con, int id, int xoff, int compIdx, byte[] d, int sizeBytes);
529nAllocationElementData1D(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint lod, jint compIdx, jbyteArray data, int sizeBytes)
530{
531    jint len = _env->GetArrayLength(data);
532    LOG_API("nAllocationElementData1D, con(%p), alloc(%p), offset(%i), comp(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, compIdx, len, sizeBytes);
533    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
534    rsAllocation1DElementData(con, (RsAllocation)alloc, offset, lod, ptr, compIdx, sizeBytes);
535    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
536}
537
538static void
539nAllocationData2D_s(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint lod, jint face,
540                    jint w, jint h, jshortArray data, int sizeBytes)
541{
542    jint len = _env->GetArrayLength(data);
543    LOG_API("nAllocation2DData_s, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)", con, (RsAllocation)alloc, xoff, yoff, w, h, len);
544    jshort *ptr = _env->GetShortArrayElements(data, NULL);
545    rsAllocation2DData(con, (RsAllocation)alloc, xoff, yoff, lod, (RsAllocationCubemapFace)face, w, h, ptr, sizeBytes);
546    _env->ReleaseShortArrayElements(data, ptr, JNI_ABORT);
547}
548
549static void
550nAllocationData2D_b(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint lod, jint face,
551                    jint w, jint h, jbyteArray data, int sizeBytes)
552{
553    jint len = _env->GetArrayLength(data);
554    LOG_API("nAllocation2DData_b, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)", con, (RsAllocation)alloc, xoff, yoff, w, h, len);
555    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
556    rsAllocation2DData(con, (RsAllocation)alloc, xoff, yoff, lod, (RsAllocationCubemapFace)face, w, h, ptr, sizeBytes);
557    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
558}
559
560static void
561nAllocationData2D_i(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint lod, jint face,
562                    jint w, jint h, jintArray data, int sizeBytes)
563{
564    jint len = _env->GetArrayLength(data);
565    LOG_API("nAllocation2DData_i, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)", con, (RsAllocation)alloc, xoff, yoff, w, h, len);
566    jint *ptr = _env->GetIntArrayElements(data, NULL);
567    rsAllocation2DData(con, (RsAllocation)alloc, xoff, yoff, lod, (RsAllocationCubemapFace)face, w, h, ptr, sizeBytes);
568    _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
569}
570
571static void
572nAllocationData2D_f(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint lod, jint face,
573                    jint w, jint h, jfloatArray data, int sizeBytes)
574{
575    jint len = _env->GetArrayLength(data);
576    LOG_API("nAllocation2DData_i, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)", con, (RsAllocation)alloc, xoff, yoff, w, h, len);
577    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
578    rsAllocation2DData(con, (RsAllocation)alloc, xoff, yoff, lod, (RsAllocationCubemapFace)face, w, h, ptr, sizeBytes);
579    _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
580}
581
582static void
583nAllocationRead_i(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jintArray data)
584{
585    jint len = _env->GetArrayLength(data);
586    LOG_API("nAllocationRead_i, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
587    jint *ptr = _env->GetIntArrayElements(data, NULL);
588    rsAllocationRead(con, (RsAllocation)alloc, ptr);
589    _env->ReleaseIntArrayElements(data, ptr, 0);
590}
591
592static void
593nAllocationRead_s(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jshortArray data)
594{
595    jint len = _env->GetArrayLength(data);
596    LOG_API("nAllocationRead_i, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
597    jshort *ptr = _env->GetShortArrayElements(data, NULL);
598    rsAllocationRead(con, (RsAllocation)alloc, ptr);
599    _env->ReleaseShortArrayElements(data, ptr, 0);
600}
601
602static void
603nAllocationRead_b(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jbyteArray data)
604{
605    jint len = _env->GetArrayLength(data);
606    LOG_API("nAllocationRead_i, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
607    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
608    rsAllocationRead(con, (RsAllocation)alloc, ptr);
609    _env->ReleaseByteArrayElements(data, ptr, 0);
610}
611
612static void
613nAllocationRead_f(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jfloatArray data)
614{
615    jint len = _env->GetArrayLength(data);
616    LOG_API("nAllocationRead_f, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
617    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
618    rsAllocationRead(con, (RsAllocation)alloc, ptr);
619    _env->ReleaseFloatArrayElements(data, ptr, 0);
620}
621
622static jint
623nAllocationGetType(JNIEnv *_env, jobject _this, RsContext con, jint a)
624{
625    LOG_API("nAllocationGetType, con(%p), a(%p)", con, (RsAllocation)a);
626    return (jint) rsaAllocationGetType(con, (RsAllocation)a);
627}
628
629static void
630nAllocationResize1D(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint dimX)
631{
632    LOG_API("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", con, (RsAllocation)alloc, dimX);
633    rsAllocationResize1D(con, (RsAllocation)alloc, dimX);
634}
635
636static void
637nAllocationResize2D(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint dimX, jint dimY)
638{
639    LOG_API("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i), sizeY(%i)", con, (RsAllocation)alloc, dimX, dimY);
640    rsAllocationResize2D(con, (RsAllocation)alloc, dimX, dimY);
641}
642
643// -----------------------------------
644
645static int
646nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, RsContext con, jint native_asset)
647{
648    LOGV("______nFileA3D %u", (uint32_t) native_asset);
649
650    Asset* asset = reinterpret_cast<Asset*>(native_asset);
651
652    jint id = (jint)rsaFileA3DCreateFromMemory(con, asset->getBuffer(false), asset->getLength());
653    return id;
654}
655
656static int
657nFileA3DCreateFromAsset(JNIEnv *_env, jobject _this, RsContext con, jobject _assetMgr, jstring _path)
658{
659    AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
660    if (mgr == NULL) {
661        return 0;
662    }
663
664    AutoJavaStringToUTF8 str(_env, _path);
665    Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
666    if (asset == NULL) {
667        return 0;
668    }
669
670    jint id = (jint)rsaFileA3DCreateFromAsset(con, asset);
671    return id;
672}
673
674static int
675nFileA3DCreateFromFile(JNIEnv *_env, jobject _this, RsContext con, jstring fileName)
676{
677    AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
678    jint id = (jint)rsaFileA3DCreateFromFile(con, fileNameUTF.c_str());
679
680    return id;
681}
682
683static int
684nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, RsContext con, jint fileA3D)
685{
686    int32_t numEntries = 0;
687    rsaFileA3DGetNumIndexEntries(con, &numEntries, (RsFile)fileA3D);
688    return numEntries;
689}
690
691static void
692nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, RsContext con, jint fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
693{
694    LOGV("______nFileA3D %u", (uint32_t) fileA3D);
695    RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
696
697    rsaFileA3DGetIndexEntries(con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
698
699    for(jint i = 0; i < numEntries; i ++) {
700        _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
701        _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
702    }
703
704    free(fileEntries);
705}
706
707static int
708nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, RsContext con, jint fileA3D, jint index)
709{
710    LOGV("______nFileA3D %u", (uint32_t) fileA3D);
711    jint id = (jint)rsaFileA3DGetEntryByIndex(con, (uint32_t)index, (RsFile)fileA3D);
712    return id;
713}
714
715// -----------------------------------
716
717static int
718nFontCreateFromFile(JNIEnv *_env, jobject _this, RsContext con,
719                    jstring fileName, jfloat fontSize, jint dpi)
720{
721    AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
722    jint id = (jint)rsFontCreateFromFile(con, fileNameUTF.c_str(), fontSize, dpi);
723
724    return id;
725}
726
727static int
728nFontCreateFromAssetStream(JNIEnv *_env, jobject _this, RsContext con,
729                           jstring name, jfloat fontSize, jint dpi, jint native_asset)
730{
731    Asset* asset = reinterpret_cast<Asset*>(native_asset);
732    AutoJavaStringToUTF8 nameUTF(_env, name);
733
734    jint id = (jint)rsFontCreateFromMemory(con, nameUTF.c_str(), fontSize, dpi,
735                                           asset->getBuffer(false), asset->getLength());
736    return id;
737}
738
739static int
740nFontCreateFromAsset(JNIEnv *_env, jobject _this, RsContext con, jobject _assetMgr, jstring _path,
741                     jfloat fontSize, jint dpi)
742{
743    AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
744    if (mgr == NULL) {
745        return 0;
746    }
747
748    AutoJavaStringToUTF8 str(_env, _path);
749    Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
750    if (asset == NULL) {
751        return 0;
752    }
753
754    jint id = (jint)rsFontCreateFromMemory(con, str.c_str(), fontSize, dpi,
755                                           asset->getBuffer(false), asset->getLength());
756    delete asset;
757    return id;
758}
759
760// -----------------------------------
761
762static void
763nScriptBindAllocation(JNIEnv *_env, jobject _this, RsContext con, jint script, jint alloc, jint slot)
764{
765    LOG_API("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", con, (RsScript)script, (RsAllocation)alloc, slot);
766    rsScriptBindAllocation(con, (RsScript)script, (RsAllocation)alloc, slot);
767}
768
769static void
770nScriptSetVarI(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jint val)
771{
772    LOG_API("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", con, (void *)script, slot, val);
773    rsScriptSetVarI(con, (RsScript)script, slot, val);
774}
775
776static void
777nScriptSetVarObj(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jint val)
778{
779    LOG_API("nScriptSetVarObj, con(%p), s(%p), slot(%i), val(%i)", con, (void *)script, slot, val);
780    rsScriptSetVarObj(con, (RsScript)script, slot, (RsObjectBase)val);
781}
782
783static void
784nScriptSetVarJ(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jlong val)
785{
786    LOG_API("nScriptSetVarJ, con(%p), s(%p), slot(%i), val(%lli)", con, (void *)script, slot, val);
787    rsScriptSetVarJ(con, (RsScript)script, slot, val);
788}
789
790static void
791nScriptSetVarF(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, float val)
792{
793    LOG_API("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", con, (void *)script, slot, val);
794    rsScriptSetVarF(con, (RsScript)script, slot, val);
795}
796
797static void
798nScriptSetVarD(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, double val)
799{
800    LOG_API("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", con, (void *)script, slot, val);
801    rsScriptSetVarD(con, (RsScript)script, slot, val);
802}
803
804static void
805nScriptSetVarV(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jbyteArray data)
806{
807    LOG_API("nScriptSetVarV, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
808    jint len = _env->GetArrayLength(data);
809    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
810    rsScriptSetVarV(con, (RsScript)script, slot, ptr, len);
811    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
812}
813
814
815static void
816nScriptSetTimeZone(JNIEnv *_env, jobject _this, RsContext con, jint script, jbyteArray timeZone)
817{
818    LOG_API("nScriptCSetTimeZone, con(%p), s(%p), timeZone(%s)", con, (void *)script, (const char *)timeZone);
819
820    jint length = _env->GetArrayLength(timeZone);
821    jbyte* timeZone_ptr;
822    timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
823
824    rsScriptSetTimeZone(con, (RsScript)script, (const char *)timeZone_ptr, length);
825
826    if (timeZone_ptr) {
827        _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
828    }
829}
830
831static void
832nScriptInvoke(JNIEnv *_env, jobject _this, RsContext con, jint obj, jint slot)
833{
834    LOG_API("nScriptInvoke, con(%p), script(%p)", con, (void *)obj);
835    rsScriptInvoke(con, (RsScript)obj, slot);
836}
837
838static void
839nScriptInvokeV(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jbyteArray data)
840{
841    LOG_API("nScriptInvokeV, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
842    jint len = _env->GetArrayLength(data);
843    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
844    rsScriptInvokeV(con, (RsScript)script, slot, ptr, len);
845    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
846}
847
848
849// -----------------------------------
850
851static jint
852nScriptCCreate(JNIEnv *_env, jobject _this, RsContext con,
853               jstring resName, jstring cacheDir,
854               jbyteArray scriptRef, jint length)
855{
856    LOG_API("nScriptCCreate, con(%p)", con);
857
858    AutoJavaStringToUTF8 resNameUTF(_env, resName);
859    AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
860    jint ret = 0;
861
862    jint _exception = 0;
863    jint remaining;
864    jbyte* script_ptr;
865    if (!scriptRef) {
866        _exception = 1;
867        //_env->ThrowNew(IAEClass, "script == null");
868        goto exit;
869    }
870    if (length < 0) {
871        _exception = 1;
872        //_env->ThrowNew(IAEClass, "length < 0");
873        goto exit;
874    }
875    remaining = _env->GetArrayLength(scriptRef);
876    if (remaining < length) {
877        _exception = 1;
878        //_env->ThrowNew(IAEClass, "length > script.length - offset");
879        goto exit;
880    }
881    script_ptr = (jbyte *)
882        _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
883
884    //rsScriptCSetText(con, (const char *)script_ptr, length);
885
886    ret = (jint)rsScriptCCreate(con, resNameUTF.c_str(), cacheDirUTF.c_str(),
887                                (const char *)script_ptr, length);
888
889exit:
890    if (script_ptr) {
891        _env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr,
892                _exception ? JNI_ABORT: 0);
893    }
894
895    return ret;
896}
897
898// ---------------------------------------------------------------------------
899
900static jint
901nProgramStoreCreate(JNIEnv *_env, jobject _this, RsContext con,
902                    jboolean colorMaskR, jboolean colorMaskG, jboolean colorMaskB, jboolean colorMaskA,
903                    jboolean depthMask, jboolean ditherEnable,
904                    jint srcFunc, jint destFunc,
905                    jint depthFunc)
906{
907    LOG_API("nProgramStoreCreate, con(%p)", con);
908    return (jint)rsProgramStoreCreate(con, colorMaskR, colorMaskG, colorMaskB, colorMaskA,
909                                      depthMask, ditherEnable, (RsBlendSrcFunc)srcFunc,
910                                      (RsBlendDstFunc)destFunc, (RsDepthFunc)depthFunc);
911}
912
913// ---------------------------------------------------------------------------
914
915static void
916nProgramBindConstants(JNIEnv *_env, jobject _this, RsContext con, jint vpv, jint slot, jint a)
917{
918    LOG_API("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", con, (RsProgramVertex)vpv, slot, (RsAllocation)a);
919    rsProgramBindConstants(con, (RsProgram)vpv, slot, (RsAllocation)a);
920}
921
922static void
923nProgramBindTexture(JNIEnv *_env, jobject _this, RsContext con, jint vpf, jint slot, jint a)
924{
925    LOG_API("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
926    rsProgramBindTexture(con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
927}
928
929static void
930nProgramBindSampler(JNIEnv *_env, jobject _this, RsContext con, jint vpf, jint slot, jint a)
931{
932    LOG_API("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsSampler)a);
933    rsProgramBindSampler(con, (RsProgramFragment)vpf, slot, (RsSampler)a);
934}
935
936// ---------------------------------------------------------------------------
937
938static jint
939nProgramFragmentCreate(JNIEnv *_env, jobject _this, RsContext con, jstring shader, jintArray params)
940{
941    AutoJavaStringToUTF8 shaderUTF(_env, shader);
942    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
943    jint paramLen = _env->GetArrayLength(params);
944
945    LOG_API("nProgramFragmentCreate, con(%p), paramLen(%i)", con, paramLen);
946
947    jint ret = (jint)rsProgramFragmentCreate(con, shaderUTF.c_str(), shaderUTF.length(), (uint32_t *)paramPtr, paramLen);
948    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
949    return ret;
950}
951
952
953// ---------------------------------------------------------------------------
954
955static jint
956nProgramVertexCreate(JNIEnv *_env, jobject _this, RsContext con, jstring shader, jintArray params)
957{
958    AutoJavaStringToUTF8 shaderUTF(_env, shader);
959    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
960    jint paramLen = _env->GetArrayLength(params);
961
962    LOG_API("nProgramVertexCreate, con(%p), paramLen(%i)", con, paramLen);
963
964    jint ret = (jint)rsProgramVertexCreate(con, shaderUTF.c_str(), shaderUTF.length(), (uint32_t *)paramPtr, paramLen);
965    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
966    return ret;
967}
968
969// ---------------------------------------------------------------------------
970
971static jint
972nProgramRasterCreate(JNIEnv *_env, jobject _this, RsContext con, jboolean pointSmooth,
973                     jboolean lineSmooth, jboolean pointSprite, jfloat lineWidth, jint cull)
974{
975    LOG_API("nProgramRasterCreate, con(%p), pointSmooth(%i), lineSmooth(%i), pointSprite(%i)",
976            con, pointSmooth, lineSmooth, pointSprite);
977    return (jint)rsProgramRasterCreate(con, pointSmooth, lineSmooth, pointSprite, lineWidth, (RsCullMode)cull);
978}
979
980
981// ---------------------------------------------------------------------------
982
983static void
984nContextBindRootScript(JNIEnv *_env, jobject _this, RsContext con, jint script)
985{
986    LOG_API("nContextBindRootScript, con(%p), script(%p)", con, (RsScript)script);
987    rsContextBindRootScript(con, (RsScript)script);
988}
989
990static void
991nContextBindProgramStore(JNIEnv *_env, jobject _this, RsContext con, jint pfs)
992{
993    LOG_API("nContextBindProgramStore, con(%p), pfs(%p)", con, (RsProgramStore)pfs);
994    rsContextBindProgramStore(con, (RsProgramStore)pfs);
995}
996
997static void
998nContextBindProgramFragment(JNIEnv *_env, jobject _this, RsContext con, jint pf)
999{
1000    LOG_API("nContextBindProgramFragment, con(%p), pf(%p)", con, (RsProgramFragment)pf);
1001    rsContextBindProgramFragment(con, (RsProgramFragment)pf);
1002}
1003
1004static void
1005nContextBindProgramVertex(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1006{
1007    LOG_API("nContextBindProgramVertex, con(%p), pf(%p)", con, (RsProgramVertex)pf);
1008    rsContextBindProgramVertex(con, (RsProgramVertex)pf);
1009}
1010
1011static void
1012nContextBindProgramRaster(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1013{
1014    LOG_API("nContextBindProgramRaster, con(%p), pf(%p)", con, (RsProgramRaster)pf);
1015    rsContextBindProgramRaster(con, (RsProgramRaster)pf);
1016}
1017
1018
1019// ---------------------------------------------------------------------------
1020
1021static void
1022nSamplerBegin(JNIEnv *_env, jobject _this, RsContext con)
1023{
1024    LOG_API("nSamplerBegin, con(%p)", con);
1025    rsSamplerBegin(con);
1026}
1027
1028static void
1029nSamplerSet(JNIEnv *_env, jobject _this, RsContext con, jint p, jint v)
1030{
1031    LOG_API("nSamplerSet, con(%p), param(%i), value(%i)", con, p, v);
1032    rsSamplerSet(con, (RsSamplerParam)p, (RsSamplerValue)v);
1033}
1034
1035static void
1036nSamplerSet2(JNIEnv *_env, jobject _this, RsContext con, jint p, jfloat v)
1037{
1038    LOG_API("nSamplerSet2, con(%p), param(%i), value(%f)", con, p, v);
1039    rsSamplerSet2(con, (RsSamplerParam)p, v);
1040}
1041
1042static jint
1043nSamplerCreate(JNIEnv *_env, jobject _this, RsContext con)
1044{
1045    LOG_API("nSamplerCreate, con(%p)", con);
1046    return (jint)rsSamplerCreate(con);
1047}
1048
1049// ---------------------------------------------------------------------------
1050
1051static jint
1052nMeshCreate(JNIEnv *_env, jobject _this, RsContext con, jint vtxCount, jint idxCount)
1053{
1054    LOG_API("nMeshCreate, con(%p), vtxCount(%i), idxCount(%i)", con, vtxCount, idxCount);
1055    int id = (int)rsMeshCreate(con, vtxCount, idxCount);
1056    return id;
1057}
1058
1059static void
1060nMeshBindVertex(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jint alloc, jint slot)
1061{
1062    LOG_API("nMeshBindVertex, con(%p), Mesh(%p), Alloc(%p), slot(%i)", con, (RsMesh)mesh, (RsAllocation)alloc, slot);
1063    rsMeshBindVertex(con, (RsMesh)mesh, (RsAllocation)alloc, slot);
1064}
1065
1066static void
1067nMeshBindIndex(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jint alloc, jint primID, jint slot)
1068{
1069    LOG_API("nMeshBindIndex, con(%p), Mesh(%p), Alloc(%p)", con, (RsMesh)mesh, (RsAllocation)alloc);
1070    rsMeshBindIndex(con, (RsMesh)mesh, (RsAllocation)alloc, primID, slot);
1071}
1072
1073static void
1074nMeshInitVertexAttribs(JNIEnv *_env, jobject _this, RsContext con, jint mesh)
1075{
1076    LOG_API("nMeshInitVertexAttribs, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1077    rsMeshInitVertexAttribs(con, (RsMesh)mesh);
1078}
1079
1080
1081static jint
1082nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, RsContext con, jint mesh)
1083{
1084    LOG_API("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1085    jint vtxCount = 0;
1086    rsaMeshGetVertexBufferCount(con, (RsMesh)mesh, &vtxCount);
1087    return vtxCount;
1088}
1089
1090static jint
1091nMeshGetIndexCount(JNIEnv *_env, jobject _this, RsContext con, jint mesh)
1092{
1093    LOG_API("nMeshGetIndexCount, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1094    jint idxCount = 0;
1095    rsaMeshGetIndexCount(con, (RsMesh)mesh, &idxCount);
1096    return idxCount;
1097}
1098
1099static void
1100nMeshGetVertices(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jintArray _ids, int numVtxIDs)
1101{
1102    LOG_API("nMeshGetVertices, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1103
1104    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
1105    rsaMeshGetVertices(con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
1106
1107    for(jint i = 0; i < numVtxIDs; i ++) {
1108        _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&allocs[i]);
1109    }
1110
1111    free(allocs);
1112}
1113
1114static void
1115nMeshGetIndices(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jintArray _idxIds, jintArray _primitives, int numIndices)
1116{
1117    LOG_API("nMeshGetVertices, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1118
1119    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
1120    uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
1121
1122    rsaMeshGetIndices(con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
1123
1124    for(jint i = 0; i < numIndices; i ++) {
1125        _env->SetIntArrayRegion(_idxIds, i, 1, (const jint*)&allocs[i]);
1126        _env->SetIntArrayRegion(_primitives, i, 1, (const jint*)&prims[i]);
1127    }
1128
1129    free(allocs);
1130    free(prims);
1131}
1132
1133// ---------------------------------------------------------------------------
1134
1135
1136static const char *classPathName = "android/renderscript/RenderScript";
1137
1138static JNINativeMethod methods[] = {
1139{"_nInit",                         "()V",                                     (void*)_nInit },
1140
1141{"nDeviceCreate",                  "()I",                                     (void*)nDeviceCreate },
1142{"nDeviceDestroy",                 "(I)V",                                    (void*)nDeviceDestroy },
1143{"nDeviceSetConfig",               "(III)V",                                  (void*)nDeviceSetConfig },
1144{"nContextGetUserMessage",         "(I[I)V",                                  (void*)nContextGetUserMessage },
1145{"nContextGetErrorMessage",        "(I)Ljava/lang/String;",                   (void*)nContextGetErrorMessage },
1146{"nContextPeekMessage",            "(I[IZ)I",                                 (void*)nContextPeekMessage },
1147
1148{"nContextInitToClient",           "(I)V",                                    (void*)nContextInitToClient },
1149{"nContextDeinitToClient",         "(I)V",                                    (void*)nContextDeinitToClient },
1150
1151
1152// All methods below are thread protected in java.
1153{"rsnContextCreate",                 "(II)I",                                 (void*)nContextCreate },
1154{"rsnContextCreateGL",               "(IIIIIIIIIIIIFI)I",                     (void*)nContextCreateGL },
1155{"rsnContextFinish",                 "(I)V",                                  (void*)nContextFinish },
1156{"rsnContextSetPriority",            "(II)V",                                 (void*)nContextSetPriority },
1157{"rsnContextSetSurface",             "(IIILandroid/view/Surface;)V",          (void*)nContextSetSurface },
1158{"rsnContextDestroy",                "(I)V",                                  (void*)nContextDestroy },
1159{"rsnContextDump",                   "(II)V",                                 (void*)nContextDump },
1160{"rsnContextPause",                  "(I)V",                                  (void*)nContextPause },
1161{"rsnContextResume",                 "(I)V",                                  (void*)nContextResume },
1162{"rsnAssignName",                    "(II[B)V",                               (void*)nAssignName },
1163{"rsnGetName",                       "(II)Ljava/lang/String;",                (void*)nGetName },
1164{"rsnObjDestroy",                    "(II)V",                                 (void*)nObjDestroy },
1165
1166{"rsnFileA3DCreateFromFile",         "(ILjava/lang/String;)I",                (void*)nFileA3DCreateFromFile },
1167{"rsnFileA3DCreateFromAssetStream",  "(II)I",                                 (void*)nFileA3DCreateFromAssetStream },
1168{"rsnFileA3DCreateFromAsset",        "(ILandroid/content/res/AssetManager;Ljava/lang/String;)I",            (void*)nFileA3DCreateFromAsset },
1169{"rsnFileA3DGetNumIndexEntries",     "(II)I",                                 (void*)nFileA3DGetNumIndexEntries },
1170{"rsnFileA3DGetIndexEntries",        "(III[I[Ljava/lang/String;)V",           (void*)nFileA3DGetIndexEntries },
1171{"rsnFileA3DGetEntryByIndex",        "(III)I",                                (void*)nFileA3DGetEntryByIndex },
1172
1173{"rsnFontCreateFromFile",            "(ILjava/lang/String;FI)I",              (void*)nFontCreateFromFile },
1174{"rsnFontCreateFromAssetStream",     "(ILjava/lang/String;FII)I",             (void*)nFontCreateFromAssetStream },
1175{"rsnFontCreateFromAsset",        "(ILandroid/content/res/AssetManager;Ljava/lang/String;FI)I",            (void*)nFontCreateFromAsset },
1176
1177{"rsnElementCreate",                 "(IIIZI)I",                              (void*)nElementCreate },
1178{"rsnElementCreate2",                "(I[I[Ljava/lang/String;[I)I",           (void*)nElementCreate2 },
1179{"rsnElementGetNativeData",          "(II[I)V",                               (void*)nElementGetNativeData },
1180{"rsnElementGetSubElements",         "(II[I[Ljava/lang/String;)V",            (void*)nElementGetSubElements },
1181
1182{"rsnTypeCreate",                    "(IIIIIZZ)I",                            (void*)nTypeCreate },
1183{"rsnTypeGetNativeData",             "(II[I)V",                               (void*)nTypeGetNativeData },
1184
1185{"rsnAllocationCreateTyped",         "(IIII)I",                               (void*)nAllocationCreateTyped },
1186{"rsnAllocationCreateFromBitmap",    "(IIILandroid/graphics/Bitmap;I)I",      (void*)nAllocationCreateFromBitmap },
1187{"rsnAllocationCubeCreateFromBitmap","(IIILandroid/graphics/Bitmap;I)I",      (void*)nAllocationCubeCreateFromBitmap },
1188
1189{"rsnAllocationCopyFromBitmap",      "(IILandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyFromBitmap },
1190{"rsnAllocationCopyToBitmap",        "(IILandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyToBitmap },
1191
1192{"rsnAllocationSyncAll",             "(III)V",                                (void*)nAllocationSyncAll },
1193{"rsnAllocationData1D",              "(IIIII[II)V",                           (void*)nAllocationData1D_i },
1194{"rsnAllocationData1D",              "(IIIII[SI)V",                           (void*)nAllocationData1D_s },
1195{"rsnAllocationData1D",              "(IIIII[BI)V",                           (void*)nAllocationData1D_b },
1196{"rsnAllocationData1D",              "(IIIII[FI)V",                           (void*)nAllocationData1D_f },
1197{"rsnAllocationElementData1D",       "(IIIII[BI)V",                           (void*)nAllocationElementData1D },
1198{"rsnAllocationData2D",              "(IIIIIIII[II)V",                        (void*)nAllocationData2D_i },
1199{"rsnAllocationData2D",              "(IIIIIIII[SI)V",                        (void*)nAllocationData2D_s },
1200{"rsnAllocationData2D",              "(IIIIIIII[BI)V",                        (void*)nAllocationData2D_b },
1201{"rsnAllocationData2D",              "(IIIIIIII[FI)V",                        (void*)nAllocationData2D_f },
1202{"rsnAllocationRead",                "(II[I)V",                               (void*)nAllocationRead_i },
1203{"rsnAllocationRead",                "(II[S)V",                               (void*)nAllocationRead_s },
1204{"rsnAllocationRead",                "(II[B)V",                               (void*)nAllocationRead_b },
1205{"rsnAllocationRead",                "(II[F)V",                               (void*)nAllocationRead_f },
1206{"rsnAllocationGetType",             "(II)I",                                 (void*)nAllocationGetType},
1207{"rsnAllocationResize1D",            "(III)V",                                (void*)nAllocationResize1D },
1208{"rsnAllocationResize2D",            "(IIII)V",                               (void*)nAllocationResize2D },
1209{"rsnAllocationGenerateMipmaps",     "(II)V",                                 (void*)nAllocationGenerateMipmaps },
1210
1211{"rsnScriptBindAllocation",          "(IIII)V",                               (void*)nScriptBindAllocation },
1212{"rsnScriptSetTimeZone",             "(II[B)V",                               (void*)nScriptSetTimeZone },
1213{"rsnScriptInvoke",                  "(III)V",                                (void*)nScriptInvoke },
1214{"rsnScriptInvokeV",                 "(III[B)V",                              (void*)nScriptInvokeV },
1215{"rsnScriptSetVarI",                 "(IIII)V",                               (void*)nScriptSetVarI },
1216{"rsnScriptSetVarJ",                 "(IIIJ)V",                               (void*)nScriptSetVarJ },
1217{"rsnScriptSetVarF",                 "(IIIF)V",                               (void*)nScriptSetVarF },
1218{"rsnScriptSetVarD",                 "(IIID)V",                               (void*)nScriptSetVarD },
1219{"rsnScriptSetVarV",                 "(III[B)V",                              (void*)nScriptSetVarV },
1220{"rsnScriptSetVarObj",               "(IIII)V",                               (void*)nScriptSetVarObj },
1221
1222{"rsnScriptCCreate",                 "(ILjava/lang/String;Ljava/lang/String;[BI)I",  (void*)nScriptCCreate },
1223
1224{"rsnProgramStoreCreate",            "(IZZZZZZIII)I",                         (void*)nProgramStoreCreate },
1225
1226{"rsnProgramBindConstants",          "(IIII)V",                               (void*)nProgramBindConstants },
1227{"rsnProgramBindTexture",            "(IIII)V",                               (void*)nProgramBindTexture },
1228{"rsnProgramBindSampler",            "(IIII)V",                               (void*)nProgramBindSampler },
1229
1230{"rsnProgramFragmentCreate",         "(ILjava/lang/String;[I)I",              (void*)nProgramFragmentCreate },
1231{"rsnProgramRasterCreate",           "(IZZZFI)I",                             (void*)nProgramRasterCreate },
1232{"rsnProgramVertexCreate",           "(ILjava/lang/String;[I)I",              (void*)nProgramVertexCreate },
1233
1234{"rsnContextBindRootScript",         "(II)V",                                 (void*)nContextBindRootScript },
1235{"rsnContextBindProgramStore",       "(II)V",                                 (void*)nContextBindProgramStore },
1236{"rsnContextBindProgramFragment",    "(II)V",                                 (void*)nContextBindProgramFragment },
1237{"rsnContextBindProgramVertex",      "(II)V",                                 (void*)nContextBindProgramVertex },
1238{"rsnContextBindProgramRaster",      "(II)V",                                 (void*)nContextBindProgramRaster },
1239
1240{"rsnSamplerBegin",                  "(I)V",                                  (void*)nSamplerBegin },
1241{"rsnSamplerSet",                    "(III)V",                                (void*)nSamplerSet },
1242{"rsnSamplerSet2",                   "(IIF)V",                                (void*)nSamplerSet2 },
1243{"rsnSamplerCreate",                 "(I)I",                                  (void*)nSamplerCreate },
1244
1245{"rsnMeshCreate",                    "(III)I",                                (void*)nMeshCreate },
1246{"rsnMeshBindVertex",                "(IIII)V",                               (void*)nMeshBindVertex },
1247{"rsnMeshBindIndex",                 "(IIIII)V",                              (void*)nMeshBindIndex },
1248{"rsnMeshInitVertexAttribs",         "(II)V",                                 (void*)nMeshInitVertexAttribs },
1249
1250{"rsnMeshGetVertexBufferCount",      "(II)I",                                 (void*)nMeshGetVertexBufferCount },
1251{"rsnMeshGetIndexCount",             "(II)I",                                 (void*)nMeshGetIndexCount },
1252{"rsnMeshGetVertices",               "(II[II)V",                              (void*)nMeshGetVertices },
1253{"rsnMeshGetIndices",                "(II[I[II)V",                            (void*)nMeshGetIndices },
1254
1255};
1256
1257static int registerFuncs(JNIEnv *_env)
1258{
1259    return android::AndroidRuntime::registerNativeMethods(
1260            _env, classPathName, methods, NELEM(methods));
1261}
1262
1263// ---------------------------------------------------------------------------
1264
1265jint JNI_OnLoad(JavaVM* vm, void* reserved)
1266{
1267    JNIEnv* env = NULL;
1268    jint result = -1;
1269
1270    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
1271        LOGE("ERROR: GetEnv failed\n");
1272        goto bail;
1273    }
1274    assert(env != NULL);
1275
1276    if (registerFuncs(env) < 0) {
1277        LOGE("ERROR: MediaPlayer native registration failed\n");
1278        goto bail;
1279    }
1280
1281    /* success -- return valid version number */
1282    result = JNI_VERSION_1_4;
1283
1284bail:
1285    return result;
1286}
1287