android_renderscript_RenderScript.cpp revision 2c74ad9aae29cc64fece926f353825a7925792c2
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 void
852nScriptCBegin(JNIEnv *_env, jobject _this, RsContext con)
853{
854    LOG_API("nScriptCBegin, con(%p)", con);
855    rsScriptCBegin(con);
856}
857
858static void
859nScriptCSetScript(JNIEnv *_env, jobject _this, RsContext con, jbyteArray scriptRef,
860                  jint offset, jint length)
861{
862    LOG_API("!!! nScriptCSetScript, con(%p)", con);
863    jint _exception = 0;
864    jint remaining;
865    jbyte* script_base = 0;
866    jbyte* script_ptr;
867    if (!scriptRef) {
868        _exception = 1;
869        //_env->ThrowNew(IAEClass, "script == null");
870        goto exit;
871    }
872    if (offset < 0) {
873        _exception = 1;
874        //_env->ThrowNew(IAEClass, "offset < 0");
875        goto exit;
876    }
877    if (length < 0) {
878        _exception = 1;
879        //_env->ThrowNew(IAEClass, "length < 0");
880        goto exit;
881    }
882    remaining = _env->GetArrayLength(scriptRef) - offset;
883    if (remaining < length) {
884        _exception = 1;
885        //_env->ThrowNew(IAEClass, "length > script.length - offset");
886        goto exit;
887    }
888    script_base = (jbyte *)
889        _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
890    script_ptr = script_base + offset;
891
892    rsScriptCSetText(con, (const char *)script_ptr, length);
893
894exit:
895    if (script_base) {
896        _env->ReleasePrimitiveArrayCritical(scriptRef, script_base,
897                _exception ? JNI_ABORT: 0);
898    }
899}
900
901static jint
902nScriptCCreate(JNIEnv *_env, jobject _this, RsContext con, jstring packageName, jstring resName, jstring cacheDir)
903{
904    LOG_API("nScriptCCreate, con(%p)", con);
905    AutoJavaStringToUTF8 packageNameUTF(_env, packageName);
906    AutoJavaStringToUTF8 resNameUTF(_env, resName);
907    AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
908    jint i = (jint)rsScriptCCreate(con, packageNameUTF.c_str(), resNameUTF.c_str(), cacheDirUTF.c_str());
909    return i;
910}
911
912// ---------------------------------------------------------------------------
913
914static void
915nProgramStoreBegin(JNIEnv *_env, jobject _this, RsContext con, jint in, jint out)
916{
917    LOG_API("nProgramStoreBegin, con(%p), in(%p), out(%p)", con, (RsElement)in, (RsElement)out);
918    rsProgramStoreBegin(con, (RsElement)in, (RsElement)out);
919}
920
921static void
922nProgramStoreDepthFunc(JNIEnv *_env, jobject _this, RsContext con, jint func)
923{
924    LOG_API("nProgramStoreDepthFunc, con(%p), func(%i)", con, func);
925    rsProgramStoreDepthFunc(con, (RsDepthFunc)func);
926}
927
928static void
929nProgramStoreDepthMask(JNIEnv *_env, jobject _this, RsContext con, jboolean enable)
930{
931    LOG_API("nProgramStoreDepthMask, con(%p), enable(%i)", con, enable);
932    rsProgramStoreDepthMask(con, enable);
933}
934
935static void
936nProgramStoreColorMask(JNIEnv *_env, jobject _this, RsContext con, jboolean r, jboolean g, jboolean b, jboolean a)
937{
938    LOG_API("nProgramStoreColorMask, con(%p), r(%i), g(%i), b(%i), a(%i)", con, r, g, b, a);
939    rsProgramStoreColorMask(con, r, g, b, a);
940}
941
942static void
943nProgramStoreBlendFunc(JNIEnv *_env, jobject _this, RsContext con, int src, int dst)
944{
945    LOG_API("nProgramStoreBlendFunc, con(%p), src(%i), dst(%i)", con, src, dst);
946    rsProgramStoreBlendFunc(con, (RsBlendSrcFunc)src, (RsBlendDstFunc)dst);
947}
948
949static void
950nProgramStoreDither(JNIEnv *_env, jobject _this, RsContext con, jboolean enable)
951{
952    LOG_API("nProgramStoreDither, con(%p), enable(%i)", con, enable);
953    rsProgramStoreDither(con, enable);
954}
955
956static jint
957nProgramStoreCreate(JNIEnv *_env, jobject _this, RsContext con)
958{
959    LOG_API("nProgramStoreCreate, con(%p)", con);
960    return (jint)rsProgramStoreCreate(con);
961}
962
963// ---------------------------------------------------------------------------
964
965static void
966nProgramBindConstants(JNIEnv *_env, jobject _this, RsContext con, jint vpv, jint slot, jint a)
967{
968    LOG_API("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", con, (RsProgramVertex)vpv, slot, (RsAllocation)a);
969    rsProgramBindConstants(con, (RsProgram)vpv, slot, (RsAllocation)a);
970}
971
972static void
973nProgramBindTexture(JNIEnv *_env, jobject _this, RsContext con, jint vpf, jint slot, jint a)
974{
975    LOG_API("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
976    rsProgramBindTexture(con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
977}
978
979static void
980nProgramBindSampler(JNIEnv *_env, jobject _this, RsContext con, jint vpf, jint slot, jint a)
981{
982    LOG_API("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsSampler)a);
983    rsProgramBindSampler(con, (RsProgramFragment)vpf, slot, (RsSampler)a);
984}
985
986// ---------------------------------------------------------------------------
987
988static jint
989nProgramFragmentCreate(JNIEnv *_env, jobject _this, RsContext con, jstring shader, jintArray params)
990{
991    AutoJavaStringToUTF8 shaderUTF(_env, shader);
992    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
993    jint paramLen = _env->GetArrayLength(params);
994
995    LOG_API("nProgramFragmentCreate, con(%p), paramLen(%i)", con, paramLen);
996
997    jint ret = (jint)rsProgramFragmentCreate(con, shaderUTF.c_str(), shaderUTF.length(), (uint32_t *)paramPtr, paramLen);
998    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
999    return ret;
1000}
1001
1002
1003// ---------------------------------------------------------------------------
1004
1005static jint
1006nProgramVertexCreate(JNIEnv *_env, jobject _this, RsContext con, jstring shader, jintArray params)
1007{
1008    AutoJavaStringToUTF8 shaderUTF(_env, shader);
1009    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1010    jint paramLen = _env->GetArrayLength(params);
1011
1012    LOG_API("nProgramVertexCreate, con(%p), paramLen(%i)", con, paramLen);
1013
1014    jint ret = (jint)rsProgramVertexCreate(con, shaderUTF.c_str(), shaderUTF.length(), (uint32_t *)paramPtr, paramLen);
1015    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1016    return ret;
1017}
1018
1019// ---------------------------------------------------------------------------
1020
1021static jint
1022nProgramRasterCreate(JNIEnv *_env, jobject _this, RsContext con, jboolean pointSmooth, jboolean lineSmooth, jboolean pointSprite)
1023{
1024    LOG_API("nProgramRasterCreate, con(%p), pointSmooth(%i), lineSmooth(%i), pointSprite(%i)",
1025            con, pointSmooth, lineSmooth, pointSprite);
1026    return (jint)rsProgramRasterCreate(con, pointSmooth, lineSmooth, pointSprite);
1027}
1028
1029static void
1030nProgramRasterSetLineWidth(JNIEnv *_env, jobject _this, RsContext con, jint vpr, jfloat v)
1031{
1032    LOG_API("nProgramRasterSetLineWidth, con(%p), vpf(%p), value(%f)", con, (RsProgramRaster)vpr, v);
1033    rsProgramRasterSetLineWidth(con, (RsProgramRaster)vpr, v);
1034}
1035
1036static void
1037nProgramRasterSetCullMode(JNIEnv *_env, jobject _this, RsContext con, jint vpr, jint v)
1038{
1039    LOG_API("nProgramRasterSetCullMode, con(%p), vpf(%p), value(%i)", con, (RsProgramRaster)vpr, v);
1040    rsProgramRasterSetCullMode(con, (RsProgramRaster)vpr, (RsCullMode)v);
1041}
1042
1043
1044// ---------------------------------------------------------------------------
1045
1046static void
1047nContextBindRootScript(JNIEnv *_env, jobject _this, RsContext con, jint script)
1048{
1049    LOG_API("nContextBindRootScript, con(%p), script(%p)", con, (RsScript)script);
1050    rsContextBindRootScript(con, (RsScript)script);
1051}
1052
1053static void
1054nContextBindProgramStore(JNIEnv *_env, jobject _this, RsContext con, jint pfs)
1055{
1056    LOG_API("nContextBindProgramStore, con(%p), pfs(%p)", con, (RsProgramStore)pfs);
1057    rsContextBindProgramStore(con, (RsProgramStore)pfs);
1058}
1059
1060static void
1061nContextBindProgramFragment(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1062{
1063    LOG_API("nContextBindProgramFragment, con(%p), pf(%p)", con, (RsProgramFragment)pf);
1064    rsContextBindProgramFragment(con, (RsProgramFragment)pf);
1065}
1066
1067static void
1068nContextBindProgramVertex(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1069{
1070    LOG_API("nContextBindProgramVertex, con(%p), pf(%p)", con, (RsProgramVertex)pf);
1071    rsContextBindProgramVertex(con, (RsProgramVertex)pf);
1072}
1073
1074static void
1075nContextBindProgramRaster(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1076{
1077    LOG_API("nContextBindProgramRaster, con(%p), pf(%p)", con, (RsProgramRaster)pf);
1078    rsContextBindProgramRaster(con, (RsProgramRaster)pf);
1079}
1080
1081
1082// ---------------------------------------------------------------------------
1083
1084static void
1085nSamplerBegin(JNIEnv *_env, jobject _this, RsContext con)
1086{
1087    LOG_API("nSamplerBegin, con(%p)", con);
1088    rsSamplerBegin(con);
1089}
1090
1091static void
1092nSamplerSet(JNIEnv *_env, jobject _this, RsContext con, jint p, jint v)
1093{
1094    LOG_API("nSamplerSet, con(%p), param(%i), value(%i)", con, p, v);
1095    rsSamplerSet(con, (RsSamplerParam)p, (RsSamplerValue)v);
1096}
1097
1098static void
1099nSamplerSet2(JNIEnv *_env, jobject _this, RsContext con, jint p, jfloat v)
1100{
1101    LOG_API("nSamplerSet2, con(%p), param(%i), value(%f)", con, p, v);
1102    rsSamplerSet2(con, (RsSamplerParam)p, v);
1103}
1104
1105static jint
1106nSamplerCreate(JNIEnv *_env, jobject _this, RsContext con)
1107{
1108    LOG_API("nSamplerCreate, con(%p)", con);
1109    return (jint)rsSamplerCreate(con);
1110}
1111
1112// ---------------------------------------------------------------------------
1113
1114static jint
1115nMeshCreate(JNIEnv *_env, jobject _this, RsContext con, jint vtxCount, jint idxCount)
1116{
1117    LOG_API("nMeshCreate, con(%p), vtxCount(%i), idxCount(%i)", con, vtxCount, idxCount);
1118    int id = (int)rsMeshCreate(con, vtxCount, idxCount);
1119    return id;
1120}
1121
1122static void
1123nMeshBindVertex(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jint alloc, jint slot)
1124{
1125    LOG_API("nMeshBindVertex, con(%p), Mesh(%p), Alloc(%p), slot(%i)", con, (RsMesh)mesh, (RsAllocation)alloc, slot);
1126    rsMeshBindVertex(con, (RsMesh)mesh, (RsAllocation)alloc, slot);
1127}
1128
1129static void
1130nMeshBindIndex(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jint alloc, jint primID, jint slot)
1131{
1132    LOG_API("nMeshBindIndex, con(%p), Mesh(%p), Alloc(%p)", con, (RsMesh)mesh, (RsAllocation)alloc);
1133    rsMeshBindIndex(con, (RsMesh)mesh, (RsAllocation)alloc, primID, slot);
1134}
1135
1136static void
1137nMeshInitVertexAttribs(JNIEnv *_env, jobject _this, RsContext con, jint mesh)
1138{
1139    LOG_API("nMeshInitVertexAttribs, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1140    rsMeshInitVertexAttribs(con, (RsMesh)mesh);
1141}
1142
1143
1144static jint
1145nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, RsContext con, jint mesh)
1146{
1147    LOG_API("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1148    jint vtxCount = 0;
1149    rsaMeshGetVertexBufferCount(con, (RsMesh)mesh, &vtxCount);
1150    return vtxCount;
1151}
1152
1153static jint
1154nMeshGetIndexCount(JNIEnv *_env, jobject _this, RsContext con, jint mesh)
1155{
1156    LOG_API("nMeshGetIndexCount, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1157    jint idxCount = 0;
1158    rsaMeshGetIndexCount(con, (RsMesh)mesh, &idxCount);
1159    return idxCount;
1160}
1161
1162static void
1163nMeshGetVertices(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jintArray _ids, int numVtxIDs)
1164{
1165    LOG_API("nMeshGetVertices, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1166
1167    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
1168    rsaMeshGetVertices(con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
1169
1170    for(jint i = 0; i < numVtxIDs; i ++) {
1171        _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&allocs[i]);
1172    }
1173
1174    free(allocs);
1175}
1176
1177static void
1178nMeshGetIndices(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jintArray _idxIds, jintArray _primitives, int numIndices)
1179{
1180    LOG_API("nMeshGetVertices, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1181
1182    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
1183    uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
1184
1185    rsaMeshGetIndices(con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
1186
1187    for(jint i = 0; i < numIndices; i ++) {
1188        _env->SetIntArrayRegion(_idxIds, i, 1, (const jint*)&allocs[i]);
1189        _env->SetIntArrayRegion(_primitives, i, 1, (const jint*)&prims[i]);
1190    }
1191
1192    free(allocs);
1193    free(prims);
1194}
1195
1196// ---------------------------------------------------------------------------
1197
1198
1199static const char *classPathName = "android/renderscript/RenderScript";
1200
1201static JNINativeMethod methods[] = {
1202{"_nInit",                         "()V",                                     (void*)_nInit },
1203
1204{"nDeviceCreate",                  "()I",                                     (void*)nDeviceCreate },
1205{"nDeviceDestroy",                 "(I)V",                                    (void*)nDeviceDestroy },
1206{"nDeviceSetConfig",               "(III)V",                                  (void*)nDeviceSetConfig },
1207{"nContextGetUserMessage",         "(I[I)V",                                  (void*)nContextGetUserMessage },
1208{"nContextGetErrorMessage",        "(I)Ljava/lang/String;",                   (void*)nContextGetErrorMessage },
1209{"nContextPeekMessage",            "(I[IZ)I",                                 (void*)nContextPeekMessage },
1210
1211{"nContextInitToClient",           "(I)V",                                    (void*)nContextInitToClient },
1212{"nContextDeinitToClient",         "(I)V",                                    (void*)nContextDeinitToClient },
1213
1214
1215// All methods below are thread protected in java.
1216{"rsnContextCreate",                 "(II)I",                                 (void*)nContextCreate },
1217{"rsnContextCreateGL",               "(IIIIIIIIIIIIFI)I",                     (void*)nContextCreateGL },
1218{"rsnContextFinish",                 "(I)V",                                  (void*)nContextFinish },
1219{"rsnContextSetPriority",            "(II)V",                                 (void*)nContextSetPriority },
1220{"rsnContextSetSurface",             "(IIILandroid/view/Surface;)V",          (void*)nContextSetSurface },
1221{"rsnContextDestroy",                "(I)V",                                  (void*)nContextDestroy },
1222{"rsnContextDump",                   "(II)V",                                 (void*)nContextDump },
1223{"rsnContextPause",                  "(I)V",                                  (void*)nContextPause },
1224{"rsnContextResume",                 "(I)V",                                  (void*)nContextResume },
1225{"rsnAssignName",                    "(II[B)V",                               (void*)nAssignName },
1226{"rsnGetName",                       "(II)Ljava/lang/String;",                (void*)nGetName },
1227{"rsnObjDestroy",                    "(II)V",                                 (void*)nObjDestroy },
1228
1229{"rsnFileA3DCreateFromFile",         "(ILjava/lang/String;)I",                (void*)nFileA3DCreateFromFile },
1230{"rsnFileA3DCreateFromAssetStream",  "(II)I",                                 (void*)nFileA3DCreateFromAssetStream },
1231{"rsnFileA3DCreateFromAsset",        "(ILandroid/content/res/AssetManager;Ljava/lang/String;)I",            (void*)nFileA3DCreateFromAsset },
1232{"rsnFileA3DGetNumIndexEntries",     "(II)I",                                 (void*)nFileA3DGetNumIndexEntries },
1233{"rsnFileA3DGetIndexEntries",        "(III[I[Ljava/lang/String;)V",           (void*)nFileA3DGetIndexEntries },
1234{"rsnFileA3DGetEntryByIndex",        "(III)I",                                (void*)nFileA3DGetEntryByIndex },
1235
1236{"rsnFontCreateFromFile",            "(ILjava/lang/String;FI)I",              (void*)nFontCreateFromFile },
1237{"rsnFontCreateFromAssetStream",     "(ILjava/lang/String;FII)I",             (void*)nFontCreateFromAssetStream },
1238{"rsnFontCreateFromAsset",        "(ILandroid/content/res/AssetManager;Ljava/lang/String;FI)I",            (void*)nFontCreateFromAsset },
1239
1240{"rsnElementCreate",                 "(IIIZI)I",                              (void*)nElementCreate },
1241{"rsnElementCreate2",                "(I[I[Ljava/lang/String;[I)I",           (void*)nElementCreate2 },
1242{"rsnElementGetNativeData",          "(II[I)V",                               (void*)nElementGetNativeData },
1243{"rsnElementGetSubElements",         "(II[I[Ljava/lang/String;)V",            (void*)nElementGetSubElements },
1244
1245{"rsnTypeCreate",                    "(IIIIIZZ)I",                            (void*)nTypeCreate },
1246{"rsnTypeGetNativeData",             "(II[I)V",                               (void*)nTypeGetNativeData },
1247
1248{"rsnAllocationCreateTyped",         "(IIII)I",                               (void*)nAllocationCreateTyped },
1249{"rsnAllocationCreateFromBitmap",    "(IIILandroid/graphics/Bitmap;I)I",      (void*)nAllocationCreateFromBitmap },
1250{"rsnAllocationCubeCreateFromBitmap","(IIILandroid/graphics/Bitmap;I)I",      (void*)nAllocationCubeCreateFromBitmap },
1251
1252{"rsnAllocationCopyFromBitmap",      "(IILandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyFromBitmap },
1253{"rsnAllocationCopyToBitmap",        "(IILandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyToBitmap },
1254
1255{"rsnAllocationSyncAll",             "(III)V",                                (void*)nAllocationSyncAll },
1256{"rsnAllocationData1D",              "(IIIII[II)V",                           (void*)nAllocationData1D_i },
1257{"rsnAllocationData1D",              "(IIIII[SI)V",                           (void*)nAllocationData1D_s },
1258{"rsnAllocationData1D",              "(IIIII[BI)V",                           (void*)nAllocationData1D_b },
1259{"rsnAllocationData1D",              "(IIIII[FI)V",                           (void*)nAllocationData1D_f },
1260{"rsnAllocationElementData1D",       "(IIIII[BI)V",                           (void*)nAllocationElementData1D },
1261{"rsnAllocationData2D",              "(IIIIIIII[II)V",                        (void*)nAllocationData2D_i },
1262{"rsnAllocationData2D",              "(IIIIIIII[SI)V",                        (void*)nAllocationData2D_s },
1263{"rsnAllocationData2D",              "(IIIIIIII[BI)V",                        (void*)nAllocationData2D_b },
1264{"rsnAllocationData2D",              "(IIIIIIII[FI)V",                        (void*)nAllocationData2D_f },
1265{"rsnAllocationRead",                "(II[I)V",                               (void*)nAllocationRead_i },
1266{"rsnAllocationRead",                "(II[S)V",                               (void*)nAllocationRead_s },
1267{"rsnAllocationRead",                "(II[B)V",                               (void*)nAllocationRead_b },
1268{"rsnAllocationRead",                "(II[F)V",                               (void*)nAllocationRead_f },
1269{"rsnAllocationGetType",             "(II)I",                                 (void*)nAllocationGetType},
1270{"rsnAllocationResize1D",            "(III)V",                                (void*)nAllocationResize1D },
1271{"rsnAllocationResize2D",            "(IIII)V",                               (void*)nAllocationResize2D },
1272{"rsnAllocationGenerateMipmaps",     "(II)V",                                 (void*)nAllocationGenerateMipmaps },
1273
1274{"rsnScriptBindAllocation",          "(IIII)V",                               (void*)nScriptBindAllocation },
1275{"rsnScriptSetTimeZone",             "(II[B)V",                               (void*)nScriptSetTimeZone },
1276{"rsnScriptInvoke",                  "(III)V",                                (void*)nScriptInvoke },
1277{"rsnScriptInvokeV",                 "(III[B)V",                              (void*)nScriptInvokeV },
1278{"rsnScriptSetVarI",                 "(IIII)V",                               (void*)nScriptSetVarI },
1279{"rsnScriptSetVarJ",                 "(IIIJ)V",                               (void*)nScriptSetVarJ },
1280{"rsnScriptSetVarF",                 "(IIIF)V",                               (void*)nScriptSetVarF },
1281{"rsnScriptSetVarD",                 "(IIID)V",                               (void*)nScriptSetVarD },
1282{"rsnScriptSetVarV",                 "(III[B)V",                              (void*)nScriptSetVarV },
1283{"rsnScriptSetVarObj",               "(IIII)V",                               (void*)nScriptSetVarObj },
1284
1285{"rsnScriptCBegin",                  "(I)V",                                  (void*)nScriptCBegin },
1286{"rsnScriptCSetScript",              "(I[BII)V",                              (void*)nScriptCSetScript },
1287{"rsnScriptCCreate",                 "(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)I",  (void*)nScriptCCreate },
1288
1289{"rsnProgramStoreBegin",             "(III)V",                                (void*)nProgramStoreBegin },
1290{"rsnProgramStoreDepthFunc",         "(II)V",                                 (void*)nProgramStoreDepthFunc },
1291{"rsnProgramStoreDepthMask",         "(IZ)V",                                 (void*)nProgramStoreDepthMask },
1292{"rsnProgramStoreColorMask",         "(IZZZZ)V",                              (void*)nProgramStoreColorMask },
1293{"rsnProgramStoreBlendFunc",         "(III)V",                                (void*)nProgramStoreBlendFunc },
1294{"rsnProgramStoreDither",            "(IZ)V",                                 (void*)nProgramStoreDither },
1295{"rsnProgramStoreCreate",            "(I)I",                                  (void*)nProgramStoreCreate },
1296
1297{"rsnProgramBindConstants",          "(IIII)V",                               (void*)nProgramBindConstants },
1298{"rsnProgramBindTexture",            "(IIII)V",                               (void*)nProgramBindTexture },
1299{"rsnProgramBindSampler",            "(IIII)V",                               (void*)nProgramBindSampler },
1300
1301{"rsnProgramFragmentCreate",         "(ILjava/lang/String;[I)I",              (void*)nProgramFragmentCreate },
1302
1303{"rsnProgramRasterCreate",           "(IZZZ)I",                               (void*)nProgramRasterCreate },
1304{"rsnProgramRasterSetLineWidth",     "(IIF)V",                                (void*)nProgramRasterSetLineWidth },
1305{"rsnProgramRasterSetCullMode",      "(III)V",                                (void*)nProgramRasterSetCullMode },
1306
1307{"rsnProgramVertexCreate",           "(ILjava/lang/String;[I)I",              (void*)nProgramVertexCreate },
1308
1309{"rsnContextBindRootScript",         "(II)V",                                 (void*)nContextBindRootScript },
1310{"rsnContextBindProgramStore",       "(II)V",                                 (void*)nContextBindProgramStore },
1311{"rsnContextBindProgramFragment",    "(II)V",                                 (void*)nContextBindProgramFragment },
1312{"rsnContextBindProgramVertex",      "(II)V",                                 (void*)nContextBindProgramVertex },
1313{"rsnContextBindProgramRaster",      "(II)V",                                 (void*)nContextBindProgramRaster },
1314
1315{"rsnSamplerBegin",                  "(I)V",                                  (void*)nSamplerBegin },
1316{"rsnSamplerSet",                    "(III)V",                                (void*)nSamplerSet },
1317{"rsnSamplerSet2",                   "(IIF)V",                                (void*)nSamplerSet2 },
1318{"rsnSamplerCreate",                 "(I)I",                                  (void*)nSamplerCreate },
1319
1320{"rsnMeshCreate",                    "(III)I",                                (void*)nMeshCreate },
1321{"rsnMeshBindVertex",                "(IIII)V",                               (void*)nMeshBindVertex },
1322{"rsnMeshBindIndex",                 "(IIIII)V",                              (void*)nMeshBindIndex },
1323{"rsnMeshInitVertexAttribs",         "(II)V",                                 (void*)nMeshInitVertexAttribs },
1324
1325{"rsnMeshGetVertexBufferCount",      "(II)I",                                 (void*)nMeshGetVertexBufferCount },
1326{"rsnMeshGetIndexCount",             "(II)I",                                 (void*)nMeshGetIndexCount },
1327{"rsnMeshGetVertices",               "(II[II)V",                              (void*)nMeshGetVertices },
1328{"rsnMeshGetIndices",                "(II[I[II)V",                            (void*)nMeshGetIndices },
1329
1330};
1331
1332static int registerFuncs(JNIEnv *_env)
1333{
1334    return android::AndroidRuntime::registerNativeMethods(
1335            _env, classPathName, methods, NELEM(methods));
1336}
1337
1338// ---------------------------------------------------------------------------
1339
1340jint JNI_OnLoad(JavaVM* vm, void* reserved)
1341{
1342    JNIEnv* env = NULL;
1343    jint result = -1;
1344
1345    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
1346        LOGE("ERROR: GetEnv failed\n");
1347        goto bail;
1348    }
1349    assert(env != NULL);
1350
1351    if (registerFuncs(env) < 0) {
1352        LOGE("ERROR: MediaPlayer native registration failed\n");
1353        goto bail;
1354    }
1355
1356    /* success -- return valid version number */
1357    result = JNI_VERSION_1_4;
1358
1359bail:
1360    return result;
1361}
1362