android_renderscript_RenderScript.cpp revision 6b32fab1dbfd8bc1cc176557fe0a7b2ebd4966bd
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "libRS_jni"
18
19#include <stdlib.h>
20#include <stdio.h>
21#include <fcntl.h>
22#include <unistd.h>
23#include <math.h>
24#include <utils/misc.h>
25
26#include <surfaceflinger/Surface.h>
27
28#include <core/SkBitmap.h>
29#include <core/SkPixelRef.h>
30#include <core/SkStream.h>
31#include <core/SkTemplates.h>
32#include <images/SkImageDecoder.h>
33
34#include <utils/Asset.h>
35#include <utils/ResourceTypes.h>
36
37#include "jni.h"
38#include "JNIHelp.h"
39#include "android_runtime/AndroidRuntime.h"
40#include "android_runtime/android_view_Surface.h"
41
42#include <RenderScript.h>
43#include <RenderScriptEnv.h>
44
45//#define LOG_API LOGE
46#define LOG_API(...)
47
48using namespace android;
49
50// ---------------------------------------------------------------------------
51
52static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
53{
54    jclass npeClazz = env->FindClass(exc);
55    env->ThrowNew(npeClazz, msg);
56}
57
58static jfieldID gContextId = 0;
59static jfieldID gNativeBitmapID = 0;
60static jfieldID gTypeNativeCache = 0;
61
62static void _nInit(JNIEnv *_env, jclass _this)
63{
64    gContextId             = _env->GetFieldID(_this, "mContext", "I");
65
66    jclass bitmapClass = _env->FindClass("android/graphics/Bitmap");
67    gNativeBitmapID = _env->GetFieldID(bitmapClass, "mNativeBitmap", "I");
68}
69
70// ---------------------------------------------------------------------------
71
72static void
73nContextFinish(JNIEnv *_env, jobject _this, RsContext con)
74{
75    LOG_API("nContextFinish, con(%p)", con);
76    rsContextFinish(con);
77}
78
79static void
80nAssignName(JNIEnv *_env, jobject _this, RsContext con, jint obj, jbyteArray str)
81{
82    LOG_API("nAssignName, con(%p), obj(%p)", con, (void *)obj);
83    jint len = _env->GetArrayLength(str);
84    jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
85    rsAssignName(con, (void *)obj, (const char *)cptr, len);
86    _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
87}
88
89static jstring
90nGetName(JNIEnv *_env, jobject _this, RsContext con, jint obj)
91{
92    LOG_API("nGetName, con(%p), obj(%p)", con, (void *)obj);
93    const char *name = NULL;
94    rsaGetName(con, (void *)obj, &name);
95    if(name == NULL || strlen(name) == 0) {
96        return NULL;
97    }
98    return _env->NewStringUTF(name);
99}
100
101static void
102nObjDestroy(JNIEnv *_env, jobject _this, RsContext con, jint obj)
103{
104    LOG_API("nObjDestroy, con(%p) obj(%p)", con, (void *)obj);
105    rsObjDestroy(con, (void *)obj);
106}
107
108// ---------------------------------------------------------------------------
109
110static jint
111nDeviceCreate(JNIEnv *_env, jobject _this)
112{
113    LOG_API("nDeviceCreate");
114    return (jint)rsDeviceCreate();
115}
116
117static void
118nDeviceDestroy(JNIEnv *_env, jobject _this, jint dev)
119{
120    LOG_API("nDeviceDestroy");
121    return rsDeviceDestroy((RsDevice)dev);
122}
123
124static void
125nDeviceSetConfig(JNIEnv *_env, jobject _this, jint dev, jint p, jint value)
126{
127    LOG_API("nDeviceSetConfig  dev(%p), param(%i), value(%i)", (void *)dev, p, value);
128    return rsDeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value);
129}
130
131static jint
132nContextCreate(JNIEnv *_env, jobject _this, jint dev, jint ver)
133{
134    LOG_API("nContextCreate");
135    return (jint)rsContextCreate((RsDevice)dev, ver);
136}
137
138static jint
139nContextCreateGL(JNIEnv *_env, jobject _this, jint dev, jint ver,
140                 int colorMin, int colorPref,
141                 int alphaMin, int alphaPref,
142                 int depthMin, int depthPref,
143                 int stencilMin, int stencilPref,
144                 int samplesMin, int samplesPref, float samplesQ)
145{
146    RsSurfaceConfig sc;
147    sc.alphaMin = alphaMin;
148    sc.alphaPref = alphaPref;
149    sc.colorMin = colorMin;
150    sc.colorPref = colorPref;
151    sc.depthMin = depthMin;
152    sc.depthPref = depthPref;
153    sc.samplesMin = samplesMin;
154    sc.samplesPref = samplesPref;
155    sc.samplesQ = samplesQ;
156
157    LOG_API("nContextCreateGL");
158    return (jint)rsContextCreateGL((RsDevice)dev, ver, sc);
159}
160
161static void
162nContextSetPriority(JNIEnv *_env, jobject _this, RsContext con, jint p)
163{
164    LOG_API("ContextSetPriority, con(%p), priority(%i)", con, p);
165    rsContextSetPriority(con, p);
166}
167
168
169
170static void
171nContextSetSurface(JNIEnv *_env, jobject _this, RsContext con, jint width, jint height, jobject wnd)
172{
173    LOG_API("nContextSetSurface, con(%p), width(%i), height(%i), surface(%p)", con, width, height, (Surface *)wnd);
174
175    Surface * window = NULL;
176    if (wnd == NULL) {
177
178    } else {
179        window = (Surface*) android_Surface_getNativeWindow(_env, wnd).get();
180    }
181
182    rsContextSetSurface(con, width, height, window);
183}
184
185static void
186nContextDestroy(JNIEnv *_env, jobject _this, RsContext con)
187{
188    LOG_API("nContextDestroy, con(%p)", con);
189    rsContextDestroy(con);
190}
191
192static void
193nContextDump(JNIEnv *_env, jobject _this, RsContext con, jint bits)
194{
195    LOG_API("nContextDump, con(%p)  bits(%i)", (RsContext)con, bits);
196    rsContextDump((RsContext)con, bits);
197}
198
199static void
200nContextPause(JNIEnv *_env, jobject _this, RsContext con)
201{
202    LOG_API("nContextPause, con(%p)", con);
203    rsContextPause(con);
204}
205
206static void
207nContextResume(JNIEnv *_env, jobject _this, RsContext con)
208{
209    LOG_API("nContextResume, con(%p)", con);
210    rsContextResume(con);
211}
212
213
214static jstring
215nContextGetErrorMessage(JNIEnv *_env, jobject _this, RsContext con)
216{
217    LOG_API("nContextGetErrorMessage, con(%p)", con);
218    char buf[1024];
219
220    size_t receiveLen;
221    uint32_t subID;
222    int id = rsContextGetMessage(con, buf, &receiveLen, &subID, sizeof(buf), true);
223    if (!id && receiveLen) {
224        LOGV("message receive buffer too small.  %i", receiveLen);
225    }
226    return _env->NewStringUTF(buf);
227}
228
229static void
230nContextGetUserMessage(JNIEnv *_env, jobject _this, RsContext con, jintArray data)
231{
232    jint len = _env->GetArrayLength(data);
233    LOG_API("nContextGetMessage, con(%p), len(%i)", con, len);
234    jint *ptr = _env->GetIntArrayElements(data, NULL);
235    size_t receiveLen;
236    uint32_t subID;
237    int id = rsContextGetMessage(con, ptr, &receiveLen, &subID, len * 4, true);
238    if (!id && receiveLen) {
239        LOGV("message receive buffer too small.  %i", receiveLen);
240    }
241    _env->ReleaseIntArrayElements(data, ptr, 0);
242}
243
244static jint
245nContextPeekMessage(JNIEnv *_env, jobject _this, RsContext con, jintArray auxData, jboolean wait)
246{
247    LOG_API("nContextPeekMessage, con(%p)", con);
248    jint *auxDataPtr = _env->GetIntArrayElements(auxData, NULL);
249    size_t receiveLen;
250    uint32_t subID;
251    int id = rsContextPeekMessage(con, &receiveLen, &subID, wait);
252    auxDataPtr[0] = (jint)subID;
253    auxDataPtr[1] = (jint)receiveLen;
254    _env->ReleaseIntArrayElements(auxData, auxDataPtr, 0);
255    return id;
256}
257
258static void nContextInitToClient(JNIEnv *_env, jobject _this, RsContext con)
259{
260    LOG_API("nContextInitToClient, con(%p)", con);
261    rsContextInitToClient(con);
262}
263
264static void nContextDeinitToClient(JNIEnv *_env, jobject _this, RsContext con)
265{
266    LOG_API("nContextDeinitToClient, con(%p)", con);
267    rsContextDeinitToClient(con);
268}
269
270
271static jint
272nElementCreate(JNIEnv *_env, jobject _this, RsContext con, jint type, jint kind, jboolean norm, jint size)
273{
274    LOG_API("nElementCreate, con(%p), type(%i), kind(%i), norm(%i), size(%i)", con, type, kind, norm, size);
275    return (jint)rsElementCreate(con, (RsDataType)type, (RsDataKind)kind, norm, size);
276}
277
278static jint
279nElementCreate2(JNIEnv *_env, jobject _this, RsContext con, jintArray _ids, jobjectArray _names, jintArray _arraySizes)
280{
281    int fieldCount = _env->GetArrayLength(_ids);
282    LOG_API("nElementCreate2, con(%p)", con);
283
284    jint *ids = _env->GetIntArrayElements(_ids, NULL);
285    jint *arraySizes = _env->GetIntArrayElements(_arraySizes, NULL);
286    const char ** nameArray = (const char **)calloc(fieldCount, sizeof(char *));
287    size_t* sizeArray = (size_t*)calloc(fieldCount, sizeof(size_t));
288
289    for (int ct=0; ct < fieldCount; ct++) {
290        jstring s = (jstring)_env->GetObjectArrayElement(_names, ct);
291        nameArray[ct] = _env->GetStringUTFChars(s, NULL);
292        sizeArray[ct] = _env->GetStringUTFLength(s);
293    }
294    jint id = (jint)rsElementCreate2(con, fieldCount, (RsElement *)ids, nameArray, sizeArray, (const uint32_t *)arraySizes);
295    for (int ct=0; ct < fieldCount; ct++) {
296        jstring s = (jstring)_env->GetObjectArrayElement(_names, ct);
297        _env->ReleaseStringUTFChars(s, nameArray[ct]);
298    }
299    _env->ReleaseIntArrayElements(_ids, ids, JNI_ABORT);
300    _env->ReleaseIntArrayElements(_arraySizes, arraySizes, JNI_ABORT);
301    free(nameArray);
302    free(sizeArray);
303    return (jint)id;
304}
305
306static void
307nElementGetNativeData(JNIEnv *_env, jobject _this, RsContext con, jint id, jintArray _elementData)
308{
309    int dataSize = _env->GetArrayLength(_elementData);
310    LOG_API("nElementGetNativeData, con(%p)", con);
311
312    // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
313    assert(dataSize == 5);
314
315    uint32_t elementData[5];
316    rsaElementGetNativeData(con, (RsElement)id, elementData, dataSize);
317
318    for(jint i = 0; i < dataSize; i ++) {
319        _env->SetIntArrayRegion(_elementData, i, 1, (const jint*)&elementData[i]);
320    }
321}
322
323
324static void
325nElementGetSubElements(JNIEnv *_env, jobject _this, RsContext con, jint id, jintArray _IDs, jobjectArray _names)
326{
327    int dataSize = _env->GetArrayLength(_IDs);
328    LOG_API("nElementGetSubElements, con(%p)", con);
329
330    uint32_t *ids = (uint32_t *)malloc((uint32_t)dataSize * sizeof(uint32_t));
331    const char **names = (const char **)malloc((uint32_t)dataSize * sizeof(const char *));
332
333    rsaElementGetSubElements(con, (RsElement)id, ids, names, (uint32_t)dataSize);
334
335    for(jint i = 0; i < dataSize; i++) {
336        _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
337        _env->SetIntArrayRegion(_IDs, i, 1, (const jint*)&ids[i]);
338    }
339
340    free(ids);
341    free(names);
342}
343
344// -----------------------------------
345
346static int
347nTypeCreate(JNIEnv *_env, jobject _this, RsContext con, RsElement eid,
348            jint dimx, jint dimy, jint dimz, jboolean mips, jboolean faces)
349{
350    LOG_API("nTypeCreate, con(%p) eid(%p), x(%i), y(%i), z(%i), mips(%i), faces(%i)",
351            con, eid, dimx, dimy, dimz, mips, faces);
352
353    jint id = (jint)rsaTypeCreate(con, (RsElement)eid, dimx, dimy, dimz, mips, faces);
354    return (jint)id;
355}
356
357static void
358nTypeGetNativeData(JNIEnv *_env, jobject _this, RsContext con, jint id, jintArray _typeData)
359{
360    // We are packing 6 items: mDimX; mDimY; mDimZ;
361    // mDimLOD; mDimFaces; mElement; into typeData
362    int elementCount = _env->GetArrayLength(_typeData);
363
364    assert(elementCount == 6);
365    LOG_API("nTypeCreate, con(%p)", con);
366
367    uint32_t typeData[6];
368    rsaTypeGetNativeData(con, (RsType)id, typeData, 6);
369
370    for(jint i = 0; i < elementCount; i ++) {
371        _env->SetIntArrayRegion(_typeData, i, 1, (const jint*)&typeData[i]);
372    }
373}
374
375// -----------------------------------
376
377static jint
378nAllocationCreateTyped(JNIEnv *_env, jobject _this, RsContext con, jint type, jint mips, jint usage)
379{
380    LOG_API("nAllocationCreateTyped, con(%p), type(%p), mip(%i), usage(%i)", con, (RsElement)type, mips, usage);
381    return (jint) rsaAllocationCreateTyped(con, (RsType)type, (RsAllocationMipmapControl)mips, (uint32_t)usage);
382}
383
384static void
385nAllocationUploadToTexture(JNIEnv *_env, jobject _this, RsContext con, jint a, jboolean genMip, jint mip)
386{
387    LOG_API("nAllocationUploadToTexture, con(%p), a(%p), genMip(%i), mip(%i)", con, (RsAllocation)a, genMip, mip);
388    rsAllocationUploadToTexture(con, (RsAllocation)a, genMip, mip);
389}
390
391static void
392nAllocationUploadToBufferObject(JNIEnv *_env, jobject _this, RsContext con, jint a)
393{
394    LOG_API("nAllocationUploadToBufferObject, con(%p), a(%p)", con, (RsAllocation)a);
395    rsAllocationUploadToBufferObject(con, (RsAllocation)a);
396}
397
398static void
399nAllocationSyncAll(JNIEnv *_env, jobject _this, RsContext con, jint a, jint bits)
400{
401    LOG_API("nAllocationSyncAll, con(%p), a(%p), bits(0x%08x)", con, (RsAllocation)a, bits);
402    rsAllocationSyncAll(con, (RsAllocation)a, (RsAllocationUsageType)bits);
403}
404
405static int
406nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, RsContext con, jint type, jint mip, jobject jbitmap, jint usage)
407{
408    SkBitmap const * nativeBitmap =
409            (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID);
410    const SkBitmap& bitmap(*nativeBitmap);
411
412    bitmap.lockPixels();
413    const void* ptr = bitmap.getPixels();
414    jint id = (jint)rsaAllocationCreateFromBitmap(con, (RsType)type, (RsAllocationMipmapControl)mip, ptr, usage);
415    bitmap.unlockPixels();
416    return id;
417}
418
419static int
420nAllocationCubeCreateFromBitmap(JNIEnv *_env, jobject _this, RsContext con, jint type, jint mip, jobject jbitmap, jint usage)
421{
422    SkBitmap const * nativeBitmap =
423            (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID);
424    const SkBitmap& bitmap(*nativeBitmap);
425
426    bitmap.lockPixels();
427    const void* ptr = bitmap.getPixels();
428    jint id = (jint)rsaAllocationCubeCreateFromBitmap(con, (RsType)type, (RsAllocationMipmapControl)mip, ptr, usage);
429    bitmap.unlockPixels();
430    return id;
431}
432
433static void
434nAllocationCopyFromBitmap(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jobject jbitmap)
435{
436    SkBitmap const * nativeBitmap =
437            (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID);
438    const SkBitmap& bitmap(*nativeBitmap);
439
440    bitmap.lockPixels();
441    const void* ptr = bitmap.getPixels();
442    rsAllocationCopyFromBitmap(con, (RsAllocation)alloc, ptr, bitmap.getSize());
443    bitmap.unlockPixels();
444}
445
446static void
447nAllocationCopyToBitmap(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jobject jbitmap)
448{
449    SkBitmap const * nativeBitmap =
450            (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID);
451    const SkBitmap& bitmap(*nativeBitmap);
452
453    bitmap.lockPixels();
454    void* ptr = bitmap.getPixels();
455    rsAllocationCopyToBitmap(con, (RsAllocation)alloc, ptr, bitmap.getSize());
456    bitmap.unlockPixels();
457}
458
459static void ReleaseBitmapCallback(void *bmp)
460{
461    SkBitmap const * nativeBitmap = (SkBitmap const *)bmp;
462    nativeBitmap->unlockPixels();
463}
464
465
466static void
467nAllocationSubData1D_i(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint count, jintArray data, int sizeBytes)
468{
469    jint len = _env->GetArrayLength(data);
470    LOG_API("nAllocation1DSubData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
471    jint *ptr = _env->GetIntArrayElements(data, NULL);
472    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
473    _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
474}
475
476static void
477nAllocationSubData1D_s(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint count, jshortArray data, int sizeBytes)
478{
479    jint len = _env->GetArrayLength(data);
480    LOG_API("nAllocation1DSubData_s, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
481    jshort *ptr = _env->GetShortArrayElements(data, NULL);
482    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
483    _env->ReleaseShortArrayElements(data, ptr, JNI_ABORT);
484}
485
486static void
487nAllocationSubData1D_b(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint count, jbyteArray data, int sizeBytes)
488{
489    jint len = _env->GetArrayLength(data);
490    LOG_API("nAllocation1DSubData_b, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
491    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
492    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
493    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
494}
495
496static void
497nAllocationSubData1D_f(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint count, jfloatArray data, int sizeBytes)
498{
499    jint len = _env->GetArrayLength(data);
500    LOG_API("nAllocation1DSubData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
501    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
502    rsAllocation1DSubData(con, (RsAllocation)alloc, offset, count, ptr, sizeBytes);
503    _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
504}
505
506static void
507//    native void rsnAllocationSubElementData1D(int con, int id, int xoff, int compIdx, byte[] d, int sizeBytes);
508nAllocationSubElementData1D(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint compIdx, jbyteArray data, int sizeBytes)
509{
510    jint len = _env->GetArrayLength(data);
511    LOG_API("nAllocationSubElementData1D, con(%p), alloc(%p), offset(%i), comp(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, compIdx, len, sizeBytes);
512    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
513    rsAllocation1DSubElementData(con, (RsAllocation)alloc, offset, ptr, compIdx, sizeBytes);
514    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
515}
516
517static void
518nAllocationSubData2D_i(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint w, jint h, jintArray data, int sizeBytes)
519{
520    jint len = _env->GetArrayLength(data);
521    LOG_API("nAllocation2DSubData_i, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)", con, (RsAllocation)alloc, xoff, yoff, w, h, len);
522    jint *ptr = _env->GetIntArrayElements(data, NULL);
523    rsAllocation2DSubData(con, (RsAllocation)alloc, xoff, yoff, w, h, ptr, sizeBytes);
524    _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
525}
526
527static void
528nAllocationSubData2D_f(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint w, jint h, jfloatArray data, int sizeBytes)
529{
530    jint len = _env->GetArrayLength(data);
531    LOG_API("nAllocation2DSubData_i, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)", con, (RsAllocation)alloc, xoff, yoff, w, h, len);
532    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
533    rsAllocation2DSubData(con, (RsAllocation)alloc, xoff, yoff, w, h, ptr, sizeBytes);
534    _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
535}
536
537static void
538nAllocationRead_i(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jintArray data)
539{
540    jint len = _env->GetArrayLength(data);
541    LOG_API("nAllocationRead_i, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
542    jint *ptr = _env->GetIntArrayElements(data, NULL);
543    rsAllocationRead(con, (RsAllocation)alloc, ptr);
544    _env->ReleaseIntArrayElements(data, ptr, 0);
545}
546
547static void
548nAllocationRead_f(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jfloatArray data)
549{
550    jint len = _env->GetArrayLength(data);
551    LOG_API("nAllocationRead_f, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
552    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
553    rsAllocationRead(con, (RsAllocation)alloc, ptr);
554    _env->ReleaseFloatArrayElements(data, ptr, 0);
555}
556
557static jint
558nAllocationGetType(JNIEnv *_env, jobject _this, RsContext con, jint a)
559{
560    LOG_API("nAllocationGetType, con(%p), a(%p)", con, (RsAllocation)a);
561    return (jint) rsaAllocationGetType(con, (RsAllocation)a);
562}
563
564static void
565nAllocationResize1D(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint dimX)
566{
567    LOG_API("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", con, (RsAllocation)alloc, dimX);
568    rsAllocationResize1D(con, (RsAllocation)alloc, dimX);
569}
570
571static void
572nAllocationResize2D(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint dimX, jint dimY)
573{
574    LOG_API("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i), sizeY(%i)", con, (RsAllocation)alloc, dimX, dimY);
575    rsAllocationResize2D(con, (RsAllocation)alloc, dimX, dimY);
576}
577
578// -----------------------------------
579
580static int
581nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, RsContext con, jint native_asset)
582{
583    LOGV("______nFileA3D %u", (uint32_t) native_asset);
584
585    Asset* asset = reinterpret_cast<Asset*>(native_asset);
586
587    jint id = (jint)rsaFileA3DCreateFromAssetStream(con, asset->getBuffer(false), asset->getLength());
588    return id;
589}
590
591static int
592nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, RsContext con, jint fileA3D)
593{
594    int32_t numEntries = 0;
595    rsaFileA3DGetNumIndexEntries(con, &numEntries, (RsFile)fileA3D);
596    return numEntries;
597}
598
599static void
600nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, RsContext con, jint fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
601{
602    LOGV("______nFileA3D %u", (uint32_t) fileA3D);
603    RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
604
605    rsaFileA3DGetIndexEntries(con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
606
607    for(jint i = 0; i < numEntries; i ++) {
608        _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
609        _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
610    }
611
612    free(fileEntries);
613}
614
615static int
616nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, RsContext con, jint fileA3D, jint index)
617{
618    LOGV("______nFileA3D %u", (uint32_t) fileA3D);
619    jint id = (jint)rsaFileA3DGetEntryByIndex(con, (uint32_t)index, (RsFile)fileA3D);
620    return id;
621}
622
623// -----------------------------------
624
625static int
626nFontCreateFromFile(JNIEnv *_env, jobject _this, RsContext con, jstring fileName, jint fontSize, jint dpi)
627{
628    const char* fileNameUTF = _env->GetStringUTFChars(fileName, NULL);
629
630    jint id = (jint)rsFontCreateFromFile(con, fileNameUTF, fontSize, dpi);
631    return id;
632}
633
634
635// -----------------------------------
636
637static void
638nAdapter1DBindAllocation(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint alloc)
639{
640    LOG_API("nAdapter1DBindAllocation, con(%p), adapter(%p), alloc(%p)", con, (RsAdapter1D)adapter, (RsAllocation)alloc);
641    rsAdapter1DBindAllocation(con, (RsAdapter1D)adapter, (RsAllocation)alloc);
642}
643
644static void
645nAdapter1DSetConstraint(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint dim, jint value)
646{
647    LOG_API("nAdapter1DSetConstraint, con(%p), adapter(%p), dim(%i), value(%i)", con, (RsAdapter1D)adapter, dim, value);
648    rsAdapter1DSetConstraint(con, (RsAdapter1D)adapter, (RsDimension)dim, value);
649}
650
651static void
652nAdapter1DData_i(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jintArray data)
653{
654    jint len = _env->GetArrayLength(data);
655    LOG_API("nAdapter1DData_i, con(%p), adapter(%p), len(%i)", con, (RsAdapter1D)adapter, len);
656    jint *ptr = _env->GetIntArrayElements(data, NULL);
657    rsAdapter1DData(con, (RsAdapter1D)adapter, ptr);
658    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
659}
660
661static void
662nAdapter1DSubData_i(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint offset, jint count, jintArray data)
663{
664    jint len = _env->GetArrayLength(data);
665    LOG_API("nAdapter1DSubData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAdapter1D)adapter, offset, count, len);
666    jint *ptr = _env->GetIntArrayElements(data, NULL);
667    rsAdapter1DSubData(con, (RsAdapter1D)adapter, offset, count, ptr);
668    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
669}
670
671static void
672nAdapter1DData_f(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jfloatArray data)
673{
674    jint len = _env->GetArrayLength(data);
675    LOG_API("nAdapter1DData_f, con(%p), adapter(%p), len(%i)", con, (RsAdapter1D)adapter, len);
676    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
677    rsAdapter1DData(con, (RsAdapter1D)adapter, ptr);
678    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
679}
680
681static void
682nAdapter1DSubData_f(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint offset, jint count, jfloatArray data)
683{
684    jint len = _env->GetArrayLength(data);
685    LOG_API("nAdapter1DSubData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i)", con, (RsAdapter1D)adapter, offset, count, len);
686    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
687    rsAdapter1DSubData(con, (RsAdapter1D)adapter, offset, count, ptr);
688    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
689}
690
691static jint
692nAdapter1DCreate(JNIEnv *_env, jobject _this, RsContext con)
693{
694    LOG_API("nAdapter1DCreate, con(%p)", con);
695    return (jint)rsAdapter1DCreate(con);
696}
697
698// -----------------------------------
699
700static void
701nAdapter2DBindAllocation(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint alloc)
702{
703    LOG_API("nAdapter2DBindAllocation, con(%p), adapter(%p), alloc(%p)", con, (RsAdapter2D)adapter, (RsAllocation)alloc);
704    rsAdapter2DBindAllocation(con, (RsAdapter2D)adapter, (RsAllocation)alloc);
705}
706
707static void
708nAdapter2DSetConstraint(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint dim, jint value)
709{
710    LOG_API("nAdapter2DSetConstraint, con(%p), adapter(%p), dim(%i), value(%i)", con, (RsAdapter2D)adapter, dim, value);
711    rsAdapter2DSetConstraint(con, (RsAdapter2D)adapter, (RsDimension)dim, value);
712}
713
714static void
715nAdapter2DData_i(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jintArray data)
716{
717    jint len = _env->GetArrayLength(data);
718    LOG_API("nAdapter2DData_i, con(%p), adapter(%p), len(%i)", con, (RsAdapter2D)adapter, len);
719    jint *ptr = _env->GetIntArrayElements(data, NULL);
720    rsAdapter2DData(con, (RsAdapter2D)adapter, ptr);
721    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
722}
723
724static void
725nAdapter2DData_f(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jfloatArray data)
726{
727    jint len = _env->GetArrayLength(data);
728    LOG_API("nAdapter2DData_f, con(%p), adapter(%p), len(%i)", con, (RsAdapter2D)adapter, len);
729    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
730    rsAdapter2DData(con, (RsAdapter2D)adapter, ptr);
731    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
732}
733
734static void
735nAdapter2DSubData_i(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint xoff, jint yoff, jint w, jint h, jintArray data)
736{
737    jint len = _env->GetArrayLength(data);
738    LOG_API("nAdapter2DSubData_i, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)",
739            con, (RsAdapter2D)adapter, xoff, yoff, w, h, len);
740    jint *ptr = _env->GetIntArrayElements(data, NULL);
741    rsAdapter2DSubData(con, (RsAdapter2D)adapter, xoff, yoff, w, h, ptr);
742    _env->ReleaseIntArrayElements(data, ptr, 0/*JNI_ABORT*/);
743}
744
745static void
746nAdapter2DSubData_f(JNIEnv *_env, jobject _this, RsContext con, jint adapter, jint xoff, jint yoff, jint w, jint h, jfloatArray data)
747{
748    jint len = _env->GetArrayLength(data);
749    LOG_API("nAdapter2DSubData_f, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)",
750            con, (RsAdapter2D)adapter, xoff, yoff, w, h, len);
751    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
752    rsAdapter2DSubData(con, (RsAdapter1D)adapter, xoff, yoff, w, h, ptr);
753    _env->ReleaseFloatArrayElements(data, ptr, 0/*JNI_ABORT*/);
754}
755
756static jint
757nAdapter2DCreate(JNIEnv *_env, jobject _this, RsContext con)
758{
759    LOG_API("nAdapter2DCreate, con(%p)", con);
760    return (jint)rsAdapter2DCreate(con);
761}
762
763// -----------------------------------
764
765static void
766nScriptBindAllocation(JNIEnv *_env, jobject _this, RsContext con, jint script, jint alloc, jint slot)
767{
768    LOG_API("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", con, (RsScript)script, (RsAllocation)alloc, slot);
769    rsScriptBindAllocation(con, (RsScript)script, (RsAllocation)alloc, slot);
770}
771
772static void
773nScriptSetVarI(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jint val)
774{
775    LOG_API("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", con, (void *)script, slot, val);
776    rsScriptSetVarI(con, (RsScript)script, slot, val);
777}
778
779static void
780nScriptSetVarObj(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jint val)
781{
782    LOG_API("nScriptSetVarObj, con(%p), s(%p), slot(%i), val(%i)", con, (void *)script, slot, val);
783    rsScriptSetVarObj(con, (RsScript)script, slot, (RsObjectBase)val);
784}
785
786static void
787nScriptSetVarJ(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jlong val)
788{
789    LOG_API("nScriptSetVarJ, con(%p), s(%p), slot(%i), val(%lli)", con, (void *)script, slot, val);
790    rsScriptSetVarJ(con, (RsScript)script, slot, val);
791}
792
793static void
794nScriptSetVarF(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, float val)
795{
796    LOG_API("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", con, (void *)script, slot, val);
797    rsScriptSetVarF(con, (RsScript)script, slot, val);
798}
799
800static void
801nScriptSetVarD(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, double val)
802{
803    LOG_API("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", con, (void *)script, slot, val);
804    rsScriptSetVarD(con, (RsScript)script, slot, val);
805}
806
807static void
808nScriptSetVarV(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jbyteArray data)
809{
810    LOG_API("nScriptSetVarV, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
811    jint len = _env->GetArrayLength(data);
812    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
813    rsScriptSetVarV(con, (RsScript)script, slot, ptr, len);
814    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
815}
816
817
818static void
819nScriptSetTimeZone(JNIEnv *_env, jobject _this, RsContext con, jint script, jbyteArray timeZone)
820{
821    LOG_API("nScriptCSetTimeZone, con(%p), s(%p), timeZone(%s)", con, (void *)script, (const char *)timeZone);
822
823    jint length = _env->GetArrayLength(timeZone);
824    jbyte* timeZone_ptr;
825    timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
826
827    rsScriptSetTimeZone(con, (RsScript)script, (const char *)timeZone_ptr, length);
828
829    if (timeZone_ptr) {
830        _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
831    }
832}
833
834static void
835nScriptInvoke(JNIEnv *_env, jobject _this, RsContext con, jint obj, jint slot)
836{
837    LOG_API("nScriptInvoke, con(%p), script(%p)", con, (void *)obj);
838    rsScriptInvoke(con, (RsScript)obj, slot);
839}
840
841static void
842nScriptInvokeV(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jbyteArray data)
843{
844    LOG_API("nScriptInvokeV, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
845    jint len = _env->GetArrayLength(data);
846    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
847    rsScriptInvokeV(con, (RsScript)script, slot, ptr, len);
848    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
849}
850
851
852// -----------------------------------
853
854static void
855nScriptCBegin(JNIEnv *_env, jobject _this, RsContext con)
856{
857    LOG_API("nScriptCBegin, con(%p)", con);
858    rsScriptCBegin(con);
859}
860
861static void
862nScriptCSetScript(JNIEnv *_env, jobject _this, RsContext con, jbyteArray scriptRef,
863                  jint offset, jint length)
864{
865    LOG_API("!!! nScriptCSetScript, con(%p)", con);
866    jint _exception = 0;
867    jint remaining;
868    jbyte* script_base = 0;
869    jbyte* script_ptr;
870    if (!scriptRef) {
871        _exception = 1;
872        //_env->ThrowNew(IAEClass, "script == null");
873        goto exit;
874    }
875    if (offset < 0) {
876        _exception = 1;
877        //_env->ThrowNew(IAEClass, "offset < 0");
878        goto exit;
879    }
880    if (length < 0) {
881        _exception = 1;
882        //_env->ThrowNew(IAEClass, "length < 0");
883        goto exit;
884    }
885    remaining = _env->GetArrayLength(scriptRef) - offset;
886    if (remaining < length) {
887        _exception = 1;
888        //_env->ThrowNew(IAEClass, "length > script.length - offset");
889        goto exit;
890    }
891    script_base = (jbyte *)
892        _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
893    script_ptr = script_base + offset;
894
895    rsScriptCSetText(con, (const char *)script_ptr, length);
896
897exit:
898    if (script_base) {
899        _env->ReleasePrimitiveArrayCritical(scriptRef, script_base,
900                _exception ? JNI_ABORT: 0);
901    }
902}
903
904static jint
905nScriptCCreate(JNIEnv *_env, jobject _this, RsContext con, jstring resName, jstring cacheDir)
906{
907    LOG_API("nScriptCCreate, con(%p)", con);
908    const char* resNameUTF = _env->GetStringUTFChars(resName, NULL);
909    const char* cacheDirUTF = _env->GetStringUTFChars(cacheDir, NULL);
910    jint i = (jint)rsScriptCCreate(con, resNameUTF, cacheDirUTF);
911    _env->ReleaseStringUTFChars(resName, resNameUTF);
912    _env->ReleaseStringUTFChars(cacheDir, cacheDirUTF);
913    return i;
914}
915
916// ---------------------------------------------------------------------------
917
918static void
919nProgramStoreBegin(JNIEnv *_env, jobject _this, RsContext con, jint in, jint out)
920{
921    LOG_API("nProgramStoreBegin, con(%p), in(%p), out(%p)", con, (RsElement)in, (RsElement)out);
922    rsProgramStoreBegin(con, (RsElement)in, (RsElement)out);
923}
924
925static void
926nProgramStoreDepthFunc(JNIEnv *_env, jobject _this, RsContext con, jint func)
927{
928    LOG_API("nProgramStoreDepthFunc, con(%p), func(%i)", con, func);
929    rsProgramStoreDepthFunc(con, (RsDepthFunc)func);
930}
931
932static void
933nProgramStoreDepthMask(JNIEnv *_env, jobject _this, RsContext con, jboolean enable)
934{
935    LOG_API("nProgramStoreDepthMask, con(%p), enable(%i)", con, enable);
936    rsProgramStoreDepthMask(con, enable);
937}
938
939static void
940nProgramStoreColorMask(JNIEnv *_env, jobject _this, RsContext con, jboolean r, jboolean g, jboolean b, jboolean a)
941{
942    LOG_API("nProgramStoreColorMask, con(%p), r(%i), g(%i), b(%i), a(%i)", con, r, g, b, a);
943    rsProgramStoreColorMask(con, r, g, b, a);
944}
945
946static void
947nProgramStoreBlendFunc(JNIEnv *_env, jobject _this, RsContext con, int src, int dst)
948{
949    LOG_API("nProgramStoreBlendFunc, con(%p), src(%i), dst(%i)", con, src, dst);
950    rsProgramStoreBlendFunc(con, (RsBlendSrcFunc)src, (RsBlendDstFunc)dst);
951}
952
953static void
954nProgramStoreDither(JNIEnv *_env, jobject _this, RsContext con, jboolean enable)
955{
956    LOG_API("nProgramStoreDither, con(%p), enable(%i)", con, enable);
957    rsProgramStoreDither(con, enable);
958}
959
960static jint
961nProgramStoreCreate(JNIEnv *_env, jobject _this, RsContext con)
962{
963    LOG_API("nProgramStoreCreate, con(%p)", con);
964    return (jint)rsProgramStoreCreate(con);
965}
966
967// ---------------------------------------------------------------------------
968
969static void
970nProgramBindConstants(JNIEnv *_env, jobject _this, RsContext con, jint vpv, jint slot, jint a)
971{
972    LOG_API("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", con, (RsProgramVertex)vpv, slot, (RsAllocation)a);
973    rsProgramBindConstants(con, (RsProgram)vpv, slot, (RsAllocation)a);
974}
975
976static void
977nProgramBindTexture(JNIEnv *_env, jobject _this, RsContext con, jint vpf, jint slot, jint a)
978{
979    LOG_API("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
980    rsProgramBindTexture(con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
981}
982
983static void
984nProgramBindSampler(JNIEnv *_env, jobject _this, RsContext con, jint vpf, jint slot, jint a)
985{
986    LOG_API("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramFragment)vpf, slot, (RsSampler)a);
987    rsProgramBindSampler(con, (RsProgramFragment)vpf, slot, (RsSampler)a);
988}
989
990// ---------------------------------------------------------------------------
991
992static jint
993nProgramFragmentCreate(JNIEnv *_env, jobject _this, RsContext con, jstring shader, jintArray params)
994{
995    const char* shaderUTF = _env->GetStringUTFChars(shader, NULL);
996    jint shaderLen = _env->GetStringUTFLength(shader);
997    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
998    jint paramLen = _env->GetArrayLength(params);
999
1000    LOG_API("nProgramFragmentCreate, con(%p), shaderLen(%i), paramLen(%i)", con, shaderLen, paramLen);
1001
1002    jint ret = (jint)rsProgramFragmentCreate(con, shaderUTF, shaderLen, (uint32_t *)paramPtr, paramLen);
1003    _env->ReleaseStringUTFChars(shader, shaderUTF);
1004    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1005    return ret;
1006}
1007
1008
1009// ---------------------------------------------------------------------------
1010
1011static jint
1012nProgramVertexCreate(JNIEnv *_env, jobject _this, RsContext con, jstring shader, jintArray params)
1013{
1014    const char* shaderUTF = _env->GetStringUTFChars(shader, NULL);
1015    jint shaderLen = _env->GetStringUTFLength(shader);
1016    jint *paramPtr = _env->GetIntArrayElements(params, NULL);
1017    jint paramLen = _env->GetArrayLength(params);
1018
1019    LOG_API("nProgramVertexCreate, con(%p), shaderLen(%i), paramLen(%i)", con, shaderLen, paramLen);
1020
1021    jint ret = (jint)rsProgramVertexCreate(con, shaderUTF, shaderLen, (uint32_t *)paramPtr, paramLen);
1022    _env->ReleaseStringUTFChars(shader, shaderUTF);
1023    _env->ReleaseIntArrayElements(params, paramPtr, JNI_ABORT);
1024    return ret;
1025}
1026
1027// ---------------------------------------------------------------------------
1028
1029static jint
1030nProgramRasterCreate(JNIEnv *_env, jobject _this, RsContext con, jboolean pointSmooth, jboolean lineSmooth, jboolean pointSprite)
1031{
1032    LOG_API("nProgramRasterCreate, con(%p), pointSmooth(%i), lineSmooth(%i), pointSprite(%i)",
1033            con, pointSmooth, lineSmooth, pointSprite);
1034    return (jint)rsProgramRasterCreate(con, pointSmooth, lineSmooth, pointSprite);
1035}
1036
1037static void
1038nProgramRasterSetLineWidth(JNIEnv *_env, jobject _this, RsContext con, jint vpr, jfloat v)
1039{
1040    LOG_API("nProgramRasterSetLineWidth, con(%p), vpf(%p), value(%f)", con, (RsProgramRaster)vpr, v);
1041    rsProgramRasterSetLineWidth(con, (RsProgramRaster)vpr, v);
1042}
1043
1044static void
1045nProgramRasterSetCullMode(JNIEnv *_env, jobject _this, RsContext con, jint vpr, jint v)
1046{
1047    LOG_API("nProgramRasterSetCullMode, con(%p), vpf(%p), value(%i)", con, (RsProgramRaster)vpr, v);
1048    rsProgramRasterSetCullMode(con, (RsProgramRaster)vpr, (RsCullMode)v);
1049}
1050
1051
1052// ---------------------------------------------------------------------------
1053
1054static void
1055nContextBindRootScript(JNIEnv *_env, jobject _this, RsContext con, jint script)
1056{
1057    LOG_API("nContextBindRootScript, con(%p), script(%p)", con, (RsScript)script);
1058    rsContextBindRootScript(con, (RsScript)script);
1059}
1060
1061static void
1062nContextBindProgramStore(JNIEnv *_env, jobject _this, RsContext con, jint pfs)
1063{
1064    LOG_API("nContextBindProgramStore, con(%p), pfs(%p)", con, (RsProgramStore)pfs);
1065    rsContextBindProgramStore(con, (RsProgramStore)pfs);
1066}
1067
1068static void
1069nContextBindProgramFragment(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1070{
1071    LOG_API("nContextBindProgramFragment, con(%p), pf(%p)", con, (RsProgramFragment)pf);
1072    rsContextBindProgramFragment(con, (RsProgramFragment)pf);
1073}
1074
1075static void
1076nContextBindProgramVertex(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1077{
1078    LOG_API("nContextBindProgramVertex, con(%p), pf(%p)", con, (RsProgramVertex)pf);
1079    rsContextBindProgramVertex(con, (RsProgramVertex)pf);
1080}
1081
1082static void
1083nContextBindProgramRaster(JNIEnv *_env, jobject _this, RsContext con, jint pf)
1084{
1085    LOG_API("nContextBindProgramRaster, con(%p), pf(%p)", con, (RsProgramRaster)pf);
1086    rsContextBindProgramRaster(con, (RsProgramRaster)pf);
1087}
1088
1089
1090// ---------------------------------------------------------------------------
1091
1092static void
1093nSamplerBegin(JNIEnv *_env, jobject _this, RsContext con)
1094{
1095    LOG_API("nSamplerBegin, con(%p)", con);
1096    rsSamplerBegin(con);
1097}
1098
1099static void
1100nSamplerSet(JNIEnv *_env, jobject _this, RsContext con, jint p, jint v)
1101{
1102    LOG_API("nSamplerSet, con(%p), param(%i), value(%i)", con, p, v);
1103    rsSamplerSet(con, (RsSamplerParam)p, (RsSamplerValue)v);
1104}
1105
1106static void
1107nSamplerSet2(JNIEnv *_env, jobject _this, RsContext con, jint p, jfloat v)
1108{
1109    LOG_API("nSamplerSet2, con(%p), param(%i), value(%f)", con, p, v);
1110    rsSamplerSet2(con, (RsSamplerParam)p, v);
1111}
1112
1113static jint
1114nSamplerCreate(JNIEnv *_env, jobject _this, RsContext con)
1115{
1116    LOG_API("nSamplerCreate, con(%p)", con);
1117    return (jint)rsSamplerCreate(con);
1118}
1119
1120// ---------------------------------------------------------------------------
1121
1122static jint
1123nMeshCreate(JNIEnv *_env, jobject _this, RsContext con, jint vtxCount, jint idxCount)
1124{
1125    LOG_API("nMeshCreate, con(%p), vtxCount(%i), idxCount(%i)", con, vtxCount, idxCount);
1126    int id = (int)rsMeshCreate(con, vtxCount, idxCount);
1127    return id;
1128}
1129
1130static void
1131nMeshBindVertex(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jint alloc, jint slot)
1132{
1133    LOG_API("nMeshBindVertex, con(%p), Mesh(%p), Alloc(%p), slot(%i)", con, (RsMesh)mesh, (RsAllocation)alloc, slot);
1134    rsMeshBindVertex(con, (RsMesh)mesh, (RsAllocation)alloc, slot);
1135}
1136
1137static void
1138nMeshBindIndex(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jint alloc, jint primID, jint slot)
1139{
1140    LOG_API("nMeshBindIndex, con(%p), Mesh(%p), Alloc(%p)", con, (RsMesh)mesh, (RsAllocation)alloc);
1141    rsMeshBindIndex(con, (RsMesh)mesh, (RsAllocation)alloc, primID, slot);
1142}
1143
1144static void
1145nMeshInitVertexAttribs(JNIEnv *_env, jobject _this, RsContext con, jint mesh)
1146{
1147    LOG_API("nMeshInitVertexAttribs, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1148    rsMeshInitVertexAttribs(con, (RsMesh)mesh);
1149}
1150
1151
1152static jint
1153nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, RsContext con, jint mesh)
1154{
1155    LOG_API("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1156    jint vtxCount = 0;
1157    rsaMeshGetVertexBufferCount(con, (RsMesh)mesh, &vtxCount);
1158    return vtxCount;
1159}
1160
1161static jint
1162nMeshGetIndexCount(JNIEnv *_env, jobject _this, RsContext con, jint mesh)
1163{
1164    LOG_API("nMeshGetIndexCount, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1165    jint idxCount = 0;
1166    rsaMeshGetIndexCount(con, (RsMesh)mesh, &idxCount);
1167    return idxCount;
1168}
1169
1170static void
1171nMeshGetVertices(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jintArray _ids, int numVtxIDs)
1172{
1173    LOG_API("nMeshGetVertices, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1174
1175    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
1176    rsaMeshGetVertices(con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
1177
1178    for(jint i = 0; i < numVtxIDs; i ++) {
1179        _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&allocs[i]);
1180    }
1181
1182    free(allocs);
1183}
1184
1185static void
1186nMeshGetIndices(JNIEnv *_env, jobject _this, RsContext con, jint mesh, jintArray _idxIds, jintArray _primitives, int numIndices)
1187{
1188    LOG_API("nMeshGetVertices, con(%p), Mesh(%p)", con, (RsMesh)mesh);
1189
1190    RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
1191    uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
1192
1193    rsaMeshGetIndices(con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
1194
1195    for(jint i = 0; i < numIndices; i ++) {
1196        _env->SetIntArrayRegion(_idxIds, i, 1, (const jint*)&allocs[i]);
1197        _env->SetIntArrayRegion(_primitives, i, 1, (const jint*)&prims[i]);
1198    }
1199
1200    free(allocs);
1201    free(prims);
1202}
1203
1204// ---------------------------------------------------------------------------
1205
1206
1207static const char *classPathName = "android/renderscript/RenderScript";
1208
1209static JNINativeMethod methods[] = {
1210{"_nInit",                         "()V",                                     (void*)_nInit },
1211
1212{"nDeviceCreate",                  "()I",                                     (void*)nDeviceCreate },
1213{"nDeviceDestroy",                 "(I)V",                                    (void*)nDeviceDestroy },
1214{"nDeviceSetConfig",               "(III)V",                                  (void*)nDeviceSetConfig },
1215{"nContextGetUserMessage",         "(I[I)V",                                  (void*)nContextGetUserMessage },
1216{"nContextGetErrorMessage",        "(I)Ljava/lang/String;",                   (void*)nContextGetErrorMessage },
1217{"nContextPeekMessage",            "(I[IZ)I",                                 (void*)nContextPeekMessage },
1218
1219{"nContextInitToClient",           "(I)V",                                    (void*)nContextInitToClient },
1220{"nContextDeinitToClient",         "(I)V",                                    (void*)nContextDeinitToClient },
1221
1222
1223// All methods below are thread protected in java.
1224{"rsnContextCreate",                 "(II)I",                                 (void*)nContextCreate },
1225{"rsnContextCreateGL",               "(IIIIIIIIIIIIF)I",                      (void*)nContextCreateGL },
1226{"rsnContextFinish",                 "(I)V",                                  (void*)nContextFinish },
1227{"rsnContextSetPriority",            "(II)V",                                 (void*)nContextSetPriority },
1228{"rsnContextSetSurface",             "(IIILandroid/view/Surface;)V",          (void*)nContextSetSurface },
1229{"rsnContextDestroy",                "(I)V",                                  (void*)nContextDestroy },
1230{"rsnContextDump",                   "(II)V",                                 (void*)nContextDump },
1231{"rsnContextPause",                  "(I)V",                                  (void*)nContextPause },
1232{"rsnContextResume",                 "(I)V",                                  (void*)nContextResume },
1233{"rsnAssignName",                    "(II[B)V",                               (void*)nAssignName },
1234{"rsnGetName",                       "(II)Ljava/lang/String;",                (void*)nGetName },
1235{"rsnObjDestroy",                    "(II)V",                                 (void*)nObjDestroy },
1236
1237{"rsnFileA3DCreateFromAssetStream",  "(II)I",                                 (void*)nFileA3DCreateFromAssetStream },
1238{"rsnFileA3DGetNumIndexEntries",     "(II)I",                                 (void*)nFileA3DGetNumIndexEntries },
1239{"rsnFileA3DGetIndexEntries",        "(III[I[Ljava/lang/String;)V",           (void*)nFileA3DGetIndexEntries },
1240{"rsnFileA3DGetEntryByIndex",        "(III)I",                                (void*)nFileA3DGetEntryByIndex },
1241
1242{"rsnFontCreateFromFile",            "(ILjava/lang/String;II)I",              (void*)nFontCreateFromFile },
1243
1244{"rsnElementCreate",                 "(IIIZI)I",                              (void*)nElementCreate },
1245{"rsnElementCreate2",                "(I[I[Ljava/lang/String;[I)I",           (void*)nElementCreate2 },
1246{"rsnElementGetNativeData",          "(II[I)V",                               (void*)nElementGetNativeData },
1247{"rsnElementGetSubElements",         "(II[I[Ljava/lang/String;)V",            (void*)nElementGetSubElements },
1248
1249{"rsnTypeCreate",                    "(IIIIIZZ)I",                            (void*)nTypeCreate },
1250{"rsnTypeGetNativeData",             "(II[I)V",                               (void*)nTypeGetNativeData },
1251
1252{"rsnAllocationCreateTyped",         "(IIII)I",                               (void*)nAllocationCreateTyped },
1253{"rsnAllocationCreateFromBitmap",    "(IIILandroid/graphics/Bitmap;I)I",      (void*)nAllocationCreateFromBitmap },
1254{"rsnAllocationCubeCreateFromBitmap","(IIILandroid/graphics/Bitmap;I)I",      (void*)nAllocationCubeCreateFromBitmap },
1255
1256{"rsnAllocationCopyFromBitmap",      "(IILandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyFromBitmap },
1257{"rsnAllocationCopyToBitmap",        "(IILandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyToBitmap },
1258
1259{"rsnAllocationUploadToTexture",     "(IIZI)V",                               (void*)nAllocationUploadToTexture },
1260{"rsnAllocationUploadToBufferObject","(II)V",                                 (void*)nAllocationUploadToBufferObject },
1261{"rsnAllocationSyncAll",             "(III)V",                                (void*)nAllocationSyncAll },
1262{"rsnAllocationSubData1D",           "(IIII[II)V",                            (void*)nAllocationSubData1D_i },
1263{"rsnAllocationSubData1D",           "(IIII[SI)V",                            (void*)nAllocationSubData1D_s },
1264{"rsnAllocationSubData1D",           "(IIII[BI)V",                            (void*)nAllocationSubData1D_b },
1265{"rsnAllocationSubData1D",           "(IIII[FI)V",                            (void*)nAllocationSubData1D_f },
1266{"rsnAllocationSubElementData1D",    "(IIII[BI)V",                            (void*)nAllocationSubElementData1D },
1267{"rsnAllocationSubData2D",           "(IIIIII[II)V",                          (void*)nAllocationSubData2D_i },
1268{"rsnAllocationSubData2D",           "(IIIIII[FI)V",                          (void*)nAllocationSubData2D_f },
1269{"rsnAllocationRead",                "(II[I)V",                               (void*)nAllocationRead_i },
1270{"rsnAllocationRead",                "(II[F)V",                               (void*)nAllocationRead_f },
1271{"rsnAllocationGetType",             "(II)I",                                 (void*)nAllocationGetType},
1272{"rsnAllocationResize1D",            "(III)V",                                (void*)nAllocationResize1D },
1273{"rsnAllocationResize2D",            "(IIII)V",                               (void*)nAllocationResize2D },
1274
1275{"rsnAdapter1DBindAllocation",       "(III)V",                                (void*)nAdapter1DBindAllocation },
1276{"rsnAdapter1DSetConstraint",        "(IIII)V",                               (void*)nAdapter1DSetConstraint },
1277{"rsnAdapter1DData",                 "(II[I)V",                               (void*)nAdapter1DData_i },
1278{"rsnAdapter1DData",                 "(II[F)V",                               (void*)nAdapter1DData_f },
1279{"rsnAdapter1DSubData",              "(IIII[I)V",                             (void*)nAdapter1DSubData_i },
1280{"rsnAdapter1DSubData",              "(IIII[F)V",                             (void*)nAdapter1DSubData_f },
1281{"rsnAdapter1DCreate",               "(I)I",                                  (void*)nAdapter1DCreate },
1282
1283{"rsnAdapter2DBindAllocation",       "(III)V",                                (void*)nAdapter2DBindAllocation },
1284{"rsnAdapter2DSetConstraint",        "(IIII)V",                               (void*)nAdapter2DSetConstraint },
1285{"rsnAdapter2DData",                 "(II[I)V",                               (void*)nAdapter2DData_i },
1286{"rsnAdapter2DData",                 "(II[F)V",                               (void*)nAdapter2DData_f },
1287{"rsnAdapter2DSubData",              "(IIIIII[I)V",                           (void*)nAdapter2DSubData_i },
1288{"rsnAdapter2DSubData",              "(IIIIII[F)V",                           (void*)nAdapter2DSubData_f },
1289{"rsnAdapter2DCreate",               "(I)I",                                  (void*)nAdapter2DCreate },
1290
1291{"rsnScriptBindAllocation",          "(IIII)V",                               (void*)nScriptBindAllocation },
1292{"rsnScriptSetTimeZone",             "(II[B)V",                               (void*)nScriptSetTimeZone },
1293{"rsnScriptInvoke",                  "(III)V",                                (void*)nScriptInvoke },
1294{"rsnScriptInvokeV",                 "(III[B)V",                              (void*)nScriptInvokeV },
1295{"rsnScriptSetVarI",                 "(IIII)V",                               (void*)nScriptSetVarI },
1296{"rsnScriptSetVarJ",                 "(IIIJ)V",                               (void*)nScriptSetVarJ },
1297{"rsnScriptSetVarF",                 "(IIIF)V",                               (void*)nScriptSetVarF },
1298{"rsnScriptSetVarD",                 "(IIID)V",                               (void*)nScriptSetVarD },
1299{"rsnScriptSetVarV",                 "(III[B)V",                              (void*)nScriptSetVarV },
1300{"rsnScriptSetVarObj",               "(IIII)V",                               (void*)nScriptSetVarObj },
1301
1302{"rsnScriptCBegin",                  "(I)V",                                  (void*)nScriptCBegin },
1303{"rsnScriptCSetScript",              "(I[BII)V",                              (void*)nScriptCSetScript },
1304{"rsnScriptCCreate",                 "(ILjava/lang/String;Ljava/lang/String;)I",  (void*)nScriptCCreate },
1305
1306{"rsnProgramStoreBegin",             "(III)V",                                (void*)nProgramStoreBegin },
1307{"rsnProgramStoreDepthFunc",         "(II)V",                                 (void*)nProgramStoreDepthFunc },
1308{"rsnProgramStoreDepthMask",         "(IZ)V",                                 (void*)nProgramStoreDepthMask },
1309{"rsnProgramStoreColorMask",         "(IZZZZ)V",                              (void*)nProgramStoreColorMask },
1310{"rsnProgramStoreBlendFunc",         "(III)V",                                (void*)nProgramStoreBlendFunc },
1311{"rsnProgramStoreDither",            "(IZ)V",                                 (void*)nProgramStoreDither },
1312{"rsnProgramStoreCreate",            "(I)I",                                  (void*)nProgramStoreCreate },
1313
1314{"rsnProgramBindConstants",          "(IIII)V",                               (void*)nProgramBindConstants },
1315{"rsnProgramBindTexture",            "(IIII)V",                               (void*)nProgramBindTexture },
1316{"rsnProgramBindSampler",            "(IIII)V",                               (void*)nProgramBindSampler },
1317
1318{"rsnProgramFragmentCreate",        "(ILjava/lang/String;[I)I",               (void*)nProgramFragmentCreate },
1319
1320{"rsnProgramRasterCreate",           "(IZZZ)I",                               (void*)nProgramRasterCreate },
1321{"rsnProgramRasterSetLineWidth",     "(IIF)V",                                (void*)nProgramRasterSetLineWidth },
1322{"rsnProgramRasterSetCullMode",      "(III)V",                                (void*)nProgramRasterSetCullMode },
1323
1324{"rsnProgramVertexCreate",          "(ILjava/lang/String;[I)I",               (void*)nProgramVertexCreate },
1325
1326{"rsnContextBindRootScript",         "(II)V",                                 (void*)nContextBindRootScript },
1327{"rsnContextBindProgramStore",       "(II)V",                                 (void*)nContextBindProgramStore },
1328{"rsnContextBindProgramFragment",    "(II)V",                                 (void*)nContextBindProgramFragment },
1329{"rsnContextBindProgramVertex",      "(II)V",                                 (void*)nContextBindProgramVertex },
1330{"rsnContextBindProgramRaster",      "(II)V",                                 (void*)nContextBindProgramRaster },
1331
1332{"rsnSamplerBegin",                  "(I)V",                                  (void*)nSamplerBegin },
1333{"rsnSamplerSet",                    "(III)V",                                (void*)nSamplerSet },
1334{"rsnSamplerSet2",                   "(IIF)V",                                (void*)nSamplerSet2 },
1335{"rsnSamplerCreate",                 "(I)I",                                  (void*)nSamplerCreate },
1336
1337{"rsnMeshCreate",                    "(III)I",                                (void*)nMeshCreate },
1338{"rsnMeshBindVertex",                "(IIII)V",                               (void*)nMeshBindVertex },
1339{"rsnMeshBindIndex",                 "(IIIII)V",                              (void*)nMeshBindIndex },
1340{"rsnMeshInitVertexAttribs",         "(II)V",                                 (void*)nMeshInitVertexAttribs },
1341
1342{"rsnMeshGetVertexBufferCount",      "(II)I",                                 (void*)nMeshGetVertexBufferCount },
1343{"rsnMeshGetIndexCount",             "(II)I",                                 (void*)nMeshGetIndexCount },
1344{"rsnMeshGetVertices",               "(II[II)V",                              (void*)nMeshGetVertices },
1345{"rsnMeshGetIndices",                "(II[I[II)V",                            (void*)nMeshGetIndices },
1346
1347};
1348
1349static int registerFuncs(JNIEnv *_env)
1350{
1351    return android::AndroidRuntime::registerNativeMethods(
1352            _env, classPathName, methods, NELEM(methods));
1353}
1354
1355// ---------------------------------------------------------------------------
1356
1357jint JNI_OnLoad(JavaVM* vm, void* reserved)
1358{
1359    JNIEnv* env = NULL;
1360    jint result = -1;
1361
1362    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
1363        LOGE("ERROR: GetEnv failed\n");
1364        goto bail;
1365    }
1366    assert(env != NULL);
1367
1368    if (registerFuncs(env) < 0) {
1369        LOGE("ERROR: MediaPlayer native registration failed\n");
1370        goto bail;
1371    }
1372
1373    /* success -- return valid version number */
1374    result = JNI_VERSION_1_4;
1375
1376bail:
1377    return result;
1378}
1379