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 <android/bitmap.h>
25#include <android/log.h>
26#include "jni.h"
27#include <rs.h>
28#include <rsEnv.h>
29
30//#define LOG_API ALOG
31#define LOG_API(...)
32
33#define NELEM(m) (sizeof(m) / sizeof((m)[0]))
34
35class AutoJavaStringToUTF8 {
36public:
37    AutoJavaStringToUTF8(JNIEnv* env, jstring str) : fEnv(env), fJStr(str) {
38        fCStr = env->GetStringUTFChars(str, NULL);
39        fLength = env->GetStringUTFLength(str);
40    }
41    ~AutoJavaStringToUTF8() {
42        fEnv->ReleaseStringUTFChars(fJStr, fCStr);
43    }
44    const char* c_str() const { return fCStr; }
45    jsize length() const { return fLength; }
46
47private:
48    JNIEnv*     fEnv;
49    jstring     fJStr;
50    const char* fCStr;
51    jsize       fLength;
52};
53
54class AutoJavaStringArrayToUTF8 {
55public:
56    AutoJavaStringArrayToUTF8(JNIEnv* env, jobjectArray strings, jsize stringsLength)
57    : mEnv(env), mStrings(strings), mStringsLength(stringsLength) {
58        mCStrings = NULL;
59        mSizeArray = NULL;
60        if (stringsLength > 0) {
61            mCStrings = (const char **)calloc(stringsLength, sizeof(char *));
62            mSizeArray = (size_t*)calloc(stringsLength, sizeof(size_t));
63            for (jsize ct = 0; ct < stringsLength; ct ++) {
64                jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
65                mCStrings[ct] = mEnv->GetStringUTFChars(s, NULL);
66                mSizeArray[ct] = mEnv->GetStringUTFLength(s);
67            }
68        }
69    }
70    ~AutoJavaStringArrayToUTF8() {
71        for (jsize ct=0; ct < mStringsLength; ct++) {
72            jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
73            mEnv->ReleaseStringUTFChars(s, mCStrings[ct]);
74        }
75        free(mCStrings);
76        free(mSizeArray);
77    }
78    const char **c_str() const { return mCStrings; }
79    size_t *c_str_len() const { return mSizeArray; }
80    jsize length() const { return mStringsLength; }
81
82private:
83    JNIEnv      *mEnv;
84    jobjectArray mStrings;
85    const char **mCStrings;
86    size_t      *mSizeArray;
87    jsize        mStringsLength;
88};
89
90// ---------------------------------------------------------------------------
91
92static void
93nContextFinish(JNIEnv *_env, jobject _this, RsContext con)
94{
95    LOG_API("nContextFinish, con(%p)", con);
96    rsContextFinish(con);
97}
98
99static void
100nObjDestroy(JNIEnv *_env, jobject _this, RsContext con, jint obj)
101{
102    LOG_API("nObjDestroy, con(%p) obj(%p)", con, (void *)obj);
103    rsObjDestroy(con, (void *)obj);
104}
105
106// ---------------------------------------------------------------------------
107
108static jint
109nDeviceCreate(JNIEnv *_env, jobject _this)
110{
111    LOG_API("nDeviceCreate");
112    return (jint)rsDeviceCreate();
113}
114
115static void
116nDeviceDestroy(JNIEnv *_env, jobject _this, jint dev)
117{
118    LOG_API("nDeviceDestroy");
119    return rsDeviceDestroy((RsDevice)dev);
120}
121
122static void
123nDeviceSetConfig(JNIEnv *_env, jobject _this, jint dev, jint p, jint value)
124{
125    LOG_API("nDeviceSetConfig  dev(%p), param(%i), value(%i)", (void *)dev, p, value);
126    return rsDeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value);
127}
128
129static jint
130nContextCreate(JNIEnv *_env, jobject _this, jint dev, jint ver, jint sdkVer, jint ct)
131{
132    LOG_API("nContextCreate");
133    return (jint)rsContextCreate((RsDevice)dev, ver, sdkVer, (RsContextType)ct, 0);
134}
135
136
137static void
138nContextSetPriority(JNIEnv *_env, jobject _this, RsContext con, jint p)
139{
140    LOG_API("ContextSetPriority, con(%p), priority(%i)", con, p);
141    rsContextSetPriority(con, p);
142}
143
144
145
146static void
147nContextDestroy(JNIEnv *_env, jobject _this, RsContext con)
148{
149    LOG_API("nContextDestroy, con(%p)", con);
150    rsContextDestroy(con);
151}
152
153static void
154nContextDump(JNIEnv *_env, jobject _this, RsContext con, jint bits)
155{
156    LOG_API("nContextDump, con(%p)  bits(%i)", (RsContext)con, bits);
157    rsContextDump((RsContext)con, bits);
158}
159
160
161static jstring
162nContextGetErrorMessage(JNIEnv *_env, jobject _this, RsContext con)
163{
164    LOG_API("nContextGetErrorMessage, con(%p)", con);
165    char buf[1024];
166
167    size_t receiveLen;
168    uint32_t subID;
169    int id = rsContextGetMessage(con,
170                                 buf, sizeof(buf),
171                                 &receiveLen, sizeof(receiveLen),
172                                 &subID, sizeof(subID));
173    if (!id && receiveLen) {
174        __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG,
175            "message receive buffer too small.  %zu", receiveLen);
176    }
177    return _env->NewStringUTF(buf);
178}
179
180static jint
181nContextGetUserMessage(JNIEnv *_env, jobject _this, RsContext con, jintArray data)
182{
183    jint len = _env->GetArrayLength(data);
184    LOG_API("nContextGetMessage, con(%p), len(%i)", con, len);
185    jint *ptr = _env->GetIntArrayElements(data, NULL);
186    size_t receiveLen;
187    uint32_t subID;
188    int id = rsContextGetMessage(con,
189                                 ptr, len * 4,
190                                 &receiveLen, sizeof(receiveLen),
191                                 &subID, sizeof(subID));
192    if (!id && receiveLen) {
193        __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG,
194            "message receive buffer too small.  %zu", receiveLen);
195    }
196    _env->ReleaseIntArrayElements(data, ptr, 0);
197    return id;
198}
199
200static jint
201nContextPeekMessage(JNIEnv *_env, jobject _this, RsContext con, jintArray auxData)
202{
203    LOG_API("nContextPeekMessage, con(%p)", con);
204    jint *auxDataPtr = _env->GetIntArrayElements(auxData, NULL);
205    size_t receiveLen;
206    uint32_t subID;
207    int id = rsContextPeekMessage(con, &receiveLen, sizeof(receiveLen),
208                                  &subID, sizeof(subID));
209    auxDataPtr[0] = (jint)subID;
210    auxDataPtr[1] = (jint)receiveLen;
211    _env->ReleaseIntArrayElements(auxData, auxDataPtr, 0);
212    return id;
213}
214
215static void nContextInitToClient(JNIEnv *_env, jobject _this, RsContext con)
216{
217    LOG_API("nContextInitToClient, con(%p)", con);
218    rsContextInitToClient(con);
219}
220
221static void nContextDeinitToClient(JNIEnv *_env, jobject _this, RsContext con)
222{
223    LOG_API("nContextDeinitToClient, con(%p)", con);
224    rsContextDeinitToClient(con);
225}
226
227static void
228nContextSendMessage(JNIEnv *_env, jobject _this, RsContext con, jint id, jintArray data)
229{
230    jint *ptr = NULL;
231    jint len = 0;
232    if (data) {
233        len = _env->GetArrayLength(data);
234        jint *ptr = _env->GetIntArrayElements(data, NULL);
235    }
236    LOG_API("nContextSendMessage, con(%p), id(%i), len(%i)", con, id, len);
237    rsContextSendMessage(con, id, (const uint8_t *)ptr, len * sizeof(int));
238    if (data) {
239        _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
240    }
241}
242
243
244
245static jint
246nElementCreate(JNIEnv *_env, jobject _this, RsContext con, jint type, jint kind, jboolean norm, jint size)
247{
248    LOG_API("nElementCreate, con(%p), type(%i), kind(%i), norm(%i), size(%i)", con, type, kind, norm, size);
249    return (jint)rsElementCreate(con, (RsDataType)type, (RsDataKind)kind, norm, size);
250}
251
252static jint
253nElementCreate2(JNIEnv *_env, jobject _this, RsContext con,
254                jintArray _ids, jobjectArray _names, jintArray _arraySizes)
255{
256    int fieldCount = _env->GetArrayLength(_ids);
257    LOG_API("nElementCreate2, con(%p)", con);
258
259    jint *ids = _env->GetIntArrayElements(_ids, NULL);
260    jint *arraySizes = _env->GetIntArrayElements(_arraySizes, NULL);
261
262    AutoJavaStringArrayToUTF8 names(_env, _names, fieldCount);
263
264    const char **nameArray = names.c_str();
265    size_t *sizeArray = names.c_str_len();
266
267    jint id = (jint)rsElementCreate2(con,
268                                     (RsElement *)ids, fieldCount,
269                                     nameArray, fieldCount * sizeof(size_t),  sizeArray,
270                                     (const uint32_t *)arraySizes, fieldCount);
271
272    _env->ReleaseIntArrayElements(_ids, ids, JNI_ABORT);
273    _env->ReleaseIntArrayElements(_arraySizes, arraySizes, JNI_ABORT);
274    return (jint)id;
275}
276
277
278
279static void
280nElementGetSubElements(JNIEnv *_env, jobject _this, RsContext con, jint id,
281                       jintArray _IDs,
282                       jobjectArray _names,
283                       jintArray _arraySizes)
284{
285    int dataSize = _env->GetArrayLength(_IDs);
286    LOG_API("nElementGetSubElements, con(%p)", con);
287
288    uint32_t *ids = (uint32_t *)malloc((uint32_t)dataSize * sizeof(uint32_t));
289    const char **names = (const char **)malloc((uint32_t)dataSize * sizeof(const char *));
290    uint32_t *arraySizes = (uint32_t *)malloc((uint32_t)dataSize * sizeof(uint32_t));
291
292    rsaElementGetSubElements(con, (RsElement)id, ids, names, arraySizes, (uint32_t)dataSize);
293
294    for(jint i = 0; i < dataSize; i++) {
295        _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
296        _env->SetIntArrayRegion(_IDs, i, 1, (const jint*)&ids[i]);
297        _env->SetIntArrayRegion(_arraySizes, i, 1, (const jint*)&arraySizes[i]);
298    }
299
300    free(ids);
301    free(names);
302    free(arraySizes);
303}
304
305// -----------------------------------
306
307static int
308nTypeCreate(JNIEnv *_env, jobject _this, RsContext con, RsElement eid,
309            jint dimx, jint dimy, jint dimz, jboolean mips, jboolean faces, jint yuv)
310{
311    LOG_API("nTypeCreate, con(%p) eid(%p), x(%i), y(%i), z(%i), mips(%i), faces(%i), yuv(%i)",
312            con, eid, dimx, dimy, dimz, mips, faces, yuv);
313
314    jint id = (jint)rsTypeCreate(con, (RsElement)eid, dimx, dimy, dimz, mips, faces, yuv);
315    return (jint)id;
316}
317
318// -----------------------------------
319
320static jint
321nAllocationCreateTyped(JNIEnv *_env, jobject _this, RsContext con, jint type, jint mips, jint usage, jint pointer)
322{
323    LOG_API("nAllocationCreateTyped, con(%p), type(%p), mip(%i), usage(%i), ptr(%p)", con, (RsElement)type, mips, usage, (void *)pointer);
324    return (jint) rsAllocationCreateTyped(con, (RsType)type, (RsAllocationMipmapControl)mips, (uint32_t)usage, (uint32_t)pointer);
325}
326
327static void
328nAllocationSyncAll(JNIEnv *_env, jobject _this, RsContext con, jint a, jint bits)
329{
330    LOG_API("nAllocationSyncAll, con(%p), a(%p), bits(0x%08x)", con, (RsAllocation)a, bits);
331    rsAllocationSyncAll(con, (RsAllocation)a, (RsAllocationUsageType)bits);
332}
333
334static void
335nAllocationGenerateMipmaps(JNIEnv *_env, jobject _this, RsContext con, jint alloc)
336{
337    LOG_API("nAllocationGenerateMipmaps, con(%p), a(%p)", con, (RsAllocation)alloc);
338    rsAllocationGenerateMipmaps(con, (RsAllocation)alloc);
339}
340
341static size_t GetBitmapSize(JNIEnv *env, jobject jbitmap) {
342    AndroidBitmapInfo info;
343    memset(&info, 0, sizeof(info));
344    AndroidBitmap_getInfo(env, jbitmap, &info);
345    size_t s = info.width * info.height;
346    switch (info.format) {
347        case ANDROID_BITMAP_FORMAT_RGBA_8888: s *= 4; break;
348        case ANDROID_BITMAP_FORMAT_RGB_565: s *= 2; break;
349        case ANDROID_BITMAP_FORMAT_RGBA_4444: s *= 2; break;
350    }
351    return s;
352}
353
354static int
355nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, RsContext con, jint type, jint mip, jobject jbitmap, jint usage)
356{
357    jint id = 0;
358    void *pixels = NULL;
359    AndroidBitmap_lockPixels(_env, jbitmap, &pixels);
360
361    if (pixels != NULL) {
362        id = (jint)rsAllocationCreateFromBitmap(con,
363                                                (RsType)type, (RsAllocationMipmapControl)mip,
364                                                pixels, GetBitmapSize(_env, jbitmap), usage);
365        AndroidBitmap_unlockPixels(_env, jbitmap);
366    }
367    return id;
368}
369
370static int
371nAllocationCreateBitmapBackedAllocation(JNIEnv *_env, jobject _this, RsContext con, jint type, jint mip, jobject jbitmap, jint usage)
372{
373    jint id = 0;
374    void *pixels = NULL;
375    AndroidBitmap_lockPixels(_env, jbitmap, &pixels);
376
377    if (pixels != NULL) {
378        id = (jint)rsAllocationCreateTyped(con,
379                                          (RsType)type, (RsAllocationMipmapControl)mip,
380                                          (uint32_t)usage, (uintptr_t)pixels);
381        AndroidBitmap_unlockPixels(_env, jbitmap);
382    }
383    return id;
384}
385
386static int
387nAllocationCubeCreateFromBitmap(JNIEnv *_env, jobject _this, RsContext con, jint type, jint mip, jobject jbitmap, jint usage)
388{
389    void *pixels = NULL;
390    AndroidBitmap_lockPixels(_env, jbitmap, &pixels);
391
392    jint id = 0;
393    if (pixels != NULL) {
394        id = (jint)rsAllocationCubeCreateFromBitmap(con,
395                                                    (RsType)type, (RsAllocationMipmapControl)mip,
396                                                    pixels, GetBitmapSize(_env, jbitmap), usage);
397        AndroidBitmap_unlockPixels(_env, jbitmap);
398    }
399    return id;
400}
401
402static void
403nAllocationCopyFromBitmap(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jobject jbitmap)
404{
405    AndroidBitmapInfo info;
406    memset(&info, 0, sizeof(info));
407    AndroidBitmap_getInfo(_env, jbitmap, &info);
408
409    void *pixels = NULL;
410    AndroidBitmap_lockPixels(_env, jbitmap, &pixels);
411
412    if (pixels != NULL) {
413        rsAllocation2DData(con, (RsAllocation)alloc, 0, 0,
414                           0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
415                           info.width, info.height, pixels, GetBitmapSize(_env, jbitmap), 0);
416        AndroidBitmap_unlockPixels(_env, jbitmap);
417    }
418}
419
420static void
421nAllocationCopyToBitmap(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jobject jbitmap)
422{
423    AndroidBitmapInfo info;
424    memset(&info, 0, sizeof(info));
425    AndroidBitmap_getInfo(_env, jbitmap, &info);
426
427    void *pixels = NULL;
428    AndroidBitmap_lockPixels(_env, jbitmap, &pixels);
429
430    if (pixels != NULL) {
431        rsAllocationCopyToBitmap(con, (RsAllocation)alloc, pixels, GetBitmapSize(_env, jbitmap));
432        AndroidBitmap_unlockPixels(_env, jbitmap);
433    }
434    //bitmap.notifyPixelsChanged();
435}
436
437
438static void
439nAllocationData1D_i(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint lod, jint count, jintArray data, int sizeBytes)
440{
441    jint len = _env->GetArrayLength(data);
442    LOG_API("nAllocation1DData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
443    jint *ptr = _env->GetIntArrayElements(data, NULL);
444    rsAllocation1DData(con, (RsAllocation)alloc, offset, lod, count, ptr, sizeBytes);
445    _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
446}
447
448static void
449nAllocationData1D_s(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint lod, jint count, jshortArray data, int sizeBytes)
450{
451    jint len = _env->GetArrayLength(data);
452    LOG_API("nAllocation1DData_s, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
453    jshort *ptr = _env->GetShortArrayElements(data, NULL);
454    rsAllocation1DData(con, (RsAllocation)alloc, offset, lod, count, ptr, sizeBytes);
455    _env->ReleaseShortArrayElements(data, ptr, JNI_ABORT);
456}
457
458static void
459nAllocationData1D_b(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint lod, jint count, jbyteArray data, int sizeBytes)
460{
461    jint len = _env->GetArrayLength(data);
462    LOG_API("nAllocation1DData_b, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
463    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
464    rsAllocation1DData(con, (RsAllocation)alloc, offset, lod, count, ptr, sizeBytes);
465    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
466}
467
468static void
469nAllocationData1D_f(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint lod, jint count, jfloatArray data, int sizeBytes)
470{
471    jint len = _env->GetArrayLength(data);
472    LOG_API("nAllocation1DData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
473    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
474    rsAllocation1DData(con, (RsAllocation)alloc, offset, lod, count, ptr, sizeBytes);
475    _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
476}
477
478static void
479//    native void rsnAllocationElementData1D(int con, int id, int xoff, int compIdx, byte[] d, int sizeBytes);
480nAllocationElementData1D(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint lod, jint compIdx, jbyteArray data, int sizeBytes)
481{
482    jint len = _env->GetArrayLength(data);
483    LOG_API("nAllocationElementData1D, con(%p), alloc(%p), offset(%i), comp(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, compIdx, len, sizeBytes);
484    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
485    rsAllocation1DElementData(con, (RsAllocation)alloc, offset, lod, ptr, sizeBytes, compIdx);
486    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
487}
488
489static void
490nAllocationData2D_s(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint lod, jint face,
491                    jint w, jint h, jshortArray data, int sizeBytes)
492{
493    jint len = _env->GetArrayLength(data);
494    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);
495    jshort *ptr = _env->GetShortArrayElements(data, NULL);
496    rsAllocation2DData(con, (RsAllocation)alloc, xoff, yoff, lod, (RsAllocationCubemapFace)face, w, h, ptr, sizeBytes, 0);
497    _env->ReleaseShortArrayElements(data, ptr, JNI_ABORT);
498}
499
500static void
501nAllocationData2D_b(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint lod, jint face,
502                    jint w, jint h, jbyteArray data, int sizeBytes)
503{
504    jint len = _env->GetArrayLength(data);
505    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);
506    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
507    rsAllocation2DData(con, (RsAllocation)alloc, xoff, yoff, lod, (RsAllocationCubemapFace)face, w, h, ptr, sizeBytes, 0);
508    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
509}
510
511static void
512nAllocationData2D_i(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint lod, jint face,
513                    jint w, jint h, jintArray data, int sizeBytes)
514{
515    jint len = _env->GetArrayLength(data);
516    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);
517    jint *ptr = _env->GetIntArrayElements(data, NULL);
518    rsAllocation2DData(con, (RsAllocation)alloc, xoff, yoff, lod, (RsAllocationCubemapFace)face, w, h, ptr, sizeBytes, 0);
519    _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
520}
521
522static void
523nAllocationData2D_f(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint lod, jint face,
524                    jint w, jint h, jfloatArray data, int sizeBytes)
525{
526    jint len = _env->GetArrayLength(data);
527    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);
528    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
529    rsAllocation2DData(con, (RsAllocation)alloc, xoff, yoff, lod, (RsAllocationCubemapFace)face, w, h, ptr, sizeBytes, 0);
530    _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
531}
532
533static void
534nAllocationData2D_alloc(JNIEnv *_env, jobject _this, RsContext con,
535                        jint dstAlloc, jint dstXoff, jint dstYoff,
536                        jint dstMip, jint dstFace,
537                        jint width, jint height,
538                        jint srcAlloc, jint srcXoff, jint srcYoff,
539                        jint srcMip, jint srcFace)
540{
541    LOG_API("nAllocation2DData_s, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
542            " dstMip(%i), dstFace(%i), width(%i), height(%i),"
543            " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i), srcFace(%i)",
544            con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip, dstFace,
545            width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
546
547    rsAllocationCopy2DRange(con,
548                            (RsAllocation)dstAlloc,
549                            dstXoff, dstYoff,
550                            dstMip, dstFace,
551                            width, height,
552                            (RsAllocation)srcAlloc,
553                            srcXoff, srcYoff,
554                            srcMip, srcFace);
555}
556
557static void
558nAllocationData3D_s(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint zoff, jint lod,
559                    jint w, jint h, jint d, jshortArray data, int sizeBytes)
560{
561    jint len = _env->GetArrayLength(data);
562    LOG_API("nAllocation3DData_s, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)", con, (RsAllocation)alloc, xoff, yoff, zoff, w, h, d, len);
563    jshort *ptr = _env->GetShortArrayElements(data, NULL);
564    rsAllocation3DData(con, (RsAllocation)alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
565    _env->ReleaseShortArrayElements(data, ptr, JNI_ABORT);
566}
567
568static void
569nAllocationData3D_b(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint zoff, jint lod,
570                    jint w, jint h, jint d, jbyteArray data, int sizeBytes)
571{
572    jint len = _env->GetArrayLength(data);
573    LOG_API("nAllocation3DData_b, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)", con, (RsAllocation)alloc, xoff, yoff, zoff, w, h, d, len);
574    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
575    rsAllocation3DData(con, (RsAllocation)alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
576    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
577}
578
579static void
580nAllocationData3D_i(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint zoff, jint lod,
581                    jint w, jint h, jint d, jintArray data, int sizeBytes)
582{
583    jint len = _env->GetArrayLength(data);
584    LOG_API("nAllocation3DData_i, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)", con, (RsAllocation)alloc, xoff, yoff, zoff, w, h, d, len);
585    jint *ptr = _env->GetIntArrayElements(data, NULL);
586    rsAllocation3DData(con, (RsAllocation)alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
587    _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
588}
589
590static void
591nAllocationData3D_f(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint zoff, jint lod,
592                    jint w, jint h, jint d, jfloatArray data, int sizeBytes)
593{
594    jint len = _env->GetArrayLength(data);
595    LOG_API("nAllocation3DData_f, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)", con, (RsAllocation)alloc, xoff, yoff, zoff, w, h, d, len);
596    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
597    rsAllocation3DData(con, (RsAllocation)alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
598    _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
599}
600
601static void
602nAllocationData3D_alloc(JNIEnv *_env, jobject _this, RsContext con,
603                        jint dstAlloc, jint dstXoff, jint dstYoff, jint dstZoff,
604                        jint dstMip,
605                        jint width, jint height, jint depth,
606                        jint srcAlloc, jint srcXoff, jint srcYoff, jint srcZoff,
607                        jint srcMip)
608{
609    LOG_API("nAllocationData3D_alloc, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
610            " dstMip(%i), width(%i), height(%i),"
611            " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i)",
612            con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip, dstFace,
613            width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
614
615    rsAllocationCopy3DRange(con,
616                            (RsAllocation)dstAlloc,
617                            dstXoff, dstYoff, dstZoff, dstMip,
618                            width, height, depth,
619                            (RsAllocation)srcAlloc,
620                            srcXoff, srcYoff, srcZoff, srcMip);
621}
622
623static void
624nAllocationRead_i(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jintArray data)
625{
626    jint len = _env->GetArrayLength(data);
627    LOG_API("nAllocationRead_i, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
628    jint *ptr = _env->GetIntArrayElements(data, NULL);
629    jsize length = _env->GetArrayLength(data);
630    rsAllocationRead(con, (RsAllocation)alloc, ptr, length * sizeof(int));
631    _env->ReleaseIntArrayElements(data, ptr, 0);
632}
633
634static void
635nAllocationRead_s(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jshortArray data)
636{
637    jint len = _env->GetArrayLength(data);
638    LOG_API("nAllocationRead_i, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
639    jshort *ptr = _env->GetShortArrayElements(data, NULL);
640    jsize length = _env->GetArrayLength(data);
641    rsAllocationRead(con, (RsAllocation)alloc, ptr, length * sizeof(short));
642    _env->ReleaseShortArrayElements(data, ptr, 0);
643}
644
645static void
646nAllocationRead_b(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jbyteArray data)
647{
648    jint len = _env->GetArrayLength(data);
649    LOG_API("nAllocationRead_i, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
650    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
651    jsize length = _env->GetArrayLength(data);
652    rsAllocationRead(con, (RsAllocation)alloc, ptr, length * sizeof(char));
653    _env->ReleaseByteArrayElements(data, ptr, 0);
654}
655
656static void
657nAllocationRead_f(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jfloatArray data)
658{
659    jint len = _env->GetArrayLength(data);
660    LOG_API("nAllocationRead_f, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
661    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
662    jsize length = _env->GetArrayLength(data);
663    rsAllocationRead(con, (RsAllocation)alloc, ptr, length * sizeof(float));
664    _env->ReleaseFloatArrayElements(data, ptr, 0);
665}
666
667static jint
668nAllocationGetType(JNIEnv *_env, jobject _this, RsContext con, jint a)
669{
670    LOG_API("nAllocationGetType, con(%p), a(%p)", con, (RsAllocation)a);
671    return (jint) rsaAllocationGetType(con, (RsAllocation)a);
672}
673
674static void
675nAllocationResize1D(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint dimX)
676{
677    LOG_API("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", con, (RsAllocation)alloc, dimX);
678    rsAllocationResize1D(con, (RsAllocation)alloc, dimX);
679}
680
681// -----------------------------------
682
683static void
684nScriptBindAllocation(JNIEnv *_env, jobject _this, RsContext con, jint script, jint alloc, jint slot)
685{
686    LOG_API("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", con, (RsScript)script, (RsAllocation)alloc, slot);
687    rsScriptBindAllocation(con, (RsScript)script, (RsAllocation)alloc, slot);
688}
689
690static void
691nScriptSetVarI(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jint val)
692{
693    LOG_API("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", con, (void *)script, slot, val);
694    rsScriptSetVarI(con, (RsScript)script, slot, val);
695}
696
697static void
698nScriptSetVarObj(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jint val)
699{
700    LOG_API("nScriptSetVarObj, con(%p), s(%p), slot(%i), val(%i)", con, (void *)script, slot, val);
701    rsScriptSetVarObj(con, (RsScript)script, slot, (RsObjectBase)val);
702}
703
704static void
705nScriptSetVarJ(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jlong val)
706{
707    LOG_API("nScriptSetVarJ, con(%p), s(%p), slot(%i), val(%lli)", con, (void *)script, slot, val);
708    rsScriptSetVarJ(con, (RsScript)script, slot, val);
709}
710
711static void
712nScriptSetVarF(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, float val)
713{
714    LOG_API("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", con, (void *)script, slot, val);
715    rsScriptSetVarF(con, (RsScript)script, slot, val);
716}
717
718static void
719nScriptSetVarD(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, double val)
720{
721    LOG_API("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", con, (void *)script, slot, val);
722    rsScriptSetVarD(con, (RsScript)script, slot, val);
723}
724
725static void
726nScriptSetVarV(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jbyteArray data)
727{
728    LOG_API("nScriptSetVarV, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
729    jint len = _env->GetArrayLength(data);
730    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
731    rsScriptSetVarV(con, (RsScript)script, slot, ptr, len);
732    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
733}
734
735static void
736nScriptSetVarVE(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jbyteArray data, jint elem, jintArray dims)
737{
738    LOG_API("nScriptSetVarVE, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
739    jint len = _env->GetArrayLength(data);
740    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
741    jint dimsLen = _env->GetArrayLength(dims) * sizeof(int);
742    jint *dimsPtr = _env->GetIntArrayElements(dims, NULL);
743    rsScriptSetVarVE(con, (RsScript)script, slot, ptr, len, (RsElement)elem,
744                     (const size_t*) dimsPtr, dimsLen);
745    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
746    _env->ReleaseIntArrayElements(dims, dimsPtr, JNI_ABORT);
747}
748
749
750static void
751nScriptSetTimeZone(JNIEnv *_env, jobject _this, RsContext con, jint script, jbyteArray timeZone)
752{
753    LOG_API("nScriptCSetTimeZone, con(%p), s(%p), timeZone(%s)", con, (void *)script, (const char *)timeZone);
754
755    jint length = _env->GetArrayLength(timeZone);
756    jbyte* timeZone_ptr;
757    timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
758
759    rsScriptSetTimeZone(con, (RsScript)script, (const char *)timeZone_ptr, length);
760
761    if (timeZone_ptr) {
762        _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
763    }
764}
765
766static void
767nScriptInvoke(JNIEnv *_env, jobject _this, RsContext con, jint obj, jint slot)
768{
769    LOG_API("nScriptInvoke, con(%p), script(%p)", con, (void *)obj);
770    rsScriptInvoke(con, (RsScript)obj, slot);
771}
772
773static void
774nScriptInvokeV(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jbyteArray data)
775{
776    LOG_API("nScriptInvokeV, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
777    jint len = _env->GetArrayLength(data);
778    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
779    rsScriptInvokeV(con, (RsScript)script, slot, ptr, len);
780    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
781}
782
783static void
784nScriptForEach(JNIEnv *_env, jobject _this, RsContext con,
785               jint script, jint slot, jint ain, jint aout)
786{
787    LOG_API("nScriptForEach, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
788    rsScriptForEach(con, (RsScript)script, slot, (RsAllocation)ain, (RsAllocation)aout, NULL, 0, NULL, 0);
789}
790static void
791nScriptForEachV(JNIEnv *_env, jobject _this, RsContext con,
792                jint script, jint slot, jint ain, jint aout, jbyteArray params)
793{
794    LOG_API("nScriptForEach, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
795    jint len = _env->GetArrayLength(params);
796    jbyte *ptr = _env->GetByteArrayElements(params, NULL);
797    rsScriptForEach(con, (RsScript)script, slot, (RsAllocation)ain, (RsAllocation)aout, ptr, len, NULL, 0);
798    _env->ReleaseByteArrayElements(params, ptr, JNI_ABORT);
799}
800
801static void
802nScriptForEachClipped(JNIEnv *_env, jobject _this, RsContext con,
803                      jint script, jint slot, jint ain, jint aout,
804                      jint xstart, jint xend,
805                      jint ystart, jint yend, jint zstart, jint zend)
806{
807    LOG_API("nScriptForEachClipped, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
808    RsScriptCall sc;
809    sc.xStart = xstart;
810    sc.xEnd = xend;
811    sc.yStart = ystart;
812    sc.yEnd = yend;
813    sc.zStart = zstart;
814    sc.zEnd = zend;
815    sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
816    sc.arrayStart = 0;
817    sc.arrayEnd = 0;
818    rsScriptForEach(con, (RsScript)script, slot, (RsAllocation)ain, (RsAllocation)aout, NULL, 0, &sc, sizeof(sc));
819}
820
821static void
822nScriptForEachClippedV(JNIEnv *_env, jobject _this, RsContext con,
823                       jint script, jint slot, jint ain, jint aout,
824                       jbyteArray params, jint xstart, jint xend,
825                       jint ystart, jint yend, jint zstart, jint zend)
826{
827    LOG_API("nScriptForEachClipped, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
828    jint len = _env->GetArrayLength(params);
829    jbyte *ptr = _env->GetByteArrayElements(params, NULL);
830    RsScriptCall sc;
831    sc.xStart = xstart;
832    sc.xEnd = xend;
833    sc.yStart = ystart;
834    sc.yEnd = yend;
835    sc.zStart = zstart;
836    sc.zEnd = zend;
837    sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
838    sc.arrayStart = 0;
839    sc.arrayEnd = 0;
840    rsScriptForEach(con, (RsScript)script, slot, (RsAllocation)ain, (RsAllocation)aout, ptr, len, &sc, sizeof(sc));
841    _env->ReleaseByteArrayElements(params, ptr, JNI_ABORT);
842}
843
844// -----------------------------------
845
846static jint
847nScriptCCreate(JNIEnv *_env, jobject _this, RsContext con,
848               jstring resName, jstring cacheDir,
849               jbyteArray scriptRef, jint length)
850{
851    LOG_API("nScriptCCreate, con(%p)", con);
852
853    AutoJavaStringToUTF8 resNameUTF(_env, resName);
854    AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
855    jint ret = 0;
856    jbyte* script_ptr = NULL;
857    jint _exception = 0;
858    jint remaining;
859    if (!scriptRef) {
860        _exception = 1;
861        //jniThrowException(_env, "java/lang/IllegalArgumentException", "script == null");
862        goto exit;
863    }
864    if (length < 0) {
865        _exception = 1;
866        //jniThrowException(_env, "java/lang/IllegalArgumentException", "length < 0");
867        goto exit;
868    }
869    remaining = _env->GetArrayLength(scriptRef);
870    if (remaining < length) {
871        _exception = 1;
872        //jniThrowException(_env, "java/lang/IllegalArgumentException",
873        //        "length > script.length - offset");
874        goto exit;
875    }
876    script_ptr = (jbyte *)
877        _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
878
879    //rsScriptCSetText(con, (const char *)script_ptr, length);
880
881    ret = (jint)rsScriptCCreate(con,
882                                resNameUTF.c_str(), resNameUTF.length(),
883                                cacheDirUTF.c_str(), cacheDirUTF.length(),
884                                (const char *)script_ptr, length);
885
886exit:
887    if (script_ptr) {
888        _env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr,
889                _exception ? JNI_ABORT: 0);
890    }
891
892    return ret;
893}
894
895static jint
896nScriptIntrinsicCreate(JNIEnv *_env, jobject _this, RsContext con, jint id, jint eid)
897{
898    LOG_API("nScriptIntrinsicCreate, con(%p) id(%i) element(%p)", con, id, (void *)eid);
899    return (jint)rsScriptIntrinsicCreate(con, id, (RsElement)eid);
900}
901
902static jint
903nScriptKernelIDCreate(JNIEnv *_env, jobject _this, RsContext con, jint sid, jint slot, jint sig)
904{
905    LOG_API("nScriptKernelIDCreate, con(%p) script(%p), slot(%i), sig(%i)", con, (void *)sid, slot, sig);
906    return (jint)rsScriptKernelIDCreate(con, (RsScript)sid, slot, sig);
907}
908
909static jint
910nScriptFieldIDCreate(JNIEnv *_env, jobject _this, RsContext con, jint sid, jint slot)
911{
912    LOG_API("nScriptFieldIDCreate, con(%p) script(%p), slot(%i)", con, (void *)sid, slot);
913    return (jint)rsScriptFieldIDCreate(con, (RsScript)sid, slot);
914}
915
916static jint
917nScriptGroupCreate(JNIEnv *_env, jobject _this, RsContext con, jintArray _kernels, jintArray _src,
918    jintArray _dstk, jintArray _dstf, jintArray _types)
919{
920    LOG_API("nScriptGroupCreate, con(%p)", con);
921
922    jint kernelsLen = _env->GetArrayLength(_kernels) * sizeof(int);
923    jint *kernelsPtr = _env->GetIntArrayElements(_kernels, NULL);
924    jint srcLen = _env->GetArrayLength(_src) * sizeof(int);
925    jint *srcPtr = _env->GetIntArrayElements(_src, NULL);
926    jint dstkLen = _env->GetArrayLength(_dstk) * sizeof(int);
927    jint *dstkPtr = _env->GetIntArrayElements(_dstk, NULL);
928    jint dstfLen = _env->GetArrayLength(_dstf) * sizeof(int);
929    jint *dstfPtr = _env->GetIntArrayElements(_dstf, NULL);
930    jint typesLen = _env->GetArrayLength(_types) * sizeof(int);
931    jint *typesPtr = _env->GetIntArrayElements(_types, NULL);
932
933    int id = (int)rsScriptGroupCreate(con,
934                               (RsScriptKernelID *)kernelsPtr, kernelsLen,
935                               (RsScriptKernelID *)srcPtr, srcLen,
936                               (RsScriptKernelID *)dstkPtr, dstkLen,
937                               (RsScriptFieldID *)dstfPtr, dstfLen,
938                               (RsType *)typesPtr, typesLen);
939
940    _env->ReleaseIntArrayElements(_kernels, kernelsPtr, 0);
941    _env->ReleaseIntArrayElements(_src, srcPtr, 0);
942    _env->ReleaseIntArrayElements(_dstk, dstkPtr, 0);
943    _env->ReleaseIntArrayElements(_dstf, dstfPtr, 0);
944    _env->ReleaseIntArrayElements(_types, typesPtr, 0);
945    return id;
946}
947
948static void
949nScriptGroupSetInput(JNIEnv *_env, jobject _this, RsContext con, jint gid, jint kid, jint alloc)
950{
951    LOG_API("nScriptGroupSetInput, con(%p) group(%p), kernelId(%p), alloc(%p)", con,
952        (void *)gid, (void *)kid, (void *)alloc);
953    rsScriptGroupSetInput(con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
954}
955
956static void
957nScriptGroupSetOutput(JNIEnv *_env, jobject _this, RsContext con, jint gid, jint kid, jint alloc)
958{
959    LOG_API("nScriptGroupSetOutput, con(%p) group(%p), kernelId(%p), alloc(%p)", con,
960        (void *)gid, (void *)kid, (void *)alloc);
961    rsScriptGroupSetOutput(con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
962}
963
964static void
965nScriptGroupExecute(JNIEnv *_env, jobject _this, RsContext con, jint gid)
966{
967    LOG_API("nScriptGroupSetOutput, con(%p) group(%p)", con, (void *)gid);
968    rsScriptGroupExecute(con, (RsScriptGroup)gid);
969}
970
971// ---------------------------------------------------------------------------
972
973static jint
974nSamplerCreate(JNIEnv *_env, jobject _this, RsContext con, jint magFilter, jint minFilter,
975               jint wrapS, jint wrapT, jint wrapR, jfloat aniso)
976{
977    LOG_API("nSamplerCreate, con(%p)", con);
978    return (jint)rsSamplerCreate(con,
979                                 (RsSamplerValue)magFilter,
980                                 (RsSamplerValue)minFilter,
981                                 (RsSamplerValue)wrapS,
982                                 (RsSamplerValue)wrapT,
983                                 (RsSamplerValue)wrapR,
984                                 aniso);
985}
986
987// ---------------------------------------------------------------------------
988
989
990static const char *classPathName = "android/support/v8/renderscript/RenderScript";
991
992static JNINativeMethod methods[] = {
993{"nDeviceCreate",                  "()I",                                     (void*)nDeviceCreate },
994{"nDeviceDestroy",                 "(I)V",                                    (void*)nDeviceDestroy },
995{"nDeviceSetConfig",               "(III)V",                                  (void*)nDeviceSetConfig },
996{"nContextGetUserMessage",         "(I[I)I",                                  (void*)nContextGetUserMessage },
997{"nContextGetErrorMessage",        "(I)Ljava/lang/String;",                   (void*)nContextGetErrorMessage },
998{"nContextPeekMessage",            "(I[I)I",                                  (void*)nContextPeekMessage },
999
1000{"nContextInitToClient",           "(I)V",                                    (void*)nContextInitToClient },
1001{"nContextDeinitToClient",         "(I)V",                                    (void*)nContextDeinitToClient },
1002
1003
1004// All methods below are thread protected in java.
1005{"rsnContextCreate",                 "(IIII)I",                               (void*)nContextCreate },
1006{"rsnContextFinish",                 "(I)V",                                  (void*)nContextFinish },
1007{"rsnContextSetPriority",            "(II)V",                                 (void*)nContextSetPriority },
1008{"rsnContextDestroy",                "(I)V",                                  (void*)nContextDestroy },
1009{"rsnContextDump",                   "(II)V",                                 (void*)nContextDump },
1010{"rsnContextSendMessage",            "(II[I)V",                               (void*)nContextSendMessage },
1011{"rsnObjDestroy",                    "(II)V",                                 (void*)nObjDestroy },
1012
1013{"rsnElementCreate",                 "(IIIZI)I",                              (void*)nElementCreate },
1014{"rsnElementCreate2",                "(I[I[Ljava/lang/String;[I)I",           (void*)nElementCreate2 },
1015{"rsnElementGetSubElements",         "(II[I[Ljava/lang/String;[I)V",          (void*)nElementGetSubElements },
1016
1017{"rsnTypeCreate",                    "(IIIIIZZI)I",                           (void*)nTypeCreate },
1018
1019{"rsnAllocationCreateTyped",         "(IIIII)I",                               (void*)nAllocationCreateTyped },
1020{"rsnAllocationCreateFromBitmap",    "(IIILandroid/graphics/Bitmap;I)I",      (void*)nAllocationCreateFromBitmap },
1021{"rsnAllocationCreateBitmapBackedAllocation",    "(IIILandroid/graphics/Bitmap;I)I",      (void*)nAllocationCreateBitmapBackedAllocation },
1022{"rsnAllocationCubeCreateFromBitmap","(IIILandroid/graphics/Bitmap;I)I",      (void*)nAllocationCubeCreateFromBitmap },
1023
1024{"rsnAllocationCopyFromBitmap",      "(IILandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyFromBitmap },
1025{"rsnAllocationCopyToBitmap",        "(IILandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyToBitmap },
1026
1027{"rsnAllocationSyncAll",             "(III)V",                                (void*)nAllocationSyncAll },
1028{"rsnAllocationData1D",              "(IIIII[II)V",                           (void*)nAllocationData1D_i },
1029{"rsnAllocationData1D",              "(IIIII[SI)V",                           (void*)nAllocationData1D_s },
1030{"rsnAllocationData1D",              "(IIIII[BI)V",                           (void*)nAllocationData1D_b },
1031{"rsnAllocationData1D",              "(IIIII[FI)V",                           (void*)nAllocationData1D_f },
1032{"rsnAllocationElementData1D",       "(IIIII[BI)V",                           (void*)nAllocationElementData1D },
1033{"rsnAllocationData2D",              "(IIIIIIII[II)V",                        (void*)nAllocationData2D_i },
1034{"rsnAllocationData2D",              "(IIIIIIII[SI)V",                        (void*)nAllocationData2D_s },
1035{"rsnAllocationData2D",              "(IIIIIIII[BI)V",                        (void*)nAllocationData2D_b },
1036{"rsnAllocationData2D",              "(IIIIIIII[FI)V",                        (void*)nAllocationData2D_f },
1037{"rsnAllocationData2D",              "(IIIIIIIIIIIII)V",                      (void*)nAllocationData2D_alloc },
1038{"rsnAllocationData3D",              "(IIIIIIIII[II)V",                       (void*)nAllocationData3D_i },
1039{"rsnAllocationData3D",              "(IIIIIIIII[SI)V",                       (void*)nAllocationData3D_s },
1040{"rsnAllocationData3D",              "(IIIIIIIII[BI)V",                       (void*)nAllocationData3D_b },
1041{"rsnAllocationData3D",              "(IIIIIIIII[FI)V",                       (void*)nAllocationData3D_f },
1042{"rsnAllocationData3D",              "(IIIIIIIIIIIIII)V",                     (void*)nAllocationData3D_alloc },
1043{"rsnAllocationRead",                "(II[I)V",                               (void*)nAllocationRead_i },
1044{"rsnAllocationRead",                "(II[S)V",                               (void*)nAllocationRead_s },
1045{"rsnAllocationRead",                "(II[B)V",                               (void*)nAllocationRead_b },
1046{"rsnAllocationRead",                "(II[F)V",                               (void*)nAllocationRead_f },
1047{"rsnAllocationGetType",             "(II)I",                                 (void*)nAllocationGetType},
1048{"rsnAllocationResize1D",            "(III)V",                                (void*)nAllocationResize1D },
1049{"rsnAllocationGenerateMipmaps",     "(II)V",                                 (void*)nAllocationGenerateMipmaps },
1050
1051{"rsnScriptBindAllocation",          "(IIII)V",                               (void*)nScriptBindAllocation },
1052{"rsnScriptSetTimeZone",             "(II[B)V",                               (void*)nScriptSetTimeZone },
1053{"rsnScriptInvoke",                  "(III)V",                                (void*)nScriptInvoke },
1054{"rsnScriptInvokeV",                 "(III[B)V",                              (void*)nScriptInvokeV },
1055{"rsnScriptForEach",                 "(IIIII)V",                              (void*)nScriptForEach },
1056{"rsnScriptForEach",                 "(IIIII[B)V",                            (void*)nScriptForEachV },
1057{"rsnScriptForEachClipped",          "(IIIIIIIIIII)V",                        (void*)nScriptForEachClipped },
1058{"rsnScriptForEachClipped",          "(IIIII[BIIIIII)V",                      (void*)nScriptForEachClippedV },
1059{"rsnScriptSetVarI",                 "(IIII)V",                               (void*)nScriptSetVarI },
1060{"rsnScriptSetVarJ",                 "(IIIJ)V",                               (void*)nScriptSetVarJ },
1061{"rsnScriptSetVarF",                 "(IIIF)V",                               (void*)nScriptSetVarF },
1062{"rsnScriptSetVarD",                 "(IIID)V",                               (void*)nScriptSetVarD },
1063{"rsnScriptSetVarV",                 "(III[B)V",                              (void*)nScriptSetVarV },
1064{"rsnScriptSetVarVE",                "(III[BI[I)V",                           (void*)nScriptSetVarVE },
1065{"rsnScriptSetVarObj",               "(IIII)V",                               (void*)nScriptSetVarObj },
1066
1067{"rsnScriptCCreate",                 "(ILjava/lang/String;Ljava/lang/String;[BI)I",  (void*)nScriptCCreate },
1068{"rsnScriptIntrinsicCreate",         "(III)I",                                (void*)nScriptIntrinsicCreate },
1069{"rsnScriptKernelIDCreate",          "(IIII)I",                               (void*)nScriptKernelIDCreate },
1070{"rsnScriptFieldIDCreate",           "(III)I",                                (void*)nScriptFieldIDCreate },
1071{"rsnScriptGroupCreate",             "(I[I[I[I[I[I)I",                        (void*)nScriptGroupCreate },
1072{"rsnScriptGroupSetInput",           "(IIII)V",                               (void*)nScriptGroupSetInput },
1073{"rsnScriptGroupSetOutput",          "(IIII)V",                               (void*)nScriptGroupSetOutput },
1074{"rsnScriptGroupExecute",            "(II)V",                                 (void*)nScriptGroupExecute },
1075
1076{"rsnSamplerCreate",                 "(IIIIIIF)I",                            (void*)nSamplerCreate },
1077
1078};
1079
1080// ---------------------------------------------------------------------------
1081
1082jint JNI_OnLoad(JavaVM* vm, void* reserved)
1083{
1084    JNIEnv* env = NULL;
1085    jclass clazz = NULL;
1086    jint result = -1;
1087
1088    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
1089        __android_log_print(ANDROID_LOG_ERROR, LOG_TAG,
1090            "ERROR: GetEnv failed\n");
1091        goto bail;
1092    }
1093    if (env == NULL) {
1094        __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "ERROR: env == NULL");
1095        goto bail;
1096    }
1097
1098    clazz = env->FindClass(classPathName);
1099    if (clazz == NULL) {
1100        goto bail;
1101    }
1102
1103    if (env->RegisterNatives(clazz, methods, NELEM(methods)) < 0) {
1104        __android_log_print(ANDROID_LOG_ERROR, LOG_TAG,
1105            "ERROR: MediaPlayer native registration failed\n");
1106        goto bail;
1107    }
1108
1109    /* success -- return valid version number */
1110    result = JNI_VERSION_1_4;
1111
1112bail:
1113    return result;
1114}
1115