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