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