android_renderscript_RenderScript.cpp revision fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33
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
956
957static void
958nScriptSetTimeZone(JNIEnv *_env, jobject _this, RsContext con, jint script, jbyteArray timeZone)
959{
960    LOG_API("nScriptCSetTimeZone, con(%p), s(%p), timeZone(%s)", con, (void *)script, (const char *)timeZone);
961
962    jint length = _env->GetArrayLength(timeZone);
963    jbyte* timeZone_ptr;
964    timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
965
966    rsScriptSetTimeZone(con, (RsScript)script, (const char *)timeZone_ptr, length);
967
968    if (timeZone_ptr) {
969        _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
970    }
971}
972
973static void
974nScriptInvoke(JNIEnv *_env, jobject _this, RsContext con, jint obj, jint slot)
975{
976    LOG_API("nScriptInvoke, con(%p), script(%p)", con, (void *)obj);
977    rsScriptInvoke(con, (RsScript)obj, slot);
978}
979
980static void
981nScriptInvokeV(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jbyteArray data)
982{
983    LOG_API("nScriptInvokeV, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
984    jint len = _env->GetArrayLength(data);
985    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
986    rsScriptInvokeV(con, (RsScript)script, slot, ptr, len);
987    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
988}
989
990static void
991nScriptForEach(JNIEnv *_env, jobject _this, RsContext con,
992               jint script, jint slot, jint ain, jint aout)
993{
994    LOG_API("nScriptForEach, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
995    rsScriptForEach(con, (RsScript)script, slot, (RsAllocation)ain, (RsAllocation)aout, NULL, 0);
996}
997static void
998nScriptForEachV(JNIEnv *_env, jobject _this, RsContext con,
999                jint script, jint slot, jint ain, jint aout, jbyteArray params)
1000{
1001    LOG_API("nScriptForEach, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
1002    jint len = _env->GetArrayLength(params);
1003    jbyte *ptr = _env->GetByteArrayElements(params, NULL);
1004    rsScriptForEach(con, (RsScript)script, slot, (RsAllocation)ain, (RsAllocation)aout, ptr, len);
1005    _env->ReleaseByteArrayElements(params, ptr, JNI_ABORT);
1006}
1007
1008
1009// -----------------------------------
1010
1011static jint
1012nScriptCCreate(JNIEnv *_env, jobject _this, RsContext con,
1013               jstring resName, jstring cacheDir,
1014               jbyteArray scriptRef, jint length)
1015{
1016    LOG_API("nScriptCCreate, con(%p)", con);
1017
1018    AutoJavaStringToUTF8 resNameUTF(_env, resName);
1019    AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
1020    jint ret = 0;
1021    jbyte* script_ptr = NULL;
1022    jint _exception = 0;
1023    jint remaining;
1024    if (!scriptRef) {
1025        _exception = 1;
1026        //jniThrowException(_env, "java/lang/IllegalArgumentException", "script == null");
1027        goto exit;
1028    }
1029    if (length < 0) {
1030        _exception = 1;
1031        //jniThrowException(_env, "java/lang/IllegalArgumentException", "length < 0");
1032        goto exit;
1033    }
1034    remaining = _env->GetArrayLength(scriptRef);
1035    if (remaining < length) {
1036        _exception = 1;
1037        //jniThrowException(_env, "java/lang/IllegalArgumentException",
1038        //        "length > script.length - offset");
1039        goto exit;
1040    }
1041    script_ptr = (jbyte *)
1042        _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
1043
1044    //rsScriptCSetText(con, (const char *)script_ptr, length);
1045
1046    ret = (jint)rsScriptCCreate(con,
1047                                resNameUTF.c_str(), resNameUTF.length(),
1048                                cacheDirUTF.c_str(), cacheDirUTF.length(),
1049                                (const char *)script_ptr, length);
1050
1051exit:
1052    if (script_ptr) {
1053        _env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr,
1054                _exception ? JNI_ABORT: 0);
1055    }
1056
1057    return ret;
1058}
1059
1060// ---------------------------------------------------------------------------
1061
1062static jint
1063nProgramStoreCreate(JNIEnv *_env, jobject _this, RsContext con,
1064                    jboolean colorMaskR, jboolean colorMaskG, jboolean colorMaskB, jboolean colorMaskA,
1065                    jboolean depthMask, jboolean ditherEnable,
1066                    jint srcFunc, jint destFunc,
1067                    jint depthFunc)
1068{
1069    LOG_API("nProgramStoreCreate, con(%p)", con);
1070    return (jint)rsProgramStoreCreate(con, colorMaskR, colorMaskG, colorMaskB, colorMaskA,
1071                                      depthMask, ditherEnable, (RsBlendSrcFunc)srcFunc,
1072                                      (RsBlendDstFunc)destFunc, (RsDepthFunc)depthFunc);
1073}
1074
1075// ---------------------------------------------------------------------------
1076
1077static void
1078nProgramBindConstants(JNIEnv *_env, jobject _this, RsContext con, jint vpv, jint slot, jint a)
1079{
1080    LOG_API("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", con, (RsProgramVertex)vpv, slot, (RsAllocation)a);
1081    rsProgramBindConstants(con, (RsProgram)vpv, slot, (RsAllocation)a);
1082}
1083
1084static void
1085nProgramBindTexture(JNIEnv *_env, jobject _this, RsContext con, jint vpf, jint slot, jint a)
1086{
1087    LOG_API("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
1088    rsProgramBindTexture(con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
1089}
1090
1091static void
1092nProgramBindSampler(JNIEnv *_env, jobject _this, RsContext con, jint vpf, jint slot, jint a)
1093{
1094    LOG_API("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsSampler)a);
1095    rsProgramBindSampler(con, (RsProgramFragment)vpf, slot, (RsSampler)a);
1096}
1097
1098// ---------------------------------------------------------------------------
1099
1100static jint
1101nProgramFragmentCreate(JNIEnv *_env, jobject _this, RsContext con, jstring shader,
1102                       jobjectArray texNames, jintArray params)
1103{
1104    AutoJavaStringToUTF8 shaderUTF(_env, shader);
1105    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1106    jint paramLen = _env->GetArrayLength(params);
1107
1108    int texCount = _env->GetArrayLength(texNames);
1109    AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
1110    const char ** nameArray = names.c_str();
1111    size_t* sizeArray = names.c_str_len();
1112
1113    LOG_API("nProgramFragmentCreate, con(%p), paramLen(%i)", con, paramLen);
1114
1115    jint ret = (jint)rsProgramFragmentCreate(con, shaderUTF.c_str(), shaderUTF.length(),
1116                                             nameArray, texCount, sizeArray,
1117                                             (uint32_t *)paramPtr, paramLen);
1118
1119    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1120    return ret;
1121}
1122
1123
1124// ---------------------------------------------------------------------------
1125
1126static jint
1127nProgramVertexCreate(JNIEnv *_env, jobject _this, RsContext con, jstring shader,
1128                     jobjectArray texNames, jintArray params)
1129{
1130    AutoJavaStringToUTF8 shaderUTF(_env, shader);
1131    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1132    jint paramLen = _env->GetArrayLength(params);
1133
1134    LOG_API("nProgramVertexCreate, con(%p), paramLen(%i)", con, paramLen);
1135
1136    int texCount = _env->GetArrayLength(texNames);
1137    AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
1138    const char ** nameArray = names.c_str();
1139    size_t* sizeArray = names.c_str_len();
1140
1141    jint ret = (jint)rsProgramVertexCreate(con, shaderUTF.c_str(), shaderUTF.length(),
1142                                           nameArray, texCount, sizeArray,
1143                                           (uint32_t *)paramPtr, paramLen);
1144
1145    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1146    return ret;
1147}
1148
1149// ---------------------------------------------------------------------------
1150
1151static jint
1152nProgramRasterCreate(JNIEnv *_env, jobject _this, RsContext con, jboolean pointSprite, jint cull)
1153{
1154    LOG_API("nProgramRasterCreate, con(%p), pointSprite(%i), cull(%i)", con, pointSprite, cull);
1155    return (jint)rsProgramRasterCreate(con, pointSprite, (RsCullMode)cull);
1156}
1157
1158
1159// ---------------------------------------------------------------------------
1160
1161static void
1162nContextBindRootScript(JNIEnv *_env, jobject _this, RsContext con, jint script)
1163{
1164    LOG_API("nContextBindRootScript, con(%p), script(%p)", con, (RsScript)script);
1165    rsContextBindRootScript(con, (RsScript)script);
1166}
1167
1168static void
1169nContextBindProgramStore(JNIEnv *_env, jobject _this, RsContext con, jint pfs)
1170{
1171    LOG_API("nContextBindProgramStore, con(%p), pfs(%p)", con, (RsProgramStore)pfs);
1172    rsContextBindProgramStore(con, (RsProgramStore)pfs);
1173}
1174
1175static void
1176nContextBindProgramFragment(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1177{
1178    LOG_API("nContextBindProgramFragment, con(%p), pf(%p)", con, (RsProgramFragment)pf);
1179    rsContextBindProgramFragment(con, (RsProgramFragment)pf);
1180}
1181
1182static void
1183nContextBindProgramVertex(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1184{
1185    LOG_API("nContextBindProgramVertex, con(%p), pf(%p)", con, (RsProgramVertex)pf);
1186    rsContextBindProgramVertex(con, (RsProgramVertex)pf);
1187}
1188
1189static void
1190nContextBindProgramRaster(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1191{
1192    LOG_API("nContextBindProgramRaster, con(%p), pf(%p)", con, (RsProgramRaster)pf);
1193    rsContextBindProgramRaster(con, (RsProgramRaster)pf);
1194}
1195
1196
1197// ---------------------------------------------------------------------------
1198
1199static jint
1200nSamplerCreate(JNIEnv *_env, jobject _this, RsContext con, jint magFilter, jint minFilter,
1201               jint wrapS, jint wrapT, jint wrapR, jfloat aniso)
1202{
1203    LOG_API("nSamplerCreate, con(%p)", con);
1204    return (jint)rsSamplerCreate(con,
1205                                 (RsSamplerValue)magFilter,
1206                                 (RsSamplerValue)minFilter,
1207                                 (RsSamplerValue)wrapS,
1208                                 (RsSamplerValue)wrapT,
1209                                 (RsSamplerValue)wrapR,
1210                                 aniso);
1211}
1212
1213// ---------------------------------------------------------------------------
1214
1215//native int  rsnPathCreate(int con, int prim, boolean isStatic, int vtx, int loop, float q);
1216static jint
1217nPathCreate(JNIEnv *_env, jobject _this, RsContext con, jint prim, jboolean isStatic, jint _vtx, jint _loop, jfloat q) {
1218    LOG_API("nPathCreate, con(%p)", con);
1219
1220    int id = (int)rsPathCreate(con, (RsPathPrimitive)prim, isStatic,
1221                               (RsAllocation)_vtx,
1222                               (RsAllocation)_loop, q);
1223    return id;
1224}
1225
1226static jint
1227nMeshCreate(JNIEnv *_env, jobject _this, RsContext con, jintArray _vtx, jintArray _idx, jintArray _prim)
1228{
1229    LOG_API("nMeshCreate, con(%p)", con);
1230
1231    jint vtxLen = _env->GetArrayLength(_vtx);
1232    jint *vtxPtr = _env->GetIntArrayElements(_vtx, NULL);
1233    jint idxLen = _env->GetArrayLength(_idx);
1234    jint *idxPtr = _env->GetIntArrayElements(_idx, NULL);
1235    jint primLen = _env->GetArrayLength(_prim);
1236    jint *primPtr = _env->GetIntArrayElements(_prim, NULL);
1237
1238    int id = (int)rsMeshCreate(con,
1239                               (RsAllocation *)vtxPtr, vtxLen,
1240                               (RsAllocation *)idxPtr, idxLen,
1241                               (uint32_t *)primPtr, primLen);
1242
1243    _env->ReleaseIntArrayElements(_vtx, vtxPtr, 0);
1244    _env->ReleaseIntArrayElements(_idx, idxPtr, 0);
1245    _env->ReleaseIntArrayElements(_prim, primPtr, 0);
1246    return id;
1247}
1248
1249static jint
1250nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, RsContext con, jint mesh)
1251{
1252    LOG_API("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1253    jint vtxCount = 0;
1254    rsaMeshGetVertexBufferCount(con, (RsMesh)mesh, &vtxCount);
1255    return vtxCount;
1256}
1257
1258static jint
1259nMeshGetIndexCount(JNIEnv *_env, jobject _this, RsContext con, jint mesh)
1260{
1261    LOG_API("nMeshGetIndexCount, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1262    jint idxCount = 0;
1263    rsaMeshGetIndexCount(con, (RsMesh)mesh, &idxCount);
1264    return idxCount;
1265}
1266
1267static void
1268nMeshGetVertices(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jintArray _ids, int numVtxIDs)
1269{
1270    LOG_API("nMeshGetVertices, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1271
1272    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
1273    rsaMeshGetVertices(con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
1274
1275    for(jint i = 0; i < numVtxIDs; i ++) {
1276        _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&allocs[i]);
1277    }
1278
1279    free(allocs);
1280}
1281
1282static void
1283nMeshGetIndices(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jintArray _idxIds, jintArray _primitives, int numIndices)
1284{
1285    LOG_API("nMeshGetVertices, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1286
1287    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
1288    uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
1289
1290    rsaMeshGetIndices(con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
1291
1292    for(jint i = 0; i < numIndices; i ++) {
1293        _env->SetIntArrayRegion(_idxIds, i, 1, (const jint*)&allocs[i]);
1294        _env->SetIntArrayRegion(_primitives, i, 1, (const jint*)&prims[i]);
1295    }
1296
1297    free(allocs);
1298    free(prims);
1299}
1300
1301// ---------------------------------------------------------------------------
1302
1303
1304static const char *classPathName = "android/renderscript/RenderScript";
1305
1306static JNINativeMethod methods[] = {
1307{"_nInit",                         "()V",                                     (void*)_nInit },
1308
1309{"nDeviceCreate",                  "()I",                                     (void*)nDeviceCreate },
1310{"nDeviceDestroy",                 "(I)V",                                    (void*)nDeviceDestroy },
1311{"nDeviceSetConfig",               "(III)V",                                  (void*)nDeviceSetConfig },
1312{"nContextGetUserMessage",         "(I[I)I",                                  (void*)nContextGetUserMessage },
1313{"nContextGetErrorMessage",        "(I)Ljava/lang/String;",                   (void*)nContextGetErrorMessage },
1314{"nContextPeekMessage",            "(I[I)I",                                  (void*)nContextPeekMessage },
1315
1316{"nContextInitToClient",           "(I)V",                                    (void*)nContextInitToClient },
1317{"nContextDeinitToClient",         "(I)V",                                    (void*)nContextDeinitToClient },
1318
1319
1320// All methods below are thread protected in java.
1321{"rsnContextCreate",                 "(III)I",                                (void*)nContextCreate },
1322{"rsnContextCreateGL",               "(IIIIIIIIIIIIIFI)I",                    (void*)nContextCreateGL },
1323{"rsnContextFinish",                 "(I)V",                                  (void*)nContextFinish },
1324{"rsnContextSetPriority",            "(II)V",                                 (void*)nContextSetPriority },
1325{"rsnContextSetSurface",             "(IIILandroid/view/Surface;)V",          (void*)nContextSetSurface },
1326{"rsnContextSetSurfaceTexture",      "(IIILandroid/graphics/SurfaceTexture;)V", (void*)nContextSetSurfaceTexture },
1327{"rsnContextDestroy",                "(I)V",                                  (void*)nContextDestroy },
1328{"rsnContextDump",                   "(II)V",                                 (void*)nContextDump },
1329{"rsnContextPause",                  "(I)V",                                  (void*)nContextPause },
1330{"rsnContextResume",                 "(I)V",                                  (void*)nContextResume },
1331{"rsnAssignName",                    "(II[B)V",                               (void*)nAssignName },
1332{"rsnGetName",                       "(II)Ljava/lang/String;",                (void*)nGetName },
1333{"rsnObjDestroy",                    "(II)V",                                 (void*)nObjDestroy },
1334
1335{"rsnFileA3DCreateFromFile",         "(ILjava/lang/String;)I",                (void*)nFileA3DCreateFromFile },
1336{"rsnFileA3DCreateFromAssetStream",  "(II)I",                                 (void*)nFileA3DCreateFromAssetStream },
1337{"rsnFileA3DCreateFromAsset",        "(ILandroid/content/res/AssetManager;Ljava/lang/String;)I",            (void*)nFileA3DCreateFromAsset },
1338{"rsnFileA3DGetNumIndexEntries",     "(II)I",                                 (void*)nFileA3DGetNumIndexEntries },
1339{"rsnFileA3DGetIndexEntries",        "(III[I[Ljava/lang/String;)V",           (void*)nFileA3DGetIndexEntries },
1340{"rsnFileA3DGetEntryByIndex",        "(III)I",                                (void*)nFileA3DGetEntryByIndex },
1341
1342{"rsnFontCreateFromFile",            "(ILjava/lang/String;FI)I",              (void*)nFontCreateFromFile },
1343{"rsnFontCreateFromAssetStream",     "(ILjava/lang/String;FII)I",             (void*)nFontCreateFromAssetStream },
1344{"rsnFontCreateFromAsset",        "(ILandroid/content/res/AssetManager;Ljava/lang/String;FI)I",            (void*)nFontCreateFromAsset },
1345
1346{"rsnElementCreate",                 "(IIIZI)I",                              (void*)nElementCreate },
1347{"rsnElementCreate2",                "(I[I[Ljava/lang/String;[I)I",           (void*)nElementCreate2 },
1348{"rsnElementGetNativeData",          "(II[I)V",                               (void*)nElementGetNativeData },
1349{"rsnElementGetSubElements",         "(II[I[Ljava/lang/String;[I)V",          (void*)nElementGetSubElements },
1350
1351{"rsnTypeCreate",                    "(IIIIIZZ)I",                            (void*)nTypeCreate },
1352{"rsnTypeGetNativeData",             "(II[I)V",                               (void*)nTypeGetNativeData },
1353
1354{"rsnAllocationCreateTyped",         "(IIIII)I",                               (void*)nAllocationCreateTyped },
1355{"rsnAllocationCreateFromBitmap",    "(IIILandroid/graphics/Bitmap;I)I",      (void*)nAllocationCreateFromBitmap },
1356{"rsnAllocationCubeCreateFromBitmap","(IIILandroid/graphics/Bitmap;I)I",      (void*)nAllocationCubeCreateFromBitmap },
1357
1358{"rsnAllocationCopyFromBitmap",      "(IILandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyFromBitmap },
1359{"rsnAllocationCopyToBitmap",        "(IILandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyToBitmap },
1360
1361{"rsnAllocationSyncAll",             "(III)V",                                (void*)nAllocationSyncAll },
1362{"rsnAllocationGetSurfaceTextureID", "(II)I",                                 (void*)nAllocationGetSurfaceTextureID },
1363{"rsnAllocationGetSurfaceTextureID2","(IILandroid/graphics/SurfaceTexture;)V",(void*)nAllocationGetSurfaceTextureID2 },
1364{"rsnAllocationSetSurface",          "(IILandroid/view/Surface;)V",           (void*)nAllocationSetSurface },
1365{"rsnAllocationIoSend",              "(II)V",                                 (void*)nAllocationIoSend },
1366{"rsnAllocationIoReceive",           "(II)V",                                 (void*)nAllocationIoReceive },
1367{"rsnAllocationData1D",              "(IIIII[II)V",                           (void*)nAllocationData1D_i },
1368{"rsnAllocationData1D",              "(IIIII[SI)V",                           (void*)nAllocationData1D_s },
1369{"rsnAllocationData1D",              "(IIIII[BI)V",                           (void*)nAllocationData1D_b },
1370{"rsnAllocationData1D",              "(IIIII[FI)V",                           (void*)nAllocationData1D_f },
1371{"rsnAllocationElementData1D",       "(IIIII[BI)V",                           (void*)nAllocationElementData1D },
1372{"rsnAllocationData2D",              "(IIIIIIII[II)V",                        (void*)nAllocationData2D_i },
1373{"rsnAllocationData2D",              "(IIIIIIII[SI)V",                        (void*)nAllocationData2D_s },
1374{"rsnAllocationData2D",              "(IIIIIIII[BI)V",                        (void*)nAllocationData2D_b },
1375{"rsnAllocationData2D",              "(IIIIIIII[FI)V",                        (void*)nAllocationData2D_f },
1376{"rsnAllocationData2D",              "(IIIIIIIIIIIII)V",                      (void*)nAllocationData2D_alloc },
1377{"rsnAllocationRead",                "(II[I)V",                               (void*)nAllocationRead_i },
1378{"rsnAllocationRead",                "(II[S)V",                               (void*)nAllocationRead_s },
1379{"rsnAllocationRead",                "(II[B)V",                               (void*)nAllocationRead_b },
1380{"rsnAllocationRead",                "(II[F)V",                               (void*)nAllocationRead_f },
1381{"rsnAllocationGetType",             "(II)I",                                 (void*)nAllocationGetType},
1382{"rsnAllocationResize1D",            "(III)V",                                (void*)nAllocationResize1D },
1383{"rsnAllocationResize2D",            "(IIII)V",                               (void*)nAllocationResize2D },
1384{"rsnAllocationGenerateMipmaps",     "(II)V",                                 (void*)nAllocationGenerateMipmaps },
1385
1386{"rsnScriptBindAllocation",          "(IIII)V",                               (void*)nScriptBindAllocation },
1387{"rsnScriptSetTimeZone",             "(II[B)V",                               (void*)nScriptSetTimeZone },
1388{"rsnScriptInvoke",                  "(III)V",                                (void*)nScriptInvoke },
1389{"rsnScriptInvokeV",                 "(III[B)V",                              (void*)nScriptInvokeV },
1390{"rsnScriptForEach",                 "(IIIII)V",                              (void*)nScriptForEach },
1391{"rsnScriptForEach",                 "(IIIII[B)V",                            (void*)nScriptForEachV },
1392{"rsnScriptSetVarI",                 "(IIII)V",                               (void*)nScriptSetVarI },
1393{"rsnScriptSetVarJ",                 "(IIIJ)V",                               (void*)nScriptSetVarJ },
1394{"rsnScriptSetVarF",                 "(IIIF)V",                               (void*)nScriptSetVarF },
1395{"rsnScriptSetVarD",                 "(IIID)V",                               (void*)nScriptSetVarD },
1396{"rsnScriptSetVarV",                 "(III[B)V",                              (void*)nScriptSetVarV },
1397{"rsnScriptSetVarObj",               "(IIII)V",                               (void*)nScriptSetVarObj },
1398
1399{"rsnScriptCCreate",                 "(ILjava/lang/String;Ljava/lang/String;[BI)I",  (void*)nScriptCCreate },
1400
1401{"rsnProgramStoreCreate",            "(IZZZZZZIII)I",                         (void*)nProgramStoreCreate },
1402
1403{"rsnProgramBindConstants",          "(IIII)V",                               (void*)nProgramBindConstants },
1404{"rsnProgramBindTexture",            "(IIII)V",                               (void*)nProgramBindTexture },
1405{"rsnProgramBindSampler",            "(IIII)V",                               (void*)nProgramBindSampler },
1406
1407{"rsnProgramFragmentCreate",         "(ILjava/lang/String;[Ljava/lang/String;[I)I",              (void*)nProgramFragmentCreate },
1408{"rsnProgramRasterCreate",           "(IZI)I",                                (void*)nProgramRasterCreate },
1409{"rsnProgramVertexCreate",           "(ILjava/lang/String;[Ljava/lang/String;[I)I",              (void*)nProgramVertexCreate },
1410
1411{"rsnContextBindRootScript",         "(II)V",                                 (void*)nContextBindRootScript },
1412{"rsnContextBindProgramStore",       "(II)V",                                 (void*)nContextBindProgramStore },
1413{"rsnContextBindProgramFragment",    "(II)V",                                 (void*)nContextBindProgramFragment },
1414{"rsnContextBindProgramVertex",      "(II)V",                                 (void*)nContextBindProgramVertex },
1415{"rsnContextBindProgramRaster",      "(II)V",                                 (void*)nContextBindProgramRaster },
1416
1417{"rsnSamplerCreate",                 "(IIIIIIF)I",                            (void*)nSamplerCreate },
1418
1419{"rsnPathCreate",                    "(IIZIIF)I",                             (void*)nPathCreate },
1420{"rsnMeshCreate",                    "(I[I[I[I)I",                            (void*)nMeshCreate },
1421
1422{"rsnMeshGetVertexBufferCount",      "(II)I",                                 (void*)nMeshGetVertexBufferCount },
1423{"rsnMeshGetIndexCount",             "(II)I",                                 (void*)nMeshGetIndexCount },
1424{"rsnMeshGetVertices",               "(II[II)V",                              (void*)nMeshGetVertices },
1425{"rsnMeshGetIndices",                "(II[I[II)V",                            (void*)nMeshGetIndices },
1426
1427};
1428
1429static int registerFuncs(JNIEnv *_env)
1430{
1431    return android::AndroidRuntime::registerNativeMethods(
1432            _env, classPathName, methods, NELEM(methods));
1433}
1434
1435// ---------------------------------------------------------------------------
1436
1437jint JNI_OnLoad(JavaVM* vm, void* reserved)
1438{
1439    JNIEnv* env = NULL;
1440    jint result = -1;
1441
1442    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
1443        ALOGE("ERROR: GetEnv failed\n");
1444        goto bail;
1445    }
1446    assert(env != NULL);
1447
1448    if (registerFuncs(env) < 0) {
1449        ALOGE("ERROR: MediaPlayer native registration failed\n");
1450        goto bail;
1451    }
1452
1453    /* success -- return valid version number */
1454    result = JNI_VERSION_1_4;
1455
1456bail:
1457    return result;
1458}
1459