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