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