android_renderscript_RenderScript.cpp revision dbf6b4b75aa70e8c1e4c0dace624a2ca8a2ca171
1/*
2 * Copyright (C) 2011-2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "libRS_jni"
18
19#include <stdlib.h>
20#include <stdio.h>
21#include <fcntl.h>
22#include <unistd.h>
23#include <math.h>
24#include <android/bitmap.h>
25#include <android/log.h>
26#include "jni.h"
27#include <rs.h>
28#include <rsEnv.h>
29
30#include "rsDispatch.h"
31#include <dlfcn.h>
32
33//#define LOG_API ALOG
34#define LOG_API(...)
35
36#define NELEM(m) (sizeof(m) / sizeof((m)[0]))
37
38class AutoJavaStringToUTF8 {
39public:
40    AutoJavaStringToUTF8(JNIEnv* env, jstring str) : fEnv(env), fJStr(str) {
41        fCStr = env->GetStringUTFChars(str, NULL);
42        fLength = env->GetStringUTFLength(str);
43    }
44    ~AutoJavaStringToUTF8() {
45        fEnv->ReleaseStringUTFChars(fJStr, fCStr);
46    }
47    const char* c_str() const { return fCStr; }
48    jsize length() const { return fLength; }
49
50private:
51    JNIEnv*     fEnv;
52    jstring     fJStr;
53    const char* fCStr;
54    jsize       fLength;
55};
56
57class AutoJavaStringArrayToUTF8 {
58public:
59    AutoJavaStringArrayToUTF8(JNIEnv* env, jobjectArray strings, jsize stringsLength)
60    : mEnv(env), mStrings(strings), mStringsLength(stringsLength) {
61        mCStrings = NULL;
62        mSizeArray = NULL;
63        if (stringsLength > 0) {
64            mCStrings = (const char **)calloc(stringsLength, sizeof(char *));
65            mSizeArray = (size_t*)calloc(stringsLength, sizeof(size_t));
66            for (jsize ct = 0; ct < stringsLength; ct ++) {
67                jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
68                mCStrings[ct] = mEnv->GetStringUTFChars(s, NULL);
69                mSizeArray[ct] = mEnv->GetStringUTFLength(s);
70            }
71        }
72    }
73    ~AutoJavaStringArrayToUTF8() {
74        for (jsize ct=0; ct < mStringsLength; ct++) {
75            jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
76            mEnv->ReleaseStringUTFChars(s, mCStrings[ct]);
77        }
78        free(mCStrings);
79        free(mSizeArray);
80    }
81    const char **c_str() const { return mCStrings; }
82    size_t *c_str_len() const { return mSizeArray; }
83    jsize length() const { return mStringsLength; }
84
85private:
86    JNIEnv      *mEnv;
87    jobjectArray mStrings;
88    const char **mCStrings;
89    size_t      *mSizeArray;
90    jsize        mStringsLength;
91};
92
93
94// ---------------------------------------------------------------------------
95
96static dispatchTable dispatchTab;
97
98static bool loadSymbols(void* handle) {
99
100    dispatchTab.AllocationGetType = (AllocationGetTypeFnPtr)dlsym(handle, "rsaAllocationGetType");
101    if (dispatchTab.AllocationGetType == NULL) {
102        LOG_API("Couldn't initialize dispatchTab.AllocationGetType");
103        return false;
104    }
105    dispatchTab.TypeGetNativeData = (TypeGetNativeDataFnPtr)dlsym(handle, "rsaTypeGetNativeData");
106    if (dispatchTab.TypeGetNativeData == NULL) {
107        LOG_API("Couldn't initialize dispatchTab.TypeGetNativeData");
108        return false;
109    }
110    dispatchTab.ElementGetNativeData = (ElementGetNativeDataFnPtr)dlsym(handle, "rsaElementGetNativeData");
111    if (dispatchTab.ElementGetNativeData == NULL) {
112        LOG_API("Couldn't initialize dispatchTab.ElementGetNativeData");
113        return false;
114    }
115    dispatchTab.ElementGetSubElements = (ElementGetSubElementsFnPtr)dlsym(handle, "rsaElementGetSubElements");
116    if (dispatchTab.ElementGetSubElements == NULL) {
117        LOG_API("Couldn't initialize dispatchTab.ElementGetSubElements");
118        return false;
119    }
120    dispatchTab.DeviceCreate = (DeviceCreateFnPtr)dlsym(handle, "rsDeviceCreate");
121    if (dispatchTab.DeviceCreate == NULL) {
122        LOG_API("Couldn't initialize dispatchTab.DeviceCreate");
123        return false;
124    }
125    dispatchTab.DeviceDestroy = (DeviceDestroyFnPtr)dlsym(handle, "rsDeviceDestroy");
126    if (dispatchTab.DeviceDestroy == NULL) {
127        LOG_API("Couldn't initialize dispatchTab.DeviceDestroy");
128        return false;
129    }
130    dispatchTab.DeviceSetConfig = (DeviceSetConfigFnPtr)dlsym(handle, "rsDeviceSetConfig");
131    if (dispatchTab.DeviceSetConfig == NULL) {
132        LOG_API("Couldn't initialize dispatchTab.DeviceSetConfig");
133        return false;
134    }
135    dispatchTab.ContextCreate = (ContextCreateFnPtr)dlsym(handle, "rsContextCreate");;
136    if (dispatchTab.ContextCreate == NULL) {
137        LOG_API("Couldn't initialize dispatchTab.ContextCreate");
138        return false;
139    }
140    dispatchTab.GetName = (GetNameFnPtr)dlsym(handle, "rsaGetName");;
141    if (dispatchTab.GetName == NULL) {
142        LOG_API("Couldn't initialize dispatchTab.GetName");
143        return false;
144    }
145    dispatchTab.ContextDestroy = (ContextDestroyFnPtr)dlsym(handle, "rsContextDestroy");
146    if (dispatchTab.ContextDestroy == NULL) {
147        LOG_API("Couldn't initialize dispatchTab.ContextDestroy");
148        return false;
149    }
150    dispatchTab.ContextGetMessage = (ContextGetMessageFnPtr)dlsym(handle, "rsContextGetMessage");
151    if (dispatchTab.ContextGetMessage == NULL) {
152        LOG_API("Couldn't initialize dispatchTab.ContextGetMessage");
153        return false;
154    }
155    dispatchTab.ContextPeekMessage = (ContextPeekMessageFnPtr)dlsym(handle, "rsContextPeekMessage");
156    if (dispatchTab.ContextPeekMessage == NULL) {
157        LOG_API("Couldn't initialize dispatchTab.ContextPeekMessage");
158        return false;
159    }
160    dispatchTab.ContextSendMessage = (ContextSendMessageFnPtr)dlsym(handle, "rsContextSendMessage");
161    if (dispatchTab.ContextSendMessage == NULL) {
162        LOG_API("Couldn't initialize dispatchTab.ContextSendMessage");
163        return false;
164    }
165    dispatchTab.ContextInitToClient = (ContextInitToClientFnPtr)dlsym(handle, "rsContextInitToClient");
166    if (dispatchTab.ContextInitToClient == NULL) {
167        LOG_API("Couldn't initialize dispatchTab.ContextInitToClient");
168        return false;
169    }
170    dispatchTab.ContextDeinitToClient = (ContextDeinitToClientFnPtr)dlsym(handle, "rsContextDeinitToClient");
171    if (dispatchTab.ContextDeinitToClient == NULL) {
172        LOG_API("Couldn't initialize dispatchTab.ContextDeinitToClient");
173        return false;
174    }
175    dispatchTab.TypeCreate = (TypeCreateFnPtr)dlsym(handle, "rsTypeCreate");
176    if (dispatchTab.TypeCreate == NULL) {
177        LOG_API("Couldn't initialize dispatchTab.TypeCreate");
178        return false;
179    }
180    dispatchTab.AllocationCreateTyped = (AllocationCreateTypedFnPtr)dlsym(handle, "rsAllocationCreateTyped");
181    if (dispatchTab.AllocationCreateTyped == NULL) {
182        LOG_API("Couldn't initialize dispatchTab.AllocationCreateTyped");
183        return false;
184    }
185    dispatchTab.AllocationCreateFromBitmap = (AllocationCreateFromBitmapFnPtr)dlsym(handle, "rsAllocationCreateFromBitmap");
186    if (dispatchTab.AllocationCreateFromBitmap == NULL) {
187        LOG_API("Couldn't initialize dispatchTab.AllocationCreateFromBitmap");
188        return false;
189    }
190    dispatchTab.AllocationCubeCreateFromBitmap = (AllocationCubeCreateFromBitmapFnPtr)dlsym(handle, "rsAllocationCubeCreateFromBitmap");
191    if (dispatchTab.AllocationCubeCreateFromBitmap == NULL) {
192        LOG_API("Couldn't initialize dispatchTab.AllocationCubeCreateFromBitmap");
193        return false;
194    }
195    dispatchTab.AllocationGetSurface = (AllocationGetSurfaceFnPtr)dlsym(handle, "rsAllocationGetSurface");
196    if (dispatchTab.AllocationGetSurface == NULL) {
197        LOG_API("Couldn't initialize dispatchTab.AllocationGetSurface");
198        return false;
199    }
200    dispatchTab.AllocationSetSurface = (AllocationSetSurfaceFnPtr)dlsym(handle, "rsAllocationSetSurface");
201    if (dispatchTab.AllocationSetSurface == NULL) {
202        LOG_API("Couldn't initialize dispatchTab.AllocationSetSurface");
203        return false;
204    }
205    dispatchTab.ContextFinish = (ContextFinishFnPtr)dlsym(handle, "rsContextFinish");
206    if (dispatchTab.ContextFinish == NULL) {
207        LOG_API("Couldn't initialize dispatchTab.ContextFinish");
208        return false;
209    }
210    dispatchTab.ContextDump = (ContextDumpFnPtr)dlsym(handle, "rsContextDump");
211    if (dispatchTab.ContextDump == NULL) {
212        LOG_API("Couldn't initialize dispatchTab.ContextDump");
213        return false;
214    }
215    dispatchTab.ContextSetPriority = (ContextSetPriorityFnPtr)dlsym(handle, "rsContextSetPriority");
216    if (dispatchTab.ContextSetPriority == NULL) {
217        LOG_API("Couldn't initialize dispatchTab.ContextSetPriority");
218        return false;
219    }
220    dispatchTab.AssignName = (AssignNameFnPtr)dlsym(handle, "rsAssignName");
221    if (dispatchTab.AssignName == NULL) {
222        LOG_API("Couldn't initialize dispatchTab.AssignName");
223        return false;
224    }
225    dispatchTab.ObjDestroy = (ObjDestroyFnPtr)dlsym(handle, "rsObjDestroy");
226    if (dispatchTab.ObjDestroy == NULL) {
227        LOG_API("Couldn't initialize dispatchTab.ObjDestroy");
228        return false;
229    }
230    dispatchTab.ElementCreate = (ElementCreateFnPtr)dlsym(handle, "rsElementCreate");
231    if (dispatchTab.ElementCreate == NULL) {
232        LOG_API("Couldn't initialize dispatchTab.ElementCreate");
233        return false;
234    }
235    dispatchTab.ElementCreate2 = (ElementCreate2FnPtr)dlsym(handle, "rsElementCreate2");
236    if (dispatchTab.ElementCreate2 == NULL) {
237        LOG_API("Couldn't initialize dispatchTab.ElementCreate2");
238        return false;
239    }
240    dispatchTab.AllocationCopyToBitmap = (AllocationCopyToBitmapFnPtr)dlsym(handle, "rsAllocationCopyToBitmap");
241    if (dispatchTab.AllocationCopyToBitmap == NULL) {
242        LOG_API("Couldn't initialize dispatchTab.AllocationCopyToBitmap");
243        return false;
244    }
245    dispatchTab.Allocation1DData = (Allocation1DDataFnPtr)dlsym(handle, "rsAllocation1DData");
246    if (dispatchTab.Allocation1DData == NULL) {
247        LOG_API("Couldn't initialize dispatchTab.Allocation1DData");
248        return false;
249    }
250    dispatchTab.Allocation1DElementData = (Allocation1DElementDataFnPtr)dlsym(handle, "rsAllocation1DElementData");
251    if (dispatchTab.Allocation1DElementData == NULL) {
252        LOG_API("Couldn't initialize dispatchTab.Allocation1DElementData");
253        return false;
254    }
255    dispatchTab.Allocation2DData = (Allocation2DDataFnPtr)dlsym(handle, "rsAllocation2DData");
256    if (dispatchTab.Allocation2DData == NULL) {
257        LOG_API("Couldn't initialize dispatchTab.Allocation2DData");
258        return false;
259    }
260    dispatchTab.Allocation3DData = (Allocation3DDataFnPtr)dlsym(handle, "rsAllocation3DData");
261    if (dispatchTab.Allocation3DData == NULL) {
262        LOG_API("Couldn't initialize dispatchTab.Allocation3DData");
263        return false;
264    }
265    dispatchTab.AllocationGenerateMipmaps = (AllocationGenerateMipmapsFnPtr)dlsym(handle, "rsAllocationGenerateMipmaps");
266    if (dispatchTab.AllocationGenerateMipmaps == NULL) {
267        LOG_API("Couldn't initialize dispatchTab.AllocationGenerateMipmaps");
268        return false;
269    }
270    dispatchTab.AllocationRead = (AllocationReadFnPtr)dlsym(handle, "rsAllocationRead");
271    if (dispatchTab.AllocationRead == NULL) {
272        LOG_API("Couldn't initialize dispatchTab.AllocationRead");
273        return false;
274    }
275    dispatchTab.Allocation1DRead = (Allocation1DReadFnPtr)dlsym(handle, "rsAllocation1DRead");
276    if (dispatchTab.Allocation1DRead == NULL) {
277        LOG_API("Couldn't initialize dispatchTab.Allocation1DRead");
278        return false;
279    }
280    dispatchTab.Allocation2DRead = (Allocation2DReadFnPtr)dlsym(handle, "rsAllocation2DRead");
281    if (dispatchTab.Allocation2DRead == NULL) {
282        LOG_API("Couldn't initialize dispatchTab.Allocation2DRead");
283        return false;
284    }
285    dispatchTab.AllocationSyncAll = (AllocationSyncAllFnPtr)dlsym(handle, "rsAllocationSyncAll");
286    if (dispatchTab.AllocationSyncAll == NULL) {
287        LOG_API("Couldn't initialize dispatchTab.AllocationSyncAll");
288        return false;
289    }
290    dispatchTab.AllocationResize1D = (AllocationResize1DFnPtr)dlsym(handle, "rsAllocationResize1D");
291    if (dispatchTab.AllocationResize1D == NULL) {
292        LOG_API("Couldn't initialize dispatchTab.AllocationResize1D");
293        return false;
294    }
295    dispatchTab.AllocationCopy2DRange = (AllocationCopy2DRangeFnPtr)dlsym(handle, "rsAllocationCopy2DRange");
296    if (dispatchTab.AllocationCopy2DRange == NULL) {
297        LOG_API("Couldn't initialize dispatchTab.AllocationCopy2DRange");
298        return false;
299    }
300    dispatchTab.AllocationCopy3DRange = (AllocationCopy3DRangeFnPtr)dlsym(handle, "rsAllocationCopy3DRange");
301    if (dispatchTab.AllocationCopy3DRange == NULL) {
302        LOG_API("Couldn't initialize dispatchTab.AllocationCopy3DRange");
303        return false;
304    }
305    dispatchTab.SamplerCreate = (SamplerCreateFnPtr)dlsym(handle, "rsSamplerCreate");
306    if (dispatchTab.SamplerCreate == NULL) {
307        LOG_API("Couldn't initialize dispatchTab.SamplerCreate");
308        return false;
309    }
310    dispatchTab.ScriptBindAllocation = (ScriptBindAllocationFnPtr)dlsym(handle, "rsScriptBindAllocation");
311    if (dispatchTab.ScriptBindAllocation == NULL) {
312        LOG_API("Couldn't initialize dispatchTab.ScriptBindAllocation");
313        return false;
314    }
315    dispatchTab.ScriptSetTimeZone = (ScriptSetTimeZoneFnPtr)dlsym(handle, "rsScriptSetTimeZone");
316    if (dispatchTab.ScriptSetTimeZone == NULL) {
317        LOG_API("Couldn't initialize dispatchTab.ScriptSetTimeZone");
318        return false;
319    }
320    dispatchTab.ScriptInvoke = (ScriptInvokeFnPtr)dlsym(handle, "rsScriptInvoke");
321    if (dispatchTab.ScriptInvoke == NULL) {
322        LOG_API("Couldn't initialize dispatchTab.ScriptInvoke");
323        return false;
324    }
325    dispatchTab.ScriptInvokeV = (ScriptInvokeVFnPtr)dlsym(handle, "rsScriptInvokeV");
326    if (dispatchTab.ScriptInvokeV == NULL) {
327        LOG_API("Couldn't initialize dispatchTab.ScriptInvokeV");
328        return false;
329    }
330    dispatchTab.ScriptForEach = (ScriptForEachFnPtr)dlsym(handle, "rsScriptForEach");
331    if (dispatchTab.ScriptForEach == NULL) {
332        LOG_API("Couldn't initialize dispatchTab.ScriptForEach");
333        return false;
334    }
335    dispatchTab.ScriptSetVarI = (ScriptSetVarIFnPtr)dlsym(handle, "rsScriptSetVarI");
336    if (dispatchTab.ScriptSetVarI == NULL) {
337        LOG_API("Couldn't initialize dispatchTab.ScriptSetVarI");
338        return false;
339    }
340    dispatchTab.ScriptSetVarObj = (ScriptSetVarObjFnPtr)dlsym(handle, "rsScriptSetVarObj");
341    if (dispatchTab.ScriptSetVarObj == NULL) {
342        LOG_API("Couldn't initialize dispatchTab.ScriptSetVarObj");
343        return false;
344    }
345    dispatchTab.ScriptSetVarJ = (ScriptSetVarJFnPtr)dlsym(handle, "rsScriptSetVarJ");
346    if (dispatchTab.ScriptSetVarJ == NULL) {
347        LOG_API("Couldn't initialize dispatchTab.ScriptSetVarJ");
348        return false;
349    }
350    dispatchTab.ScriptSetVarF = (ScriptSetVarFFnPtr)dlsym(handle, "rsScriptSetVarF");
351    if (dispatchTab.ScriptSetVarF == NULL) {
352        LOG_API("Couldn't initialize dispatchTab.ScriptSetVarF");
353        return false;
354    }
355    dispatchTab.ScriptSetVarD = (ScriptSetVarDFnPtr)dlsym(handle, "rsScriptSetVarD");
356    if (dispatchTab.ScriptSetVarD == NULL) {
357        LOG_API("Couldn't initialize dispatchTab.ScriptSetVarD");
358        return false;
359    }
360    dispatchTab.ScriptSetVarV = (ScriptSetVarVFnPtr)dlsym(handle, "rsScriptSetVarV");
361    if (dispatchTab.ScriptSetVarV == NULL) {
362        LOG_API("Couldn't initialize dispatchTab.ScriptSetVarV");
363        return false;
364    }
365    dispatchTab.ScriptGetVarV = (ScriptGetVarVFnPtr)dlsym(handle, "rsScriptGetVarV");
366    if (dispatchTab.ScriptGetVarV == NULL) {
367        LOG_API("Couldn't initialize dispatchTab.ScriptGetVarV");
368        return false;
369    }
370    dispatchTab.ScriptSetVarVE = (ScriptSetVarVEFnPtr)dlsym(handle, "rsScriptSetVarVE");
371    if (dispatchTab.ScriptSetVarVE == NULL) {
372        LOG_API("Couldn't initialize dispatchTab.ScriptSetVarVE");
373        return false;
374    }
375    dispatchTab.ScriptCCreate = (ScriptCCreateFnPtr)dlsym(handle, "rsScriptCCreate");
376    if (dispatchTab.ScriptCCreate == NULL) {
377        LOG_API("Couldn't initialize dispatchTab.ScriptCCreate");
378        return false;
379    }
380    dispatchTab.ScriptIntrinsicCreate = (ScriptIntrinsicCreateFnPtr)dlsym(handle, "rsScriptIntrinsicCreate");
381    if (dispatchTab.ScriptIntrinsicCreate == NULL) {
382        LOG_API("Couldn't initialize dispatchTab.ScriptIntrinsicCreate");
383        return false;
384    }
385    dispatchTab.ScriptKernelIDCreate = (ScriptKernelIDCreateFnPtr)dlsym(handle, "rsScriptKernelIDCreate");
386    if (dispatchTab.ScriptKernelIDCreate == NULL) {
387        LOG_API("Couldn't initialize dispatchTab.ScriptKernelIDCreate");
388        return false;
389    }
390    dispatchTab.ScriptFieldIDCreate = (ScriptFieldIDCreateFnPtr)dlsym(handle, "rsScriptFieldIDCreate");
391    if (dispatchTab.ScriptFieldIDCreate == NULL) {
392        LOG_API("Couldn't initialize dispatchTab.ScriptFieldIDCreate");
393        return false;
394    }
395    dispatchTab.ScriptGroupCreate = (ScriptGroupCreateFnPtr)dlsym(handle, "rsScriptGroupCreate");
396    if (dispatchTab.ScriptGroupCreate == NULL) {
397        LOG_API("Couldn't initialize dispatchTab.ScriptGroupCreate");
398        return false;
399    }
400    dispatchTab.ScriptGroupSetOutput = (ScriptGroupSetOutputFnPtr)dlsym(handle, "rsScriptGroupSetOutput");
401    if (dispatchTab.ScriptGroupSetOutput == NULL) {
402        LOG_API("Couldn't initialize dispatchTab.ScriptGroupSetOutput");
403        return false;
404    }
405    dispatchTab.ScriptGroupSetInput = (ScriptGroupSetInputFnPtr)dlsym(handle, "rsScriptGroupSetInput");
406    if (dispatchTab.ScriptGroupSetInput == NULL) {
407        LOG_API("Couldn't initialize dispatchTab.ScriptGroupSetInput");
408        return false;
409    }
410    dispatchTab.ScriptGroupExecute = (ScriptGroupExecuteFnPtr)dlsym(handle, "rsScriptGroupExecute");
411    if (dispatchTab.ScriptGroupExecute == NULL) {
412        LOG_API("Couldn't initialize dispatchTab.ScriptGroupExecute");
413        return false;
414    }
415    dispatchTab.AllocationIoSend = (AllocationIoSendFnPtr)dlsym(handle, "rsAllocationIoSend");
416    if (dispatchTab.AllocationIoSend == NULL) {
417        LOG_API("Couldn't initialize dispatchTab.AllocationIoSend");
418        return false;
419    }
420    dispatchTab.AllocationIoReceive = (AllocationIoReceiveFnPtr)dlsym(handle, "rsAllocationIoReceive");
421    if (dispatchTab.AllocationIoReceive == NULL) {
422        LOG_API("Couldn't initialize dispatchTab.AllocationIoReceive");
423        return false;
424    }
425    dispatchTab.AllocationGetPointer = (AllocationGetPointerFnPtr)dlsym(handle, "rsAllocationGetPointer");
426    if (dispatchTab.AllocationGetPointer == NULL) {
427        LOG_API("Couldn't initialize dispatchTab.AllocationGetPointer");
428        return false;
429    }
430
431    return true;
432}
433
434
435static jboolean nLoadSO(JNIEnv *_env, jobject _this, jboolean useNative) {
436    void* handle = NULL;
437    if (useNative) {
438        handle = dlopen("libRS.so", RTLD_LAZY | RTLD_LOCAL);
439    } else {
440        handle = dlopen("libRSSupport.so", RTLD_LAZY | RTLD_LOCAL);
441    }
442    if (handle == NULL) {
443        LOG_API("couldn't dlopen %s, %s", filename, dlerror());
444        return false;
445    }
446
447    if (loadSymbols(handle) == false) {
448        LOG_API("%s init failed!", filename);
449        return false;
450    }
451    LOG_API("Successfully loaded %s", filename);
452    return true;
453}
454
455// ---------------------------------------------------------------------------
456
457static void
458nContextFinish(JNIEnv *_env, jobject _this, RsContext con)
459{
460    LOG_API("nContextFinish, con(%p)", con);
461    dispatchTab.ContextFinish(con);
462}
463
464static void
465nObjDestroy(JNIEnv *_env, jobject _this, RsContext con, jint obj)
466{
467    LOG_API("nObjDestroy, con(%p) obj(%p)", con, (void *)obj);
468    dispatchTab.ObjDestroy(con, (void *)obj);
469}
470
471// ---------------------------------------------------------------------------
472
473static jint
474nDeviceCreate(JNIEnv *_env, jobject _this)
475{
476    LOG_API("nDeviceCreate");
477    return (jint)dispatchTab.DeviceCreate();
478}
479
480static void
481nDeviceDestroy(JNIEnv *_env, jobject _this, jint dev)
482{
483    LOG_API("nDeviceDestroy");
484    return dispatchTab.DeviceDestroy((RsDevice)dev);
485}
486
487static void
488nDeviceSetConfig(JNIEnv *_env, jobject _this, jint dev, jint p, jint value)
489{
490    LOG_API("nDeviceSetConfig  dev(%p), param(%i), value(%i)", (void *)dev, p, value);
491    return dispatchTab.DeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value);
492}
493
494static jint
495nContextCreate(JNIEnv *_env, jobject _this, jint dev, jint ver, jint sdkVer, jint ct)
496{
497    LOG_API("nContextCreate");
498    return (jint)dispatchTab.ContextCreate((RsDevice)dev, ver, sdkVer, (RsContextType)ct, 0);
499}
500
501
502static void
503nContextSetPriority(JNIEnv *_env, jobject _this, RsContext con, jint p)
504{
505    LOG_API("ContextSetPriority, con(%p), priority(%i)", con, p);
506    dispatchTab.ContextSetPriority(con, p);
507}
508
509
510
511static void
512nContextDestroy(JNIEnv *_env, jobject _this, RsContext con)
513{
514    LOG_API("nContextDestroy, con(%p)", con);
515    dispatchTab.ContextDestroy(con);
516}
517
518static void
519nContextDump(JNIEnv *_env, jobject _this, RsContext con, jint bits)
520{
521    LOG_API("nContextDump, con(%p)  bits(%i)", (RsContext)con, bits);
522    dispatchTab.ContextDump((RsContext)con, bits);
523}
524
525
526static jstring
527nContextGetErrorMessage(JNIEnv *_env, jobject _this, RsContext con)
528{
529    LOG_API("nContextGetErrorMessage, con(%p)", con);
530    char buf[1024];
531
532    size_t receiveLen;
533    uint32_t subID;
534    int id = dispatchTab.ContextGetMessage(con,
535                                 buf, sizeof(buf),
536                                 &receiveLen, sizeof(receiveLen),
537                                 &subID, sizeof(subID));
538    if (!id && receiveLen) {
539        //        __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG,
540        //            "message receive buffer too small.  %zu", receiveLen);
541    }
542    return _env->NewStringUTF(buf);
543}
544
545static jint
546nContextGetUserMessage(JNIEnv *_env, jobject _this, RsContext con, jintArray data)
547{
548    jint len = _env->GetArrayLength(data);
549    LOG_API("nContextGetMessage, con(%p), len(%i)", con, len);
550    jint *ptr = _env->GetIntArrayElements(data, NULL);
551    size_t receiveLen;
552    uint32_t subID;
553    int id = dispatchTab.ContextGetMessage(con,
554                                 ptr, len * 4,
555                                 &receiveLen, sizeof(receiveLen),
556                                 &subID, sizeof(subID));
557    if (!id && receiveLen) {
558        //        __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG,
559        //            "message receive buffer too small.  %zu", receiveLen);
560    }
561    _env->ReleaseIntArrayElements(data, ptr, 0);
562    return id;
563}
564
565static jint
566nContextPeekMessage(JNIEnv *_env, jobject _this, RsContext con, jintArray auxData)
567{
568    LOG_API("nContextPeekMessage, con(%p)", con);
569    jint *auxDataPtr = _env->GetIntArrayElements(auxData, NULL);
570    size_t receiveLen;
571    uint32_t subID;
572    int id = dispatchTab.ContextPeekMessage(con, &receiveLen, sizeof(receiveLen),
573                                  &subID, sizeof(subID));
574    auxDataPtr[0] = (jint)subID;
575    auxDataPtr[1] = (jint)receiveLen;
576    _env->ReleaseIntArrayElements(auxData, auxDataPtr, 0);
577    return id;
578}
579
580static void nContextInitToClient(JNIEnv *_env, jobject _this, RsContext con)
581{
582    LOG_API("nContextInitToClient, con(%p)", con);
583    dispatchTab.ContextInitToClient(con);
584}
585
586static void nContextDeinitToClient(JNIEnv *_env, jobject _this, RsContext con)
587{
588    LOG_API("nContextDeinitToClient, con(%p)", con);
589    dispatchTab.ContextDeinitToClient(con);
590}
591
592static void
593nContextSendMessage(JNIEnv *_env, jobject _this, RsContext con, jint id, jintArray data)
594{
595    jint *ptr = NULL;
596    jint len = 0;
597    if (data) {
598        len = _env->GetArrayLength(data);
599        jint *ptr = _env->GetIntArrayElements(data, NULL);
600    }
601    LOG_API("nContextSendMessage, con(%p), id(%i), len(%i)", con, id, len);
602    dispatchTab.ContextSendMessage(con, id, (const uint8_t *)ptr, len * sizeof(int));
603    if (data) {
604        _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
605    }
606}
607
608
609
610static jint
611nElementCreate(JNIEnv *_env, jobject _this, RsContext con, jint type, jint kind, jboolean norm, jint size)
612{
613    LOG_API("nElementCreate, con(%p), type(%i), kind(%i), norm(%i), size(%i)", con, type, kind, norm, size);
614    return (jint)dispatchTab.ElementCreate(con, (RsDataType)type, (RsDataKind)kind, norm, size);
615}
616
617static jint
618nElementCreate2(JNIEnv *_env, jobject _this, RsContext con,
619                jintArray _ids, jobjectArray _names, jintArray _arraySizes)
620{
621    int fieldCount = _env->GetArrayLength(_ids);
622    LOG_API("nElementCreate2, con(%p)", con);
623
624    jint *ids = _env->GetIntArrayElements(_ids, NULL);
625    jint *arraySizes = _env->GetIntArrayElements(_arraySizes, NULL);
626
627    AutoJavaStringArrayToUTF8 names(_env, _names, fieldCount);
628
629    const char **nameArray = names.c_str();
630    size_t *sizeArray = names.c_str_len();
631
632    jint id = (jint)dispatchTab.ElementCreate2(con,
633                                     (RsElement *)ids, fieldCount,
634                                     nameArray, fieldCount * sizeof(size_t),  sizeArray,
635                                     (const uint32_t *)arraySizes, fieldCount);
636
637    _env->ReleaseIntArrayElements(_ids, ids, JNI_ABORT);
638    _env->ReleaseIntArrayElements(_arraySizes, arraySizes, JNI_ABORT);
639    return (jint)id;
640}
641
642
643
644static void
645nElementGetSubElements(JNIEnv *_env, jobject _this, RsContext con, jint id,
646                       jintArray _IDs,
647                       jobjectArray _names,
648                       jintArray _arraySizes)
649{
650    int dataSize = _env->GetArrayLength(_IDs);
651    LOG_API("nElementGetSubElements, con(%p)", con);
652
653    uint32_t *ids = (uint32_t *)malloc((uint32_t)dataSize * sizeof(uint32_t));
654    const char **names = (const char **)malloc((uint32_t)dataSize * sizeof(const char *));
655    uint32_t *arraySizes = (uint32_t *)malloc((uint32_t)dataSize * sizeof(uint32_t));
656
657    dispatchTab.ElementGetSubElements(con, (RsElement)id, ids, names, arraySizes, (uint32_t)dataSize);
658
659    for(jint i = 0; i < dataSize; i++) {
660        _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
661        _env->SetIntArrayRegion(_IDs, i, 1, (const jint*)&ids[i]);
662        _env->SetIntArrayRegion(_arraySizes, i, 1, (const jint*)&arraySizes[i]);
663    }
664
665    free(ids);
666    free(names);
667    free(arraySizes);
668}
669
670// -----------------------------------
671
672static int
673nTypeCreate(JNIEnv *_env, jobject _this, RsContext con, RsElement eid,
674            jint dimx, jint dimy, jint dimz, jboolean mips, jboolean faces, jint yuv)
675{
676    LOG_API("nTypeCreate, con(%p) eid(%p), x(%i), y(%i), z(%i), mips(%i), faces(%i), yuv(%i)",
677            con, eid, dimx, dimy, dimz, mips, faces, yuv);
678
679    jint id = (jint)dispatchTab.TypeCreate(con, (RsElement)eid, dimx, dimy, dimz, mips, faces, yuv);
680    return (jint)id;
681}
682
683// -----------------------------------
684
685static jint
686nAllocationCreateTyped(JNIEnv *_env, jobject _this, RsContext con, jint type, jint mips, jint usage, jint pointer)
687{
688    LOG_API("nAllocationCreateTyped, con(%p), type(%p), mip(%i), usage(%i), ptr(%p)", con, (RsElement)type, mips, usage, (void *)pointer);
689    return (jint) dispatchTab.AllocationCreateTyped(con, (RsType)type, (RsAllocationMipmapControl)mips, (uint32_t)usage, (uint32_t)pointer);
690}
691
692static void
693nAllocationSyncAll(JNIEnv *_env, jobject _this, RsContext con, jint a, jint bits)
694{
695    LOG_API("nAllocationSyncAll, con(%p), a(%p), bits(0x%08x)", con, (RsAllocation)a, bits);
696    dispatchTab.AllocationSyncAll(con, (RsAllocation)a, (RsAllocationUsageType)bits);
697}
698
699static void
700nAllocationGenerateMipmaps(JNIEnv *_env, jobject _this, RsContext con, jint alloc)
701{
702    LOG_API("nAllocationGenerateMipmaps, con(%p), a(%p)", con, (RsAllocation)alloc);
703    dispatchTab.AllocationGenerateMipmaps(con, (RsAllocation)alloc);
704}
705
706static size_t GetBitmapSize(JNIEnv *env, jobject jbitmap) {
707    AndroidBitmapInfo info;
708    memset(&info, 0, sizeof(info));
709    AndroidBitmap_getInfo(env, jbitmap, &info);
710    size_t s = info.width * info.height;
711    switch (info.format) {
712        case ANDROID_BITMAP_FORMAT_RGBA_8888: s *= 4; break;
713        case ANDROID_BITMAP_FORMAT_RGB_565: s *= 2; break;
714        case ANDROID_BITMAP_FORMAT_RGBA_4444: s *= 2; break;
715    }
716    return s;
717}
718
719static int
720nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, RsContext con, jint type, jint mip, jobject jbitmap, jint usage)
721{
722    jint id = 0;
723    void *pixels = NULL;
724    AndroidBitmap_lockPixels(_env, jbitmap, &pixels);
725
726    if (pixels != NULL) {
727        id = (jint)dispatchTab.AllocationCreateFromBitmap(con,
728                                                (RsType)type, (RsAllocationMipmapControl)mip,
729                                                pixels, GetBitmapSize(_env, jbitmap), usage);
730        AndroidBitmap_unlockPixels(_env, jbitmap);
731    }
732    return id;
733}
734
735static int
736nAllocationCreateBitmapBackedAllocation(JNIEnv *_env, jobject _this, RsContext con, jint type, jint mip, jobject jbitmap, jint usage)
737{
738    jint id = 0;
739    void *pixels = NULL;
740    AndroidBitmap_lockPixels(_env, jbitmap, &pixels);
741
742    if (pixels != NULL) {
743        id = (jint)dispatchTab.AllocationCreateTyped(con,
744                                          (RsType)type, (RsAllocationMipmapControl)mip,
745                                          (uint32_t)usage, (uintptr_t)pixels);
746        AndroidBitmap_unlockPixels(_env, jbitmap);
747    }
748    return id;
749}
750
751static int
752nAllocationCubeCreateFromBitmap(JNIEnv *_env, jobject _this, RsContext con, jint type, jint mip, jobject jbitmap, jint usage)
753{
754    void *pixels = NULL;
755    AndroidBitmap_lockPixels(_env, jbitmap, &pixels);
756
757    jint id = 0;
758    if (pixels != NULL) {
759        id = (jint)dispatchTab.AllocationCubeCreateFromBitmap(con,
760                                                    (RsType)type, (RsAllocationMipmapControl)mip,
761                                                    pixels, GetBitmapSize(_env, jbitmap), usage);
762        AndroidBitmap_unlockPixels(_env, jbitmap);
763    }
764    return id;
765}
766
767static void
768nAllocationCopyFromBitmap(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jobject jbitmap)
769{
770    AndroidBitmapInfo info;
771    memset(&info, 0, sizeof(info));
772    AndroidBitmap_getInfo(_env, jbitmap, &info);
773
774    void *pixels = NULL;
775    AndroidBitmap_lockPixels(_env, jbitmap, &pixels);
776
777    if (pixels != NULL) {
778        dispatchTab.Allocation2DData(con, (RsAllocation)alloc, 0, 0,
779                           0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
780                           info.width, info.height, pixels, GetBitmapSize(_env, jbitmap), 0);
781        AndroidBitmap_unlockPixels(_env, jbitmap);
782    }
783}
784
785static void
786nAllocationCopyToBitmap(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jobject jbitmap)
787{
788    AndroidBitmapInfo info;
789    memset(&info, 0, sizeof(info));
790    AndroidBitmap_getInfo(_env, jbitmap, &info);
791
792    void *pixels = NULL;
793    AndroidBitmap_lockPixels(_env, jbitmap, &pixels);
794
795    if (pixels != NULL) {
796        dispatchTab.AllocationCopyToBitmap(con, (RsAllocation)alloc, pixels, GetBitmapSize(_env, jbitmap));
797        AndroidBitmap_unlockPixels(_env, jbitmap);
798    }
799    //bitmap.notifyPixelsChanged();
800}
801
802
803static void
804nAllocationData1D_i(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint lod, jint count, jintArray data, int sizeBytes)
805{
806    jint len = _env->GetArrayLength(data);
807    LOG_API("nAllocation1DData_i, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
808    jint *ptr = _env->GetIntArrayElements(data, NULL);
809    dispatchTab.Allocation1DData(con, (RsAllocation)alloc, offset, lod, count, ptr, sizeBytes);
810    _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
811}
812
813static void
814nAllocationData1D_s(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint lod, jint count, jshortArray data, int sizeBytes)
815{
816    jint len = _env->GetArrayLength(data);
817    LOG_API("nAllocation1DData_s, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
818    jshort *ptr = _env->GetShortArrayElements(data, NULL);
819    dispatchTab.Allocation1DData(con, (RsAllocation)alloc, offset, lod, count, ptr, sizeBytes);
820    _env->ReleaseShortArrayElements(data, ptr, JNI_ABORT);
821}
822
823static void
824nAllocationData1D_b(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint lod, jint count, jbyteArray data, int sizeBytes)
825{
826    jint len = _env->GetArrayLength(data);
827    LOG_API("nAllocation1DData_b, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
828    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
829    dispatchTab.Allocation1DData(con, (RsAllocation)alloc, offset, lod, count, ptr, sizeBytes);
830    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
831}
832
833static void
834nAllocationData1D_f(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint lod, jint count, jfloatArray data, int sizeBytes)
835{
836    jint len = _env->GetArrayLength(data);
837    LOG_API("nAllocation1DData_f, con(%p), adapter(%p), offset(%i), count(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, count, len, sizeBytes);
838    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
839    dispatchTab.Allocation1DData(con, (RsAllocation)alloc, offset, lod, count, ptr, sizeBytes);
840    _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
841}
842
843static void
844//    native void rsnAllocationElementData1D(int con, int id, int xoff, int compIdx, byte[] d, int sizeBytes);
845nAllocationElementData1D(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint offset, jint lod, jint compIdx, jbyteArray data, int sizeBytes)
846{
847    jint len = _env->GetArrayLength(data);
848    LOG_API("nAllocationElementData1D, con(%p), alloc(%p), offset(%i), comp(%i), len(%i), sizeBytes(%i)", con, (RsAllocation)alloc, offset, compIdx, len, sizeBytes);
849    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
850    dispatchTab.Allocation1DElementData(con, (RsAllocation)alloc, offset, lod, ptr, sizeBytes, compIdx);
851    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
852}
853
854static void
855nAllocationData2D_s(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint lod, jint face,
856                    jint w, jint h, jshortArray data, int sizeBytes)
857{
858    jint len = _env->GetArrayLength(data);
859    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);
860    jshort *ptr = _env->GetShortArrayElements(data, NULL);
861    dispatchTab.Allocation2DData(con, (RsAllocation)alloc, xoff, yoff, lod, (RsAllocationCubemapFace)face, w, h, ptr, sizeBytes, 0);
862    _env->ReleaseShortArrayElements(data, ptr, JNI_ABORT);
863}
864
865static void
866nAllocationData2D_b(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint lod, jint face,
867                    jint w, jint h, jbyteArray data, int sizeBytes)
868{
869    jint len = _env->GetArrayLength(data);
870    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);
871    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
872    dispatchTab.Allocation2DData(con, (RsAllocation)alloc, xoff, yoff, lod, (RsAllocationCubemapFace)face, w, h, ptr, sizeBytes, 0);
873    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
874}
875
876static void
877nAllocationData2D_i(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint lod, jint face,
878                    jint w, jint h, jintArray data, int sizeBytes)
879{
880    jint len = _env->GetArrayLength(data);
881    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);
882    jint *ptr = _env->GetIntArrayElements(data, NULL);
883    dispatchTab.Allocation2DData(con, (RsAllocation)alloc, xoff, yoff, lod, (RsAllocationCubemapFace)face, w, h, ptr, sizeBytes, 0);
884    _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
885}
886
887static void
888nAllocationData2D_f(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint lod, jint face,
889                    jint w, jint h, jfloatArray data, int sizeBytes)
890{
891    jint len = _env->GetArrayLength(data);
892    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);
893    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
894    dispatchTab.Allocation2DData(con, (RsAllocation)alloc, xoff, yoff, lod, (RsAllocationCubemapFace)face, w, h, ptr, sizeBytes, 0);
895    _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
896}
897
898static void
899nAllocationData2D_alloc(JNIEnv *_env, jobject _this, RsContext con,
900                        jint dstAlloc, jint dstXoff, jint dstYoff,
901                        jint dstMip, jint dstFace,
902                        jint width, jint height,
903                        jint srcAlloc, jint srcXoff, jint srcYoff,
904                        jint srcMip, jint srcFace)
905{
906    LOG_API("nAllocation2DData_s, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
907            " dstMip(%i), dstFace(%i), width(%i), height(%i),"
908            " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i), srcFace(%i)",
909            con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip, dstFace,
910            width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
911
912    dispatchTab.AllocationCopy2DRange(con,
913                            (RsAllocation)dstAlloc,
914                            dstXoff, dstYoff,
915                            dstMip, dstFace,
916                            width, height,
917                            (RsAllocation)srcAlloc,
918                            srcXoff, srcYoff,
919                            srcMip, srcFace);
920}
921
922static void
923nAllocationData3D_s(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint zoff, jint lod,
924                    jint w, jint h, jint d, jshortArray data, int sizeBytes)
925{
926    jint len = _env->GetArrayLength(data);
927    LOG_API("nAllocation3DData_s, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)", con, (RsAllocation)alloc, xoff, yoff, zoff, w, h, d, len);
928    jshort *ptr = _env->GetShortArrayElements(data, NULL);
929    dispatchTab.Allocation3DData(con, (RsAllocation)alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
930    _env->ReleaseShortArrayElements(data, ptr, JNI_ABORT);
931}
932
933static void
934nAllocationData3D_b(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint zoff, jint lod,
935                    jint w, jint h, jint d, jbyteArray data, int sizeBytes)
936{
937    jint len = _env->GetArrayLength(data);
938    LOG_API("nAllocation3DData_b, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)", con, (RsAllocation)alloc, xoff, yoff, zoff, w, h, d, len);
939    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
940    dispatchTab.Allocation3DData(con, (RsAllocation)alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
941    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
942}
943
944static void
945nAllocationData3D_i(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint zoff, jint lod,
946                    jint w, jint h, jint d, jintArray data, int sizeBytes)
947{
948    jint len = _env->GetArrayLength(data);
949    LOG_API("nAllocation3DData_i, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)", con, (RsAllocation)alloc, xoff, yoff, zoff, w, h, d, len);
950    jint *ptr = _env->GetIntArrayElements(data, NULL);
951    dispatchTab.Allocation3DData(con, (RsAllocation)alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
952    _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
953}
954
955static void
956nAllocationData3D_f(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint xoff, jint yoff, jint zoff, jint lod,
957                    jint w, jint h, jint d, jfloatArray data, int sizeBytes)
958{
959    jint len = _env->GetArrayLength(data);
960    LOG_API("nAllocation3DData_f, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i)", con, (RsAllocation)alloc, xoff, yoff, zoff, w, h, d, len);
961    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
962    dispatchTab.Allocation3DData(con, (RsAllocation)alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
963    _env->ReleaseFloatArrayElements(data, ptr, JNI_ABORT);
964}
965
966static void
967nAllocationData3D_alloc(JNIEnv *_env, jobject _this, RsContext con,
968                        jint dstAlloc, jint dstXoff, jint dstYoff, jint dstZoff,
969                        jint dstMip,
970                        jint width, jint height, jint depth,
971                        jint srcAlloc, jint srcXoff, jint srcYoff, jint srcZoff,
972                        jint srcMip)
973{
974    LOG_API("nAllocationData3D_alloc, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
975            " dstMip(%i), width(%i), height(%i),"
976            " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i)",
977            con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip, dstFace,
978            width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
979
980    dispatchTab.AllocationCopy3DRange(con,
981                            (RsAllocation)dstAlloc,
982                            dstXoff, dstYoff, dstZoff, dstMip,
983                            width, height, depth,
984                            (RsAllocation)srcAlloc,
985                            srcXoff, srcYoff, srcZoff, srcMip);
986}
987
988static void
989nAllocationRead_i(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jintArray data)
990{
991    jint len = _env->GetArrayLength(data);
992    LOG_API("nAllocationRead_i, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
993    jint *ptr = _env->GetIntArrayElements(data, NULL);
994    jsize length = _env->GetArrayLength(data);
995    dispatchTab.AllocationRead(con, (RsAllocation)alloc, ptr, length * sizeof(int));
996    _env->ReleaseIntArrayElements(data, ptr, 0);
997}
998
999static void
1000nAllocationRead_s(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jshortArray data)
1001{
1002    jint len = _env->GetArrayLength(data);
1003    LOG_API("nAllocationRead_i, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
1004    jshort *ptr = _env->GetShortArrayElements(data, NULL);
1005    jsize length = _env->GetArrayLength(data);
1006    dispatchTab.AllocationRead(con, (RsAllocation)alloc, ptr, length * sizeof(short));
1007    _env->ReleaseShortArrayElements(data, ptr, 0);
1008}
1009
1010static void
1011nAllocationRead_b(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jbyteArray data)
1012{
1013    jint len = _env->GetArrayLength(data);
1014    LOG_API("nAllocationRead_i, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
1015    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
1016    jsize length = _env->GetArrayLength(data);
1017    dispatchTab.AllocationRead(con, (RsAllocation)alloc, ptr, length * sizeof(char));
1018    _env->ReleaseByteArrayElements(data, ptr, 0);
1019}
1020
1021static void
1022nAllocationRead_f(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jfloatArray data)
1023{
1024    jint len = _env->GetArrayLength(data);
1025    LOG_API("nAllocationRead_f, con(%p), alloc(%p), len(%i)", con, (RsAllocation)alloc, len);
1026    jfloat *ptr = _env->GetFloatArrayElements(data, NULL);
1027    jsize length = _env->GetArrayLength(data);
1028    dispatchTab.AllocationRead(con, (RsAllocation)alloc, ptr, length * sizeof(float));
1029    _env->ReleaseFloatArrayElements(data, ptr, 0);
1030}
1031
1032static jint
1033nAllocationGetType(JNIEnv *_env, jobject _this, RsContext con, jint a)
1034{
1035    LOG_API("nAllocationGetType, con(%p), a(%p)", con, (RsAllocation)a);
1036    return (jint) dispatchTab.AllocationGetType(con, (RsAllocation)a);
1037}
1038
1039static void
1040nAllocationResize1D(JNIEnv *_env, jobject _this, RsContext con, jint alloc, jint dimX)
1041{
1042    LOG_API("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", con, (RsAllocation)alloc, dimX);
1043    dispatchTab.AllocationResize1D(con, (RsAllocation)alloc, dimX);
1044}
1045
1046// -----------------------------------
1047
1048static void
1049nScriptBindAllocation(JNIEnv *_env, jobject _this, RsContext con, jint script, jint alloc, jint slot)
1050{
1051    LOG_API("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", con, (RsScript)script, (RsAllocation)alloc, slot);
1052    dispatchTab.ScriptBindAllocation(con, (RsScript)script, (RsAllocation)alloc, slot);
1053}
1054
1055static void
1056nScriptSetVarI(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jint val)
1057{
1058    LOG_API("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", con, (void *)script, slot, val);
1059    dispatchTab.ScriptSetVarI(con, (RsScript)script, slot, val);
1060}
1061
1062static void
1063nScriptSetVarObj(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jint val)
1064{
1065    LOG_API("nScriptSetVarObj, con(%p), s(%p), slot(%i), val(%i)", con, (void *)script, slot, val);
1066    dispatchTab.ScriptSetVarObj(con, (RsScript)script, slot, (RsObjectBase)val);
1067}
1068
1069static void
1070nScriptSetVarJ(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jlong val)
1071{
1072    LOG_API("nScriptSetVarJ, con(%p), s(%p), slot(%i), val(%lli)", con, (void *)script, slot, val);
1073    dispatchTab.ScriptSetVarJ(con, (RsScript)script, slot, val);
1074}
1075
1076static void
1077nScriptSetVarF(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, float val)
1078{
1079    LOG_API("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", con, (void *)script, slot, val);
1080    dispatchTab.ScriptSetVarF(con, (RsScript)script, slot, val);
1081}
1082
1083static void
1084nScriptSetVarD(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, double val)
1085{
1086    LOG_API("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", con, (void *)script, slot, val);
1087    dispatchTab.ScriptSetVarD(con, (RsScript)script, slot, val);
1088}
1089
1090static void
1091nScriptSetVarV(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jbyteArray data)
1092{
1093    LOG_API("nScriptSetVarV, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
1094    jint len = _env->GetArrayLength(data);
1095    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
1096    dispatchTab.ScriptSetVarV(con, (RsScript)script, slot, ptr, len);
1097    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1098}
1099
1100static void
1101nScriptSetVarVE(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jbyteArray data, jint elem, jintArray dims)
1102{
1103    LOG_API("nScriptSetVarVE, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
1104    jint len = _env->GetArrayLength(data);
1105    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
1106    jint dimsLen = _env->GetArrayLength(dims) * sizeof(int);
1107    jint *dimsPtr = _env->GetIntArrayElements(dims, NULL);
1108    dispatchTab.ScriptSetVarVE(con, (RsScript)script, slot, ptr, len, (RsElement)elem,
1109                     (const uint32_t *)dimsPtr, dimsLen);
1110    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1111    _env->ReleaseIntArrayElements(dims, dimsPtr, JNI_ABORT);
1112}
1113
1114
1115static void
1116nScriptSetTimeZone(JNIEnv *_env, jobject _this, RsContext con, jint script, jbyteArray timeZone)
1117{
1118    LOG_API("nScriptCSetTimeZone, con(%p), s(%p), timeZone(%s)", con, (void *)script, (const char *)timeZone);
1119
1120    jint length = _env->GetArrayLength(timeZone);
1121    jbyte* timeZone_ptr;
1122    timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
1123
1124    dispatchTab.ScriptSetTimeZone(con, (RsScript)script, (const char *)timeZone_ptr, length);
1125
1126    if (timeZone_ptr) {
1127        _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
1128    }
1129}
1130
1131static void
1132nScriptInvoke(JNIEnv *_env, jobject _this, RsContext con, jint obj, jint slot)
1133{
1134    LOG_API("nScriptInvoke, con(%p), script(%p)", con, (void *)obj);
1135    dispatchTab.ScriptInvoke(con, (RsScript)obj, slot);
1136}
1137
1138static void
1139nScriptInvokeV(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jbyteArray data)
1140{
1141    LOG_API("nScriptInvokeV, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
1142    jint len = _env->GetArrayLength(data);
1143    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
1144    dispatchTab.ScriptInvokeV(con, (RsScript)script, slot, ptr, len);
1145    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1146}
1147
1148static void
1149nScriptForEach(JNIEnv *_env, jobject _this, RsContext con,
1150               jint script, jint slot, jint ain, jint aout)
1151{
1152    LOG_API("nScriptForEach, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
1153    dispatchTab.ScriptForEach(con, (RsScript)script, slot, (RsAllocation)ain, (RsAllocation)aout, NULL, 0, NULL, 0);
1154}
1155static void
1156nScriptForEachV(JNIEnv *_env, jobject _this, RsContext con,
1157                jint script, jint slot, jint ain, jint aout, jbyteArray params)
1158{
1159    LOG_API("nScriptForEach, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
1160    jint len = _env->GetArrayLength(params);
1161    jbyte *ptr = _env->GetByteArrayElements(params, NULL);
1162    dispatchTab.ScriptForEach(con, (RsScript)script, slot, (RsAllocation)ain, (RsAllocation)aout, ptr, len, NULL, 0);
1163    _env->ReleaseByteArrayElements(params, ptr, JNI_ABORT);
1164}
1165
1166static void
1167nScriptForEachClipped(JNIEnv *_env, jobject _this, RsContext con,
1168                      jint script, jint slot, jint ain, jint aout,
1169                      jint xstart, jint xend,
1170                      jint ystart, jint yend, jint zstart, jint zend)
1171{
1172    LOG_API("nScriptForEachClipped, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
1173    RsScriptCall sc;
1174    sc.xStart = xstart;
1175    sc.xEnd = xend;
1176    sc.yStart = ystart;
1177    sc.yEnd = yend;
1178    sc.zStart = zstart;
1179    sc.zEnd = zend;
1180    sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
1181    sc.arrayStart = 0;
1182    sc.arrayEnd = 0;
1183    dispatchTab.ScriptForEach(con, (RsScript)script, slot, (RsAllocation)ain, (RsAllocation)aout, NULL, 0, &sc, sizeof(sc));
1184}
1185
1186static void
1187nScriptForEachClippedV(JNIEnv *_env, jobject _this, RsContext con,
1188                       jint script, jint slot, jint ain, jint aout,
1189                       jbyteArray params, jint xstart, jint xend,
1190                       jint ystart, jint yend, jint zstart, jint zend)
1191{
1192    LOG_API("nScriptForEachClipped, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
1193    jint len = _env->GetArrayLength(params);
1194    jbyte *ptr = _env->GetByteArrayElements(params, NULL);
1195    RsScriptCall sc;
1196    sc.xStart = xstart;
1197    sc.xEnd = xend;
1198    sc.yStart = ystart;
1199    sc.yEnd = yend;
1200    sc.zStart = zstart;
1201    sc.zEnd = zend;
1202    sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
1203    sc.arrayStart = 0;
1204    sc.arrayEnd = 0;
1205    dispatchTab.ScriptForEach(con, (RsScript)script, slot, (RsAllocation)ain, (RsAllocation)aout, ptr, len, &sc, sizeof(sc));
1206    _env->ReleaseByteArrayElements(params, ptr, JNI_ABORT);
1207}
1208
1209// -----------------------------------
1210
1211static jint
1212nScriptCCreate(JNIEnv *_env, jobject _this, RsContext con,
1213               jstring resName, jstring cacheDir,
1214               jbyteArray scriptRef, jint length)
1215{
1216    LOG_API("nScriptCCreate, con(%p)", con);
1217
1218    AutoJavaStringToUTF8 resNameUTF(_env, resName);
1219    AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
1220    jint ret = 0;
1221    jbyte* script_ptr = NULL;
1222    jint _exception = 0;
1223    jint remaining;
1224    if (!scriptRef) {
1225        _exception = 1;
1226        //jniThrowException(_env, "java/lang/IllegalArgumentException", "script == null");
1227        goto exit;
1228    }
1229    if (length < 0) {
1230        _exception = 1;
1231        //jniThrowException(_env, "java/lang/IllegalArgumentException", "length < 0");
1232        goto exit;
1233    }
1234    remaining = _env->GetArrayLength(scriptRef);
1235    if (remaining < length) {
1236        _exception = 1;
1237        //jniThrowException(_env, "java/lang/IllegalArgumentException",
1238        //        "length > script.length - offset");
1239        goto exit;
1240    }
1241    script_ptr = (jbyte *)
1242        _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
1243
1244    //rsScriptCSetText(con, (const char *)script_ptr, length);
1245
1246    ret = (jint)dispatchTab.ScriptCCreate(con,
1247                                resNameUTF.c_str(), resNameUTF.length(),
1248                                cacheDirUTF.c_str(), cacheDirUTF.length(),
1249                                (const char *)script_ptr, length);
1250
1251exit:
1252    if (script_ptr) {
1253        _env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr,
1254                _exception ? JNI_ABORT: 0);
1255    }
1256
1257    return ret;
1258}
1259
1260static jint
1261nScriptIntrinsicCreate(JNIEnv *_env, jobject _this, RsContext con, jint id, jint eid)
1262{
1263    LOG_API("nScriptIntrinsicCreate, con(%p) id(%i) element(%p)", con, id, (void *)eid);
1264    return (jint)dispatchTab.ScriptIntrinsicCreate(con, id, (RsElement)eid);
1265}
1266
1267static jint
1268nScriptKernelIDCreate(JNIEnv *_env, jobject _this, RsContext con, jint sid, jint slot, jint sig)
1269{
1270    LOG_API("nScriptKernelIDCreate, con(%p) script(%p), slot(%i), sig(%i)", con, (void *)sid, slot, sig);
1271    return (jint)dispatchTab.ScriptKernelIDCreate(con, (RsScript)sid, slot, sig);
1272}
1273
1274static jint
1275nScriptFieldIDCreate(JNIEnv *_env, jobject _this, RsContext con, jint sid, jint slot)
1276{
1277    LOG_API("nScriptFieldIDCreate, con(%p) script(%p), slot(%i)", con, (void *)sid, slot);
1278    return (jint)dispatchTab.ScriptFieldIDCreate(con, (RsScript)sid, slot);
1279}
1280
1281static jint
1282nScriptGroupCreate(JNIEnv *_env, jobject _this, RsContext con, jintArray _kernels, jintArray _src,
1283    jintArray _dstk, jintArray _dstf, jintArray _types)
1284{
1285    LOG_API("nScriptGroupCreate, con(%p)", con);
1286
1287    jint kernelsLen = _env->GetArrayLength(_kernels) * sizeof(int);
1288    jint *kernelsPtr = _env->GetIntArrayElements(_kernels, NULL);
1289    jint srcLen = _env->GetArrayLength(_src) * sizeof(int);
1290    jint *srcPtr = _env->GetIntArrayElements(_src, NULL);
1291    jint dstkLen = _env->GetArrayLength(_dstk) * sizeof(int);
1292    jint *dstkPtr = _env->GetIntArrayElements(_dstk, NULL);
1293    jint dstfLen = _env->GetArrayLength(_dstf) * sizeof(int);
1294    jint *dstfPtr = _env->GetIntArrayElements(_dstf, NULL);
1295    jint typesLen = _env->GetArrayLength(_types) * sizeof(int);
1296    jint *typesPtr = _env->GetIntArrayElements(_types, NULL);
1297
1298    int id = (int)dispatchTab.ScriptGroupCreate(con,
1299                               (RsScriptKernelID *)kernelsPtr, kernelsLen,
1300                               (RsScriptKernelID *)srcPtr, srcLen,
1301                               (RsScriptKernelID *)dstkPtr, dstkLen,
1302                               (RsScriptFieldID *)dstfPtr, dstfLen,
1303                               (RsType *)typesPtr, typesLen);
1304
1305    _env->ReleaseIntArrayElements(_kernels, kernelsPtr, 0);
1306    _env->ReleaseIntArrayElements(_src, srcPtr, 0);
1307    _env->ReleaseIntArrayElements(_dstk, dstkPtr, 0);
1308    _env->ReleaseIntArrayElements(_dstf, dstfPtr, 0);
1309    _env->ReleaseIntArrayElements(_types, typesPtr, 0);
1310    return id;
1311}
1312
1313static void
1314nScriptGroupSetInput(JNIEnv *_env, jobject _this, RsContext con, jint gid, jint kid, jint alloc)
1315{
1316    LOG_API("nScriptGroupSetInput, con(%p) group(%p), kernelId(%p), alloc(%p)", con,
1317        (void *)gid, (void *)kid, (void *)alloc);
1318    dispatchTab.ScriptGroupSetInput(con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
1319}
1320
1321static void
1322nScriptGroupSetOutput(JNIEnv *_env, jobject _this, RsContext con, jint gid, jint kid, jint alloc)
1323{
1324    LOG_API("nScriptGroupSetOutput, con(%p) group(%p), kernelId(%p), alloc(%p)", con,
1325        (void *)gid, (void *)kid, (void *)alloc);
1326    dispatchTab.ScriptGroupSetOutput(con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
1327}
1328
1329static void
1330nScriptGroupExecute(JNIEnv *_env, jobject _this, RsContext con, jint gid)
1331{
1332    LOG_API("nScriptGroupSetOutput, con(%p) group(%p)", con, (void *)gid);
1333    dispatchTab.ScriptGroupExecute(con, (RsScriptGroup)gid);
1334}
1335
1336// ---------------------------------------------------------------------------
1337
1338static jint
1339nSamplerCreate(JNIEnv *_env, jobject _this, RsContext con, jint magFilter, jint minFilter,
1340               jint wrapS, jint wrapT, jint wrapR, jfloat aniso)
1341{
1342    LOG_API("nSamplerCreate, con(%p)", con);
1343    return (jint)dispatchTab.SamplerCreate(con,
1344                                 (RsSamplerValue)magFilter,
1345                                 (RsSamplerValue)minFilter,
1346                                 (RsSamplerValue)wrapS,
1347                                 (RsSamplerValue)wrapT,
1348                                 (RsSamplerValue)wrapR,
1349                                 aniso);
1350}
1351
1352// ---------------------------------------------------------------------------
1353
1354
1355static const char *classPathName = "android/support/v8/renderscript/RenderScript";
1356
1357static JNINativeMethod methods[] = {
1358{"nLoadSO",                        "(Z)Z",                                    (bool*)nLoadSO },
1359{"nDeviceCreate",                  "()I",                                     (void*)nDeviceCreate },
1360{"nDeviceDestroy",                 "(I)V",                                    (void*)nDeviceDestroy },
1361{"nDeviceSetConfig",               "(III)V",                                  (void*)nDeviceSetConfig },
1362{"nContextGetUserMessage",         "(I[I)I",                                  (void*)nContextGetUserMessage },
1363{"nContextGetErrorMessage",        "(I)Ljava/lang/String;",                   (void*)nContextGetErrorMessage },
1364{"nContextPeekMessage",            "(I[I)I",                                  (void*)nContextPeekMessage },
1365
1366{"nContextInitToClient",           "(I)V",                                    (void*)nContextInitToClient },
1367{"nContextDeinitToClient",         "(I)V",                                    (void*)nContextDeinitToClient },
1368
1369
1370// All methods below are thread protected in java.
1371{"rsnContextCreate",                 "(IIII)I",                               (void*)nContextCreate },
1372{"rsnContextFinish",                 "(I)V",                                  (void*)nContextFinish },
1373{"rsnContextSetPriority",            "(II)V",                                 (void*)nContextSetPriority },
1374{"rsnContextDestroy",                "(I)V",                                  (void*)nContextDestroy },
1375{"rsnContextDump",                   "(II)V",                                 (void*)nContextDump },
1376{"rsnContextSendMessage",            "(II[I)V",                               (void*)nContextSendMessage },
1377{"rsnObjDestroy",                    "(II)V",                                 (void*)nObjDestroy },
1378
1379{"rsnElementCreate",                 "(IIIZI)I",                              (void*)nElementCreate },
1380{"rsnElementCreate2",                "(I[I[Ljava/lang/String;[I)I",           (void*)nElementCreate2 },
1381{"rsnElementGetSubElements",         "(II[I[Ljava/lang/String;[I)V",          (void*)nElementGetSubElements },
1382
1383{"rsnTypeCreate",                    "(IIIIIZZI)I",                           (void*)nTypeCreate },
1384
1385{"rsnAllocationCreateTyped",         "(IIIII)I",                               (void*)nAllocationCreateTyped },
1386{"rsnAllocationCreateFromBitmap",    "(IIILandroid/graphics/Bitmap;I)I",      (void*)nAllocationCreateFromBitmap },
1387{"rsnAllocationCreateBitmapBackedAllocation",    "(IIILandroid/graphics/Bitmap;I)I",      (void*)nAllocationCreateBitmapBackedAllocation },
1388{"rsnAllocationCubeCreateFromBitmap","(IIILandroid/graphics/Bitmap;I)I",      (void*)nAllocationCubeCreateFromBitmap },
1389
1390{"rsnAllocationCopyFromBitmap",      "(IILandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyFromBitmap },
1391{"rsnAllocationCopyToBitmap",        "(IILandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyToBitmap },
1392
1393{"rsnAllocationSyncAll",             "(III)V",                                (void*)nAllocationSyncAll },
1394{"rsnAllocationData1D",              "(IIIII[II)V",                           (void*)nAllocationData1D_i },
1395{"rsnAllocationData1D",              "(IIIII[SI)V",                           (void*)nAllocationData1D_s },
1396{"rsnAllocationData1D",              "(IIIII[BI)V",                           (void*)nAllocationData1D_b },
1397{"rsnAllocationData1D",              "(IIIII[FI)V",                           (void*)nAllocationData1D_f },
1398{"rsnAllocationElementData1D",       "(IIIII[BI)V",                           (void*)nAllocationElementData1D },
1399{"rsnAllocationData2D",              "(IIIIIIII[II)V",                        (void*)nAllocationData2D_i },
1400{"rsnAllocationData2D",              "(IIIIIIII[SI)V",                        (void*)nAllocationData2D_s },
1401{"rsnAllocationData2D",              "(IIIIIIII[BI)V",                        (void*)nAllocationData2D_b },
1402{"rsnAllocationData2D",              "(IIIIIIII[FI)V",                        (void*)nAllocationData2D_f },
1403{"rsnAllocationData2D",              "(IIIIIIIIIIIII)V",                      (void*)nAllocationData2D_alloc },
1404{"rsnAllocationData3D",              "(IIIIIIIII[II)V",                       (void*)nAllocationData3D_i },
1405{"rsnAllocationData3D",              "(IIIIIIIII[SI)V",                       (void*)nAllocationData3D_s },
1406{"rsnAllocationData3D",              "(IIIIIIIII[BI)V",                       (void*)nAllocationData3D_b },
1407{"rsnAllocationData3D",              "(IIIIIIIII[FI)V",                       (void*)nAllocationData3D_f },
1408{"rsnAllocationData3D",              "(IIIIIIIIIIIIII)V",                     (void*)nAllocationData3D_alloc },
1409{"rsnAllocationRead",                "(II[I)V",                               (void*)nAllocationRead_i },
1410{"rsnAllocationRead",                "(II[S)V",                               (void*)nAllocationRead_s },
1411{"rsnAllocationRead",                "(II[B)V",                               (void*)nAllocationRead_b },
1412{"rsnAllocationRead",                "(II[F)V",                               (void*)nAllocationRead_f },
1413{"rsnAllocationGetType",             "(II)I",                                 (void*)nAllocationGetType},
1414{"rsnAllocationResize1D",            "(III)V",                                (void*)nAllocationResize1D },
1415{"rsnAllocationGenerateMipmaps",     "(II)V",                                 (void*)nAllocationGenerateMipmaps },
1416
1417{"rsnScriptBindAllocation",          "(IIII)V",                               (void*)nScriptBindAllocation },
1418{"rsnScriptSetTimeZone",             "(II[B)V",                               (void*)nScriptSetTimeZone },
1419{"rsnScriptInvoke",                  "(III)V",                                (void*)nScriptInvoke },
1420{"rsnScriptInvokeV",                 "(III[B)V",                              (void*)nScriptInvokeV },
1421{"rsnScriptForEach",                 "(IIIII)V",                              (void*)nScriptForEach },
1422{"rsnScriptForEach",                 "(IIIII[B)V",                            (void*)nScriptForEachV },
1423{"rsnScriptForEachClipped",          "(IIIIIIIIIII)V",                        (void*)nScriptForEachClipped },
1424{"rsnScriptForEachClipped",          "(IIIII[BIIIIII)V",                      (void*)nScriptForEachClippedV },
1425{"rsnScriptSetVarI",                 "(IIII)V",                               (void*)nScriptSetVarI },
1426{"rsnScriptSetVarJ",                 "(IIIJ)V",                               (void*)nScriptSetVarJ },
1427{"rsnScriptSetVarF",                 "(IIIF)V",                               (void*)nScriptSetVarF },
1428{"rsnScriptSetVarD",                 "(IIID)V",                               (void*)nScriptSetVarD },
1429{"rsnScriptSetVarV",                 "(III[B)V",                              (void*)nScriptSetVarV },
1430{"rsnScriptSetVarVE",                "(III[BI[I)V",                           (void*)nScriptSetVarVE },
1431{"rsnScriptSetVarObj",               "(IIII)V",                               (void*)nScriptSetVarObj },
1432
1433{"rsnScriptCCreate",                 "(ILjava/lang/String;Ljava/lang/String;[BI)I",  (void*)nScriptCCreate },
1434{"rsnScriptIntrinsicCreate",         "(III)I",                                (void*)nScriptIntrinsicCreate },
1435{"rsnScriptKernelIDCreate",          "(IIII)I",                               (void*)nScriptKernelIDCreate },
1436{"rsnScriptFieldIDCreate",           "(III)I",                                (void*)nScriptFieldIDCreate },
1437{"rsnScriptGroupCreate",             "(I[I[I[I[I[I)I",                        (void*)nScriptGroupCreate },
1438{"rsnScriptGroupSetInput",           "(IIII)V",                               (void*)nScriptGroupSetInput },
1439{"rsnScriptGroupSetOutput",          "(IIII)V",                               (void*)nScriptGroupSetOutput },
1440{"rsnScriptGroupExecute",            "(II)V",                                 (void*)nScriptGroupExecute },
1441
1442{"rsnSamplerCreate",                 "(IIIIIIF)I",                            (void*)nSamplerCreate },
1443
1444};
1445
1446// ---------------------------------------------------------------------------
1447
1448jint JNI_OnLoad(JavaVM* vm, void* reserved)
1449{
1450    JNIEnv* env = NULL;
1451    jclass clazz = NULL;
1452    jint result = -1;
1453
1454    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
1455        //        __android_log_print(ANDROID_LOG_ERROR, LOG_TAG,
1456        //            "ERROR: GetEnv failed\n");
1457        goto bail;
1458    }
1459    if (env == NULL) {
1460        //        __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "ERROR: env == NULL");
1461        goto bail;
1462    }
1463
1464    clazz = env->FindClass(classPathName);
1465    if (clazz == NULL) {
1466        goto bail;
1467    }
1468
1469    if (env->RegisterNatives(clazz, methods, NELEM(methods)) < 0) {
1470        //        __android_log_print(ANDROID_LOG_ERROR, LOG_TAG,
1471        //            "ERROR: MediaPlayer native registration failed\n");
1472        goto bail;
1473    }
1474
1475    /* success -- return valid version number */
1476    result = JNI_VERSION_1_4;
1477
1478bail:
1479    return result;
1480}
1481