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