blob: 5ae895dbbce6db973b86051800cbdf77acb0d8b0 [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
Stephen Hines4cbe25a2012-01-18 18:46:27 -08002 * Copyright (C) 2011-2012 The Android Open Source Project
Jason Samsd19f10d2009-05-22 14:03:28 -07003 *
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
Jason Samsd1516df2015-05-05 18:00:34 -070017#define LOG_TAG "RenderScript_jni"
Jason Samsf29ca502009-06-23 12:22:47 -070018
Jason Samsd19f10d2009-05-22 14:03:28 -070019#include <stdlib.h>
20#include <stdio.h>
21#include <fcntl.h>
22#include <unistd.h>
23#include <math.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070024#include <utils/misc.h>
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +010025#include <inttypes.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070026
Adam Lesinskibebfcc42018-02-12 14:27:46 -080027#include <android-base/macros.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080028#include <androidfw/Asset.h>
Adam Lesinskibebfcc42018-02-12 14:27:46 -080029#include <androidfw/AssetManager2.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080030#include <androidfw/ResourceTypes.h>
Steven Morelanddc01e932017-05-01 12:56:08 -070031#include <android-base/macros.h>
Jason Samsf29ca502009-06-23 12:22:47 -070032
Jason Samsd19f10d2009-05-22 14:03:28 -070033#include "jni.h"
Steven Moreland2279b252017-07-19 09:50:45 -070034#include <nativehelper/JNIHelp.h>
Derek Sollenbergerdb98b522019-12-27 15:10:50 -050035#include <android/graphics/bitmap.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070036#include "android_runtime/AndroidRuntime.h"
Jim Milleree956052010-08-19 18:56:00 -070037#include "android_runtime/android_view_Surface.h"
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -080038#include "android_runtime/android_util_AssetManager.h"
Miao Wang33287e82017-03-06 09:31:32 -080039#include "android/native_window.h"
40#include "android/native_window_jni.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070041
Jason Sams1d6983a2012-02-16 16:07:49 -080042#include <rsEnv.h>
Miao Wangcbb02062017-01-24 18:58:17 -080043#include <rsApiStubs.h>
Jason Samsfb9aa9f2012-03-28 15:30:07 -070044#include <gui/Surface.h>
Andy McFaddend47f7d82012-12-18 09:48:38 -080045#include <gui/GLConsumer.h>
Jason Samsfaa32b32011-06-20 16:58:04 -070046#include <android_runtime/android_graphics_SurfaceTexture.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070047
Steve Block3762c312012-01-06 19:20:56 +000048//#define LOG_API ALOGE
Andreas Gampe67333922014-11-10 20:35:59 -080049static constexpr bool kLogApi = false;
Jason Samsd19f10d2009-05-22 14:03:28 -070050
51using namespace android;
52
Stephen Hines414fa2c2014-04-17 01:02:42 -070053#define PER_ARRAY_TYPE(flag, fnc, readonly, ...) { \
Jason Samse729a942013-11-06 11:22:02 -080054 jint len = 0; \
Chris Wailes488230c32014-08-14 11:22:40 -070055 void *ptr = nullptr; \
Miao Wang87e908d2015-03-02 15:15:15 -080056 void *srcPtr = nullptr; \
Jason Sams21659ac2013-11-06 15:08:07 -080057 size_t typeBytes = 0; \
Stephen Hines414fa2c2014-04-17 01:02:42 -070058 jint relFlag = 0; \
59 if (readonly) { \
60 /* The on-release mode should only be JNI_ABORT for read-only accesses. */ \
Miao Wang87e908d2015-03-02 15:15:15 -080061 /* readonly = true, also indicates we are copying to the allocation . */ \
Stephen Hines414fa2c2014-04-17 01:02:42 -070062 relFlag = JNI_ABORT; \
63 } \
Jason Samse729a942013-11-06 11:22:02 -080064 switch(dataType) { \
65 case RS_TYPE_FLOAT_32: \
66 len = _env->GetArrayLength((jfloatArray)data); \
67 ptr = _env->GetFloatArrayElements((jfloatArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -070068 if (ptr == nullptr) { \
69 ALOGE("Failed to get Java array elements."); \
70 return; \
71 } \
Jason Sams21659ac2013-11-06 15:08:07 -080072 typeBytes = 4; \
Miao Wang87e908d2015-03-02 15:15:15 -080073 if (usePadding) { \
74 srcPtr = ptr; \
75 len = len / 3 * 4; \
76 if (count == 0) { \
77 count = len / 4; \
78 } \
79 ptr = malloc (len * typeBytes); \
80 if (readonly) { \
81 copyWithPadding(ptr, srcPtr, mSize, count); \
82 fnc(__VA_ARGS__); \
83 } else { \
84 fnc(__VA_ARGS__); \
85 copyWithUnPadding(srcPtr, ptr, mSize, count); \
86 } \
87 free(ptr); \
88 ptr = srcPtr; \
89 } else { \
90 fnc(__VA_ARGS__); \
91 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -070092 _env->ReleaseFloatArrayElements((jfloatArray)data, (jfloat *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -080093 return; \
94 case RS_TYPE_FLOAT_64: \
95 len = _env->GetArrayLength((jdoubleArray)data); \
96 ptr = _env->GetDoubleArrayElements((jdoubleArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -070097 if (ptr == nullptr) { \
98 ALOGE("Failed to get Java array elements."); \
99 return; \
100 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800101 typeBytes = 8; \
Miao Wang87e908d2015-03-02 15:15:15 -0800102 if (usePadding) { \
103 srcPtr = ptr; \
104 len = len / 3 * 4; \
105 if (count == 0) { \
106 count = len / 4; \
107 } \
108 ptr = malloc (len * typeBytes); \
109 if (readonly) { \
110 copyWithPadding(ptr, srcPtr, mSize, count); \
111 fnc(__VA_ARGS__); \
112 } else { \
113 fnc(__VA_ARGS__); \
114 copyWithUnPadding(srcPtr, ptr, mSize, count); \
115 } \
116 free(ptr); \
117 ptr = srcPtr; \
118 } else { \
119 fnc(__VA_ARGS__); \
120 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700121 _env->ReleaseDoubleArrayElements((jdoubleArray)data, (jdouble *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800122 return; \
123 case RS_TYPE_SIGNED_8: \
124 case RS_TYPE_UNSIGNED_8: \
125 len = _env->GetArrayLength((jbyteArray)data); \
126 ptr = _env->GetByteArrayElements((jbyteArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700127 if (ptr == nullptr) { \
128 ALOGE("Failed to get Java array elements."); \
129 return; \
130 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800131 typeBytes = 1; \
Miao Wang87e908d2015-03-02 15:15:15 -0800132 if (usePadding) { \
133 srcPtr = ptr; \
134 len = len / 3 * 4; \
135 if (count == 0) { \
136 count = len / 4; \
137 } \
138 ptr = malloc (len * typeBytes); \
139 if (readonly) { \
140 copyWithPadding(ptr, srcPtr, mSize, count); \
141 fnc(__VA_ARGS__); \
142 } else { \
143 fnc(__VA_ARGS__); \
144 copyWithUnPadding(srcPtr, ptr, mSize, count); \
145 } \
146 free(ptr); \
147 ptr = srcPtr; \
148 } else { \
149 fnc(__VA_ARGS__); \
150 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700151 _env->ReleaseByteArrayElements((jbyteArray)data, (jbyte*)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800152 return; \
153 case RS_TYPE_SIGNED_16: \
154 case RS_TYPE_UNSIGNED_16: \
Pirama Arumuga Nainar13332152016-03-01 20:37:19 -0800155 case RS_TYPE_FLOAT_16: \
Jason Samse729a942013-11-06 11:22:02 -0800156 len = _env->GetArrayLength((jshortArray)data); \
157 ptr = _env->GetShortArrayElements((jshortArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700158 if (ptr == nullptr) { \
159 ALOGE("Failed to get Java array elements."); \
160 return; \
161 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800162 typeBytes = 2; \
Miao Wang87e908d2015-03-02 15:15:15 -0800163 if (usePadding) { \
164 srcPtr = ptr; \
165 len = len / 3 * 4; \
166 if (count == 0) { \
167 count = len / 4; \
168 } \
169 ptr = malloc (len * typeBytes); \
170 if (readonly) { \
171 copyWithPadding(ptr, srcPtr, mSize, count); \
172 fnc(__VA_ARGS__); \
173 } else { \
174 fnc(__VA_ARGS__); \
175 copyWithUnPadding(srcPtr, ptr, mSize, count); \
176 } \
177 free(ptr); \
178 ptr = srcPtr; \
179 } else { \
180 fnc(__VA_ARGS__); \
181 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700182 _env->ReleaseShortArrayElements((jshortArray)data, (jshort *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800183 return; \
184 case RS_TYPE_SIGNED_32: \
185 case RS_TYPE_UNSIGNED_32: \
186 len = _env->GetArrayLength((jintArray)data); \
187 ptr = _env->GetIntArrayElements((jintArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700188 if (ptr == nullptr) { \
189 ALOGE("Failed to get Java array elements."); \
190 return; \
191 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800192 typeBytes = 4; \
Miao Wang87e908d2015-03-02 15:15:15 -0800193 if (usePadding) { \
194 srcPtr = ptr; \
195 len = len / 3 * 4; \
196 if (count == 0) { \
197 count = len / 4; \
198 } \
199 ptr = malloc (len * typeBytes); \
200 if (readonly) { \
201 copyWithPadding(ptr, srcPtr, mSize, count); \
202 fnc(__VA_ARGS__); \
203 } else { \
204 fnc(__VA_ARGS__); \
205 copyWithUnPadding(srcPtr, ptr, mSize, count); \
206 } \
207 free(ptr); \
208 ptr = srcPtr; \
209 } else { \
210 fnc(__VA_ARGS__); \
211 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700212 _env->ReleaseIntArrayElements((jintArray)data, (jint *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800213 return; \
214 case RS_TYPE_SIGNED_64: \
215 case RS_TYPE_UNSIGNED_64: \
216 len = _env->GetArrayLength((jlongArray)data); \
217 ptr = _env->GetLongArrayElements((jlongArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700218 if (ptr == nullptr) { \
219 ALOGE("Failed to get Java array elements."); \
220 return; \
221 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800222 typeBytes = 8; \
Miao Wang87e908d2015-03-02 15:15:15 -0800223 if (usePadding) { \
224 srcPtr = ptr; \
225 len = len / 3 * 4; \
226 if (count == 0) { \
227 count = len / 4; \
228 } \
229 ptr = malloc (len * typeBytes); \
230 if (readonly) { \
231 copyWithPadding(ptr, srcPtr, mSize, count); \
232 fnc(__VA_ARGS__); \
233 } else { \
234 fnc(__VA_ARGS__); \
235 copyWithUnPadding(srcPtr, ptr, mSize, count); \
236 } \
237 free(ptr); \
238 ptr = srcPtr; \
239 } else { \
240 fnc(__VA_ARGS__); \
241 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700242 _env->ReleaseLongArrayElements((jlongArray)data, (jlong *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800243 return; \
244 default: \
245 break; \
246 } \
Miao Wang87e908d2015-03-02 15:15:15 -0800247 UNUSED(len, ptr, srcPtr, typeBytes, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800248}
249
250
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800251class AutoJavaStringToUTF8 {
252public:
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800253 AutoJavaStringToUTF8(JNIEnv* env, jstring str) : fEnv(env), fJStr(str) {
Chris Wailes488230c32014-08-14 11:22:40 -0700254 fCStr = env->GetStringUTFChars(str, nullptr);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800255 fLength = env->GetStringUTFLength(str);
256 }
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800257 ~AutoJavaStringToUTF8() {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800258 fEnv->ReleaseStringUTFChars(fJStr, fCStr);
259 }
260 const char* c_str() const { return fCStr; }
261 jsize length() const { return fLength; }
262
263private:
264 JNIEnv* fEnv;
265 jstring fJStr;
266 const char* fCStr;
267 jsize fLength;
268};
269
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800270class AutoJavaStringArrayToUTF8 {
271public:
272 AutoJavaStringArrayToUTF8(JNIEnv* env, jobjectArray strings, jsize stringsLength)
273 : mEnv(env), mStrings(strings), mStringsLength(stringsLength) {
Chris Wailes488230c32014-08-14 11:22:40 -0700274 mCStrings = nullptr;
275 mSizeArray = nullptr;
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800276 if (stringsLength > 0) {
277 mCStrings = (const char **)calloc(stringsLength, sizeof(char *));
278 mSizeArray = (size_t*)calloc(stringsLength, sizeof(size_t));
279 for (jsize ct = 0; ct < stringsLength; ct ++) {
280 jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
Chris Wailes488230c32014-08-14 11:22:40 -0700281 mCStrings[ct] = mEnv->GetStringUTFChars(s, nullptr);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800282 mSizeArray[ct] = mEnv->GetStringUTFLength(s);
283 }
284 }
285 }
286 ~AutoJavaStringArrayToUTF8() {
287 for (jsize ct=0; ct < mStringsLength; ct++) {
288 jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
289 mEnv->ReleaseStringUTFChars(s, mCStrings[ct]);
290 }
291 free(mCStrings);
292 free(mSizeArray);
293 }
294 const char **c_str() const { return mCStrings; }
295 size_t *c_str_len() const { return mSizeArray; }
296 jsize length() const { return mStringsLength; }
297
298private:
299 JNIEnv *mEnv;
300 jobjectArray mStrings;
301 const char **mCStrings;
302 size_t *mSizeArray;
303 jsize mStringsLength;
304};
305
Jason Samsd19f10d2009-05-22 14:03:28 -0700306// ---------------------------------------------------------------------------
307
Jason Samsffe9f482009-06-01 17:45:53 -0700308static jfieldID gContextId = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -0700309
310static void _nInit(JNIEnv *_env, jclass _this)
311{
Tim Murrayeff663f2013-11-15 13:08:30 -0800312 gContextId = _env->GetFieldID(_this, "mContext", "J");
Jason Samsd19f10d2009-05-22 14:03:28 -0700313}
314
Jason Samsd19f10d2009-05-22 14:03:28 -0700315// ---------------------------------------------------------------------------
316
Miao Wang87e908d2015-03-02 15:15:15 -0800317static void copyWithPadding(void* ptr, void* srcPtr, int mSize, int count) {
318 int sizeBytesPad = mSize * 4;
319 int sizeBytes = mSize * 3;
320 uint8_t *dst = static_cast<uint8_t *>(ptr);
321 uint8_t *src = static_cast<uint8_t *>(srcPtr);
322 for (int i = 0; i < count; i++) {
323 memcpy(dst, src, sizeBytes);
324 dst += sizeBytesPad;
325 src += sizeBytes;
326 }
327}
328
329static void copyWithUnPadding(void* ptr, void* srcPtr, int mSize, int count) {
330 int sizeBytesPad = mSize * 4;
331 int sizeBytes = mSize * 3;
332 uint8_t *dst = static_cast<uint8_t *>(ptr);
333 uint8_t *src = static_cast<uint8_t *>(srcPtr);
334 for (int i = 0; i < count; i++) {
335 memcpy(dst, src, sizeBytes);
336 dst += sizeBytes;
337 src += sizeBytesPad;
338 }
339}
340
341
342// ---------------------------------------------------------------------------
Jason Sams3eaa3382009-06-10 15:04:38 -0700343static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800344nContextFinish(JNIEnv *_env, jobject _this, jlong con)
Jason Sams96ed4cf2010-06-15 12:15:57 -0700345{
Andreas Gampe67333922014-11-10 20:35:59 -0800346 if (kLogApi) {
347 ALOGD("nContextFinish, con(%p)", (RsContext)con);
348 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800349 rsContextFinish((RsContext)con);
Jason Sams96ed4cf2010-06-15 12:15:57 -0700350}
351
Yang Ni281c3252014-10-24 08:52:24 -0700352static jlong
353nClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong kernelID,
354 jlong returnValue, jlongArray fieldIDArray,
355 jlongArray valueArray, jintArray sizeArray,
356 jlongArray depClosureArray, jlongArray depFieldIDArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700357 jlong ret = 0;
358
Yang Ni281c3252014-10-24 08:52:24 -0700359 jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
360 jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700361 if (jFieldIDs == nullptr) {
362 ALOGE("Failed to get Java array elements: fieldIDs.");
363 return ret;
364 }
365
Yang Ni281c3252014-10-24 08:52:24 -0700366 jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
367 jsize values_length = _env->GetArrayLength(valueArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700368 if (jValues == nullptr) {
369 ALOGE("Failed to get Java array elements: values.");
370 return ret;
371 }
372
Yang Ni17c2d7a2015-04-30 16:13:54 -0700373 jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
Yang Ni281c3252014-10-24 08:52:24 -0700374 jsize sizes_length = _env->GetArrayLength(sizeArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700375 if (jSizes == nullptr) {
376 ALOGE("Failed to get Java array elements: sizes.");
377 return ret;
378 }
379
Yang Ni281c3252014-10-24 08:52:24 -0700380 jlong* jDepClosures =
381 _env->GetLongArrayElements(depClosureArray, nullptr);
382 jsize depClosures_length = _env->GetArrayLength(depClosureArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700383 if (jDepClosures == nullptr) {
384 ALOGE("Failed to get Java array elements: depClosures.");
385 return ret;
386 }
387
Yang Ni281c3252014-10-24 08:52:24 -0700388 jlong* jDepFieldIDs =
389 _env->GetLongArrayElements(depFieldIDArray, nullptr);
390 jsize depFieldIDs_length = _env->GetArrayLength(depFieldIDArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700391 if (jDepFieldIDs == nullptr) {
392 ALOGE("Failed to get Java array elements: depFieldIDs.");
393 return ret;
394 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700395
396 size_t numValues, numDependencies;
397 RsScriptFieldID* fieldIDs;
Yang Ni17c2d7a2015-04-30 16:13:54 -0700398 RsClosure* depClosures;
399 RsScriptFieldID* depFieldIDs;
400
401 if (fieldIDs_length != values_length || values_length != sizes_length) {
402 ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
403 goto exit;
404 }
405
406 numValues = (size_t)fieldIDs_length;
407
408 if (depClosures_length != depFieldIDs_length) {
409 ALOGE("Unmatched closures and field IDs for dependencies in closure creation.");
410 goto exit;
411 }
412
413 numDependencies = (size_t)depClosures_length;
414
415 if (numDependencies > numValues) {
416 ALOGE("Unexpected number of dependencies in closure creation");
417 goto exit;
418 }
419
Yang Ni7b2a46f2015-05-05 12:41:19 -0700420 if (numValues > RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700421 ALOGE("Too many arguments or globals in closure creation");
422 goto exit;
423 }
424
Yang Ni86c5c2d2016-03-25 15:49:07 -0700425 if (numValues > 0) {
426 fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
427 if (fieldIDs == nullptr) {
428 goto exit;
429 }
430 } else {
431 // numValues == 0
432 // alloca(0) implementation is platform-dependent.
433 fieldIDs = nullptr;
Yang Ni17c2d7a2015-04-30 16:13:54 -0700434 }
435
436 for (size_t i = 0; i < numValues; i++) {
437 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
438 }
439
Yang Ni86c5c2d2016-03-25 15:49:07 -0700440 if (numDependencies > 0) {
441 depClosures = (RsClosure*)alloca(sizeof(RsClosure) * numDependencies);
442 if (depClosures == nullptr) {
443 goto exit;
444 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700445
Yang Ni86c5c2d2016-03-25 15:49:07 -0700446 for (size_t i = 0; i < numDependencies; i++) {
447 depClosures[i] = (RsClosure)jDepClosures[i];
448 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700449
Yang Ni86c5c2d2016-03-25 15:49:07 -0700450 depFieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numDependencies);
451 if (depFieldIDs == nullptr) {
452 goto exit;
453 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700454
Yang Ni86c5c2d2016-03-25 15:49:07 -0700455 for (size_t i = 0; i < numDependencies; i++) {
456 depFieldIDs[i] = (RsClosure)jDepFieldIDs[i];
457 }
458 } else {
459 // alloca(0) implementation is platform-dependent.
460 depClosures = nullptr;
461 depFieldIDs = nullptr;
Yang Ni281c3252014-10-24 08:52:24 -0700462 }
463
Yang Ni17c2d7a2015-04-30 16:13:54 -0700464 ret = (jlong)(uintptr_t)rsClosureCreate(
Yang Ni281c3252014-10-24 08:52:24 -0700465 (RsContext)con, (RsScriptKernelID)kernelID, (RsAllocation)returnValue,
Yang Ni263cc902015-11-10 13:27:04 -0800466 fieldIDs, numValues, jValues, numValues,
Yang Ni17c2d7a2015-04-30 16:13:54 -0700467 (int*)jSizes, numValues,
468 depClosures, numDependencies,
469 depFieldIDs, numDependencies);
470
471exit:
472
473 _env->ReleaseLongArrayElements(depFieldIDArray, jDepFieldIDs, JNI_ABORT);
474 _env->ReleaseLongArrayElements(depClosureArray, jDepClosures, JNI_ABORT);
475 _env->ReleaseIntArrayElements (sizeArray, jSizes, JNI_ABORT);
476 _env->ReleaseLongArrayElements(valueArray, jValues, JNI_ABORT);
477 _env->ReleaseLongArrayElements(fieldIDArray, jFieldIDs, JNI_ABORT);
478
479 return ret;
Yang Ni281c3252014-10-24 08:52:24 -0700480}
481
Yang Nibe392ad2015-01-23 17:16:02 -0800482static jlong
483nInvokeClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong invokeID,
484 jbyteArray paramArray, jlongArray fieldIDArray, jlongArray valueArray,
485 jintArray sizeArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700486 jlong ret = 0;
487
Yang Nibe392ad2015-01-23 17:16:02 -0800488 jbyte* jParams = _env->GetByteArrayElements(paramArray, nullptr);
489 jsize jParamLength = _env->GetArrayLength(paramArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700490 if (jParams == nullptr) {
491 ALOGE("Failed to get Java array elements: params.");
492 return ret;
493 }
494
Yang Nibe392ad2015-01-23 17:16:02 -0800495 jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
496 jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700497 if (jFieldIDs == nullptr) {
498 ALOGE("Failed to get Java array elements: fieldIDs.");
499 return ret;
500 }
501
Yang Ni17c2d7a2015-04-30 16:13:54 -0700502 jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
503 jsize values_length = _env->GetArrayLength(valueArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700504 if (jValues == nullptr) {
505 ALOGE("Failed to get Java array elements: values.");
506 return ret;
507 }
508
Yang Ni17c2d7a2015-04-30 16:13:54 -0700509 jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
510 jsize sizes_length = _env->GetArrayLength(sizeArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700511 if (jSizes == nullptr) {
512 ALOGE("Failed to get Java array elements: sizes.");
513 return ret;
514 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700515
516 size_t numValues;
517 RsScriptFieldID* fieldIDs;
Yang Ni17c2d7a2015-04-30 16:13:54 -0700518
519 if (fieldIDs_length != values_length || values_length != sizes_length) {
520 ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
521 goto exit;
522 }
523
524 numValues = (size_t) fieldIDs_length;
525
Yang Ni7b2a46f2015-05-05 12:41:19 -0700526 if (numValues > RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700527 ALOGE("Too many arguments or globals in closure creation");
528 goto exit;
529 }
530
531 fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
532 if (fieldIDs == nullptr) {
533 goto exit;
534 }
535
536 for (size_t i = 0; i< numValues; i++) {
Yang Nibe392ad2015-01-23 17:16:02 -0800537 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
538 }
539
Yang Ni17c2d7a2015-04-30 16:13:54 -0700540 ret = (jlong)(uintptr_t)rsInvokeClosureCreate(
Yang Nibe392ad2015-01-23 17:16:02 -0800541 (RsContext)con, (RsScriptInvokeID)invokeID, jParams, jParamLength,
Yang Ni263cc902015-11-10 13:27:04 -0800542 fieldIDs, numValues, jValues, numValues,
Yang Ni17c2d7a2015-04-30 16:13:54 -0700543 (int*)jSizes, numValues);
544
545exit:
546
547 _env->ReleaseIntArrayElements (sizeArray, jSizes, JNI_ABORT);
548 _env->ReleaseLongArrayElements(valueArray, jValues, JNI_ABORT);
549 _env->ReleaseLongArrayElements(fieldIDArray, jFieldIDs, JNI_ABORT);
550 _env->ReleaseByteArrayElements(paramArray, jParams, JNI_ABORT);
551
552 return ret;
Yang Nibe392ad2015-01-23 17:16:02 -0800553}
554
Yang Ni281c3252014-10-24 08:52:24 -0700555static void
556nClosureSetArg(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
557 jint index, jlong value, jint size) {
Yang Ni263cc902015-11-10 13:27:04 -0800558 // Size is signed with -1 indicating the value is an Allocation
Yang Ni281c3252014-10-24 08:52:24 -0700559 rsClosureSetArg((RsContext)con, (RsClosure)closureID, (uint32_t)index,
Yang Ni263cc902015-11-10 13:27:04 -0800560 (uintptr_t)value, size);
Yang Ni281c3252014-10-24 08:52:24 -0700561}
562
563static void
564nClosureSetGlobal(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
565 jlong fieldID, jlong value, jint size) {
Yang Ni263cc902015-11-10 13:27:04 -0800566 // Size is signed with -1 indicating the value is an Allocation
Yang Ni281c3252014-10-24 08:52:24 -0700567 rsClosureSetGlobal((RsContext)con, (RsClosure)closureID,
Yang Ni263cc902015-11-10 13:27:04 -0800568 (RsScriptFieldID)fieldID, (int64_t)value, size);
Yang Ni281c3252014-10-24 08:52:24 -0700569}
570
571static long
Yang Ni35be56c2015-04-02 17:47:56 -0700572nScriptGroup2Create(JNIEnv *_env, jobject _this, jlong con, jstring name,
Yang Niebf63402015-01-16 11:06:26 -0800573 jstring cacheDir, jlongArray closureArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700574 jlong ret = 0;
575
Yang Ni35be56c2015-04-02 17:47:56 -0700576 AutoJavaStringToUTF8 nameUTF(_env, name);
Yang Niebf63402015-01-16 11:06:26 -0800577 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
578
Yang Ni281c3252014-10-24 08:52:24 -0700579 jlong* jClosures = _env->GetLongArrayElements(closureArray, nullptr);
580 jsize numClosures = _env->GetArrayLength(closureArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700581 if (jClosures == nullptr) {
582 ALOGE("Failed to get Java array elements: closures.");
583 return ret;
584 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700585
586 RsClosure* closures;
587
Yang Ni7b2a46f2015-05-05 12:41:19 -0700588 if (numClosures > (jsize) RS_SCRIPT_GROUP_MAX_NUMBER_CLOSURES) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700589 ALOGE("Too many closures in script group");
590 goto exit;
591 }
592
593 closures = (RsClosure*)alloca(sizeof(RsClosure) * numClosures);
594 if (closures == nullptr) {
595 goto exit;
596 }
597
Yang Ni281c3252014-10-24 08:52:24 -0700598 for (int i = 0; i < numClosures; i++) {
599 closures[i] = (RsClosure)jClosures[i];
600 }
601
Yang Ni17c2d7a2015-04-30 16:13:54 -0700602 ret = (jlong)(uintptr_t)rsScriptGroup2Create(
Yang Ni35be56c2015-04-02 17:47:56 -0700603 (RsContext)con, nameUTF.c_str(), nameUTF.length(),
604 cacheDirUTF.c_str(), cacheDirUTF.length(),
Yang Niebf63402015-01-16 11:06:26 -0800605 closures, numClosures);
Yang Ni17c2d7a2015-04-30 16:13:54 -0700606
607exit:
608
609 _env->ReleaseLongArrayElements(closureArray, jClosures, JNI_ABORT);
610
611 return ret;
Yang Ni281c3252014-10-24 08:52:24 -0700612}
613
614static void
615nScriptGroup2Execute(JNIEnv *_env, jobject _this, jlong con, jlong groupID) {
616 rsScriptGroupExecute((RsContext)con, (RsScriptGroup2)groupID);
617}
618
Jason Sams96ed4cf2010-06-15 12:15:57 -0700619static void
Tim Murray25207df2015-01-12 16:47:56 -0800620nScriptIntrinsicBLAS_Single(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
621 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
622 jfloat alpha, jlong A, jlong B, jfloat beta, jlong C, jint incX, jint incY,
623 jint KL, jint KU) {
624 RsBlasCall call;
625 memset(&call, 0, sizeof(call));
626 call.func = (RsBlasFunction)func;
627 call.transA = (RsBlasTranspose)TransA;
628 call.transB = (RsBlasTranspose)TransB;
629 call.side = (RsBlasSide)Side;
630 call.uplo = (RsBlasUplo)Uplo;
631 call.diag = (RsBlasDiag)Diag;
632 call.M = M;
633 call.N = N;
634 call.K = K;
635 call.alpha.f = alpha;
636 call.beta.f = beta;
637 call.incX = incX;
638 call.incY = incY;
639 call.KL = KL;
640 call.KU = KU;
641
642 RsAllocation in_allocs[3];
643 in_allocs[0] = (RsAllocation)A;
644 in_allocs[1] = (RsAllocation)B;
645 in_allocs[2] = (RsAllocation)C;
646
647 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wangb742fcc2016-10-06 10:45:42 -0700648 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800649 &call, sizeof(call), nullptr, 0);
650}
651
652static void
653nScriptIntrinsicBLAS_Double(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
654 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
655 jdouble alpha, jlong A, jlong B, jdouble beta, jlong C, jint incX, jint incY,
656 jint KL, jint KU) {
657 RsBlasCall call;
658 memset(&call, 0, sizeof(call));
659 call.func = (RsBlasFunction)func;
660 call.transA = (RsBlasTranspose)TransA;
661 call.transB = (RsBlasTranspose)TransB;
662 call.side = (RsBlasSide)Side;
663 call.uplo = (RsBlasUplo)Uplo;
664 call.diag = (RsBlasDiag)Diag;
665 call.M = M;
666 call.N = N;
667 call.K = K;
668 call.alpha.d = alpha;
669 call.beta.d = beta;
670 call.incX = incX;
671 call.incY = incY;
672 call.KL = KL;
673 call.KU = KU;
674
675 RsAllocation in_allocs[3];
676 in_allocs[0] = (RsAllocation)A;
677 in_allocs[1] = (RsAllocation)B;
678 in_allocs[2] = (RsAllocation)C;
679
680 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700681 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800682 &call, sizeof(call), nullptr, 0);
683}
684
685static void
686nScriptIntrinsicBLAS_Complex(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
687 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
688 jfloat alphaX, jfloat alphaY, jlong A, jlong B, jfloat betaX,
689 jfloat betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
690 RsBlasCall call;
691 memset(&call, 0, sizeof(call));
692 call.func = (RsBlasFunction)func;
693 call.transA = (RsBlasTranspose)TransA;
694 call.transB = (RsBlasTranspose)TransB;
695 call.side = (RsBlasSide)Side;
696 call.uplo = (RsBlasUplo)Uplo;
697 call.diag = (RsBlasDiag)Diag;
698 call.M = M;
699 call.N = N;
700 call.K = K;
701 call.alpha.c.r = alphaX;
702 call.alpha.c.i = alphaY;
703 call.beta.c.r = betaX;
Miao Wang82585b32015-04-30 13:44:49 -0700704 call.beta.c.i = betaY;
Tim Murray25207df2015-01-12 16:47:56 -0800705 call.incX = incX;
706 call.incY = incY;
707 call.KL = KL;
708 call.KU = KU;
709
710 RsAllocation in_allocs[3];
711 in_allocs[0] = (RsAllocation)A;
712 in_allocs[1] = (RsAllocation)B;
713 in_allocs[2] = (RsAllocation)C;
714
715 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700716 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800717 &call, sizeof(call), nullptr, 0);
718}
719
720static void
721nScriptIntrinsicBLAS_Z(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
722 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
723 jdouble alphaX, jdouble alphaY, jlong A, jlong B, jdouble betaX,
724 jdouble betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
725 RsBlasCall call;
726 memset(&call, 0, sizeof(call));
727 call.func = (RsBlasFunction)func;
728 call.transA = (RsBlasTranspose)TransA;
729 call.transB = (RsBlasTranspose)TransB;
730 call.side = (RsBlasSide)Side;
731 call.uplo = (RsBlasUplo)Uplo;
732 call.diag = (RsBlasDiag)Diag;
733 call.M = M;
734 call.N = N;
735 call.K = K;
736 call.alpha.z.r = alphaX;
737 call.alpha.z.i = alphaY;
738 call.beta.z.r = betaX;
Miao Wang82585b32015-04-30 13:44:49 -0700739 call.beta.z.i = betaY;
Tim Murray25207df2015-01-12 16:47:56 -0800740 call.incX = incX;
741 call.incY = incY;
742 call.KL = KL;
743 call.KU = KU;
744
745 RsAllocation in_allocs[3];
746 in_allocs[0] = (RsAllocation)A;
747 in_allocs[1] = (RsAllocation)B;
748 in_allocs[2] = (RsAllocation)C;
749
750 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700751 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800752 &call, sizeof(call), nullptr, 0);
753}
754
755
756static void
Tim Murray9cb16a22015-04-01 11:07:16 -0700757nScriptIntrinsicBLAS_BNNM(JNIEnv *_env, jobject _this, jlong con, jlong id, jint M, jint N, jint K,
758 jlong A, jint a_offset, jlong B, jint b_offset, jlong C, jint c_offset,
759 jint c_mult_int) {
760 RsBlasCall call;
761 memset(&call, 0, sizeof(call));
762 call.func = RsBlas_bnnm;
763 call.M = M;
764 call.N = N;
765 call.K = K;
Miao Wang25148062015-06-29 17:43:03 -0700766 call.a_offset = a_offset & 0xFF;
767 call.b_offset = b_offset & 0xFF;
Tim Murray9cb16a22015-04-01 11:07:16 -0700768 call.c_offset = c_offset;
769 call.c_mult_int = c_mult_int;
770
771 RsAllocation in_allocs[3];
772 in_allocs[0] = (RsAllocation)A;
773 in_allocs[1] = (RsAllocation)B;
774 in_allocs[2] = (RsAllocation)C;
775
776 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700777 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray9cb16a22015-04-01 11:07:16 -0700778 &call, sizeof(call), nullptr, 0);
779}
780
781
782static void
Tim Murray460a0492013-11-19 12:45:54 -0800783nAssignName(JNIEnv *_env, jobject _this, jlong con, jlong obj, jbyteArray str)
Jason Sams3eaa3382009-06-10 15:04:38 -0700784{
Andreas Gampe67333922014-11-10 20:35:59 -0800785 if (kLogApi) {
786 ALOGD("nAssignName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
787 }
Jason Sams3eaa3382009-06-10 15:04:38 -0700788 jint len = _env->GetArrayLength(str);
789 jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
Miao Wangba8766c2015-10-12 17:24:13 -0700790 if (cptr == nullptr) {
791 ALOGE("Failed to get Java array elements");
792 return;
793 }
794
Tim Murrayeff663f2013-11-15 13:08:30 -0800795 rsAssignName((RsContext)con, (void *)obj, (const char *)cptr, len);
Jason Sams3eaa3382009-06-10 15:04:38 -0700796 _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
797}
798
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700799static jstring
Tim Murray460a0492013-11-19 12:45:54 -0800800nGetName(JNIEnv *_env, jobject _this, jlong con, jlong obj)
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700801{
Andreas Gampe67333922014-11-10 20:35:59 -0800802 if (kLogApi) {
803 ALOGD("nGetName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
804 }
Chris Wailes488230c32014-08-14 11:22:40 -0700805 const char *name = nullptr;
Tim Murrayeff663f2013-11-15 13:08:30 -0800806 rsaGetName((RsContext)con, (void *)obj, &name);
Chris Wailes488230c32014-08-14 11:22:40 -0700807 if(name == nullptr || strlen(name) == 0) {
808 return nullptr;
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700809 }
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700810 return _env->NewStringUTF(name);
811}
812
Jason Sams7ce033d2009-08-18 14:14:24 -0700813static void
Tim Murray460a0492013-11-19 12:45:54 -0800814nObjDestroy(JNIEnv *_env, jobject _this, jlong con, jlong obj)
Jason Sams7ce033d2009-08-18 14:14:24 -0700815{
Andreas Gampe67333922014-11-10 20:35:59 -0800816 if (kLogApi) {
817 ALOGD("nObjDestroy, con(%p) obj(%p)", (RsContext)con, (void *)obj);
818 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800819 rsObjDestroy((RsContext)con, (void *)obj);
Jason Sams7ce033d2009-08-18 14:14:24 -0700820}
821
Jason Sams3eaa3382009-06-10 15:04:38 -0700822// ---------------------------------------------------------------------------
823
Tim Murrayeff663f2013-11-15 13:08:30 -0800824static jlong
Jason Samsd19f10d2009-05-22 14:03:28 -0700825nDeviceCreate(JNIEnv *_env, jobject _this)
826{
Andreas Gampe67333922014-11-10 20:35:59 -0800827 if (kLogApi) {
828 ALOGD("nDeviceCreate");
829 }
Tim Murray3aa89c12014-08-18 17:51:22 -0700830 return (jlong)(uintptr_t)rsDeviceCreate();
Jason Samsd19f10d2009-05-22 14:03:28 -0700831}
832
833static void
Tim Murray5eaf4682014-01-10 11:25:52 -0800834nDeviceDestroy(JNIEnv *_env, jobject _this, jlong dev)
Jason Samsd19f10d2009-05-22 14:03:28 -0700835{
Andreas Gampe67333922014-11-10 20:35:59 -0800836 if (kLogApi) {
837 ALOGD("nDeviceDestroy");
838 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700839 return rsDeviceDestroy((RsDevice)dev);
840}
841
Jason Samsebfb4362009-09-23 13:57:02 -0700842static void
Tim Murray5eaf4682014-01-10 11:25:52 -0800843nDeviceSetConfig(JNIEnv *_env, jobject _this, jlong dev, jint p, jint value)
Jason Samsebfb4362009-09-23 13:57:02 -0700844{
Andreas Gampe67333922014-11-10 20:35:59 -0800845 if (kLogApi) {
846 ALOGD("nDeviceSetConfig dev(%p), param(%i), value(%i)", (void *)dev, p, value);
847 }
Jason Samsebfb4362009-09-23 13:57:02 -0700848 return rsDeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value);
849}
850
Tim Murrayeff663f2013-11-15 13:08:30 -0800851static jlong
Jason Sams81cd2b12014-12-02 12:36:43 -0800852nContextCreate(JNIEnv *_env, jobject _this, jlong dev, jint flags, jint sdkVer, jint contextType)
Jason Samsd19f10d2009-05-22 14:03:28 -0700853{
Andreas Gampe67333922014-11-10 20:35:59 -0800854 if (kLogApi) {
855 ALOGD("nContextCreate");
856 }
Jason Sams81cd2b12014-12-02 12:36:43 -0800857 return (jlong)(uintptr_t)rsContextCreate((RsDevice)dev, 0, sdkVer, (RsContextType)contextType, flags);
Jason Sams704ff642010-02-09 16:05:07 -0800858}
859
Tim Murrayeff663f2013-11-15 13:08:30 -0800860static jlong
Tim Murray5eaf4682014-01-10 11:25:52 -0800861nContextCreateGL(JNIEnv *_env, jobject _this, jlong dev, jint ver, jint sdkVer,
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000862 jint colorMin, jint colorPref,
863 jint alphaMin, jint alphaPref,
864 jint depthMin, jint depthPref,
865 jint stencilMin, jint stencilPref,
866 jint samplesMin, jint samplesPref, jfloat samplesQ,
867 jint dpi)
Jason Sams704ff642010-02-09 16:05:07 -0800868{
Yang Ni86c5c2d2016-03-25 15:49:07 -0700869 RsSurfaceConfig sc = {};
Jason Sams11c8af92010-10-13 15:31:10 -0700870 sc.alphaMin = alphaMin;
871 sc.alphaPref = alphaPref;
872 sc.colorMin = colorMin;
873 sc.colorPref = colorPref;
874 sc.depthMin = depthMin;
875 sc.depthPref = depthPref;
876 sc.samplesMin = samplesMin;
877 sc.samplesPref = samplesPref;
878 sc.samplesQ = samplesQ;
879
Andreas Gampe67333922014-11-10 20:35:59 -0800880 if (kLogApi) {
881 ALOGD("nContextCreateGL");
882 }
Tim Murray3aa89c12014-08-18 17:51:22 -0700883 return (jlong)(uintptr_t)rsContextCreateGL((RsDevice)dev, ver, sdkVer, sc, dpi);
Jason Samsd19f10d2009-05-22 14:03:28 -0700884}
885
886static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800887nContextSetPriority(JNIEnv *_env, jobject _this, jlong con, jint p)
Jason Sams7d787b42009-11-15 12:14:26 -0800888{
Andreas Gampe67333922014-11-10 20:35:59 -0800889 if (kLogApi) {
890 ALOGD("ContextSetPriority, con(%p), priority(%i)", (RsContext)con, p);
891 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800892 rsContextSetPriority((RsContext)con, p);
Jason Sams7d787b42009-11-15 12:14:26 -0800893}
894
Tim Murray47f31582015-04-07 15:43:24 -0700895static void
896nContextSetCacheDir(JNIEnv *_env, jobject _this, jlong con, jstring cacheDir)
897{
898 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
899
900 if (kLogApi) {
901 ALOGD("ContextSetCacheDir, con(%p), cacheDir(%s)", (RsContext)con, cacheDirUTF.c_str());
902 }
903 rsContextSetCacheDir((RsContext)con, cacheDirUTF.c_str(), cacheDirUTF.length());
904}
905
Jason Sams7d787b42009-11-15 12:14:26 -0800906
907
908static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800909nContextSetSurface(JNIEnv *_env, jobject _this, jlong con, jint width, jint height, jobject wnd)
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800910{
Andreas Gampe67333922014-11-10 20:35:59 -0800911 if (kLogApi) {
912 ALOGD("nContextSetSurface, con(%p), width(%i), height(%i), surface(%p)", (RsContext)con,
913 width, height, (Surface *)wnd);
914 }
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800915
Chris Wailes488230c32014-08-14 11:22:40 -0700916 ANativeWindow * window = nullptr;
917 if (wnd == nullptr) {
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800918
919 } else {
Jeff Brown64a55af2012-08-26 02:47:39 -0700920 window = android_view_Surface_getNativeWindow(_env, wnd).get();
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800921 }
922
Tim Murrayeff663f2013-11-15 13:08:30 -0800923 rsContextSetSurface((RsContext)con, width, height, window);
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800924}
925
926static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800927nContextDestroy(JNIEnv *_env, jobject _this, jlong con)
Jason Samsd19f10d2009-05-22 14:03:28 -0700928{
Andreas Gampe67333922014-11-10 20:35:59 -0800929 if (kLogApi) {
930 ALOGD("nContextDestroy, con(%p)", (RsContext)con);
931 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800932 rsContextDestroy((RsContext)con);
Jason Samsd19f10d2009-05-22 14:03:28 -0700933}
934
Jason Sams715333b2009-11-17 17:26:46 -0800935static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800936nContextDump(JNIEnv *_env, jobject _this, jlong con, jint bits)
Jason Sams715333b2009-11-17 17:26:46 -0800937{
Andreas Gampe67333922014-11-10 20:35:59 -0800938 if (kLogApi) {
939 ALOGD("nContextDump, con(%p) bits(%i)", (RsContext)con, bits);
940 }
Jason Sams715333b2009-11-17 17:26:46 -0800941 rsContextDump((RsContext)con, bits);
942}
Jason Samsd19f10d2009-05-22 14:03:28 -0700943
944static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800945nContextPause(JNIEnv *_env, jobject _this, jlong con)
Jason Sams65e7aa52009-09-24 17:38:20 -0700946{
Andreas Gampe67333922014-11-10 20:35:59 -0800947 if (kLogApi) {
948 ALOGD("nContextPause, con(%p)", (RsContext)con);
949 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800950 rsContextPause((RsContext)con);
Jason Sams65e7aa52009-09-24 17:38:20 -0700951}
952
953static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800954nContextResume(JNIEnv *_env, jobject _this, jlong con)
Jason Sams65e7aa52009-09-24 17:38:20 -0700955{
Andreas Gampe67333922014-11-10 20:35:59 -0800956 if (kLogApi) {
957 ALOGD("nContextResume, con(%p)", (RsContext)con);
958 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800959 rsContextResume((RsContext)con);
Jason Sams65e7aa52009-09-24 17:38:20 -0700960}
961
Jason Sams1c415172010-11-08 17:06:46 -0800962
963static jstring
Tim Murrayeff663f2013-11-15 13:08:30 -0800964nContextGetErrorMessage(JNIEnv *_env, jobject _this, jlong con)
Jason Sams1c415172010-11-08 17:06:46 -0800965{
Andreas Gampe67333922014-11-10 20:35:59 -0800966 if (kLogApi) {
967 ALOGD("nContextGetErrorMessage, con(%p)", (RsContext)con);
968 }
Jason Sams1c415172010-11-08 17:06:46 -0800969 char buf[1024];
970
971 size_t receiveLen;
972 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800973 int id = rsContextGetMessage((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -0700974 buf, sizeof(buf),
975 &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -0700976 &subID, sizeof(subID));
Jason Sams1c415172010-11-08 17:06:46 -0800977 if (!id && receiveLen) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +0100978 ALOGV("message receive buffer too small. %zu", receiveLen);
Jason Sams1c415172010-11-08 17:06:46 -0800979 }
980 return _env->NewStringUTF(buf);
981}
982
Jason Samsedbfabd2011-05-17 15:01:29 -0700983static jint
Tim Murrayeff663f2013-11-15 13:08:30 -0800984nContextGetUserMessage(JNIEnv *_env, jobject _this, jlong con, jintArray data)
Jason Sams516c3192009-10-06 13:58:47 -0700985{
Jason Sams516c3192009-10-06 13:58:47 -0700986 jint len = _env->GetArrayLength(data);
Andreas Gampe67333922014-11-10 20:35:59 -0800987 if (kLogApi) {
988 ALOGD("nContextGetMessage, con(%p), len(%i)", (RsContext)con, len);
989 }
Chris Wailes488230c32014-08-14 11:22:40 -0700990 jint *ptr = _env->GetIntArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -0700991 if (ptr == nullptr) {
992 ALOGE("Failed to get Java array elements");
993 return 0;
994 }
Jason Sams516c3192009-10-06 13:58:47 -0700995 size_t receiveLen;
Jason Sams1c415172010-11-08 17:06:46 -0800996 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800997 int id = rsContextGetMessage((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -0700998 ptr, len * 4,
999 &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -07001000 &subID, sizeof(subID));
Jason Sams516c3192009-10-06 13:58:47 -07001001 if (!id && receiveLen) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001002 ALOGV("message receive buffer too small. %zu", receiveLen);
Jason Sams516c3192009-10-06 13:58:47 -07001003 }
1004 _env->ReleaseIntArrayElements(data, ptr, 0);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001005 return (jint)id;
Jason Sams1c415172010-11-08 17:06:46 -08001006}
1007
1008static jint
Tim Murrayeff663f2013-11-15 13:08:30 -08001009nContextPeekMessage(JNIEnv *_env, jobject _this, jlong con, jintArray auxData)
Jason Sams1c415172010-11-08 17:06:46 -08001010{
Andreas Gampe67333922014-11-10 20:35:59 -08001011 if (kLogApi) {
1012 ALOGD("nContextPeekMessage, con(%p)", (RsContext)con);
1013 }
Chris Wailes488230c32014-08-14 11:22:40 -07001014 jint *auxDataPtr = _env->GetIntArrayElements(auxData, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001015 if (auxDataPtr == nullptr) {
1016 ALOGE("Failed to get Java array elements");
1017 return 0;
1018 }
Jason Sams1c415172010-11-08 17:06:46 -08001019 size_t receiveLen;
1020 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -08001021 int id = rsContextPeekMessage((RsContext)con, &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -07001022 &subID, sizeof(subID));
Jason Sams1c415172010-11-08 17:06:46 -08001023 auxDataPtr[0] = (jint)subID;
1024 auxDataPtr[1] = (jint)receiveLen;
1025 _env->ReleaseIntArrayElements(auxData, auxDataPtr, 0);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001026 return (jint)id;
Jason Sams516c3192009-10-06 13:58:47 -07001027}
1028
Tim Murrayeff663f2013-11-15 13:08:30 -08001029static void nContextInitToClient(JNIEnv *_env, jobject _this, jlong con)
Jason Sams516c3192009-10-06 13:58:47 -07001030{
Andreas Gampe67333922014-11-10 20:35:59 -08001031 if (kLogApi) {
1032 ALOGD("nContextInitToClient, con(%p)", (RsContext)con);
1033 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001034 rsContextInitToClient((RsContext)con);
Jason Sams516c3192009-10-06 13:58:47 -07001035}
1036
Tim Murrayeff663f2013-11-15 13:08:30 -08001037static void nContextDeinitToClient(JNIEnv *_env, jobject _this, jlong con)
Jason Sams516c3192009-10-06 13:58:47 -07001038{
Andreas Gampe67333922014-11-10 20:35:59 -08001039 if (kLogApi) {
1040 ALOGD("nContextDeinitToClient, con(%p)", (RsContext)con);
1041 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001042 rsContextDeinitToClient((RsContext)con);
Jason Sams516c3192009-10-06 13:58:47 -07001043}
1044
Jason Sams455d6442013-02-05 19:20:18 -08001045static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001046nContextSendMessage(JNIEnv *_env, jobject _this, jlong con, jint id, jintArray data)
Jason Sams455d6442013-02-05 19:20:18 -08001047{
Chris Wailes488230c32014-08-14 11:22:40 -07001048 jint *ptr = nullptr;
Jason Sams455d6442013-02-05 19:20:18 -08001049 jint len = 0;
1050 if (data) {
1051 len = _env->GetArrayLength(data);
Stephen Hines4a043c12014-08-21 23:20:32 -07001052 ptr = _env->GetIntArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001053 if (ptr == nullptr) {
1054 ALOGE("Failed to get Java array elements");
1055 return;
1056 }
Jason Sams455d6442013-02-05 19:20:18 -08001057 }
Andreas Gampe67333922014-11-10 20:35:59 -08001058 if (kLogApi) {
1059 ALOGD("nContextSendMessage, con(%p), id(%i), len(%i)", (RsContext)con, id, len);
1060 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001061 rsContextSendMessage((RsContext)con, id, (const uint8_t *)ptr, len * sizeof(int));
Jason Sams455d6442013-02-05 19:20:18 -08001062 if (data) {
1063 _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
1064 }
1065}
1066
1067
Jason Sams516c3192009-10-06 13:58:47 -07001068
Tim Murray460a0492013-11-19 12:45:54 -08001069static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001070nElementCreate(JNIEnv *_env, jobject _this, jlong con, jlong type, jint kind, jboolean norm,
1071 jint size)
Jason Samsd19f10d2009-05-22 14:03:28 -07001072{
Andreas Gampe67333922014-11-10 20:35:59 -08001073 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001074 ALOGD("nElementCreate, con(%p), type(%" PRId64 "), kind(%i), norm(%i), size(%i)", (RsContext)con,
Andreas Gampe67333922014-11-10 20:35:59 -08001075 type, kind, norm, size);
1076 }
1077 return (jlong)(uintptr_t)rsElementCreate((RsContext)con, (RsDataType)type, (RsDataKind)kind,
Yang Ni8c8daea2016-03-08 21:01:54 +00001078 norm, size);
Jason Samsd19f10d2009-05-22 14:03:28 -07001079}
1080
Tim Murray460a0492013-11-19 12:45:54 -08001081static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001082nElementCreate2(JNIEnv *_env, jobject _this, jlong con,
Ashok Bhat98071552014-02-12 09:54:43 +00001083 jlongArray _ids, jobjectArray _names, jintArray _arraySizes)
Jason Samsd19f10d2009-05-22 14:03:28 -07001084{
Jason Sams718cd1f2009-12-23 14:35:29 -08001085 int fieldCount = _env->GetArrayLength(_ids);
Andreas Gampe67333922014-11-10 20:35:59 -08001086 if (kLogApi) {
1087 ALOGD("nElementCreate2, con(%p)", (RsContext)con);
1088 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001089
Chris Wailes488230c32014-08-14 11:22:40 -07001090 jlong *jIds = _env->GetLongArrayElements(_ids, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001091 if (jIds == nullptr) {
1092 ALOGE("Failed to get Java array elements: ids");
1093 return 0;
1094 }
Chris Wailes488230c32014-08-14 11:22:40 -07001095 jint *jArraySizes = _env->GetIntArrayElements(_arraySizes, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001096 if (jArraySizes == nullptr) {
1097 ALOGE("Failed to get Java array elements: arraySizes");
1098 return 0;
1099 }
Ashok Bhat98071552014-02-12 09:54:43 +00001100
1101 RsElement *ids = (RsElement*)malloc(fieldCount * sizeof(RsElement));
1102 uint32_t *arraySizes = (uint32_t *)malloc(fieldCount * sizeof(uint32_t));
1103
1104 for(int i = 0; i < fieldCount; i ++) {
1105 ids[i] = (RsElement)jIds[i];
1106 arraySizes[i] = (uint32_t)jArraySizes[i];
1107 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001108
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08001109 AutoJavaStringArrayToUTF8 names(_env, _names, fieldCount);
1110
1111 const char **nameArray = names.c_str();
1112 size_t *sizeArray = names.c_str_len();
1113
Tim Murray3aa89c12014-08-18 17:51:22 -07001114 jlong id = (jlong)(uintptr_t)rsElementCreate2((RsContext)con,
Ashok Bhat98071552014-02-12 09:54:43 +00001115 (const RsElement *)ids, fieldCount,
Jason Sams7a22e102011-05-06 14:14:30 -07001116 nameArray, fieldCount * sizeof(size_t), sizeArray,
Yang Ni8c8daea2016-03-08 21:01:54 +00001117 (const uint32_t *)arraySizes, fieldCount);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08001118
Ashok Bhat98071552014-02-12 09:54:43 +00001119 free(ids);
1120 free(arraySizes);
1121 _env->ReleaseLongArrayElements(_ids, jIds, JNI_ABORT);
1122 _env->ReleaseIntArrayElements(_arraySizes, jArraySizes, JNI_ABORT);
1123
Tim Murray3aa89c12014-08-18 17:51:22 -07001124 return (jlong)(uintptr_t)id;
Jason Samsd19f10d2009-05-22 14:03:28 -07001125}
1126
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001127static void
Tim Murray460a0492013-11-19 12:45:54 -08001128nElementGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jintArray _elementData)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001129{
1130 int dataSize = _env->GetArrayLength(_elementData);
Andreas Gampe67333922014-11-10 20:35:59 -08001131 if (kLogApi) {
1132 ALOGD("nElementGetNativeData, con(%p)", (RsContext)con);
1133 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001134
1135 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
1136 assert(dataSize == 5);
1137
Miao Wangcbb02062017-01-24 18:58:17 -08001138 uint32_t elementData[5];
Tim Murrayeff663f2013-11-15 13:08:30 -08001139 rsaElementGetNativeData((RsContext)con, (RsElement)id, elementData, dataSize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001140
1141 for(jint i = 0; i < dataSize; i ++) {
Ashok Bhat98071552014-02-12 09:54:43 +00001142 const jint data = (jint)elementData[i];
1143 _env->SetIntArrayRegion(_elementData, i, 1, &data);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001144 }
1145}
1146
1147
1148static void
Tim Murray460a0492013-11-19 12:45:54 -08001149nElementGetSubElements(JNIEnv *_env, jobject _this, jlong con, jlong id,
Ashok Bhat98071552014-02-12 09:54:43 +00001150 jlongArray _IDs,
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -07001151 jobjectArray _names,
1152 jintArray _arraySizes)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001153{
Ashok Bhat98071552014-02-12 09:54:43 +00001154 uint32_t dataSize = _env->GetArrayLength(_IDs);
Andreas Gampe67333922014-11-10 20:35:59 -08001155 if (kLogApi) {
1156 ALOGD("nElementGetSubElements, con(%p)", (RsContext)con);
1157 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001158
Ashok Bhat98071552014-02-12 09:54:43 +00001159 uintptr_t *ids = (uintptr_t*)malloc(dataSize * sizeof(uintptr_t));
1160 const char **names = (const char **)malloc(dataSize * sizeof(const char *));
Miao Wangcbb02062017-01-24 18:58:17 -08001161 size_t *arraySizes = (size_t *)malloc(dataSize * sizeof(size_t));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001162
Andreas Gampe67333922014-11-10 20:35:59 -08001163 rsaElementGetSubElements((RsContext)con, (RsElement)id, ids, names, arraySizes,
1164 (uint32_t)dataSize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001165
Ashok Bhat98071552014-02-12 09:54:43 +00001166 for(uint32_t i = 0; i < dataSize; i++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07001167 const jlong id = (jlong)(uintptr_t)ids[i];
Ashok Bhat98071552014-02-12 09:54:43 +00001168 const jint arraySize = (jint)arraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001169 _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
Ashok Bhat98071552014-02-12 09:54:43 +00001170 _env->SetLongArrayRegion(_IDs, i, 1, &id);
1171 _env->SetIntArrayRegion(_arraySizes, i, 1, &arraySize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001172 }
1173
1174 free(ids);
1175 free(names);
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -07001176 free(arraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001177}
1178
Jason Samsd19f10d2009-05-22 14:03:28 -07001179// -----------------------------------
1180
Tim Murray460a0492013-11-19 12:45:54 -08001181static jlong
1182nTypeCreate(JNIEnv *_env, jobject _this, jlong con, jlong eid,
Jason Samsb109cc72013-01-07 18:20:12 -08001183 jint dimx, jint dimy, jint dimz, jboolean mips, jboolean faces, jint yuv)
Jason Samsd19f10d2009-05-22 14:03:28 -07001184{
Andreas Gampe67333922014-11-10 20:35:59 -08001185 if (kLogApi) {
1186 ALOGD("nTypeCreate, con(%p) eid(%p), x(%i), y(%i), z(%i), mips(%i), faces(%i), yuv(%i)",
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001187 (RsContext)con, (void*)eid, dimx, dimy, dimz, mips, faces, yuv);
Andreas Gampe67333922014-11-10 20:35:59 -08001188 }
Jason Sams3b9c52a2010-10-14 17:48:46 -07001189
Andreas Gampe67333922014-11-10 20:35:59 -08001190 return (jlong)(uintptr_t)rsTypeCreate((RsContext)con, (RsElement)eid, dimx, dimy, dimz, mips,
Yang Ni8c8daea2016-03-08 21:01:54 +00001191 faces, yuv);
Jason Samsd19f10d2009-05-22 14:03:28 -07001192}
1193
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001194static void
Ashok Bhat98071552014-02-12 09:54:43 +00001195nTypeGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jlongArray _typeData)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001196{
1197 // We are packing 6 items: mDimX; mDimY; mDimZ;
1198 // mDimLOD; mDimFaces; mElement; into typeData
1199 int elementCount = _env->GetArrayLength(_typeData);
1200
1201 assert(elementCount == 6);
Andreas Gampe67333922014-11-10 20:35:59 -08001202 if (kLogApi) {
1203 ALOGD("nTypeGetNativeData, con(%p)", (RsContext)con);
1204 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001205
Ashok Bhat98071552014-02-12 09:54:43 +00001206 uintptr_t typeData[6];
Tim Murrayeff663f2013-11-15 13:08:30 -08001207 rsaTypeGetNativeData((RsContext)con, (RsType)id, typeData, 6);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001208
1209 for(jint i = 0; i < elementCount; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07001210 const jlong data = (jlong)(uintptr_t)typeData[i];
Ashok Bhat98071552014-02-12 09:54:43 +00001211 _env->SetLongArrayRegion(_typeData, i, 1, &data);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001212 }
1213}
1214
Jason Samsd19f10d2009-05-22 14:03:28 -07001215// -----------------------------------
1216
Tim Murray460a0492013-11-19 12:45:54 -08001217static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001218nAllocationCreateTyped(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mips, jint usage,
1219 jlong pointer)
Jason Samsd19f10d2009-05-22 14:03:28 -07001220{
Andreas Gampe67333922014-11-10 20:35:59 -08001221 if (kLogApi) {
1222 ALOGD("nAllocationCreateTyped, con(%p), type(%p), mip(%i), usage(%i), ptr(%p)",
1223 (RsContext)con, (RsElement)type, mips, usage, (void *)pointer);
1224 }
1225 return (jlong)(uintptr_t) rsAllocationCreateTyped((RsContext)con, (RsType)type,
1226 (RsAllocationMipmapControl)mips,
Yang Ni8c8daea2016-03-08 21:01:54 +00001227 (uint32_t)usage, (uintptr_t)pointer);
Jason Samsd19f10d2009-05-22 14:03:28 -07001228}
1229
Jason Samsd19f10d2009-05-22 14:03:28 -07001230static void
Tim Murray460a0492013-11-19 12:45:54 -08001231nAllocationSyncAll(JNIEnv *_env, jobject _this, jlong con, jlong a, jint bits)
Jason Sams5476b452010-12-08 16:14:36 -08001232{
Andreas Gampe67333922014-11-10 20:35:59 -08001233 if (kLogApi) {
1234 ALOGD("nAllocationSyncAll, con(%p), a(%p), bits(0x%08x)", (RsContext)con, (RsAllocation)a,
1235 bits);
1236 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001237 rsAllocationSyncAll((RsContext)con, (RsAllocation)a, (RsAllocationUsageType)bits);
Jason Sams5476b452010-12-08 16:14:36 -08001238}
1239
Miao Wang8c150922015-10-26 17:44:10 -07001240static void
1241nAllocationSetupBufferQueue(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint numAlloc)
1242{
1243 if (kLogApi) {
1244 ALOGD("nAllocationSetupBufferQueue, con(%p), alloc(%p), numAlloc(%d)", (RsContext)con,
1245 (RsAllocation)alloc, numAlloc);
1246 }
1247 rsAllocationSetupBufferQueue((RsContext)con, (RsAllocation)alloc, (uint32_t)numAlloc);
1248}
1249
1250static void
1251nAllocationShareBufferQueue(JNIEnv *_env, jobject _this, jlong con, jlong alloc1, jlong alloc2)
1252{
1253 if (kLogApi) {
1254 ALOGD("nAllocationShareBufferQueue, con(%p), alloc1(%p), alloc2(%p)", (RsContext)con,
1255 (RsAllocation)alloc1, (RsAllocation)alloc2);
1256 }
1257
1258 rsAllocationShareBufferQueue((RsContext)con, (RsAllocation)alloc1, (RsAllocation)alloc2);
1259}
1260
Jason Sams72226e02013-02-22 12:45:54 -08001261static jobject
Tim Murray460a0492013-11-19 12:45:54 -08001262nAllocationGetSurface(JNIEnv *_env, jobject _this, jlong con, jlong a)
Jason Sams615e7ce2012-01-13 14:01:20 -08001263{
Andreas Gampe67333922014-11-10 20:35:59 -08001264 if (kLogApi) {
1265 ALOGD("nAllocationGetSurface, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1266 }
Jason Sams615e7ce2012-01-13 14:01:20 -08001267
Miao Wang1e95fc82017-03-04 16:28:56 -08001268 ANativeWindow *anw = (ANativeWindow *)rsAllocationGetSurface((RsContext)con, (RsAllocation)a);
1269
1270 sp<Surface> surface(static_cast<Surface*>(anw));
1271 sp<IGraphicBufferProducer> bp = surface->getIGraphicBufferProducer();
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001272
Jason Sams72226e02013-02-22 12:45:54 -08001273 jobject o = android_view_Surface_createFromIGraphicBufferProducer(_env, bp);
1274 return o;
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001275}
1276
1277static void
Tim Murray460a0492013-11-19 12:45:54 -08001278nAllocationSetSurface(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject sur)
Jason Sams163766c2012-02-15 12:04:24 -08001279{
Andreas Gampe67333922014-11-10 20:35:59 -08001280 if (kLogApi) {
1281 ALOGD("nAllocationSetSurface, con(%p), alloc(%p), surface(%p)", (RsContext)con,
1282 (RsAllocation)alloc, (Surface *)sur);
1283 }
Jason Sams163766c2012-02-15 12:04:24 -08001284
Miao Wang33287e82017-03-06 09:31:32 -08001285 ANativeWindow *anw = nullptr;
Jason Sams163766c2012-02-15 12:04:24 -08001286 if (sur != 0) {
Miao Wangf35ddc92017-04-03 16:42:03 -07001287 // Connect the native window handle to buffer queue.
Miao Wang33287e82017-03-06 09:31:32 -08001288 anw = ANativeWindow_fromSurface(_env, sur);
Miao Wangf35ddc92017-04-03 16:42:03 -07001289 native_window_api_connect(anw, NATIVE_WINDOW_API_CPU);
Jason Sams163766c2012-02-15 12:04:24 -08001290 }
1291
Miao Wang33287e82017-03-06 09:31:32 -08001292 rsAllocationSetSurface((RsContext)con, (RsAllocation)alloc, anw);
Jason Sams163766c2012-02-15 12:04:24 -08001293}
1294
1295static void
Tim Murray460a0492013-11-19 12:45:54 -08001296nAllocationIoSend(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Sams163766c2012-02-15 12:04:24 -08001297{
Andreas Gampe67333922014-11-10 20:35:59 -08001298 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001299 ALOGD("nAllocationIoSend, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
Andreas Gampe67333922014-11-10 20:35:59 -08001300 }
Tim Murray460a0492013-11-19 12:45:54 -08001301 rsAllocationIoSend((RsContext)con, (RsAllocation)alloc);
Jason Sams163766c2012-02-15 12:04:24 -08001302}
1303
Miao Wang8c150922015-10-26 17:44:10 -07001304static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001305nAllocationIoReceive(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Sams163766c2012-02-15 12:04:24 -08001306{
Andreas Gampe67333922014-11-10 20:35:59 -08001307 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001308 ALOGD("nAllocationIoReceive, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
Andreas Gampe67333922014-11-10 20:35:59 -08001309 }
Miao Wang8c150922015-10-26 17:44:10 -07001310 return (jlong) rsAllocationIoReceive((RsContext)con, (RsAllocation)alloc);
Jason Sams163766c2012-02-15 12:04:24 -08001311}
1312
Jason Sams163766c2012-02-15 12:04:24 -08001313static void
Tim Murray460a0492013-11-19 12:45:54 -08001314nAllocationGenerateMipmaps(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Samsf7086092011-01-12 13:28:37 -08001315{
Andreas Gampe67333922014-11-10 20:35:59 -08001316 if (kLogApi) {
1317 ALOGD("nAllocationGenerateMipmaps, con(%p), a(%p)", (RsContext)con, (RsAllocation)alloc);
1318 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001319 rsAllocationGenerateMipmaps((RsContext)con, (RsAllocation)alloc);
Jason Samsf7086092011-01-12 13:28:37 -08001320}
1321
Derek Sollenbergerdb98b522019-12-27 15:10:50 -05001322static size_t computeByteSize(const android::graphics::Bitmap& bitmap) {
1323 AndroidBitmapInfo info = bitmap.getInfo();
1324 return info.height * info.stride;
1325}
1326
Tim Murray460a0492013-11-19 12:45:54 -08001327static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001328nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
Derek Sollenbergerdb98b522019-12-27 15:10:50 -05001329 jobject jbitmap, jint usage)
Jason Samsffe9f482009-06-01 17:45:53 -07001330{
Derek Sollenbergerdb98b522019-12-27 15:10:50 -05001331 android::graphics::Bitmap bitmap(_env, jbitmap);
Jason Sams5476b452010-12-08 16:14:36 -08001332 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001333 jlong id = (jlong)(uintptr_t)rsAllocationCreateFromBitmap((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001334 (RsType)type, (RsAllocationMipmapControl)mip,
Derek Sollenbergerdb98b522019-12-27 15:10:50 -05001335 ptr, computeByteSize(bitmap), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001336 return id;
Jason Samsffe9f482009-06-01 17:45:53 -07001337}
Jason Samsfe08d992009-05-27 14:45:32 -07001338
Tim Murray460a0492013-11-19 12:45:54 -08001339static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001340nAllocationCreateBitmapBackedAllocation(JNIEnv *_env, jobject _this, jlong con, jlong type,
Derek Sollenbergerdb98b522019-12-27 15:10:50 -05001341 jint mip, jobject jbitmap, jint usage)
Tim Murraya3145512012-12-04 17:59:29 -08001342{
Derek Sollenbergerdb98b522019-12-27 15:10:50 -05001343 android::graphics::Bitmap bitmap(_env, jbitmap);
Tim Murraya3145512012-12-04 17:59:29 -08001344 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001345 jlong id = (jlong)(uintptr_t)rsAllocationCreateTyped((RsContext)con,
Tim Murraya3145512012-12-04 17:59:29 -08001346 (RsType)type, (RsAllocationMipmapControl)mip,
Yang Ni8c8daea2016-03-08 21:01:54 +00001347 (uint32_t)usage, (uintptr_t)ptr);
Tim Murraya3145512012-12-04 17:59:29 -08001348 return id;
1349}
1350
Tim Murray460a0492013-11-19 12:45:54 -08001351static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001352nAllocationCubeCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
Derek Sollenbergerdb98b522019-12-27 15:10:50 -05001353 jobject jbitmap, jint usage)
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001354{
Derek Sollenbergerdb98b522019-12-27 15:10:50 -05001355 android::graphics::Bitmap bitmap(_env, jbitmap);
Jason Sams5476b452010-12-08 16:14:36 -08001356 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001357 jlong id = (jlong)(uintptr_t)rsAllocationCubeCreateFromBitmap((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001358 (RsType)type, (RsAllocationMipmapControl)mip,
Derek Sollenbergerdb98b522019-12-27 15:10:50 -05001359 ptr, computeByteSize(bitmap), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001360 return id;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001361}
1362
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001363static void
Derek Sollenbergerdb98b522019-12-27 15:10:50 -05001364nAllocationCopyFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001365{
Derek Sollenbergerdb98b522019-12-27 15:10:50 -05001366 android::graphics::Bitmap bitmap(_env, jbitmap);
1367 int w = bitmap.getInfo().width;
1368 int h = bitmap.getInfo().height;
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001369
Jason Sams4ef66502010-12-10 16:03:15 -08001370 const void* ptr = bitmap.getPixels();
Tim Murrayeff663f2013-11-15 13:08:30 -08001371 rsAllocation2DData((RsContext)con, (RsAllocation)alloc, 0, 0,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001372 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Derek Sollenbergerdb98b522019-12-27 15:10:50 -05001373 w, h, ptr, computeByteSize(bitmap), 0);
Jason Sams4ef66502010-12-10 16:03:15 -08001374}
1375
1376static void
Derek Sollenbergerdb98b522019-12-27 15:10:50 -05001377nAllocationCopyToBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Jason Sams4ef66502010-12-10 16:03:15 -08001378{
Derek Sollenbergerdb98b522019-12-27 15:10:50 -05001379 android::graphics::Bitmap bitmap(_env, jbitmap);
Jason Sams4ef66502010-12-10 16:03:15 -08001380 void* ptr = bitmap.getPixels();
Derek Sollenbergerdb98b522019-12-27 15:10:50 -05001381 rsAllocationCopyToBitmap((RsContext)con, (RsAllocation)alloc, ptr, computeByteSize(bitmap));
Alex Sakhartchouk835b8542011-07-20 14:33:10 -07001382 bitmap.notifyPixelsChanged();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001383}
1384
Stephen Hines414fa2c2014-04-17 01:02:42 -07001385// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Samsd19f10d2009-05-22 14:03:28 -07001386static void
Tim Murray460a0492013-11-19 12:45:54 -08001387nAllocationData1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001388 jint count, jobject data, jint sizeBytes, jint dataType, jint mSize,
1389 jboolean usePadding)
Jason Samsd19f10d2009-05-22 14:03:28 -07001390{
Jason Samse729a942013-11-06 11:22:02 -08001391 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001392 if (kLogApi) {
1393 ALOGD("nAllocation1DData, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1394 "dataType(%i)", (RsContext)con, (RsAllocation)alloc, offset, count, sizeBytes,
1395 dataType);
1396 }
Miao Wang87e908d2015-03-02 15:15:15 -08001397 PER_ARRAY_TYPE(nullptr, rsAllocation1DData, true,
1398 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -07001399}
1400
1401static void
Miao Wangc8e237e2015-02-20 18:36:32 -08001402nAllocationElementData(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1403 jint xoff, jint yoff, jint zoff,
1404 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Jason Sams49bdaf02010-08-31 13:50:42 -07001405{
Andreas Gampe67333922014-11-10 20:35:59 -08001406 if (kLogApi) {
Yang Ni86c5c2d2016-03-25 15:49:07 -07001407 jint len = _env->GetArrayLength(data);
Miao Wangc8e237e2015-02-20 18:36:32 -08001408 ALOGD("nAllocationElementData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1409 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
Andreas Gampe67333922014-11-10 20:35:59 -08001410 sizeBytes);
1411 }
Chris Wailes488230c32014-08-14 11:22:40 -07001412 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001413 if (ptr == nullptr) {
1414 ALOGE("Failed to get Java array elements");
1415 return;
1416 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001417 rsAllocationElementData((RsContext)con, (RsAllocation)alloc,
1418 xoff, yoff, zoff,
1419 lod, ptr, sizeBytes, compIdx);
Jason Sams49bdaf02010-08-31 13:50:42 -07001420 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1421}
1422
Miao Wangc8e237e2015-02-20 18:36:32 -08001423
Stephen Hines414fa2c2014-04-17 01:02:42 -07001424// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Sams49bdaf02010-08-31 13:50:42 -07001425static void
Tim Murray460a0492013-11-19 12:45:54 -08001426nAllocationData2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001427 jint w, jint h, jobject data, jint sizeBytes, jint dataType, jint mSize,
1428 jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001429{
Jason Samse729a942013-11-06 11:22:02 -08001430 RsAllocation *alloc = (RsAllocation *)_alloc;
1431 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001432 if (kLogApi) {
1433 ALOGD("nAllocation2DData, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1434 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1435 }
Miao Wang87e908d2015-03-02 15:15:15 -08001436 int count = w * h;
1437 PER_ARRAY_TYPE(nullptr, rsAllocation2DData, true,
1438 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Samsd19f10d2009-05-22 14:03:28 -07001439}
1440
Stephen Hines414fa2c2014-04-17 01:02:42 -07001441// Copies from the Allocation pointed to by srcAlloc into the Allocation
1442// pointed to by dstAlloc.
Jason Sams40a29e82009-08-10 14:55:26 -07001443static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001444nAllocationData2D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001445 jlong dstAlloc, jint dstXoff, jint dstYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001446 jint dstMip, jint dstFace,
1447 jint width, jint height,
Tim Murray460a0492013-11-19 12:45:54 -08001448 jlong srcAlloc, jint srcXoff, jint srcYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001449 jint srcMip, jint srcFace)
1450{
Andreas Gampe67333922014-11-10 20:35:59 -08001451 if (kLogApi) {
1452 ALOGD("nAllocation2DData_s, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1453 " dstMip(%i), dstFace(%i), width(%i), height(%i),"
1454 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i), srcFace(%i)",
1455 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip, dstFace,
1456 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
1457 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001458
Tim Murrayeff663f2013-11-15 13:08:30 -08001459 rsAllocationCopy2DRange((RsContext)con,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001460 (RsAllocation)dstAlloc,
1461 dstXoff, dstYoff,
1462 dstMip, dstFace,
1463 width, height,
1464 (RsAllocation)srcAlloc,
1465 srcXoff, srcYoff,
1466 srcMip, srcFace);
1467}
1468
Stephen Hines414fa2c2014-04-17 01:02:42 -07001469// Copies from the Java object data into the Allocation pointed to by _alloc.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001470static void
Tim Murray460a0492013-11-19 12:45:54 -08001471nAllocationData3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001472 jint w, jint h, jint d, jobject data, jint sizeBytes, jint dataType,
1473 jint mSize, jboolean usePadding)
Jason Samsb05d6892013-04-09 15:59:24 -07001474{
Jason Samse729a942013-11-06 11:22:02 -08001475 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001476 if (kLogApi) {
1477 ALOGD("nAllocation3DData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1478 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1479 lod, w, h, d, sizeBytes);
1480 }
Miao Wang87e908d2015-03-02 15:15:15 -08001481 int count = w * h * d;
1482 PER_ARRAY_TYPE(nullptr, rsAllocation3DData, true,
1483 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Jason Samsb05d6892013-04-09 15:59:24 -07001484}
1485
Stephen Hines414fa2c2014-04-17 01:02:42 -07001486// Copies from the Allocation pointed to by srcAlloc into the Allocation
1487// pointed to by dstAlloc.
Jason Samsb05d6892013-04-09 15:59:24 -07001488static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001489nAllocationData3D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001490 jlong dstAlloc, jint dstXoff, jint dstYoff, jint dstZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001491 jint dstMip,
1492 jint width, jint height, jint depth,
Tim Murray460a0492013-11-19 12:45:54 -08001493 jlong srcAlloc, jint srcXoff, jint srcYoff, jint srcZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001494 jint srcMip)
1495{
Andreas Gampe67333922014-11-10 20:35:59 -08001496 if (kLogApi) {
1497 ALOGD("nAllocationData3D_alloc, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1498 " dstMip(%i), width(%i), height(%i),"
1499 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i)",
1500 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip,
1501 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip);
1502 }
Jason Samsb05d6892013-04-09 15:59:24 -07001503
Tim Murrayeff663f2013-11-15 13:08:30 -08001504 rsAllocationCopy3DRange((RsContext)con,
Jason Samsb05d6892013-04-09 15:59:24 -07001505 (RsAllocation)dstAlloc,
1506 dstXoff, dstYoff, dstZoff, dstMip,
1507 width, height, depth,
1508 (RsAllocation)srcAlloc,
1509 srcXoff, srcYoff, srcZoff, srcMip);
1510}
1511
Jason Sams21659ac2013-11-06 15:08:07 -08001512
Stephen Hines414fa2c2014-04-17 01:02:42 -07001513// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsb05d6892013-04-09 15:59:24 -07001514static void
Miao Wang87e908d2015-03-02 15:15:15 -08001515nAllocationRead(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jobject data, jint dataType,
1516 jint mSize, jboolean usePadding)
Jason Sams40a29e82009-08-10 14:55:26 -07001517{
Jason Sams21659ac2013-11-06 15:08:07 -08001518 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001519 if (kLogApi) {
1520 ALOGD("nAllocationRead, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
1521 }
Miao Wang87e908d2015-03-02 15:15:15 -08001522 int count = 0;
1523 PER_ARRAY_TYPE(0, rsAllocationRead, false,
1524 (RsContext)con, alloc, ptr, len * typeBytes);
Jason Sams40a29e82009-08-10 14:55:26 -07001525}
1526
Stephen Hines414fa2c2014-04-17 01:02:42 -07001527// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Sams40a29e82009-08-10 14:55:26 -07001528static void
Tim Murray460a0492013-11-19 12:45:54 -08001529nAllocationRead1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001530 jint count, jobject data, jint sizeBytes, jint dataType,
1531 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001532{
Jason Sams21659ac2013-11-06 15:08:07 -08001533 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001534 if (kLogApi) {
1535 ALOGD("nAllocation1DRead, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1536 "dataType(%i)", (RsContext)con, alloc, offset, count, sizeBytes, dataType);
1537 }
Miao Wang87e908d2015-03-02 15:15:15 -08001538 PER_ARRAY_TYPE(0, rsAllocation1DRead, false,
1539 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsfb9f82c2011-01-12 14:53:25 -08001540}
1541
Miao Wangc8e237e2015-02-20 18:36:32 -08001542// Copies from the Element in the Allocation pointed to by _alloc into the Java array data.
1543static void
Miao Wang45cec0a2015-03-04 16:40:21 -08001544nAllocationElementRead(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
Miao Wangc8e237e2015-02-20 18:36:32 -08001545 jint xoff, jint yoff, jint zoff,
Miao Wang45cec0a2015-03-04 16:40:21 -08001546 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Miao Wangc8e237e2015-02-20 18:36:32 -08001547{
Miao Wangc8e237e2015-02-20 18:36:32 -08001548 if (kLogApi) {
Yang Ni86c5c2d2016-03-25 15:49:07 -07001549 jint len = _env->GetArrayLength(data);
Miao Wang45cec0a2015-03-04 16:40:21 -08001550 ALOGD("nAllocationElementRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1551 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
1552 sizeBytes);
Miao Wangc8e237e2015-02-20 18:36:32 -08001553 }
Miao Wang45cec0a2015-03-04 16:40:21 -08001554 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001555 if (ptr == nullptr) {
1556 ALOGE("Failed to get Java array elements");
1557 return;
1558 }
Miao Wang45cec0a2015-03-04 16:40:21 -08001559 rsAllocationElementRead((RsContext)con, (RsAllocation)alloc,
1560 xoff, yoff, zoff,
Jason Samsa7e25092015-03-11 11:00:00 -07001561 lod, ptr, sizeBytes, compIdx);
Miao Wangbfa5e652015-05-04 15:29:25 -07001562 _env->ReleaseByteArrayElements(data, ptr, 0);
Miao Wangc8e237e2015-02-20 18:36:32 -08001563}
1564
Stephen Hines414fa2c2014-04-17 01:02:42 -07001565// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsfb9f82c2011-01-12 14:53:25 -08001566static void
Tim Murray460a0492013-11-19 12:45:54 -08001567nAllocationRead2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001568 jint w, jint h, jobject data, jint sizeBytes, jint dataType,
1569 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001570{
Jason Sams21659ac2013-11-06 15:08:07 -08001571 RsAllocation *alloc = (RsAllocation *)_alloc;
1572 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001573 if (kLogApi) {
1574 ALOGD("nAllocation2DRead, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1575 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1576 }
Miao Wang87e908d2015-03-02 15:15:15 -08001577 int count = w * h;
1578 PER_ARRAY_TYPE(0, rsAllocation2DRead, false,
1579 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Sams40a29e82009-08-10 14:55:26 -07001580}
Miao Wang87e908d2015-03-02 15:15:15 -08001581
Miao Wangc8e237e2015-02-20 18:36:32 -08001582// Copies from the Allocation pointed to by _alloc into the Java object data.
1583static void
1584nAllocationRead3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001585 jint w, jint h, jint d, jobject data, int sizeBytes, int dataType,
1586 jint mSize, jboolean usePadding)
Miao Wangc8e237e2015-02-20 18:36:32 -08001587{
1588 RsAllocation *alloc = (RsAllocation *)_alloc;
1589 if (kLogApi) {
1590 ALOGD("nAllocation3DRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1591 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1592 lod, w, h, d, sizeBytes);
1593 }
Miao Wang87e908d2015-03-02 15:15:15 -08001594 int count = w * h * d;
1595 PER_ARRAY_TYPE(nullptr, rsAllocation3DRead, false,
1596 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Miao Wangc8e237e2015-02-20 18:36:32 -08001597}
Jason Samsd19f10d2009-05-22 14:03:28 -07001598
Tim Murray460a0492013-11-19 12:45:54 -08001599static jlong
1600nAllocationGetType(JNIEnv *_env, jobject _this, jlong con, jlong a)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001601{
Andreas Gampe67333922014-11-10 20:35:59 -08001602 if (kLogApi) {
1603 ALOGD("nAllocationGetType, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1604 }
Tim Murray3aa89c12014-08-18 17:51:22 -07001605 return (jlong)(uintptr_t) rsaAllocationGetType((RsContext)con, (RsAllocation)a);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001606}
1607
Jason Sams5edc6082010-10-05 13:32:49 -07001608static void
Tim Murray460a0492013-11-19 12:45:54 -08001609nAllocationResize1D(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint dimX)
Jason Sams5edc6082010-10-05 13:32:49 -07001610{
Andreas Gampe67333922014-11-10 20:35:59 -08001611 if (kLogApi) {
1612 ALOGD("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", (RsContext)con,
1613 (RsAllocation)alloc, dimX);
1614 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001615 rsAllocationResize1D((RsContext)con, (RsAllocation)alloc, dimX);
Jason Sams5edc6082010-10-05 13:32:49 -07001616}
1617
Jason Sams46ba27e32015-02-06 17:45:15 -08001618
1619static jlong
1620nAllocationAdapterCreate(JNIEnv *_env, jobject _this, jlong con, jlong basealloc, jlong type)
1621{
1622 if (kLogApi) {
1623 ALOGD("nAllocationAdapterCreate, con(%p), base(%p), type(%p)",
1624 (RsContext)con, (RsAllocation)basealloc, (RsElement)type);
1625 }
1626 return (jlong)(uintptr_t) rsAllocationAdapterCreate((RsContext)con, (RsType)type,
1627 (RsAllocation)basealloc);
1628
1629}
1630
1631static void
1632nAllocationAdapterOffset(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1633 jint x, jint y, jint z, jint face, jint lod,
1634 jint a1, jint a2, jint a3, jint a4)
1635{
1636 uint32_t params[] = {
1637 (uint32_t)x, (uint32_t)y, (uint32_t)z, (uint32_t)face,
1638 (uint32_t)lod, (uint32_t)a1, (uint32_t)a2, (uint32_t)a3, (uint32_t)a4
1639 };
1640 if (kLogApi) {
1641 ALOGD("nAllocationAdapterOffset, con(%p), alloc(%p), x(%i), y(%i), z(%i), face(%i), lod(%i), arrays(%i %i %i %i)",
1642 (RsContext)con, (RsAllocation)alloc, x, y, z, face, lod, a1, a2, a3, a4);
1643 }
1644 rsAllocationAdapterOffset((RsContext)con, (RsAllocation)alloc,
1645 params, sizeof(params));
1646}
1647
1648
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001649// -----------------------------------
1650
Tim Murray460a0492013-11-19 12:45:54 -08001651static jlong
1652nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con, jlong native_asset)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001653{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001654 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001655 ALOGV("______nFileA3D %p", asset);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001656
Tim Murray3aa89c12014-08-18 17:51:22 -07001657 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromMemory((RsContext)con, asset->getBuffer(false), asset->getLength());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001658 return id;
1659}
1660
Tim Murray460a0492013-11-19 12:45:54 -08001661static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001662nFileA3DCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001663{
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001664 Guarded<AssetManager2>* mgr = AssetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001665 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001666 return 0;
1667 }
1668
1669 AutoJavaStringToUTF8 str(_env, _path);
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001670 std::unique_ptr<Asset> asset;
1671 {
1672 ScopedLock<AssetManager2> locked_mgr(*mgr);
1673 asset = locked_mgr->Open(str.c_str(), Asset::ACCESS_BUFFER);
1674 if (asset == nullptr) {
1675 return 0;
1676 }
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001677 }
1678
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001679 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromAsset((RsContext)con, asset.release());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001680 return id;
1681}
1682
Tim Murray460a0492013-11-19 12:45:54 -08001683static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001684nFileA3DCreateFromFile(JNIEnv *_env, jobject _this, jlong con, jstring fileName)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001685{
1686 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001687 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromFile((RsContext)con, fileNameUTF.c_str());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001688
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001689 return id;
1690}
1691
Tim Murray460a0492013-11-19 12:45:54 -08001692static jint
1693nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001694{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001695 int32_t numEntries = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001696 rsaFileA3DGetNumIndexEntries((RsContext)con, &numEntries, (RsFile)fileA3D);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001697 return (jint)numEntries;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001698}
1699
1700static void
Tim Murray460a0492013-11-19 12:45:54 -08001701nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001702{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001703 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001704 RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
1705
Tim Murrayeff663f2013-11-15 13:08:30 -08001706 rsaFileA3DGetIndexEntries((RsContext)con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001707
1708 for(jint i = 0; i < numEntries; i ++) {
1709 _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
1710 _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
1711 }
1712
1713 free(fileEntries);
1714}
1715
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001716static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001717nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint index)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001718{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001719 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Tim Murray3aa89c12014-08-18 17:51:22 -07001720 jlong id = (jlong)(uintptr_t)rsaFileA3DGetEntryByIndex((RsContext)con, (uint32_t)index, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001721 return id;
1722}
Jason Samsd19f10d2009-05-22 14:03:28 -07001723
1724// -----------------------------------
1725
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001726static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001727nFontCreateFromFile(JNIEnv *_env, jobject _this, jlong con,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001728 jstring fileName, jfloat fontSize, jint dpi)
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001729{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001730 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001731 jlong id = (jlong)(uintptr_t)rsFontCreateFromFile((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001732 fileNameUTF.c_str(), fileNameUTF.length(),
1733 fontSize, dpi);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001734
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001735 return id;
1736}
1737
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001738static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001739nFontCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con,
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001740 jstring name, jfloat fontSize, jint dpi, jlong native_asset)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001741{
1742 Asset* asset = reinterpret_cast<Asset*>(native_asset);
1743 AutoJavaStringToUTF8 nameUTF(_env, name);
1744
Tim Murray3aa89c12014-08-18 17:51:22 -07001745 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001746 nameUTF.c_str(), nameUTF.length(),
1747 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001748 asset->getBuffer(false), asset->getLength());
1749 return id;
1750}
1751
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001752static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001753nFontCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001754 jfloat fontSize, jint dpi)
1755{
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001756 Guarded<AssetManager2>* mgr = AssetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001757 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001758 return 0;
1759 }
1760
1761 AutoJavaStringToUTF8 str(_env, _path);
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001762 std::unique_ptr<Asset> asset;
1763 {
1764 ScopedLock<AssetManager2> locked_mgr(*mgr);
1765 asset = locked_mgr->Open(str.c_str(), Asset::ACCESS_BUFFER);
1766 if (asset == nullptr) {
1767 return 0;
1768 }
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001769 }
1770
Tim Murray3aa89c12014-08-18 17:51:22 -07001771 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001772 str.c_str(), str.length(),
1773 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001774 asset->getBuffer(false), asset->getLength());
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001775 return id;
1776}
1777
Jason Samsbd1c3ad2009-08-03 16:03:08 -07001778// -----------------------------------
1779
1780static void
Tim Murray460a0492013-11-19 12:45:54 -08001781nScriptBindAllocation(JNIEnv *_env, jobject _this, jlong con, jlong script, jlong alloc, jint slot)
Jason Samsd19f10d2009-05-22 14:03:28 -07001782{
Andreas Gampe67333922014-11-10 20:35:59 -08001783 if (kLogApi) {
1784 ALOGD("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", (RsContext)con,
1785 (RsScript)script, (RsAllocation)alloc, slot);
1786 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001787 rsScriptBindAllocation((RsContext)con, (RsScript)script, (RsAllocation)alloc, slot);
Jason Samsd19f10d2009-05-22 14:03:28 -07001788}
1789
1790static void
Tim Murray460a0492013-11-19 12:45:54 -08001791nScriptSetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jint val)
Jason Sams4d339932010-05-11 14:03:58 -07001792{
Andreas Gampe67333922014-11-10 20:35:59 -08001793 if (kLogApi) {
1794 ALOGD("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", (RsContext)con, (void *)script,
1795 slot, val);
1796 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001797 rsScriptSetVarI((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001798}
1799
Tim Murray7c4caad2013-04-10 16:21:40 -07001800static jint
Tim Murray460a0492013-11-19 12:45:54 -08001801nScriptGetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001802{
Andreas Gampe67333922014-11-10 20:35:59 -08001803 if (kLogApi) {
1804 ALOGD("nScriptGetVarI, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1805 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001806 int value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001807 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001808 return value;
1809}
1810
Jason Sams4d339932010-05-11 14:03:58 -07001811static void
Tim Murray460a0492013-11-19 12:45:54 -08001812nScriptSetVarObj(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001813{
Andreas Gampe67333922014-11-10 20:35:59 -08001814 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001815 ALOGD("nScriptSetVarObj, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001816 slot, val);
1817 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001818 rsScriptSetVarObj((RsContext)con, (RsScript)script, slot, (RsObjectBase)val);
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001819}
1820
1821static void
Tim Murray460a0492013-11-19 12:45:54 -08001822nScriptSetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Stephen Hines031ec58c2010-10-11 10:54:21 -07001823{
Andreas Gampe67333922014-11-10 20:35:59 -08001824 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001825 ALOGD("nScriptSetVarJ, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001826 slot, val);
1827 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001828 rsScriptSetVarJ((RsContext)con, (RsScript)script, slot, val);
Stephen Hines031ec58c2010-10-11 10:54:21 -07001829}
1830
Tim Murray7c4caad2013-04-10 16:21:40 -07001831static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001832nScriptGetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001833{
Andreas Gampe67333922014-11-10 20:35:59 -08001834 if (kLogApi) {
1835 ALOGD("nScriptGetVarJ, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1836 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001837 jlong value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001838 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001839 return value;
1840}
1841
Stephen Hines031ec58c2010-10-11 10:54:21 -07001842static void
Tim Murray460a0492013-11-19 12:45:54 -08001843nScriptSetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, float val)
Jason Sams4d339932010-05-11 14:03:58 -07001844{
Andreas Gampe67333922014-11-10 20:35:59 -08001845 if (kLogApi) {
1846 ALOGD("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", (RsContext)con, (void *)script,
1847 slot, val);
1848 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001849 rsScriptSetVarF((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001850}
1851
Tim Murray7c4caad2013-04-10 16:21:40 -07001852static jfloat
Tim Murray460a0492013-11-19 12:45:54 -08001853nScriptGetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001854{
Andreas Gampe67333922014-11-10 20:35:59 -08001855 if (kLogApi) {
1856 ALOGD("nScriptGetVarF, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1857 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001858 jfloat value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001859 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001860 return value;
1861}
1862
Jason Sams4d339932010-05-11 14:03:58 -07001863static void
Tim Murray460a0492013-11-19 12:45:54 -08001864nScriptSetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, double val)
Stephen Hinesca54ec32010-09-20 17:20:30 -07001865{
Andreas Gampe67333922014-11-10 20:35:59 -08001866 if (kLogApi) {
1867 ALOGD("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", (RsContext)con, (void *)script,
1868 slot, val);
1869 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001870 rsScriptSetVarD((RsContext)con, (RsScript)script, slot, val);
Stephen Hinesca54ec32010-09-20 17:20:30 -07001871}
1872
Tim Murray7c4caad2013-04-10 16:21:40 -07001873static jdouble
Tim Murray460a0492013-11-19 12:45:54 -08001874nScriptGetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001875{
Andreas Gampe67333922014-11-10 20:35:59 -08001876 if (kLogApi) {
1877 ALOGD("nScriptGetVarD, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1878 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001879 jdouble value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001880 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001881 return value;
1882}
1883
Stephen Hinesca54ec32010-09-20 17:20:30 -07001884static void
Tim Murray460a0492013-11-19 12:45:54 -08001885nScriptSetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001886{
Andreas Gampe67333922014-11-10 20:35:59 -08001887 if (kLogApi) {
1888 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1889 }
Jason Sams4d339932010-05-11 14:03:58 -07001890 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001891 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001892 if (ptr == nullptr) {
1893 ALOGE("Failed to get Java array elements");
1894 return;
1895 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001896 rsScriptSetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001897 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1898}
1899
Stephen Hinesadeb8092012-04-20 14:26:06 -07001900static void
Tim Murray460a0492013-11-19 12:45:54 -08001901nScriptGetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Tim Murray7c4caad2013-04-10 16:21:40 -07001902{
Andreas Gampe67333922014-11-10 20:35:59 -08001903 if (kLogApi) {
1904 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1905 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001906 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001907 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001908 if (ptr == nullptr) {
1909 ALOGE("Failed to get Java array elements");
1910 return;
1911 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001912 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Stephen Hines414fa2c2014-04-17 01:02:42 -07001913 _env->ReleaseByteArrayElements(data, ptr, 0);
Tim Murray7c4caad2013-04-10 16:21:40 -07001914}
1915
1916static void
Andreas Gampe67333922014-11-10 20:35:59 -08001917nScriptSetVarVE(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data,
1918 jlong elem, jintArray dims)
Stephen Hinesadeb8092012-04-20 14:26:06 -07001919{
Andreas Gampe67333922014-11-10 20:35:59 -08001920 if (kLogApi) {
1921 ALOGD("nScriptSetVarVE, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1922 }
Stephen Hinesadeb8092012-04-20 14:26:06 -07001923 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001924 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001925 if (ptr == nullptr) {
1926 ALOGE("Failed to get Java array elements");
1927 return;
1928 }
Stephen Hinesadeb8092012-04-20 14:26:06 -07001929 jint dimsLen = _env->GetArrayLength(dims) * sizeof(int);
Chris Wailes488230c32014-08-14 11:22:40 -07001930 jint *dimsPtr = _env->GetIntArrayElements(dims, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001931 if (dimsPtr == nullptr) {
1932 ALOGE("Failed to get Java array elements");
1933 return;
1934 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001935 rsScriptSetVarVE((RsContext)con, (RsScript)script, slot, ptr, len, (RsElement)elem,
Stephen Hinesbc5d3ee2014-06-25 00:03:39 -07001936 (const uint32_t*) dimsPtr, dimsLen);
Stephen Hinesadeb8092012-04-20 14:26:06 -07001937 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1938 _env->ReleaseIntArrayElements(dims, dimsPtr, JNI_ABORT);
1939}
1940
Jason Samsd19f10d2009-05-22 14:03:28 -07001941
1942static void
Tim Murray460a0492013-11-19 12:45:54 -08001943nScriptSetTimeZone(JNIEnv *_env, jobject _this, jlong con, jlong script, jbyteArray timeZone)
Jason Samsd19f10d2009-05-22 14:03:28 -07001944{
Andreas Gampe67333922014-11-10 20:35:59 -08001945 if (kLogApi) {
1946 ALOGD("nScriptCSetTimeZone, con(%p), s(%p)", (RsContext)con, (void *)script);
1947 }
Romain Guy584a3752009-07-30 18:45:01 -07001948
1949 jint length = _env->GetArrayLength(timeZone);
1950 jbyte* timeZone_ptr;
1951 timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
Miao Wangba8766c2015-10-12 17:24:13 -07001952 if (timeZone_ptr == nullptr) {
1953 ALOGE("Failed to get Java array elements");
1954 return;
1955 }
Romain Guy584a3752009-07-30 18:45:01 -07001956
Tim Murrayeff663f2013-11-15 13:08:30 -08001957 rsScriptSetTimeZone((RsContext)con, (RsScript)script, (const char *)timeZone_ptr, length);
Romain Guy584a3752009-07-30 18:45:01 -07001958
1959 if (timeZone_ptr) {
1960 _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
1961 }
1962}
1963
Jason Samsfbf0b9e2009-08-13 12:59:04 -07001964static void
Tim Murray460a0492013-11-19 12:45:54 -08001965nScriptInvoke(JNIEnv *_env, jobject _this, jlong con, jlong obj, jint slot)
Jason Samsbe2e8412009-09-16 15:04:38 -07001966{
Andreas Gampe67333922014-11-10 20:35:59 -08001967 if (kLogApi) {
1968 ALOGD("nScriptInvoke, con(%p), script(%p)", (RsContext)con, (void *)obj);
1969 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001970 rsScriptInvoke((RsContext)con, (RsScript)obj, slot);
Jason Samsbe2e8412009-09-16 15:04:38 -07001971}
1972
1973static void
Tim Murray460a0492013-11-19 12:45:54 -08001974nScriptInvokeV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001975{
Andreas Gampe67333922014-11-10 20:35:59 -08001976 if (kLogApi) {
1977 ALOGD("nScriptInvokeV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1978 }
Jason Sams4d339932010-05-11 14:03:58 -07001979 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001980 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001981 if (ptr == nullptr) {
1982 ALOGE("Failed to get Java array elements");
1983 return;
1984 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001985 rsScriptInvokeV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001986 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1987}
1988
Jason Sams6e494d32011-04-27 16:33:11 -07001989static void
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001990nScriptForEach(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
1991 jlongArray ains, jlong aout, jbyteArray params,
1992 jintArray limits)
Jason Sams6e494d32011-04-27 16:33:11 -07001993{
Andreas Gampe67333922014-11-10 20:35:59 -08001994 if (kLogApi) {
Chih-Hung Hsieh9eb9dd32015-05-06 14:42:04 -07001995 ALOGD("nScriptForEach, con(%p), s(%p), slot(%i) ains(%p) aout(%" PRId64 ")", (RsContext)con, (void *)script, slot, ains, aout);
Andreas Gampe67333922014-11-10 20:35:59 -08001996 }
Jason Sams6e494d32011-04-27 16:33:11 -07001997
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001998 jint in_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07001999 jlong *in_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002000
Chris Wailes488230c32014-08-14 11:22:40 -07002001 RsAllocation *in_allocs = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002002
Chris Wailes488230c32014-08-14 11:22:40 -07002003 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002004 in_len = _env->GetArrayLength(ains);
Yang Ni7b2a46f2015-05-05 12:41:19 -07002005 if (in_len > (jint)RS_KERNEL_MAX_ARGUMENTS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -07002006 ALOGE("Too many arguments in kernel launch.");
2007 // TODO (b/20758983): Report back to Java and throw an exception
2008 return;
2009 }
Chris Wailes94961062014-06-11 12:01:28 -07002010
Yang Ni17c2d7a2015-04-30 16:13:54 -07002011 in_ptr = _env->GetLongArrayElements(ains, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002012 if (in_ptr == nullptr) {
2013 ALOGE("Failed to get Java array elements");
2014 return;
2015 }
2016
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002017 if (sizeof(RsAllocation) == sizeof(jlong)) {
2018 in_allocs = (RsAllocation*)in_ptr;
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002019 } else {
2020 // Convert from 64-bit jlong types to the native pointer type.
Chris Wailes94961062014-06-11 12:01:28 -07002021
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002022 in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
Yang Ni17c2d7a2015-04-30 16:13:54 -07002023 if (in_allocs == nullptr) {
2024 ALOGE("Failed launching kernel for lack of memory.");
2025 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2026 return;
2027 }
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002028
2029 for (int index = in_len; --index >= 0;) {
2030 in_allocs[index] = (RsAllocation)in_ptr[index];
2031 }
2032 }
Chris Wailes94961062014-06-11 12:01:28 -07002033 }
2034
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002035 jint param_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002036 jbyte *param_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002037
Chris Wailes488230c32014-08-14 11:22:40 -07002038 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002039 param_len = _env->GetArrayLength(params);
Chris Wailes488230c32014-08-14 11:22:40 -07002040 param_ptr = _env->GetByteArrayElements(params, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002041 if (param_ptr == nullptr) {
2042 ALOGE("Failed to get Java array elements");
2043 return;
2044 }
Chris Wailes94961062014-06-11 12:01:28 -07002045 }
2046
Chris Wailes488230c32014-08-14 11:22:40 -07002047 RsScriptCall sc, *sca = nullptr;
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002048 uint32_t sc_size = 0;
Chris Wailes94961062014-06-11 12:01:28 -07002049
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002050 jint limit_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002051 jint *limit_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002052
Chris Wailes488230c32014-08-14 11:22:40 -07002053 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002054 limit_len = _env->GetArrayLength(limits);
Chris Wailes488230c32014-08-14 11:22:40 -07002055 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002056 if (limit_ptr == nullptr) {
2057 ALOGE("Failed to get Java array elements");
2058 return;
2059 }
Chris Wailes94961062014-06-11 12:01:28 -07002060
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002061 assert(limit_len == 6);
Andreas Gampe67333922014-11-10 20:35:59 -08002062 UNUSED(limit_len); // As the assert might not be compiled.
Chris Wailes94961062014-06-11 12:01:28 -07002063
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002064 sc.xStart = limit_ptr[0];
2065 sc.xEnd = limit_ptr[1];
2066 sc.yStart = limit_ptr[2];
2067 sc.yEnd = limit_ptr[3];
2068 sc.zStart = limit_ptr[4];
2069 sc.zEnd = limit_ptr[5];
2070 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
Jason Sams14331ab2015-01-26 18:14:36 -08002071 sc.arrayStart = 0;
2072 sc.arrayEnd = 0;
2073 sc.array2Start = 0;
2074 sc.array2End = 0;
2075 sc.array3Start = 0;
2076 sc.array3End = 0;
2077 sc.array4Start = 0;
2078 sc.array4End = 0;
Chris Wailes94961062014-06-11 12:01:28 -07002079
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002080 sca = &sc;
Yang Nie8f2e442016-03-15 16:00:02 -07002081 // sc_size is required, but unused, by the runtime and drivers.
2082 sc_size = sizeof(sc);
Chris Wailes94961062014-06-11 12:01:28 -07002083 }
2084
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002085 rsScriptForEachMulti((RsContext)con, (RsScript)script, slot,
2086 in_allocs, in_len, (RsAllocation)aout,
2087 param_ptr, param_len, sca, sc_size);
Chris Wailes94961062014-06-11 12:01:28 -07002088
Chris Wailes488230c32014-08-14 11:22:40 -07002089 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002090 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
Chris Wailes94961062014-06-11 12:01:28 -07002091 }
2092
Chris Wailes488230c32014-08-14 11:22:40 -07002093 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002094 _env->ReleaseByteArrayElements(params, param_ptr, JNI_ABORT);
2095 }
2096
Chris Wailes488230c32014-08-14 11:22:40 -07002097 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002098 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
2099 }
Chris Wailes94961062014-06-11 12:01:28 -07002100}
2101
Matt Wala36eb1f72015-07-20 15:35:27 -07002102static void
2103nScriptReduce(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
David Gross4a457852016-06-02 14:46:55 -07002104 jlongArray ains, jlong aout, jintArray limits)
Matt Wala36eb1f72015-07-20 15:35:27 -07002105{
2106 if (kLogApi) {
David Gross4a457852016-06-02 14:46:55 -07002107 ALOGD("nScriptReduce, con(%p), s(%p), slot(%i) ains(%p) aout(%" PRId64 ")", (RsContext)con, (void *)script, slot, ains, aout);
David Gross26ef7a732016-01-12 12:19:15 -08002108 }
2109
2110 if (ains == nullptr) {
2111 ALOGE("At least one input required.");
2112 // TODO (b/20758983): Report back to Java and throw an exception
2113 return;
2114 }
2115 jint in_len = _env->GetArrayLength(ains);
2116 if (in_len > (jint)RS_KERNEL_MAX_ARGUMENTS) {
2117 ALOGE("Too many arguments in kernel launch.");
2118 // TODO (b/20758983): Report back to Java and throw an exception
2119 return;
2120 }
2121
2122 jlong *in_ptr = _env->GetLongArrayElements(ains, nullptr);
2123 if (in_ptr == nullptr) {
2124 ALOGE("Failed to get Java array elements");
2125 // TODO (b/20758983): Report back to Java and throw an exception
2126 return;
2127 }
2128
2129 RsAllocation *in_allocs = nullptr;
2130 if (sizeof(RsAllocation) == sizeof(jlong)) {
2131 in_allocs = (RsAllocation*)in_ptr;
2132 } else {
2133 // Convert from 64-bit jlong types to the native pointer type.
2134
2135 in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
2136 if (in_allocs == nullptr) {
2137 ALOGE("Failed launching kernel for lack of memory.");
2138 // TODO (b/20758983): Report back to Java and throw an exception
2139 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2140 return;
2141 }
2142
2143 for (int index = in_len; --index >= 0;) {
2144 in_allocs[index] = (RsAllocation)in_ptr[index];
2145 }
2146 }
2147
2148 RsScriptCall sc, *sca = nullptr;
2149 uint32_t sc_size = 0;
2150
2151 jint limit_len = 0;
2152 jint *limit_ptr = nullptr;
2153
2154 if (limits != nullptr) {
2155 limit_len = _env->GetArrayLength(limits);
2156 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
2157 if (limit_ptr == nullptr) {
2158 ALOGE("Failed to get Java array elements");
2159 // TODO (b/20758983): Report back to Java and throw an exception
2160 return;
2161 }
2162
2163 assert(limit_len == 6);
2164 UNUSED(limit_len); // As the assert might not be compiled.
2165
2166 sc.xStart = limit_ptr[0];
2167 sc.xEnd = limit_ptr[1];
2168 sc.yStart = limit_ptr[2];
2169 sc.yEnd = limit_ptr[3];
2170 sc.zStart = limit_ptr[4];
2171 sc.zEnd = limit_ptr[5];
2172 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
2173 sc.arrayStart = 0;
2174 sc.arrayEnd = 0;
2175 sc.array2Start = 0;
2176 sc.array2End = 0;
2177 sc.array3Start = 0;
2178 sc.array3End = 0;
2179 sc.array4Start = 0;
2180 sc.array4End = 0;
2181
2182 sca = &sc;
2183 sc_size = sizeof(sc);
2184 }
2185
David Gross4a457852016-06-02 14:46:55 -07002186 rsScriptReduce((RsContext)con, (RsScript)script, slot,
2187 in_allocs, in_len, (RsAllocation)aout,
2188 sca, sc_size);
David Gross26ef7a732016-01-12 12:19:15 -08002189
2190 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2191
2192 if (limits != nullptr) {
2193 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
2194 }
2195}
2196
Jason Sams22534172009-08-04 16:58:20 -07002197// -----------------------------------
2198
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002199static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002200nScriptCCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Samse4a06c52011-03-16 16:29:28 -07002201 jstring resName, jstring cacheDir,
2202 jbyteArray scriptRef, jint length)
Jason Sams22534172009-08-04 16:58:20 -07002203{
Andreas Gampe67333922014-11-10 20:35:59 -08002204 if (kLogApi) {
2205 ALOGD("nScriptCCreate, con(%p)", (RsContext)con);
2206 }
Jason Sams22534172009-08-04 16:58:20 -07002207
Jason Samse4a06c52011-03-16 16:29:28 -07002208 AutoJavaStringToUTF8 resNameUTF(_env, resName);
2209 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002210 jlong ret = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002211 jbyte* script_ptr = nullptr;
Jack Palevich43702d82009-05-28 13:38:16 -07002212 jint _exception = 0;
2213 jint remaining;
Jack Palevich43702d82009-05-28 13:38:16 -07002214 if (!scriptRef) {
2215 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002216 //jniThrowException(_env, "java/lang/IllegalArgumentException", "script == null");
Jack Palevich43702d82009-05-28 13:38:16 -07002217 goto exit;
2218 }
Jack Palevich43702d82009-05-28 13:38:16 -07002219 if (length < 0) {
2220 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002221 //jniThrowException(_env, "java/lang/IllegalArgumentException", "length < 0");
Jack Palevich43702d82009-05-28 13:38:16 -07002222 goto exit;
2223 }
Jason Samse4a06c52011-03-16 16:29:28 -07002224 remaining = _env->GetArrayLength(scriptRef);
Jack Palevich43702d82009-05-28 13:38:16 -07002225 if (remaining < length) {
2226 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002227 //jniThrowException(_env, "java/lang/IllegalArgumentException",
2228 // "length > script.length - offset");
Jack Palevich43702d82009-05-28 13:38:16 -07002229 goto exit;
2230 }
Jason Samse4a06c52011-03-16 16:29:28 -07002231 script_ptr = (jbyte *)
Jack Palevich43702d82009-05-28 13:38:16 -07002232 _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
Miao Wangba8766c2015-10-12 17:24:13 -07002233 if (script_ptr == nullptr) {
2234 ALOGE("Failed to get Java array elements");
2235 return ret;
2236 }
Jack Palevich43702d82009-05-28 13:38:16 -07002237
Tim Murrayeff663f2013-11-15 13:08:30 -08002238 //rsScriptCSetText((RsContext)con, (const char *)script_ptr, length);
Jason Samse4a06c52011-03-16 16:29:28 -07002239
Tim Murray3aa89c12014-08-18 17:51:22 -07002240 ret = (jlong)(uintptr_t)rsScriptCCreate((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07002241 resNameUTF.c_str(), resNameUTF.length(),
2242 cacheDirUTF.c_str(), cacheDirUTF.length(),
Jason Samse4a06c52011-03-16 16:29:28 -07002243 (const char *)script_ptr, length);
Jason Sams39ddc9502009-06-05 17:35:09 -07002244
Jack Palevich43702d82009-05-28 13:38:16 -07002245exit:
Jason Samse4a06c52011-03-16 16:29:28 -07002246 if (script_ptr) {
2247 _env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr,
Jack Palevich43702d82009-05-28 13:38:16 -07002248 _exception ? JNI_ABORT: 0);
2249 }
Jason Samsd19f10d2009-05-22 14:03:28 -07002250
Tim Murray3aa89c12014-08-18 17:51:22 -07002251 return (jlong)(uintptr_t)ret;
Jason Samsd19f10d2009-05-22 14:03:28 -07002252}
2253
Tim Murray460a0492013-11-19 12:45:54 -08002254static jlong
2255nScriptIntrinsicCreate(JNIEnv *_env, jobject _this, jlong con, jint id, jlong eid)
Jason Sams6ab97682012-08-10 12:09:43 -07002256{
Andreas Gampe67333922014-11-10 20:35:59 -08002257 if (kLogApi) {
2258 ALOGD("nScriptIntrinsicCreate, con(%p) id(%i) element(%p)", (RsContext)con, id,
2259 (void *)eid);
2260 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002261 return (jlong)(uintptr_t)rsScriptIntrinsicCreate((RsContext)con, id, (RsElement)eid);
Jason Sams6ab97682012-08-10 12:09:43 -07002262}
2263
Tim Murray460a0492013-11-19 12:45:54 -08002264static jlong
2265nScriptKernelIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot, jint sig)
Jason Sams08a81582012-09-18 12:32:10 -07002266{
Andreas Gampe67333922014-11-10 20:35:59 -08002267 if (kLogApi) {
2268 ALOGD("nScriptKernelIDCreate, con(%p) script(%p), slot(%i), sig(%i)", (RsContext)con,
2269 (void *)sid, slot, sig);
2270 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002271 return (jlong)(uintptr_t)rsScriptKernelIDCreate((RsContext)con, (RsScript)sid, slot, sig);
Jason Sams08a81582012-09-18 12:32:10 -07002272}
2273
Tim Murray460a0492013-11-19 12:45:54 -08002274static jlong
Yang Nibe392ad2015-01-23 17:16:02 -08002275nScriptInvokeIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
2276{
2277 if (kLogApi) {
Elliott Hughes7ff53fa2015-02-05 21:36:10 -08002278 ALOGD("nScriptInvokeIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con,
Yang Nibe392ad2015-01-23 17:16:02 -08002279 (void *)sid, slot);
2280 }
2281 return (jlong)(uintptr_t)rsScriptInvokeIDCreate((RsContext)con, (RsScript)sid, slot);
2282}
2283
2284static jlong
Tim Murray460a0492013-11-19 12:45:54 -08002285nScriptFieldIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
Jason Sams08a81582012-09-18 12:32:10 -07002286{
Andreas Gampe67333922014-11-10 20:35:59 -08002287 if (kLogApi) {
2288 ALOGD("nScriptFieldIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con, (void *)sid,
2289 slot);
2290 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002291 return (jlong)(uintptr_t)rsScriptFieldIDCreate((RsContext)con, (RsScript)sid, slot);
Jason Sams08a81582012-09-18 12:32:10 -07002292}
2293
Tim Murray460a0492013-11-19 12:45:54 -08002294static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002295nScriptGroupCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _kernels, jlongArray _src,
2296 jlongArray _dstk, jlongArray _dstf, jlongArray _types)
Jason Sams08a81582012-09-18 12:32:10 -07002297{
Andreas Gampe67333922014-11-10 20:35:59 -08002298 if (kLogApi) {
2299 ALOGD("nScriptGroupCreate, con(%p)", (RsContext)con);
2300 }
Jason Sams08a81582012-09-18 12:32:10 -07002301
Miao Wanga4ad5f82016-02-11 12:32:39 -08002302 jlong id = 0;
2303
2304 RsScriptKernelID* kernelsPtr;
Ashok Bhat98071552014-02-12 09:54:43 +00002305 jint kernelsLen = _env->GetArrayLength(_kernels);
Chris Wailes488230c32014-08-14 11:22:40 -07002306 jlong *jKernelsPtr = _env->GetLongArrayElements(_kernels, nullptr);
Miao Wanga4ad5f82016-02-11 12:32:39 -08002307
2308 RsScriptKernelID* srcPtr;
2309 jint srcLen = _env->GetArrayLength(_src);
2310 jlong *jSrcPtr = _env->GetLongArrayElements(_src, nullptr);
2311
2312 RsScriptKernelID* dstkPtr;
2313 jint dstkLen = _env->GetArrayLength(_dstk);
2314 jlong *jDstkPtr = _env->GetLongArrayElements(_dstk, nullptr);
2315
2316 RsScriptKernelID* dstfPtr;
2317 jint dstfLen = _env->GetArrayLength(_dstf);
2318 jlong *jDstfPtr = _env->GetLongArrayElements(_dstf, nullptr);
2319
2320 RsType* typesPtr;
2321 jint typesLen = _env->GetArrayLength(_types);
2322 jlong *jTypesPtr = _env->GetLongArrayElements(_types, nullptr);
2323
Miao Wangba8766c2015-10-12 17:24:13 -07002324 if (jKernelsPtr == nullptr) {
2325 ALOGE("Failed to get Java array elements: kernels");
Miao Wanga4ad5f82016-02-11 12:32:39 -08002326 goto cleanup;
Miao Wangba8766c2015-10-12 17:24:13 -07002327 }
Miao Wanga4ad5f82016-02-11 12:32:39 -08002328 if (jSrcPtr == nullptr) {
2329 ALOGE("Failed to get Java array elements: src");
2330 goto cleanup;
2331 }
2332 if (jDstkPtr == nullptr) {
2333 ALOGE("Failed to get Java array elements: dstk");
2334 goto cleanup;
2335 }
2336 if (jDstfPtr == nullptr) {
2337 ALOGE("Failed to get Java array elements: dstf");
2338 goto cleanup;
2339 }
2340 if (jTypesPtr == nullptr) {
2341 ALOGE("Failed to get Java array elements: types");
2342 goto cleanup;
2343 }
2344
2345 kernelsPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * kernelsLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002346 for(int i = 0; i < kernelsLen; ++i) {
2347 kernelsPtr[i] = (RsScriptKernelID)jKernelsPtr[i];
2348 }
Jason Sams08a81582012-09-18 12:32:10 -07002349
Miao Wanga4ad5f82016-02-11 12:32:39 -08002350 srcPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * srcLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002351 for(int i = 0; i < srcLen; ++i) {
2352 srcPtr[i] = (RsScriptKernelID)jSrcPtr[i];
2353 }
Jason Sams08a81582012-09-18 12:32:10 -07002354
Miao Wanga4ad5f82016-02-11 12:32:39 -08002355 dstkPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstkLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002356 for(int i = 0; i < dstkLen; ++i) {
2357 dstkPtr[i] = (RsScriptKernelID)jDstkPtr[i];
2358 }
2359
Miao Wanga4ad5f82016-02-11 12:32:39 -08002360 dstfPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstfLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002361 for(int i = 0; i < dstfLen; ++i) {
2362 dstfPtr[i] = (RsScriptKernelID)jDstfPtr[i];
2363 }
2364
Miao Wanga4ad5f82016-02-11 12:32:39 -08002365 typesPtr = (RsType*) malloc(sizeof(RsType) * typesLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002366 for(int i = 0; i < typesLen; ++i) {
2367 typesPtr[i] = (RsType)jTypesPtr[i];
2368 }
2369
Miao Wanga4ad5f82016-02-11 12:32:39 -08002370 id = (jlong)(uintptr_t)rsScriptGroupCreate((RsContext)con,
Ashok Bhat98071552014-02-12 09:54:43 +00002371 (RsScriptKernelID *)kernelsPtr, kernelsLen * sizeof(RsScriptKernelID),
2372 (RsScriptKernelID *)srcPtr, srcLen * sizeof(RsScriptKernelID),
2373 (RsScriptKernelID *)dstkPtr, dstkLen * sizeof(RsScriptKernelID),
2374 (RsScriptFieldID *)dstfPtr, dstfLen * sizeof(RsScriptKernelID),
2375 (RsType *)typesPtr, typesLen * sizeof(RsType));
2376
2377 free(kernelsPtr);
2378 free(srcPtr);
2379 free(dstkPtr);
2380 free(dstfPtr);
2381 free(typesPtr);
Miao Wanga4ad5f82016-02-11 12:32:39 -08002382
2383cleanup:
2384 if (jKernelsPtr != nullptr) {
2385 _env->ReleaseLongArrayElements(_kernels, jKernelsPtr, 0);
2386 }
2387 if (jSrcPtr != nullptr) {
2388 _env->ReleaseLongArrayElements(_src, jSrcPtr, 0);
2389 }
2390 if (jDstkPtr != nullptr) {
2391 _env->ReleaseLongArrayElements(_dstk, jDstkPtr, 0);
2392 }
2393 if (jDstfPtr != nullptr) {
2394 _env->ReleaseLongArrayElements(_dstf, jDstfPtr, 0);
2395 }
2396 if (jTypesPtr != nullptr) {
2397 _env->ReleaseLongArrayElements(_types, jTypesPtr, 0);
2398 }
2399
Jason Sams08a81582012-09-18 12:32:10 -07002400 return id;
2401}
2402
2403static void
Tim Murray460a0492013-11-19 12:45:54 -08002404nScriptGroupSetInput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002405{
Andreas Gampe67333922014-11-10 20:35:59 -08002406 if (kLogApi) {
2407 ALOGD("nScriptGroupSetInput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2408 (void *)gid, (void *)kid, (void *)alloc);
2409 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002410 rsScriptGroupSetInput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002411}
2412
2413static void
Tim Murray460a0492013-11-19 12:45:54 -08002414nScriptGroupSetOutput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002415{
Andreas Gampe67333922014-11-10 20:35:59 -08002416 if (kLogApi) {
2417 ALOGD("nScriptGroupSetOutput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2418 (void *)gid, (void *)kid, (void *)alloc);
2419 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002420 rsScriptGroupSetOutput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002421}
2422
2423static void
Tim Murray460a0492013-11-19 12:45:54 -08002424nScriptGroupExecute(JNIEnv *_env, jobject _this, jlong con, jlong gid)
Jason Sams08a81582012-09-18 12:32:10 -07002425{
Andreas Gampe67333922014-11-10 20:35:59 -08002426 if (kLogApi) {
2427 ALOGD("nScriptGroupSetOutput, con(%p) group(%p)", (RsContext)con, (void *)gid);
2428 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002429 rsScriptGroupExecute((RsContext)con, (RsScriptGroup)gid);
Jason Sams08a81582012-09-18 12:32:10 -07002430}
2431
Jason Samsd19f10d2009-05-22 14:03:28 -07002432// ---------------------------------------------------------------------------
2433
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002434static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002435nProgramStoreCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Sams331bf9b2011-04-06 11:23:54 -07002436 jboolean colorMaskR, jboolean colorMaskG, jboolean colorMaskB, jboolean colorMaskA,
2437 jboolean depthMask, jboolean ditherEnable,
2438 jint srcFunc, jint destFunc,
2439 jint depthFunc)
Jason Samsd19f10d2009-05-22 14:03:28 -07002440{
Andreas Gampe67333922014-11-10 20:35:59 -08002441 if (kLogApi) {
2442 ALOGD("nProgramStoreCreate, con(%p)", (RsContext)con);
2443 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002444 return (jlong)(uintptr_t)rsProgramStoreCreate((RsContext)con, colorMaskR, colorMaskG, colorMaskB, colorMaskA,
Jason Sams331bf9b2011-04-06 11:23:54 -07002445 depthMask, ditherEnable, (RsBlendSrcFunc)srcFunc,
2446 (RsBlendDstFunc)destFunc, (RsDepthFunc)depthFunc);
Jason Samsd19f10d2009-05-22 14:03:28 -07002447}
2448
Jason Sams0011bcf2009-12-15 12:58:36 -08002449// ---------------------------------------------------------------------------
2450
2451static void
Tim Murray460a0492013-11-19 12:45:54 -08002452nProgramBindConstants(JNIEnv *_env, jobject _this, jlong con, jlong vpv, jint slot, jlong a)
Jason Sams0011bcf2009-12-15 12:58:36 -08002453{
Andreas Gampe67333922014-11-10 20:35:59 -08002454 if (kLogApi) {
2455 ALOGD("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", (RsContext)con,
2456 (RsProgramVertex)vpv, slot, (RsAllocation)a);
2457 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002458 rsProgramBindConstants((RsContext)con, (RsProgram)vpv, slot, (RsAllocation)a);
Jason Sams0011bcf2009-12-15 12:58:36 -08002459}
Jason Sams54c0ec12009-11-30 14:49:55 -08002460
Jason Sams68afd012009-12-17 16:55:08 -08002461static void
Tim Murray460a0492013-11-19 12:45:54 -08002462nProgramBindTexture(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002463{
Andreas Gampe67333922014-11-10 20:35:59 -08002464 if (kLogApi) {
2465 ALOGD("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2466 (RsProgramFragment)vpf, slot, (RsAllocation)a);
2467 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002468 rsProgramBindTexture((RsContext)con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
Jason Sams68afd012009-12-17 16:55:08 -08002469}
2470
2471static void
Tim Murray460a0492013-11-19 12:45:54 -08002472nProgramBindSampler(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002473{
Andreas Gampe67333922014-11-10 20:35:59 -08002474 if (kLogApi) {
2475 ALOGD("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2476 (RsProgramFragment)vpf, slot, (RsSampler)a);
2477 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002478 rsProgramBindSampler((RsContext)con, (RsProgramFragment)vpf, slot, (RsSampler)a);
Jason Sams68afd012009-12-17 16:55:08 -08002479}
2480
Jason Samsd19f10d2009-05-22 14:03:28 -07002481// ---------------------------------------------------------------------------
2482
Tim Murray460a0492013-11-19 12:45:54 -08002483static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002484nProgramFragmentCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002485 jobjectArray texNames, jlongArray params)
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002486{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002487 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002488 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002489 jint paramLen = _env->GetArrayLength(params);
Miao Wangba8766c2015-10-12 17:24:13 -07002490 if (jParamPtr == nullptr) {
2491 ALOGE("Failed to get Java array elements");
2492 return 0;
2493 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002494
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002495 int texCount = _env->GetArrayLength(texNames);
2496 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2497 const char ** nameArray = names.c_str();
2498 size_t* sizeArray = names.c_str_len();
2499
Andreas Gampe67333922014-11-10 20:35:59 -08002500 if (kLogApi) {
2501 ALOGD("nProgramFragmentCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2502 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002503
Ashok Bhat98071552014-02-12 09:54:43 +00002504 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2505 for(int i = 0; i < paramLen; ++i) {
2506 paramPtr[i] = (uintptr_t)jParamPtr[i];
2507 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002508 jlong ret = (jlong)(uintptr_t)rsProgramFragmentCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002509 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002510 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002511
Ashok Bhat98071552014-02-12 09:54:43 +00002512 free(paramPtr);
2513 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002514 return ret;
2515}
2516
2517
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002518// ---------------------------------------------------------------------------
2519
Tim Murray460a0492013-11-19 12:45:54 -08002520static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002521nProgramVertexCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002522 jobjectArray texNames, jlongArray params)
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002523{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002524 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002525 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams0011bcf2009-12-15 12:58:36 -08002526 jint paramLen = _env->GetArrayLength(params);
Miao Wangba8766c2015-10-12 17:24:13 -07002527 if (jParamPtr == nullptr) {
2528 ALOGE("Failed to get Java array elements");
2529 return 0;
2530 }
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002531
Andreas Gampe67333922014-11-10 20:35:59 -08002532 if (kLogApi) {
2533 ALOGD("nProgramVertexCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2534 }
Jason Sams0011bcf2009-12-15 12:58:36 -08002535
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002536 int texCount = _env->GetArrayLength(texNames);
2537 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2538 const char ** nameArray = names.c_str();
2539 size_t* sizeArray = names.c_str_len();
2540
Ashok Bhat98071552014-02-12 09:54:43 +00002541 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2542 for(int i = 0; i < paramLen; ++i) {
2543 paramPtr[i] = (uintptr_t)jParamPtr[i];
2544 }
2545
Tim Murray3aa89c12014-08-18 17:51:22 -07002546 jlong ret = (jlong)(uintptr_t)rsProgramVertexCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002547 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002548 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002549
Ashok Bhat98071552014-02-12 09:54:43 +00002550 free(paramPtr);
2551 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams0011bcf2009-12-15 12:58:36 -08002552 return ret;
2553}
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002554
Jason Samsebfb4362009-09-23 13:57:02 -07002555// ---------------------------------------------------------------------------
2556
Tim Murray460a0492013-11-19 12:45:54 -08002557static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002558nProgramRasterCreate(JNIEnv *_env, jobject _this, jlong con, jboolean pointSprite, jint cull)
Jason Samsebfb4362009-09-23 13:57:02 -07002559{
Andreas Gampe67333922014-11-10 20:35:59 -08002560 if (kLogApi) {
2561 ALOGD("nProgramRasterCreate, con(%p), pointSprite(%i), cull(%i)", (RsContext)con,
2562 pointSprite, cull);
2563 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002564 return (jlong)(uintptr_t)rsProgramRasterCreate((RsContext)con, pointSprite, (RsCullMode)cull);
Jason Samsebfb4362009-09-23 13:57:02 -07002565}
2566
Jason Samsd19f10d2009-05-22 14:03:28 -07002567
2568// ---------------------------------------------------------------------------
2569
2570static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002571nContextBindRootScript(JNIEnv *_env, jobject _this, jlong con, jlong script)
Jason Samsd19f10d2009-05-22 14:03:28 -07002572{
Andreas Gampe67333922014-11-10 20:35:59 -08002573 if (kLogApi) {
2574 ALOGD("nContextBindRootScript, con(%p), script(%p)", (RsContext)con, (RsScript)script);
2575 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002576 rsContextBindRootScript((RsContext)con, (RsScript)script);
Jason Samsd19f10d2009-05-22 14:03:28 -07002577}
2578
2579static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002580nContextBindProgramStore(JNIEnv *_env, jobject _this, jlong con, jlong pfs)
Jason Samsd19f10d2009-05-22 14:03:28 -07002581{
Andreas Gampe67333922014-11-10 20:35:59 -08002582 if (kLogApi) {
2583 ALOGD("nContextBindProgramStore, con(%p), pfs(%p)", (RsContext)con, (RsProgramStore)pfs);
2584 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002585 rsContextBindProgramStore((RsContext)con, (RsProgramStore)pfs);
Jason Samsd19f10d2009-05-22 14:03:28 -07002586}
2587
2588static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002589nContextBindProgramFragment(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsd19f10d2009-05-22 14:03:28 -07002590{
Andreas Gampe67333922014-11-10 20:35:59 -08002591 if (kLogApi) {
2592 ALOGD("nContextBindProgramFragment, con(%p), pf(%p)", (RsContext)con,
2593 (RsProgramFragment)pf);
2594 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002595 rsContextBindProgramFragment((RsContext)con, (RsProgramFragment)pf);
Jason Samsd19f10d2009-05-22 14:03:28 -07002596}
2597
Jason Sams0826a6f2009-06-15 19:04:56 -07002598static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002599nContextBindProgramVertex(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Sams0826a6f2009-06-15 19:04:56 -07002600{
Andreas Gampe67333922014-11-10 20:35:59 -08002601 if (kLogApi) {
2602 ALOGD("nContextBindProgramVertex, con(%p), pf(%p)", (RsContext)con, (RsProgramVertex)pf);
2603 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002604 rsContextBindProgramVertex((RsContext)con, (RsProgramVertex)pf);
Jason Sams0826a6f2009-06-15 19:04:56 -07002605}
2606
Joe Onoratod7b37742009-08-09 22:57:44 -07002607static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002608nContextBindProgramRaster(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsebfb4362009-09-23 13:57:02 -07002609{
Andreas Gampe67333922014-11-10 20:35:59 -08002610 if (kLogApi) {
2611 ALOGD("nContextBindProgramRaster, con(%p), pf(%p)", (RsContext)con, (RsProgramRaster)pf);
2612 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002613 rsContextBindProgramRaster((RsContext)con, (RsProgramRaster)pf);
Jason Samsebfb4362009-09-23 13:57:02 -07002614}
2615
Joe Onoratod7b37742009-08-09 22:57:44 -07002616
Jason Sams02fb2cb2009-05-28 15:37:57 -07002617// ---------------------------------------------------------------------------
2618
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002619static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002620nSamplerCreate(JNIEnv *_env, jobject _this, jlong con, jint magFilter, jint minFilter,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002621 jint wrapS, jint wrapT, jint wrapR, jfloat aniso)
Jason Sams02fb2cb2009-05-28 15:37:57 -07002622{
Andreas Gampe67333922014-11-10 20:35:59 -08002623 if (kLogApi) {
2624 ALOGD("nSamplerCreate, con(%p)", (RsContext)con);
2625 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002626 return (jlong)(uintptr_t)rsSamplerCreate((RsContext)con,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002627 (RsSamplerValue)magFilter,
2628 (RsSamplerValue)minFilter,
2629 (RsSamplerValue)wrapS,
2630 (RsSamplerValue)wrapT,
2631 (RsSamplerValue)wrapR,
2632 aniso);
Jason Sams02fb2cb2009-05-28 15:37:57 -07002633}
2634
Jason Samsbba134c2009-06-22 15:49:21 -07002635// ---------------------------------------------------------------------------
2636
Tim Murray460a0492013-11-19 12:45:54 -08002637static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002638nMeshCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _vtx, jlongArray _idx, jintArray _prim)
Jason Samsbba134c2009-06-22 15:49:21 -07002639{
Andreas Gampe67333922014-11-10 20:35:59 -08002640 if (kLogApi) {
2641 ALOGD("nMeshCreate, con(%p)", (RsContext)con);
2642 }
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002643
Miao Wanga4ad5f82016-02-11 12:32:39 -08002644 jlong id = 0;
2645
2646 RsAllocation* vtxPtr;
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002647 jint vtxLen = _env->GetArrayLength(_vtx);
Chris Wailes488230c32014-08-14 11:22:40 -07002648 jlong *jVtxPtr = _env->GetLongArrayElements(_vtx, nullptr);
Miao Wanga4ad5f82016-02-11 12:32:39 -08002649
2650 RsAllocation* idxPtr;
2651 jint idxLen = _env->GetArrayLength(_idx);
2652 jlong *jIdxPtr = _env->GetLongArrayElements(_idx, nullptr);
2653
2654 jint primLen = _env->GetArrayLength(_prim);
2655 jint *primPtr = _env->GetIntArrayElements(_prim, nullptr);
2656
Miao Wangba8766c2015-10-12 17:24:13 -07002657 if (jVtxPtr == nullptr) {
2658 ALOGE("Failed to get Java array elements: vtx");
Miao Wanga4ad5f82016-02-11 12:32:39 -08002659 goto cleanupMesh;
Miao Wangba8766c2015-10-12 17:24:13 -07002660 }
Miao Wanga4ad5f82016-02-11 12:32:39 -08002661 if (jIdxPtr == nullptr) {
2662 ALOGE("Failed to get Java array elements: idx");
2663 goto cleanupMesh;
2664 }
2665 if (primPtr == nullptr) {
2666 ALOGE("Failed to get Java array elements: prim");
2667 goto cleanupMesh;
2668 }
2669
2670 vtxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * vtxLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002671 for(int i = 0; i < vtxLen; ++i) {
2672 vtxPtr[i] = (RsAllocation)(uintptr_t)jVtxPtr[i];
2673 }
2674
Miao Wanga4ad5f82016-02-11 12:32:39 -08002675 idxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * idxLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002676 for(int i = 0; i < idxLen; ++i) {
2677 idxPtr[i] = (RsAllocation)(uintptr_t)jIdxPtr[i];
2678 }
2679
Miao Wanga4ad5f82016-02-11 12:32:39 -08002680 id = (jlong)(uintptr_t)rsMeshCreate((RsContext)con,
2681 (RsAllocation *)vtxPtr, vtxLen,
2682 (RsAllocation *)idxPtr, idxLen,
2683 (uint32_t *)primPtr, primLen);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002684
Ashok Bhat98071552014-02-12 09:54:43 +00002685 free(vtxPtr);
2686 free(idxPtr);
Miao Wanga4ad5f82016-02-11 12:32:39 -08002687
2688cleanupMesh:
2689 if (jVtxPtr != nullptr) {
2690 _env->ReleaseLongArrayElements(_vtx, jVtxPtr, 0);
2691 }
2692 if (jIdxPtr != nullptr) {
2693 _env->ReleaseLongArrayElements(_idx, jIdxPtr, 0);
2694 }
2695 if (primPtr != nullptr) {
2696 _env->ReleaseIntArrayElements(_prim, primPtr, 0);
2697 }
2698
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002699 return id;
2700}
2701
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002702static jint
Tim Murray460a0492013-11-19 12:45:54 -08002703nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002704{
Andreas Gampe67333922014-11-10 20:35:59 -08002705 if (kLogApi) {
2706 ALOGD("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2707 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002708 jint vtxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002709 rsaMeshGetVertexBufferCount((RsContext)con, (RsMesh)mesh, &vtxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002710 return vtxCount;
2711}
2712
2713static jint
Tim Murray460a0492013-11-19 12:45:54 -08002714nMeshGetIndexCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002715{
Andreas Gampe67333922014-11-10 20:35:59 -08002716 if (kLogApi) {
2717 ALOGD("nMeshGetIndexCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2718 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002719 jint idxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002720 rsaMeshGetIndexCount((RsContext)con, (RsMesh)mesh, &idxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002721 return idxCount;
2722}
2723
2724static void
Ashok Bhat98071552014-02-12 09:54:43 +00002725nMeshGetVertices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _ids, jint numVtxIDs)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002726{
Andreas Gampe67333922014-11-10 20:35:59 -08002727 if (kLogApi) {
2728 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2729 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002730
2731 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
Tim Murrayeff663f2013-11-15 13:08:30 -08002732 rsaMeshGetVertices((RsContext)con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002733
2734 for(jint i = 0; i < numVtxIDs; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002735 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002736 _env->SetLongArrayRegion(_ids, i, 1, &alloc);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002737 }
2738
2739 free(allocs);
2740}
2741
2742static void
Ashok Bhat98071552014-02-12 09:54:43 +00002743nMeshGetIndices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _idxIds, jintArray _primitives, jint numIndices)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002744{
Andreas Gampe67333922014-11-10 20:35:59 -08002745 if (kLogApi) {
2746 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2747 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002748
2749 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
2750 uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
2751
Tim Murrayeff663f2013-11-15 13:08:30 -08002752 rsaMeshGetIndices((RsContext)con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002753
2754 for(jint i = 0; i < numIndices; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002755 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002756 const jint prim = (jint)prims[i];
2757 _env->SetLongArrayRegion(_idxIds, i, 1, &alloc);
2758 _env->SetIntArrayRegion(_primitives, i, 1, &prim);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002759 }
2760
2761 free(allocs);
2762 free(prims);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002763}
2764
Tim Murray56f9e6f2014-05-16 11:47:26 -07002765static jint
2766nSystemGetPointerSize(JNIEnv *_env, jobject _this) {
2767 return (jint)sizeof(void*);
2768}
2769
Miao Wang0facf022015-11-25 11:21:13 -08002770static jobject
2771nAllocationGetByteBuffer(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
2772 jlongArray strideArr, jint xBytesSize,
2773 jint dimY, jint dimZ) {
2774 if (kLogApi) {
2775 ALOGD("nAllocationGetByteBuffer, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
2776 }
Tim Murray56f9e6f2014-05-16 11:47:26 -07002777
Miao Wang0facf022015-11-25 11:21:13 -08002778 jlong *jStridePtr = _env->GetLongArrayElements(strideArr, nullptr);
2779 if (jStridePtr == nullptr) {
2780 ALOGE("Failed to get Java array elements: strideArr");
2781 return 0;
2782 }
2783
2784 size_t strideIn = xBytesSize;
2785 void* ptr = nullptr;
2786 if (alloc != 0) {
2787 ptr = rsAllocationGetPointer((RsContext)con, (RsAllocation)alloc, 0,
2788 RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, 0, 0,
2789 &strideIn, sizeof(size_t));
2790 }
2791
2792 jobject byteBuffer = nullptr;
2793 if (ptr != nullptr) {
2794 size_t bufferSize = strideIn;
2795 jStridePtr[0] = strideIn;
2796 if (dimY > 0) {
2797 bufferSize *= dimY;
2798 }
2799 if (dimZ > 0) {
2800 bufferSize *= dimZ;
2801 }
2802 byteBuffer = _env->NewDirectByteBuffer(ptr, (jlong) bufferSize);
2803 }
2804 _env->ReleaseLongArrayElements(strideArr, jStridePtr, 0);
2805 return byteBuffer;
2806}
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002807// ---------------------------------------------------------------------------
2808
Jason Samsd19f10d2009-05-22 14:03:28 -07002809
Jason Sams94d8e90a2009-06-10 16:09:05 -07002810static const char *classPathName = "android/renderscript/RenderScript";
Jason Samsd19f10d2009-05-22 14:03:28 -07002811
Daniel Micay76f6a862015-09-19 17:31:01 -04002812static const JNINativeMethod methods[] = {
Jason Sams1c415172010-11-08 17:06:46 -08002813{"_nInit", "()V", (void*)_nInit },
Jason Samsea84a7c2009-09-04 14:42:41 -07002814
Tim Murrayeff663f2013-11-15 13:08:30 -08002815{"nDeviceCreate", "()J", (void*)nDeviceCreate },
2816{"nDeviceDestroy", "(J)V", (void*)nDeviceDestroy },
2817{"nDeviceSetConfig", "(JII)V", (void*)nDeviceSetConfig },
2818{"nContextGetUserMessage", "(J[I)I", (void*)nContextGetUserMessage },
2819{"nContextGetErrorMessage", "(J)Ljava/lang/String;", (void*)nContextGetErrorMessage },
2820{"nContextPeekMessage", "(J[I)I", (void*)nContextPeekMessage },
Jason Sams1c415172010-11-08 17:06:46 -08002821
Tim Murrayeff663f2013-11-15 13:08:30 -08002822{"nContextInitToClient", "(J)V", (void*)nContextInitToClient },
2823{"nContextDeinitToClient", "(J)V", (void*)nContextDeinitToClient },
Jason Samsd19f10d2009-05-22 14:03:28 -07002824
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002825
Jason Sams2e1872f2010-08-17 16:25:41 -07002826// All methods below are thread protected in java.
Tim Murrayeff663f2013-11-15 13:08:30 -08002827{"rsnContextCreate", "(JIII)J", (void*)nContextCreate },
2828{"rsnContextCreateGL", "(JIIIIIIIIIIIIFI)J", (void*)nContextCreateGL },
2829{"rsnContextFinish", "(J)V", (void*)nContextFinish },
2830{"rsnContextSetPriority", "(JI)V", (void*)nContextSetPriority },
Tim Murray47f31582015-04-07 15:43:24 -07002831{"rsnContextSetCacheDir", "(JLjava/lang/String;)V", (void*)nContextSetCacheDir },
Tim Murrayeff663f2013-11-15 13:08:30 -08002832{"rsnContextSetSurface", "(JIILandroid/view/Surface;)V", (void*)nContextSetSurface },
2833{"rsnContextDestroy", "(J)V", (void*)nContextDestroy },
2834{"rsnContextDump", "(JI)V", (void*)nContextDump },
2835{"rsnContextPause", "(J)V", (void*)nContextPause },
2836{"rsnContextResume", "(J)V", (void*)nContextResume },
2837{"rsnContextSendMessage", "(JI[I)V", (void*)nContextSendMessage },
Yang Ni281c3252014-10-24 08:52:24 -07002838{"rsnClosureCreate", "(JJJ[J[J[I[J[J)J", (void*)nClosureCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002839{"rsnInvokeClosureCreate", "(JJ[B[J[J[I)J", (void*)nInvokeClosureCreate },
Yang Ni281c3252014-10-24 08:52:24 -07002840{"rsnClosureSetArg", "(JJIJI)V", (void*)nClosureSetArg },
2841{"rsnClosureSetGlobal", "(JJJJI)V", (void*)nClosureSetGlobal },
Tim Murray460a0492013-11-19 12:45:54 -08002842{"rsnAssignName", "(JJ[B)V", (void*)nAssignName },
2843{"rsnGetName", "(JJ)Ljava/lang/String;", (void*)nGetName },
2844{"rsnObjDestroy", "(JJ)V", (void*)nObjDestroy },
Jason Sams64676f32009-07-08 18:01:53 -07002845
Tim Murray460a0492013-11-19 12:45:54 -08002846{"rsnFileA3DCreateFromFile", "(JLjava/lang/String;)J", (void*)nFileA3DCreateFromFile },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002847{"rsnFileA3DCreateFromAssetStream", "(JJ)J", (void*)nFileA3DCreateFromAssetStream },
Tim Murray460a0492013-11-19 12:45:54 -08002848{"rsnFileA3DCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;)J", (void*)nFileA3DCreateFromAsset },
2849{"rsnFileA3DGetNumIndexEntries", "(JJ)I", (void*)nFileA3DGetNumIndexEntries },
2850{"rsnFileA3DGetIndexEntries", "(JJI[I[Ljava/lang/String;)V", (void*)nFileA3DGetIndexEntries },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002851{"rsnFileA3DGetEntryByIndex", "(JJI)J", (void*)nFileA3DGetEntryByIndex },
Jason Samsd19f10d2009-05-22 14:03:28 -07002852
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002853{"rsnFontCreateFromFile", "(JLjava/lang/String;FI)J", (void*)nFontCreateFromFile },
2854{"rsnFontCreateFromAssetStream", "(JLjava/lang/String;FIJ)J", (void*)nFontCreateFromAssetStream },
2855{"rsnFontCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;FI)J", (void*)nFontCreateFromAsset },
Jason Samsd19f10d2009-05-22 14:03:28 -07002856
Tim Murray460a0492013-11-19 12:45:54 -08002857{"rsnElementCreate", "(JJIZI)J", (void*)nElementCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002858{"rsnElementCreate2", "(J[J[Ljava/lang/String;[I)J", (void*)nElementCreate2 },
Tim Murray460a0492013-11-19 12:45:54 -08002859{"rsnElementGetNativeData", "(JJ[I)V", (void*)nElementGetNativeData },
Ashok Bhat98071552014-02-12 09:54:43 +00002860{"rsnElementGetSubElements", "(JJ[J[Ljava/lang/String;[I)V", (void*)nElementGetSubElements },
Jason Samsd19f10d2009-05-22 14:03:28 -07002861
Tim Murray460a0492013-11-19 12:45:54 -08002862{"rsnTypeCreate", "(JJIIIZZI)J", (void*)nTypeCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002863{"rsnTypeGetNativeData", "(JJ[J)V", (void*)nTypeGetNativeData },
Jason Samsd19f10d2009-05-22 14:03:28 -07002864
Leon Scroggins III71fae622019-03-26 16:28:41 -04002865{"rsnAllocationCreateTyped", "(JJIIJ)J", (void*)nAllocationCreateTyped },
Derek Sollenbergerdb98b522019-12-27 15:10:50 -05002866{"rsnAllocationCreateFromBitmap", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateFromBitmap },
2867{"rsnAllocationCreateBitmapBackedAllocation", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateBitmapBackedAllocation },
2868{"rsnAllocationCubeCreateFromBitmap","(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCubeCreateFromBitmap },
Jason Sams5476b452010-12-08 16:14:36 -08002869
Derek Sollenbergerdb98b522019-12-27 15:10:50 -05002870{"rsnAllocationCopyFromBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyFromBitmap },
2871{"rsnAllocationCopyToBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyToBitmap },
Jason Sams4ef66502010-12-10 16:03:15 -08002872
Tim Murray460a0492013-11-19 12:45:54 -08002873{"rsnAllocationSyncAll", "(JJI)V", (void*)nAllocationSyncAll },
Miao Wang8c150922015-10-26 17:44:10 -07002874{"rsnAllocationSetupBufferQueue", "(JJI)V", (void*)nAllocationSetupBufferQueue },
2875{"rsnAllocationShareBufferQueue", "(JJJ)V", (void*)nAllocationShareBufferQueue },
Tim Murray460a0492013-11-19 12:45:54 -08002876{"rsnAllocationGetSurface", "(JJ)Landroid/view/Surface;", (void*)nAllocationGetSurface },
2877{"rsnAllocationSetSurface", "(JJLandroid/view/Surface;)V", (void*)nAllocationSetSurface },
2878{"rsnAllocationIoSend", "(JJ)V", (void*)nAllocationIoSend },
Miao Wang8c150922015-10-26 17:44:10 -07002879{"rsnAllocationIoReceive", "(JJ)J", (void*)nAllocationIoReceive },
Miao Wang87e908d2015-03-02 15:15:15 -08002880{"rsnAllocationData1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData1D },
Miao Wangc8e237e2015-02-20 18:36:32 -08002881{"rsnAllocationElementData", "(JJIIIII[BI)V", (void*)nAllocationElementData },
Miao Wang87e908d2015-03-02 15:15:15 -08002882{"rsnAllocationData2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData2D },
Tim Murray460a0492013-11-19 12:45:54 -08002883{"rsnAllocationData2D", "(JJIIIIIIJIIII)V", (void*)nAllocationData2D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002884{"rsnAllocationData3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData3D },
Tim Murray460a0492013-11-19 12:45:54 -08002885{"rsnAllocationData3D", "(JJIIIIIIIJIIII)V", (void*)nAllocationData3D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002886{"rsnAllocationRead", "(JJLjava/lang/Object;IIZ)V", (void*)nAllocationRead },
2887{"rsnAllocationRead1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead1D },
Miao Wang45cec0a2015-03-04 16:40:21 -08002888{"rsnAllocationElementRead", "(JJIIIII[BI)V", (void*)nAllocationElementRead },
Miao Wang87e908d2015-03-02 15:15:15 -08002889{"rsnAllocationRead2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead2D },
2890{"rsnAllocationRead3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead3D },
Tim Murray460a0492013-11-19 12:45:54 -08002891{"rsnAllocationGetType", "(JJ)J", (void*)nAllocationGetType},
2892{"rsnAllocationResize1D", "(JJI)V", (void*)nAllocationResize1D },
2893{"rsnAllocationGenerateMipmaps", "(JJ)V", (void*)nAllocationGenerateMipmaps },
Jason Samsbd1c3ad2009-08-03 16:03:08 -07002894
Jason Sams46ba27e32015-02-06 17:45:15 -08002895{"rsnAllocationAdapterCreate", "(JJJ)J", (void*)nAllocationAdapterCreate },
2896{"rsnAllocationAdapterOffset", "(JJIIIIIIIII)V", (void*)nAllocationAdapterOffset },
2897
Tim Murray460a0492013-11-19 12:45:54 -08002898{"rsnScriptBindAllocation", "(JJJI)V", (void*)nScriptBindAllocation },
2899{"rsnScriptSetTimeZone", "(JJ[B)V", (void*)nScriptSetTimeZone },
2900{"rsnScriptInvoke", "(JJI)V", (void*)nScriptInvoke },
2901{"rsnScriptInvokeV", "(JJI[B)V", (void*)nScriptInvokeV },
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002902
2903{"rsnScriptForEach", "(JJI[JJ[B[I)V", (void*)nScriptForEach },
David Gross4a457852016-06-02 14:46:55 -07002904{"rsnScriptReduce", "(JJI[JJ[I)V", (void*)nScriptReduce },
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002905
Tim Murray460a0492013-11-19 12:45:54 -08002906{"rsnScriptSetVarI", "(JJII)V", (void*)nScriptSetVarI },
2907{"rsnScriptGetVarI", "(JJI)I", (void*)nScriptGetVarI },
2908{"rsnScriptSetVarJ", "(JJIJ)V", (void*)nScriptSetVarJ },
2909{"rsnScriptGetVarJ", "(JJI)J", (void*)nScriptGetVarJ },
2910{"rsnScriptSetVarF", "(JJIF)V", (void*)nScriptSetVarF },
2911{"rsnScriptGetVarF", "(JJI)F", (void*)nScriptGetVarF },
2912{"rsnScriptSetVarD", "(JJID)V", (void*)nScriptSetVarD },
2913{"rsnScriptGetVarD", "(JJI)D", (void*)nScriptGetVarD },
2914{"rsnScriptSetVarV", "(JJI[B)V", (void*)nScriptSetVarV },
2915{"rsnScriptGetVarV", "(JJI[B)V", (void*)nScriptGetVarV },
2916{"rsnScriptSetVarVE", "(JJI[BJ[I)V", (void*)nScriptSetVarVE },
2917{"rsnScriptSetVarObj", "(JJIJ)V", (void*)nScriptSetVarObj },
Jason Samsd19f10d2009-05-22 14:03:28 -07002918
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002919{"rsnScriptCCreate", "(JLjava/lang/String;Ljava/lang/String;[BI)J", (void*)nScriptCCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002920{"rsnScriptIntrinsicCreate", "(JIJ)J", (void*)nScriptIntrinsicCreate },
2921{"rsnScriptKernelIDCreate", "(JJII)J", (void*)nScriptKernelIDCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002922{"rsnScriptInvokeIDCreate", "(JJI)J", (void*)nScriptInvokeIDCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002923{"rsnScriptFieldIDCreate", "(JJI)J", (void*)nScriptFieldIDCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002924{"rsnScriptGroupCreate", "(J[J[J[J[J[J)J", (void*)nScriptGroupCreate },
Yang Ni35be56c2015-04-02 17:47:56 -07002925{"rsnScriptGroup2Create", "(JLjava/lang/String;Ljava/lang/String;[J)J", (void*)nScriptGroup2Create },
Tim Murray460a0492013-11-19 12:45:54 -08002926{"rsnScriptGroupSetInput", "(JJJJ)V", (void*)nScriptGroupSetInput },
2927{"rsnScriptGroupSetOutput", "(JJJJ)V", (void*)nScriptGroupSetOutput },
2928{"rsnScriptGroupExecute", "(JJ)V", (void*)nScriptGroupExecute },
Yang Ni281c3252014-10-24 08:52:24 -07002929{"rsnScriptGroup2Execute", "(JJ)V", (void*)nScriptGroup2Execute },
Jason Sams0011bcf2009-12-15 12:58:36 -08002930
Tim Murray25207df2015-01-12 16:47:56 -08002931{"rsnScriptIntrinsicBLAS_Single", "(JJIIIIIIIIIFJJFJIIII)V", (void*)nScriptIntrinsicBLAS_Single },
2932{"rsnScriptIntrinsicBLAS_Double", "(JJIIIIIIIIIDJJDJIIII)V", (void*)nScriptIntrinsicBLAS_Double },
2933{"rsnScriptIntrinsicBLAS_Complex", "(JJIIIIIIIIIFFJJFFJIIII)V", (void*)nScriptIntrinsicBLAS_Complex },
2934{"rsnScriptIntrinsicBLAS_Z", "(JJIIIIIIIIIDDJJDDJIIII)V", (void*)nScriptIntrinsicBLAS_Z },
2935
Tim Murray9cb16a22015-04-01 11:07:16 -07002936{"rsnScriptIntrinsicBLAS_BNNM", "(JJIIIJIJIJII)V", (void*)nScriptIntrinsicBLAS_BNNM },
2937
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002938{"rsnProgramStoreCreate", "(JZZZZZZIII)J", (void*)nProgramStoreCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002939
Tim Murray460a0492013-11-19 12:45:54 -08002940{"rsnProgramBindConstants", "(JJIJ)V", (void*)nProgramBindConstants },
2941{"rsnProgramBindTexture", "(JJIJ)V", (void*)nProgramBindTexture },
2942{"rsnProgramBindSampler", "(JJIJ)V", (void*)nProgramBindSampler },
Jason Samsebfb4362009-09-23 13:57:02 -07002943
Ashok Bhat98071552014-02-12 09:54:43 +00002944{"rsnProgramFragmentCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramFragmentCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002945{"rsnProgramRasterCreate", "(JZI)J", (void*)nProgramRasterCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002946{"rsnProgramVertexCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramVertexCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002947
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002948{"rsnContextBindRootScript", "(JJ)V", (void*)nContextBindRootScript },
2949{"rsnContextBindProgramStore", "(JJ)V", (void*)nContextBindProgramStore },
2950{"rsnContextBindProgramFragment", "(JJ)V", (void*)nContextBindProgramFragment },
2951{"rsnContextBindProgramVertex", "(JJ)V", (void*)nContextBindProgramVertex },
2952{"rsnContextBindProgramRaster", "(JJ)V", (void*)nContextBindProgramRaster },
Jason Sams02fb2cb2009-05-28 15:37:57 -07002953
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002954{"rsnSamplerCreate", "(JIIIIIF)J", (void*)nSamplerCreate },
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002955
Ashok Bhat98071552014-02-12 09:54:43 +00002956{"rsnMeshCreate", "(J[J[J[I)J", (void*)nMeshCreate },
Jason Sams2e1872f2010-08-17 16:25:41 -07002957
Tim Murray460a0492013-11-19 12:45:54 -08002958{"rsnMeshGetVertexBufferCount", "(JJ)I", (void*)nMeshGetVertexBufferCount },
2959{"rsnMeshGetIndexCount", "(JJ)I", (void*)nMeshGetIndexCount },
Ashok Bhat98071552014-02-12 09:54:43 +00002960{"rsnMeshGetVertices", "(JJ[JI)V", (void*)nMeshGetVertices },
2961{"rsnMeshGetIndices", "(JJ[J[II)V", (void*)nMeshGetIndices },
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002962
Tim Murray56f9e6f2014-05-16 11:47:26 -07002963{"rsnSystemGetPointerSize", "()I", (void*)nSystemGetPointerSize },
Miao Wang0facf022015-11-25 11:21:13 -08002964{"rsnAllocationGetByteBuffer", "(JJ[JIII)Ljava/nio/ByteBuffer;", (void*)nAllocationGetByteBuffer },
Jason Samsd19f10d2009-05-22 14:03:28 -07002965};
2966
2967static int registerFuncs(JNIEnv *_env)
2968{
2969 return android::AndroidRuntime::registerNativeMethods(
2970 _env, classPathName, methods, NELEM(methods));
2971}
2972
2973// ---------------------------------------------------------------------------
2974
2975jint JNI_OnLoad(JavaVM* vm, void* reserved)
2976{
Chris Wailes488230c32014-08-14 11:22:40 -07002977 JNIEnv* env = nullptr;
Jason Samsd19f10d2009-05-22 14:03:28 -07002978 jint result = -1;
2979
Jason Samsd19f10d2009-05-22 14:03:28 -07002980 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
Steve Block3762c312012-01-06 19:20:56 +00002981 ALOGE("ERROR: GetEnv failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07002982 goto bail;
2983 }
Chris Wailes488230c32014-08-14 11:22:40 -07002984 assert(env != nullptr);
Jason Samsd19f10d2009-05-22 14:03:28 -07002985
2986 if (registerFuncs(env) < 0) {
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002987 ALOGE("ERROR: Renderscript native registration failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07002988 goto bail;
2989 }
2990
2991 /* success -- return valid version number */
2992 result = JNI_VERSION_1_4;
2993
2994bail:
2995 return result;
2996}