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