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