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