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 "RenderScript_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 #include <inttypes.h>
26 
27 #include <android-base/macros.h>
28 #include <androidfw/Asset.h>
29 #include <androidfw/AssetManager2.h>
30 #include <androidfw/ResourceTypes.h>
31 #include <android-base/macros.h>
32 
33 #include "jni.h"
34 #include <nativehelper/JNIHelp.h>
35 #include <android/graphics/bitmap.h>
36 #include "android_runtime/AndroidRuntime.h"
37 #include "android_runtime/android_view_Surface.h"
38 #include "android_runtime/android_util_AssetManager.h"
39 #include "android/native_window.h"
40 #include "android/native_window_jni.h"
41 
42 #include <rsEnv.h>
43 #include <rsApiStubs.h>
44 #include <gui/Surface.h>
45 #include <gui/GLConsumer.h>
46 #include <android_runtime/android_graphics_SurfaceTexture.h>
47 
48 //#define LOG_API ALOGE
49 static constexpr bool kLogApi = false;
50 
51 using namespace android;
52 
53 #define PER_ARRAY_TYPE(flag, fnc, readonly, ...) {                                      \
54     jint len = 0;                                                                       \
55     void *ptr = nullptr;                                                                \
56     void *srcPtr = nullptr;                                                             \
57     size_t typeBytes = 0;                                                               \
58     jint relFlag = 0;                                                                   \
59     if (readonly) {                                                                     \
60         /* The on-release mode should only be JNI_ABORT for read-only accesses. */      \
61         /* readonly = true, also indicates we are copying to the allocation   . */      \
62         relFlag = JNI_ABORT;                                                            \
63     }                                                                                   \
64     switch(dataType) {                                                                  \
65     case RS_TYPE_FLOAT_32:                                                              \
66         len = _env->GetArrayLength((jfloatArray)data);                                  \
67         ptr = _env->GetFloatArrayElements((jfloatArray)data, flag);                     \
68         if (ptr == nullptr) {                                                           \
69             ALOGE("Failed to get Java array elements.");                                \
70             return;                                                                     \
71         }                                                                               \
72         typeBytes = 4;                                                                  \
73         if (usePadding) {                                                               \
74             srcPtr = ptr;                                                               \
75             len = len / 3 * 4;                                                          \
76             if (count == 0) {                                                           \
77                 count = len / 4;                                                        \
78             }                                                                           \
79             ptr = malloc (len * typeBytes);                                             \
80             if (readonly) {                                                             \
81                 copyWithPadding(ptr, srcPtr, mSize, count);                             \
82                 fnc(__VA_ARGS__);                                                       \
83             } else {                                                                    \
84                 fnc(__VA_ARGS__);                                                       \
85                 copyWithUnPadding(srcPtr, ptr, mSize, count);                           \
86             }                                                                           \
87             free(ptr);                                                                  \
88             ptr = srcPtr;                                                               \
89         } else {                                                                        \
90             fnc(__VA_ARGS__);                                                           \
91         }                                                                               \
92         _env->ReleaseFloatArrayElements((jfloatArray)data, (jfloat *)ptr, relFlag);     \
93         return;                                                                         \
94     case RS_TYPE_FLOAT_64:                                                              \
95         len = _env->GetArrayLength((jdoubleArray)data);                                 \
96         ptr = _env->GetDoubleArrayElements((jdoubleArray)data, flag);                   \
97         if (ptr == nullptr) {                                                           \
98             ALOGE("Failed to get Java array elements.");                                \
99             return;                                                                     \
100         }                                                                               \
101         typeBytes = 8;                                                                  \
102         if (usePadding) {                                                               \
103             srcPtr = ptr;                                                               \
104             len = len / 3 * 4;                                                          \
105             if (count == 0) {                                                           \
106                 count = len / 4;                                                        \
107             }                                                                           \
108             ptr = malloc (len * typeBytes);                                             \
109             if (readonly) {                                                             \
110                 copyWithPadding(ptr, srcPtr, mSize, count);                             \
111                 fnc(__VA_ARGS__);                                                       \
112             } else {                                                                    \
113                 fnc(__VA_ARGS__);                                                       \
114                 copyWithUnPadding(srcPtr, ptr, mSize, count);                           \
115             }                                                                           \
116             free(ptr);                                                                  \
117             ptr = srcPtr;                                                               \
118         } else {                                                                        \
119             fnc(__VA_ARGS__);                                                           \
120         }                                                                               \
121         _env->ReleaseDoubleArrayElements((jdoubleArray)data, (jdouble *)ptr, relFlag);  \
122         return;                                                                         \
123     case RS_TYPE_SIGNED_8:                                                              \
124     case RS_TYPE_UNSIGNED_8:                                                            \
125         len = _env->GetArrayLength((jbyteArray)data);                                   \
126         ptr = _env->GetByteArrayElements((jbyteArray)data, flag);                       \
127         if (ptr == nullptr) {                                                           \
128             ALOGE("Failed to get Java array elements.");                                \
129             return;                                                                     \
130         }                                                                               \
131         typeBytes = 1;                                                                  \
132         if (usePadding) {                                                               \
133             srcPtr = ptr;                                                               \
134             len = len / 3 * 4;                                                          \
135             if (count == 0) {                                                           \
136                 count = len / 4;                                                        \
137             }                                                                           \
138             ptr = malloc (len * typeBytes);                                             \
139             if (readonly) {                                                             \
140                 copyWithPadding(ptr, srcPtr, mSize, count);                             \
141                 fnc(__VA_ARGS__);                                                       \
142             } else {                                                                    \
143                 fnc(__VA_ARGS__);                                                       \
144                 copyWithUnPadding(srcPtr, ptr, mSize, count);                           \
145             }                                                                           \
146             free(ptr);                                                                  \
147             ptr = srcPtr;                                                               \
148         } else {                                                                        \
149             fnc(__VA_ARGS__);                                                           \
150         }                                                                               \
151         _env->ReleaseByteArrayElements((jbyteArray)data, (jbyte*)ptr, relFlag);         \
152         return;                                                                         \
153     case RS_TYPE_SIGNED_16:                                                             \
154     case RS_TYPE_UNSIGNED_16:                                                           \
155     case RS_TYPE_FLOAT_16:                                                              \
156         len = _env->GetArrayLength((jshortArray)data);                                  \
157         ptr = _env->GetShortArrayElements((jshortArray)data, flag);                     \
158         if (ptr == nullptr) {                                                           \
159             ALOGE("Failed to get Java array elements.");                                \
160             return;                                                                     \
161         }                                                                               \
162         typeBytes = 2;                                                                  \
163         if (usePadding) {                                                               \
164             srcPtr = ptr;                                                               \
165             len = len / 3 * 4;                                                          \
166             if (count == 0) {                                                           \
167                 count = len / 4;                                                        \
168             }                                                                           \
169             ptr = malloc (len * typeBytes);                                             \
170             if (readonly) {                                                             \
171                 copyWithPadding(ptr, srcPtr, mSize, count);                             \
172                 fnc(__VA_ARGS__);                                                       \
173             } else {                                                                    \
174                 fnc(__VA_ARGS__);                                                       \
175                 copyWithUnPadding(srcPtr, ptr, mSize, count);                           \
176             }                                                                           \
177             free(ptr);                                                                  \
178             ptr = srcPtr;                                                               \
179         } else {                                                                        \
180             fnc(__VA_ARGS__);                                                           \
181         }                                                                               \
182         _env->ReleaseShortArrayElements((jshortArray)data, (jshort *)ptr, relFlag);     \
183         return;                                                                         \
184     case RS_TYPE_SIGNED_32:                                                             \
185     case RS_TYPE_UNSIGNED_32:                                                           \
186         len = _env->GetArrayLength((jintArray)data);                                    \
187         ptr = _env->GetIntArrayElements((jintArray)data, flag);                         \
188         if (ptr == nullptr) {                                                           \
189             ALOGE("Failed to get Java array elements.");                                \
190             return;                                                                     \
191         }                                                                               \
192         typeBytes = 4;                                                                  \
193         if (usePadding) {                                                               \
194             srcPtr = ptr;                                                               \
195             len = len / 3 * 4;                                                          \
196             if (count == 0) {                                                           \
197                 count = len / 4;                                                        \
198             }                                                                           \
199             ptr = malloc (len * typeBytes);                                             \
200             if (readonly) {                                                             \
201                 copyWithPadding(ptr, srcPtr, mSize, count);                             \
202                 fnc(__VA_ARGS__);                                                       \
203             } else {                                                                    \
204                 fnc(__VA_ARGS__);                                                       \
205                 copyWithUnPadding(srcPtr, ptr, mSize, count);                           \
206             }                                                                           \
207             free(ptr);                                                                  \
208             ptr = srcPtr;                                                               \
209         } else {                                                                        \
210             fnc(__VA_ARGS__);                                                           \
211         }                                                                               \
212         _env->ReleaseIntArrayElements((jintArray)data, (jint *)ptr, relFlag);           \
213         return;                                                                         \
214     case RS_TYPE_SIGNED_64:                                                             \
215     case RS_TYPE_UNSIGNED_64:                                                           \
216         len = _env->GetArrayLength((jlongArray)data);                                   \
217         ptr = _env->GetLongArrayElements((jlongArray)data, flag);                       \
218         if (ptr == nullptr) {                                                           \
219             ALOGE("Failed to get Java array elements.");                                \
220             return;                                                                     \
221         }                                                                               \
222         typeBytes = 8;                                                                  \
223         if (usePadding) {                                                               \
224             srcPtr = ptr;                                                               \
225             len = len / 3 * 4;                                                          \
226             if (count == 0) {                                                           \
227                 count = len / 4;                                                        \
228             }                                                                           \
229             ptr = malloc (len * typeBytes);                                             \
230             if (readonly) {                                                             \
231                 copyWithPadding(ptr, srcPtr, mSize, count);                             \
232                 fnc(__VA_ARGS__);                                                       \
233             } else {                                                                    \
234                 fnc(__VA_ARGS__);                                                       \
235                 copyWithUnPadding(srcPtr, ptr, mSize, count);                           \
236             }                                                                           \
237             free(ptr);                                                                  \
238             ptr = srcPtr;                                                               \
239         } else {                                                                        \
240             fnc(__VA_ARGS__);                                                           \
241         }                                                                               \
242         _env->ReleaseLongArrayElements((jlongArray)data, (jlong *)ptr, relFlag);        \
243         return;                                                                         \
244     default:                                                                            \
245         break;                                                                          \
246     }                                                                                   \
247     UNUSED(len, ptr, srcPtr, typeBytes, relFlag);                                       \
248 }
249 
250 
251 class AutoJavaStringToUTF8 {
252 public:
AutoJavaStringToUTF8(JNIEnv * env,jstring str)253     AutoJavaStringToUTF8(JNIEnv* env, jstring str) : fEnv(env), fJStr(str) {
254         fCStr = env->GetStringUTFChars(str, nullptr);
255         fLength = env->GetStringUTFLength(str);
256     }
~AutoJavaStringToUTF8()257     ~AutoJavaStringToUTF8() {
258         fEnv->ReleaseStringUTFChars(fJStr, fCStr);
259     }
c_str() const260     const char* c_str() const { return fCStr; }
length() const261     jsize length() const { return fLength; }
262 
263 private:
264     JNIEnv*     fEnv;
265     jstring     fJStr;
266     const char* fCStr;
267     jsize       fLength;
268 };
269 
270 class AutoJavaStringArrayToUTF8 {
271 public:
AutoJavaStringArrayToUTF8(JNIEnv * env,jobjectArray strings,jsize stringsLength)272     AutoJavaStringArrayToUTF8(JNIEnv* env, jobjectArray strings, jsize stringsLength)
273     : mEnv(env), mStrings(strings), mStringsLength(stringsLength) {
274         mCStrings = nullptr;
275         mSizeArray = nullptr;
276         if (stringsLength > 0) {
277             mCStrings = (const char **)calloc(stringsLength, sizeof(char *));
278             mSizeArray = (size_t*)calloc(stringsLength, sizeof(size_t));
279             for (jsize ct = 0; ct < stringsLength; ct ++) {
280                 jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
281                 mCStrings[ct] = mEnv->GetStringUTFChars(s, nullptr);
282                 mSizeArray[ct] = mEnv->GetStringUTFLength(s);
283             }
284         }
285     }
~AutoJavaStringArrayToUTF8()286     ~AutoJavaStringArrayToUTF8() {
287         for (jsize ct=0; ct < mStringsLength; ct++) {
288             jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
289             mEnv->ReleaseStringUTFChars(s, mCStrings[ct]);
290         }
291         free(mCStrings);
292         free(mSizeArray);
293     }
c_str() const294     const char **c_str() const { return mCStrings; }
c_str_len() const295     size_t *c_str_len() const { return mSizeArray; }
length() const296     jsize length() const { return mStringsLength; }
297 
298 private:
299     JNIEnv      *mEnv;
300     jobjectArray mStrings;
301     const char **mCStrings;
302     size_t      *mSizeArray;
303     jsize        mStringsLength;
304 };
305 
306 // ---------------------------------------------------------------------------
307 
308 static jfieldID gContextId = 0;
309 
_nInit(JNIEnv * _env,jclass _this)310 static void _nInit(JNIEnv *_env, jclass _this)
311 {
312     gContextId             = _env->GetFieldID(_this, "mContext", "J");
313 }
314 
315 // ---------------------------------------------------------------------------
316 
copyWithPadding(void * ptr,void * srcPtr,int mSize,int count)317 static void copyWithPadding(void* ptr, void* srcPtr, int mSize, int count) {
318     int sizeBytesPad = mSize * 4;
319     int sizeBytes = mSize * 3;
320     uint8_t *dst = static_cast<uint8_t *>(ptr);
321     uint8_t *src = static_cast<uint8_t *>(srcPtr);
322     for (int i = 0; i < count; i++) {
323         memcpy(dst, src, sizeBytes);
324         dst += sizeBytesPad;
325         src += sizeBytes;
326     }
327 }
328 
copyWithUnPadding(void * ptr,void * srcPtr,int mSize,int count)329 static void copyWithUnPadding(void* ptr, void* srcPtr, int mSize, int count) {
330     int sizeBytesPad = mSize * 4;
331     int sizeBytes = mSize * 3;
332     uint8_t *dst = static_cast<uint8_t *>(ptr);
333     uint8_t *src = static_cast<uint8_t *>(srcPtr);
334     for (int i = 0; i < count; i++) {
335         memcpy(dst, src, sizeBytes);
336         dst += sizeBytes;
337         src += sizeBytesPad;
338     }
339 }
340 
341 
342 // ---------------------------------------------------------------------------
343 static void
nContextFinish(JNIEnv * _env,jobject _this,jlong con)344 nContextFinish(JNIEnv *_env, jobject _this, jlong con)
345 {
346     if (kLogApi) {
347         ALOGD("nContextFinish, con(%p)", (RsContext)con);
348     }
349     rsContextFinish((RsContext)con);
350 }
351 
352 static jlong
nClosureCreate(JNIEnv * _env,jobject _this,jlong con,jlong kernelID,jlong returnValue,jlongArray fieldIDArray,jlongArray valueArray,jintArray sizeArray,jlongArray depClosureArray,jlongArray depFieldIDArray)353 nClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong kernelID,
354                jlong returnValue, jlongArray fieldIDArray,
355                jlongArray valueArray, jintArray sizeArray,
356                jlongArray depClosureArray, jlongArray depFieldIDArray) {
357   jlong ret = 0;
358 
359   jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
360   jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
361   if (jFieldIDs == nullptr) {
362       ALOGE("Failed to get Java array elements: fieldIDs.");
363       return ret;
364   }
365 
366   jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
367   jsize values_length = _env->GetArrayLength(valueArray);
368   if (jValues == nullptr) {
369       ALOGE("Failed to get Java array elements: values.");
370       return ret;
371   }
372 
373   jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
374   jsize sizes_length = _env->GetArrayLength(sizeArray);
375   if (jSizes == nullptr) {
376       ALOGE("Failed to get Java array elements: sizes.");
377       return ret;
378   }
379 
380   jlong* jDepClosures =
381       _env->GetLongArrayElements(depClosureArray, nullptr);
382   jsize depClosures_length = _env->GetArrayLength(depClosureArray);
383   if (jDepClosures == nullptr) {
384       ALOGE("Failed to get Java array elements: depClosures.");
385       return ret;
386   }
387 
388   jlong* jDepFieldIDs =
389       _env->GetLongArrayElements(depFieldIDArray, nullptr);
390   jsize depFieldIDs_length = _env->GetArrayLength(depFieldIDArray);
391   if (jDepFieldIDs == nullptr) {
392       ALOGE("Failed to get Java array elements: depFieldIDs.");
393       return ret;
394   }
395 
396   size_t numValues, numDependencies;
397   RsScriptFieldID* fieldIDs;
398   RsClosure* depClosures;
399   RsScriptFieldID* depFieldIDs;
400 
401   if (fieldIDs_length != values_length || values_length != sizes_length) {
402       ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
403       goto exit;
404   }
405 
406   numValues = (size_t)fieldIDs_length;
407 
408   if (depClosures_length != depFieldIDs_length) {
409       ALOGE("Unmatched closures and field IDs for dependencies in closure creation.");
410       goto exit;
411   }
412 
413   numDependencies = (size_t)depClosures_length;
414 
415   if (numDependencies > numValues) {
416       ALOGE("Unexpected number of dependencies in closure creation");
417       goto exit;
418   }
419 
420   if (numValues > RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS) {
421       ALOGE("Too many arguments or globals in closure creation");
422       goto exit;
423   }
424 
425   if (numValues > 0) {
426       fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
427       if (fieldIDs == nullptr) {
428           goto exit;
429       }
430   } else {
431       // numValues == 0
432       // alloca(0) implementation is platform-dependent.
433       fieldIDs = nullptr;
434   }
435 
436   for (size_t i = 0; i < numValues; i++) {
437     fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
438   }
439 
440   if (numDependencies > 0) {
441       depClosures = (RsClosure*)alloca(sizeof(RsClosure) * numDependencies);
442       if (depClosures == nullptr) {
443           goto exit;
444       }
445 
446       for (size_t i = 0; i < numDependencies; i++) {
447           depClosures[i] = (RsClosure)jDepClosures[i];
448       }
449 
450       depFieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numDependencies);
451       if (depFieldIDs == nullptr) {
452           goto exit;
453       }
454 
455       for (size_t i = 0; i < numDependencies; i++) {
456           depFieldIDs[i] = (RsClosure)jDepFieldIDs[i];
457       }
458   } else {
459       // alloca(0) implementation is platform-dependent.
460       depClosures = nullptr;
461       depFieldIDs = nullptr;
462   }
463 
464   ret = (jlong)(uintptr_t)rsClosureCreate(
465       (RsContext)con, (RsScriptKernelID)kernelID, (RsAllocation)returnValue,
466       fieldIDs, numValues, jValues, numValues,
467       (int*)jSizes, numValues,
468       depClosures, numDependencies,
469       depFieldIDs, numDependencies);
470 
471 exit:
472 
473   _env->ReleaseLongArrayElements(depFieldIDArray, jDepFieldIDs, JNI_ABORT);
474   _env->ReleaseLongArrayElements(depClosureArray, jDepClosures, JNI_ABORT);
475   _env->ReleaseIntArrayElements (sizeArray,       jSizes,       JNI_ABORT);
476   _env->ReleaseLongArrayElements(valueArray,      jValues,      JNI_ABORT);
477   _env->ReleaseLongArrayElements(fieldIDArray,    jFieldIDs,    JNI_ABORT);
478 
479   return ret;
480 }
481 
482 static jlong
nInvokeClosureCreate(JNIEnv * _env,jobject _this,jlong con,jlong invokeID,jbyteArray paramArray,jlongArray fieldIDArray,jlongArray valueArray,jintArray sizeArray)483 nInvokeClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong invokeID,
484                      jbyteArray paramArray, jlongArray fieldIDArray, jlongArray valueArray,
485                      jintArray sizeArray) {
486   jlong ret = 0;
487 
488   jbyte* jParams = _env->GetByteArrayElements(paramArray, nullptr);
489   jsize jParamLength = _env->GetArrayLength(paramArray);
490   if (jParams == nullptr) {
491       ALOGE("Failed to get Java array elements: params.");
492       return ret;
493   }
494 
495   jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
496   jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
497   if (jFieldIDs == nullptr) {
498       ALOGE("Failed to get Java array elements: fieldIDs.");
499       return ret;
500   }
501 
502   jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
503   jsize values_length = _env->GetArrayLength(valueArray);
504   if (jValues == nullptr) {
505       ALOGE("Failed to get Java array elements: values.");
506       return ret;
507   }
508 
509   jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
510   jsize sizes_length = _env->GetArrayLength(sizeArray);
511   if (jSizes == nullptr) {
512       ALOGE("Failed to get Java array elements: sizes.");
513       return ret;
514   }
515 
516   size_t numValues;
517   RsScriptFieldID* fieldIDs;
518 
519   if (fieldIDs_length != values_length || values_length != sizes_length) {
520       ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
521       goto exit;
522   }
523 
524   numValues = (size_t) fieldIDs_length;
525 
526   if (numValues > RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS) {
527       ALOGE("Too many arguments or globals in closure creation");
528       goto exit;
529   }
530 
531   fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
532   if (fieldIDs == nullptr) {
533       goto exit;
534   }
535 
536   for (size_t i = 0; i< numValues; i++) {
537     fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
538   }
539 
540   ret = (jlong)(uintptr_t)rsInvokeClosureCreate(
541       (RsContext)con, (RsScriptInvokeID)invokeID, jParams, jParamLength,
542       fieldIDs, numValues, jValues, numValues,
543       (int*)jSizes, numValues);
544 
545 exit:
546 
547   _env->ReleaseIntArrayElements (sizeArray,       jSizes,       JNI_ABORT);
548   _env->ReleaseLongArrayElements(valueArray,      jValues,      JNI_ABORT);
549   _env->ReleaseLongArrayElements(fieldIDArray,    jFieldIDs,    JNI_ABORT);
550   _env->ReleaseByteArrayElements(paramArray,      jParams,      JNI_ABORT);
551 
552   return ret;
553 }
554 
555 static void
nClosureSetArg(JNIEnv * _env,jobject _this,jlong con,jlong closureID,jint index,jlong value,jint size)556 nClosureSetArg(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
557                jint index, jlong value, jint size) {
558   // Size is signed with -1 indicating the value is an Allocation
559   rsClosureSetArg((RsContext)con, (RsClosure)closureID, (uint32_t)index,
560                   (uintptr_t)value, size);
561 }
562 
563 static void
nClosureSetGlobal(JNIEnv * _env,jobject _this,jlong con,jlong closureID,jlong fieldID,jlong value,jint size)564 nClosureSetGlobal(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
565                   jlong fieldID, jlong value, jint size) {
566   // Size is signed with -1 indicating the value is an Allocation
567   rsClosureSetGlobal((RsContext)con, (RsClosure)closureID,
568                      (RsScriptFieldID)fieldID, (int64_t)value, size);
569 }
570 
571 static long
nScriptGroup2Create(JNIEnv * _env,jobject _this,jlong con,jstring name,jstring cacheDir,jlongArray closureArray)572 nScriptGroup2Create(JNIEnv *_env, jobject _this, jlong con, jstring name,
573                     jstring cacheDir, jlongArray closureArray) {
574   jlong ret = 0;
575 
576   AutoJavaStringToUTF8 nameUTF(_env, name);
577   AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
578 
579   jlong* jClosures = _env->GetLongArrayElements(closureArray, nullptr);
580   jsize numClosures = _env->GetArrayLength(closureArray);
581   if (jClosures == nullptr) {
582       ALOGE("Failed to get Java array elements: closures.");
583       return ret;
584   }
585 
586   RsClosure* closures;
587 
588   if (numClosures > (jsize) RS_SCRIPT_GROUP_MAX_NUMBER_CLOSURES) {
589     ALOGE("Too many closures in script group");
590     goto exit;
591   }
592 
593   closures = (RsClosure*)alloca(sizeof(RsClosure) * numClosures);
594   if (closures == nullptr) {
595       goto exit;
596   }
597 
598   for (int i = 0; i < numClosures; i++) {
599     closures[i] = (RsClosure)jClosures[i];
600   }
601 
602   ret = (jlong)(uintptr_t)rsScriptGroup2Create(
603       (RsContext)con, nameUTF.c_str(), nameUTF.length(),
604       cacheDirUTF.c_str(), cacheDirUTF.length(),
605       closures, numClosures);
606 
607 exit:
608 
609   _env->ReleaseLongArrayElements(closureArray, jClosures, JNI_ABORT);
610 
611   return ret;
612 }
613 
614 static void
nScriptGroup2Execute(JNIEnv * _env,jobject _this,jlong con,jlong groupID)615 nScriptGroup2Execute(JNIEnv *_env, jobject _this, jlong con, jlong groupID) {
616   rsScriptGroupExecute((RsContext)con, (RsScriptGroup2)groupID);
617 }
618 
619 static void
nScriptIntrinsicBLAS_Single(JNIEnv * _env,jobject _this,jlong con,jlong id,jint func,jint TransA,jint TransB,jint Side,jint Uplo,jint Diag,jint M,jint N,jint K,jfloat alpha,jlong A,jlong B,jfloat beta,jlong C,jint incX,jint incY,jint KL,jint KU)620 nScriptIntrinsicBLAS_Single(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
621                             jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
622                             jfloat alpha, jlong A, jlong B, jfloat beta, jlong C, jint incX, jint incY,
623                             jint KL, jint KU) {
624     RsBlasCall call;
625     memset(&call, 0, sizeof(call));
626     call.func = (RsBlasFunction)func;
627     call.transA = (RsBlasTranspose)TransA;
628     call.transB = (RsBlasTranspose)TransB;
629     call.side = (RsBlasSide)Side;
630     call.uplo = (RsBlasUplo)Uplo;
631     call.diag = (RsBlasDiag)Diag;
632     call.M = M;
633     call.N = N;
634     call.K = K;
635     call.alpha.f = alpha;
636     call.beta.f = beta;
637     call.incX = incX;
638     call.incY = incY;
639     call.KL = KL;
640     call.KU = KU;
641 
642     RsAllocation in_allocs[3];
643     in_allocs[0] = (RsAllocation)A;
644     in_allocs[1] = (RsAllocation)B;
645     in_allocs[2] = (RsAllocation)C;
646 
647     rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
648                          in_allocs, NELEM(in_allocs), nullptr,
649                          &call, sizeof(call), nullptr, 0);
650 }
651 
652 static void
nScriptIntrinsicBLAS_Double(JNIEnv * _env,jobject _this,jlong con,jlong id,jint func,jint TransA,jint TransB,jint Side,jint Uplo,jint Diag,jint M,jint N,jint K,jdouble alpha,jlong A,jlong B,jdouble beta,jlong C,jint incX,jint incY,jint KL,jint KU)653 nScriptIntrinsicBLAS_Double(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
654                             jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
655                             jdouble alpha, jlong A, jlong B, jdouble beta, jlong C, jint incX, jint incY,
656                             jint KL, jint KU) {
657     RsBlasCall call;
658     memset(&call, 0, sizeof(call));
659     call.func = (RsBlasFunction)func;
660     call.transA = (RsBlasTranspose)TransA;
661     call.transB = (RsBlasTranspose)TransB;
662     call.side = (RsBlasSide)Side;
663     call.uplo = (RsBlasUplo)Uplo;
664     call.diag = (RsBlasDiag)Diag;
665     call.M = M;
666     call.N = N;
667     call.K = K;
668     call.alpha.d = alpha;
669     call.beta.d = beta;
670     call.incX = incX;
671     call.incY = incY;
672     call.KL = KL;
673     call.KU = KU;
674 
675     RsAllocation in_allocs[3];
676     in_allocs[0] = (RsAllocation)A;
677     in_allocs[1] = (RsAllocation)B;
678     in_allocs[2] = (RsAllocation)C;
679 
680     rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
681                          in_allocs, NELEM(in_allocs), nullptr,
682                          &call, sizeof(call), nullptr, 0);
683 }
684 
685 static void
nScriptIntrinsicBLAS_Complex(JNIEnv * _env,jobject _this,jlong con,jlong id,jint func,jint TransA,jint TransB,jint Side,jint Uplo,jint Diag,jint M,jint N,jint K,jfloat alphaX,jfloat alphaY,jlong A,jlong B,jfloat betaX,jfloat betaY,jlong C,jint incX,jint incY,jint KL,jint KU)686 nScriptIntrinsicBLAS_Complex(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
687                              jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
688                              jfloat alphaX, jfloat alphaY, jlong A, jlong B, jfloat betaX,
689                              jfloat betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
690     RsBlasCall call;
691     memset(&call, 0, sizeof(call));
692     call.func = (RsBlasFunction)func;
693     call.transA = (RsBlasTranspose)TransA;
694     call.transB = (RsBlasTranspose)TransB;
695     call.side = (RsBlasSide)Side;
696     call.uplo = (RsBlasUplo)Uplo;
697     call.diag = (RsBlasDiag)Diag;
698     call.M = M;
699     call.N = N;
700     call.K = K;
701     call.alpha.c.r = alphaX;
702     call.alpha.c.i = alphaY;
703     call.beta.c.r = betaX;
704     call.beta.c.i = betaY;
705     call.incX = incX;
706     call.incY = incY;
707     call.KL = KL;
708     call.KU = KU;
709 
710     RsAllocation in_allocs[3];
711     in_allocs[0] = (RsAllocation)A;
712     in_allocs[1] = (RsAllocation)B;
713     in_allocs[2] = (RsAllocation)C;
714 
715     rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
716                          in_allocs, NELEM(in_allocs), nullptr,
717                          &call, sizeof(call), nullptr, 0);
718 }
719 
720 static void
nScriptIntrinsicBLAS_Z(JNIEnv * _env,jobject _this,jlong con,jlong id,jint func,jint TransA,jint TransB,jint Side,jint Uplo,jint Diag,jint M,jint N,jint K,jdouble alphaX,jdouble alphaY,jlong A,jlong B,jdouble betaX,jdouble betaY,jlong C,jint incX,jint incY,jint KL,jint KU)721 nScriptIntrinsicBLAS_Z(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
722                        jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
723                        jdouble alphaX, jdouble alphaY, jlong A, jlong B, jdouble betaX,
724                        jdouble betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
725     RsBlasCall call;
726     memset(&call, 0, sizeof(call));
727     call.func = (RsBlasFunction)func;
728     call.transA = (RsBlasTranspose)TransA;
729     call.transB = (RsBlasTranspose)TransB;
730     call.side = (RsBlasSide)Side;
731     call.uplo = (RsBlasUplo)Uplo;
732     call.diag = (RsBlasDiag)Diag;
733     call.M = M;
734     call.N = N;
735     call.K = K;
736     call.alpha.z.r = alphaX;
737     call.alpha.z.i = alphaY;
738     call.beta.z.r = betaX;
739     call.beta.z.i = betaY;
740     call.incX = incX;
741     call.incY = incY;
742     call.KL = KL;
743     call.KU = KU;
744 
745     RsAllocation in_allocs[3];
746     in_allocs[0] = (RsAllocation)A;
747     in_allocs[1] = (RsAllocation)B;
748     in_allocs[2] = (RsAllocation)C;
749 
750     rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
751                          in_allocs, NELEM(in_allocs), nullptr,
752                          &call, sizeof(call), nullptr, 0);
753 }
754 
755 
756 static void
nScriptIntrinsicBLAS_BNNM(JNIEnv * _env,jobject _this,jlong con,jlong id,jint M,jint N,jint K,jlong A,jint a_offset,jlong B,jint b_offset,jlong C,jint c_offset,jint c_mult_int)757 nScriptIntrinsicBLAS_BNNM(JNIEnv *_env, jobject _this, jlong con, jlong id, jint M, jint N, jint K,
758                                              jlong A, jint a_offset, jlong B, jint b_offset, jlong C, jint c_offset,
759                                              jint c_mult_int) {
760     RsBlasCall call;
761     memset(&call, 0, sizeof(call));
762     call.func = RsBlas_bnnm;
763     call.M = M;
764     call.N = N;
765     call.K = K;
766     call.a_offset = a_offset & 0xFF;
767     call.b_offset = b_offset & 0xFF;
768     call.c_offset = c_offset;
769     call.c_mult_int = c_mult_int;
770 
771     RsAllocation in_allocs[3];
772     in_allocs[0] = (RsAllocation)A;
773     in_allocs[1] = (RsAllocation)B;
774     in_allocs[2] = (RsAllocation)C;
775 
776     rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
777                          in_allocs, NELEM(in_allocs), nullptr,
778                          &call, sizeof(call), nullptr, 0);
779 }
780 
781 
782 static void
nAssignName(JNIEnv * _env,jobject _this,jlong con,jlong obj,jbyteArray str)783 nAssignName(JNIEnv *_env, jobject _this, jlong con, jlong obj, jbyteArray str)
784 {
785     if (kLogApi) {
786         ALOGD("nAssignName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
787     }
788     jint len = _env->GetArrayLength(str);
789     jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
790     if (cptr == nullptr) {
791         ALOGE("Failed to get Java array elements");
792         return;
793     }
794 
795     rsAssignName((RsContext)con, (void *)obj, (const char *)cptr, len);
796     _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
797 }
798 
799 static jstring
nGetName(JNIEnv * _env,jobject _this,jlong con,jlong obj)800 nGetName(JNIEnv *_env, jobject _this, jlong con, jlong obj)
801 {
802     if (kLogApi) {
803         ALOGD("nGetName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
804     }
805     const char *name = nullptr;
806     rsaGetName((RsContext)con, (void *)obj, &name);
807     if(name == nullptr || strlen(name) == 0) {
808         return nullptr;
809     }
810     return _env->NewStringUTF(name);
811 }
812 
813 static void
nObjDestroy(JNIEnv * _env,jobject _this,jlong con,jlong obj)814 nObjDestroy(JNIEnv *_env, jobject _this, jlong con, jlong obj)
815 {
816     if (kLogApi) {
817         ALOGD("nObjDestroy, con(%p) obj(%p)", (RsContext)con, (void *)obj);
818     }
819     rsObjDestroy((RsContext)con, (void *)obj);
820 }
821 
822 // ---------------------------------------------------------------------------
823 
824 static jlong
nDeviceCreate(JNIEnv * _env,jobject _this)825 nDeviceCreate(JNIEnv *_env, jobject _this)
826 {
827     if (kLogApi) {
828         ALOGD("nDeviceCreate");
829     }
830     return (jlong)(uintptr_t)rsDeviceCreate();
831 }
832 
833 static void
nDeviceDestroy(JNIEnv * _env,jobject _this,jlong dev)834 nDeviceDestroy(JNIEnv *_env, jobject _this, jlong dev)
835 {
836     if (kLogApi) {
837         ALOGD("nDeviceDestroy");
838     }
839     return rsDeviceDestroy((RsDevice)dev);
840 }
841 
842 static void
nDeviceSetConfig(JNIEnv * _env,jobject _this,jlong dev,jint p,jint value)843 nDeviceSetConfig(JNIEnv *_env, jobject _this, jlong dev, jint p, jint value)
844 {
845     if (kLogApi) {
846         ALOGD("nDeviceSetConfig  dev(%p), param(%i), value(%i)", (void *)dev, p, value);
847     }
848     return rsDeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value);
849 }
850 
851 static jlong
nContextCreate(JNIEnv * _env,jobject _this,jlong dev,jint flags,jint sdkVer,jint contextType)852 nContextCreate(JNIEnv *_env, jobject _this, jlong dev, jint flags, jint sdkVer, jint contextType)
853 {
854     if (kLogApi) {
855         ALOGD("nContextCreate");
856     }
857     return (jlong)(uintptr_t)rsContextCreate((RsDevice)dev, 0, sdkVer, (RsContextType)contextType, flags);
858 }
859 
860 static jlong
nContextCreateGL(JNIEnv * _env,jobject _this,jlong dev,jint ver,jint sdkVer,jint colorMin,jint colorPref,jint alphaMin,jint alphaPref,jint depthMin,jint depthPref,jint stencilMin,jint stencilPref,jint samplesMin,jint samplesPref,jfloat samplesQ,jint dpi)861 nContextCreateGL(JNIEnv *_env, jobject _this, jlong dev, jint ver, jint sdkVer,
862                  jint colorMin, jint colorPref,
863                  jint alphaMin, jint alphaPref,
864                  jint depthMin, jint depthPref,
865                  jint stencilMin, jint stencilPref,
866                  jint samplesMin, jint samplesPref, jfloat samplesQ,
867                  jint dpi)
868 {
869     RsSurfaceConfig sc = {};
870     sc.alphaMin = alphaMin;
871     sc.alphaPref = alphaPref;
872     sc.colorMin = colorMin;
873     sc.colorPref = colorPref;
874     sc.depthMin = depthMin;
875     sc.depthPref = depthPref;
876     sc.samplesMin = samplesMin;
877     sc.samplesPref = samplesPref;
878     sc.samplesQ = samplesQ;
879 
880     if (kLogApi) {
881         ALOGD("nContextCreateGL");
882     }
883     return (jlong)(uintptr_t)rsContextCreateGL((RsDevice)dev, ver, sdkVer, sc, dpi);
884 }
885 
886 static void
nContextSetPriority(JNIEnv * _env,jobject _this,jlong con,jint p)887 nContextSetPriority(JNIEnv *_env, jobject _this, jlong con, jint p)
888 {
889     if (kLogApi) {
890         ALOGD("ContextSetPriority, con(%p), priority(%i)", (RsContext)con, p);
891     }
892     rsContextSetPriority((RsContext)con, p);
893 }
894 
895 static void
nContextSetCacheDir(JNIEnv * _env,jobject _this,jlong con,jstring cacheDir)896 nContextSetCacheDir(JNIEnv *_env, jobject _this, jlong con, jstring cacheDir)
897 {
898     AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
899 
900     if (kLogApi) {
901         ALOGD("ContextSetCacheDir, con(%p), cacheDir(%s)", (RsContext)con, cacheDirUTF.c_str());
902     }
903     rsContextSetCacheDir((RsContext)con, cacheDirUTF.c_str(), cacheDirUTF.length());
904 }
905 
906 
907 
908 static void
nContextSetSurface(JNIEnv * _env,jobject _this,jlong con,jint width,jint height,jobject wnd)909 nContextSetSurface(JNIEnv *_env, jobject _this, jlong con, jint width, jint height, jobject wnd)
910 {
911     if (kLogApi) {
912         ALOGD("nContextSetSurface, con(%p), width(%i), height(%i), surface(%p)", (RsContext)con,
913               width, height, (Surface *)wnd);
914     }
915 
916     ANativeWindow * window = nullptr;
917     if (wnd == nullptr) {
918 
919     } else {
920         window = android_view_Surface_getNativeWindow(_env, wnd).get();
921     }
922 
923     rsContextSetSurface((RsContext)con, width, height, window);
924 }
925 
926 static void
nContextDestroy(JNIEnv * _env,jobject _this,jlong con)927 nContextDestroy(JNIEnv *_env, jobject _this, jlong con)
928 {
929     if (kLogApi) {
930         ALOGD("nContextDestroy, con(%p)", (RsContext)con);
931     }
932     rsContextDestroy((RsContext)con);
933 }
934 
935 static void
nContextDump(JNIEnv * _env,jobject _this,jlong con,jint bits)936 nContextDump(JNIEnv *_env, jobject _this, jlong con, jint bits)
937 {
938     if (kLogApi) {
939         ALOGD("nContextDump, con(%p)  bits(%i)", (RsContext)con, bits);
940     }
941     rsContextDump((RsContext)con, bits);
942 }
943 
944 static void
nContextPause(JNIEnv * _env,jobject _this,jlong con)945 nContextPause(JNIEnv *_env, jobject _this, jlong con)
946 {
947     if (kLogApi) {
948         ALOGD("nContextPause, con(%p)", (RsContext)con);
949     }
950     rsContextPause((RsContext)con);
951 }
952 
953 static void
nContextResume(JNIEnv * _env,jobject _this,jlong con)954 nContextResume(JNIEnv *_env, jobject _this, jlong con)
955 {
956     if (kLogApi) {
957         ALOGD("nContextResume, con(%p)", (RsContext)con);
958     }
959     rsContextResume((RsContext)con);
960 }
961 
962 
963 static jstring
nContextGetErrorMessage(JNIEnv * _env,jobject _this,jlong con)964 nContextGetErrorMessage(JNIEnv *_env, jobject _this, jlong con)
965 {
966     if (kLogApi) {
967         ALOGD("nContextGetErrorMessage, con(%p)", (RsContext)con);
968     }
969     char buf[1024];
970 
971     size_t receiveLen;
972     uint32_t subID;
973     int id = rsContextGetMessage((RsContext)con,
974                                  buf, sizeof(buf),
975                                  &receiveLen, sizeof(receiveLen),
976                                  &subID, sizeof(subID));
977     if (!id && receiveLen) {
978         ALOGV("message receive buffer too small.  %zu", receiveLen);
979     }
980     return _env->NewStringUTF(buf);
981 }
982 
983 static jint
nContextGetUserMessage(JNIEnv * _env,jobject _this,jlong con,jintArray data)984 nContextGetUserMessage(JNIEnv *_env, jobject _this, jlong con, jintArray data)
985 {
986     jint len = _env->GetArrayLength(data);
987     if (kLogApi) {
988         ALOGD("nContextGetMessage, con(%p), len(%i)", (RsContext)con, len);
989     }
990     jint *ptr = _env->GetIntArrayElements(data, nullptr);
991     if (ptr == nullptr) {
992         ALOGE("Failed to get Java array elements");
993         return 0;
994     }
995     size_t receiveLen;
996     uint32_t subID;
997     int id = rsContextGetMessage((RsContext)con,
998                                  ptr, len * 4,
999                                  &receiveLen, sizeof(receiveLen),
1000                                  &subID, sizeof(subID));
1001     if (!id && receiveLen) {
1002         ALOGV("message receive buffer too small.  %zu", receiveLen);
1003     }
1004     _env->ReleaseIntArrayElements(data, ptr, 0);
1005     return (jint)id;
1006 }
1007 
1008 static jint
nContextPeekMessage(JNIEnv * _env,jobject _this,jlong con,jintArray auxData)1009 nContextPeekMessage(JNIEnv *_env, jobject _this, jlong con, jintArray auxData)
1010 {
1011     if (kLogApi) {
1012         ALOGD("nContextPeekMessage, con(%p)", (RsContext)con);
1013     }
1014     jint *auxDataPtr = _env->GetIntArrayElements(auxData, nullptr);
1015     if (auxDataPtr == nullptr) {
1016         ALOGE("Failed to get Java array elements");
1017         return 0;
1018     }
1019     size_t receiveLen;
1020     uint32_t subID;
1021     int id = rsContextPeekMessage((RsContext)con, &receiveLen, sizeof(receiveLen),
1022                                   &subID, sizeof(subID));
1023     auxDataPtr[0] = (jint)subID;
1024     auxDataPtr[1] = (jint)receiveLen;
1025     _env->ReleaseIntArrayElements(auxData, auxDataPtr, 0);
1026     return (jint)id;
1027 }
1028 
nContextInitToClient(JNIEnv * _env,jobject _this,jlong con)1029 static void nContextInitToClient(JNIEnv *_env, jobject _this, jlong con)
1030 {
1031     if (kLogApi) {
1032         ALOGD("nContextInitToClient, con(%p)", (RsContext)con);
1033     }
1034     rsContextInitToClient((RsContext)con);
1035 }
1036 
nContextDeinitToClient(JNIEnv * _env,jobject _this,jlong con)1037 static void nContextDeinitToClient(JNIEnv *_env, jobject _this, jlong con)
1038 {
1039     if (kLogApi) {
1040         ALOGD("nContextDeinitToClient, con(%p)", (RsContext)con);
1041     }
1042     rsContextDeinitToClient((RsContext)con);
1043 }
1044 
1045 static void
nContextSendMessage(JNIEnv * _env,jobject _this,jlong con,jint id,jintArray data)1046 nContextSendMessage(JNIEnv *_env, jobject _this, jlong con, jint id, jintArray data)
1047 {
1048     jint *ptr = nullptr;
1049     jint len = 0;
1050     if (data) {
1051         len = _env->GetArrayLength(data);
1052         ptr = _env->GetIntArrayElements(data, nullptr);
1053         if (ptr == nullptr) {
1054             ALOGE("Failed to get Java array elements");
1055             return;
1056         }
1057     }
1058     if (kLogApi) {
1059         ALOGD("nContextSendMessage, con(%p), id(%i), len(%i)", (RsContext)con, id, len);
1060     }
1061     rsContextSendMessage((RsContext)con, id, (const uint8_t *)ptr, len * sizeof(int));
1062     if (data) {
1063         _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
1064     }
1065 }
1066 
1067 
1068 
1069 static jlong
nElementCreate(JNIEnv * _env,jobject _this,jlong con,jlong type,jint kind,jboolean norm,jint size)1070 nElementCreate(JNIEnv *_env, jobject _this, jlong con, jlong type, jint kind, jboolean norm,
1071                jint size)
1072 {
1073     if (kLogApi) {
1074         ALOGD("nElementCreate, con(%p), type(%" PRId64 "), kind(%i), norm(%i), size(%i)", (RsContext)con,
1075               type, kind, norm, size);
1076     }
1077     return (jlong)(uintptr_t)rsElementCreate((RsContext)con, (RsDataType)type, (RsDataKind)kind,
1078                                              norm, size);
1079 }
1080 
1081 static jlong
nElementCreate2(JNIEnv * _env,jobject _this,jlong con,jlongArray _ids,jobjectArray _names,jintArray _arraySizes)1082 nElementCreate2(JNIEnv *_env, jobject _this, jlong con,
1083                 jlongArray _ids, jobjectArray _names, jintArray _arraySizes)
1084 {
1085     int fieldCount = _env->GetArrayLength(_ids);
1086     if (kLogApi) {
1087         ALOGD("nElementCreate2, con(%p)", (RsContext)con);
1088     }
1089 
1090     jlong *jIds = _env->GetLongArrayElements(_ids, nullptr);
1091     if (jIds == nullptr) {
1092         ALOGE("Failed to get Java array elements: ids");
1093         return 0;
1094     }
1095     jint *jArraySizes = _env->GetIntArrayElements(_arraySizes, nullptr);
1096     if (jArraySizes == nullptr) {
1097         ALOGE("Failed to get Java array elements: arraySizes");
1098         return 0;
1099     }
1100 
1101     RsElement *ids = (RsElement*)malloc(fieldCount * sizeof(RsElement));
1102     uint32_t *arraySizes = (uint32_t *)malloc(fieldCount * sizeof(uint32_t));
1103 
1104     for(int i = 0; i < fieldCount; i ++) {
1105         ids[i] = (RsElement)jIds[i];
1106         arraySizes[i] = (uint32_t)jArraySizes[i];
1107     }
1108 
1109     AutoJavaStringArrayToUTF8 names(_env, _names, fieldCount);
1110 
1111     const char **nameArray = names.c_str();
1112     size_t *sizeArray = names.c_str_len();
1113 
1114     jlong id = (jlong)(uintptr_t)rsElementCreate2((RsContext)con,
1115                                      (const RsElement *)ids, fieldCount,
1116                                      nameArray, fieldCount * sizeof(size_t),  sizeArray,
1117                                      (const uint32_t *)arraySizes, fieldCount);
1118 
1119     free(ids);
1120     free(arraySizes);
1121     _env->ReleaseLongArrayElements(_ids, jIds, JNI_ABORT);
1122     _env->ReleaseIntArrayElements(_arraySizes, jArraySizes, JNI_ABORT);
1123 
1124     return (jlong)(uintptr_t)id;
1125 }
1126 
1127 static void
nElementGetNativeData(JNIEnv * _env,jobject _this,jlong con,jlong id,jintArray _elementData)1128 nElementGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jintArray _elementData)
1129 {
1130     int dataSize = _env->GetArrayLength(_elementData);
1131     if (kLogApi) {
1132         ALOGD("nElementGetNativeData, con(%p)", (RsContext)con);
1133     }
1134 
1135     // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
1136     assert(dataSize == 5);
1137 
1138     uint32_t elementData[5];
1139     rsaElementGetNativeData((RsContext)con, (RsElement)id, elementData, dataSize);
1140 
1141     for(jint i = 0; i < dataSize; i ++) {
1142         const jint data = (jint)elementData[i];
1143         _env->SetIntArrayRegion(_elementData, i, 1, &data);
1144     }
1145 }
1146 
1147 
1148 static void
nElementGetSubElements(JNIEnv * _env,jobject _this,jlong con,jlong id,jlongArray _IDs,jobjectArray _names,jintArray _arraySizes)1149 nElementGetSubElements(JNIEnv *_env, jobject _this, jlong con, jlong id,
1150                        jlongArray _IDs,
1151                        jobjectArray _names,
1152                        jintArray _arraySizes)
1153 {
1154     uint32_t dataSize = _env->GetArrayLength(_IDs);
1155     if (kLogApi) {
1156         ALOGD("nElementGetSubElements, con(%p)", (RsContext)con);
1157     }
1158 
1159     uintptr_t *ids = (uintptr_t*)malloc(dataSize * sizeof(uintptr_t));
1160     const char **names = (const char **)malloc(dataSize * sizeof(const char *));
1161     size_t *arraySizes = (size_t *)malloc(dataSize * sizeof(size_t));
1162 
1163     rsaElementGetSubElements((RsContext)con, (RsElement)id, ids, names, arraySizes,
1164                              (uint32_t)dataSize);
1165 
1166     for(uint32_t i = 0; i < dataSize; i++) {
1167         const jlong id = (jlong)(uintptr_t)ids[i];
1168         const jint arraySize = (jint)arraySizes[i];
1169         _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
1170         _env->SetLongArrayRegion(_IDs, i, 1, &id);
1171         _env->SetIntArrayRegion(_arraySizes, i, 1, &arraySize);
1172     }
1173 
1174     free(ids);
1175     free(names);
1176     free(arraySizes);
1177 }
1178 
1179 // -----------------------------------
1180 
1181 static jlong
nTypeCreate(JNIEnv * _env,jobject _this,jlong con,jlong eid,jint dimx,jint dimy,jint dimz,jboolean mips,jboolean faces,jint yuv)1182 nTypeCreate(JNIEnv *_env, jobject _this, jlong con, jlong eid,
1183             jint dimx, jint dimy, jint dimz, jboolean mips, jboolean faces, jint yuv)
1184 {
1185     if (kLogApi) {
1186         ALOGD("nTypeCreate, con(%p) eid(%p), x(%i), y(%i), z(%i), mips(%i), faces(%i), yuv(%i)",
1187               (RsContext)con, (void*)eid, dimx, dimy, dimz, mips, faces, yuv);
1188     }
1189 
1190     return (jlong)(uintptr_t)rsTypeCreate((RsContext)con, (RsElement)eid, dimx, dimy, dimz, mips,
1191                                           faces, yuv);
1192 }
1193 
1194 static void
nTypeGetNativeData(JNIEnv * _env,jobject _this,jlong con,jlong id,jlongArray _typeData)1195 nTypeGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jlongArray _typeData)
1196 {
1197     // We are packing 6 items: mDimX; mDimY; mDimZ;
1198     // mDimLOD; mDimFaces; mElement; into typeData
1199     int elementCount = _env->GetArrayLength(_typeData);
1200 
1201     assert(elementCount == 6);
1202     if (kLogApi) {
1203         ALOGD("nTypeGetNativeData, con(%p)", (RsContext)con);
1204     }
1205 
1206     uintptr_t typeData[6];
1207     rsaTypeGetNativeData((RsContext)con, (RsType)id, typeData, 6);
1208 
1209     for(jint i = 0; i < elementCount; i ++) {
1210         const jlong data = (jlong)(uintptr_t)typeData[i];
1211         _env->SetLongArrayRegion(_typeData, i, 1, &data);
1212     }
1213 }
1214 
1215 // -----------------------------------
1216 
1217 static jlong
nAllocationCreateTyped(JNIEnv * _env,jobject _this,jlong con,jlong type,jint mips,jint usage,jlong pointer)1218 nAllocationCreateTyped(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mips, jint usage,
1219                        jlong pointer)
1220 {
1221     if (kLogApi) {
1222         ALOGD("nAllocationCreateTyped, con(%p), type(%p), mip(%i), usage(%i), ptr(%p)",
1223               (RsContext)con, (RsElement)type, mips, usage, (void *)pointer);
1224     }
1225     return (jlong)(uintptr_t) rsAllocationCreateTyped((RsContext)con, (RsType)type,
1226                                                       (RsAllocationMipmapControl)mips,
1227                                                       (uint32_t)usage, (uintptr_t)pointer);
1228 }
1229 
1230 static void
nAllocationSyncAll(JNIEnv * _env,jobject _this,jlong con,jlong a,jint bits)1231 nAllocationSyncAll(JNIEnv *_env, jobject _this, jlong con, jlong a, jint bits)
1232 {
1233     if (kLogApi) {
1234         ALOGD("nAllocationSyncAll, con(%p), a(%p), bits(0x%08x)", (RsContext)con, (RsAllocation)a,
1235               bits);
1236     }
1237     rsAllocationSyncAll((RsContext)con, (RsAllocation)a, (RsAllocationUsageType)bits);
1238 }
1239 
1240 static void
nAllocationSetupBufferQueue(JNIEnv * _env,jobject _this,jlong con,jlong alloc,jint numAlloc)1241 nAllocationSetupBufferQueue(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint numAlloc)
1242 {
1243     if (kLogApi) {
1244         ALOGD("nAllocationSetupBufferQueue, con(%p), alloc(%p), numAlloc(%d)", (RsContext)con,
1245               (RsAllocation)alloc, numAlloc);
1246     }
1247     rsAllocationSetupBufferQueue((RsContext)con, (RsAllocation)alloc, (uint32_t)numAlloc);
1248 }
1249 
1250 static void
nAllocationShareBufferQueue(JNIEnv * _env,jobject _this,jlong con,jlong alloc1,jlong alloc2)1251 nAllocationShareBufferQueue(JNIEnv *_env, jobject _this, jlong con, jlong alloc1, jlong alloc2)
1252 {
1253     if (kLogApi) {
1254         ALOGD("nAllocationShareBufferQueue, con(%p), alloc1(%p), alloc2(%p)", (RsContext)con,
1255               (RsAllocation)alloc1, (RsAllocation)alloc2);
1256     }
1257 
1258     rsAllocationShareBufferQueue((RsContext)con, (RsAllocation)alloc1, (RsAllocation)alloc2);
1259 }
1260 
1261 static jobject
nAllocationGetSurface(JNIEnv * _env,jobject _this,jlong con,jlong a)1262 nAllocationGetSurface(JNIEnv *_env, jobject _this, jlong con, jlong a)
1263 {
1264     if (kLogApi) {
1265         ALOGD("nAllocationGetSurface, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1266     }
1267 
1268     ANativeWindow *anw = (ANativeWindow *)rsAllocationGetSurface((RsContext)con, (RsAllocation)a);
1269 
1270     sp<Surface> surface(static_cast<Surface*>(anw));
1271     sp<IGraphicBufferProducer> bp = surface->getIGraphicBufferProducer();
1272 
1273     jobject o = android_view_Surface_createFromIGraphicBufferProducer(_env, bp);
1274     return o;
1275 }
1276 
1277 static void
nAllocationSetSurface(JNIEnv * _env,jobject _this,jlong con,jlong alloc,jobject sur)1278 nAllocationSetSurface(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject sur)
1279 {
1280     if (kLogApi) {
1281         ALOGD("nAllocationSetSurface, con(%p), alloc(%p), surface(%p)", (RsContext)con,
1282               (RsAllocation)alloc, (Surface *)sur);
1283     }
1284 
1285     ANativeWindow *anw = nullptr;
1286     if (sur != 0) {
1287         // Connect the native window handle to buffer queue.
1288         anw = ANativeWindow_fromSurface(_env, sur);
1289         native_window_api_connect(anw, NATIVE_WINDOW_API_CPU);
1290     }
1291 
1292     rsAllocationSetSurface((RsContext)con, (RsAllocation)alloc, anw);
1293 }
1294 
1295 static void
nAllocationIoSend(JNIEnv * _env,jobject _this,jlong con,jlong alloc)1296 nAllocationIoSend(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
1297 {
1298     if (kLogApi) {
1299         ALOGD("nAllocationIoSend, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
1300     }
1301     rsAllocationIoSend((RsContext)con, (RsAllocation)alloc);
1302 }
1303 
1304 static jlong
nAllocationIoReceive(JNIEnv * _env,jobject _this,jlong con,jlong alloc)1305 nAllocationIoReceive(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
1306 {
1307     if (kLogApi) {
1308         ALOGD("nAllocationIoReceive, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
1309     }
1310     return (jlong) rsAllocationIoReceive((RsContext)con, (RsAllocation)alloc);
1311 }
1312 
1313 static void
nAllocationGenerateMipmaps(JNIEnv * _env,jobject _this,jlong con,jlong alloc)1314 nAllocationGenerateMipmaps(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
1315 {
1316     if (kLogApi) {
1317         ALOGD("nAllocationGenerateMipmaps, con(%p), a(%p)", (RsContext)con, (RsAllocation)alloc);
1318     }
1319     rsAllocationGenerateMipmaps((RsContext)con, (RsAllocation)alloc);
1320 }
1321 
computeByteSize(const android::graphics::Bitmap & bitmap)1322 static size_t computeByteSize(const android::graphics::Bitmap& bitmap) {
1323     AndroidBitmapInfo info = bitmap.getInfo();
1324     return info.height * info.stride;
1325 }
1326 
1327 static jlong
nAllocationCreateFromBitmap(JNIEnv * _env,jobject _this,jlong con,jlong type,jint mip,jobject jbitmap,jint usage)1328 nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1329                             jobject jbitmap, jint usage)
1330 {
1331     android::graphics::Bitmap bitmap(_env, jbitmap);
1332     const void* ptr = bitmap.getPixels();
1333     jlong id = (jlong)(uintptr_t)rsAllocationCreateFromBitmap((RsContext)con,
1334                                                   (RsType)type, (RsAllocationMipmapControl)mip,
1335                                                   ptr, computeByteSize(bitmap), usage);
1336     return id;
1337 }
1338 
1339 static jlong
nAllocationCreateBitmapBackedAllocation(JNIEnv * _env,jobject _this,jlong con,jlong type,jint mip,jobject jbitmap,jint usage)1340 nAllocationCreateBitmapBackedAllocation(JNIEnv *_env, jobject _this, jlong con, jlong type,
1341                                         jint mip, jobject jbitmap, jint usage)
1342 {
1343     android::graphics::Bitmap bitmap(_env, jbitmap);
1344     const void* ptr = bitmap.getPixels();
1345     jlong id = (jlong)(uintptr_t)rsAllocationCreateTyped((RsContext)con,
1346                                             (RsType)type, (RsAllocationMipmapControl)mip,
1347                                             (uint32_t)usage, (uintptr_t)ptr);
1348     return id;
1349 }
1350 
1351 static jlong
nAllocationCubeCreateFromBitmap(JNIEnv * _env,jobject _this,jlong con,jlong type,jint mip,jobject jbitmap,jint usage)1352 nAllocationCubeCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1353                                 jobject jbitmap, jint usage)
1354 {
1355     android::graphics::Bitmap bitmap(_env, jbitmap);
1356     const void* ptr = bitmap.getPixels();
1357     jlong id = (jlong)(uintptr_t)rsAllocationCubeCreateFromBitmap((RsContext)con,
1358                                                       (RsType)type, (RsAllocationMipmapControl)mip,
1359                                                       ptr, computeByteSize(bitmap), usage);
1360     return id;
1361 }
1362 
1363 static void
nAllocationCopyFromBitmap(JNIEnv * _env,jobject _this,jlong con,jlong alloc,jobject jbitmap)1364 nAllocationCopyFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
1365 {
1366     android::graphics::Bitmap bitmap(_env, jbitmap);
1367     int w = bitmap.getInfo().width;
1368     int h = bitmap.getInfo().height;
1369 
1370     const void* ptr = bitmap.getPixels();
1371     rsAllocation2DData((RsContext)con, (RsAllocation)alloc, 0, 0,
1372                        0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
1373                        w, h, ptr, computeByteSize(bitmap), 0);
1374 }
1375 
1376 static void
nAllocationCopyToBitmap(JNIEnv * _env,jobject _this,jlong con,jlong alloc,jobject jbitmap)1377 nAllocationCopyToBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
1378 {
1379     android::graphics::Bitmap bitmap(_env, jbitmap);
1380     void* ptr = bitmap.getPixels();
1381     rsAllocationCopyToBitmap((RsContext)con, (RsAllocation)alloc, ptr, computeByteSize(bitmap));
1382     bitmap.notifyPixelsChanged();
1383 }
1384 
1385 // Copies from the Java object data into the Allocation pointed to by _alloc.
1386 static void
nAllocationData1D(JNIEnv * _env,jobject _this,jlong con,jlong _alloc,jint offset,jint lod,jint count,jobject data,jint sizeBytes,jint dataType,jint mSize,jboolean usePadding)1387 nAllocationData1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
1388                   jint count, jobject data, jint sizeBytes, jint dataType, jint mSize,
1389                   jboolean usePadding)
1390 {
1391     RsAllocation *alloc = (RsAllocation *)_alloc;
1392     if (kLogApi) {
1393         ALOGD("nAllocation1DData, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1394               "dataType(%i)", (RsContext)con, (RsAllocation)alloc, offset, count, sizeBytes,
1395               dataType);
1396     }
1397     PER_ARRAY_TYPE(nullptr, rsAllocation1DData, true,
1398                    (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
1399 }
1400 
1401 static void
nAllocationElementData(JNIEnv * _env,jobject _this,jlong con,jlong alloc,jint xoff,jint yoff,jint zoff,jint lod,jint compIdx,jbyteArray data,jint sizeBytes)1402 nAllocationElementData(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1403                        jint xoff, jint yoff, jint zoff,
1404                        jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
1405 {
1406     if (kLogApi) {
1407         jint len = _env->GetArrayLength(data);
1408         ALOGD("nAllocationElementData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1409               "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
1410               sizeBytes);
1411     }
1412     jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
1413     if (ptr == nullptr) {
1414         ALOGE("Failed to get Java array elements");
1415         return;
1416     }
1417     rsAllocationElementData((RsContext)con, (RsAllocation)alloc,
1418                             xoff, yoff, zoff,
1419                             lod, ptr, sizeBytes, compIdx);
1420     _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1421 }
1422 
1423 
1424 // Copies from the Java object data into the Allocation pointed to by _alloc.
1425 static void
nAllocationData2D(JNIEnv * _env,jobject _this,jlong con,jlong _alloc,jint xoff,jint yoff,jint lod,jint _face,jint w,jint h,jobject data,jint sizeBytes,jint dataType,jint mSize,jboolean usePadding)1426 nAllocationData2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
1427                   jint w, jint h, jobject data, jint sizeBytes, jint dataType, jint mSize,
1428                   jboolean usePadding)
1429 {
1430     RsAllocation *alloc = (RsAllocation *)_alloc;
1431     RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
1432     if (kLogApi) {
1433         ALOGD("nAllocation2DData, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1434               "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1435     }
1436     int count = w * h;
1437     PER_ARRAY_TYPE(nullptr, rsAllocation2DData, true,
1438                    (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
1439 }
1440 
1441 // Copies from the Allocation pointed to by srcAlloc into the Allocation
1442 // pointed to by dstAlloc.
1443 static void
nAllocationData2D_alloc(JNIEnv * _env,jobject _this,jlong con,jlong dstAlloc,jint dstXoff,jint dstYoff,jint dstMip,jint dstFace,jint width,jint height,jlong srcAlloc,jint srcXoff,jint srcYoff,jint srcMip,jint srcFace)1444 nAllocationData2D_alloc(JNIEnv *_env, jobject _this, jlong con,
1445                         jlong dstAlloc, jint dstXoff, jint dstYoff,
1446                         jint dstMip, jint dstFace,
1447                         jint width, jint height,
1448                         jlong srcAlloc, jint srcXoff, jint srcYoff,
1449                         jint srcMip, jint srcFace)
1450 {
1451     if (kLogApi) {
1452         ALOGD("nAllocation2DData_s, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1453               " dstMip(%i), dstFace(%i), width(%i), height(%i),"
1454               " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i), srcFace(%i)",
1455               (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip, dstFace,
1456               width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
1457     }
1458 
1459     rsAllocationCopy2DRange((RsContext)con,
1460                             (RsAllocation)dstAlloc,
1461                             dstXoff, dstYoff,
1462                             dstMip, dstFace,
1463                             width, height,
1464                             (RsAllocation)srcAlloc,
1465                             srcXoff, srcYoff,
1466                             srcMip, srcFace);
1467 }
1468 
1469 // Copies from the Java object data into the Allocation pointed to by _alloc.
1470 static void
nAllocationData3D(JNIEnv * _env,jobject _this,jlong con,jlong _alloc,jint xoff,jint yoff,jint zoff,jint lod,jint w,jint h,jint d,jobject data,jint sizeBytes,jint dataType,jint mSize,jboolean usePadding)1471 nAllocationData3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
1472                   jint w, jint h, jint d, jobject data, jint sizeBytes, jint dataType,
1473                   jint mSize, jboolean usePadding)
1474 {
1475     RsAllocation *alloc = (RsAllocation *)_alloc;
1476     if (kLogApi) {
1477         ALOGD("nAllocation3DData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1478               " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1479               lod, w, h, d, sizeBytes);
1480     }
1481     int count = w * h * d;
1482     PER_ARRAY_TYPE(nullptr, rsAllocation3DData, true,
1483                    (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
1484 }
1485 
1486 // Copies from the Allocation pointed to by srcAlloc into the Allocation
1487 // pointed to by dstAlloc.
1488 static void
nAllocationData3D_alloc(JNIEnv * _env,jobject _this,jlong con,jlong dstAlloc,jint dstXoff,jint dstYoff,jint dstZoff,jint dstMip,jint width,jint height,jint depth,jlong srcAlloc,jint srcXoff,jint srcYoff,jint srcZoff,jint srcMip)1489 nAllocationData3D_alloc(JNIEnv *_env, jobject _this, jlong con,
1490                         jlong dstAlloc, jint dstXoff, jint dstYoff, jint dstZoff,
1491                         jint dstMip,
1492                         jint width, jint height, jint depth,
1493                         jlong srcAlloc, jint srcXoff, jint srcYoff, jint srcZoff,
1494                         jint srcMip)
1495 {
1496     if (kLogApi) {
1497         ALOGD("nAllocationData3D_alloc, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1498               " dstMip(%i), width(%i), height(%i),"
1499               " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i)",
1500               (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip,
1501               width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip);
1502     }
1503 
1504     rsAllocationCopy3DRange((RsContext)con,
1505                             (RsAllocation)dstAlloc,
1506                             dstXoff, dstYoff, dstZoff, dstMip,
1507                             width, height, depth,
1508                             (RsAllocation)srcAlloc,
1509                             srcXoff, srcYoff, srcZoff, srcMip);
1510 }
1511 
1512 
1513 // Copies from the Allocation pointed to by _alloc into the Java object data.
1514 static void
nAllocationRead(JNIEnv * _env,jobject _this,jlong con,jlong _alloc,jobject data,jint dataType,jint mSize,jboolean usePadding)1515 nAllocationRead(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jobject data, jint dataType,
1516                 jint mSize, jboolean usePadding)
1517 {
1518     RsAllocation *alloc = (RsAllocation *)_alloc;
1519     if (kLogApi) {
1520         ALOGD("nAllocationRead, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
1521     }
1522     int count = 0;
1523     PER_ARRAY_TYPE(0, rsAllocationRead, false,
1524                    (RsContext)con, alloc, ptr, len * typeBytes);
1525 }
1526 
1527 // Copies from the Allocation pointed to by _alloc into the Java object data.
1528 static void
nAllocationRead1D(JNIEnv * _env,jobject _this,jlong con,jlong _alloc,jint offset,jint lod,jint count,jobject data,jint sizeBytes,jint dataType,jint mSize,jboolean usePadding)1529 nAllocationRead1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
1530                   jint count, jobject data, jint sizeBytes, jint dataType,
1531                   jint mSize, jboolean usePadding)
1532 {
1533     RsAllocation *alloc = (RsAllocation *)_alloc;
1534     if (kLogApi) {
1535         ALOGD("nAllocation1DRead, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1536               "dataType(%i)", (RsContext)con, alloc, offset, count, sizeBytes, dataType);
1537     }
1538     PER_ARRAY_TYPE(0, rsAllocation1DRead, false,
1539                    (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
1540 }
1541 
1542 // Copies from the Element in the Allocation pointed to by _alloc into the Java array data.
1543 static void
nAllocationElementRead(JNIEnv * _env,jobject _this,jlong con,jlong alloc,jint xoff,jint yoff,jint zoff,jint lod,jint compIdx,jbyteArray data,jint sizeBytes)1544 nAllocationElementRead(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1545                        jint xoff, jint yoff, jint zoff,
1546                        jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
1547 {
1548     if (kLogApi) {
1549         jint len = _env->GetArrayLength(data);
1550         ALOGD("nAllocationElementRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1551               "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
1552               sizeBytes);
1553     }
1554     jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
1555     if (ptr == nullptr) {
1556         ALOGE("Failed to get Java array elements");
1557         return;
1558     }
1559     rsAllocationElementRead((RsContext)con, (RsAllocation)alloc,
1560                             xoff, yoff, zoff,
1561                             lod, ptr, sizeBytes, compIdx);
1562     _env->ReleaseByteArrayElements(data, ptr, 0);
1563 }
1564 
1565 // Copies from the Allocation pointed to by _alloc into the Java object data.
1566 static void
nAllocationRead2D(JNIEnv * _env,jobject _this,jlong con,jlong _alloc,jint xoff,jint yoff,jint lod,jint _face,jint w,jint h,jobject data,jint sizeBytes,jint dataType,jint mSize,jboolean usePadding)1567 nAllocationRead2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
1568                   jint w, jint h, jobject data, jint sizeBytes, jint dataType,
1569                   jint mSize, jboolean usePadding)
1570 {
1571     RsAllocation *alloc = (RsAllocation *)_alloc;
1572     RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
1573     if (kLogApi) {
1574         ALOGD("nAllocation2DRead, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1575               "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1576     }
1577     int count = w * h;
1578     PER_ARRAY_TYPE(0, rsAllocation2DRead, false,
1579                    (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
1580 }
1581 
1582 // Copies from the Allocation pointed to by _alloc into the Java object data.
1583 static void
nAllocationRead3D(JNIEnv * _env,jobject _this,jlong con,jlong _alloc,jint xoff,jint yoff,jint zoff,jint lod,jint w,jint h,jint d,jobject data,int sizeBytes,int dataType,jint mSize,jboolean usePadding)1584 nAllocationRead3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
1585                   jint w, jint h, jint d, jobject data, int sizeBytes, int dataType,
1586                   jint mSize, jboolean usePadding)
1587 {
1588     RsAllocation *alloc = (RsAllocation *)_alloc;
1589     if (kLogApi) {
1590         ALOGD("nAllocation3DRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1591               " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1592               lod, w, h, d, sizeBytes);
1593     }
1594     int count = w * h * d;
1595     PER_ARRAY_TYPE(nullptr, rsAllocation3DRead, false,
1596                    (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
1597 }
1598 
1599 static jlong
nAllocationGetType(JNIEnv * _env,jobject _this,jlong con,jlong a)1600 nAllocationGetType(JNIEnv *_env, jobject _this, jlong con, jlong a)
1601 {
1602     if (kLogApi) {
1603         ALOGD("nAllocationGetType, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1604     }
1605     return (jlong)(uintptr_t) rsaAllocationGetType((RsContext)con, (RsAllocation)a);
1606 }
1607 
1608 static void
nAllocationResize1D(JNIEnv * _env,jobject _this,jlong con,jlong alloc,jint dimX)1609 nAllocationResize1D(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint dimX)
1610 {
1611     if (kLogApi) {
1612         ALOGD("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", (RsContext)con,
1613               (RsAllocation)alloc, dimX);
1614     }
1615     rsAllocationResize1D((RsContext)con, (RsAllocation)alloc, dimX);
1616 }
1617 
1618 
1619 static jlong
nAllocationAdapterCreate(JNIEnv * _env,jobject _this,jlong con,jlong basealloc,jlong type)1620 nAllocationAdapterCreate(JNIEnv *_env, jobject _this, jlong con, jlong basealloc, jlong type)
1621 {
1622     if (kLogApi) {
1623         ALOGD("nAllocationAdapterCreate, con(%p), base(%p), type(%p)",
1624               (RsContext)con, (RsAllocation)basealloc, (RsElement)type);
1625     }
1626     return (jlong)(uintptr_t) rsAllocationAdapterCreate((RsContext)con, (RsType)type,
1627                                                         (RsAllocation)basealloc);
1628 
1629 }
1630 
1631 static void
nAllocationAdapterOffset(JNIEnv * _env,jobject _this,jlong con,jlong alloc,jint x,jint y,jint z,jint face,jint lod,jint a1,jint a2,jint a3,jint a4)1632 nAllocationAdapterOffset(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1633                         jint x, jint y, jint z, jint face, jint lod,
1634                         jint a1, jint a2, jint a3, jint a4)
1635 {
1636     uint32_t params[] = {
1637         (uint32_t)x, (uint32_t)y, (uint32_t)z, (uint32_t)face,
1638         (uint32_t)lod, (uint32_t)a1, (uint32_t)a2, (uint32_t)a3, (uint32_t)a4
1639     };
1640     if (kLogApi) {
1641         ALOGD("nAllocationAdapterOffset, con(%p), alloc(%p), x(%i), y(%i), z(%i), face(%i), lod(%i), arrays(%i %i %i %i)",
1642               (RsContext)con, (RsAllocation)alloc, x, y, z, face, lod, a1, a2, a3, a4);
1643     }
1644     rsAllocationAdapterOffset((RsContext)con, (RsAllocation)alloc,
1645                               params, sizeof(params));
1646 }
1647 
1648 
1649 // -----------------------------------
1650 
1651 static jlong
nFileA3DCreateFromAssetStream(JNIEnv * _env,jobject _this,jlong con,jlong native_asset)1652 nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con, jlong native_asset)
1653 {
1654     Asset* asset = reinterpret_cast<Asset*>(native_asset);
1655     ALOGV("______nFileA3D %p", asset);
1656 
1657     jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromMemory((RsContext)con, asset->getBuffer(false), asset->getLength());
1658     return id;
1659 }
1660 
1661 static jlong
nFileA3DCreateFromAsset(JNIEnv * _env,jobject _this,jlong con,jobject _assetMgr,jstring _path)1662 nFileA3DCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path)
1663 {
1664     Guarded<AssetManager2>* mgr = AssetManagerForJavaObject(_env, _assetMgr);
1665     if (mgr == nullptr) {
1666         return 0;
1667     }
1668 
1669     AutoJavaStringToUTF8 str(_env, _path);
1670     std::unique_ptr<Asset> asset;
1671     {
1672         ScopedLock<AssetManager2> locked_mgr(*mgr);
1673         asset = locked_mgr->Open(str.c_str(), Asset::ACCESS_BUFFER);
1674         if (asset == nullptr) {
1675             return 0;
1676         }
1677     }
1678 
1679     jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromAsset((RsContext)con, asset.release());
1680     return id;
1681 }
1682 
1683 static jlong
nFileA3DCreateFromFile(JNIEnv * _env,jobject _this,jlong con,jstring fileName)1684 nFileA3DCreateFromFile(JNIEnv *_env, jobject _this, jlong con, jstring fileName)
1685 {
1686     AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
1687     jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromFile((RsContext)con, fileNameUTF.c_str());
1688 
1689     return id;
1690 }
1691 
1692 static jint
nFileA3DGetNumIndexEntries(JNIEnv * _env,jobject _this,jlong con,jlong fileA3D)1693 nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D)
1694 {
1695     int32_t numEntries = 0;
1696     rsaFileA3DGetNumIndexEntries((RsContext)con, &numEntries, (RsFile)fileA3D);
1697     return (jint)numEntries;
1698 }
1699 
1700 static void
nFileA3DGetIndexEntries(JNIEnv * _env,jobject _this,jlong con,jlong fileA3D,jint numEntries,jintArray _ids,jobjectArray _entries)1701 nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
1702 {
1703     ALOGV("______nFileA3D %p", (RsFile) fileA3D);
1704     RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
1705 
1706     rsaFileA3DGetIndexEntries((RsContext)con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
1707 
1708     for(jint i = 0; i < numEntries; i ++) {
1709         _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
1710         _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
1711     }
1712 
1713     free(fileEntries);
1714 }
1715 
1716 static jlong
nFileA3DGetEntryByIndex(JNIEnv * _env,jobject _this,jlong con,jlong fileA3D,jint index)1717 nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint index)
1718 {
1719     ALOGV("______nFileA3D %p", (RsFile) fileA3D);
1720     jlong id = (jlong)(uintptr_t)rsaFileA3DGetEntryByIndex((RsContext)con, (uint32_t)index, (RsFile)fileA3D);
1721     return id;
1722 }
1723 
1724 // -----------------------------------
1725 
1726 static jlong
nFontCreateFromFile(JNIEnv * _env,jobject _this,jlong con,jstring fileName,jfloat fontSize,jint dpi)1727 nFontCreateFromFile(JNIEnv *_env, jobject _this, jlong con,
1728                     jstring fileName, jfloat fontSize, jint dpi)
1729 {
1730     AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
1731     jlong id = (jlong)(uintptr_t)rsFontCreateFromFile((RsContext)con,
1732                                          fileNameUTF.c_str(), fileNameUTF.length(),
1733                                          fontSize, dpi);
1734 
1735     return id;
1736 }
1737 
1738 static jlong
nFontCreateFromAssetStream(JNIEnv * _env,jobject _this,jlong con,jstring name,jfloat fontSize,jint dpi,jlong native_asset)1739 nFontCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con,
1740                            jstring name, jfloat fontSize, jint dpi, jlong native_asset)
1741 {
1742     Asset* asset = reinterpret_cast<Asset*>(native_asset);
1743     AutoJavaStringToUTF8 nameUTF(_env, name);
1744 
1745     jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
1746                                            nameUTF.c_str(), nameUTF.length(),
1747                                            fontSize, dpi,
1748                                            asset->getBuffer(false), asset->getLength());
1749     return id;
1750 }
1751 
1752 static jlong
nFontCreateFromAsset(JNIEnv * _env,jobject _this,jlong con,jobject _assetMgr,jstring _path,jfloat fontSize,jint dpi)1753 nFontCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path,
1754                      jfloat fontSize, jint dpi)
1755 {
1756     Guarded<AssetManager2>* mgr = AssetManagerForJavaObject(_env, _assetMgr);
1757     if (mgr == nullptr) {
1758         return 0;
1759     }
1760 
1761     AutoJavaStringToUTF8 str(_env, _path);
1762     std::unique_ptr<Asset> asset;
1763     {
1764         ScopedLock<AssetManager2> locked_mgr(*mgr);
1765         asset = locked_mgr->Open(str.c_str(), Asset::ACCESS_BUFFER);
1766         if (asset == nullptr) {
1767             return 0;
1768         }
1769     }
1770 
1771     jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
1772                                            str.c_str(), str.length(),
1773                                            fontSize, dpi,
1774                                            asset->getBuffer(false), asset->getLength());
1775     return id;
1776 }
1777 
1778 // -----------------------------------
1779 
1780 static void
nScriptBindAllocation(JNIEnv * _env,jobject _this,jlong con,jlong script,jlong alloc,jint slot)1781 nScriptBindAllocation(JNIEnv *_env, jobject _this, jlong con, jlong script, jlong alloc, jint slot)
1782 {
1783     if (kLogApi) {
1784         ALOGD("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", (RsContext)con,
1785               (RsScript)script, (RsAllocation)alloc, slot);
1786     }
1787     rsScriptBindAllocation((RsContext)con, (RsScript)script, (RsAllocation)alloc, slot);
1788 }
1789 
1790 static void
nScriptSetVarI(JNIEnv * _env,jobject _this,jlong con,jlong script,jint slot,jint val)1791 nScriptSetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jint val)
1792 {
1793     if (kLogApi) {
1794         ALOGD("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", (RsContext)con, (void *)script,
1795               slot, val);
1796     }
1797     rsScriptSetVarI((RsContext)con, (RsScript)script, slot, val);
1798 }
1799 
1800 static jint
nScriptGetVarI(JNIEnv * _env,jobject _this,jlong con,jlong script,jint slot)1801 nScriptGetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
1802 {
1803     if (kLogApi) {
1804         ALOGD("nScriptGetVarI, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1805     }
1806     int value = 0;
1807     rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
1808     return value;
1809 }
1810 
1811 static void
nScriptSetVarObj(JNIEnv * _env,jobject _this,jlong con,jlong script,jint slot,jlong val)1812 nScriptSetVarObj(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
1813 {
1814     if (kLogApi) {
1815         ALOGD("nScriptSetVarObj, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
1816               slot, val);
1817     }
1818     rsScriptSetVarObj((RsContext)con, (RsScript)script, slot, (RsObjectBase)val);
1819 }
1820 
1821 static void
nScriptSetVarJ(JNIEnv * _env,jobject _this,jlong con,jlong script,jint slot,jlong val)1822 nScriptSetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
1823 {
1824     if (kLogApi) {
1825         ALOGD("nScriptSetVarJ, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
1826               slot, val);
1827     }
1828     rsScriptSetVarJ((RsContext)con, (RsScript)script, slot, val);
1829 }
1830 
1831 static jlong
nScriptGetVarJ(JNIEnv * _env,jobject _this,jlong con,jlong script,jint slot)1832 nScriptGetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
1833 {
1834     if (kLogApi) {
1835         ALOGD("nScriptGetVarJ, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1836     }
1837     jlong value = 0;
1838     rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
1839     return value;
1840 }
1841 
1842 static void
nScriptSetVarF(JNIEnv * _env,jobject _this,jlong con,jlong script,jint slot,float val)1843 nScriptSetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, float val)
1844 {
1845     if (kLogApi) {
1846         ALOGD("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", (RsContext)con, (void *)script,
1847               slot, val);
1848     }
1849     rsScriptSetVarF((RsContext)con, (RsScript)script, slot, val);
1850 }
1851 
1852 static jfloat
nScriptGetVarF(JNIEnv * _env,jobject _this,jlong con,jlong script,jint slot)1853 nScriptGetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
1854 {
1855     if (kLogApi) {
1856         ALOGD("nScriptGetVarF, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1857     }
1858     jfloat value = 0;
1859     rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
1860     return value;
1861 }
1862 
1863 static void
nScriptSetVarD(JNIEnv * _env,jobject _this,jlong con,jlong script,jint slot,double val)1864 nScriptSetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, double val)
1865 {
1866     if (kLogApi) {
1867         ALOGD("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", (RsContext)con, (void *)script,
1868               slot, val);
1869     }
1870     rsScriptSetVarD((RsContext)con, (RsScript)script, slot, val);
1871 }
1872 
1873 static jdouble
nScriptGetVarD(JNIEnv * _env,jobject _this,jlong con,jlong script,jint slot)1874 nScriptGetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
1875 {
1876     if (kLogApi) {
1877         ALOGD("nScriptGetVarD, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1878     }
1879     jdouble value = 0;
1880     rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
1881     return value;
1882 }
1883 
1884 static void
nScriptSetVarV(JNIEnv * _env,jobject _this,jlong con,jlong script,jint slot,jbyteArray data)1885 nScriptSetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
1886 {
1887     if (kLogApi) {
1888         ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1889     }
1890     jint len = _env->GetArrayLength(data);
1891     jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
1892     if (ptr == nullptr) {
1893         ALOGE("Failed to get Java array elements");
1894         return;
1895     }
1896     rsScriptSetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
1897     _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1898 }
1899 
1900 static void
nScriptGetVarV(JNIEnv * _env,jobject _this,jlong con,jlong script,jint slot,jbyteArray data)1901 nScriptGetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
1902 {
1903     if (kLogApi) {
1904         ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1905     }
1906     jint len = _env->GetArrayLength(data);
1907     jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
1908     if (ptr == nullptr) {
1909         ALOGE("Failed to get Java array elements");
1910         return;
1911     }
1912     rsScriptGetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
1913     _env->ReleaseByteArrayElements(data, ptr, 0);
1914 }
1915 
1916 static void
nScriptSetVarVE(JNIEnv * _env,jobject _this,jlong con,jlong script,jint slot,jbyteArray data,jlong elem,jintArray dims)1917 nScriptSetVarVE(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data,
1918                 jlong elem, jintArray dims)
1919 {
1920     if (kLogApi) {
1921         ALOGD("nScriptSetVarVE, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1922     }
1923     jint len = _env->GetArrayLength(data);
1924     jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
1925     if (ptr == nullptr) {
1926         ALOGE("Failed to get Java array elements");
1927         return;
1928     }
1929     jint dimsLen = _env->GetArrayLength(dims) * sizeof(int);
1930     jint *dimsPtr = _env->GetIntArrayElements(dims, nullptr);
1931     if (dimsPtr == nullptr) {
1932         ALOGE("Failed to get Java array elements");
1933         return;
1934     }
1935     rsScriptSetVarVE((RsContext)con, (RsScript)script, slot, ptr, len, (RsElement)elem,
1936                      (const uint32_t*) dimsPtr, dimsLen);
1937     _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1938     _env->ReleaseIntArrayElements(dims, dimsPtr, JNI_ABORT);
1939 }
1940 
1941 
1942 static void
nScriptSetTimeZone(JNIEnv * _env,jobject _this,jlong con,jlong script,jbyteArray timeZone)1943 nScriptSetTimeZone(JNIEnv *_env, jobject _this, jlong con, jlong script, jbyteArray timeZone)
1944 {
1945     if (kLogApi) {
1946         ALOGD("nScriptCSetTimeZone, con(%p), s(%p)", (RsContext)con, (void *)script);
1947     }
1948 
1949     jint length = _env->GetArrayLength(timeZone);
1950     jbyte* timeZone_ptr;
1951     timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
1952     if (timeZone_ptr == nullptr) {
1953         ALOGE("Failed to get Java array elements");
1954         return;
1955     }
1956 
1957     rsScriptSetTimeZone((RsContext)con, (RsScript)script, (const char *)timeZone_ptr, length);
1958 
1959     if (timeZone_ptr) {
1960         _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
1961     }
1962 }
1963 
1964 static void
nScriptInvoke(JNIEnv * _env,jobject _this,jlong con,jlong obj,jint slot)1965 nScriptInvoke(JNIEnv *_env, jobject _this, jlong con, jlong obj, jint slot)
1966 {
1967     if (kLogApi) {
1968         ALOGD("nScriptInvoke, con(%p), script(%p)", (RsContext)con, (void *)obj);
1969     }
1970     rsScriptInvoke((RsContext)con, (RsScript)obj, slot);
1971 }
1972 
1973 static void
nScriptInvokeV(JNIEnv * _env,jobject _this,jlong con,jlong script,jint slot,jbyteArray data)1974 nScriptInvokeV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
1975 {
1976     if (kLogApi) {
1977         ALOGD("nScriptInvokeV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1978     }
1979     jint len = _env->GetArrayLength(data);
1980     jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
1981     if (ptr == nullptr) {
1982         ALOGE("Failed to get Java array elements");
1983         return;
1984     }
1985     rsScriptInvokeV((RsContext)con, (RsScript)script, slot, ptr, len);
1986     _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1987 }
1988 
1989 static void
nScriptForEach(JNIEnv * _env,jobject _this,jlong con,jlong script,jint slot,jlongArray ains,jlong aout,jbyteArray params,jintArray limits)1990 nScriptForEach(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
1991                jlongArray ains, jlong aout, jbyteArray params,
1992                jintArray limits)
1993 {
1994     if (kLogApi) {
1995         ALOGD("nScriptForEach, con(%p), s(%p), slot(%i) ains(%p) aout(%" PRId64 ")", (RsContext)con, (void *)script, slot, ains, aout);
1996     }
1997 
1998     jint   in_len = 0;
1999     jlong *in_ptr = nullptr;
2000 
2001     RsAllocation *in_allocs = nullptr;
2002 
2003     if (ains != nullptr) {
2004         in_len = _env->GetArrayLength(ains);
2005         if (in_len > (jint)RS_KERNEL_MAX_ARGUMENTS) {
2006             ALOGE("Too many arguments in kernel launch.");
2007             // TODO (b/20758983): Report back to Java and throw an exception
2008             return;
2009         }
2010 
2011         in_ptr = _env->GetLongArrayElements(ains, nullptr);
2012         if (in_ptr == nullptr) {
2013             ALOGE("Failed to get Java array elements");
2014             return;
2015         }
2016 
2017         if (sizeof(RsAllocation) == sizeof(jlong)) {
2018             in_allocs = (RsAllocation*)in_ptr;
2019         } else {
2020             // Convert from 64-bit jlong types to the native pointer type.
2021 
2022             in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
2023             if (in_allocs == nullptr) {
2024                 ALOGE("Failed launching kernel for lack of memory.");
2025                 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2026                 return;
2027             }
2028 
2029             for (int index = in_len; --index >= 0;) {
2030                 in_allocs[index] = (RsAllocation)in_ptr[index];
2031             }
2032         }
2033     }
2034 
2035     jint   param_len = 0;
2036     jbyte *param_ptr = nullptr;
2037 
2038     if (params != nullptr) {
2039         param_len = _env->GetArrayLength(params);
2040         param_ptr = _env->GetByteArrayElements(params, nullptr);
2041         if (param_ptr == nullptr) {
2042             ALOGE("Failed to get Java array elements");
2043             return;
2044         }
2045     }
2046 
2047     RsScriptCall sc, *sca = nullptr;
2048     uint32_t sc_size = 0;
2049 
2050     jint  limit_len = 0;
2051     jint *limit_ptr = nullptr;
2052 
2053     if (limits != nullptr) {
2054         limit_len = _env->GetArrayLength(limits);
2055         limit_ptr = _env->GetIntArrayElements(limits, nullptr);
2056         if (limit_ptr == nullptr) {
2057             ALOGE("Failed to get Java array elements");
2058             return;
2059         }
2060 
2061         assert(limit_len == 6);
2062         UNUSED(limit_len);  // As the assert might not be compiled.
2063 
2064         sc.xStart     = limit_ptr[0];
2065         sc.xEnd       = limit_ptr[1];
2066         sc.yStart     = limit_ptr[2];
2067         sc.yEnd       = limit_ptr[3];
2068         sc.zStart     = limit_ptr[4];
2069         sc.zEnd       = limit_ptr[5];
2070         sc.strategy   = RS_FOR_EACH_STRATEGY_DONT_CARE;
2071         sc.arrayStart = 0;
2072         sc.arrayEnd = 0;
2073         sc.array2Start = 0;
2074         sc.array2End = 0;
2075         sc.array3Start = 0;
2076         sc.array3End = 0;
2077         sc.array4Start = 0;
2078         sc.array4End = 0;
2079 
2080         sca = &sc;
2081         // sc_size is required, but unused, by the runtime and drivers.
2082         sc_size = sizeof(sc);
2083     }
2084 
2085     rsScriptForEachMulti((RsContext)con, (RsScript)script, slot,
2086                          in_allocs, in_len, (RsAllocation)aout,
2087                          param_ptr, param_len, sca, sc_size);
2088 
2089     if (ains != nullptr) {
2090         _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2091     }
2092 
2093     if (params != nullptr) {
2094         _env->ReleaseByteArrayElements(params, param_ptr, JNI_ABORT);
2095     }
2096 
2097     if (limits != nullptr) {
2098         _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
2099     }
2100 }
2101 
2102 static void
nScriptReduce(JNIEnv * _env,jobject _this,jlong con,jlong script,jint slot,jlongArray ains,jlong aout,jintArray limits)2103 nScriptReduce(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
2104               jlongArray ains, jlong aout, jintArray limits)
2105 {
2106     if (kLogApi) {
2107         ALOGD("nScriptReduce, con(%p), s(%p), slot(%i) ains(%p) aout(%" PRId64 ")", (RsContext)con, (void *)script, slot, ains, aout);
2108     }
2109 
2110     if (ains == nullptr) {
2111         ALOGE("At least one input required.");
2112         // TODO (b/20758983): Report back to Java and throw an exception
2113         return;
2114     }
2115     jint in_len = _env->GetArrayLength(ains);
2116     if (in_len > (jint)RS_KERNEL_MAX_ARGUMENTS) {
2117         ALOGE("Too many arguments in kernel launch.");
2118         // TODO (b/20758983): Report back to Java and throw an exception
2119         return;
2120     }
2121 
2122     jlong *in_ptr = _env->GetLongArrayElements(ains, nullptr);
2123     if (in_ptr == nullptr) {
2124         ALOGE("Failed to get Java array elements");
2125         // TODO (b/20758983): Report back to Java and throw an exception
2126         return;
2127     }
2128 
2129     RsAllocation *in_allocs = nullptr;
2130     if (sizeof(RsAllocation) == sizeof(jlong)) {
2131         in_allocs = (RsAllocation*)in_ptr;
2132     } else {
2133         // Convert from 64-bit jlong types to the native pointer type.
2134 
2135         in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
2136         if (in_allocs == nullptr) {
2137             ALOGE("Failed launching kernel for lack of memory.");
2138             // TODO (b/20758983): Report back to Java and throw an exception
2139             _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2140             return;
2141         }
2142 
2143         for (int index = in_len; --index >= 0;) {
2144             in_allocs[index] = (RsAllocation)in_ptr[index];
2145         }
2146     }
2147 
2148     RsScriptCall sc, *sca = nullptr;
2149     uint32_t sc_size = 0;
2150 
2151     jint  limit_len = 0;
2152     jint *limit_ptr = nullptr;
2153 
2154     if (limits != nullptr) {
2155         limit_len = _env->GetArrayLength(limits);
2156         limit_ptr = _env->GetIntArrayElements(limits, nullptr);
2157         if (limit_ptr == nullptr) {
2158             ALOGE("Failed to get Java array elements");
2159             // TODO (b/20758983): Report back to Java and throw an exception
2160             return;
2161         }
2162 
2163         assert(limit_len == 6);
2164         UNUSED(limit_len);  // As the assert might not be compiled.
2165 
2166         sc.xStart     = limit_ptr[0];
2167         sc.xEnd       = limit_ptr[1];
2168         sc.yStart     = limit_ptr[2];
2169         sc.yEnd       = limit_ptr[3];
2170         sc.zStart     = limit_ptr[4];
2171         sc.zEnd       = limit_ptr[5];
2172         sc.strategy   = RS_FOR_EACH_STRATEGY_DONT_CARE;
2173         sc.arrayStart = 0;
2174         sc.arrayEnd = 0;
2175         sc.array2Start = 0;
2176         sc.array2End = 0;
2177         sc.array3Start = 0;
2178         sc.array3End = 0;
2179         sc.array4Start = 0;
2180         sc.array4End = 0;
2181 
2182         sca = &sc;
2183         sc_size = sizeof(sc);
2184     }
2185 
2186     rsScriptReduce((RsContext)con, (RsScript)script, slot,
2187                    in_allocs, in_len, (RsAllocation)aout,
2188                    sca, sc_size);
2189 
2190     _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2191 
2192     if (limits != nullptr) {
2193         _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
2194     }
2195 }
2196 
2197 // -----------------------------------
2198 
2199 static jlong
nScriptCCreate(JNIEnv * _env,jobject _this,jlong con,jstring resName,jstring cacheDir,jbyteArray scriptRef,jint length)2200 nScriptCCreate(JNIEnv *_env, jobject _this, jlong con,
2201                jstring resName, jstring cacheDir,
2202                jbyteArray scriptRef, jint length)
2203 {
2204     if (kLogApi) {
2205         ALOGD("nScriptCCreate, con(%p)", (RsContext)con);
2206     }
2207 
2208     AutoJavaStringToUTF8 resNameUTF(_env, resName);
2209     AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
2210     jlong ret = 0;
2211     jbyte* script_ptr = nullptr;
2212     jint _exception = 0;
2213     jint remaining;
2214     if (!scriptRef) {
2215         _exception = 1;
2216         //jniThrowException(_env, "java/lang/IllegalArgumentException", "script == null");
2217         goto exit;
2218     }
2219     if (length < 0) {
2220         _exception = 1;
2221         //jniThrowException(_env, "java/lang/IllegalArgumentException", "length < 0");
2222         goto exit;
2223     }
2224     remaining = _env->GetArrayLength(scriptRef);
2225     if (remaining < length) {
2226         _exception = 1;
2227         //jniThrowException(_env, "java/lang/IllegalArgumentException",
2228         //        "length > script.length - offset");
2229         goto exit;
2230     }
2231     script_ptr = (jbyte *)
2232         _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
2233     if (script_ptr == nullptr) {
2234         ALOGE("Failed to get Java array elements");
2235         return ret;
2236     }
2237 
2238     //rsScriptCSetText((RsContext)con, (const char *)script_ptr, length);
2239 
2240     ret = (jlong)(uintptr_t)rsScriptCCreate((RsContext)con,
2241                                 resNameUTF.c_str(), resNameUTF.length(),
2242                                 cacheDirUTF.c_str(), cacheDirUTF.length(),
2243                                 (const char *)script_ptr, length);
2244 
2245 exit:
2246     if (script_ptr) {
2247         _env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr,
2248                 _exception ? JNI_ABORT: 0);
2249     }
2250 
2251     return (jlong)(uintptr_t)ret;
2252 }
2253 
2254 static jlong
nScriptIntrinsicCreate(JNIEnv * _env,jobject _this,jlong con,jint id,jlong eid)2255 nScriptIntrinsicCreate(JNIEnv *_env, jobject _this, jlong con, jint id, jlong eid)
2256 {
2257     if (kLogApi) {
2258         ALOGD("nScriptIntrinsicCreate, con(%p) id(%i) element(%p)", (RsContext)con, id,
2259               (void *)eid);
2260     }
2261     return (jlong)(uintptr_t)rsScriptIntrinsicCreate((RsContext)con, id, (RsElement)eid);
2262 }
2263 
2264 static jlong
nScriptKernelIDCreate(JNIEnv * _env,jobject _this,jlong con,jlong sid,jint slot,jint sig)2265 nScriptKernelIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot, jint sig)
2266 {
2267     if (kLogApi) {
2268         ALOGD("nScriptKernelIDCreate, con(%p) script(%p), slot(%i), sig(%i)", (RsContext)con,
2269               (void *)sid, slot, sig);
2270     }
2271     return (jlong)(uintptr_t)rsScriptKernelIDCreate((RsContext)con, (RsScript)sid, slot, sig);
2272 }
2273 
2274 static jlong
nScriptInvokeIDCreate(JNIEnv * _env,jobject _this,jlong con,jlong sid,jint slot)2275 nScriptInvokeIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
2276 {
2277     if (kLogApi) {
2278         ALOGD("nScriptInvokeIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con,
2279               (void *)sid, slot);
2280     }
2281     return (jlong)(uintptr_t)rsScriptInvokeIDCreate((RsContext)con, (RsScript)sid, slot);
2282 }
2283 
2284 static jlong
nScriptFieldIDCreate(JNIEnv * _env,jobject _this,jlong con,jlong sid,jint slot)2285 nScriptFieldIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
2286 {
2287     if (kLogApi) {
2288         ALOGD("nScriptFieldIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con, (void *)sid,
2289               slot);
2290     }
2291     return (jlong)(uintptr_t)rsScriptFieldIDCreate((RsContext)con, (RsScript)sid, slot);
2292 }
2293 
2294 static jlong
nScriptGroupCreate(JNIEnv * _env,jobject _this,jlong con,jlongArray _kernels,jlongArray _src,jlongArray _dstk,jlongArray _dstf,jlongArray _types)2295 nScriptGroupCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _kernels, jlongArray _src,
2296     jlongArray _dstk, jlongArray _dstf, jlongArray _types)
2297 {
2298     if (kLogApi) {
2299         ALOGD("nScriptGroupCreate, con(%p)", (RsContext)con);
2300     }
2301 
2302     jlong id = 0;
2303 
2304     RsScriptKernelID* kernelsPtr;
2305     jint kernelsLen = _env->GetArrayLength(_kernels);
2306     jlong *jKernelsPtr = _env->GetLongArrayElements(_kernels, nullptr);
2307 
2308     RsScriptKernelID* srcPtr;
2309     jint srcLen = _env->GetArrayLength(_src);
2310     jlong *jSrcPtr = _env->GetLongArrayElements(_src, nullptr);
2311 
2312     RsScriptKernelID* dstkPtr;
2313     jint dstkLen = _env->GetArrayLength(_dstk);
2314     jlong *jDstkPtr = _env->GetLongArrayElements(_dstk, nullptr);
2315 
2316     RsScriptKernelID* dstfPtr;
2317     jint dstfLen = _env->GetArrayLength(_dstf);
2318     jlong *jDstfPtr = _env->GetLongArrayElements(_dstf, nullptr);
2319 
2320     RsType* typesPtr;
2321     jint typesLen = _env->GetArrayLength(_types);
2322     jlong *jTypesPtr = _env->GetLongArrayElements(_types, nullptr);
2323 
2324     if (jKernelsPtr == nullptr) {
2325         ALOGE("Failed to get Java array elements: kernels");
2326         goto cleanup;
2327     }
2328     if (jSrcPtr == nullptr) {
2329         ALOGE("Failed to get Java array elements: src");
2330         goto cleanup;
2331     }
2332     if (jDstkPtr == nullptr) {
2333         ALOGE("Failed to get Java array elements: dstk");
2334         goto cleanup;
2335     }
2336     if (jDstfPtr == nullptr) {
2337         ALOGE("Failed to get Java array elements: dstf");
2338         goto cleanup;
2339     }
2340     if (jTypesPtr == nullptr) {
2341         ALOGE("Failed to get Java array elements: types");
2342         goto cleanup;
2343     }
2344 
2345     kernelsPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * kernelsLen);
2346     for(int i = 0; i < kernelsLen; ++i) {
2347         kernelsPtr[i] = (RsScriptKernelID)jKernelsPtr[i];
2348     }
2349 
2350     srcPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * srcLen);
2351     for(int i = 0; i < srcLen; ++i) {
2352         srcPtr[i] = (RsScriptKernelID)jSrcPtr[i];
2353     }
2354 
2355     dstkPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstkLen);
2356     for(int i = 0; i < dstkLen; ++i) {
2357         dstkPtr[i] = (RsScriptKernelID)jDstkPtr[i];
2358     }
2359 
2360     dstfPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstfLen);
2361     for(int i = 0; i < dstfLen; ++i) {
2362         dstfPtr[i] = (RsScriptKernelID)jDstfPtr[i];
2363     }
2364 
2365     typesPtr = (RsType*) malloc(sizeof(RsType) * typesLen);
2366     for(int i = 0; i < typesLen; ++i) {
2367         typesPtr[i] = (RsType)jTypesPtr[i];
2368     }
2369 
2370     id = (jlong)(uintptr_t)rsScriptGroupCreate((RsContext)con,
2371                                (RsScriptKernelID *)kernelsPtr, kernelsLen * sizeof(RsScriptKernelID),
2372                                (RsScriptKernelID *)srcPtr, srcLen * sizeof(RsScriptKernelID),
2373                                (RsScriptKernelID *)dstkPtr, dstkLen * sizeof(RsScriptKernelID),
2374                                (RsScriptFieldID *)dstfPtr, dstfLen * sizeof(RsScriptKernelID),
2375                                (RsType *)typesPtr, typesLen * sizeof(RsType));
2376 
2377     free(kernelsPtr);
2378     free(srcPtr);
2379     free(dstkPtr);
2380     free(dstfPtr);
2381     free(typesPtr);
2382 
2383 cleanup:
2384     if (jKernelsPtr != nullptr) {
2385         _env->ReleaseLongArrayElements(_kernels, jKernelsPtr, 0);
2386     }
2387     if (jSrcPtr != nullptr) {
2388         _env->ReleaseLongArrayElements(_src, jSrcPtr, 0);
2389     }
2390     if (jDstkPtr != nullptr) {
2391         _env->ReleaseLongArrayElements(_dstk, jDstkPtr, 0);
2392     }
2393     if (jDstfPtr != nullptr) {
2394         _env->ReleaseLongArrayElements(_dstf, jDstfPtr, 0);
2395     }
2396     if (jTypesPtr != nullptr) {
2397         _env->ReleaseLongArrayElements(_types, jTypesPtr, 0);
2398     }
2399 
2400     return id;
2401 }
2402 
2403 static void
nScriptGroupSetInput(JNIEnv * _env,jobject _this,jlong con,jlong gid,jlong kid,jlong alloc)2404 nScriptGroupSetInput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
2405 {
2406     if (kLogApi) {
2407         ALOGD("nScriptGroupSetInput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2408               (void *)gid, (void *)kid, (void *)alloc);
2409     }
2410     rsScriptGroupSetInput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
2411 }
2412 
2413 static void
nScriptGroupSetOutput(JNIEnv * _env,jobject _this,jlong con,jlong gid,jlong kid,jlong alloc)2414 nScriptGroupSetOutput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
2415 {
2416     if (kLogApi) {
2417         ALOGD("nScriptGroupSetOutput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2418               (void *)gid, (void *)kid, (void *)alloc);
2419     }
2420     rsScriptGroupSetOutput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
2421 }
2422 
2423 static void
nScriptGroupExecute(JNIEnv * _env,jobject _this,jlong con,jlong gid)2424 nScriptGroupExecute(JNIEnv *_env, jobject _this, jlong con, jlong gid)
2425 {
2426     if (kLogApi) {
2427         ALOGD("nScriptGroupSetOutput, con(%p) group(%p)", (RsContext)con, (void *)gid);
2428     }
2429     rsScriptGroupExecute((RsContext)con, (RsScriptGroup)gid);
2430 }
2431 
2432 // ---------------------------------------------------------------------------
2433 
2434 static jlong
nProgramStoreCreate(JNIEnv * _env,jobject _this,jlong con,jboolean colorMaskR,jboolean colorMaskG,jboolean colorMaskB,jboolean colorMaskA,jboolean depthMask,jboolean ditherEnable,jint srcFunc,jint destFunc,jint depthFunc)2435 nProgramStoreCreate(JNIEnv *_env, jobject _this, jlong con,
2436                     jboolean colorMaskR, jboolean colorMaskG, jboolean colorMaskB, jboolean colorMaskA,
2437                     jboolean depthMask, jboolean ditherEnable,
2438                     jint srcFunc, jint destFunc,
2439                     jint depthFunc)
2440 {
2441     if (kLogApi) {
2442         ALOGD("nProgramStoreCreate, con(%p)", (RsContext)con);
2443     }
2444     return (jlong)(uintptr_t)rsProgramStoreCreate((RsContext)con, colorMaskR, colorMaskG, colorMaskB, colorMaskA,
2445                                       depthMask, ditherEnable, (RsBlendSrcFunc)srcFunc,
2446                                       (RsBlendDstFunc)destFunc, (RsDepthFunc)depthFunc);
2447 }
2448 
2449 // ---------------------------------------------------------------------------
2450 
2451 static void
nProgramBindConstants(JNIEnv * _env,jobject _this,jlong con,jlong vpv,jint slot,jlong a)2452 nProgramBindConstants(JNIEnv *_env, jobject _this, jlong con, jlong vpv, jint slot, jlong a)
2453 {
2454     if (kLogApi) {
2455         ALOGD("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", (RsContext)con,
2456               (RsProgramVertex)vpv, slot, (RsAllocation)a);
2457     }
2458     rsProgramBindConstants((RsContext)con, (RsProgram)vpv, slot, (RsAllocation)a);
2459 }
2460 
2461 static void
nProgramBindTexture(JNIEnv * _env,jobject _this,jlong con,jlong vpf,jint slot,jlong a)2462 nProgramBindTexture(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
2463 {
2464     if (kLogApi) {
2465         ALOGD("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2466               (RsProgramFragment)vpf, slot, (RsAllocation)a);
2467     }
2468     rsProgramBindTexture((RsContext)con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
2469 }
2470 
2471 static void
nProgramBindSampler(JNIEnv * _env,jobject _this,jlong con,jlong vpf,jint slot,jlong a)2472 nProgramBindSampler(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
2473 {
2474     if (kLogApi) {
2475         ALOGD("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2476               (RsProgramFragment)vpf, slot, (RsSampler)a);
2477     }
2478     rsProgramBindSampler((RsContext)con, (RsProgramFragment)vpf, slot, (RsSampler)a);
2479 }
2480 
2481 // ---------------------------------------------------------------------------
2482 
2483 static jlong
nProgramFragmentCreate(JNIEnv * _env,jobject _this,jlong con,jstring shader,jobjectArray texNames,jlongArray params)2484 nProgramFragmentCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
2485                        jobjectArray texNames, jlongArray params)
2486 {
2487     AutoJavaStringToUTF8 shaderUTF(_env, shader);
2488     jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
2489     jint paramLen = _env->GetArrayLength(params);
2490     if (jParamPtr == nullptr) {
2491         ALOGE("Failed to get Java array elements");
2492         return 0;
2493     }
2494 
2495     int texCount = _env->GetArrayLength(texNames);
2496     AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2497     const char ** nameArray = names.c_str();
2498     size_t* sizeArray = names.c_str_len();
2499 
2500     if (kLogApi) {
2501         ALOGD("nProgramFragmentCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2502     }
2503 
2504     uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2505     for(int i = 0; i < paramLen; ++i) {
2506         paramPtr[i] = (uintptr_t)jParamPtr[i];
2507     }
2508     jlong ret = (jlong)(uintptr_t)rsProgramFragmentCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
2509                                              nameArray, texCount, sizeArray,
2510                                              paramPtr, paramLen);
2511 
2512     free(paramPtr);
2513     _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
2514     return ret;
2515 }
2516 
2517 
2518 // ---------------------------------------------------------------------------
2519 
2520 static jlong
nProgramVertexCreate(JNIEnv * _env,jobject _this,jlong con,jstring shader,jobjectArray texNames,jlongArray params)2521 nProgramVertexCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
2522                      jobjectArray texNames, jlongArray params)
2523 {
2524     AutoJavaStringToUTF8 shaderUTF(_env, shader);
2525     jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
2526     jint paramLen = _env->GetArrayLength(params);
2527     if (jParamPtr == nullptr) {
2528         ALOGE("Failed to get Java array elements");
2529         return 0;
2530     }
2531 
2532     if (kLogApi) {
2533         ALOGD("nProgramVertexCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2534     }
2535 
2536     int texCount = _env->GetArrayLength(texNames);
2537     AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2538     const char ** nameArray = names.c_str();
2539     size_t* sizeArray = names.c_str_len();
2540 
2541     uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2542     for(int i = 0; i < paramLen; ++i) {
2543         paramPtr[i] = (uintptr_t)jParamPtr[i];
2544     }
2545 
2546     jlong ret = (jlong)(uintptr_t)rsProgramVertexCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
2547                                            nameArray, texCount, sizeArray,
2548                                            paramPtr, paramLen);
2549 
2550     free(paramPtr);
2551     _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
2552     return ret;
2553 }
2554 
2555 // ---------------------------------------------------------------------------
2556 
2557 static jlong
nProgramRasterCreate(JNIEnv * _env,jobject _this,jlong con,jboolean pointSprite,jint cull)2558 nProgramRasterCreate(JNIEnv *_env, jobject _this, jlong con, jboolean pointSprite, jint cull)
2559 {
2560     if (kLogApi) {
2561         ALOGD("nProgramRasterCreate, con(%p), pointSprite(%i), cull(%i)", (RsContext)con,
2562               pointSprite, cull);
2563     }
2564     return (jlong)(uintptr_t)rsProgramRasterCreate((RsContext)con, pointSprite, (RsCullMode)cull);
2565 }
2566 
2567 
2568 // ---------------------------------------------------------------------------
2569 
2570 static void
nContextBindRootScript(JNIEnv * _env,jobject _this,jlong con,jlong script)2571 nContextBindRootScript(JNIEnv *_env, jobject _this, jlong con, jlong script)
2572 {
2573     if (kLogApi) {
2574         ALOGD("nContextBindRootScript, con(%p), script(%p)", (RsContext)con, (RsScript)script);
2575     }
2576     rsContextBindRootScript((RsContext)con, (RsScript)script);
2577 }
2578 
2579 static void
nContextBindProgramStore(JNIEnv * _env,jobject _this,jlong con,jlong pfs)2580 nContextBindProgramStore(JNIEnv *_env, jobject _this, jlong con, jlong pfs)
2581 {
2582     if (kLogApi) {
2583         ALOGD("nContextBindProgramStore, con(%p), pfs(%p)", (RsContext)con, (RsProgramStore)pfs);
2584     }
2585     rsContextBindProgramStore((RsContext)con, (RsProgramStore)pfs);
2586 }
2587 
2588 static void
nContextBindProgramFragment(JNIEnv * _env,jobject _this,jlong con,jlong pf)2589 nContextBindProgramFragment(JNIEnv *_env, jobject _this, jlong con, jlong pf)
2590 {
2591     if (kLogApi) {
2592         ALOGD("nContextBindProgramFragment, con(%p), pf(%p)", (RsContext)con,
2593               (RsProgramFragment)pf);
2594     }
2595     rsContextBindProgramFragment((RsContext)con, (RsProgramFragment)pf);
2596 }
2597 
2598 static void
nContextBindProgramVertex(JNIEnv * _env,jobject _this,jlong con,jlong pf)2599 nContextBindProgramVertex(JNIEnv *_env, jobject _this, jlong con, jlong pf)
2600 {
2601     if (kLogApi) {
2602         ALOGD("nContextBindProgramVertex, con(%p), pf(%p)", (RsContext)con, (RsProgramVertex)pf);
2603     }
2604     rsContextBindProgramVertex((RsContext)con, (RsProgramVertex)pf);
2605 }
2606 
2607 static void
nContextBindProgramRaster(JNIEnv * _env,jobject _this,jlong con,jlong pf)2608 nContextBindProgramRaster(JNIEnv *_env, jobject _this, jlong con, jlong pf)
2609 {
2610     if (kLogApi) {
2611         ALOGD("nContextBindProgramRaster, con(%p), pf(%p)", (RsContext)con, (RsProgramRaster)pf);
2612     }
2613     rsContextBindProgramRaster((RsContext)con, (RsProgramRaster)pf);
2614 }
2615 
2616 
2617 // ---------------------------------------------------------------------------
2618 
2619 static jlong
nSamplerCreate(JNIEnv * _env,jobject _this,jlong con,jint magFilter,jint minFilter,jint wrapS,jint wrapT,jint wrapR,jfloat aniso)2620 nSamplerCreate(JNIEnv *_env, jobject _this, jlong con, jint magFilter, jint minFilter,
2621                jint wrapS, jint wrapT, jint wrapR, jfloat aniso)
2622 {
2623     if (kLogApi) {
2624         ALOGD("nSamplerCreate, con(%p)", (RsContext)con);
2625     }
2626     return (jlong)(uintptr_t)rsSamplerCreate((RsContext)con,
2627                                  (RsSamplerValue)magFilter,
2628                                  (RsSamplerValue)minFilter,
2629                                  (RsSamplerValue)wrapS,
2630                                  (RsSamplerValue)wrapT,
2631                                  (RsSamplerValue)wrapR,
2632                                  aniso);
2633 }
2634 
2635 // ---------------------------------------------------------------------------
2636 
2637 static jlong
nMeshCreate(JNIEnv * _env,jobject _this,jlong con,jlongArray _vtx,jlongArray _idx,jintArray _prim)2638 nMeshCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _vtx, jlongArray _idx, jintArray _prim)
2639 {
2640     if (kLogApi) {
2641         ALOGD("nMeshCreate, con(%p)", (RsContext)con);
2642     }
2643 
2644     jlong id = 0;
2645 
2646     RsAllocation* vtxPtr;
2647     jint vtxLen = _env->GetArrayLength(_vtx);
2648     jlong *jVtxPtr = _env->GetLongArrayElements(_vtx, nullptr);
2649 
2650     RsAllocation* idxPtr;
2651     jint idxLen = _env->GetArrayLength(_idx);
2652     jlong *jIdxPtr = _env->GetLongArrayElements(_idx, nullptr);
2653 
2654     jint primLen = _env->GetArrayLength(_prim);
2655     jint *primPtr = _env->GetIntArrayElements(_prim, nullptr);
2656 
2657     if (jVtxPtr == nullptr) {
2658         ALOGE("Failed to get Java array elements: vtx");
2659         goto cleanupMesh;
2660     }
2661     if (jIdxPtr == nullptr) {
2662         ALOGE("Failed to get Java array elements: idx");
2663         goto cleanupMesh;
2664     }
2665     if (primPtr == nullptr) {
2666         ALOGE("Failed to get Java array elements: prim");
2667         goto cleanupMesh;
2668     }
2669 
2670     vtxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * vtxLen);
2671     for(int i = 0; i < vtxLen; ++i) {
2672         vtxPtr[i] = (RsAllocation)(uintptr_t)jVtxPtr[i];
2673     }
2674 
2675     idxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * idxLen);
2676     for(int i = 0; i < idxLen; ++i) {
2677         idxPtr[i] = (RsAllocation)(uintptr_t)jIdxPtr[i];
2678     }
2679 
2680     id = (jlong)(uintptr_t)rsMeshCreate((RsContext)con,
2681                                         (RsAllocation *)vtxPtr, vtxLen,
2682                                         (RsAllocation *)idxPtr, idxLen,
2683                                         (uint32_t *)primPtr, primLen);
2684 
2685     free(vtxPtr);
2686     free(idxPtr);
2687 
2688 cleanupMesh:
2689     if (jVtxPtr != nullptr) {
2690         _env->ReleaseLongArrayElements(_vtx, jVtxPtr, 0);
2691     }
2692     if (jIdxPtr != nullptr) {
2693         _env->ReleaseLongArrayElements(_idx, jIdxPtr, 0);
2694     }
2695     if (primPtr != nullptr) {
2696         _env->ReleaseIntArrayElements(_prim, primPtr, 0);
2697     }
2698 
2699     return id;
2700 }
2701 
2702 static jint
nMeshGetVertexBufferCount(JNIEnv * _env,jobject _this,jlong con,jlong mesh)2703 nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
2704 {
2705     if (kLogApi) {
2706         ALOGD("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2707     }
2708     jint vtxCount = 0;
2709     rsaMeshGetVertexBufferCount((RsContext)con, (RsMesh)mesh, &vtxCount);
2710     return vtxCount;
2711 }
2712 
2713 static jint
nMeshGetIndexCount(JNIEnv * _env,jobject _this,jlong con,jlong mesh)2714 nMeshGetIndexCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
2715 {
2716     if (kLogApi) {
2717         ALOGD("nMeshGetIndexCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2718     }
2719     jint idxCount = 0;
2720     rsaMeshGetIndexCount((RsContext)con, (RsMesh)mesh, &idxCount);
2721     return idxCount;
2722 }
2723 
2724 static void
nMeshGetVertices(JNIEnv * _env,jobject _this,jlong con,jlong mesh,jlongArray _ids,jint numVtxIDs)2725 nMeshGetVertices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _ids, jint numVtxIDs)
2726 {
2727     if (kLogApi) {
2728         ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2729     }
2730 
2731     RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
2732     rsaMeshGetVertices((RsContext)con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
2733 
2734     for(jint i = 0; i < numVtxIDs; i ++) {
2735         const jlong alloc = (jlong)(uintptr_t)allocs[i];
2736         _env->SetLongArrayRegion(_ids, i, 1, &alloc);
2737     }
2738 
2739     free(allocs);
2740 }
2741 
2742 static void
nMeshGetIndices(JNIEnv * _env,jobject _this,jlong con,jlong mesh,jlongArray _idxIds,jintArray _primitives,jint numIndices)2743 nMeshGetIndices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _idxIds, jintArray _primitives, jint numIndices)
2744 {
2745     if (kLogApi) {
2746         ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2747     }
2748 
2749     RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
2750     uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
2751 
2752     rsaMeshGetIndices((RsContext)con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
2753 
2754     for(jint i = 0; i < numIndices; i ++) {
2755         const jlong alloc = (jlong)(uintptr_t)allocs[i];
2756         const jint prim = (jint)prims[i];
2757         _env->SetLongArrayRegion(_idxIds, i, 1, &alloc);
2758         _env->SetIntArrayRegion(_primitives, i, 1, &prim);
2759     }
2760 
2761     free(allocs);
2762     free(prims);
2763 }
2764 
2765 static jint
nSystemGetPointerSize(JNIEnv * _env,jobject _this)2766 nSystemGetPointerSize(JNIEnv *_env, jobject _this) {
2767     return (jint)sizeof(void*);
2768 }
2769 
2770 static jobject
nAllocationGetByteBuffer(JNIEnv * _env,jobject _this,jlong con,jlong alloc,jlongArray strideArr,jint xBytesSize,jint dimY,jint dimZ)2771 nAllocationGetByteBuffer(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
2772                         jlongArray strideArr, jint xBytesSize,
2773                         jint dimY, jint dimZ) {
2774     if (kLogApi) {
2775         ALOGD("nAllocationGetByteBuffer, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
2776     }
2777 
2778     jlong *jStridePtr = _env->GetLongArrayElements(strideArr, nullptr);
2779     if (jStridePtr == nullptr) {
2780         ALOGE("Failed to get Java array elements: strideArr");
2781         return 0;
2782     }
2783 
2784     size_t strideIn = xBytesSize;
2785     void* ptr = nullptr;
2786     if (alloc != 0) {
2787         ptr = rsAllocationGetPointer((RsContext)con, (RsAllocation)alloc, 0,
2788                                      RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, 0, 0,
2789                                      &strideIn, sizeof(size_t));
2790     }
2791 
2792     jobject byteBuffer = nullptr;
2793     if (ptr != nullptr) {
2794         size_t bufferSize = strideIn;
2795         jStridePtr[0] = strideIn;
2796         if (dimY > 0) {
2797             bufferSize *= dimY;
2798         }
2799         if (dimZ > 0) {
2800             bufferSize *= dimZ;
2801         }
2802         byteBuffer = _env->NewDirectByteBuffer(ptr, (jlong) bufferSize);
2803     }
2804     _env->ReleaseLongArrayElements(strideArr, jStridePtr, 0);
2805     return byteBuffer;
2806 }
2807 // ---------------------------------------------------------------------------
2808 
2809 
2810 static const char *classPathName = "android/renderscript/RenderScript";
2811 
2812 static const JNINativeMethod methods[] = {
2813 {"_nInit",                         "()V",                                     (void*)_nInit },
2814 
2815 {"nDeviceCreate",                  "()J",                                     (void*)nDeviceCreate },
2816 {"nDeviceDestroy",                 "(J)V",                                    (void*)nDeviceDestroy },
2817 {"nDeviceSetConfig",               "(JII)V",                                  (void*)nDeviceSetConfig },
2818 {"nContextGetUserMessage",         "(J[I)I",                                  (void*)nContextGetUserMessage },
2819 {"nContextGetErrorMessage",        "(J)Ljava/lang/String;",                   (void*)nContextGetErrorMessage },
2820 {"nContextPeekMessage",            "(J[I)I",                                  (void*)nContextPeekMessage },
2821 
2822 {"nContextInitToClient",           "(J)V",                                    (void*)nContextInitToClient },
2823 {"nContextDeinitToClient",         "(J)V",                                    (void*)nContextDeinitToClient },
2824 
2825 
2826 // All methods below are thread protected in java.
2827 {"rsnContextCreate",                 "(JIII)J",                               (void*)nContextCreate },
2828 {"rsnContextCreateGL",               "(JIIIIIIIIIIIIFI)J",                    (void*)nContextCreateGL },
2829 {"rsnContextFinish",                 "(J)V",                                  (void*)nContextFinish },
2830 {"rsnContextSetPriority",            "(JI)V",                                 (void*)nContextSetPriority },
2831 {"rsnContextSetCacheDir",            "(JLjava/lang/String;)V",                (void*)nContextSetCacheDir },
2832 {"rsnContextSetSurface",             "(JIILandroid/view/Surface;)V",          (void*)nContextSetSurface },
2833 {"rsnContextDestroy",                "(J)V",                                  (void*)nContextDestroy },
2834 {"rsnContextDump",                   "(JI)V",                                 (void*)nContextDump },
2835 {"rsnContextPause",                  "(J)V",                                  (void*)nContextPause },
2836 {"rsnContextResume",                 "(J)V",                                  (void*)nContextResume },
2837 {"rsnContextSendMessage",            "(JI[I)V",                               (void*)nContextSendMessage },
2838 {"rsnClosureCreate",                 "(JJJ[J[J[I[J[J)J",                      (void*)nClosureCreate },
2839 {"rsnInvokeClosureCreate",           "(JJ[B[J[J[I)J",                         (void*)nInvokeClosureCreate },
2840 {"rsnClosureSetArg",                 "(JJIJI)V",                              (void*)nClosureSetArg },
2841 {"rsnClosureSetGlobal",              "(JJJJI)V",                              (void*)nClosureSetGlobal },
2842 {"rsnAssignName",                    "(JJ[B)V",                               (void*)nAssignName },
2843 {"rsnGetName",                       "(JJ)Ljava/lang/String;",                (void*)nGetName },
2844 {"rsnObjDestroy",                    "(JJ)V",                                 (void*)nObjDestroy },
2845 
2846 {"rsnFileA3DCreateFromFile",         "(JLjava/lang/String;)J",                (void*)nFileA3DCreateFromFile },
2847 {"rsnFileA3DCreateFromAssetStream",  "(JJ)J",                                 (void*)nFileA3DCreateFromAssetStream },
2848 {"rsnFileA3DCreateFromAsset",        "(JLandroid/content/res/AssetManager;Ljava/lang/String;)J",            (void*)nFileA3DCreateFromAsset },
2849 {"rsnFileA3DGetNumIndexEntries",     "(JJ)I",                                 (void*)nFileA3DGetNumIndexEntries },
2850 {"rsnFileA3DGetIndexEntries",        "(JJI[I[Ljava/lang/String;)V",           (void*)nFileA3DGetIndexEntries },
2851 {"rsnFileA3DGetEntryByIndex",        "(JJI)J",                                (void*)nFileA3DGetEntryByIndex },
2852 
2853 {"rsnFontCreateFromFile",            "(JLjava/lang/String;FI)J",              (void*)nFontCreateFromFile },
2854 {"rsnFontCreateFromAssetStream",     "(JLjava/lang/String;FIJ)J",             (void*)nFontCreateFromAssetStream },
2855 {"rsnFontCreateFromAsset",        "(JLandroid/content/res/AssetManager;Ljava/lang/String;FI)J",            (void*)nFontCreateFromAsset },
2856 
2857 {"rsnElementCreate",                 "(JJIZI)J",                              (void*)nElementCreate },
2858 {"rsnElementCreate2",                "(J[J[Ljava/lang/String;[I)J",           (void*)nElementCreate2 },
2859 {"rsnElementGetNativeData",          "(JJ[I)V",                               (void*)nElementGetNativeData },
2860 {"rsnElementGetSubElements",         "(JJ[J[Ljava/lang/String;[I)V",          (void*)nElementGetSubElements },
2861 
2862 {"rsnTypeCreate",                    "(JJIIIZZI)J",                           (void*)nTypeCreate },
2863 {"rsnTypeGetNativeData",             "(JJ[J)V",                               (void*)nTypeGetNativeData },
2864 
2865 {"rsnAllocationCreateTyped",         "(JJIIJ)J",                              (void*)nAllocationCreateTyped },
2866 {"rsnAllocationCreateFromBitmap",    "(JJILandroid/graphics/Bitmap;I)J",      (void*)nAllocationCreateFromBitmap },
2867 {"rsnAllocationCreateBitmapBackedAllocation",    "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateBitmapBackedAllocation },
2868 {"rsnAllocationCubeCreateFromBitmap","(JJILandroid/graphics/Bitmap;I)J",      (void*)nAllocationCubeCreateFromBitmap },
2869 
2870 {"rsnAllocationCopyFromBitmap",      "(JJLandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyFromBitmap },
2871 {"rsnAllocationCopyToBitmap",        "(JJLandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyToBitmap },
2872 
2873 {"rsnAllocationSyncAll",             "(JJI)V",                                (void*)nAllocationSyncAll },
2874 {"rsnAllocationSetupBufferQueue",    "(JJI)V",                                (void*)nAllocationSetupBufferQueue },
2875 {"rsnAllocationShareBufferQueue",    "(JJJ)V",                                (void*)nAllocationShareBufferQueue },
2876 {"rsnAllocationGetSurface",          "(JJ)Landroid/view/Surface;",            (void*)nAllocationGetSurface },
2877 {"rsnAllocationSetSurface",          "(JJLandroid/view/Surface;)V",           (void*)nAllocationSetSurface },
2878 {"rsnAllocationIoSend",              "(JJ)V",                                 (void*)nAllocationIoSend },
2879 {"rsnAllocationIoReceive",           "(JJ)J",                                 (void*)nAllocationIoReceive },
2880 {"rsnAllocationData1D",              "(JJIIILjava/lang/Object;IIIZ)V",        (void*)nAllocationData1D },
2881 {"rsnAllocationElementData",         "(JJIIIII[BI)V",                         (void*)nAllocationElementData },
2882 {"rsnAllocationData2D",              "(JJIIIIIILjava/lang/Object;IIIZ)V",     (void*)nAllocationData2D },
2883 {"rsnAllocationData2D",              "(JJIIIIIIJIIII)V",                      (void*)nAllocationData2D_alloc },
2884 {"rsnAllocationData3D",              "(JJIIIIIIILjava/lang/Object;IIIZ)V",    (void*)nAllocationData3D },
2885 {"rsnAllocationData3D",              "(JJIIIIIIIJIIII)V",                     (void*)nAllocationData3D_alloc },
2886 {"rsnAllocationRead",                "(JJLjava/lang/Object;IIZ)V",            (void*)nAllocationRead },
2887 {"rsnAllocationRead1D",              "(JJIIILjava/lang/Object;IIIZ)V",        (void*)nAllocationRead1D },
2888 {"rsnAllocationElementRead",         "(JJIIIII[BI)V",                         (void*)nAllocationElementRead },
2889 {"rsnAllocationRead2D",              "(JJIIIIIILjava/lang/Object;IIIZ)V",     (void*)nAllocationRead2D },
2890 {"rsnAllocationRead3D",              "(JJIIIIIIILjava/lang/Object;IIIZ)V",    (void*)nAllocationRead3D },
2891 {"rsnAllocationGetType",             "(JJ)J",                                 (void*)nAllocationGetType},
2892 {"rsnAllocationResize1D",            "(JJI)V",                                (void*)nAllocationResize1D },
2893 {"rsnAllocationGenerateMipmaps",     "(JJ)V",                                 (void*)nAllocationGenerateMipmaps },
2894 
2895 {"rsnAllocationAdapterCreate",       "(JJJ)J",                                (void*)nAllocationAdapterCreate },
2896 {"rsnAllocationAdapterOffset",       "(JJIIIIIIIII)V",                        (void*)nAllocationAdapterOffset },
2897 
2898 {"rsnScriptBindAllocation",          "(JJJI)V",                               (void*)nScriptBindAllocation },
2899 {"rsnScriptSetTimeZone",             "(JJ[B)V",                               (void*)nScriptSetTimeZone },
2900 {"rsnScriptInvoke",                  "(JJI)V",                                (void*)nScriptInvoke },
2901 {"rsnScriptInvokeV",                 "(JJI[B)V",                              (void*)nScriptInvokeV },
2902 
2903 {"rsnScriptForEach",                 "(JJI[JJ[B[I)V",                         (void*)nScriptForEach },
2904 {"rsnScriptReduce",                  "(JJI[JJ[I)V",                           (void*)nScriptReduce },
2905 
2906 {"rsnScriptSetVarI",                 "(JJII)V",                               (void*)nScriptSetVarI },
2907 {"rsnScriptGetVarI",                 "(JJI)I",                                (void*)nScriptGetVarI },
2908 {"rsnScriptSetVarJ",                 "(JJIJ)V",                               (void*)nScriptSetVarJ },
2909 {"rsnScriptGetVarJ",                 "(JJI)J",                                (void*)nScriptGetVarJ },
2910 {"rsnScriptSetVarF",                 "(JJIF)V",                               (void*)nScriptSetVarF },
2911 {"rsnScriptGetVarF",                 "(JJI)F",                                (void*)nScriptGetVarF },
2912 {"rsnScriptSetVarD",                 "(JJID)V",                               (void*)nScriptSetVarD },
2913 {"rsnScriptGetVarD",                 "(JJI)D",                                (void*)nScriptGetVarD },
2914 {"rsnScriptSetVarV",                 "(JJI[B)V",                              (void*)nScriptSetVarV },
2915 {"rsnScriptGetVarV",                 "(JJI[B)V",                              (void*)nScriptGetVarV },
2916 {"rsnScriptSetVarVE",                "(JJI[BJ[I)V",                           (void*)nScriptSetVarVE },
2917 {"rsnScriptSetVarObj",               "(JJIJ)V",                               (void*)nScriptSetVarObj },
2918 
2919 {"rsnScriptCCreate",                 "(JLjava/lang/String;Ljava/lang/String;[BI)J",  (void*)nScriptCCreate },
2920 {"rsnScriptIntrinsicCreate",         "(JIJ)J",                                (void*)nScriptIntrinsicCreate },
2921 {"rsnScriptKernelIDCreate",          "(JJII)J",                               (void*)nScriptKernelIDCreate },
2922 {"rsnScriptInvokeIDCreate",          "(JJI)J",                                (void*)nScriptInvokeIDCreate },
2923 {"rsnScriptFieldIDCreate",           "(JJI)J",                                (void*)nScriptFieldIDCreate },
2924 {"rsnScriptGroupCreate",             "(J[J[J[J[J[J)J",                        (void*)nScriptGroupCreate },
2925 {"rsnScriptGroup2Create",            "(JLjava/lang/String;Ljava/lang/String;[J)J", (void*)nScriptGroup2Create },
2926 {"rsnScriptGroupSetInput",           "(JJJJ)V",                               (void*)nScriptGroupSetInput },
2927 {"rsnScriptGroupSetOutput",          "(JJJJ)V",                               (void*)nScriptGroupSetOutput },
2928 {"rsnScriptGroupExecute",            "(JJ)V",                                 (void*)nScriptGroupExecute },
2929 {"rsnScriptGroup2Execute",           "(JJ)V",                                 (void*)nScriptGroup2Execute },
2930 
2931 {"rsnScriptIntrinsicBLAS_Single",    "(JJIIIIIIIIIFJJFJIIII)V",               (void*)nScriptIntrinsicBLAS_Single },
2932 {"rsnScriptIntrinsicBLAS_Double",    "(JJIIIIIIIIIDJJDJIIII)V",               (void*)nScriptIntrinsicBLAS_Double },
2933 {"rsnScriptIntrinsicBLAS_Complex",   "(JJIIIIIIIIIFFJJFFJIIII)V",             (void*)nScriptIntrinsicBLAS_Complex },
2934 {"rsnScriptIntrinsicBLAS_Z",         "(JJIIIIIIIIIDDJJDDJIIII)V",             (void*)nScriptIntrinsicBLAS_Z },
2935 
2936 {"rsnScriptIntrinsicBLAS_BNNM",      "(JJIIIJIJIJII)V",                       (void*)nScriptIntrinsicBLAS_BNNM },
2937 
2938 {"rsnProgramStoreCreate",            "(JZZZZZZIII)J",                         (void*)nProgramStoreCreate },
2939 
2940 {"rsnProgramBindConstants",          "(JJIJ)V",                               (void*)nProgramBindConstants },
2941 {"rsnProgramBindTexture",            "(JJIJ)V",                               (void*)nProgramBindTexture },
2942 {"rsnProgramBindSampler",            "(JJIJ)V",                               (void*)nProgramBindSampler },
2943 
2944 {"rsnProgramFragmentCreate",         "(JLjava/lang/String;[Ljava/lang/String;[J)J",              (void*)nProgramFragmentCreate },
2945 {"rsnProgramRasterCreate",           "(JZI)J",                                (void*)nProgramRasterCreate },
2946 {"rsnProgramVertexCreate",           "(JLjava/lang/String;[Ljava/lang/String;[J)J",              (void*)nProgramVertexCreate },
2947 
2948 {"rsnContextBindRootScript",         "(JJ)V",                                 (void*)nContextBindRootScript },
2949 {"rsnContextBindProgramStore",       "(JJ)V",                                 (void*)nContextBindProgramStore },
2950 {"rsnContextBindProgramFragment",    "(JJ)V",                                 (void*)nContextBindProgramFragment },
2951 {"rsnContextBindProgramVertex",      "(JJ)V",                                 (void*)nContextBindProgramVertex },
2952 {"rsnContextBindProgramRaster",      "(JJ)V",                                 (void*)nContextBindProgramRaster },
2953 
2954 {"rsnSamplerCreate",                 "(JIIIIIF)J",                            (void*)nSamplerCreate },
2955 
2956 {"rsnMeshCreate",                    "(J[J[J[I)J",                            (void*)nMeshCreate },
2957 
2958 {"rsnMeshGetVertexBufferCount",      "(JJ)I",                                 (void*)nMeshGetVertexBufferCount },
2959 {"rsnMeshGetIndexCount",             "(JJ)I",                                 (void*)nMeshGetIndexCount },
2960 {"rsnMeshGetVertices",               "(JJ[JI)V",                              (void*)nMeshGetVertices },
2961 {"rsnMeshGetIndices",                "(JJ[J[II)V",                            (void*)nMeshGetIndices },
2962 
2963 {"rsnSystemGetPointerSize",          "()I",                                   (void*)nSystemGetPointerSize },
2964 {"rsnAllocationGetByteBuffer",       "(JJ[JIII)Ljava/nio/ByteBuffer;",        (void*)nAllocationGetByteBuffer },
2965 };
2966 
registerFuncs(JNIEnv * _env)2967 static int registerFuncs(JNIEnv *_env)
2968 {
2969     return android::AndroidRuntime::registerNativeMethods(
2970             _env, classPathName, methods, NELEM(methods));
2971 }
2972 
2973 // ---------------------------------------------------------------------------
2974 
JNI_OnLoad(JavaVM * vm,void * reserved)2975 jint JNI_OnLoad(JavaVM* vm, void* reserved)
2976 {
2977     JNIEnv* env = nullptr;
2978     jint result = -1;
2979 
2980     if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
2981         ALOGE("ERROR: GetEnv failed\n");
2982         goto bail;
2983     }
2984     assert(env != nullptr);
2985 
2986     if (registerFuncs(env) < 0) {
2987         ALOGE("ERROR: Renderscript native registration failed\n");
2988         goto bail;
2989     }
2990 
2991     /* success -- return valid version number */
2992     result = JNI_VERSION_1_4;
2993 
2994 bail:
2995     return result;
2996 }
2997