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