blob: 52d0e08e4e7f4f1be96b47e731532ff4eeb7507e [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 Lesinskidcb3c652017-01-23 12:58:11 -080027#include <android-base/macros.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080028#include <androidfw/Asset.h>
Adam Lesinskidcb3c652017-01-23 12:58:11 -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>
Jason Samsd19f10d2009-05-22 14:03:28 -070035#include "android_runtime/AndroidRuntime.h"
Jim Milleree956052010-08-19 18:56:00 -070036#include "android_runtime/android_view_Surface.h"
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -080037#include "android_runtime/android_util_AssetManager.h"
John Reckf4faeac2015-03-05 13:50:31 -080038#include "android/graphics/GraphicsJNI.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
Tim Murray460a0492013-11-19 12:45:54 -08001322static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001323nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1324 jobject jbitmap, jint usage)
Jason Samsffe9f482009-06-01 17:45:53 -07001325{
John Recked207b92015-04-10 13:52:57 -07001326 SkBitmap bitmap;
1327 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Samsffe9f482009-06-01 17:45:53 -07001328
Jason Sams5476b452010-12-08 16:14:36 -08001329 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001330 jlong id = (jlong)(uintptr_t)rsAllocationCreateFromBitmap((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001331 (RsType)type, (RsAllocationMipmapControl)mip,
Mike Reed7569de02017-10-06 16:25:49 -04001332 ptr, bitmap.computeByteSize(), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001333 return id;
Jason Samsffe9f482009-06-01 17:45:53 -07001334}
Jason Samsfe08d992009-05-27 14:45:32 -07001335
Tim Murray460a0492013-11-19 12:45:54 -08001336static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001337nAllocationCreateBitmapBackedAllocation(JNIEnv *_env, jobject _this, jlong con, jlong type,
1338 jint mip, jobject jbitmap, jint usage)
Tim Murraya3145512012-12-04 17:59:29 -08001339{
John Recked207b92015-04-10 13:52:57 -07001340 SkBitmap bitmap;
1341 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Tim Murraya3145512012-12-04 17:59:29 -08001342
Tim Murraya3145512012-12-04 17:59:29 -08001343 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001344 jlong id = (jlong)(uintptr_t)rsAllocationCreateTyped((RsContext)con,
Tim Murraya3145512012-12-04 17:59:29 -08001345 (RsType)type, (RsAllocationMipmapControl)mip,
Yang Ni8c8daea2016-03-08 21:01:54 +00001346 (uint32_t)usage, (uintptr_t)ptr);
Tim Murraya3145512012-12-04 17:59:29 -08001347 return id;
1348}
1349
Tim Murray460a0492013-11-19 12:45:54 -08001350static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001351nAllocationCubeCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1352 jobject jbitmap, jint usage)
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001353{
John Recked207b92015-04-10 13:52:57 -07001354 SkBitmap bitmap;
1355 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001356
Jason Sams5476b452010-12-08 16:14:36 -08001357 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001358 jlong id = (jlong)(uintptr_t)rsAllocationCubeCreateFromBitmap((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001359 (RsType)type, (RsAllocationMipmapControl)mip,
Mike Reed7569de02017-10-06 16:25:49 -04001360 ptr, bitmap.computeByteSize(), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001361 return id;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001362}
1363
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001364static void
Tim Murray460a0492013-11-19 12:45:54 -08001365nAllocationCopyFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001366{
John Recked207b92015-04-10 13:52:57 -07001367 SkBitmap bitmap;
1368 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Samsf7086092011-01-12 13:28:37 -08001369 int w = bitmap.width();
1370 int h = bitmap.height();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001371
Jason Sams4ef66502010-12-10 16:03:15 -08001372 const void* ptr = bitmap.getPixels();
Tim Murrayeff663f2013-11-15 13:08:30 -08001373 rsAllocation2DData((RsContext)con, (RsAllocation)alloc, 0, 0,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001374 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Mike Reed7569de02017-10-06 16:25:49 -04001375 w, h, ptr, bitmap.computeByteSize(), 0);
Jason Sams4ef66502010-12-10 16:03:15 -08001376}
1377
1378static void
Tim Murray460a0492013-11-19 12:45:54 -08001379nAllocationCopyToBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Jason Sams4ef66502010-12-10 16:03:15 -08001380{
John Recked207b92015-04-10 13:52:57 -07001381 SkBitmap bitmap;
1382 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Sams4ef66502010-12-10 16:03:15 -08001383
Jason Sams4ef66502010-12-10 16:03:15 -08001384 void* ptr = bitmap.getPixels();
Mike Reed7569de02017-10-06 16:25:49 -04001385 rsAllocationCopyToBitmap((RsContext)con, (RsAllocation)alloc, ptr, bitmap.computeByteSize());
Alex Sakhartchouk835b8542011-07-20 14:33:10 -07001386 bitmap.notifyPixelsChanged();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001387}
1388
Stephen Hines414fa2c2014-04-17 01:02:42 -07001389// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Samsd19f10d2009-05-22 14:03:28 -07001390static void
Tim Murray460a0492013-11-19 12:45:54 -08001391nAllocationData1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001392 jint count, jobject data, jint sizeBytes, jint dataType, jint mSize,
1393 jboolean usePadding)
Jason Samsd19f10d2009-05-22 14:03:28 -07001394{
Jason Samse729a942013-11-06 11:22:02 -08001395 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001396 if (kLogApi) {
1397 ALOGD("nAllocation1DData, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1398 "dataType(%i)", (RsContext)con, (RsAllocation)alloc, offset, count, sizeBytes,
1399 dataType);
1400 }
Miao Wang87e908d2015-03-02 15:15:15 -08001401 PER_ARRAY_TYPE(nullptr, rsAllocation1DData, true,
1402 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -07001403}
1404
1405static void
Miao Wangc8e237e2015-02-20 18:36:32 -08001406nAllocationElementData(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1407 jint xoff, jint yoff, jint zoff,
1408 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Jason Sams49bdaf02010-08-31 13:50:42 -07001409{
Andreas Gampe67333922014-11-10 20:35:59 -08001410 if (kLogApi) {
Yang Ni86c5c2d2016-03-25 15:49:07 -07001411 jint len = _env->GetArrayLength(data);
Miao Wangc8e237e2015-02-20 18:36:32 -08001412 ALOGD("nAllocationElementData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1413 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
Andreas Gampe67333922014-11-10 20:35:59 -08001414 sizeBytes);
1415 }
Chris Wailes488230c32014-08-14 11:22:40 -07001416 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001417 if (ptr == nullptr) {
1418 ALOGE("Failed to get Java array elements");
1419 return;
1420 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001421 rsAllocationElementData((RsContext)con, (RsAllocation)alloc,
1422 xoff, yoff, zoff,
1423 lod, ptr, sizeBytes, compIdx);
Jason Sams49bdaf02010-08-31 13:50:42 -07001424 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1425}
1426
Miao Wangc8e237e2015-02-20 18:36:32 -08001427
Stephen Hines414fa2c2014-04-17 01:02:42 -07001428// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Sams49bdaf02010-08-31 13:50:42 -07001429static void
Tim Murray460a0492013-11-19 12:45:54 -08001430nAllocationData2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001431 jint w, jint h, jobject data, jint sizeBytes, jint dataType, jint mSize,
1432 jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001433{
Jason Samse729a942013-11-06 11:22:02 -08001434 RsAllocation *alloc = (RsAllocation *)_alloc;
1435 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001436 if (kLogApi) {
1437 ALOGD("nAllocation2DData, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1438 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1439 }
Miao Wang87e908d2015-03-02 15:15:15 -08001440 int count = w * h;
1441 PER_ARRAY_TYPE(nullptr, rsAllocation2DData, true,
1442 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Samsd19f10d2009-05-22 14:03:28 -07001443}
1444
Stephen Hines414fa2c2014-04-17 01:02:42 -07001445// Copies from the Allocation pointed to by srcAlloc into the Allocation
1446// pointed to by dstAlloc.
Jason Sams40a29e82009-08-10 14:55:26 -07001447static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001448nAllocationData2D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001449 jlong dstAlloc, jint dstXoff, jint dstYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001450 jint dstMip, jint dstFace,
1451 jint width, jint height,
Tim Murray460a0492013-11-19 12:45:54 -08001452 jlong srcAlloc, jint srcXoff, jint srcYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001453 jint srcMip, jint srcFace)
1454{
Andreas Gampe67333922014-11-10 20:35:59 -08001455 if (kLogApi) {
1456 ALOGD("nAllocation2DData_s, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1457 " dstMip(%i), dstFace(%i), width(%i), height(%i),"
1458 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i), srcFace(%i)",
1459 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip, dstFace,
1460 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
1461 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001462
Tim Murrayeff663f2013-11-15 13:08:30 -08001463 rsAllocationCopy2DRange((RsContext)con,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001464 (RsAllocation)dstAlloc,
1465 dstXoff, dstYoff,
1466 dstMip, dstFace,
1467 width, height,
1468 (RsAllocation)srcAlloc,
1469 srcXoff, srcYoff,
1470 srcMip, srcFace);
1471}
1472
Stephen Hines414fa2c2014-04-17 01:02:42 -07001473// Copies from the Java object data into the Allocation pointed to by _alloc.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001474static void
Tim Murray460a0492013-11-19 12:45:54 -08001475nAllocationData3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001476 jint w, jint h, jint d, jobject data, jint sizeBytes, jint dataType,
1477 jint mSize, jboolean usePadding)
Jason Samsb05d6892013-04-09 15:59:24 -07001478{
Jason Samse729a942013-11-06 11:22:02 -08001479 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001480 if (kLogApi) {
1481 ALOGD("nAllocation3DData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1482 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1483 lod, w, h, d, sizeBytes);
1484 }
Miao Wang87e908d2015-03-02 15:15:15 -08001485 int count = w * h * d;
1486 PER_ARRAY_TYPE(nullptr, rsAllocation3DData, true,
1487 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Jason Samsb05d6892013-04-09 15:59:24 -07001488}
1489
Stephen Hines414fa2c2014-04-17 01:02:42 -07001490// Copies from the Allocation pointed to by srcAlloc into the Allocation
1491// pointed to by dstAlloc.
Jason Samsb05d6892013-04-09 15:59:24 -07001492static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001493nAllocationData3D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001494 jlong dstAlloc, jint dstXoff, jint dstYoff, jint dstZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001495 jint dstMip,
1496 jint width, jint height, jint depth,
Tim Murray460a0492013-11-19 12:45:54 -08001497 jlong srcAlloc, jint srcXoff, jint srcYoff, jint srcZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001498 jint srcMip)
1499{
Andreas Gampe67333922014-11-10 20:35:59 -08001500 if (kLogApi) {
1501 ALOGD("nAllocationData3D_alloc, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1502 " dstMip(%i), width(%i), height(%i),"
1503 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i)",
1504 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip,
1505 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip);
1506 }
Jason Samsb05d6892013-04-09 15:59:24 -07001507
Tim Murrayeff663f2013-11-15 13:08:30 -08001508 rsAllocationCopy3DRange((RsContext)con,
Jason Samsb05d6892013-04-09 15:59:24 -07001509 (RsAllocation)dstAlloc,
1510 dstXoff, dstYoff, dstZoff, dstMip,
1511 width, height, depth,
1512 (RsAllocation)srcAlloc,
1513 srcXoff, srcYoff, srcZoff, srcMip);
1514}
1515
Jason Sams21659ac2013-11-06 15:08:07 -08001516
Stephen Hines414fa2c2014-04-17 01:02:42 -07001517// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsb05d6892013-04-09 15:59:24 -07001518static void
Miao Wang87e908d2015-03-02 15:15:15 -08001519nAllocationRead(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jobject data, jint dataType,
1520 jint mSize, jboolean usePadding)
Jason Sams40a29e82009-08-10 14:55:26 -07001521{
Jason Sams21659ac2013-11-06 15:08:07 -08001522 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001523 if (kLogApi) {
1524 ALOGD("nAllocationRead, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
1525 }
Miao Wang87e908d2015-03-02 15:15:15 -08001526 int count = 0;
1527 PER_ARRAY_TYPE(0, rsAllocationRead, false,
1528 (RsContext)con, alloc, ptr, len * typeBytes);
Jason Sams40a29e82009-08-10 14:55:26 -07001529}
1530
Stephen Hines414fa2c2014-04-17 01:02:42 -07001531// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Sams40a29e82009-08-10 14:55:26 -07001532static void
Tim Murray460a0492013-11-19 12:45:54 -08001533nAllocationRead1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001534 jint count, jobject data, jint sizeBytes, jint dataType,
1535 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001536{
Jason Sams21659ac2013-11-06 15:08:07 -08001537 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001538 if (kLogApi) {
1539 ALOGD("nAllocation1DRead, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1540 "dataType(%i)", (RsContext)con, alloc, offset, count, sizeBytes, dataType);
1541 }
Miao Wang87e908d2015-03-02 15:15:15 -08001542 PER_ARRAY_TYPE(0, rsAllocation1DRead, false,
1543 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsfb9f82c2011-01-12 14:53:25 -08001544}
1545
Miao Wangc8e237e2015-02-20 18:36:32 -08001546// Copies from the Element in the Allocation pointed to by _alloc into the Java array data.
1547static void
Miao Wang45cec0a2015-03-04 16:40:21 -08001548nAllocationElementRead(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
Miao Wangc8e237e2015-02-20 18:36:32 -08001549 jint xoff, jint yoff, jint zoff,
Miao Wang45cec0a2015-03-04 16:40:21 -08001550 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Miao Wangc8e237e2015-02-20 18:36:32 -08001551{
Miao Wangc8e237e2015-02-20 18:36:32 -08001552 if (kLogApi) {
Yang Ni86c5c2d2016-03-25 15:49:07 -07001553 jint len = _env->GetArrayLength(data);
Miao Wang45cec0a2015-03-04 16:40:21 -08001554 ALOGD("nAllocationElementRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1555 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
1556 sizeBytes);
Miao Wangc8e237e2015-02-20 18:36:32 -08001557 }
Miao Wang45cec0a2015-03-04 16:40:21 -08001558 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001559 if (ptr == nullptr) {
1560 ALOGE("Failed to get Java array elements");
1561 return;
1562 }
Miao Wang45cec0a2015-03-04 16:40:21 -08001563 rsAllocationElementRead((RsContext)con, (RsAllocation)alloc,
1564 xoff, yoff, zoff,
Jason Samsa7e25092015-03-11 11:00:00 -07001565 lod, ptr, sizeBytes, compIdx);
Miao Wangbfa5e652015-05-04 15:29:25 -07001566 _env->ReleaseByteArrayElements(data, ptr, 0);
Miao Wangc8e237e2015-02-20 18:36:32 -08001567}
1568
Stephen Hines414fa2c2014-04-17 01:02:42 -07001569// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsfb9f82c2011-01-12 14:53:25 -08001570static void
Tim Murray460a0492013-11-19 12:45:54 -08001571nAllocationRead2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001572 jint w, jint h, jobject data, jint sizeBytes, jint dataType,
1573 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001574{
Jason Sams21659ac2013-11-06 15:08:07 -08001575 RsAllocation *alloc = (RsAllocation *)_alloc;
1576 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001577 if (kLogApi) {
1578 ALOGD("nAllocation2DRead, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1579 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1580 }
Miao Wang87e908d2015-03-02 15:15:15 -08001581 int count = w * h;
1582 PER_ARRAY_TYPE(0, rsAllocation2DRead, false,
1583 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Sams40a29e82009-08-10 14:55:26 -07001584}
Miao Wang87e908d2015-03-02 15:15:15 -08001585
Miao Wangc8e237e2015-02-20 18:36:32 -08001586// Copies from the Allocation pointed to by _alloc into the Java object data.
1587static void
1588nAllocationRead3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001589 jint w, jint h, jint d, jobject data, int sizeBytes, int dataType,
1590 jint mSize, jboolean usePadding)
Miao Wangc8e237e2015-02-20 18:36:32 -08001591{
1592 RsAllocation *alloc = (RsAllocation *)_alloc;
1593 if (kLogApi) {
1594 ALOGD("nAllocation3DRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1595 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1596 lod, w, h, d, sizeBytes);
1597 }
Miao Wang87e908d2015-03-02 15:15:15 -08001598 int count = w * h * d;
1599 PER_ARRAY_TYPE(nullptr, rsAllocation3DRead, false,
1600 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Miao Wangc8e237e2015-02-20 18:36:32 -08001601}
Jason Samsd19f10d2009-05-22 14:03:28 -07001602
Tim Murray460a0492013-11-19 12:45:54 -08001603static jlong
1604nAllocationGetType(JNIEnv *_env, jobject _this, jlong con, jlong a)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001605{
Andreas Gampe67333922014-11-10 20:35:59 -08001606 if (kLogApi) {
1607 ALOGD("nAllocationGetType, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1608 }
Tim Murray3aa89c12014-08-18 17:51:22 -07001609 return (jlong)(uintptr_t) rsaAllocationGetType((RsContext)con, (RsAllocation)a);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001610}
1611
Jason Sams5edc6082010-10-05 13:32:49 -07001612static void
Tim Murray460a0492013-11-19 12:45:54 -08001613nAllocationResize1D(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint dimX)
Jason Sams5edc6082010-10-05 13:32:49 -07001614{
Andreas Gampe67333922014-11-10 20:35:59 -08001615 if (kLogApi) {
1616 ALOGD("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", (RsContext)con,
1617 (RsAllocation)alloc, dimX);
1618 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001619 rsAllocationResize1D((RsContext)con, (RsAllocation)alloc, dimX);
Jason Sams5edc6082010-10-05 13:32:49 -07001620}
1621
Jason Sams46ba27e32015-02-06 17:45:15 -08001622
1623static jlong
1624nAllocationAdapterCreate(JNIEnv *_env, jobject _this, jlong con, jlong basealloc, jlong type)
1625{
1626 if (kLogApi) {
1627 ALOGD("nAllocationAdapterCreate, con(%p), base(%p), type(%p)",
1628 (RsContext)con, (RsAllocation)basealloc, (RsElement)type);
1629 }
1630 return (jlong)(uintptr_t) rsAllocationAdapterCreate((RsContext)con, (RsType)type,
1631 (RsAllocation)basealloc);
1632
1633}
1634
1635static void
1636nAllocationAdapterOffset(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1637 jint x, jint y, jint z, jint face, jint lod,
1638 jint a1, jint a2, jint a3, jint a4)
1639{
1640 uint32_t params[] = {
1641 (uint32_t)x, (uint32_t)y, (uint32_t)z, (uint32_t)face,
1642 (uint32_t)lod, (uint32_t)a1, (uint32_t)a2, (uint32_t)a3, (uint32_t)a4
1643 };
1644 if (kLogApi) {
1645 ALOGD("nAllocationAdapterOffset, con(%p), alloc(%p), x(%i), y(%i), z(%i), face(%i), lod(%i), arrays(%i %i %i %i)",
1646 (RsContext)con, (RsAllocation)alloc, x, y, z, face, lod, a1, a2, a3, a4);
1647 }
1648 rsAllocationAdapterOffset((RsContext)con, (RsAllocation)alloc,
1649 params, sizeof(params));
1650}
1651
1652
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001653// -----------------------------------
1654
Tim Murray460a0492013-11-19 12:45:54 -08001655static jlong
1656nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con, jlong native_asset)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001657{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001658 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001659 ALOGV("______nFileA3D %p", asset);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001660
Tim Murray3aa89c12014-08-18 17:51:22 -07001661 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromMemory((RsContext)con, asset->getBuffer(false), asset->getLength());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001662 return id;
1663}
1664
Tim Murray460a0492013-11-19 12:45:54 -08001665static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001666nFileA3DCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001667{
Adam Lesinskidcb3c652017-01-23 12:58:11 -08001668 Guarded<AssetManager2>* mgr = AssetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001669 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001670 return 0;
1671 }
1672
1673 AutoJavaStringToUTF8 str(_env, _path);
Adam Lesinskidcb3c652017-01-23 12:58:11 -08001674 std::unique_ptr<Asset> asset;
1675 {
1676 ScopedLock<AssetManager2> locked_mgr(*mgr);
1677 asset = locked_mgr->Open(str.c_str(), Asset::ACCESS_BUFFER);
1678 if (asset == nullptr) {
1679 return 0;
1680 }
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001681 }
1682
Adam Lesinskidcb3c652017-01-23 12:58:11 -08001683 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromAsset((RsContext)con, asset.release());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001684 return id;
1685}
1686
Tim Murray460a0492013-11-19 12:45:54 -08001687static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001688nFileA3DCreateFromFile(JNIEnv *_env, jobject _this, jlong con, jstring fileName)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001689{
1690 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001691 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromFile((RsContext)con, fileNameUTF.c_str());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001692
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001693 return id;
1694}
1695
Tim Murray460a0492013-11-19 12:45:54 -08001696static jint
1697nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001698{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001699 int32_t numEntries = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001700 rsaFileA3DGetNumIndexEntries((RsContext)con, &numEntries, (RsFile)fileA3D);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001701 return (jint)numEntries;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001702}
1703
1704static void
Tim Murray460a0492013-11-19 12:45:54 -08001705nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001706{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001707 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001708 RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
1709
Tim Murrayeff663f2013-11-15 13:08:30 -08001710 rsaFileA3DGetIndexEntries((RsContext)con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001711
1712 for(jint i = 0; i < numEntries; i ++) {
1713 _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
1714 _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
1715 }
1716
1717 free(fileEntries);
1718}
1719
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001720static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001721nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint index)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001722{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001723 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Tim Murray3aa89c12014-08-18 17:51:22 -07001724 jlong id = (jlong)(uintptr_t)rsaFileA3DGetEntryByIndex((RsContext)con, (uint32_t)index, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001725 return id;
1726}
Jason Samsd19f10d2009-05-22 14:03:28 -07001727
1728// -----------------------------------
1729
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001730static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001731nFontCreateFromFile(JNIEnv *_env, jobject _this, jlong con,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001732 jstring fileName, jfloat fontSize, jint dpi)
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001733{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001734 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001735 jlong id = (jlong)(uintptr_t)rsFontCreateFromFile((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001736 fileNameUTF.c_str(), fileNameUTF.length(),
1737 fontSize, dpi);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001738
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001739 return id;
1740}
1741
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001742static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001743nFontCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con,
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001744 jstring name, jfloat fontSize, jint dpi, jlong native_asset)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001745{
1746 Asset* asset = reinterpret_cast<Asset*>(native_asset);
1747 AutoJavaStringToUTF8 nameUTF(_env, name);
1748
Tim Murray3aa89c12014-08-18 17:51:22 -07001749 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001750 nameUTF.c_str(), nameUTF.length(),
1751 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001752 asset->getBuffer(false), asset->getLength());
1753 return id;
1754}
1755
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001756static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001757nFontCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001758 jfloat fontSize, jint dpi)
1759{
Adam Lesinskidcb3c652017-01-23 12:58:11 -08001760 Guarded<AssetManager2>* mgr = AssetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001761 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001762 return 0;
1763 }
1764
1765 AutoJavaStringToUTF8 str(_env, _path);
Adam Lesinskidcb3c652017-01-23 12:58:11 -08001766 std::unique_ptr<Asset> asset;
1767 {
1768 ScopedLock<AssetManager2> locked_mgr(*mgr);
1769 asset = locked_mgr->Open(str.c_str(), Asset::ACCESS_BUFFER);
1770 if (asset == nullptr) {
1771 return 0;
1772 }
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001773 }
1774
Tim Murray3aa89c12014-08-18 17:51:22 -07001775 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001776 str.c_str(), str.length(),
1777 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001778 asset->getBuffer(false), asset->getLength());
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001779 return id;
1780}
1781
Jason Samsbd1c3ad2009-08-03 16:03:08 -07001782// -----------------------------------
1783
1784static void
Tim Murray460a0492013-11-19 12:45:54 -08001785nScriptBindAllocation(JNIEnv *_env, jobject _this, jlong con, jlong script, jlong alloc, jint slot)
Jason Samsd19f10d2009-05-22 14:03:28 -07001786{
Andreas Gampe67333922014-11-10 20:35:59 -08001787 if (kLogApi) {
1788 ALOGD("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", (RsContext)con,
1789 (RsScript)script, (RsAllocation)alloc, slot);
1790 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001791 rsScriptBindAllocation((RsContext)con, (RsScript)script, (RsAllocation)alloc, slot);
Jason Samsd19f10d2009-05-22 14:03:28 -07001792}
1793
1794static void
Tim Murray460a0492013-11-19 12:45:54 -08001795nScriptSetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jint val)
Jason Sams4d339932010-05-11 14:03:58 -07001796{
Andreas Gampe67333922014-11-10 20:35:59 -08001797 if (kLogApi) {
1798 ALOGD("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", (RsContext)con, (void *)script,
1799 slot, val);
1800 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001801 rsScriptSetVarI((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001802}
1803
Tim Murray7c4caad2013-04-10 16:21:40 -07001804static jint
Tim Murray460a0492013-11-19 12:45:54 -08001805nScriptGetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001806{
Andreas Gampe67333922014-11-10 20:35:59 -08001807 if (kLogApi) {
1808 ALOGD("nScriptGetVarI, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1809 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001810 int value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001811 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001812 return value;
1813}
1814
Jason Sams4d339932010-05-11 14:03:58 -07001815static void
Tim Murray460a0492013-11-19 12:45:54 -08001816nScriptSetVarObj(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001817{
Andreas Gampe67333922014-11-10 20:35:59 -08001818 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001819 ALOGD("nScriptSetVarObj, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001820 slot, val);
1821 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001822 rsScriptSetVarObj((RsContext)con, (RsScript)script, slot, (RsObjectBase)val);
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001823}
1824
1825static void
Tim Murray460a0492013-11-19 12:45:54 -08001826nScriptSetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Stephen Hines031ec58c2010-10-11 10:54:21 -07001827{
Andreas Gampe67333922014-11-10 20:35:59 -08001828 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001829 ALOGD("nScriptSetVarJ, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001830 slot, val);
1831 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001832 rsScriptSetVarJ((RsContext)con, (RsScript)script, slot, val);
Stephen Hines031ec58c2010-10-11 10:54:21 -07001833}
1834
Tim Murray7c4caad2013-04-10 16:21:40 -07001835static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001836nScriptGetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001837{
Andreas Gampe67333922014-11-10 20:35:59 -08001838 if (kLogApi) {
1839 ALOGD("nScriptGetVarJ, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1840 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001841 jlong value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001842 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001843 return value;
1844}
1845
Stephen Hines031ec58c2010-10-11 10:54:21 -07001846static void
Tim Murray460a0492013-11-19 12:45:54 -08001847nScriptSetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, float val)
Jason Sams4d339932010-05-11 14:03:58 -07001848{
Andreas Gampe67333922014-11-10 20:35:59 -08001849 if (kLogApi) {
1850 ALOGD("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", (RsContext)con, (void *)script,
1851 slot, val);
1852 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001853 rsScriptSetVarF((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001854}
1855
Tim Murray7c4caad2013-04-10 16:21:40 -07001856static jfloat
Tim Murray460a0492013-11-19 12:45:54 -08001857nScriptGetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001858{
Andreas Gampe67333922014-11-10 20:35:59 -08001859 if (kLogApi) {
1860 ALOGD("nScriptGetVarF, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1861 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001862 jfloat value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001863 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001864 return value;
1865}
1866
Jason Sams4d339932010-05-11 14:03:58 -07001867static void
Tim Murray460a0492013-11-19 12:45:54 -08001868nScriptSetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, double val)
Stephen Hinesca54ec32010-09-20 17:20:30 -07001869{
Andreas Gampe67333922014-11-10 20:35:59 -08001870 if (kLogApi) {
1871 ALOGD("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", (RsContext)con, (void *)script,
1872 slot, val);
1873 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001874 rsScriptSetVarD((RsContext)con, (RsScript)script, slot, val);
Stephen Hinesca54ec32010-09-20 17:20:30 -07001875}
1876
Tim Murray7c4caad2013-04-10 16:21:40 -07001877static jdouble
Tim Murray460a0492013-11-19 12:45:54 -08001878nScriptGetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001879{
Andreas Gampe67333922014-11-10 20:35:59 -08001880 if (kLogApi) {
1881 ALOGD("nScriptGetVarD, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1882 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001883 jdouble value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001884 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001885 return value;
1886}
1887
Stephen Hinesca54ec32010-09-20 17:20:30 -07001888static void
Tim Murray460a0492013-11-19 12:45:54 -08001889nScriptSetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001890{
Andreas Gampe67333922014-11-10 20:35:59 -08001891 if (kLogApi) {
1892 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1893 }
Jason Sams4d339932010-05-11 14:03:58 -07001894 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001895 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001896 if (ptr == nullptr) {
1897 ALOGE("Failed to get Java array elements");
1898 return;
1899 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001900 rsScriptSetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001901 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1902}
1903
Stephen Hinesadeb8092012-04-20 14:26:06 -07001904static void
Tim Murray460a0492013-11-19 12:45:54 -08001905nScriptGetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Tim Murray7c4caad2013-04-10 16:21:40 -07001906{
Andreas Gampe67333922014-11-10 20:35:59 -08001907 if (kLogApi) {
1908 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1909 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001910 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001911 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001912 if (ptr == nullptr) {
1913 ALOGE("Failed to get Java array elements");
1914 return;
1915 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001916 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Stephen Hines414fa2c2014-04-17 01:02:42 -07001917 _env->ReleaseByteArrayElements(data, ptr, 0);
Tim Murray7c4caad2013-04-10 16:21:40 -07001918}
1919
1920static void
Andreas Gampe67333922014-11-10 20:35:59 -08001921nScriptSetVarVE(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data,
1922 jlong elem, jintArray dims)
Stephen Hinesadeb8092012-04-20 14:26:06 -07001923{
Andreas Gampe67333922014-11-10 20:35:59 -08001924 if (kLogApi) {
1925 ALOGD("nScriptSetVarVE, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1926 }
Stephen Hinesadeb8092012-04-20 14:26:06 -07001927 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001928 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001929 if (ptr == nullptr) {
1930 ALOGE("Failed to get Java array elements");
1931 return;
1932 }
Stephen Hinesadeb8092012-04-20 14:26:06 -07001933 jint dimsLen = _env->GetArrayLength(dims) * sizeof(int);
Chris Wailes488230c32014-08-14 11:22:40 -07001934 jint *dimsPtr = _env->GetIntArrayElements(dims, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001935 if (dimsPtr == nullptr) {
1936 ALOGE("Failed to get Java array elements");
1937 return;
1938 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001939 rsScriptSetVarVE((RsContext)con, (RsScript)script, slot, ptr, len, (RsElement)elem,
Stephen Hinesbc5d3ee2014-06-25 00:03:39 -07001940 (const uint32_t*) dimsPtr, dimsLen);
Stephen Hinesadeb8092012-04-20 14:26:06 -07001941 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1942 _env->ReleaseIntArrayElements(dims, dimsPtr, JNI_ABORT);
1943}
1944
Jason Samsd19f10d2009-05-22 14:03:28 -07001945
1946static void
Tim Murray460a0492013-11-19 12:45:54 -08001947nScriptSetTimeZone(JNIEnv *_env, jobject _this, jlong con, jlong script, jbyteArray timeZone)
Jason Samsd19f10d2009-05-22 14:03:28 -07001948{
Andreas Gampe67333922014-11-10 20:35:59 -08001949 if (kLogApi) {
1950 ALOGD("nScriptCSetTimeZone, con(%p), s(%p)", (RsContext)con, (void *)script);
1951 }
Romain Guy584a3752009-07-30 18:45:01 -07001952
1953 jint length = _env->GetArrayLength(timeZone);
1954 jbyte* timeZone_ptr;
1955 timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
Miao Wangba8766c2015-10-12 17:24:13 -07001956 if (timeZone_ptr == nullptr) {
1957 ALOGE("Failed to get Java array elements");
1958 return;
1959 }
Romain Guy584a3752009-07-30 18:45:01 -07001960
Tim Murrayeff663f2013-11-15 13:08:30 -08001961 rsScriptSetTimeZone((RsContext)con, (RsScript)script, (const char *)timeZone_ptr, length);
Romain Guy584a3752009-07-30 18:45:01 -07001962
1963 if (timeZone_ptr) {
1964 _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
1965 }
1966}
1967
Jason Samsfbf0b9e2009-08-13 12:59:04 -07001968static void
Tim Murray460a0492013-11-19 12:45:54 -08001969nScriptInvoke(JNIEnv *_env, jobject _this, jlong con, jlong obj, jint slot)
Jason Samsbe2e8412009-09-16 15:04:38 -07001970{
Andreas Gampe67333922014-11-10 20:35:59 -08001971 if (kLogApi) {
1972 ALOGD("nScriptInvoke, con(%p), script(%p)", (RsContext)con, (void *)obj);
1973 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001974 rsScriptInvoke((RsContext)con, (RsScript)obj, slot);
Jason Samsbe2e8412009-09-16 15:04:38 -07001975}
1976
1977static void
Tim Murray460a0492013-11-19 12:45:54 -08001978nScriptInvokeV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001979{
Andreas Gampe67333922014-11-10 20:35:59 -08001980 if (kLogApi) {
1981 ALOGD("nScriptInvokeV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1982 }
Jason Sams4d339932010-05-11 14:03:58 -07001983 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001984 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001985 if (ptr == nullptr) {
1986 ALOGE("Failed to get Java array elements");
1987 return;
1988 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001989 rsScriptInvokeV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001990 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1991}
1992
Jason Sams6e494d32011-04-27 16:33:11 -07001993static void
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001994nScriptForEach(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
1995 jlongArray ains, jlong aout, jbyteArray params,
1996 jintArray limits)
Jason Sams6e494d32011-04-27 16:33:11 -07001997{
Andreas Gampe67333922014-11-10 20:35:59 -08001998 if (kLogApi) {
Chih-Hung Hsieh9eb9dd32015-05-06 14:42:04 -07001999 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 -08002000 }
Jason Sams6e494d32011-04-27 16:33:11 -07002001
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002002 jint in_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002003 jlong *in_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002004
Chris Wailes488230c32014-08-14 11:22:40 -07002005 RsAllocation *in_allocs = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002006
Chris Wailes488230c32014-08-14 11:22:40 -07002007 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002008 in_len = _env->GetArrayLength(ains);
Yang Ni7b2a46f2015-05-05 12:41:19 -07002009 if (in_len > (jint)RS_KERNEL_MAX_ARGUMENTS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -07002010 ALOGE("Too many arguments in kernel launch.");
2011 // TODO (b/20758983): Report back to Java and throw an exception
2012 return;
2013 }
Chris Wailes94961062014-06-11 12:01:28 -07002014
Yang Ni17c2d7a2015-04-30 16:13:54 -07002015 in_ptr = _env->GetLongArrayElements(ains, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002016 if (in_ptr == nullptr) {
2017 ALOGE("Failed to get Java array elements");
2018 return;
2019 }
2020
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002021 if (sizeof(RsAllocation) == sizeof(jlong)) {
2022 in_allocs = (RsAllocation*)in_ptr;
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002023 } else {
2024 // Convert from 64-bit jlong types to the native pointer type.
Chris Wailes94961062014-06-11 12:01:28 -07002025
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002026 in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
Yang Ni17c2d7a2015-04-30 16:13:54 -07002027 if (in_allocs == nullptr) {
2028 ALOGE("Failed launching kernel for lack of memory.");
2029 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2030 return;
2031 }
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002032
2033 for (int index = in_len; --index >= 0;) {
2034 in_allocs[index] = (RsAllocation)in_ptr[index];
2035 }
2036 }
Chris Wailes94961062014-06-11 12:01:28 -07002037 }
2038
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002039 jint param_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002040 jbyte *param_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002041
Chris Wailes488230c32014-08-14 11:22:40 -07002042 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002043 param_len = _env->GetArrayLength(params);
Chris Wailes488230c32014-08-14 11:22:40 -07002044 param_ptr = _env->GetByteArrayElements(params, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002045 if (param_ptr == nullptr) {
2046 ALOGE("Failed to get Java array elements");
2047 return;
2048 }
Chris Wailes94961062014-06-11 12:01:28 -07002049 }
2050
Chris Wailes488230c32014-08-14 11:22:40 -07002051 RsScriptCall sc, *sca = nullptr;
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002052 uint32_t sc_size = 0;
Chris Wailes94961062014-06-11 12:01:28 -07002053
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002054 jint limit_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002055 jint *limit_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002056
Chris Wailes488230c32014-08-14 11:22:40 -07002057 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002058 limit_len = _env->GetArrayLength(limits);
Chris Wailes488230c32014-08-14 11:22:40 -07002059 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002060 if (limit_ptr == nullptr) {
2061 ALOGE("Failed to get Java array elements");
2062 return;
2063 }
Chris Wailes94961062014-06-11 12:01:28 -07002064
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002065 assert(limit_len == 6);
Andreas Gampe67333922014-11-10 20:35:59 -08002066 UNUSED(limit_len); // As the assert might not be compiled.
Chris Wailes94961062014-06-11 12:01:28 -07002067
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002068 sc.xStart = limit_ptr[0];
2069 sc.xEnd = limit_ptr[1];
2070 sc.yStart = limit_ptr[2];
2071 sc.yEnd = limit_ptr[3];
2072 sc.zStart = limit_ptr[4];
2073 sc.zEnd = limit_ptr[5];
2074 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
Jason Sams14331ab2015-01-26 18:14:36 -08002075 sc.arrayStart = 0;
2076 sc.arrayEnd = 0;
2077 sc.array2Start = 0;
2078 sc.array2End = 0;
2079 sc.array3Start = 0;
2080 sc.array3End = 0;
2081 sc.array4Start = 0;
2082 sc.array4End = 0;
Chris Wailes94961062014-06-11 12:01:28 -07002083
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002084 sca = &sc;
Yang Nie8f2e442016-03-15 16:00:02 -07002085 // sc_size is required, but unused, by the runtime and drivers.
2086 sc_size = sizeof(sc);
Chris Wailes94961062014-06-11 12:01:28 -07002087 }
2088
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002089 rsScriptForEachMulti((RsContext)con, (RsScript)script, slot,
2090 in_allocs, in_len, (RsAllocation)aout,
2091 param_ptr, param_len, sca, sc_size);
Chris Wailes94961062014-06-11 12:01:28 -07002092
Chris Wailes488230c32014-08-14 11:22:40 -07002093 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002094 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
Chris Wailes94961062014-06-11 12:01:28 -07002095 }
2096
Chris Wailes488230c32014-08-14 11:22:40 -07002097 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002098 _env->ReleaseByteArrayElements(params, param_ptr, JNI_ABORT);
2099 }
2100
Chris Wailes488230c32014-08-14 11:22:40 -07002101 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002102 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
2103 }
Chris Wailes94961062014-06-11 12:01:28 -07002104}
2105
Matt Wala36eb1f72015-07-20 15:35:27 -07002106static void
2107nScriptReduce(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
David Gross4a457852016-06-02 14:46:55 -07002108 jlongArray ains, jlong aout, jintArray limits)
Matt Wala36eb1f72015-07-20 15:35:27 -07002109{
2110 if (kLogApi) {
David Gross4a457852016-06-02 14:46:55 -07002111 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 -08002112 }
2113
2114 if (ains == nullptr) {
2115 ALOGE("At least one input required.");
2116 // TODO (b/20758983): Report back to Java and throw an exception
2117 return;
2118 }
2119 jint in_len = _env->GetArrayLength(ains);
2120 if (in_len > (jint)RS_KERNEL_MAX_ARGUMENTS) {
2121 ALOGE("Too many arguments in kernel launch.");
2122 // TODO (b/20758983): Report back to Java and throw an exception
2123 return;
2124 }
2125
2126 jlong *in_ptr = _env->GetLongArrayElements(ains, nullptr);
2127 if (in_ptr == nullptr) {
2128 ALOGE("Failed to get Java array elements");
2129 // TODO (b/20758983): Report back to Java and throw an exception
2130 return;
2131 }
2132
2133 RsAllocation *in_allocs = nullptr;
2134 if (sizeof(RsAllocation) == sizeof(jlong)) {
2135 in_allocs = (RsAllocation*)in_ptr;
2136 } else {
2137 // Convert from 64-bit jlong types to the native pointer type.
2138
2139 in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
2140 if (in_allocs == nullptr) {
2141 ALOGE("Failed launching kernel for lack of memory.");
2142 // TODO (b/20758983): Report back to Java and throw an exception
2143 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2144 return;
2145 }
2146
2147 for (int index = in_len; --index >= 0;) {
2148 in_allocs[index] = (RsAllocation)in_ptr[index];
2149 }
2150 }
2151
2152 RsScriptCall sc, *sca = nullptr;
2153 uint32_t sc_size = 0;
2154
2155 jint limit_len = 0;
2156 jint *limit_ptr = nullptr;
2157
2158 if (limits != nullptr) {
2159 limit_len = _env->GetArrayLength(limits);
2160 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
2161 if (limit_ptr == nullptr) {
2162 ALOGE("Failed to get Java array elements");
2163 // TODO (b/20758983): Report back to Java and throw an exception
2164 return;
2165 }
2166
2167 assert(limit_len == 6);
2168 UNUSED(limit_len); // As the assert might not be compiled.
2169
2170 sc.xStart = limit_ptr[0];
2171 sc.xEnd = limit_ptr[1];
2172 sc.yStart = limit_ptr[2];
2173 sc.yEnd = limit_ptr[3];
2174 sc.zStart = limit_ptr[4];
2175 sc.zEnd = limit_ptr[5];
2176 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
2177 sc.arrayStart = 0;
2178 sc.arrayEnd = 0;
2179 sc.array2Start = 0;
2180 sc.array2End = 0;
2181 sc.array3Start = 0;
2182 sc.array3End = 0;
2183 sc.array4Start = 0;
2184 sc.array4End = 0;
2185
2186 sca = &sc;
2187 sc_size = sizeof(sc);
2188 }
2189
David Gross4a457852016-06-02 14:46:55 -07002190 rsScriptReduce((RsContext)con, (RsScript)script, slot,
2191 in_allocs, in_len, (RsAllocation)aout,
2192 sca, sc_size);
David Gross26ef7a732016-01-12 12:19:15 -08002193
2194 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2195
2196 if (limits != nullptr) {
2197 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
2198 }
2199}
2200
Jason Sams22534172009-08-04 16:58:20 -07002201// -----------------------------------
2202
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002203static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002204nScriptCCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Samse4a06c52011-03-16 16:29:28 -07002205 jstring resName, jstring cacheDir,
2206 jbyteArray scriptRef, jint length)
Jason Sams22534172009-08-04 16:58:20 -07002207{
Andreas Gampe67333922014-11-10 20:35:59 -08002208 if (kLogApi) {
2209 ALOGD("nScriptCCreate, con(%p)", (RsContext)con);
2210 }
Jason Sams22534172009-08-04 16:58:20 -07002211
Jason Samse4a06c52011-03-16 16:29:28 -07002212 AutoJavaStringToUTF8 resNameUTF(_env, resName);
2213 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002214 jlong ret = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002215 jbyte* script_ptr = nullptr;
Jack Palevich43702d82009-05-28 13:38:16 -07002216 jint _exception = 0;
2217 jint remaining;
Jack Palevich43702d82009-05-28 13:38:16 -07002218 if (!scriptRef) {
2219 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002220 //jniThrowException(_env, "java/lang/IllegalArgumentException", "script == null");
Jack Palevich43702d82009-05-28 13:38:16 -07002221 goto exit;
2222 }
Jack Palevich43702d82009-05-28 13:38:16 -07002223 if (length < 0) {
2224 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002225 //jniThrowException(_env, "java/lang/IllegalArgumentException", "length < 0");
Jack Palevich43702d82009-05-28 13:38:16 -07002226 goto exit;
2227 }
Jason Samse4a06c52011-03-16 16:29:28 -07002228 remaining = _env->GetArrayLength(scriptRef);
Jack Palevich43702d82009-05-28 13:38:16 -07002229 if (remaining < length) {
2230 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002231 //jniThrowException(_env, "java/lang/IllegalArgumentException",
2232 // "length > script.length - offset");
Jack Palevich43702d82009-05-28 13:38:16 -07002233 goto exit;
2234 }
Jason Samse4a06c52011-03-16 16:29:28 -07002235 script_ptr = (jbyte *)
Jack Palevich43702d82009-05-28 13:38:16 -07002236 _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
Miao Wangba8766c2015-10-12 17:24:13 -07002237 if (script_ptr == nullptr) {
2238 ALOGE("Failed to get Java array elements");
2239 return ret;
2240 }
Jack Palevich43702d82009-05-28 13:38:16 -07002241
Tim Murrayeff663f2013-11-15 13:08:30 -08002242 //rsScriptCSetText((RsContext)con, (const char *)script_ptr, length);
Jason Samse4a06c52011-03-16 16:29:28 -07002243
Tim Murray3aa89c12014-08-18 17:51:22 -07002244 ret = (jlong)(uintptr_t)rsScriptCCreate((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07002245 resNameUTF.c_str(), resNameUTF.length(),
2246 cacheDirUTF.c_str(), cacheDirUTF.length(),
Jason Samse4a06c52011-03-16 16:29:28 -07002247 (const char *)script_ptr, length);
Jason Sams39ddc9502009-06-05 17:35:09 -07002248
Jack Palevich43702d82009-05-28 13:38:16 -07002249exit:
Jason Samse4a06c52011-03-16 16:29:28 -07002250 if (script_ptr) {
2251 _env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr,
Jack Palevich43702d82009-05-28 13:38:16 -07002252 _exception ? JNI_ABORT: 0);
2253 }
Jason Samsd19f10d2009-05-22 14:03:28 -07002254
Tim Murray3aa89c12014-08-18 17:51:22 -07002255 return (jlong)(uintptr_t)ret;
Jason Samsd19f10d2009-05-22 14:03:28 -07002256}
2257
Tim Murray460a0492013-11-19 12:45:54 -08002258static jlong
2259nScriptIntrinsicCreate(JNIEnv *_env, jobject _this, jlong con, jint id, jlong eid)
Jason Sams6ab97682012-08-10 12:09:43 -07002260{
Andreas Gampe67333922014-11-10 20:35:59 -08002261 if (kLogApi) {
2262 ALOGD("nScriptIntrinsicCreate, con(%p) id(%i) element(%p)", (RsContext)con, id,
2263 (void *)eid);
2264 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002265 return (jlong)(uintptr_t)rsScriptIntrinsicCreate((RsContext)con, id, (RsElement)eid);
Jason Sams6ab97682012-08-10 12:09:43 -07002266}
2267
Tim Murray460a0492013-11-19 12:45:54 -08002268static jlong
2269nScriptKernelIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot, jint sig)
Jason Sams08a81582012-09-18 12:32:10 -07002270{
Andreas Gampe67333922014-11-10 20:35:59 -08002271 if (kLogApi) {
2272 ALOGD("nScriptKernelIDCreate, con(%p) script(%p), slot(%i), sig(%i)", (RsContext)con,
2273 (void *)sid, slot, sig);
2274 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002275 return (jlong)(uintptr_t)rsScriptKernelIDCreate((RsContext)con, (RsScript)sid, slot, sig);
Jason Sams08a81582012-09-18 12:32:10 -07002276}
2277
Tim Murray460a0492013-11-19 12:45:54 -08002278static jlong
Yang Nibe392ad2015-01-23 17:16:02 -08002279nScriptInvokeIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
2280{
2281 if (kLogApi) {
Elliott Hughes7ff53fa2015-02-05 21:36:10 -08002282 ALOGD("nScriptInvokeIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con,
Yang Nibe392ad2015-01-23 17:16:02 -08002283 (void *)sid, slot);
2284 }
2285 return (jlong)(uintptr_t)rsScriptInvokeIDCreate((RsContext)con, (RsScript)sid, slot);
2286}
2287
2288static jlong
Tim Murray460a0492013-11-19 12:45:54 -08002289nScriptFieldIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
Jason Sams08a81582012-09-18 12:32:10 -07002290{
Andreas Gampe67333922014-11-10 20:35:59 -08002291 if (kLogApi) {
2292 ALOGD("nScriptFieldIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con, (void *)sid,
2293 slot);
2294 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002295 return (jlong)(uintptr_t)rsScriptFieldIDCreate((RsContext)con, (RsScript)sid, slot);
Jason Sams08a81582012-09-18 12:32:10 -07002296}
2297
Tim Murray460a0492013-11-19 12:45:54 -08002298static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002299nScriptGroupCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _kernels, jlongArray _src,
2300 jlongArray _dstk, jlongArray _dstf, jlongArray _types)
Jason Sams08a81582012-09-18 12:32:10 -07002301{
Andreas Gampe67333922014-11-10 20:35:59 -08002302 if (kLogApi) {
2303 ALOGD("nScriptGroupCreate, con(%p)", (RsContext)con);
2304 }
Jason Sams08a81582012-09-18 12:32:10 -07002305
Miao Wanga4ad5f82016-02-11 12:32:39 -08002306 jlong id = 0;
2307
2308 RsScriptKernelID* kernelsPtr;
Ashok Bhat98071552014-02-12 09:54:43 +00002309 jint kernelsLen = _env->GetArrayLength(_kernels);
Chris Wailes488230c32014-08-14 11:22:40 -07002310 jlong *jKernelsPtr = _env->GetLongArrayElements(_kernels, nullptr);
Miao Wanga4ad5f82016-02-11 12:32:39 -08002311
2312 RsScriptKernelID* srcPtr;
2313 jint srcLen = _env->GetArrayLength(_src);
2314 jlong *jSrcPtr = _env->GetLongArrayElements(_src, nullptr);
2315
2316 RsScriptKernelID* dstkPtr;
2317 jint dstkLen = _env->GetArrayLength(_dstk);
2318 jlong *jDstkPtr = _env->GetLongArrayElements(_dstk, nullptr);
2319
2320 RsScriptKernelID* dstfPtr;
2321 jint dstfLen = _env->GetArrayLength(_dstf);
2322 jlong *jDstfPtr = _env->GetLongArrayElements(_dstf, nullptr);
2323
2324 RsType* typesPtr;
2325 jint typesLen = _env->GetArrayLength(_types);
2326 jlong *jTypesPtr = _env->GetLongArrayElements(_types, nullptr);
2327
Miao Wangba8766c2015-10-12 17:24:13 -07002328 if (jKernelsPtr == nullptr) {
2329 ALOGE("Failed to get Java array elements: kernels");
Miao Wanga4ad5f82016-02-11 12:32:39 -08002330 goto cleanup;
Miao Wangba8766c2015-10-12 17:24:13 -07002331 }
Miao Wanga4ad5f82016-02-11 12:32:39 -08002332 if (jSrcPtr == nullptr) {
2333 ALOGE("Failed to get Java array elements: src");
2334 goto cleanup;
2335 }
2336 if (jDstkPtr == nullptr) {
2337 ALOGE("Failed to get Java array elements: dstk");
2338 goto cleanup;
2339 }
2340 if (jDstfPtr == nullptr) {
2341 ALOGE("Failed to get Java array elements: dstf");
2342 goto cleanup;
2343 }
2344 if (jTypesPtr == nullptr) {
2345 ALOGE("Failed to get Java array elements: types");
2346 goto cleanup;
2347 }
2348
2349 kernelsPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * kernelsLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002350 for(int i = 0; i < kernelsLen; ++i) {
2351 kernelsPtr[i] = (RsScriptKernelID)jKernelsPtr[i];
2352 }
Jason Sams08a81582012-09-18 12:32:10 -07002353
Miao Wanga4ad5f82016-02-11 12:32:39 -08002354 srcPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * srcLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002355 for(int i = 0; i < srcLen; ++i) {
2356 srcPtr[i] = (RsScriptKernelID)jSrcPtr[i];
2357 }
Jason Sams08a81582012-09-18 12:32:10 -07002358
Miao Wanga4ad5f82016-02-11 12:32:39 -08002359 dstkPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstkLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002360 for(int i = 0; i < dstkLen; ++i) {
2361 dstkPtr[i] = (RsScriptKernelID)jDstkPtr[i];
2362 }
2363
Miao Wanga4ad5f82016-02-11 12:32:39 -08002364 dstfPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstfLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002365 for(int i = 0; i < dstfLen; ++i) {
2366 dstfPtr[i] = (RsScriptKernelID)jDstfPtr[i];
2367 }
2368
Miao Wanga4ad5f82016-02-11 12:32:39 -08002369 typesPtr = (RsType*) malloc(sizeof(RsType) * typesLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002370 for(int i = 0; i < typesLen; ++i) {
2371 typesPtr[i] = (RsType)jTypesPtr[i];
2372 }
2373
Miao Wanga4ad5f82016-02-11 12:32:39 -08002374 id = (jlong)(uintptr_t)rsScriptGroupCreate((RsContext)con,
Ashok Bhat98071552014-02-12 09:54:43 +00002375 (RsScriptKernelID *)kernelsPtr, kernelsLen * sizeof(RsScriptKernelID),
2376 (RsScriptKernelID *)srcPtr, srcLen * sizeof(RsScriptKernelID),
2377 (RsScriptKernelID *)dstkPtr, dstkLen * sizeof(RsScriptKernelID),
2378 (RsScriptFieldID *)dstfPtr, dstfLen * sizeof(RsScriptKernelID),
2379 (RsType *)typesPtr, typesLen * sizeof(RsType));
2380
2381 free(kernelsPtr);
2382 free(srcPtr);
2383 free(dstkPtr);
2384 free(dstfPtr);
2385 free(typesPtr);
Miao Wanga4ad5f82016-02-11 12:32:39 -08002386
2387cleanup:
2388 if (jKernelsPtr != nullptr) {
2389 _env->ReleaseLongArrayElements(_kernels, jKernelsPtr, 0);
2390 }
2391 if (jSrcPtr != nullptr) {
2392 _env->ReleaseLongArrayElements(_src, jSrcPtr, 0);
2393 }
2394 if (jDstkPtr != nullptr) {
2395 _env->ReleaseLongArrayElements(_dstk, jDstkPtr, 0);
2396 }
2397 if (jDstfPtr != nullptr) {
2398 _env->ReleaseLongArrayElements(_dstf, jDstfPtr, 0);
2399 }
2400 if (jTypesPtr != nullptr) {
2401 _env->ReleaseLongArrayElements(_types, jTypesPtr, 0);
2402 }
2403
Jason Sams08a81582012-09-18 12:32:10 -07002404 return id;
2405}
2406
2407static void
Tim Murray460a0492013-11-19 12:45:54 -08002408nScriptGroupSetInput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002409{
Andreas Gampe67333922014-11-10 20:35:59 -08002410 if (kLogApi) {
2411 ALOGD("nScriptGroupSetInput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2412 (void *)gid, (void *)kid, (void *)alloc);
2413 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002414 rsScriptGroupSetInput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002415}
2416
2417static void
Tim Murray460a0492013-11-19 12:45:54 -08002418nScriptGroupSetOutput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002419{
Andreas Gampe67333922014-11-10 20:35:59 -08002420 if (kLogApi) {
2421 ALOGD("nScriptGroupSetOutput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2422 (void *)gid, (void *)kid, (void *)alloc);
2423 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002424 rsScriptGroupSetOutput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002425}
2426
2427static void
Tim Murray460a0492013-11-19 12:45:54 -08002428nScriptGroupExecute(JNIEnv *_env, jobject _this, jlong con, jlong gid)
Jason Sams08a81582012-09-18 12:32:10 -07002429{
Andreas Gampe67333922014-11-10 20:35:59 -08002430 if (kLogApi) {
2431 ALOGD("nScriptGroupSetOutput, con(%p) group(%p)", (RsContext)con, (void *)gid);
2432 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002433 rsScriptGroupExecute((RsContext)con, (RsScriptGroup)gid);
Jason Sams08a81582012-09-18 12:32:10 -07002434}
2435
Jason Samsd19f10d2009-05-22 14:03:28 -07002436// ---------------------------------------------------------------------------
2437
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002438static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002439nProgramStoreCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Sams331bf9b2011-04-06 11:23:54 -07002440 jboolean colorMaskR, jboolean colorMaskG, jboolean colorMaskB, jboolean colorMaskA,
2441 jboolean depthMask, jboolean ditherEnable,
2442 jint srcFunc, jint destFunc,
2443 jint depthFunc)
Jason Samsd19f10d2009-05-22 14:03:28 -07002444{
Andreas Gampe67333922014-11-10 20:35:59 -08002445 if (kLogApi) {
2446 ALOGD("nProgramStoreCreate, con(%p)", (RsContext)con);
2447 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002448 return (jlong)(uintptr_t)rsProgramStoreCreate((RsContext)con, colorMaskR, colorMaskG, colorMaskB, colorMaskA,
Jason Sams331bf9b2011-04-06 11:23:54 -07002449 depthMask, ditherEnable, (RsBlendSrcFunc)srcFunc,
2450 (RsBlendDstFunc)destFunc, (RsDepthFunc)depthFunc);
Jason Samsd19f10d2009-05-22 14:03:28 -07002451}
2452
Jason Sams0011bcf2009-12-15 12:58:36 -08002453// ---------------------------------------------------------------------------
2454
2455static void
Tim Murray460a0492013-11-19 12:45:54 -08002456nProgramBindConstants(JNIEnv *_env, jobject _this, jlong con, jlong vpv, jint slot, jlong a)
Jason Sams0011bcf2009-12-15 12:58:36 -08002457{
Andreas Gampe67333922014-11-10 20:35:59 -08002458 if (kLogApi) {
2459 ALOGD("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", (RsContext)con,
2460 (RsProgramVertex)vpv, slot, (RsAllocation)a);
2461 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002462 rsProgramBindConstants((RsContext)con, (RsProgram)vpv, slot, (RsAllocation)a);
Jason Sams0011bcf2009-12-15 12:58:36 -08002463}
Jason Sams54c0ec12009-11-30 14:49:55 -08002464
Jason Sams68afd012009-12-17 16:55:08 -08002465static void
Tim Murray460a0492013-11-19 12:45:54 -08002466nProgramBindTexture(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002467{
Andreas Gampe67333922014-11-10 20:35:59 -08002468 if (kLogApi) {
2469 ALOGD("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2470 (RsProgramFragment)vpf, slot, (RsAllocation)a);
2471 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002472 rsProgramBindTexture((RsContext)con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
Jason Sams68afd012009-12-17 16:55:08 -08002473}
2474
2475static void
Tim Murray460a0492013-11-19 12:45:54 -08002476nProgramBindSampler(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002477{
Andreas Gampe67333922014-11-10 20:35:59 -08002478 if (kLogApi) {
2479 ALOGD("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2480 (RsProgramFragment)vpf, slot, (RsSampler)a);
2481 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002482 rsProgramBindSampler((RsContext)con, (RsProgramFragment)vpf, slot, (RsSampler)a);
Jason Sams68afd012009-12-17 16:55:08 -08002483}
2484
Jason Samsd19f10d2009-05-22 14:03:28 -07002485// ---------------------------------------------------------------------------
2486
Tim Murray460a0492013-11-19 12:45:54 -08002487static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002488nProgramFragmentCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002489 jobjectArray texNames, jlongArray params)
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002490{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002491 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002492 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002493 jint paramLen = _env->GetArrayLength(params);
Miao Wangba8766c2015-10-12 17:24:13 -07002494 if (jParamPtr == nullptr) {
2495 ALOGE("Failed to get Java array elements");
2496 return 0;
2497 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002498
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002499 int texCount = _env->GetArrayLength(texNames);
2500 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2501 const char ** nameArray = names.c_str();
2502 size_t* sizeArray = names.c_str_len();
2503
Andreas Gampe67333922014-11-10 20:35:59 -08002504 if (kLogApi) {
2505 ALOGD("nProgramFragmentCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2506 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002507
Ashok Bhat98071552014-02-12 09:54:43 +00002508 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2509 for(int i = 0; i < paramLen; ++i) {
2510 paramPtr[i] = (uintptr_t)jParamPtr[i];
2511 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002512 jlong ret = (jlong)(uintptr_t)rsProgramFragmentCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002513 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002514 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002515
Ashok Bhat98071552014-02-12 09:54:43 +00002516 free(paramPtr);
2517 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002518 return ret;
2519}
2520
2521
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002522// ---------------------------------------------------------------------------
2523
Tim Murray460a0492013-11-19 12:45:54 -08002524static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002525nProgramVertexCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002526 jobjectArray texNames, jlongArray params)
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002527{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002528 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002529 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams0011bcf2009-12-15 12:58:36 -08002530 jint paramLen = _env->GetArrayLength(params);
Miao Wangba8766c2015-10-12 17:24:13 -07002531 if (jParamPtr == nullptr) {
2532 ALOGE("Failed to get Java array elements");
2533 return 0;
2534 }
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002535
Andreas Gampe67333922014-11-10 20:35:59 -08002536 if (kLogApi) {
2537 ALOGD("nProgramVertexCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2538 }
Jason Sams0011bcf2009-12-15 12:58:36 -08002539
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002540 int texCount = _env->GetArrayLength(texNames);
2541 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2542 const char ** nameArray = names.c_str();
2543 size_t* sizeArray = names.c_str_len();
2544
Ashok Bhat98071552014-02-12 09:54:43 +00002545 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2546 for(int i = 0; i < paramLen; ++i) {
2547 paramPtr[i] = (uintptr_t)jParamPtr[i];
2548 }
2549
Tim Murray3aa89c12014-08-18 17:51:22 -07002550 jlong ret = (jlong)(uintptr_t)rsProgramVertexCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002551 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002552 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002553
Ashok Bhat98071552014-02-12 09:54:43 +00002554 free(paramPtr);
2555 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams0011bcf2009-12-15 12:58:36 -08002556 return ret;
2557}
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002558
Jason Samsebfb4362009-09-23 13:57:02 -07002559// ---------------------------------------------------------------------------
2560
Tim Murray460a0492013-11-19 12:45:54 -08002561static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002562nProgramRasterCreate(JNIEnv *_env, jobject _this, jlong con, jboolean pointSprite, jint cull)
Jason Samsebfb4362009-09-23 13:57:02 -07002563{
Andreas Gampe67333922014-11-10 20:35:59 -08002564 if (kLogApi) {
2565 ALOGD("nProgramRasterCreate, con(%p), pointSprite(%i), cull(%i)", (RsContext)con,
2566 pointSprite, cull);
2567 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002568 return (jlong)(uintptr_t)rsProgramRasterCreate((RsContext)con, pointSprite, (RsCullMode)cull);
Jason Samsebfb4362009-09-23 13:57:02 -07002569}
2570
Jason Samsd19f10d2009-05-22 14:03:28 -07002571
2572// ---------------------------------------------------------------------------
2573
2574static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002575nContextBindRootScript(JNIEnv *_env, jobject _this, jlong con, jlong script)
Jason Samsd19f10d2009-05-22 14:03:28 -07002576{
Andreas Gampe67333922014-11-10 20:35:59 -08002577 if (kLogApi) {
2578 ALOGD("nContextBindRootScript, con(%p), script(%p)", (RsContext)con, (RsScript)script);
2579 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002580 rsContextBindRootScript((RsContext)con, (RsScript)script);
Jason Samsd19f10d2009-05-22 14:03:28 -07002581}
2582
2583static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002584nContextBindProgramStore(JNIEnv *_env, jobject _this, jlong con, jlong pfs)
Jason Samsd19f10d2009-05-22 14:03:28 -07002585{
Andreas Gampe67333922014-11-10 20:35:59 -08002586 if (kLogApi) {
2587 ALOGD("nContextBindProgramStore, con(%p), pfs(%p)", (RsContext)con, (RsProgramStore)pfs);
2588 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002589 rsContextBindProgramStore((RsContext)con, (RsProgramStore)pfs);
Jason Samsd19f10d2009-05-22 14:03:28 -07002590}
2591
2592static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002593nContextBindProgramFragment(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsd19f10d2009-05-22 14:03:28 -07002594{
Andreas Gampe67333922014-11-10 20:35:59 -08002595 if (kLogApi) {
2596 ALOGD("nContextBindProgramFragment, con(%p), pf(%p)", (RsContext)con,
2597 (RsProgramFragment)pf);
2598 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002599 rsContextBindProgramFragment((RsContext)con, (RsProgramFragment)pf);
Jason Samsd19f10d2009-05-22 14:03:28 -07002600}
2601
Jason Sams0826a6f2009-06-15 19:04:56 -07002602static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002603nContextBindProgramVertex(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Sams0826a6f2009-06-15 19:04:56 -07002604{
Andreas Gampe67333922014-11-10 20:35:59 -08002605 if (kLogApi) {
2606 ALOGD("nContextBindProgramVertex, con(%p), pf(%p)", (RsContext)con, (RsProgramVertex)pf);
2607 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002608 rsContextBindProgramVertex((RsContext)con, (RsProgramVertex)pf);
Jason Sams0826a6f2009-06-15 19:04:56 -07002609}
2610
Joe Onoratod7b37742009-08-09 22:57:44 -07002611static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002612nContextBindProgramRaster(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsebfb4362009-09-23 13:57:02 -07002613{
Andreas Gampe67333922014-11-10 20:35:59 -08002614 if (kLogApi) {
2615 ALOGD("nContextBindProgramRaster, con(%p), pf(%p)", (RsContext)con, (RsProgramRaster)pf);
2616 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002617 rsContextBindProgramRaster((RsContext)con, (RsProgramRaster)pf);
Jason Samsebfb4362009-09-23 13:57:02 -07002618}
2619
Joe Onoratod7b37742009-08-09 22:57:44 -07002620
Jason Sams02fb2cb2009-05-28 15:37:57 -07002621// ---------------------------------------------------------------------------
2622
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002623static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002624nSamplerCreate(JNIEnv *_env, jobject _this, jlong con, jint magFilter, jint minFilter,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002625 jint wrapS, jint wrapT, jint wrapR, jfloat aniso)
Jason Sams02fb2cb2009-05-28 15:37:57 -07002626{
Andreas Gampe67333922014-11-10 20:35:59 -08002627 if (kLogApi) {
2628 ALOGD("nSamplerCreate, con(%p)", (RsContext)con);
2629 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002630 return (jlong)(uintptr_t)rsSamplerCreate((RsContext)con,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002631 (RsSamplerValue)magFilter,
2632 (RsSamplerValue)minFilter,
2633 (RsSamplerValue)wrapS,
2634 (RsSamplerValue)wrapT,
2635 (RsSamplerValue)wrapR,
2636 aniso);
Jason Sams02fb2cb2009-05-28 15:37:57 -07002637}
2638
Jason Samsbba134c2009-06-22 15:49:21 -07002639// ---------------------------------------------------------------------------
2640
Tim Murray460a0492013-11-19 12:45:54 -08002641static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002642nMeshCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _vtx, jlongArray _idx, jintArray _prim)
Jason Samsbba134c2009-06-22 15:49:21 -07002643{
Andreas Gampe67333922014-11-10 20:35:59 -08002644 if (kLogApi) {
2645 ALOGD("nMeshCreate, con(%p)", (RsContext)con);
2646 }
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002647
Miao Wanga4ad5f82016-02-11 12:32:39 -08002648 jlong id = 0;
2649
2650 RsAllocation* vtxPtr;
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002651 jint vtxLen = _env->GetArrayLength(_vtx);
Chris Wailes488230c32014-08-14 11:22:40 -07002652 jlong *jVtxPtr = _env->GetLongArrayElements(_vtx, nullptr);
Miao Wanga4ad5f82016-02-11 12:32:39 -08002653
2654 RsAllocation* idxPtr;
2655 jint idxLen = _env->GetArrayLength(_idx);
2656 jlong *jIdxPtr = _env->GetLongArrayElements(_idx, nullptr);
2657
2658 jint primLen = _env->GetArrayLength(_prim);
2659 jint *primPtr = _env->GetIntArrayElements(_prim, nullptr);
2660
Miao Wangba8766c2015-10-12 17:24:13 -07002661 if (jVtxPtr == nullptr) {
2662 ALOGE("Failed to get Java array elements: vtx");
Miao Wanga4ad5f82016-02-11 12:32:39 -08002663 goto cleanupMesh;
Miao Wangba8766c2015-10-12 17:24:13 -07002664 }
Miao Wanga4ad5f82016-02-11 12:32:39 -08002665 if (jIdxPtr == nullptr) {
2666 ALOGE("Failed to get Java array elements: idx");
2667 goto cleanupMesh;
2668 }
2669 if (primPtr == nullptr) {
2670 ALOGE("Failed to get Java array elements: prim");
2671 goto cleanupMesh;
2672 }
2673
2674 vtxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * vtxLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002675 for(int i = 0; i < vtxLen; ++i) {
2676 vtxPtr[i] = (RsAllocation)(uintptr_t)jVtxPtr[i];
2677 }
2678
Miao Wanga4ad5f82016-02-11 12:32:39 -08002679 idxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * idxLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002680 for(int i = 0; i < idxLen; ++i) {
2681 idxPtr[i] = (RsAllocation)(uintptr_t)jIdxPtr[i];
2682 }
2683
Miao Wanga4ad5f82016-02-11 12:32:39 -08002684 id = (jlong)(uintptr_t)rsMeshCreate((RsContext)con,
2685 (RsAllocation *)vtxPtr, vtxLen,
2686 (RsAllocation *)idxPtr, idxLen,
2687 (uint32_t *)primPtr, primLen);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002688
Ashok Bhat98071552014-02-12 09:54:43 +00002689 free(vtxPtr);
2690 free(idxPtr);
Miao Wanga4ad5f82016-02-11 12:32:39 -08002691
2692cleanupMesh:
2693 if (jVtxPtr != nullptr) {
2694 _env->ReleaseLongArrayElements(_vtx, jVtxPtr, 0);
2695 }
2696 if (jIdxPtr != nullptr) {
2697 _env->ReleaseLongArrayElements(_idx, jIdxPtr, 0);
2698 }
2699 if (primPtr != nullptr) {
2700 _env->ReleaseIntArrayElements(_prim, primPtr, 0);
2701 }
2702
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002703 return id;
2704}
2705
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002706static jint
Tim Murray460a0492013-11-19 12:45:54 -08002707nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002708{
Andreas Gampe67333922014-11-10 20:35:59 -08002709 if (kLogApi) {
2710 ALOGD("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2711 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002712 jint vtxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002713 rsaMeshGetVertexBufferCount((RsContext)con, (RsMesh)mesh, &vtxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002714 return vtxCount;
2715}
2716
2717static jint
Tim Murray460a0492013-11-19 12:45:54 -08002718nMeshGetIndexCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002719{
Andreas Gampe67333922014-11-10 20:35:59 -08002720 if (kLogApi) {
2721 ALOGD("nMeshGetIndexCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2722 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002723 jint idxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002724 rsaMeshGetIndexCount((RsContext)con, (RsMesh)mesh, &idxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002725 return idxCount;
2726}
2727
2728static void
Ashok Bhat98071552014-02-12 09:54:43 +00002729nMeshGetVertices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _ids, jint numVtxIDs)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002730{
Andreas Gampe67333922014-11-10 20:35:59 -08002731 if (kLogApi) {
2732 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2733 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002734
2735 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
Tim Murrayeff663f2013-11-15 13:08:30 -08002736 rsaMeshGetVertices((RsContext)con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002737
2738 for(jint i = 0; i < numVtxIDs; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002739 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002740 _env->SetLongArrayRegion(_ids, i, 1, &alloc);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002741 }
2742
2743 free(allocs);
2744}
2745
2746static void
Ashok Bhat98071552014-02-12 09:54:43 +00002747nMeshGetIndices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _idxIds, jintArray _primitives, jint numIndices)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002748{
Andreas Gampe67333922014-11-10 20:35:59 -08002749 if (kLogApi) {
2750 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2751 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002752
2753 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
2754 uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
2755
Tim Murrayeff663f2013-11-15 13:08:30 -08002756 rsaMeshGetIndices((RsContext)con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002757
2758 for(jint i = 0; i < numIndices; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002759 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002760 const jint prim = (jint)prims[i];
2761 _env->SetLongArrayRegion(_idxIds, i, 1, &alloc);
2762 _env->SetIntArrayRegion(_primitives, i, 1, &prim);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002763 }
2764
2765 free(allocs);
2766 free(prims);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002767}
2768
Tim Murray56f9e6f2014-05-16 11:47:26 -07002769static jint
2770nSystemGetPointerSize(JNIEnv *_env, jobject _this) {
2771 return (jint)sizeof(void*);
2772}
2773
Miao Wang0facf022015-11-25 11:21:13 -08002774static jobject
2775nAllocationGetByteBuffer(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
2776 jlongArray strideArr, jint xBytesSize,
2777 jint dimY, jint dimZ) {
2778 if (kLogApi) {
2779 ALOGD("nAllocationGetByteBuffer, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
2780 }
Tim Murray56f9e6f2014-05-16 11:47:26 -07002781
Miao Wang0facf022015-11-25 11:21:13 -08002782 jlong *jStridePtr = _env->GetLongArrayElements(strideArr, nullptr);
2783 if (jStridePtr == nullptr) {
2784 ALOGE("Failed to get Java array elements: strideArr");
2785 return 0;
2786 }
2787
2788 size_t strideIn = xBytesSize;
2789 void* ptr = nullptr;
2790 if (alloc != 0) {
2791 ptr = rsAllocationGetPointer((RsContext)con, (RsAllocation)alloc, 0,
2792 RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, 0, 0,
2793 &strideIn, sizeof(size_t));
2794 }
2795
2796 jobject byteBuffer = nullptr;
2797 if (ptr != nullptr) {
2798 size_t bufferSize = strideIn;
2799 jStridePtr[0] = strideIn;
2800 if (dimY > 0) {
2801 bufferSize *= dimY;
2802 }
2803 if (dimZ > 0) {
2804 bufferSize *= dimZ;
2805 }
2806 byteBuffer = _env->NewDirectByteBuffer(ptr, (jlong) bufferSize);
2807 }
2808 _env->ReleaseLongArrayElements(strideArr, jStridePtr, 0);
2809 return byteBuffer;
2810}
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002811// ---------------------------------------------------------------------------
2812
Jason Samsd19f10d2009-05-22 14:03:28 -07002813
Jason Sams94d8e90a2009-06-10 16:09:05 -07002814static const char *classPathName = "android/renderscript/RenderScript";
Jason Samsd19f10d2009-05-22 14:03:28 -07002815
Daniel Micay76f6a862015-09-19 17:31:01 -04002816static const JNINativeMethod methods[] = {
Jason Sams1c415172010-11-08 17:06:46 -08002817{"_nInit", "()V", (void*)_nInit },
Jason Samsea84a7c2009-09-04 14:42:41 -07002818
Tim Murrayeff663f2013-11-15 13:08:30 -08002819{"nDeviceCreate", "()J", (void*)nDeviceCreate },
2820{"nDeviceDestroy", "(J)V", (void*)nDeviceDestroy },
2821{"nDeviceSetConfig", "(JII)V", (void*)nDeviceSetConfig },
2822{"nContextGetUserMessage", "(J[I)I", (void*)nContextGetUserMessage },
2823{"nContextGetErrorMessage", "(J)Ljava/lang/String;", (void*)nContextGetErrorMessage },
2824{"nContextPeekMessage", "(J[I)I", (void*)nContextPeekMessage },
Jason Sams1c415172010-11-08 17:06:46 -08002825
Tim Murrayeff663f2013-11-15 13:08:30 -08002826{"nContextInitToClient", "(J)V", (void*)nContextInitToClient },
2827{"nContextDeinitToClient", "(J)V", (void*)nContextDeinitToClient },
Jason Samsd19f10d2009-05-22 14:03:28 -07002828
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002829
Jason Sams2e1872f2010-08-17 16:25:41 -07002830// All methods below are thread protected in java.
Tim Murrayeff663f2013-11-15 13:08:30 -08002831{"rsnContextCreate", "(JIII)J", (void*)nContextCreate },
2832{"rsnContextCreateGL", "(JIIIIIIIIIIIIFI)J", (void*)nContextCreateGL },
2833{"rsnContextFinish", "(J)V", (void*)nContextFinish },
2834{"rsnContextSetPriority", "(JI)V", (void*)nContextSetPriority },
Tim Murray47f31582015-04-07 15:43:24 -07002835{"rsnContextSetCacheDir", "(JLjava/lang/String;)V", (void*)nContextSetCacheDir },
Tim Murrayeff663f2013-11-15 13:08:30 -08002836{"rsnContextSetSurface", "(JIILandroid/view/Surface;)V", (void*)nContextSetSurface },
2837{"rsnContextDestroy", "(J)V", (void*)nContextDestroy },
2838{"rsnContextDump", "(JI)V", (void*)nContextDump },
2839{"rsnContextPause", "(J)V", (void*)nContextPause },
2840{"rsnContextResume", "(J)V", (void*)nContextResume },
2841{"rsnContextSendMessage", "(JI[I)V", (void*)nContextSendMessage },
Yang Ni281c3252014-10-24 08:52:24 -07002842{"rsnClosureCreate", "(JJJ[J[J[I[J[J)J", (void*)nClosureCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002843{"rsnInvokeClosureCreate", "(JJ[B[J[J[I)J", (void*)nInvokeClosureCreate },
Yang Ni281c3252014-10-24 08:52:24 -07002844{"rsnClosureSetArg", "(JJIJI)V", (void*)nClosureSetArg },
2845{"rsnClosureSetGlobal", "(JJJJI)V", (void*)nClosureSetGlobal },
Tim Murray460a0492013-11-19 12:45:54 -08002846{"rsnAssignName", "(JJ[B)V", (void*)nAssignName },
2847{"rsnGetName", "(JJ)Ljava/lang/String;", (void*)nGetName },
2848{"rsnObjDestroy", "(JJ)V", (void*)nObjDestroy },
Jason Sams64676f32009-07-08 18:01:53 -07002849
Tim Murray460a0492013-11-19 12:45:54 -08002850{"rsnFileA3DCreateFromFile", "(JLjava/lang/String;)J", (void*)nFileA3DCreateFromFile },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002851{"rsnFileA3DCreateFromAssetStream", "(JJ)J", (void*)nFileA3DCreateFromAssetStream },
Tim Murray460a0492013-11-19 12:45:54 -08002852{"rsnFileA3DCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;)J", (void*)nFileA3DCreateFromAsset },
2853{"rsnFileA3DGetNumIndexEntries", "(JJ)I", (void*)nFileA3DGetNumIndexEntries },
2854{"rsnFileA3DGetIndexEntries", "(JJI[I[Ljava/lang/String;)V", (void*)nFileA3DGetIndexEntries },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002855{"rsnFileA3DGetEntryByIndex", "(JJI)J", (void*)nFileA3DGetEntryByIndex },
Jason Samsd19f10d2009-05-22 14:03:28 -07002856
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002857{"rsnFontCreateFromFile", "(JLjava/lang/String;FI)J", (void*)nFontCreateFromFile },
2858{"rsnFontCreateFromAssetStream", "(JLjava/lang/String;FIJ)J", (void*)nFontCreateFromAssetStream },
2859{"rsnFontCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;FI)J", (void*)nFontCreateFromAsset },
Jason Samsd19f10d2009-05-22 14:03:28 -07002860
Tim Murray460a0492013-11-19 12:45:54 -08002861{"rsnElementCreate", "(JJIZI)J", (void*)nElementCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002862{"rsnElementCreate2", "(J[J[Ljava/lang/String;[I)J", (void*)nElementCreate2 },
Tim Murray460a0492013-11-19 12:45:54 -08002863{"rsnElementGetNativeData", "(JJ[I)V", (void*)nElementGetNativeData },
Ashok Bhat98071552014-02-12 09:54:43 +00002864{"rsnElementGetSubElements", "(JJ[J[Ljava/lang/String;[I)V", (void*)nElementGetSubElements },
Jason Samsd19f10d2009-05-22 14:03:28 -07002865
Tim Murray460a0492013-11-19 12:45:54 -08002866{"rsnTypeCreate", "(JJIIIZZI)J", (void*)nTypeCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002867{"rsnTypeGetNativeData", "(JJ[J)V", (void*)nTypeGetNativeData },
Jason Samsd19f10d2009-05-22 14:03:28 -07002868
Ashok Bhat98071552014-02-12 09:54:43 +00002869{"rsnAllocationCreateTyped", "(JJIIJ)J", (void*)nAllocationCreateTyped },
Tim Murray460a0492013-11-19 12:45:54 -08002870{"rsnAllocationCreateFromBitmap", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateFromBitmap },
2871{"rsnAllocationCreateBitmapBackedAllocation", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateBitmapBackedAllocation },
2872{"rsnAllocationCubeCreateFromBitmap","(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCubeCreateFromBitmap },
Jason Sams5476b452010-12-08 16:14:36 -08002873
Tim Murray460a0492013-11-19 12:45:54 -08002874{"rsnAllocationCopyFromBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyFromBitmap },
2875{"rsnAllocationCopyToBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyToBitmap },
Jason Sams4ef66502010-12-10 16:03:15 -08002876
Tim Murray460a0492013-11-19 12:45:54 -08002877{"rsnAllocationSyncAll", "(JJI)V", (void*)nAllocationSyncAll },
Miao Wang8c150922015-10-26 17:44:10 -07002878{"rsnAllocationSetupBufferQueue", "(JJI)V", (void*)nAllocationSetupBufferQueue },
2879{"rsnAllocationShareBufferQueue", "(JJJ)V", (void*)nAllocationShareBufferQueue },
Tim Murray460a0492013-11-19 12:45:54 -08002880{"rsnAllocationGetSurface", "(JJ)Landroid/view/Surface;", (void*)nAllocationGetSurface },
2881{"rsnAllocationSetSurface", "(JJLandroid/view/Surface;)V", (void*)nAllocationSetSurface },
2882{"rsnAllocationIoSend", "(JJ)V", (void*)nAllocationIoSend },
Miao Wang8c150922015-10-26 17:44:10 -07002883{"rsnAllocationIoReceive", "(JJ)J", (void*)nAllocationIoReceive },
Miao Wang87e908d2015-03-02 15:15:15 -08002884{"rsnAllocationData1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData1D },
Miao Wangc8e237e2015-02-20 18:36:32 -08002885{"rsnAllocationElementData", "(JJIIIII[BI)V", (void*)nAllocationElementData },
Miao Wang87e908d2015-03-02 15:15:15 -08002886{"rsnAllocationData2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData2D },
Tim Murray460a0492013-11-19 12:45:54 -08002887{"rsnAllocationData2D", "(JJIIIIIIJIIII)V", (void*)nAllocationData2D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002888{"rsnAllocationData3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData3D },
Tim Murray460a0492013-11-19 12:45:54 -08002889{"rsnAllocationData3D", "(JJIIIIIIIJIIII)V", (void*)nAllocationData3D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002890{"rsnAllocationRead", "(JJLjava/lang/Object;IIZ)V", (void*)nAllocationRead },
2891{"rsnAllocationRead1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead1D },
Miao Wang45cec0a2015-03-04 16:40:21 -08002892{"rsnAllocationElementRead", "(JJIIIII[BI)V", (void*)nAllocationElementRead },
Miao Wang87e908d2015-03-02 15:15:15 -08002893{"rsnAllocationRead2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead2D },
2894{"rsnAllocationRead3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead3D },
Tim Murray460a0492013-11-19 12:45:54 -08002895{"rsnAllocationGetType", "(JJ)J", (void*)nAllocationGetType},
2896{"rsnAllocationResize1D", "(JJI)V", (void*)nAllocationResize1D },
2897{"rsnAllocationGenerateMipmaps", "(JJ)V", (void*)nAllocationGenerateMipmaps },
Jason Samsbd1c3ad2009-08-03 16:03:08 -07002898
Jason Sams46ba27e32015-02-06 17:45:15 -08002899{"rsnAllocationAdapterCreate", "(JJJ)J", (void*)nAllocationAdapterCreate },
2900{"rsnAllocationAdapterOffset", "(JJIIIIIIIII)V", (void*)nAllocationAdapterOffset },
2901
Tim Murray460a0492013-11-19 12:45:54 -08002902{"rsnScriptBindAllocation", "(JJJI)V", (void*)nScriptBindAllocation },
2903{"rsnScriptSetTimeZone", "(JJ[B)V", (void*)nScriptSetTimeZone },
2904{"rsnScriptInvoke", "(JJI)V", (void*)nScriptInvoke },
2905{"rsnScriptInvokeV", "(JJI[B)V", (void*)nScriptInvokeV },
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002906
2907{"rsnScriptForEach", "(JJI[JJ[B[I)V", (void*)nScriptForEach },
David Gross4a457852016-06-02 14:46:55 -07002908{"rsnScriptReduce", "(JJI[JJ[I)V", (void*)nScriptReduce },
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002909
Tim Murray460a0492013-11-19 12:45:54 -08002910{"rsnScriptSetVarI", "(JJII)V", (void*)nScriptSetVarI },
2911{"rsnScriptGetVarI", "(JJI)I", (void*)nScriptGetVarI },
2912{"rsnScriptSetVarJ", "(JJIJ)V", (void*)nScriptSetVarJ },
2913{"rsnScriptGetVarJ", "(JJI)J", (void*)nScriptGetVarJ },
2914{"rsnScriptSetVarF", "(JJIF)V", (void*)nScriptSetVarF },
2915{"rsnScriptGetVarF", "(JJI)F", (void*)nScriptGetVarF },
2916{"rsnScriptSetVarD", "(JJID)V", (void*)nScriptSetVarD },
2917{"rsnScriptGetVarD", "(JJI)D", (void*)nScriptGetVarD },
2918{"rsnScriptSetVarV", "(JJI[B)V", (void*)nScriptSetVarV },
2919{"rsnScriptGetVarV", "(JJI[B)V", (void*)nScriptGetVarV },
2920{"rsnScriptSetVarVE", "(JJI[BJ[I)V", (void*)nScriptSetVarVE },
2921{"rsnScriptSetVarObj", "(JJIJ)V", (void*)nScriptSetVarObj },
Jason Samsd19f10d2009-05-22 14:03:28 -07002922
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002923{"rsnScriptCCreate", "(JLjava/lang/String;Ljava/lang/String;[BI)J", (void*)nScriptCCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002924{"rsnScriptIntrinsicCreate", "(JIJ)J", (void*)nScriptIntrinsicCreate },
2925{"rsnScriptKernelIDCreate", "(JJII)J", (void*)nScriptKernelIDCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002926{"rsnScriptInvokeIDCreate", "(JJI)J", (void*)nScriptInvokeIDCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002927{"rsnScriptFieldIDCreate", "(JJI)J", (void*)nScriptFieldIDCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002928{"rsnScriptGroupCreate", "(J[J[J[J[J[J)J", (void*)nScriptGroupCreate },
Yang Ni35be56c2015-04-02 17:47:56 -07002929{"rsnScriptGroup2Create", "(JLjava/lang/String;Ljava/lang/String;[J)J", (void*)nScriptGroup2Create },
Tim Murray460a0492013-11-19 12:45:54 -08002930{"rsnScriptGroupSetInput", "(JJJJ)V", (void*)nScriptGroupSetInput },
2931{"rsnScriptGroupSetOutput", "(JJJJ)V", (void*)nScriptGroupSetOutput },
2932{"rsnScriptGroupExecute", "(JJ)V", (void*)nScriptGroupExecute },
Yang Ni281c3252014-10-24 08:52:24 -07002933{"rsnScriptGroup2Execute", "(JJ)V", (void*)nScriptGroup2Execute },
Jason Sams0011bcf2009-12-15 12:58:36 -08002934
Tim Murray25207df2015-01-12 16:47:56 -08002935{"rsnScriptIntrinsicBLAS_Single", "(JJIIIIIIIIIFJJFJIIII)V", (void*)nScriptIntrinsicBLAS_Single },
2936{"rsnScriptIntrinsicBLAS_Double", "(JJIIIIIIIIIDJJDJIIII)V", (void*)nScriptIntrinsicBLAS_Double },
2937{"rsnScriptIntrinsicBLAS_Complex", "(JJIIIIIIIIIFFJJFFJIIII)V", (void*)nScriptIntrinsicBLAS_Complex },
2938{"rsnScriptIntrinsicBLAS_Z", "(JJIIIIIIIIIDDJJDDJIIII)V", (void*)nScriptIntrinsicBLAS_Z },
2939
Tim Murray9cb16a22015-04-01 11:07:16 -07002940{"rsnScriptIntrinsicBLAS_BNNM", "(JJIIIJIJIJII)V", (void*)nScriptIntrinsicBLAS_BNNM },
2941
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002942{"rsnProgramStoreCreate", "(JZZZZZZIII)J", (void*)nProgramStoreCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002943
Tim Murray460a0492013-11-19 12:45:54 -08002944{"rsnProgramBindConstants", "(JJIJ)V", (void*)nProgramBindConstants },
2945{"rsnProgramBindTexture", "(JJIJ)V", (void*)nProgramBindTexture },
2946{"rsnProgramBindSampler", "(JJIJ)V", (void*)nProgramBindSampler },
Jason Samsebfb4362009-09-23 13:57:02 -07002947
Ashok Bhat98071552014-02-12 09:54:43 +00002948{"rsnProgramFragmentCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramFragmentCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002949{"rsnProgramRasterCreate", "(JZI)J", (void*)nProgramRasterCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002950{"rsnProgramVertexCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramVertexCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002951
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002952{"rsnContextBindRootScript", "(JJ)V", (void*)nContextBindRootScript },
2953{"rsnContextBindProgramStore", "(JJ)V", (void*)nContextBindProgramStore },
2954{"rsnContextBindProgramFragment", "(JJ)V", (void*)nContextBindProgramFragment },
2955{"rsnContextBindProgramVertex", "(JJ)V", (void*)nContextBindProgramVertex },
2956{"rsnContextBindProgramRaster", "(JJ)V", (void*)nContextBindProgramRaster },
Jason Sams02fb2cb2009-05-28 15:37:57 -07002957
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002958{"rsnSamplerCreate", "(JIIIIIF)J", (void*)nSamplerCreate },
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002959
Ashok Bhat98071552014-02-12 09:54:43 +00002960{"rsnMeshCreate", "(J[J[J[I)J", (void*)nMeshCreate },
Jason Sams2e1872f2010-08-17 16:25:41 -07002961
Tim Murray460a0492013-11-19 12:45:54 -08002962{"rsnMeshGetVertexBufferCount", "(JJ)I", (void*)nMeshGetVertexBufferCount },
2963{"rsnMeshGetIndexCount", "(JJ)I", (void*)nMeshGetIndexCount },
Ashok Bhat98071552014-02-12 09:54:43 +00002964{"rsnMeshGetVertices", "(JJ[JI)V", (void*)nMeshGetVertices },
2965{"rsnMeshGetIndices", "(JJ[J[II)V", (void*)nMeshGetIndices },
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002966
Tim Murray56f9e6f2014-05-16 11:47:26 -07002967{"rsnSystemGetPointerSize", "()I", (void*)nSystemGetPointerSize },
Miao Wang0facf022015-11-25 11:21:13 -08002968{"rsnAllocationGetByteBuffer", "(JJ[JIII)Ljava/nio/ByteBuffer;", (void*)nAllocationGetByteBuffer },
Jason Samsd19f10d2009-05-22 14:03:28 -07002969};
2970
2971static int registerFuncs(JNIEnv *_env)
2972{
2973 return android::AndroidRuntime::registerNativeMethods(
2974 _env, classPathName, methods, NELEM(methods));
2975}
2976
2977// ---------------------------------------------------------------------------
2978
2979jint JNI_OnLoad(JavaVM* vm, void* reserved)
2980{
Chris Wailes488230c32014-08-14 11:22:40 -07002981 JNIEnv* env = nullptr;
Jason Samsd19f10d2009-05-22 14:03:28 -07002982 jint result = -1;
2983
Jason Samsd19f10d2009-05-22 14:03:28 -07002984 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
Steve Block3762c312012-01-06 19:20:56 +00002985 ALOGE("ERROR: GetEnv failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07002986 goto bail;
2987 }
Chris Wailes488230c32014-08-14 11:22:40 -07002988 assert(env != nullptr);
Jason Samsd19f10d2009-05-22 14:03:28 -07002989
2990 if (registerFuncs(env) < 0) {
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002991 ALOGE("ERROR: Renderscript native registration failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07002992 goto bail;
2993 }
2994
2995 /* success -- return valid version number */
2996 result = JNI_VERSION_1_4;
2997
2998bail:
2999 return result;
3000}