blob: c663255d3315762299b902c28eac5b2c161e45f6 [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
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080027#include <androidfw/Asset.h>
28#include <androidfw/AssetManager.h>
29#include <androidfw/ResourceTypes.h>
Jason Samsf29ca502009-06-23 12:22:47 -070030
Jason Samsd19f10d2009-05-22 14:03:28 -070031#include "jni.h"
32#include "JNIHelp.h"
33#include "android_runtime/AndroidRuntime.h"
Jim Milleree956052010-08-19 18:56:00 -070034#include "android_runtime/android_view_Surface.h"
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -080035#include "android_runtime/android_util_AssetManager.h"
John Reckf4faeac2015-03-05 13:50:31 -080036#include "android/graphics/GraphicsJNI.h"
Miao Wang33287e82017-03-06 09:31:32 -080037#include "android/native_window.h"
38#include "android/native_window_jni.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070039
Jason Sams1d6983a2012-02-16 16:07:49 -080040#include <rsEnv.h>
Miao Wangcbb02062017-01-24 18:58:17 -080041#include <rsApiStubs.h>
Jason Samsfb9aa9f2012-03-28 15:30:07 -070042#include <gui/Surface.h>
Andy McFaddend47f7d82012-12-18 09:48:38 -080043#include <gui/GLConsumer.h>
Jason Samsfaa32b32011-06-20 16:58:04 -070044#include <android_runtime/android_graphics_SurfaceTexture.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070045
Steve Block3762c312012-01-06 19:20:56 +000046//#define LOG_API ALOGE
Andreas Gampe67333922014-11-10 20:35:59 -080047static constexpr bool kLogApi = false;
Jason Samsd19f10d2009-05-22 14:03:28 -070048
49using namespace android;
50
Andreas Gampe67333922014-11-10 20:35:59 -080051template <typename... T>
52void UNUSED(T... t) {}
53
Stephen Hines414fa2c2014-04-17 01:02:42 -070054#define PER_ARRAY_TYPE(flag, fnc, readonly, ...) { \
Jason Samse729a942013-11-06 11:22:02 -080055 jint len = 0; \
Chris Wailes488230c32014-08-14 11:22:40 -070056 void *ptr = nullptr; \
Miao Wang87e908d2015-03-02 15:15:15 -080057 void *srcPtr = nullptr; \
Jason Sams21659ac2013-11-06 15:08:07 -080058 size_t typeBytes = 0; \
Stephen Hines414fa2c2014-04-17 01:02:42 -070059 jint relFlag = 0; \
60 if (readonly) { \
61 /* The on-release mode should only be JNI_ABORT for read-only accesses. */ \
Miao Wang87e908d2015-03-02 15:15:15 -080062 /* readonly = true, also indicates we are copying to the allocation . */ \
Stephen Hines414fa2c2014-04-17 01:02:42 -070063 relFlag = JNI_ABORT; \
64 } \
Jason Samse729a942013-11-06 11:22:02 -080065 switch(dataType) { \
66 case RS_TYPE_FLOAT_32: \
67 len = _env->GetArrayLength((jfloatArray)data); \
68 ptr = _env->GetFloatArrayElements((jfloatArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -070069 if (ptr == nullptr) { \
70 ALOGE("Failed to get Java array elements."); \
71 return; \
72 } \
Jason Sams21659ac2013-11-06 15:08:07 -080073 typeBytes = 4; \
Miao Wang87e908d2015-03-02 15:15:15 -080074 if (usePadding) { \
75 srcPtr = ptr; \
76 len = len / 3 * 4; \
77 if (count == 0) { \
78 count = len / 4; \
79 } \
80 ptr = malloc (len * typeBytes); \
81 if (readonly) { \
82 copyWithPadding(ptr, srcPtr, mSize, count); \
83 fnc(__VA_ARGS__); \
84 } else { \
85 fnc(__VA_ARGS__); \
86 copyWithUnPadding(srcPtr, ptr, mSize, count); \
87 } \
88 free(ptr); \
89 ptr = srcPtr; \
90 } else { \
91 fnc(__VA_ARGS__); \
92 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -070093 _env->ReleaseFloatArrayElements((jfloatArray)data, (jfloat *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -080094 return; \
95 case RS_TYPE_FLOAT_64: \
96 len = _env->GetArrayLength((jdoubleArray)data); \
97 ptr = _env->GetDoubleArrayElements((jdoubleArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -070098 if (ptr == nullptr) { \
99 ALOGE("Failed to get Java array elements."); \
100 return; \
101 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800102 typeBytes = 8; \
Miao Wang87e908d2015-03-02 15:15:15 -0800103 if (usePadding) { \
104 srcPtr = ptr; \
105 len = len / 3 * 4; \
106 if (count == 0) { \
107 count = len / 4; \
108 } \
109 ptr = malloc (len * typeBytes); \
110 if (readonly) { \
111 copyWithPadding(ptr, srcPtr, mSize, count); \
112 fnc(__VA_ARGS__); \
113 } else { \
114 fnc(__VA_ARGS__); \
115 copyWithUnPadding(srcPtr, ptr, mSize, count); \
116 } \
117 free(ptr); \
118 ptr = srcPtr; \
119 } else { \
120 fnc(__VA_ARGS__); \
121 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700122 _env->ReleaseDoubleArrayElements((jdoubleArray)data, (jdouble *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800123 return; \
124 case RS_TYPE_SIGNED_8: \
125 case RS_TYPE_UNSIGNED_8: \
126 len = _env->GetArrayLength((jbyteArray)data); \
127 ptr = _env->GetByteArrayElements((jbyteArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700128 if (ptr == nullptr) { \
129 ALOGE("Failed to get Java array elements."); \
130 return; \
131 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800132 typeBytes = 1; \
Miao Wang87e908d2015-03-02 15:15:15 -0800133 if (usePadding) { \
134 srcPtr = ptr; \
135 len = len / 3 * 4; \
136 if (count == 0) { \
137 count = len / 4; \
138 } \
139 ptr = malloc (len * typeBytes); \
140 if (readonly) { \
141 copyWithPadding(ptr, srcPtr, mSize, count); \
142 fnc(__VA_ARGS__); \
143 } else { \
144 fnc(__VA_ARGS__); \
145 copyWithUnPadding(srcPtr, ptr, mSize, count); \
146 } \
147 free(ptr); \
148 ptr = srcPtr; \
149 } else { \
150 fnc(__VA_ARGS__); \
151 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700152 _env->ReleaseByteArrayElements((jbyteArray)data, (jbyte*)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800153 return; \
154 case RS_TYPE_SIGNED_16: \
155 case RS_TYPE_UNSIGNED_16: \
Pirama Arumuga Nainar13332152016-03-01 20:37:19 -0800156 case RS_TYPE_FLOAT_16: \
Jason Samse729a942013-11-06 11:22:02 -0800157 len = _env->GetArrayLength((jshortArray)data); \
158 ptr = _env->GetShortArrayElements((jshortArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700159 if (ptr == nullptr) { \
160 ALOGE("Failed to get Java array elements."); \
161 return; \
162 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800163 typeBytes = 2; \
Miao Wang87e908d2015-03-02 15:15:15 -0800164 if (usePadding) { \
165 srcPtr = ptr; \
166 len = len / 3 * 4; \
167 if (count == 0) { \
168 count = len / 4; \
169 } \
170 ptr = malloc (len * typeBytes); \
171 if (readonly) { \
172 copyWithPadding(ptr, srcPtr, mSize, count); \
173 fnc(__VA_ARGS__); \
174 } else { \
175 fnc(__VA_ARGS__); \
176 copyWithUnPadding(srcPtr, ptr, mSize, count); \
177 } \
178 free(ptr); \
179 ptr = srcPtr; \
180 } else { \
181 fnc(__VA_ARGS__); \
182 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700183 _env->ReleaseShortArrayElements((jshortArray)data, (jshort *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800184 return; \
185 case RS_TYPE_SIGNED_32: \
186 case RS_TYPE_UNSIGNED_32: \
187 len = _env->GetArrayLength((jintArray)data); \
188 ptr = _env->GetIntArrayElements((jintArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700189 if (ptr == nullptr) { \
190 ALOGE("Failed to get Java array elements."); \
191 return; \
192 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800193 typeBytes = 4; \
Miao Wang87e908d2015-03-02 15:15:15 -0800194 if (usePadding) { \
195 srcPtr = ptr; \
196 len = len / 3 * 4; \
197 if (count == 0) { \
198 count = len / 4; \
199 } \
200 ptr = malloc (len * typeBytes); \
201 if (readonly) { \
202 copyWithPadding(ptr, srcPtr, mSize, count); \
203 fnc(__VA_ARGS__); \
204 } else { \
205 fnc(__VA_ARGS__); \
206 copyWithUnPadding(srcPtr, ptr, mSize, count); \
207 } \
208 free(ptr); \
209 ptr = srcPtr; \
210 } else { \
211 fnc(__VA_ARGS__); \
212 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700213 _env->ReleaseIntArrayElements((jintArray)data, (jint *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800214 return; \
215 case RS_TYPE_SIGNED_64: \
216 case RS_TYPE_UNSIGNED_64: \
217 len = _env->GetArrayLength((jlongArray)data); \
218 ptr = _env->GetLongArrayElements((jlongArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700219 if (ptr == nullptr) { \
220 ALOGE("Failed to get Java array elements."); \
221 return; \
222 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800223 typeBytes = 8; \
Miao Wang87e908d2015-03-02 15:15:15 -0800224 if (usePadding) { \
225 srcPtr = ptr; \
226 len = len / 3 * 4; \
227 if (count == 0) { \
228 count = len / 4; \
229 } \
230 ptr = malloc (len * typeBytes); \
231 if (readonly) { \
232 copyWithPadding(ptr, srcPtr, mSize, count); \
233 fnc(__VA_ARGS__); \
234 } else { \
235 fnc(__VA_ARGS__); \
236 copyWithUnPadding(srcPtr, ptr, mSize, count); \
237 } \
238 free(ptr); \
239 ptr = srcPtr; \
240 } else { \
241 fnc(__VA_ARGS__); \
242 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700243 _env->ReleaseLongArrayElements((jlongArray)data, (jlong *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800244 return; \
245 default: \
246 break; \
247 } \
Miao Wang87e908d2015-03-02 15:15:15 -0800248 UNUSED(len, ptr, srcPtr, typeBytes, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800249}
250
251
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800252class AutoJavaStringToUTF8 {
253public:
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800254 AutoJavaStringToUTF8(JNIEnv* env, jstring str) : fEnv(env), fJStr(str) {
Chris Wailes488230c32014-08-14 11:22:40 -0700255 fCStr = env->GetStringUTFChars(str, nullptr);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800256 fLength = env->GetStringUTFLength(str);
257 }
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800258 ~AutoJavaStringToUTF8() {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800259 fEnv->ReleaseStringUTFChars(fJStr, fCStr);
260 }
261 const char* c_str() const { return fCStr; }
262 jsize length() const { return fLength; }
263
264private:
265 JNIEnv* fEnv;
266 jstring fJStr;
267 const char* fCStr;
268 jsize fLength;
269};
270
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800271class AutoJavaStringArrayToUTF8 {
272public:
273 AutoJavaStringArrayToUTF8(JNIEnv* env, jobjectArray strings, jsize stringsLength)
274 : mEnv(env), mStrings(strings), mStringsLength(stringsLength) {
Chris Wailes488230c32014-08-14 11:22:40 -0700275 mCStrings = nullptr;
276 mSizeArray = nullptr;
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800277 if (stringsLength > 0) {
278 mCStrings = (const char **)calloc(stringsLength, sizeof(char *));
279 mSizeArray = (size_t*)calloc(stringsLength, sizeof(size_t));
280 for (jsize ct = 0; ct < stringsLength; ct ++) {
281 jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
Chris Wailes488230c32014-08-14 11:22:40 -0700282 mCStrings[ct] = mEnv->GetStringUTFChars(s, nullptr);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800283 mSizeArray[ct] = mEnv->GetStringUTFLength(s);
284 }
285 }
286 }
287 ~AutoJavaStringArrayToUTF8() {
288 for (jsize ct=0; ct < mStringsLength; ct++) {
289 jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
290 mEnv->ReleaseStringUTFChars(s, mCStrings[ct]);
291 }
292 free(mCStrings);
293 free(mSizeArray);
294 }
295 const char **c_str() const { return mCStrings; }
296 size_t *c_str_len() const { return mSizeArray; }
297 jsize length() const { return mStringsLength; }
298
299private:
300 JNIEnv *mEnv;
301 jobjectArray mStrings;
302 const char **mCStrings;
303 size_t *mSizeArray;
304 jsize mStringsLength;
305};
306
Jason Samsd19f10d2009-05-22 14:03:28 -0700307// ---------------------------------------------------------------------------
308
Jason Samsffe9f482009-06-01 17:45:53 -0700309static jfieldID gContextId = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -0700310
311static void _nInit(JNIEnv *_env, jclass _this)
312{
Tim Murrayeff663f2013-11-15 13:08:30 -0800313 gContextId = _env->GetFieldID(_this, "mContext", "J");
Jason Samsd19f10d2009-05-22 14:03:28 -0700314}
315
Jason Samsd19f10d2009-05-22 14:03:28 -0700316// ---------------------------------------------------------------------------
317
Miao Wang87e908d2015-03-02 15:15:15 -0800318static void copyWithPadding(void* ptr, void* srcPtr, int mSize, int count) {
319 int sizeBytesPad = mSize * 4;
320 int sizeBytes = mSize * 3;
321 uint8_t *dst = static_cast<uint8_t *>(ptr);
322 uint8_t *src = static_cast<uint8_t *>(srcPtr);
323 for (int i = 0; i < count; i++) {
324 memcpy(dst, src, sizeBytes);
325 dst += sizeBytesPad;
326 src += sizeBytes;
327 }
328}
329
330static void copyWithUnPadding(void* ptr, void* srcPtr, int mSize, int count) {
331 int sizeBytesPad = mSize * 4;
332 int sizeBytes = mSize * 3;
333 uint8_t *dst = static_cast<uint8_t *>(ptr);
334 uint8_t *src = static_cast<uint8_t *>(srcPtr);
335 for (int i = 0; i < count; i++) {
336 memcpy(dst, src, sizeBytes);
337 dst += sizeBytes;
338 src += sizeBytesPad;
339 }
340}
341
342
343// ---------------------------------------------------------------------------
Jason Sams3eaa338e2009-06-10 15:04:38 -0700344static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800345nContextFinish(JNIEnv *_env, jobject _this, jlong con)
Jason Sams96ed4cf2010-06-15 12:15:57 -0700346{
Andreas Gampe67333922014-11-10 20:35:59 -0800347 if (kLogApi) {
348 ALOGD("nContextFinish, con(%p)", (RsContext)con);
349 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800350 rsContextFinish((RsContext)con);
Jason Sams96ed4cf2010-06-15 12:15:57 -0700351}
352
Yang Ni281c3252014-10-24 08:52:24 -0700353static jlong
354nClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong kernelID,
355 jlong returnValue, jlongArray fieldIDArray,
356 jlongArray valueArray, jintArray sizeArray,
357 jlongArray depClosureArray, jlongArray depFieldIDArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700358 jlong ret = 0;
359
Yang Ni281c3252014-10-24 08:52:24 -0700360 jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
361 jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700362 if (jFieldIDs == nullptr) {
363 ALOGE("Failed to get Java array elements: fieldIDs.");
364 return ret;
365 }
366
Yang Ni281c3252014-10-24 08:52:24 -0700367 jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
368 jsize values_length = _env->GetArrayLength(valueArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700369 if (jValues == nullptr) {
370 ALOGE("Failed to get Java array elements: values.");
371 return ret;
372 }
373
Yang Ni17c2d7a2015-04-30 16:13:54 -0700374 jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
Yang Ni281c3252014-10-24 08:52:24 -0700375 jsize sizes_length = _env->GetArrayLength(sizeArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700376 if (jSizes == nullptr) {
377 ALOGE("Failed to get Java array elements: sizes.");
378 return ret;
379 }
380
Yang Ni281c3252014-10-24 08:52:24 -0700381 jlong* jDepClosures =
382 _env->GetLongArrayElements(depClosureArray, nullptr);
383 jsize depClosures_length = _env->GetArrayLength(depClosureArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700384 if (jDepClosures == nullptr) {
385 ALOGE("Failed to get Java array elements: depClosures.");
386 return ret;
387 }
388
Yang Ni281c3252014-10-24 08:52:24 -0700389 jlong* jDepFieldIDs =
390 _env->GetLongArrayElements(depFieldIDArray, nullptr);
391 jsize depFieldIDs_length = _env->GetArrayLength(depFieldIDArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700392 if (jDepFieldIDs == nullptr) {
393 ALOGE("Failed to get Java array elements: depFieldIDs.");
394 return ret;
395 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700396
397 size_t numValues, numDependencies;
398 RsScriptFieldID* fieldIDs;
Yang Ni17c2d7a2015-04-30 16:13:54 -0700399 RsClosure* depClosures;
400 RsScriptFieldID* depFieldIDs;
401
402 if (fieldIDs_length != values_length || values_length != sizes_length) {
403 ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
404 goto exit;
405 }
406
407 numValues = (size_t)fieldIDs_length;
408
409 if (depClosures_length != depFieldIDs_length) {
410 ALOGE("Unmatched closures and field IDs for dependencies in closure creation.");
411 goto exit;
412 }
413
414 numDependencies = (size_t)depClosures_length;
415
416 if (numDependencies > numValues) {
417 ALOGE("Unexpected number of dependencies in closure creation");
418 goto exit;
419 }
420
Yang Ni7b2a46f2015-05-05 12:41:19 -0700421 if (numValues > RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700422 ALOGE("Too many arguments or globals in closure creation");
423 goto exit;
424 }
425
Yang Ni86c5c2d2016-03-25 15:49:07 -0700426 if (numValues > 0) {
427 fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
428 if (fieldIDs == nullptr) {
429 goto exit;
430 }
431 } else {
432 // numValues == 0
433 // alloca(0) implementation is platform-dependent.
434 fieldIDs = nullptr;
Yang Ni17c2d7a2015-04-30 16:13:54 -0700435 }
436
437 for (size_t i = 0; i < numValues; i++) {
438 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
439 }
440
Yang Ni86c5c2d2016-03-25 15:49:07 -0700441 if (numDependencies > 0) {
442 depClosures = (RsClosure*)alloca(sizeof(RsClosure) * numDependencies);
443 if (depClosures == nullptr) {
444 goto exit;
445 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700446
Yang Ni86c5c2d2016-03-25 15:49:07 -0700447 for (size_t i = 0; i < numDependencies; i++) {
448 depClosures[i] = (RsClosure)jDepClosures[i];
449 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700450
Yang Ni86c5c2d2016-03-25 15:49:07 -0700451 depFieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numDependencies);
452 if (depFieldIDs == nullptr) {
453 goto exit;
454 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700455
Yang Ni86c5c2d2016-03-25 15:49:07 -0700456 for (size_t i = 0; i < numDependencies; i++) {
457 depFieldIDs[i] = (RsClosure)jDepFieldIDs[i];
458 }
459 } else {
460 // alloca(0) implementation is platform-dependent.
461 depClosures = nullptr;
462 depFieldIDs = nullptr;
Yang Ni281c3252014-10-24 08:52:24 -0700463 }
464
Yang Ni17c2d7a2015-04-30 16:13:54 -0700465 ret = (jlong)(uintptr_t)rsClosureCreate(
Yang Ni281c3252014-10-24 08:52:24 -0700466 (RsContext)con, (RsScriptKernelID)kernelID, (RsAllocation)returnValue,
Yang Ni263cc902015-11-10 13:27:04 -0800467 fieldIDs, numValues, jValues, numValues,
Yang Ni17c2d7a2015-04-30 16:13:54 -0700468 (int*)jSizes, numValues,
469 depClosures, numDependencies,
470 depFieldIDs, numDependencies);
471
472exit:
473
474 _env->ReleaseLongArrayElements(depFieldIDArray, jDepFieldIDs, JNI_ABORT);
475 _env->ReleaseLongArrayElements(depClosureArray, jDepClosures, JNI_ABORT);
476 _env->ReleaseIntArrayElements (sizeArray, jSizes, JNI_ABORT);
477 _env->ReleaseLongArrayElements(valueArray, jValues, JNI_ABORT);
478 _env->ReleaseLongArrayElements(fieldIDArray, jFieldIDs, JNI_ABORT);
479
480 return ret;
Yang Ni281c3252014-10-24 08:52:24 -0700481}
482
Yang Nibe392ad2015-01-23 17:16:02 -0800483static jlong
484nInvokeClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong invokeID,
485 jbyteArray paramArray, jlongArray fieldIDArray, jlongArray valueArray,
486 jintArray sizeArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700487 jlong ret = 0;
488
Yang Nibe392ad2015-01-23 17:16:02 -0800489 jbyte* jParams = _env->GetByteArrayElements(paramArray, nullptr);
490 jsize jParamLength = _env->GetArrayLength(paramArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700491 if (jParams == nullptr) {
492 ALOGE("Failed to get Java array elements: params.");
493 return ret;
494 }
495
Yang Nibe392ad2015-01-23 17:16:02 -0800496 jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
497 jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700498 if (jFieldIDs == nullptr) {
499 ALOGE("Failed to get Java array elements: fieldIDs.");
500 return ret;
501 }
502
Yang Ni17c2d7a2015-04-30 16:13:54 -0700503 jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
504 jsize values_length = _env->GetArrayLength(valueArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700505 if (jValues == nullptr) {
506 ALOGE("Failed to get Java array elements: values.");
507 return ret;
508 }
509
Yang Ni17c2d7a2015-04-30 16:13:54 -0700510 jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
511 jsize sizes_length = _env->GetArrayLength(sizeArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700512 if (jSizes == nullptr) {
513 ALOGE("Failed to get Java array elements: sizes.");
514 return ret;
515 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700516
517 size_t numValues;
518 RsScriptFieldID* fieldIDs;
Yang Ni17c2d7a2015-04-30 16:13:54 -0700519
520 if (fieldIDs_length != values_length || values_length != sizes_length) {
521 ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
522 goto exit;
523 }
524
525 numValues = (size_t) fieldIDs_length;
526
Yang Ni7b2a46f2015-05-05 12:41:19 -0700527 if (numValues > RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700528 ALOGE("Too many arguments or globals in closure creation");
529 goto exit;
530 }
531
532 fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
533 if (fieldIDs == nullptr) {
534 goto exit;
535 }
536
537 for (size_t i = 0; i< numValues; i++) {
Yang Nibe392ad2015-01-23 17:16:02 -0800538 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
539 }
540
Yang Ni17c2d7a2015-04-30 16:13:54 -0700541 ret = (jlong)(uintptr_t)rsInvokeClosureCreate(
Yang Nibe392ad2015-01-23 17:16:02 -0800542 (RsContext)con, (RsScriptInvokeID)invokeID, jParams, jParamLength,
Yang Ni263cc902015-11-10 13:27:04 -0800543 fieldIDs, numValues, jValues, numValues,
Yang Ni17c2d7a2015-04-30 16:13:54 -0700544 (int*)jSizes, numValues);
545
546exit:
547
548 _env->ReleaseIntArrayElements (sizeArray, jSizes, JNI_ABORT);
549 _env->ReleaseLongArrayElements(valueArray, jValues, JNI_ABORT);
550 _env->ReleaseLongArrayElements(fieldIDArray, jFieldIDs, JNI_ABORT);
551 _env->ReleaseByteArrayElements(paramArray, jParams, JNI_ABORT);
552
553 return ret;
Yang Nibe392ad2015-01-23 17:16:02 -0800554}
555
Yang Ni281c3252014-10-24 08:52:24 -0700556static void
557nClosureSetArg(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
558 jint index, jlong value, jint size) {
Yang Ni263cc902015-11-10 13:27:04 -0800559 // Size is signed with -1 indicating the value is an Allocation
Yang Ni281c3252014-10-24 08:52:24 -0700560 rsClosureSetArg((RsContext)con, (RsClosure)closureID, (uint32_t)index,
Yang Ni263cc902015-11-10 13:27:04 -0800561 (uintptr_t)value, size);
Yang Ni281c3252014-10-24 08:52:24 -0700562}
563
564static void
565nClosureSetGlobal(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
566 jlong fieldID, jlong value, jint size) {
Yang Ni263cc902015-11-10 13:27:04 -0800567 // Size is signed with -1 indicating the value is an Allocation
Yang Ni281c3252014-10-24 08:52:24 -0700568 rsClosureSetGlobal((RsContext)con, (RsClosure)closureID,
Yang Ni263cc902015-11-10 13:27:04 -0800569 (RsScriptFieldID)fieldID, (int64_t)value, size);
Yang Ni281c3252014-10-24 08:52:24 -0700570}
571
572static long
Yang Ni35be56c2015-04-02 17:47:56 -0700573nScriptGroup2Create(JNIEnv *_env, jobject _this, jlong con, jstring name,
Yang Niebf63402015-01-16 11:06:26 -0800574 jstring cacheDir, jlongArray closureArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700575 jlong ret = 0;
576
Yang Ni35be56c2015-04-02 17:47:56 -0700577 AutoJavaStringToUTF8 nameUTF(_env, name);
Yang Niebf63402015-01-16 11:06:26 -0800578 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
579
Yang Ni281c3252014-10-24 08:52:24 -0700580 jlong* jClosures = _env->GetLongArrayElements(closureArray, nullptr);
581 jsize numClosures = _env->GetArrayLength(closureArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700582 if (jClosures == nullptr) {
583 ALOGE("Failed to get Java array elements: closures.");
584 return ret;
585 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700586
587 RsClosure* closures;
588
Yang Ni7b2a46f2015-05-05 12:41:19 -0700589 if (numClosures > (jsize) RS_SCRIPT_GROUP_MAX_NUMBER_CLOSURES) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700590 ALOGE("Too many closures in script group");
591 goto exit;
592 }
593
594 closures = (RsClosure*)alloca(sizeof(RsClosure) * numClosures);
595 if (closures == nullptr) {
596 goto exit;
597 }
598
Yang Ni281c3252014-10-24 08:52:24 -0700599 for (int i = 0; i < numClosures; i++) {
600 closures[i] = (RsClosure)jClosures[i];
601 }
602
Yang Ni17c2d7a2015-04-30 16:13:54 -0700603 ret = (jlong)(uintptr_t)rsScriptGroup2Create(
Yang Ni35be56c2015-04-02 17:47:56 -0700604 (RsContext)con, nameUTF.c_str(), nameUTF.length(),
605 cacheDirUTF.c_str(), cacheDirUTF.length(),
Yang Niebf63402015-01-16 11:06:26 -0800606 closures, numClosures);
Yang Ni17c2d7a2015-04-30 16:13:54 -0700607
608exit:
609
610 _env->ReleaseLongArrayElements(closureArray, jClosures, JNI_ABORT);
611
612 return ret;
Yang Ni281c3252014-10-24 08:52:24 -0700613}
614
615static void
616nScriptGroup2Execute(JNIEnv *_env, jobject _this, jlong con, jlong groupID) {
617 rsScriptGroupExecute((RsContext)con, (RsScriptGroup2)groupID);
618}
619
Jason Sams96ed4cf2010-06-15 12:15:57 -0700620static void
Tim Murray25207df2015-01-12 16:47:56 -0800621nScriptIntrinsicBLAS_Single(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
622 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
623 jfloat alpha, jlong A, jlong B, jfloat beta, jlong C, jint incX, jint incY,
624 jint KL, jint KU) {
625 RsBlasCall call;
626 memset(&call, 0, sizeof(call));
627 call.func = (RsBlasFunction)func;
628 call.transA = (RsBlasTranspose)TransA;
629 call.transB = (RsBlasTranspose)TransB;
630 call.side = (RsBlasSide)Side;
631 call.uplo = (RsBlasUplo)Uplo;
632 call.diag = (RsBlasDiag)Diag;
633 call.M = M;
634 call.N = N;
635 call.K = K;
636 call.alpha.f = alpha;
637 call.beta.f = beta;
638 call.incX = incX;
639 call.incY = incY;
640 call.KL = KL;
641 call.KU = KU;
642
643 RsAllocation in_allocs[3];
644 in_allocs[0] = (RsAllocation)A;
645 in_allocs[1] = (RsAllocation)B;
646 in_allocs[2] = (RsAllocation)C;
647
648 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wangb742fcc2016-10-06 10:45:42 -0700649 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800650 &call, sizeof(call), nullptr, 0);
651}
652
653static void
654nScriptIntrinsicBLAS_Double(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
655 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
656 jdouble alpha, jlong A, jlong B, jdouble beta, jlong C, jint incX, jint incY,
657 jint KL, jint KU) {
658 RsBlasCall call;
659 memset(&call, 0, sizeof(call));
660 call.func = (RsBlasFunction)func;
661 call.transA = (RsBlasTranspose)TransA;
662 call.transB = (RsBlasTranspose)TransB;
663 call.side = (RsBlasSide)Side;
664 call.uplo = (RsBlasUplo)Uplo;
665 call.diag = (RsBlasDiag)Diag;
666 call.M = M;
667 call.N = N;
668 call.K = K;
669 call.alpha.d = alpha;
670 call.beta.d = beta;
671 call.incX = incX;
672 call.incY = incY;
673 call.KL = KL;
674 call.KU = KU;
675
676 RsAllocation in_allocs[3];
677 in_allocs[0] = (RsAllocation)A;
678 in_allocs[1] = (RsAllocation)B;
679 in_allocs[2] = (RsAllocation)C;
680
681 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700682 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800683 &call, sizeof(call), nullptr, 0);
684}
685
686static void
687nScriptIntrinsicBLAS_Complex(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
688 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
689 jfloat alphaX, jfloat alphaY, jlong A, jlong B, jfloat betaX,
690 jfloat betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
691 RsBlasCall call;
692 memset(&call, 0, sizeof(call));
693 call.func = (RsBlasFunction)func;
694 call.transA = (RsBlasTranspose)TransA;
695 call.transB = (RsBlasTranspose)TransB;
696 call.side = (RsBlasSide)Side;
697 call.uplo = (RsBlasUplo)Uplo;
698 call.diag = (RsBlasDiag)Diag;
699 call.M = M;
700 call.N = N;
701 call.K = K;
702 call.alpha.c.r = alphaX;
703 call.alpha.c.i = alphaY;
704 call.beta.c.r = betaX;
Miao Wang82585b32015-04-30 13:44:49 -0700705 call.beta.c.i = betaY;
Tim Murray25207df2015-01-12 16:47:56 -0800706 call.incX = incX;
707 call.incY = incY;
708 call.KL = KL;
709 call.KU = KU;
710
711 RsAllocation in_allocs[3];
712 in_allocs[0] = (RsAllocation)A;
713 in_allocs[1] = (RsAllocation)B;
714 in_allocs[2] = (RsAllocation)C;
715
716 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700717 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800718 &call, sizeof(call), nullptr, 0);
719}
720
721static void
722nScriptIntrinsicBLAS_Z(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
723 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
724 jdouble alphaX, jdouble alphaY, jlong A, jlong B, jdouble betaX,
725 jdouble betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
726 RsBlasCall call;
727 memset(&call, 0, sizeof(call));
728 call.func = (RsBlasFunction)func;
729 call.transA = (RsBlasTranspose)TransA;
730 call.transB = (RsBlasTranspose)TransB;
731 call.side = (RsBlasSide)Side;
732 call.uplo = (RsBlasUplo)Uplo;
733 call.diag = (RsBlasDiag)Diag;
734 call.M = M;
735 call.N = N;
736 call.K = K;
737 call.alpha.z.r = alphaX;
738 call.alpha.z.i = alphaY;
739 call.beta.z.r = betaX;
Miao Wang82585b32015-04-30 13:44:49 -0700740 call.beta.z.i = betaY;
Tim Murray25207df2015-01-12 16:47:56 -0800741 call.incX = incX;
742 call.incY = incY;
743 call.KL = KL;
744 call.KU = KU;
745
746 RsAllocation in_allocs[3];
747 in_allocs[0] = (RsAllocation)A;
748 in_allocs[1] = (RsAllocation)B;
749 in_allocs[2] = (RsAllocation)C;
750
751 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700752 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800753 &call, sizeof(call), nullptr, 0);
754}
755
756
757static void
Tim Murray9cb16a22015-04-01 11:07:16 -0700758nScriptIntrinsicBLAS_BNNM(JNIEnv *_env, jobject _this, jlong con, jlong id, jint M, jint N, jint K,
759 jlong A, jint a_offset, jlong B, jint b_offset, jlong C, jint c_offset,
760 jint c_mult_int) {
761 RsBlasCall call;
762 memset(&call, 0, sizeof(call));
763 call.func = RsBlas_bnnm;
764 call.M = M;
765 call.N = N;
766 call.K = K;
Miao Wang25148062015-06-29 17:43:03 -0700767 call.a_offset = a_offset & 0xFF;
768 call.b_offset = b_offset & 0xFF;
Tim Murray9cb16a22015-04-01 11:07:16 -0700769 call.c_offset = c_offset;
770 call.c_mult_int = c_mult_int;
771
772 RsAllocation in_allocs[3];
773 in_allocs[0] = (RsAllocation)A;
774 in_allocs[1] = (RsAllocation)B;
775 in_allocs[2] = (RsAllocation)C;
776
777 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700778 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray9cb16a22015-04-01 11:07:16 -0700779 &call, sizeof(call), nullptr, 0);
780}
781
782
783static void
Tim Murray460a0492013-11-19 12:45:54 -0800784nAssignName(JNIEnv *_env, jobject _this, jlong con, jlong obj, jbyteArray str)
Jason Sams3eaa338e2009-06-10 15:04:38 -0700785{
Andreas Gampe67333922014-11-10 20:35:59 -0800786 if (kLogApi) {
787 ALOGD("nAssignName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
788 }
Jason Sams3eaa338e2009-06-10 15:04:38 -0700789 jint len = _env->GetArrayLength(str);
790 jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
Miao Wangba8766c2015-10-12 17:24:13 -0700791 if (cptr == nullptr) {
792 ALOGE("Failed to get Java array elements");
793 return;
794 }
795
Tim Murrayeff663f2013-11-15 13:08:30 -0800796 rsAssignName((RsContext)con, (void *)obj, (const char *)cptr, len);
Jason Sams3eaa338e2009-06-10 15:04:38 -0700797 _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
798}
799
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700800static jstring
Tim Murray460a0492013-11-19 12:45:54 -0800801nGetName(JNIEnv *_env, jobject _this, jlong con, jlong obj)
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700802{
Andreas Gampe67333922014-11-10 20:35:59 -0800803 if (kLogApi) {
804 ALOGD("nGetName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
805 }
Chris Wailes488230c32014-08-14 11:22:40 -0700806 const char *name = nullptr;
Tim Murrayeff663f2013-11-15 13:08:30 -0800807 rsaGetName((RsContext)con, (void *)obj, &name);
Chris Wailes488230c32014-08-14 11:22:40 -0700808 if(name == nullptr || strlen(name) == 0) {
809 return nullptr;
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700810 }
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700811 return _env->NewStringUTF(name);
812}
813
Jason Sams7ce033d2009-08-18 14:14:24 -0700814static void
Tim Murray460a0492013-11-19 12:45:54 -0800815nObjDestroy(JNIEnv *_env, jobject _this, jlong con, jlong obj)
Jason Sams7ce033d2009-08-18 14:14:24 -0700816{
Andreas Gampe67333922014-11-10 20:35:59 -0800817 if (kLogApi) {
818 ALOGD("nObjDestroy, con(%p) obj(%p)", (RsContext)con, (void *)obj);
819 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800820 rsObjDestroy((RsContext)con, (void *)obj);
Jason Sams7ce033d2009-08-18 14:14:24 -0700821}
822
Jason Sams3eaa338e2009-06-10 15:04:38 -0700823// ---------------------------------------------------------------------------
824
Tim Murrayeff663f2013-11-15 13:08:30 -0800825static jlong
Jason Samsd19f10d2009-05-22 14:03:28 -0700826nDeviceCreate(JNIEnv *_env, jobject _this)
827{
Andreas Gampe67333922014-11-10 20:35:59 -0800828 if (kLogApi) {
829 ALOGD("nDeviceCreate");
830 }
Tim Murray3aa89c12014-08-18 17:51:22 -0700831 return (jlong)(uintptr_t)rsDeviceCreate();
Jason Samsd19f10d2009-05-22 14:03:28 -0700832}
833
834static void
Tim Murray5eaf4682014-01-10 11:25:52 -0800835nDeviceDestroy(JNIEnv *_env, jobject _this, jlong dev)
Jason Samsd19f10d2009-05-22 14:03:28 -0700836{
Andreas Gampe67333922014-11-10 20:35:59 -0800837 if (kLogApi) {
838 ALOGD("nDeviceDestroy");
839 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700840 return rsDeviceDestroy((RsDevice)dev);
841}
842
Jason Samsebfb4362009-09-23 13:57:02 -0700843static void
Tim Murray5eaf4682014-01-10 11:25:52 -0800844nDeviceSetConfig(JNIEnv *_env, jobject _this, jlong dev, jint p, jint value)
Jason Samsebfb4362009-09-23 13:57:02 -0700845{
Andreas Gampe67333922014-11-10 20:35:59 -0800846 if (kLogApi) {
847 ALOGD("nDeviceSetConfig dev(%p), param(%i), value(%i)", (void *)dev, p, value);
848 }
Jason Samsebfb4362009-09-23 13:57:02 -0700849 return rsDeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value);
850}
851
Tim Murrayeff663f2013-11-15 13:08:30 -0800852static jlong
Jason Sams81cd2b12014-12-02 12:36:43 -0800853nContextCreate(JNIEnv *_env, jobject _this, jlong dev, jint flags, jint sdkVer, jint contextType)
Jason Samsd19f10d2009-05-22 14:03:28 -0700854{
Andreas Gampe67333922014-11-10 20:35:59 -0800855 if (kLogApi) {
856 ALOGD("nContextCreate");
857 }
Jason Sams81cd2b12014-12-02 12:36:43 -0800858 return (jlong)(uintptr_t)rsContextCreate((RsDevice)dev, 0, sdkVer, (RsContextType)contextType, flags);
Jason Sams704ff642010-02-09 16:05:07 -0800859}
860
Tim Murrayeff663f2013-11-15 13:08:30 -0800861static jlong
Tim Murray5eaf4682014-01-10 11:25:52 -0800862nContextCreateGL(JNIEnv *_env, jobject _this, jlong dev, jint ver, jint sdkVer,
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000863 jint colorMin, jint colorPref,
864 jint alphaMin, jint alphaPref,
865 jint depthMin, jint depthPref,
866 jint stencilMin, jint stencilPref,
867 jint samplesMin, jint samplesPref, jfloat samplesQ,
868 jint dpi)
Jason Sams704ff642010-02-09 16:05:07 -0800869{
Yang Ni86c5c2d2016-03-25 15:49:07 -0700870 RsSurfaceConfig sc = {};
Jason Sams11c8af92010-10-13 15:31:10 -0700871 sc.alphaMin = alphaMin;
872 sc.alphaPref = alphaPref;
873 sc.colorMin = colorMin;
874 sc.colorPref = colorPref;
875 sc.depthMin = depthMin;
876 sc.depthPref = depthPref;
877 sc.samplesMin = samplesMin;
878 sc.samplesPref = samplesPref;
879 sc.samplesQ = samplesQ;
880
Andreas Gampe67333922014-11-10 20:35:59 -0800881 if (kLogApi) {
882 ALOGD("nContextCreateGL");
883 }
Tim Murray3aa89c12014-08-18 17:51:22 -0700884 return (jlong)(uintptr_t)rsContextCreateGL((RsDevice)dev, ver, sdkVer, sc, dpi);
Jason Samsd19f10d2009-05-22 14:03:28 -0700885}
886
887static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800888nContextSetPriority(JNIEnv *_env, jobject _this, jlong con, jint p)
Jason Sams7d787b42009-11-15 12:14:26 -0800889{
Andreas Gampe67333922014-11-10 20:35:59 -0800890 if (kLogApi) {
891 ALOGD("ContextSetPriority, con(%p), priority(%i)", (RsContext)con, p);
892 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800893 rsContextSetPriority((RsContext)con, p);
Jason Sams7d787b42009-11-15 12:14:26 -0800894}
895
Tim Murray47f31582015-04-07 15:43:24 -0700896static void
897nContextSetCacheDir(JNIEnv *_env, jobject _this, jlong con, jstring cacheDir)
898{
899 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
900
901 if (kLogApi) {
902 ALOGD("ContextSetCacheDir, con(%p), cacheDir(%s)", (RsContext)con, cacheDirUTF.c_str());
903 }
904 rsContextSetCacheDir((RsContext)con, cacheDirUTF.c_str(), cacheDirUTF.length());
905}
906
Jason Sams7d787b42009-11-15 12:14:26 -0800907
908
909static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800910nContextSetSurface(JNIEnv *_env, jobject _this, jlong con, jint width, jint height, jobject wnd)
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800911{
Andreas Gampe67333922014-11-10 20:35:59 -0800912 if (kLogApi) {
913 ALOGD("nContextSetSurface, con(%p), width(%i), height(%i), surface(%p)", (RsContext)con,
914 width, height, (Surface *)wnd);
915 }
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800916
Chris Wailes488230c32014-08-14 11:22:40 -0700917 ANativeWindow * window = nullptr;
918 if (wnd == nullptr) {
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800919
920 } else {
Jeff Brown64a55af2012-08-26 02:47:39 -0700921 window = android_view_Surface_getNativeWindow(_env, wnd).get();
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800922 }
923
Tim Murrayeff663f2013-11-15 13:08:30 -0800924 rsContextSetSurface((RsContext)con, width, height, window);
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800925}
926
927static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800928nContextDestroy(JNIEnv *_env, jobject _this, jlong con)
Jason Samsd19f10d2009-05-22 14:03:28 -0700929{
Andreas Gampe67333922014-11-10 20:35:59 -0800930 if (kLogApi) {
931 ALOGD("nContextDestroy, con(%p)", (RsContext)con);
932 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800933 rsContextDestroy((RsContext)con);
Jason Samsd19f10d2009-05-22 14:03:28 -0700934}
935
Jason Sams715333b2009-11-17 17:26:46 -0800936static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800937nContextDump(JNIEnv *_env, jobject _this, jlong con, jint bits)
Jason Sams715333b2009-11-17 17:26:46 -0800938{
Andreas Gampe67333922014-11-10 20:35:59 -0800939 if (kLogApi) {
940 ALOGD("nContextDump, con(%p) bits(%i)", (RsContext)con, bits);
941 }
Jason Sams715333b2009-11-17 17:26:46 -0800942 rsContextDump((RsContext)con, bits);
943}
Jason Samsd19f10d2009-05-22 14:03:28 -0700944
945static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800946nContextPause(JNIEnv *_env, jobject _this, jlong con)
Jason Sams65e7aa52009-09-24 17:38:20 -0700947{
Andreas Gampe67333922014-11-10 20:35:59 -0800948 if (kLogApi) {
949 ALOGD("nContextPause, con(%p)", (RsContext)con);
950 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800951 rsContextPause((RsContext)con);
Jason Sams65e7aa52009-09-24 17:38:20 -0700952}
953
954static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800955nContextResume(JNIEnv *_env, jobject _this, jlong con)
Jason Sams65e7aa52009-09-24 17:38:20 -0700956{
Andreas Gampe67333922014-11-10 20:35:59 -0800957 if (kLogApi) {
958 ALOGD("nContextResume, con(%p)", (RsContext)con);
959 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800960 rsContextResume((RsContext)con);
Jason Sams65e7aa52009-09-24 17:38:20 -0700961}
962
Jason Sams1c415172010-11-08 17:06:46 -0800963
964static jstring
Tim Murrayeff663f2013-11-15 13:08:30 -0800965nContextGetErrorMessage(JNIEnv *_env, jobject _this, jlong con)
Jason Sams1c415172010-11-08 17:06:46 -0800966{
Andreas Gampe67333922014-11-10 20:35:59 -0800967 if (kLogApi) {
968 ALOGD("nContextGetErrorMessage, con(%p)", (RsContext)con);
969 }
Jason Sams1c415172010-11-08 17:06:46 -0800970 char buf[1024];
971
972 size_t receiveLen;
973 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800974 int id = rsContextGetMessage((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -0700975 buf, sizeof(buf),
976 &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -0700977 &subID, sizeof(subID));
Jason Sams1c415172010-11-08 17:06:46 -0800978 if (!id && receiveLen) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +0100979 ALOGV("message receive buffer too small. %zu", receiveLen);
Jason Sams1c415172010-11-08 17:06:46 -0800980 }
981 return _env->NewStringUTF(buf);
982}
983
Jason Samsedbfabd2011-05-17 15:01:29 -0700984static jint
Tim Murrayeff663f2013-11-15 13:08:30 -0800985nContextGetUserMessage(JNIEnv *_env, jobject _this, jlong con, jintArray data)
Jason Sams516c3192009-10-06 13:58:47 -0700986{
Jason Sams516c3192009-10-06 13:58:47 -0700987 jint len = _env->GetArrayLength(data);
Andreas Gampe67333922014-11-10 20:35:59 -0800988 if (kLogApi) {
989 ALOGD("nContextGetMessage, con(%p), len(%i)", (RsContext)con, len);
990 }
Chris Wailes488230c32014-08-14 11:22:40 -0700991 jint *ptr = _env->GetIntArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -0700992 if (ptr == nullptr) {
993 ALOGE("Failed to get Java array elements");
994 return 0;
995 }
Jason Sams516c3192009-10-06 13:58:47 -0700996 size_t receiveLen;
Jason Sams1c415172010-11-08 17:06:46 -0800997 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800998 int id = rsContextGetMessage((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -0700999 ptr, len * 4,
1000 &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -07001001 &subID, sizeof(subID));
Jason Sams516c3192009-10-06 13:58:47 -07001002 if (!id && receiveLen) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001003 ALOGV("message receive buffer too small. %zu", receiveLen);
Jason Sams516c3192009-10-06 13:58:47 -07001004 }
1005 _env->ReleaseIntArrayElements(data, ptr, 0);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001006 return (jint)id;
Jason Sams1c415172010-11-08 17:06:46 -08001007}
1008
1009static jint
Tim Murrayeff663f2013-11-15 13:08:30 -08001010nContextPeekMessage(JNIEnv *_env, jobject _this, jlong con, jintArray auxData)
Jason Sams1c415172010-11-08 17:06:46 -08001011{
Andreas Gampe67333922014-11-10 20:35:59 -08001012 if (kLogApi) {
1013 ALOGD("nContextPeekMessage, con(%p)", (RsContext)con);
1014 }
Chris Wailes488230c32014-08-14 11:22:40 -07001015 jint *auxDataPtr = _env->GetIntArrayElements(auxData, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001016 if (auxDataPtr == nullptr) {
1017 ALOGE("Failed to get Java array elements");
1018 return 0;
1019 }
Jason Sams1c415172010-11-08 17:06:46 -08001020 size_t receiveLen;
1021 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -08001022 int id = rsContextPeekMessage((RsContext)con, &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -07001023 &subID, sizeof(subID));
Jason Sams1c415172010-11-08 17:06:46 -08001024 auxDataPtr[0] = (jint)subID;
1025 auxDataPtr[1] = (jint)receiveLen;
1026 _env->ReleaseIntArrayElements(auxData, auxDataPtr, 0);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001027 return (jint)id;
Jason Sams516c3192009-10-06 13:58:47 -07001028}
1029
Tim Murrayeff663f2013-11-15 13:08:30 -08001030static void nContextInitToClient(JNIEnv *_env, jobject _this, jlong con)
Jason Sams516c3192009-10-06 13:58:47 -07001031{
Andreas Gampe67333922014-11-10 20:35:59 -08001032 if (kLogApi) {
1033 ALOGD("nContextInitToClient, con(%p)", (RsContext)con);
1034 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001035 rsContextInitToClient((RsContext)con);
Jason Sams516c3192009-10-06 13:58:47 -07001036}
1037
Tim Murrayeff663f2013-11-15 13:08:30 -08001038static void nContextDeinitToClient(JNIEnv *_env, jobject _this, jlong con)
Jason Sams516c3192009-10-06 13:58:47 -07001039{
Andreas Gampe67333922014-11-10 20:35:59 -08001040 if (kLogApi) {
1041 ALOGD("nContextDeinitToClient, con(%p)", (RsContext)con);
1042 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001043 rsContextDeinitToClient((RsContext)con);
Jason Sams516c3192009-10-06 13:58:47 -07001044}
1045
Jason Sams455d6442013-02-05 19:20:18 -08001046static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001047nContextSendMessage(JNIEnv *_env, jobject _this, jlong con, jint id, jintArray data)
Jason Sams455d6442013-02-05 19:20:18 -08001048{
Chris Wailes488230c32014-08-14 11:22:40 -07001049 jint *ptr = nullptr;
Jason Sams455d6442013-02-05 19:20:18 -08001050 jint len = 0;
1051 if (data) {
1052 len = _env->GetArrayLength(data);
Stephen Hines4a043c12014-08-21 23:20:32 -07001053 ptr = _env->GetIntArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001054 if (ptr == nullptr) {
1055 ALOGE("Failed to get Java array elements");
1056 return;
1057 }
Jason Sams455d6442013-02-05 19:20:18 -08001058 }
Andreas Gampe67333922014-11-10 20:35:59 -08001059 if (kLogApi) {
1060 ALOGD("nContextSendMessage, con(%p), id(%i), len(%i)", (RsContext)con, id, len);
1061 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001062 rsContextSendMessage((RsContext)con, id, (const uint8_t *)ptr, len * sizeof(int));
Jason Sams455d6442013-02-05 19:20:18 -08001063 if (data) {
1064 _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
1065 }
1066}
1067
1068
Jason Sams516c3192009-10-06 13:58:47 -07001069
Tim Murray460a0492013-11-19 12:45:54 -08001070static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001071nElementCreate(JNIEnv *_env, jobject _this, jlong con, jlong type, jint kind, jboolean norm,
1072 jint size)
Jason Samsd19f10d2009-05-22 14:03:28 -07001073{
Andreas Gampe67333922014-11-10 20:35:59 -08001074 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001075 ALOGD("nElementCreate, con(%p), type(%" PRId64 "), kind(%i), norm(%i), size(%i)", (RsContext)con,
Andreas Gampe67333922014-11-10 20:35:59 -08001076 type, kind, norm, size);
1077 }
1078 return (jlong)(uintptr_t)rsElementCreate((RsContext)con, (RsDataType)type, (RsDataKind)kind,
Yang Ni8c8daea2016-03-08 21:01:54 +00001079 norm, size);
Jason Samsd19f10d2009-05-22 14:03:28 -07001080}
1081
Tim Murray460a0492013-11-19 12:45:54 -08001082static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001083nElementCreate2(JNIEnv *_env, jobject _this, jlong con,
Ashok Bhat98071552014-02-12 09:54:43 +00001084 jlongArray _ids, jobjectArray _names, jintArray _arraySizes)
Jason Samsd19f10d2009-05-22 14:03:28 -07001085{
Jason Sams718cd1f2009-12-23 14:35:29 -08001086 int fieldCount = _env->GetArrayLength(_ids);
Andreas Gampe67333922014-11-10 20:35:59 -08001087 if (kLogApi) {
1088 ALOGD("nElementCreate2, con(%p)", (RsContext)con);
1089 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001090
Chris Wailes488230c32014-08-14 11:22:40 -07001091 jlong *jIds = _env->GetLongArrayElements(_ids, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001092 if (jIds == nullptr) {
1093 ALOGE("Failed to get Java array elements: ids");
1094 return 0;
1095 }
Chris Wailes488230c32014-08-14 11:22:40 -07001096 jint *jArraySizes = _env->GetIntArrayElements(_arraySizes, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001097 if (jArraySizes == nullptr) {
1098 ALOGE("Failed to get Java array elements: arraySizes");
1099 return 0;
1100 }
Ashok Bhat98071552014-02-12 09:54:43 +00001101
1102 RsElement *ids = (RsElement*)malloc(fieldCount * sizeof(RsElement));
1103 uint32_t *arraySizes = (uint32_t *)malloc(fieldCount * sizeof(uint32_t));
1104
1105 for(int i = 0; i < fieldCount; i ++) {
1106 ids[i] = (RsElement)jIds[i];
1107 arraySizes[i] = (uint32_t)jArraySizes[i];
1108 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001109
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08001110 AutoJavaStringArrayToUTF8 names(_env, _names, fieldCount);
1111
1112 const char **nameArray = names.c_str();
1113 size_t *sizeArray = names.c_str_len();
1114
Tim Murray3aa89c12014-08-18 17:51:22 -07001115 jlong id = (jlong)(uintptr_t)rsElementCreate2((RsContext)con,
Ashok Bhat98071552014-02-12 09:54:43 +00001116 (const RsElement *)ids, fieldCount,
Jason Sams7a22e102011-05-06 14:14:30 -07001117 nameArray, fieldCount * sizeof(size_t), sizeArray,
Yang Ni8c8daea2016-03-08 21:01:54 +00001118 (const uint32_t *)arraySizes, fieldCount);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08001119
Ashok Bhat98071552014-02-12 09:54:43 +00001120 free(ids);
1121 free(arraySizes);
1122 _env->ReleaseLongArrayElements(_ids, jIds, JNI_ABORT);
1123 _env->ReleaseIntArrayElements(_arraySizes, jArraySizes, JNI_ABORT);
1124
Tim Murray3aa89c12014-08-18 17:51:22 -07001125 return (jlong)(uintptr_t)id;
Jason Samsd19f10d2009-05-22 14:03:28 -07001126}
1127
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001128static void
Tim Murray460a0492013-11-19 12:45:54 -08001129nElementGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jintArray _elementData)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001130{
1131 int dataSize = _env->GetArrayLength(_elementData);
Andreas Gampe67333922014-11-10 20:35:59 -08001132 if (kLogApi) {
1133 ALOGD("nElementGetNativeData, con(%p)", (RsContext)con);
1134 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001135
1136 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
1137 assert(dataSize == 5);
1138
Miao Wangcbb02062017-01-24 18:58:17 -08001139 uint32_t elementData[5];
Tim Murrayeff663f2013-11-15 13:08:30 -08001140 rsaElementGetNativeData((RsContext)con, (RsElement)id, elementData, dataSize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001141
1142 for(jint i = 0; i < dataSize; i ++) {
Ashok Bhat98071552014-02-12 09:54:43 +00001143 const jint data = (jint)elementData[i];
1144 _env->SetIntArrayRegion(_elementData, i, 1, &data);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001145 }
1146}
1147
1148
1149static void
Tim Murray460a0492013-11-19 12:45:54 -08001150nElementGetSubElements(JNIEnv *_env, jobject _this, jlong con, jlong id,
Ashok Bhat98071552014-02-12 09:54:43 +00001151 jlongArray _IDs,
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -07001152 jobjectArray _names,
1153 jintArray _arraySizes)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001154{
Ashok Bhat98071552014-02-12 09:54:43 +00001155 uint32_t dataSize = _env->GetArrayLength(_IDs);
Andreas Gampe67333922014-11-10 20:35:59 -08001156 if (kLogApi) {
1157 ALOGD("nElementGetSubElements, con(%p)", (RsContext)con);
1158 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001159
Ashok Bhat98071552014-02-12 09:54:43 +00001160 uintptr_t *ids = (uintptr_t*)malloc(dataSize * sizeof(uintptr_t));
1161 const char **names = (const char **)malloc(dataSize * sizeof(const char *));
Miao Wangcbb02062017-01-24 18:58:17 -08001162 size_t *arraySizes = (size_t *)malloc(dataSize * sizeof(size_t));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001163
Andreas Gampe67333922014-11-10 20:35:59 -08001164 rsaElementGetSubElements((RsContext)con, (RsElement)id, ids, names, arraySizes,
1165 (uint32_t)dataSize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001166
Ashok Bhat98071552014-02-12 09:54:43 +00001167 for(uint32_t i = 0; i < dataSize; i++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07001168 const jlong id = (jlong)(uintptr_t)ids[i];
Ashok Bhat98071552014-02-12 09:54:43 +00001169 const jint arraySize = (jint)arraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001170 _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
Ashok Bhat98071552014-02-12 09:54:43 +00001171 _env->SetLongArrayRegion(_IDs, i, 1, &id);
1172 _env->SetIntArrayRegion(_arraySizes, i, 1, &arraySize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001173 }
1174
1175 free(ids);
1176 free(names);
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -07001177 free(arraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001178}
1179
Jason Samsd19f10d2009-05-22 14:03:28 -07001180// -----------------------------------
1181
Tim Murray460a0492013-11-19 12:45:54 -08001182static jlong
1183nTypeCreate(JNIEnv *_env, jobject _this, jlong con, jlong eid,
Jason Samsb109cc72013-01-07 18:20:12 -08001184 jint dimx, jint dimy, jint dimz, jboolean mips, jboolean faces, jint yuv)
Jason Samsd19f10d2009-05-22 14:03:28 -07001185{
Andreas Gampe67333922014-11-10 20:35:59 -08001186 if (kLogApi) {
1187 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 +01001188 (RsContext)con, (void*)eid, dimx, dimy, dimz, mips, faces, yuv);
Andreas Gampe67333922014-11-10 20:35:59 -08001189 }
Jason Sams3b9c52a2010-10-14 17:48:46 -07001190
Andreas Gampe67333922014-11-10 20:35:59 -08001191 return (jlong)(uintptr_t)rsTypeCreate((RsContext)con, (RsElement)eid, dimx, dimy, dimz, mips,
Yang Ni8c8daea2016-03-08 21:01:54 +00001192 faces, yuv);
Jason Samsd19f10d2009-05-22 14:03:28 -07001193}
1194
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001195static void
Ashok Bhat98071552014-02-12 09:54:43 +00001196nTypeGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jlongArray _typeData)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001197{
1198 // We are packing 6 items: mDimX; mDimY; mDimZ;
1199 // mDimLOD; mDimFaces; mElement; into typeData
1200 int elementCount = _env->GetArrayLength(_typeData);
1201
1202 assert(elementCount == 6);
Andreas Gampe67333922014-11-10 20:35:59 -08001203 if (kLogApi) {
1204 ALOGD("nTypeGetNativeData, con(%p)", (RsContext)con);
1205 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001206
Ashok Bhat98071552014-02-12 09:54:43 +00001207 uintptr_t typeData[6];
Tim Murrayeff663f2013-11-15 13:08:30 -08001208 rsaTypeGetNativeData((RsContext)con, (RsType)id, typeData, 6);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001209
1210 for(jint i = 0; i < elementCount; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07001211 const jlong data = (jlong)(uintptr_t)typeData[i];
Ashok Bhat98071552014-02-12 09:54:43 +00001212 _env->SetLongArrayRegion(_typeData, i, 1, &data);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001213 }
1214}
1215
Jason Samsd19f10d2009-05-22 14:03:28 -07001216// -----------------------------------
1217
Tim Murray460a0492013-11-19 12:45:54 -08001218static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001219nAllocationCreateTyped(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mips, jint usage,
1220 jlong pointer)
Jason Samsd19f10d2009-05-22 14:03:28 -07001221{
Andreas Gampe67333922014-11-10 20:35:59 -08001222 if (kLogApi) {
1223 ALOGD("nAllocationCreateTyped, con(%p), type(%p), mip(%i), usage(%i), ptr(%p)",
1224 (RsContext)con, (RsElement)type, mips, usage, (void *)pointer);
1225 }
1226 return (jlong)(uintptr_t) rsAllocationCreateTyped((RsContext)con, (RsType)type,
1227 (RsAllocationMipmapControl)mips,
Yang Ni8c8daea2016-03-08 21:01:54 +00001228 (uint32_t)usage, (uintptr_t)pointer);
Jason Samsd19f10d2009-05-22 14:03:28 -07001229}
1230
Jason Samsd19f10d2009-05-22 14:03:28 -07001231static void
Tim Murray460a0492013-11-19 12:45:54 -08001232nAllocationSyncAll(JNIEnv *_env, jobject _this, jlong con, jlong a, jint bits)
Jason Sams5476b452010-12-08 16:14:36 -08001233{
Andreas Gampe67333922014-11-10 20:35:59 -08001234 if (kLogApi) {
1235 ALOGD("nAllocationSyncAll, con(%p), a(%p), bits(0x%08x)", (RsContext)con, (RsAllocation)a,
1236 bits);
1237 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001238 rsAllocationSyncAll((RsContext)con, (RsAllocation)a, (RsAllocationUsageType)bits);
Jason Sams5476b452010-12-08 16:14:36 -08001239}
1240
Miao Wang8c150922015-10-26 17:44:10 -07001241static void
1242nAllocationSetupBufferQueue(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint numAlloc)
1243{
1244 if (kLogApi) {
1245 ALOGD("nAllocationSetupBufferQueue, con(%p), alloc(%p), numAlloc(%d)", (RsContext)con,
1246 (RsAllocation)alloc, numAlloc);
1247 }
1248 rsAllocationSetupBufferQueue((RsContext)con, (RsAllocation)alloc, (uint32_t)numAlloc);
1249}
1250
1251static void
1252nAllocationShareBufferQueue(JNIEnv *_env, jobject _this, jlong con, jlong alloc1, jlong alloc2)
1253{
1254 if (kLogApi) {
1255 ALOGD("nAllocationShareBufferQueue, con(%p), alloc1(%p), alloc2(%p)", (RsContext)con,
1256 (RsAllocation)alloc1, (RsAllocation)alloc2);
1257 }
1258
1259 rsAllocationShareBufferQueue((RsContext)con, (RsAllocation)alloc1, (RsAllocation)alloc2);
1260}
1261
Jason Sams72226e02013-02-22 12:45:54 -08001262static jobject
Tim Murray460a0492013-11-19 12:45:54 -08001263nAllocationGetSurface(JNIEnv *_env, jobject _this, jlong con, jlong a)
Jason Sams615e7ce2012-01-13 14:01:20 -08001264{
Andreas Gampe67333922014-11-10 20:35:59 -08001265 if (kLogApi) {
1266 ALOGD("nAllocationGetSurface, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1267 }
Jason Sams615e7ce2012-01-13 14:01:20 -08001268
Andreas Gampe67333922014-11-10 20:35:59 -08001269 IGraphicBufferProducer *v = (IGraphicBufferProducer *)rsAllocationGetSurface((RsContext)con,
1270 (RsAllocation)a);
Jason Sams72226e02013-02-22 12:45:54 -08001271 sp<IGraphicBufferProducer> bp = v;
Chris Wailes488230c32014-08-14 11:22:40 -07001272 v->decStrong(nullptr);
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001273
Jason Sams72226e02013-02-22 12:45:54 -08001274 jobject o = android_view_Surface_createFromIGraphicBufferProducer(_env, bp);
1275 return o;
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001276}
1277
1278static void
Tim Murray460a0492013-11-19 12:45:54 -08001279nAllocationSetSurface(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject sur)
Jason Sams163766c2012-02-15 12:04:24 -08001280{
Andreas Gampe67333922014-11-10 20:35:59 -08001281 if (kLogApi) {
1282 ALOGD("nAllocationSetSurface, con(%p), alloc(%p), surface(%p)", (RsContext)con,
1283 (RsAllocation)alloc, (Surface *)sur);
1284 }
Jason Sams163766c2012-02-15 12:04:24 -08001285
Miao Wang33287e82017-03-06 09:31:32 -08001286 ANativeWindow *anw = nullptr;
Jason Sams163766c2012-02-15 12:04:24 -08001287 if (sur != 0) {
Miao Wang33287e82017-03-06 09:31:32 -08001288 anw = ANativeWindow_fromSurface(_env, sur);
Jason Sams163766c2012-02-15 12:04:24 -08001289 }
1290
Miao Wang33287e82017-03-06 09:31:32 -08001291 rsAllocationSetSurface((RsContext)con, (RsAllocation)alloc, anw);
Jason Sams163766c2012-02-15 12:04:24 -08001292}
1293
1294static void
Tim Murray460a0492013-11-19 12:45:54 -08001295nAllocationIoSend(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Sams163766c2012-02-15 12:04:24 -08001296{
Andreas Gampe67333922014-11-10 20:35:59 -08001297 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001298 ALOGD("nAllocationIoSend, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
Andreas Gampe67333922014-11-10 20:35:59 -08001299 }
Tim Murray460a0492013-11-19 12:45:54 -08001300 rsAllocationIoSend((RsContext)con, (RsAllocation)alloc);
Jason Sams163766c2012-02-15 12:04:24 -08001301}
1302
Miao Wang8c150922015-10-26 17:44:10 -07001303static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001304nAllocationIoReceive(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Sams163766c2012-02-15 12:04:24 -08001305{
Andreas Gampe67333922014-11-10 20:35:59 -08001306 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001307 ALOGD("nAllocationIoReceive, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
Andreas Gampe67333922014-11-10 20:35:59 -08001308 }
Miao Wang8c150922015-10-26 17:44:10 -07001309 return (jlong) rsAllocationIoReceive((RsContext)con, (RsAllocation)alloc);
Jason Sams163766c2012-02-15 12:04:24 -08001310}
1311
Jason Sams163766c2012-02-15 12:04:24 -08001312static void
Tim Murray460a0492013-11-19 12:45:54 -08001313nAllocationGenerateMipmaps(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Samsf7086092011-01-12 13:28:37 -08001314{
Andreas Gampe67333922014-11-10 20:35:59 -08001315 if (kLogApi) {
1316 ALOGD("nAllocationGenerateMipmaps, con(%p), a(%p)", (RsContext)con, (RsAllocation)alloc);
1317 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001318 rsAllocationGenerateMipmaps((RsContext)con, (RsAllocation)alloc);
Jason Samsf7086092011-01-12 13:28:37 -08001319}
1320
Tim Murray460a0492013-11-19 12:45:54 -08001321static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001322nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1323 jobject jbitmap, jint usage)
Jason Samsffe9f482009-06-01 17:45:53 -07001324{
John Recked207b92015-04-10 13:52:57 -07001325 SkBitmap bitmap;
1326 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Samsffe9f482009-06-01 17:45:53 -07001327
Jason Sams5476b452010-12-08 16:14:36 -08001328 bitmap.lockPixels();
1329 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,
Yang Ni8c8daea2016-03-08 21:01:54 +00001332 ptr, bitmap.getSize(), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001333 bitmap.unlockPixels();
1334 return id;
Jason Samsffe9f482009-06-01 17:45:53 -07001335}
Jason Samsfe08d992009-05-27 14:45:32 -07001336
Tim Murray460a0492013-11-19 12:45:54 -08001337static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001338nAllocationCreateBitmapBackedAllocation(JNIEnv *_env, jobject _this, jlong con, jlong type,
1339 jint mip, jobject jbitmap, jint usage)
Tim Murraya3145512012-12-04 17:59:29 -08001340{
John Recked207b92015-04-10 13:52:57 -07001341 SkBitmap bitmap;
1342 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Tim Murraya3145512012-12-04 17:59:29 -08001343
1344 bitmap.lockPixels();
1345 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001346 jlong id = (jlong)(uintptr_t)rsAllocationCreateTyped((RsContext)con,
Tim Murraya3145512012-12-04 17:59:29 -08001347 (RsType)type, (RsAllocationMipmapControl)mip,
Yang Ni8c8daea2016-03-08 21:01:54 +00001348 (uint32_t)usage, (uintptr_t)ptr);
Tim Murraya3145512012-12-04 17:59:29 -08001349 bitmap.unlockPixels();
1350 return id;
1351}
1352
Tim Murray460a0492013-11-19 12:45:54 -08001353static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001354nAllocationCubeCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1355 jobject jbitmap, jint usage)
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001356{
John Recked207b92015-04-10 13:52:57 -07001357 SkBitmap bitmap;
1358 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001359
Jason Sams5476b452010-12-08 16:14:36 -08001360 bitmap.lockPixels();
1361 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001362 jlong id = (jlong)(uintptr_t)rsAllocationCubeCreateFromBitmap((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001363 (RsType)type, (RsAllocationMipmapControl)mip,
Yang Ni8c8daea2016-03-08 21:01:54 +00001364 ptr, bitmap.getSize(), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001365 bitmap.unlockPixels();
1366 return id;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001367}
1368
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001369static void
Tim Murray460a0492013-11-19 12:45:54 -08001370nAllocationCopyFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001371{
John Recked207b92015-04-10 13:52:57 -07001372 SkBitmap bitmap;
1373 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Samsf7086092011-01-12 13:28:37 -08001374 int w = bitmap.width();
1375 int h = bitmap.height();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001376
Jason Sams4ef66502010-12-10 16:03:15 -08001377 bitmap.lockPixels();
1378 const void* ptr = bitmap.getPixels();
Tim Murrayeff663f2013-11-15 13:08:30 -08001379 rsAllocation2DData((RsContext)con, (RsAllocation)alloc, 0, 0,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001380 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Tim Murray38faea302012-11-27 14:55:08 -08001381 w, h, ptr, bitmap.getSize(), 0);
Jason Sams4ef66502010-12-10 16:03:15 -08001382 bitmap.unlockPixels();
1383}
1384
1385static void
Tim Murray460a0492013-11-19 12:45:54 -08001386nAllocationCopyToBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Jason Sams4ef66502010-12-10 16:03:15 -08001387{
John Recked207b92015-04-10 13:52:57 -07001388 SkBitmap bitmap;
1389 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Sams4ef66502010-12-10 16:03:15 -08001390
1391 bitmap.lockPixels();
1392 void* ptr = bitmap.getPixels();
Tim Murrayeff663f2013-11-15 13:08:30 -08001393 rsAllocationCopyToBitmap((RsContext)con, (RsAllocation)alloc, ptr, bitmap.getSize());
Jason Sams4ef66502010-12-10 16:03:15 -08001394 bitmap.unlockPixels();
Alex Sakhartchouk835b8542011-07-20 14:33:10 -07001395 bitmap.notifyPixelsChanged();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001396}
1397
Stephen Hines414fa2c2014-04-17 01:02:42 -07001398// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Samsd19f10d2009-05-22 14:03:28 -07001399static void
Tim Murray460a0492013-11-19 12:45:54 -08001400nAllocationData1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001401 jint count, jobject data, jint sizeBytes, jint dataType, jint mSize,
1402 jboolean usePadding)
Jason Samsd19f10d2009-05-22 14:03:28 -07001403{
Jason Samse729a942013-11-06 11:22:02 -08001404 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001405 if (kLogApi) {
1406 ALOGD("nAllocation1DData, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1407 "dataType(%i)", (RsContext)con, (RsAllocation)alloc, offset, count, sizeBytes,
1408 dataType);
1409 }
Miao Wang87e908d2015-03-02 15:15:15 -08001410 PER_ARRAY_TYPE(nullptr, rsAllocation1DData, true,
1411 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -07001412}
1413
1414static void
Miao Wangc8e237e2015-02-20 18:36:32 -08001415nAllocationElementData(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1416 jint xoff, jint yoff, jint zoff,
1417 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Jason Sams49bdaf02010-08-31 13:50:42 -07001418{
Andreas Gampe67333922014-11-10 20:35:59 -08001419 if (kLogApi) {
Yang Ni86c5c2d2016-03-25 15:49:07 -07001420 jint len = _env->GetArrayLength(data);
Miao Wangc8e237e2015-02-20 18:36:32 -08001421 ALOGD("nAllocationElementData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1422 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
Andreas Gampe67333922014-11-10 20:35:59 -08001423 sizeBytes);
1424 }
Chris Wailes488230c32014-08-14 11:22:40 -07001425 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001426 if (ptr == nullptr) {
1427 ALOGE("Failed to get Java array elements");
1428 return;
1429 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001430 rsAllocationElementData((RsContext)con, (RsAllocation)alloc,
1431 xoff, yoff, zoff,
1432 lod, ptr, sizeBytes, compIdx);
Jason Sams49bdaf02010-08-31 13:50:42 -07001433 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1434}
1435
Miao Wangc8e237e2015-02-20 18:36:32 -08001436
Stephen Hines414fa2c2014-04-17 01:02:42 -07001437// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Sams49bdaf02010-08-31 13:50:42 -07001438static void
Tim Murray460a0492013-11-19 12:45:54 -08001439nAllocationData2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001440 jint w, jint h, jobject data, jint sizeBytes, jint dataType, jint mSize,
1441 jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001442{
Jason Samse729a942013-11-06 11:22:02 -08001443 RsAllocation *alloc = (RsAllocation *)_alloc;
1444 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001445 if (kLogApi) {
1446 ALOGD("nAllocation2DData, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1447 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1448 }
Miao Wang87e908d2015-03-02 15:15:15 -08001449 int count = w * h;
1450 PER_ARRAY_TYPE(nullptr, rsAllocation2DData, true,
1451 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Samsd19f10d2009-05-22 14:03:28 -07001452}
1453
Stephen Hines414fa2c2014-04-17 01:02:42 -07001454// Copies from the Allocation pointed to by srcAlloc into the Allocation
1455// pointed to by dstAlloc.
Jason Sams40a29e82009-08-10 14:55:26 -07001456static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001457nAllocationData2D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001458 jlong dstAlloc, jint dstXoff, jint dstYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001459 jint dstMip, jint dstFace,
1460 jint width, jint height,
Tim Murray460a0492013-11-19 12:45:54 -08001461 jlong srcAlloc, jint srcXoff, jint srcYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001462 jint srcMip, jint srcFace)
1463{
Andreas Gampe67333922014-11-10 20:35:59 -08001464 if (kLogApi) {
1465 ALOGD("nAllocation2DData_s, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1466 " dstMip(%i), dstFace(%i), width(%i), height(%i),"
1467 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i), srcFace(%i)",
1468 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip, dstFace,
1469 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
1470 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001471
Tim Murrayeff663f2013-11-15 13:08:30 -08001472 rsAllocationCopy2DRange((RsContext)con,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001473 (RsAllocation)dstAlloc,
1474 dstXoff, dstYoff,
1475 dstMip, dstFace,
1476 width, height,
1477 (RsAllocation)srcAlloc,
1478 srcXoff, srcYoff,
1479 srcMip, srcFace);
1480}
1481
Stephen Hines414fa2c2014-04-17 01:02:42 -07001482// Copies from the Java object data into the Allocation pointed to by _alloc.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001483static void
Tim Murray460a0492013-11-19 12:45:54 -08001484nAllocationData3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001485 jint w, jint h, jint d, jobject data, jint sizeBytes, jint dataType,
1486 jint mSize, jboolean usePadding)
Jason Samsb05d6892013-04-09 15:59:24 -07001487{
Jason Samse729a942013-11-06 11:22:02 -08001488 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001489 if (kLogApi) {
1490 ALOGD("nAllocation3DData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1491 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1492 lod, w, h, d, sizeBytes);
1493 }
Miao Wang87e908d2015-03-02 15:15:15 -08001494 int count = w * h * d;
1495 PER_ARRAY_TYPE(nullptr, rsAllocation3DData, true,
1496 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Jason Samsb05d6892013-04-09 15:59:24 -07001497}
1498
Stephen Hines414fa2c2014-04-17 01:02:42 -07001499// Copies from the Allocation pointed to by srcAlloc into the Allocation
1500// pointed to by dstAlloc.
Jason Samsb05d6892013-04-09 15:59:24 -07001501static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001502nAllocationData3D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001503 jlong dstAlloc, jint dstXoff, jint dstYoff, jint dstZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001504 jint dstMip,
1505 jint width, jint height, jint depth,
Tim Murray460a0492013-11-19 12:45:54 -08001506 jlong srcAlloc, jint srcXoff, jint srcYoff, jint srcZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001507 jint srcMip)
1508{
Andreas Gampe67333922014-11-10 20:35:59 -08001509 if (kLogApi) {
1510 ALOGD("nAllocationData3D_alloc, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1511 " dstMip(%i), width(%i), height(%i),"
1512 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i)",
1513 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip,
1514 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip);
1515 }
Jason Samsb05d6892013-04-09 15:59:24 -07001516
Tim Murrayeff663f2013-11-15 13:08:30 -08001517 rsAllocationCopy3DRange((RsContext)con,
Jason Samsb05d6892013-04-09 15:59:24 -07001518 (RsAllocation)dstAlloc,
1519 dstXoff, dstYoff, dstZoff, dstMip,
1520 width, height, depth,
1521 (RsAllocation)srcAlloc,
1522 srcXoff, srcYoff, srcZoff, srcMip);
1523}
1524
Jason Sams21659ac2013-11-06 15:08:07 -08001525
Stephen Hines414fa2c2014-04-17 01:02:42 -07001526// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsb05d6892013-04-09 15:59:24 -07001527static void
Miao Wang87e908d2015-03-02 15:15:15 -08001528nAllocationRead(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jobject data, jint dataType,
1529 jint mSize, jboolean usePadding)
Jason Sams40a29e82009-08-10 14:55:26 -07001530{
Jason Sams21659ac2013-11-06 15:08:07 -08001531 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001532 if (kLogApi) {
1533 ALOGD("nAllocationRead, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
1534 }
Miao Wang87e908d2015-03-02 15:15:15 -08001535 int count = 0;
1536 PER_ARRAY_TYPE(0, rsAllocationRead, false,
1537 (RsContext)con, alloc, ptr, len * typeBytes);
Jason Sams40a29e82009-08-10 14:55:26 -07001538}
1539
Stephen Hines414fa2c2014-04-17 01:02:42 -07001540// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Sams40a29e82009-08-10 14:55:26 -07001541static void
Tim Murray460a0492013-11-19 12:45:54 -08001542nAllocationRead1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001543 jint count, jobject data, jint sizeBytes, jint dataType,
1544 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001545{
Jason Sams21659ac2013-11-06 15:08:07 -08001546 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001547 if (kLogApi) {
1548 ALOGD("nAllocation1DRead, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1549 "dataType(%i)", (RsContext)con, alloc, offset, count, sizeBytes, dataType);
1550 }
Miao Wang87e908d2015-03-02 15:15:15 -08001551 PER_ARRAY_TYPE(0, rsAllocation1DRead, false,
1552 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsfb9f82c2011-01-12 14:53:25 -08001553}
1554
Miao Wangc8e237e2015-02-20 18:36:32 -08001555// Copies from the Element in the Allocation pointed to by _alloc into the Java array data.
1556static void
Miao Wang45cec0a2015-03-04 16:40:21 -08001557nAllocationElementRead(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
Miao Wangc8e237e2015-02-20 18:36:32 -08001558 jint xoff, jint yoff, jint zoff,
Miao Wang45cec0a2015-03-04 16:40:21 -08001559 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Miao Wangc8e237e2015-02-20 18:36:32 -08001560{
Miao Wangc8e237e2015-02-20 18:36:32 -08001561 if (kLogApi) {
Yang Ni86c5c2d2016-03-25 15:49:07 -07001562 jint len = _env->GetArrayLength(data);
Miao Wang45cec0a2015-03-04 16:40:21 -08001563 ALOGD("nAllocationElementRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1564 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
1565 sizeBytes);
Miao Wangc8e237e2015-02-20 18:36:32 -08001566 }
Miao Wang45cec0a2015-03-04 16:40:21 -08001567 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001568 if (ptr == nullptr) {
1569 ALOGE("Failed to get Java array elements");
1570 return;
1571 }
Miao Wang45cec0a2015-03-04 16:40:21 -08001572 rsAllocationElementRead((RsContext)con, (RsAllocation)alloc,
1573 xoff, yoff, zoff,
Jason Samsa7e25092015-03-11 11:00:00 -07001574 lod, ptr, sizeBytes, compIdx);
Miao Wangbfa5e652015-05-04 15:29:25 -07001575 _env->ReleaseByteArrayElements(data, ptr, 0);
Miao Wangc8e237e2015-02-20 18:36:32 -08001576}
1577
Stephen Hines414fa2c2014-04-17 01:02:42 -07001578// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsfb9f82c2011-01-12 14:53:25 -08001579static void
Tim Murray460a0492013-11-19 12:45:54 -08001580nAllocationRead2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001581 jint w, jint h, jobject data, jint sizeBytes, jint dataType,
1582 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001583{
Jason Sams21659ac2013-11-06 15:08:07 -08001584 RsAllocation *alloc = (RsAllocation *)_alloc;
1585 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001586 if (kLogApi) {
1587 ALOGD("nAllocation2DRead, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1588 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1589 }
Miao Wang87e908d2015-03-02 15:15:15 -08001590 int count = w * h;
1591 PER_ARRAY_TYPE(0, rsAllocation2DRead, false,
1592 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Sams40a29e82009-08-10 14:55:26 -07001593}
Miao Wang87e908d2015-03-02 15:15:15 -08001594
Miao Wangc8e237e2015-02-20 18:36:32 -08001595// Copies from the Allocation pointed to by _alloc into the Java object data.
1596static void
1597nAllocationRead3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001598 jint w, jint h, jint d, jobject data, int sizeBytes, int dataType,
1599 jint mSize, jboolean usePadding)
Miao Wangc8e237e2015-02-20 18:36:32 -08001600{
1601 RsAllocation *alloc = (RsAllocation *)_alloc;
1602 if (kLogApi) {
1603 ALOGD("nAllocation3DRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1604 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1605 lod, w, h, d, sizeBytes);
1606 }
Miao Wang87e908d2015-03-02 15:15:15 -08001607 int count = w * h * d;
1608 PER_ARRAY_TYPE(nullptr, rsAllocation3DRead, false,
1609 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Miao Wangc8e237e2015-02-20 18:36:32 -08001610}
Jason Samsd19f10d2009-05-22 14:03:28 -07001611
Tim Murray460a0492013-11-19 12:45:54 -08001612static jlong
1613nAllocationGetType(JNIEnv *_env, jobject _this, jlong con, jlong a)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001614{
Andreas Gampe67333922014-11-10 20:35:59 -08001615 if (kLogApi) {
1616 ALOGD("nAllocationGetType, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1617 }
Tim Murray3aa89c12014-08-18 17:51:22 -07001618 return (jlong)(uintptr_t) rsaAllocationGetType((RsContext)con, (RsAllocation)a);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001619}
1620
Jason Sams5edc6082010-10-05 13:32:49 -07001621static void
Tim Murray460a0492013-11-19 12:45:54 -08001622nAllocationResize1D(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint dimX)
Jason Sams5edc6082010-10-05 13:32:49 -07001623{
Andreas Gampe67333922014-11-10 20:35:59 -08001624 if (kLogApi) {
1625 ALOGD("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", (RsContext)con,
1626 (RsAllocation)alloc, dimX);
1627 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001628 rsAllocationResize1D((RsContext)con, (RsAllocation)alloc, dimX);
Jason Sams5edc6082010-10-05 13:32:49 -07001629}
1630
Jason Sams46ba27e32015-02-06 17:45:15 -08001631
1632static jlong
1633nAllocationAdapterCreate(JNIEnv *_env, jobject _this, jlong con, jlong basealloc, jlong type)
1634{
1635 if (kLogApi) {
1636 ALOGD("nAllocationAdapterCreate, con(%p), base(%p), type(%p)",
1637 (RsContext)con, (RsAllocation)basealloc, (RsElement)type);
1638 }
1639 return (jlong)(uintptr_t) rsAllocationAdapterCreate((RsContext)con, (RsType)type,
1640 (RsAllocation)basealloc);
1641
1642}
1643
1644static void
1645nAllocationAdapterOffset(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1646 jint x, jint y, jint z, jint face, jint lod,
1647 jint a1, jint a2, jint a3, jint a4)
1648{
1649 uint32_t params[] = {
1650 (uint32_t)x, (uint32_t)y, (uint32_t)z, (uint32_t)face,
1651 (uint32_t)lod, (uint32_t)a1, (uint32_t)a2, (uint32_t)a3, (uint32_t)a4
1652 };
1653 if (kLogApi) {
1654 ALOGD("nAllocationAdapterOffset, con(%p), alloc(%p), x(%i), y(%i), z(%i), face(%i), lod(%i), arrays(%i %i %i %i)",
1655 (RsContext)con, (RsAllocation)alloc, x, y, z, face, lod, a1, a2, a3, a4);
1656 }
1657 rsAllocationAdapterOffset((RsContext)con, (RsAllocation)alloc,
1658 params, sizeof(params));
1659}
1660
1661
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001662// -----------------------------------
1663
Tim Murray460a0492013-11-19 12:45:54 -08001664static jlong
1665nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con, jlong native_asset)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001666{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001667 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001668 ALOGV("______nFileA3D %p", asset);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001669
Tim Murray3aa89c12014-08-18 17:51:22 -07001670 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromMemory((RsContext)con, asset->getBuffer(false), asset->getLength());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001671 return id;
1672}
1673
Tim Murray460a0492013-11-19 12:45:54 -08001674static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001675nFileA3DCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001676{
1677 AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001678 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001679 return 0;
1680 }
1681
1682 AutoJavaStringToUTF8 str(_env, _path);
1683 Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
Chris Wailes488230c32014-08-14 11:22:40 -07001684 if (asset == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001685 return 0;
1686 }
1687
Tim Murray3aa89c12014-08-18 17:51:22 -07001688 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromAsset((RsContext)con, asset);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001689 return id;
1690}
1691
Tim Murray460a0492013-11-19 12:45:54 -08001692static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001693nFileA3DCreateFromFile(JNIEnv *_env, jobject _this, jlong con, jstring fileName)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001694{
1695 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001696 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromFile((RsContext)con, fileNameUTF.c_str());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001697
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001698 return id;
1699}
1700
Tim Murray460a0492013-11-19 12:45:54 -08001701static jint
1702nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001703{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001704 int32_t numEntries = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001705 rsaFileA3DGetNumIndexEntries((RsContext)con, &numEntries, (RsFile)fileA3D);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001706 return (jint)numEntries;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001707}
1708
1709static void
Tim Murray460a0492013-11-19 12:45:54 -08001710nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001711{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001712 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001713 RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
1714
Tim Murrayeff663f2013-11-15 13:08:30 -08001715 rsaFileA3DGetIndexEntries((RsContext)con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001716
1717 for(jint i = 0; i < numEntries; i ++) {
1718 _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
1719 _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
1720 }
1721
1722 free(fileEntries);
1723}
1724
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001725static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001726nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint index)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001727{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001728 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Tim Murray3aa89c12014-08-18 17:51:22 -07001729 jlong id = (jlong)(uintptr_t)rsaFileA3DGetEntryByIndex((RsContext)con, (uint32_t)index, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001730 return id;
1731}
Jason Samsd19f10d2009-05-22 14:03:28 -07001732
1733// -----------------------------------
1734
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001735static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001736nFontCreateFromFile(JNIEnv *_env, jobject _this, jlong con,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001737 jstring fileName, jfloat fontSize, jint dpi)
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001738{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001739 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001740 jlong id = (jlong)(uintptr_t)rsFontCreateFromFile((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001741 fileNameUTF.c_str(), fileNameUTF.length(),
1742 fontSize, dpi);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001743
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001744 return id;
1745}
1746
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001747static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001748nFontCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con,
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001749 jstring name, jfloat fontSize, jint dpi, jlong native_asset)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001750{
1751 Asset* asset = reinterpret_cast<Asset*>(native_asset);
1752 AutoJavaStringToUTF8 nameUTF(_env, name);
1753
Tim Murray3aa89c12014-08-18 17:51:22 -07001754 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001755 nameUTF.c_str(), nameUTF.length(),
1756 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001757 asset->getBuffer(false), asset->getLength());
1758 return id;
1759}
1760
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001761static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001762nFontCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001763 jfloat fontSize, jint dpi)
1764{
1765 AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001766 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001767 return 0;
1768 }
1769
1770 AutoJavaStringToUTF8 str(_env, _path);
1771 Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
Chris Wailes488230c32014-08-14 11:22:40 -07001772 if (asset == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001773 return 0;
1774 }
1775
Tim Murray3aa89c12014-08-18 17:51:22 -07001776 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001777 str.c_str(), str.length(),
1778 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001779 asset->getBuffer(false), asset->getLength());
1780 delete asset;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001781 return id;
1782}
1783
Jason Samsbd1c3ad2009-08-03 16:03:08 -07001784// -----------------------------------
1785
1786static void
Tim Murray460a0492013-11-19 12:45:54 -08001787nScriptBindAllocation(JNIEnv *_env, jobject _this, jlong con, jlong script, jlong alloc, jint slot)
Jason Samsd19f10d2009-05-22 14:03:28 -07001788{
Andreas Gampe67333922014-11-10 20:35:59 -08001789 if (kLogApi) {
1790 ALOGD("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", (RsContext)con,
1791 (RsScript)script, (RsAllocation)alloc, slot);
1792 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001793 rsScriptBindAllocation((RsContext)con, (RsScript)script, (RsAllocation)alloc, slot);
Jason Samsd19f10d2009-05-22 14:03:28 -07001794}
1795
1796static void
Tim Murray460a0492013-11-19 12:45:54 -08001797nScriptSetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jint val)
Jason Sams4d339932010-05-11 14:03:58 -07001798{
Andreas Gampe67333922014-11-10 20:35:59 -08001799 if (kLogApi) {
1800 ALOGD("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", (RsContext)con, (void *)script,
1801 slot, val);
1802 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001803 rsScriptSetVarI((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001804}
1805
Tim Murray7c4caad2013-04-10 16:21:40 -07001806static jint
Tim Murray460a0492013-11-19 12:45:54 -08001807nScriptGetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001808{
Andreas Gampe67333922014-11-10 20:35:59 -08001809 if (kLogApi) {
1810 ALOGD("nScriptGetVarI, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1811 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001812 int value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001813 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001814 return value;
1815}
1816
Jason Sams4d339932010-05-11 14:03:58 -07001817static void
Tim Murray460a0492013-11-19 12:45:54 -08001818nScriptSetVarObj(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001819{
Andreas Gampe67333922014-11-10 20:35:59 -08001820 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001821 ALOGD("nScriptSetVarObj, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001822 slot, val);
1823 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001824 rsScriptSetVarObj((RsContext)con, (RsScript)script, slot, (RsObjectBase)val);
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001825}
1826
1827static void
Tim Murray460a0492013-11-19 12:45:54 -08001828nScriptSetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Stephen Hines031ec58c2010-10-11 10:54:21 -07001829{
Andreas Gampe67333922014-11-10 20:35:59 -08001830 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001831 ALOGD("nScriptSetVarJ, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001832 slot, val);
1833 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001834 rsScriptSetVarJ((RsContext)con, (RsScript)script, slot, val);
Stephen Hines031ec58c2010-10-11 10:54:21 -07001835}
1836
Tim Murray7c4caad2013-04-10 16:21:40 -07001837static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001838nScriptGetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001839{
Andreas Gampe67333922014-11-10 20:35:59 -08001840 if (kLogApi) {
1841 ALOGD("nScriptGetVarJ, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1842 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001843 jlong value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001844 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001845 return value;
1846}
1847
Stephen Hines031ec58c2010-10-11 10:54:21 -07001848static void
Tim Murray460a0492013-11-19 12:45:54 -08001849nScriptSetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, float val)
Jason Sams4d339932010-05-11 14:03:58 -07001850{
Andreas Gampe67333922014-11-10 20:35:59 -08001851 if (kLogApi) {
1852 ALOGD("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", (RsContext)con, (void *)script,
1853 slot, val);
1854 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001855 rsScriptSetVarF((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001856}
1857
Tim Murray7c4caad2013-04-10 16:21:40 -07001858static jfloat
Tim Murray460a0492013-11-19 12:45:54 -08001859nScriptGetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001860{
Andreas Gampe67333922014-11-10 20:35:59 -08001861 if (kLogApi) {
1862 ALOGD("nScriptGetVarF, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1863 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001864 jfloat value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001865 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001866 return value;
1867}
1868
Jason Sams4d339932010-05-11 14:03:58 -07001869static void
Tim Murray460a0492013-11-19 12:45:54 -08001870nScriptSetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, double val)
Stephen Hinesca54ec32010-09-20 17:20:30 -07001871{
Andreas Gampe67333922014-11-10 20:35:59 -08001872 if (kLogApi) {
1873 ALOGD("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", (RsContext)con, (void *)script,
1874 slot, val);
1875 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001876 rsScriptSetVarD((RsContext)con, (RsScript)script, slot, val);
Stephen Hinesca54ec32010-09-20 17:20:30 -07001877}
1878
Tim Murray7c4caad2013-04-10 16:21:40 -07001879static jdouble
Tim Murray460a0492013-11-19 12:45:54 -08001880nScriptGetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001881{
Andreas Gampe67333922014-11-10 20:35:59 -08001882 if (kLogApi) {
1883 ALOGD("nScriptGetVarD, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1884 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001885 jdouble value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001886 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001887 return value;
1888}
1889
Stephen Hinesca54ec32010-09-20 17:20:30 -07001890static void
Tim Murray460a0492013-11-19 12:45:54 -08001891nScriptSetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001892{
Andreas Gampe67333922014-11-10 20:35:59 -08001893 if (kLogApi) {
1894 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1895 }
Jason Sams4d339932010-05-11 14:03:58 -07001896 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001897 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001898 if (ptr == nullptr) {
1899 ALOGE("Failed to get Java array elements");
1900 return;
1901 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001902 rsScriptSetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001903 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1904}
1905
Stephen Hinesadeb8092012-04-20 14:26:06 -07001906static void
Tim Murray460a0492013-11-19 12:45:54 -08001907nScriptGetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Tim Murray7c4caad2013-04-10 16:21:40 -07001908{
Andreas Gampe67333922014-11-10 20:35:59 -08001909 if (kLogApi) {
1910 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1911 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001912 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001913 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001914 if (ptr == nullptr) {
1915 ALOGE("Failed to get Java array elements");
1916 return;
1917 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001918 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Stephen Hines414fa2c2014-04-17 01:02:42 -07001919 _env->ReleaseByteArrayElements(data, ptr, 0);
Tim Murray7c4caad2013-04-10 16:21:40 -07001920}
1921
1922static void
Andreas Gampe67333922014-11-10 20:35:59 -08001923nScriptSetVarVE(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data,
1924 jlong elem, jintArray dims)
Stephen Hinesadeb8092012-04-20 14:26:06 -07001925{
Andreas Gampe67333922014-11-10 20:35:59 -08001926 if (kLogApi) {
1927 ALOGD("nScriptSetVarVE, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1928 }
Stephen Hinesadeb8092012-04-20 14:26:06 -07001929 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001930 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001931 if (ptr == nullptr) {
1932 ALOGE("Failed to get Java array elements");
1933 return;
1934 }
Stephen Hinesadeb8092012-04-20 14:26:06 -07001935 jint dimsLen = _env->GetArrayLength(dims) * sizeof(int);
Chris Wailes488230c32014-08-14 11:22:40 -07001936 jint *dimsPtr = _env->GetIntArrayElements(dims, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001937 if (dimsPtr == nullptr) {
1938 ALOGE("Failed to get Java array elements");
1939 return;
1940 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001941 rsScriptSetVarVE((RsContext)con, (RsScript)script, slot, ptr, len, (RsElement)elem,
Stephen Hinesbc5d3ee2014-06-25 00:03:39 -07001942 (const uint32_t*) dimsPtr, dimsLen);
Stephen Hinesadeb8092012-04-20 14:26:06 -07001943 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1944 _env->ReleaseIntArrayElements(dims, dimsPtr, JNI_ABORT);
1945}
1946
Jason Samsd19f10d2009-05-22 14:03:28 -07001947
1948static void
Tim Murray460a0492013-11-19 12:45:54 -08001949nScriptSetTimeZone(JNIEnv *_env, jobject _this, jlong con, jlong script, jbyteArray timeZone)
Jason Samsd19f10d2009-05-22 14:03:28 -07001950{
Andreas Gampe67333922014-11-10 20:35:59 -08001951 if (kLogApi) {
1952 ALOGD("nScriptCSetTimeZone, con(%p), s(%p)", (RsContext)con, (void *)script);
1953 }
Romain Guy584a3752009-07-30 18:45:01 -07001954
1955 jint length = _env->GetArrayLength(timeZone);
1956 jbyte* timeZone_ptr;
1957 timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
Miao Wangba8766c2015-10-12 17:24:13 -07001958 if (timeZone_ptr == nullptr) {
1959 ALOGE("Failed to get Java array elements");
1960 return;
1961 }
Romain Guy584a3752009-07-30 18:45:01 -07001962
Tim Murrayeff663f2013-11-15 13:08:30 -08001963 rsScriptSetTimeZone((RsContext)con, (RsScript)script, (const char *)timeZone_ptr, length);
Romain Guy584a3752009-07-30 18:45:01 -07001964
1965 if (timeZone_ptr) {
1966 _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
1967 }
1968}
1969
Jason Samsfbf0b9e2009-08-13 12:59:04 -07001970static void
Tim Murray460a0492013-11-19 12:45:54 -08001971nScriptInvoke(JNIEnv *_env, jobject _this, jlong con, jlong obj, jint slot)
Jason Samsbe2e8412009-09-16 15:04:38 -07001972{
Andreas Gampe67333922014-11-10 20:35:59 -08001973 if (kLogApi) {
1974 ALOGD("nScriptInvoke, con(%p), script(%p)", (RsContext)con, (void *)obj);
1975 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001976 rsScriptInvoke((RsContext)con, (RsScript)obj, slot);
Jason Samsbe2e8412009-09-16 15:04:38 -07001977}
1978
1979static void
Tim Murray460a0492013-11-19 12:45:54 -08001980nScriptInvokeV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001981{
Andreas Gampe67333922014-11-10 20:35:59 -08001982 if (kLogApi) {
1983 ALOGD("nScriptInvokeV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1984 }
Jason Sams4d339932010-05-11 14:03:58 -07001985 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001986 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001987 if (ptr == nullptr) {
1988 ALOGE("Failed to get Java array elements");
1989 return;
1990 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001991 rsScriptInvokeV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001992 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1993}
1994
Jason Sams6e494d32011-04-27 16:33:11 -07001995static void
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001996nScriptForEach(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
1997 jlongArray ains, jlong aout, jbyteArray params,
1998 jintArray limits)
Jason Sams6e494d32011-04-27 16:33:11 -07001999{
Andreas Gampe67333922014-11-10 20:35:59 -08002000 if (kLogApi) {
Chih-Hung Hsieh9eb9dd32015-05-06 14:42:04 -07002001 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 -08002002 }
Jason Sams6e494d32011-04-27 16:33:11 -07002003
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002004 jint in_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002005 jlong *in_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002006
Chris Wailes488230c32014-08-14 11:22:40 -07002007 RsAllocation *in_allocs = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002008
Chris Wailes488230c32014-08-14 11:22:40 -07002009 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002010 in_len = _env->GetArrayLength(ains);
Yang Ni7b2a46f2015-05-05 12:41:19 -07002011 if (in_len > (jint)RS_KERNEL_MAX_ARGUMENTS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -07002012 ALOGE("Too many arguments in kernel launch.");
2013 // TODO (b/20758983): Report back to Java and throw an exception
2014 return;
2015 }
Chris Wailes94961062014-06-11 12:01:28 -07002016
Yang Ni17c2d7a2015-04-30 16:13:54 -07002017 in_ptr = _env->GetLongArrayElements(ains, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002018 if (in_ptr == nullptr) {
2019 ALOGE("Failed to get Java array elements");
2020 return;
2021 }
2022
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002023 if (sizeof(RsAllocation) == sizeof(jlong)) {
2024 in_allocs = (RsAllocation*)in_ptr;
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002025 } else {
2026 // Convert from 64-bit jlong types to the native pointer type.
Chris Wailes94961062014-06-11 12:01:28 -07002027
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002028 in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
Yang Ni17c2d7a2015-04-30 16:13:54 -07002029 if (in_allocs == nullptr) {
2030 ALOGE("Failed launching kernel for lack of memory.");
2031 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2032 return;
2033 }
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002034
2035 for (int index = in_len; --index >= 0;) {
2036 in_allocs[index] = (RsAllocation)in_ptr[index];
2037 }
2038 }
Chris Wailes94961062014-06-11 12:01:28 -07002039 }
2040
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002041 jint param_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002042 jbyte *param_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002043
Chris Wailes488230c32014-08-14 11:22:40 -07002044 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002045 param_len = _env->GetArrayLength(params);
Chris Wailes488230c32014-08-14 11:22:40 -07002046 param_ptr = _env->GetByteArrayElements(params, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002047 if (param_ptr == nullptr) {
2048 ALOGE("Failed to get Java array elements");
2049 return;
2050 }
Chris Wailes94961062014-06-11 12:01:28 -07002051 }
2052
Chris Wailes488230c32014-08-14 11:22:40 -07002053 RsScriptCall sc, *sca = nullptr;
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002054 uint32_t sc_size = 0;
Chris Wailes94961062014-06-11 12:01:28 -07002055
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002056 jint limit_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002057 jint *limit_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002058
Chris Wailes488230c32014-08-14 11:22:40 -07002059 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002060 limit_len = _env->GetArrayLength(limits);
Chris Wailes488230c32014-08-14 11:22:40 -07002061 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002062 if (limit_ptr == nullptr) {
2063 ALOGE("Failed to get Java array elements");
2064 return;
2065 }
Chris Wailes94961062014-06-11 12:01:28 -07002066
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002067 assert(limit_len == 6);
Andreas Gampe67333922014-11-10 20:35:59 -08002068 UNUSED(limit_len); // As the assert might not be compiled.
Chris Wailes94961062014-06-11 12:01:28 -07002069
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002070 sc.xStart = limit_ptr[0];
2071 sc.xEnd = limit_ptr[1];
2072 sc.yStart = limit_ptr[2];
2073 sc.yEnd = limit_ptr[3];
2074 sc.zStart = limit_ptr[4];
2075 sc.zEnd = limit_ptr[5];
2076 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
Jason Sams14331ab2015-01-26 18:14:36 -08002077 sc.arrayStart = 0;
2078 sc.arrayEnd = 0;
2079 sc.array2Start = 0;
2080 sc.array2End = 0;
2081 sc.array3Start = 0;
2082 sc.array3End = 0;
2083 sc.array4Start = 0;
2084 sc.array4End = 0;
Chris Wailes94961062014-06-11 12:01:28 -07002085
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002086 sca = &sc;
Yang Nie8f2e442016-03-15 16:00:02 -07002087 // sc_size is required, but unused, by the runtime and drivers.
2088 sc_size = sizeof(sc);
Chris Wailes94961062014-06-11 12:01:28 -07002089 }
2090
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002091 rsScriptForEachMulti((RsContext)con, (RsScript)script, slot,
2092 in_allocs, in_len, (RsAllocation)aout,
2093 param_ptr, param_len, sca, sc_size);
Chris Wailes94961062014-06-11 12:01:28 -07002094
Chris Wailes488230c32014-08-14 11:22:40 -07002095 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002096 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
Chris Wailes94961062014-06-11 12:01:28 -07002097 }
2098
Chris Wailes488230c32014-08-14 11:22:40 -07002099 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002100 _env->ReleaseByteArrayElements(params, param_ptr, JNI_ABORT);
2101 }
2102
Chris Wailes488230c32014-08-14 11:22:40 -07002103 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002104 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
2105 }
Chris Wailes94961062014-06-11 12:01:28 -07002106}
2107
Matt Wala36eb1f72015-07-20 15:35:27 -07002108static void
2109nScriptReduce(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
David Gross4a457852016-06-02 14:46:55 -07002110 jlongArray ains, jlong aout, jintArray limits)
Matt Wala36eb1f72015-07-20 15:35:27 -07002111{
2112 if (kLogApi) {
David Gross4a457852016-06-02 14:46:55 -07002113 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 -08002114 }
2115
2116 if (ains == nullptr) {
2117 ALOGE("At least one input required.");
2118 // TODO (b/20758983): Report back to Java and throw an exception
2119 return;
2120 }
2121 jint in_len = _env->GetArrayLength(ains);
2122 if (in_len > (jint)RS_KERNEL_MAX_ARGUMENTS) {
2123 ALOGE("Too many arguments in kernel launch.");
2124 // TODO (b/20758983): Report back to Java and throw an exception
2125 return;
2126 }
2127
2128 jlong *in_ptr = _env->GetLongArrayElements(ains, nullptr);
2129 if (in_ptr == nullptr) {
2130 ALOGE("Failed to get Java array elements");
2131 // TODO (b/20758983): Report back to Java and throw an exception
2132 return;
2133 }
2134
2135 RsAllocation *in_allocs = nullptr;
2136 if (sizeof(RsAllocation) == sizeof(jlong)) {
2137 in_allocs = (RsAllocation*)in_ptr;
2138 } else {
2139 // Convert from 64-bit jlong types to the native pointer type.
2140
2141 in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
2142 if (in_allocs == nullptr) {
2143 ALOGE("Failed launching kernel for lack of memory.");
2144 // TODO (b/20758983): Report back to Java and throw an exception
2145 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2146 return;
2147 }
2148
2149 for (int index = in_len; --index >= 0;) {
2150 in_allocs[index] = (RsAllocation)in_ptr[index];
2151 }
2152 }
2153
2154 RsScriptCall sc, *sca = nullptr;
2155 uint32_t sc_size = 0;
2156
2157 jint limit_len = 0;
2158 jint *limit_ptr = nullptr;
2159
2160 if (limits != nullptr) {
2161 limit_len = _env->GetArrayLength(limits);
2162 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
2163 if (limit_ptr == nullptr) {
2164 ALOGE("Failed to get Java array elements");
2165 // TODO (b/20758983): Report back to Java and throw an exception
2166 return;
2167 }
2168
2169 assert(limit_len == 6);
2170 UNUSED(limit_len); // As the assert might not be compiled.
2171
2172 sc.xStart = limit_ptr[0];
2173 sc.xEnd = limit_ptr[1];
2174 sc.yStart = limit_ptr[2];
2175 sc.yEnd = limit_ptr[3];
2176 sc.zStart = limit_ptr[4];
2177 sc.zEnd = limit_ptr[5];
2178 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
2179 sc.arrayStart = 0;
2180 sc.arrayEnd = 0;
2181 sc.array2Start = 0;
2182 sc.array2End = 0;
2183 sc.array3Start = 0;
2184 sc.array3End = 0;
2185 sc.array4Start = 0;
2186 sc.array4End = 0;
2187
2188 sca = &sc;
2189 sc_size = sizeof(sc);
2190 }
2191
David Gross4a457852016-06-02 14:46:55 -07002192 rsScriptReduce((RsContext)con, (RsScript)script, slot,
2193 in_allocs, in_len, (RsAllocation)aout,
2194 sca, sc_size);
David Gross26ef7a732016-01-12 12:19:15 -08002195
2196 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2197
2198 if (limits != nullptr) {
2199 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
2200 }
2201}
2202
Jason Sams22534172009-08-04 16:58:20 -07002203// -----------------------------------
2204
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002205static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002206nScriptCCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Samse4a06c52011-03-16 16:29:28 -07002207 jstring resName, jstring cacheDir,
2208 jbyteArray scriptRef, jint length)
Jason Sams22534172009-08-04 16:58:20 -07002209{
Andreas Gampe67333922014-11-10 20:35:59 -08002210 if (kLogApi) {
2211 ALOGD("nScriptCCreate, con(%p)", (RsContext)con);
2212 }
Jason Sams22534172009-08-04 16:58:20 -07002213
Jason Samse4a06c52011-03-16 16:29:28 -07002214 AutoJavaStringToUTF8 resNameUTF(_env, resName);
2215 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002216 jlong ret = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002217 jbyte* script_ptr = nullptr;
Jack Palevich43702d82009-05-28 13:38:16 -07002218 jint _exception = 0;
2219 jint remaining;
Jack Palevich43702d82009-05-28 13:38:16 -07002220 if (!scriptRef) {
2221 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002222 //jniThrowException(_env, "java/lang/IllegalArgumentException", "script == null");
Jack Palevich43702d82009-05-28 13:38:16 -07002223 goto exit;
2224 }
Jack Palevich43702d82009-05-28 13:38:16 -07002225 if (length < 0) {
2226 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002227 //jniThrowException(_env, "java/lang/IllegalArgumentException", "length < 0");
Jack Palevich43702d82009-05-28 13:38:16 -07002228 goto exit;
2229 }
Jason Samse4a06c52011-03-16 16:29:28 -07002230 remaining = _env->GetArrayLength(scriptRef);
Jack Palevich43702d82009-05-28 13:38:16 -07002231 if (remaining < length) {
2232 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002233 //jniThrowException(_env, "java/lang/IllegalArgumentException",
2234 // "length > script.length - offset");
Jack Palevich43702d82009-05-28 13:38:16 -07002235 goto exit;
2236 }
Jason Samse4a06c52011-03-16 16:29:28 -07002237 script_ptr = (jbyte *)
Jack Palevich43702d82009-05-28 13:38:16 -07002238 _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
Miao Wangba8766c2015-10-12 17:24:13 -07002239 if (script_ptr == nullptr) {
2240 ALOGE("Failed to get Java array elements");
2241 return ret;
2242 }
Jack Palevich43702d82009-05-28 13:38:16 -07002243
Tim Murrayeff663f2013-11-15 13:08:30 -08002244 //rsScriptCSetText((RsContext)con, (const char *)script_ptr, length);
Jason Samse4a06c52011-03-16 16:29:28 -07002245
Tim Murray3aa89c12014-08-18 17:51:22 -07002246 ret = (jlong)(uintptr_t)rsScriptCCreate((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07002247 resNameUTF.c_str(), resNameUTF.length(),
2248 cacheDirUTF.c_str(), cacheDirUTF.length(),
Jason Samse4a06c52011-03-16 16:29:28 -07002249 (const char *)script_ptr, length);
Jason Sams39ddc9502009-06-05 17:35:09 -07002250
Jack Palevich43702d82009-05-28 13:38:16 -07002251exit:
Jason Samse4a06c52011-03-16 16:29:28 -07002252 if (script_ptr) {
2253 _env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr,
Jack Palevich43702d82009-05-28 13:38:16 -07002254 _exception ? JNI_ABORT: 0);
2255 }
Jason Samsd19f10d2009-05-22 14:03:28 -07002256
Tim Murray3aa89c12014-08-18 17:51:22 -07002257 return (jlong)(uintptr_t)ret;
Jason Samsd19f10d2009-05-22 14:03:28 -07002258}
2259
Tim Murray460a0492013-11-19 12:45:54 -08002260static jlong
2261nScriptIntrinsicCreate(JNIEnv *_env, jobject _this, jlong con, jint id, jlong eid)
Jason Sams6ab97682012-08-10 12:09:43 -07002262{
Andreas Gampe67333922014-11-10 20:35:59 -08002263 if (kLogApi) {
2264 ALOGD("nScriptIntrinsicCreate, con(%p) id(%i) element(%p)", (RsContext)con, id,
2265 (void *)eid);
2266 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002267 return (jlong)(uintptr_t)rsScriptIntrinsicCreate((RsContext)con, id, (RsElement)eid);
Jason Sams6ab97682012-08-10 12:09:43 -07002268}
2269
Tim Murray460a0492013-11-19 12:45:54 -08002270static jlong
2271nScriptKernelIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot, jint sig)
Jason Sams08a81582012-09-18 12:32:10 -07002272{
Andreas Gampe67333922014-11-10 20:35:59 -08002273 if (kLogApi) {
2274 ALOGD("nScriptKernelIDCreate, con(%p) script(%p), slot(%i), sig(%i)", (RsContext)con,
2275 (void *)sid, slot, sig);
2276 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002277 return (jlong)(uintptr_t)rsScriptKernelIDCreate((RsContext)con, (RsScript)sid, slot, sig);
Jason Sams08a81582012-09-18 12:32:10 -07002278}
2279
Tim Murray460a0492013-11-19 12:45:54 -08002280static jlong
Yang Nibe392ad2015-01-23 17:16:02 -08002281nScriptInvokeIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
2282{
2283 if (kLogApi) {
Elliott Hughes7ff53fa2015-02-05 21:36:10 -08002284 ALOGD("nScriptInvokeIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con,
Yang Nibe392ad2015-01-23 17:16:02 -08002285 (void *)sid, slot);
2286 }
2287 return (jlong)(uintptr_t)rsScriptInvokeIDCreate((RsContext)con, (RsScript)sid, slot);
2288}
2289
2290static jlong
Tim Murray460a0492013-11-19 12:45:54 -08002291nScriptFieldIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
Jason Sams08a81582012-09-18 12:32:10 -07002292{
Andreas Gampe67333922014-11-10 20:35:59 -08002293 if (kLogApi) {
2294 ALOGD("nScriptFieldIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con, (void *)sid,
2295 slot);
2296 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002297 return (jlong)(uintptr_t)rsScriptFieldIDCreate((RsContext)con, (RsScript)sid, slot);
Jason Sams08a81582012-09-18 12:32:10 -07002298}
2299
Tim Murray460a0492013-11-19 12:45:54 -08002300static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002301nScriptGroupCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _kernels, jlongArray _src,
2302 jlongArray _dstk, jlongArray _dstf, jlongArray _types)
Jason Sams08a81582012-09-18 12:32:10 -07002303{
Andreas Gampe67333922014-11-10 20:35:59 -08002304 if (kLogApi) {
2305 ALOGD("nScriptGroupCreate, con(%p)", (RsContext)con);
2306 }
Jason Sams08a81582012-09-18 12:32:10 -07002307
Miao Wanga4ad5f82016-02-11 12:32:39 -08002308 jlong id = 0;
2309
2310 RsScriptKernelID* kernelsPtr;
Ashok Bhat98071552014-02-12 09:54:43 +00002311 jint kernelsLen = _env->GetArrayLength(_kernels);
Chris Wailes488230c32014-08-14 11:22:40 -07002312 jlong *jKernelsPtr = _env->GetLongArrayElements(_kernels, nullptr);
Miao Wanga4ad5f82016-02-11 12:32:39 -08002313
2314 RsScriptKernelID* srcPtr;
2315 jint srcLen = _env->GetArrayLength(_src);
2316 jlong *jSrcPtr = _env->GetLongArrayElements(_src, nullptr);
2317
2318 RsScriptKernelID* dstkPtr;
2319 jint dstkLen = _env->GetArrayLength(_dstk);
2320 jlong *jDstkPtr = _env->GetLongArrayElements(_dstk, nullptr);
2321
2322 RsScriptKernelID* dstfPtr;
2323 jint dstfLen = _env->GetArrayLength(_dstf);
2324 jlong *jDstfPtr = _env->GetLongArrayElements(_dstf, nullptr);
2325
2326 RsType* typesPtr;
2327 jint typesLen = _env->GetArrayLength(_types);
2328 jlong *jTypesPtr = _env->GetLongArrayElements(_types, nullptr);
2329
Miao Wangba8766c2015-10-12 17:24:13 -07002330 if (jKernelsPtr == nullptr) {
2331 ALOGE("Failed to get Java array elements: kernels");
Miao Wanga4ad5f82016-02-11 12:32:39 -08002332 goto cleanup;
Miao Wangba8766c2015-10-12 17:24:13 -07002333 }
Miao Wanga4ad5f82016-02-11 12:32:39 -08002334 if (jSrcPtr == nullptr) {
2335 ALOGE("Failed to get Java array elements: src");
2336 goto cleanup;
2337 }
2338 if (jDstkPtr == nullptr) {
2339 ALOGE("Failed to get Java array elements: dstk");
2340 goto cleanup;
2341 }
2342 if (jDstfPtr == nullptr) {
2343 ALOGE("Failed to get Java array elements: dstf");
2344 goto cleanup;
2345 }
2346 if (jTypesPtr == nullptr) {
2347 ALOGE("Failed to get Java array elements: types");
2348 goto cleanup;
2349 }
2350
2351 kernelsPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * kernelsLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002352 for(int i = 0; i < kernelsLen; ++i) {
2353 kernelsPtr[i] = (RsScriptKernelID)jKernelsPtr[i];
2354 }
Jason Sams08a81582012-09-18 12:32:10 -07002355
Miao Wanga4ad5f82016-02-11 12:32:39 -08002356 srcPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * srcLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002357 for(int i = 0; i < srcLen; ++i) {
2358 srcPtr[i] = (RsScriptKernelID)jSrcPtr[i];
2359 }
Jason Sams08a81582012-09-18 12:32:10 -07002360
Miao Wanga4ad5f82016-02-11 12:32:39 -08002361 dstkPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstkLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002362 for(int i = 0; i < dstkLen; ++i) {
2363 dstkPtr[i] = (RsScriptKernelID)jDstkPtr[i];
2364 }
2365
Miao Wanga4ad5f82016-02-11 12:32:39 -08002366 dstfPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstfLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002367 for(int i = 0; i < dstfLen; ++i) {
2368 dstfPtr[i] = (RsScriptKernelID)jDstfPtr[i];
2369 }
2370
Miao Wanga4ad5f82016-02-11 12:32:39 -08002371 typesPtr = (RsType*) malloc(sizeof(RsType) * typesLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002372 for(int i = 0; i < typesLen; ++i) {
2373 typesPtr[i] = (RsType)jTypesPtr[i];
2374 }
2375
Miao Wanga4ad5f82016-02-11 12:32:39 -08002376 id = (jlong)(uintptr_t)rsScriptGroupCreate((RsContext)con,
Ashok Bhat98071552014-02-12 09:54:43 +00002377 (RsScriptKernelID *)kernelsPtr, kernelsLen * sizeof(RsScriptKernelID),
2378 (RsScriptKernelID *)srcPtr, srcLen * sizeof(RsScriptKernelID),
2379 (RsScriptKernelID *)dstkPtr, dstkLen * sizeof(RsScriptKernelID),
2380 (RsScriptFieldID *)dstfPtr, dstfLen * sizeof(RsScriptKernelID),
2381 (RsType *)typesPtr, typesLen * sizeof(RsType));
2382
2383 free(kernelsPtr);
2384 free(srcPtr);
2385 free(dstkPtr);
2386 free(dstfPtr);
2387 free(typesPtr);
Miao Wanga4ad5f82016-02-11 12:32:39 -08002388
2389cleanup:
2390 if (jKernelsPtr != nullptr) {
2391 _env->ReleaseLongArrayElements(_kernels, jKernelsPtr, 0);
2392 }
2393 if (jSrcPtr != nullptr) {
2394 _env->ReleaseLongArrayElements(_src, jSrcPtr, 0);
2395 }
2396 if (jDstkPtr != nullptr) {
2397 _env->ReleaseLongArrayElements(_dstk, jDstkPtr, 0);
2398 }
2399 if (jDstfPtr != nullptr) {
2400 _env->ReleaseLongArrayElements(_dstf, jDstfPtr, 0);
2401 }
2402 if (jTypesPtr != nullptr) {
2403 _env->ReleaseLongArrayElements(_types, jTypesPtr, 0);
2404 }
2405
Jason Sams08a81582012-09-18 12:32:10 -07002406 return id;
2407}
2408
2409static void
Tim Murray460a0492013-11-19 12:45:54 -08002410nScriptGroupSetInput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002411{
Andreas Gampe67333922014-11-10 20:35:59 -08002412 if (kLogApi) {
2413 ALOGD("nScriptGroupSetInput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2414 (void *)gid, (void *)kid, (void *)alloc);
2415 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002416 rsScriptGroupSetInput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002417}
2418
2419static void
Tim Murray460a0492013-11-19 12:45:54 -08002420nScriptGroupSetOutput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002421{
Andreas Gampe67333922014-11-10 20:35:59 -08002422 if (kLogApi) {
2423 ALOGD("nScriptGroupSetOutput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2424 (void *)gid, (void *)kid, (void *)alloc);
2425 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002426 rsScriptGroupSetOutput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002427}
2428
2429static void
Tim Murray460a0492013-11-19 12:45:54 -08002430nScriptGroupExecute(JNIEnv *_env, jobject _this, jlong con, jlong gid)
Jason Sams08a81582012-09-18 12:32:10 -07002431{
Andreas Gampe67333922014-11-10 20:35:59 -08002432 if (kLogApi) {
2433 ALOGD("nScriptGroupSetOutput, con(%p) group(%p)", (RsContext)con, (void *)gid);
2434 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002435 rsScriptGroupExecute((RsContext)con, (RsScriptGroup)gid);
Jason Sams08a81582012-09-18 12:32:10 -07002436}
2437
Jason Samsd19f10d2009-05-22 14:03:28 -07002438// ---------------------------------------------------------------------------
2439
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002440static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002441nProgramStoreCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Sams331bf9b2011-04-06 11:23:54 -07002442 jboolean colorMaskR, jboolean colorMaskG, jboolean colorMaskB, jboolean colorMaskA,
2443 jboolean depthMask, jboolean ditherEnable,
2444 jint srcFunc, jint destFunc,
2445 jint depthFunc)
Jason Samsd19f10d2009-05-22 14:03:28 -07002446{
Andreas Gampe67333922014-11-10 20:35:59 -08002447 if (kLogApi) {
2448 ALOGD("nProgramStoreCreate, con(%p)", (RsContext)con);
2449 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002450 return (jlong)(uintptr_t)rsProgramStoreCreate((RsContext)con, colorMaskR, colorMaskG, colorMaskB, colorMaskA,
Jason Sams331bf9b2011-04-06 11:23:54 -07002451 depthMask, ditherEnable, (RsBlendSrcFunc)srcFunc,
2452 (RsBlendDstFunc)destFunc, (RsDepthFunc)depthFunc);
Jason Samsd19f10d2009-05-22 14:03:28 -07002453}
2454
Jason Sams0011bcf2009-12-15 12:58:36 -08002455// ---------------------------------------------------------------------------
2456
2457static void
Tim Murray460a0492013-11-19 12:45:54 -08002458nProgramBindConstants(JNIEnv *_env, jobject _this, jlong con, jlong vpv, jint slot, jlong a)
Jason Sams0011bcf2009-12-15 12:58:36 -08002459{
Andreas Gampe67333922014-11-10 20:35:59 -08002460 if (kLogApi) {
2461 ALOGD("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", (RsContext)con,
2462 (RsProgramVertex)vpv, slot, (RsAllocation)a);
2463 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002464 rsProgramBindConstants((RsContext)con, (RsProgram)vpv, slot, (RsAllocation)a);
Jason Sams0011bcf2009-12-15 12:58:36 -08002465}
Jason Sams54c0ec12009-11-30 14:49:55 -08002466
Jason Sams68afd012009-12-17 16:55:08 -08002467static void
Tim Murray460a0492013-11-19 12:45:54 -08002468nProgramBindTexture(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002469{
Andreas Gampe67333922014-11-10 20:35:59 -08002470 if (kLogApi) {
2471 ALOGD("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2472 (RsProgramFragment)vpf, slot, (RsAllocation)a);
2473 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002474 rsProgramBindTexture((RsContext)con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
Jason Sams68afd012009-12-17 16:55:08 -08002475}
2476
2477static void
Tim Murray460a0492013-11-19 12:45:54 -08002478nProgramBindSampler(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002479{
Andreas Gampe67333922014-11-10 20:35:59 -08002480 if (kLogApi) {
2481 ALOGD("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2482 (RsProgramFragment)vpf, slot, (RsSampler)a);
2483 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002484 rsProgramBindSampler((RsContext)con, (RsProgramFragment)vpf, slot, (RsSampler)a);
Jason Sams68afd012009-12-17 16:55:08 -08002485}
2486
Jason Samsd19f10d2009-05-22 14:03:28 -07002487// ---------------------------------------------------------------------------
2488
Tim Murray460a0492013-11-19 12:45:54 -08002489static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002490nProgramFragmentCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002491 jobjectArray texNames, jlongArray params)
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002492{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002493 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002494 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002495 jint paramLen = _env->GetArrayLength(params);
Miao Wangba8766c2015-10-12 17:24:13 -07002496 if (jParamPtr == nullptr) {
2497 ALOGE("Failed to get Java array elements");
2498 return 0;
2499 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002500
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002501 int texCount = _env->GetArrayLength(texNames);
2502 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2503 const char ** nameArray = names.c_str();
2504 size_t* sizeArray = names.c_str_len();
2505
Andreas Gampe67333922014-11-10 20:35:59 -08002506 if (kLogApi) {
2507 ALOGD("nProgramFragmentCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2508 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002509
Ashok Bhat98071552014-02-12 09:54:43 +00002510 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2511 for(int i = 0; i < paramLen; ++i) {
2512 paramPtr[i] = (uintptr_t)jParamPtr[i];
2513 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002514 jlong ret = (jlong)(uintptr_t)rsProgramFragmentCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002515 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002516 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002517
Ashok Bhat98071552014-02-12 09:54:43 +00002518 free(paramPtr);
2519 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002520 return ret;
2521}
2522
2523
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002524// ---------------------------------------------------------------------------
2525
Tim Murray460a0492013-11-19 12:45:54 -08002526static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002527nProgramVertexCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002528 jobjectArray texNames, jlongArray params)
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002529{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002530 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002531 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams0011bcf2009-12-15 12:58:36 -08002532 jint paramLen = _env->GetArrayLength(params);
Miao Wangba8766c2015-10-12 17:24:13 -07002533 if (jParamPtr == nullptr) {
2534 ALOGE("Failed to get Java array elements");
2535 return 0;
2536 }
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002537
Andreas Gampe67333922014-11-10 20:35:59 -08002538 if (kLogApi) {
2539 ALOGD("nProgramVertexCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2540 }
Jason Sams0011bcf2009-12-15 12:58:36 -08002541
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002542 int texCount = _env->GetArrayLength(texNames);
2543 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2544 const char ** nameArray = names.c_str();
2545 size_t* sizeArray = names.c_str_len();
2546
Ashok Bhat98071552014-02-12 09:54:43 +00002547 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2548 for(int i = 0; i < paramLen; ++i) {
2549 paramPtr[i] = (uintptr_t)jParamPtr[i];
2550 }
2551
Tim Murray3aa89c12014-08-18 17:51:22 -07002552 jlong ret = (jlong)(uintptr_t)rsProgramVertexCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002553 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002554 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002555
Ashok Bhat98071552014-02-12 09:54:43 +00002556 free(paramPtr);
2557 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams0011bcf2009-12-15 12:58:36 -08002558 return ret;
2559}
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002560
Jason Samsebfb4362009-09-23 13:57:02 -07002561// ---------------------------------------------------------------------------
2562
Tim Murray460a0492013-11-19 12:45:54 -08002563static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002564nProgramRasterCreate(JNIEnv *_env, jobject _this, jlong con, jboolean pointSprite, jint cull)
Jason Samsebfb4362009-09-23 13:57:02 -07002565{
Andreas Gampe67333922014-11-10 20:35:59 -08002566 if (kLogApi) {
2567 ALOGD("nProgramRasterCreate, con(%p), pointSprite(%i), cull(%i)", (RsContext)con,
2568 pointSprite, cull);
2569 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002570 return (jlong)(uintptr_t)rsProgramRasterCreate((RsContext)con, pointSprite, (RsCullMode)cull);
Jason Samsebfb4362009-09-23 13:57:02 -07002571}
2572
Jason Samsd19f10d2009-05-22 14:03:28 -07002573
2574// ---------------------------------------------------------------------------
2575
2576static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002577nContextBindRootScript(JNIEnv *_env, jobject _this, jlong con, jlong script)
Jason Samsd19f10d2009-05-22 14:03:28 -07002578{
Andreas Gampe67333922014-11-10 20:35:59 -08002579 if (kLogApi) {
2580 ALOGD("nContextBindRootScript, con(%p), script(%p)", (RsContext)con, (RsScript)script);
2581 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002582 rsContextBindRootScript((RsContext)con, (RsScript)script);
Jason Samsd19f10d2009-05-22 14:03:28 -07002583}
2584
2585static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002586nContextBindProgramStore(JNIEnv *_env, jobject _this, jlong con, jlong pfs)
Jason Samsd19f10d2009-05-22 14:03:28 -07002587{
Andreas Gampe67333922014-11-10 20:35:59 -08002588 if (kLogApi) {
2589 ALOGD("nContextBindProgramStore, con(%p), pfs(%p)", (RsContext)con, (RsProgramStore)pfs);
2590 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002591 rsContextBindProgramStore((RsContext)con, (RsProgramStore)pfs);
Jason Samsd19f10d2009-05-22 14:03:28 -07002592}
2593
2594static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002595nContextBindProgramFragment(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsd19f10d2009-05-22 14:03:28 -07002596{
Andreas Gampe67333922014-11-10 20:35:59 -08002597 if (kLogApi) {
2598 ALOGD("nContextBindProgramFragment, con(%p), pf(%p)", (RsContext)con,
2599 (RsProgramFragment)pf);
2600 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002601 rsContextBindProgramFragment((RsContext)con, (RsProgramFragment)pf);
Jason Samsd19f10d2009-05-22 14:03:28 -07002602}
2603
Jason Sams0826a6f2009-06-15 19:04:56 -07002604static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002605nContextBindProgramVertex(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Sams0826a6f2009-06-15 19:04:56 -07002606{
Andreas Gampe67333922014-11-10 20:35:59 -08002607 if (kLogApi) {
2608 ALOGD("nContextBindProgramVertex, con(%p), pf(%p)", (RsContext)con, (RsProgramVertex)pf);
2609 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002610 rsContextBindProgramVertex((RsContext)con, (RsProgramVertex)pf);
Jason Sams0826a6f2009-06-15 19:04:56 -07002611}
2612
Joe Onoratod7b37742009-08-09 22:57:44 -07002613static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002614nContextBindProgramRaster(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsebfb4362009-09-23 13:57:02 -07002615{
Andreas Gampe67333922014-11-10 20:35:59 -08002616 if (kLogApi) {
2617 ALOGD("nContextBindProgramRaster, con(%p), pf(%p)", (RsContext)con, (RsProgramRaster)pf);
2618 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002619 rsContextBindProgramRaster((RsContext)con, (RsProgramRaster)pf);
Jason Samsebfb4362009-09-23 13:57:02 -07002620}
2621
Joe Onoratod7b37742009-08-09 22:57:44 -07002622
Jason Sams02fb2cb2009-05-28 15:37:57 -07002623// ---------------------------------------------------------------------------
2624
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002625static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002626nSamplerCreate(JNIEnv *_env, jobject _this, jlong con, jint magFilter, jint minFilter,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002627 jint wrapS, jint wrapT, jint wrapR, jfloat aniso)
Jason Sams02fb2cb2009-05-28 15:37:57 -07002628{
Andreas Gampe67333922014-11-10 20:35:59 -08002629 if (kLogApi) {
2630 ALOGD("nSamplerCreate, con(%p)", (RsContext)con);
2631 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002632 return (jlong)(uintptr_t)rsSamplerCreate((RsContext)con,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002633 (RsSamplerValue)magFilter,
2634 (RsSamplerValue)minFilter,
2635 (RsSamplerValue)wrapS,
2636 (RsSamplerValue)wrapT,
2637 (RsSamplerValue)wrapR,
2638 aniso);
Jason Sams02fb2cb2009-05-28 15:37:57 -07002639}
2640
Jason Samsbba134c2009-06-22 15:49:21 -07002641// ---------------------------------------------------------------------------
2642
Tim Murray460a0492013-11-19 12:45:54 -08002643static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002644nMeshCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _vtx, jlongArray _idx, jintArray _prim)
Jason Samsbba134c2009-06-22 15:49:21 -07002645{
Andreas Gampe67333922014-11-10 20:35:59 -08002646 if (kLogApi) {
2647 ALOGD("nMeshCreate, con(%p)", (RsContext)con);
2648 }
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002649
Miao Wanga4ad5f82016-02-11 12:32:39 -08002650 jlong id = 0;
2651
2652 RsAllocation* vtxPtr;
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002653 jint vtxLen = _env->GetArrayLength(_vtx);
Chris Wailes488230c32014-08-14 11:22:40 -07002654 jlong *jVtxPtr = _env->GetLongArrayElements(_vtx, nullptr);
Miao Wanga4ad5f82016-02-11 12:32:39 -08002655
2656 RsAllocation* idxPtr;
2657 jint idxLen = _env->GetArrayLength(_idx);
2658 jlong *jIdxPtr = _env->GetLongArrayElements(_idx, nullptr);
2659
2660 jint primLen = _env->GetArrayLength(_prim);
2661 jint *primPtr = _env->GetIntArrayElements(_prim, nullptr);
2662
Miao Wangba8766c2015-10-12 17:24:13 -07002663 if (jVtxPtr == nullptr) {
2664 ALOGE("Failed to get Java array elements: vtx");
Miao Wanga4ad5f82016-02-11 12:32:39 -08002665 goto cleanupMesh;
Miao Wangba8766c2015-10-12 17:24:13 -07002666 }
Miao Wanga4ad5f82016-02-11 12:32:39 -08002667 if (jIdxPtr == nullptr) {
2668 ALOGE("Failed to get Java array elements: idx");
2669 goto cleanupMesh;
2670 }
2671 if (primPtr == nullptr) {
2672 ALOGE("Failed to get Java array elements: prim");
2673 goto cleanupMesh;
2674 }
2675
2676 vtxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * vtxLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002677 for(int i = 0; i < vtxLen; ++i) {
2678 vtxPtr[i] = (RsAllocation)(uintptr_t)jVtxPtr[i];
2679 }
2680
Miao Wanga4ad5f82016-02-11 12:32:39 -08002681 idxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * idxLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002682 for(int i = 0; i < idxLen; ++i) {
2683 idxPtr[i] = (RsAllocation)(uintptr_t)jIdxPtr[i];
2684 }
2685
Miao Wanga4ad5f82016-02-11 12:32:39 -08002686 id = (jlong)(uintptr_t)rsMeshCreate((RsContext)con,
2687 (RsAllocation *)vtxPtr, vtxLen,
2688 (RsAllocation *)idxPtr, idxLen,
2689 (uint32_t *)primPtr, primLen);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002690
Ashok Bhat98071552014-02-12 09:54:43 +00002691 free(vtxPtr);
2692 free(idxPtr);
Miao Wanga4ad5f82016-02-11 12:32:39 -08002693
2694cleanupMesh:
2695 if (jVtxPtr != nullptr) {
2696 _env->ReleaseLongArrayElements(_vtx, jVtxPtr, 0);
2697 }
2698 if (jIdxPtr != nullptr) {
2699 _env->ReleaseLongArrayElements(_idx, jIdxPtr, 0);
2700 }
2701 if (primPtr != nullptr) {
2702 _env->ReleaseIntArrayElements(_prim, primPtr, 0);
2703 }
2704
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002705 return id;
2706}
2707
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002708static jint
Tim Murray460a0492013-11-19 12:45:54 -08002709nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002710{
Andreas Gampe67333922014-11-10 20:35:59 -08002711 if (kLogApi) {
2712 ALOGD("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2713 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002714 jint vtxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002715 rsaMeshGetVertexBufferCount((RsContext)con, (RsMesh)mesh, &vtxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002716 return vtxCount;
2717}
2718
2719static jint
Tim Murray460a0492013-11-19 12:45:54 -08002720nMeshGetIndexCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002721{
Andreas Gampe67333922014-11-10 20:35:59 -08002722 if (kLogApi) {
2723 ALOGD("nMeshGetIndexCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2724 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002725 jint idxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002726 rsaMeshGetIndexCount((RsContext)con, (RsMesh)mesh, &idxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002727 return idxCount;
2728}
2729
2730static void
Ashok Bhat98071552014-02-12 09:54:43 +00002731nMeshGetVertices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _ids, jint numVtxIDs)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002732{
Andreas Gampe67333922014-11-10 20:35:59 -08002733 if (kLogApi) {
2734 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2735 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002736
2737 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
Tim Murrayeff663f2013-11-15 13:08:30 -08002738 rsaMeshGetVertices((RsContext)con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002739
2740 for(jint i = 0; i < numVtxIDs; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002741 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002742 _env->SetLongArrayRegion(_ids, i, 1, &alloc);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002743 }
2744
2745 free(allocs);
2746}
2747
2748static void
Ashok Bhat98071552014-02-12 09:54:43 +00002749nMeshGetIndices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _idxIds, jintArray _primitives, jint numIndices)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002750{
Andreas Gampe67333922014-11-10 20:35:59 -08002751 if (kLogApi) {
2752 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2753 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002754
2755 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
2756 uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
2757
Tim Murrayeff663f2013-11-15 13:08:30 -08002758 rsaMeshGetIndices((RsContext)con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002759
2760 for(jint i = 0; i < numIndices; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002761 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002762 const jint prim = (jint)prims[i];
2763 _env->SetLongArrayRegion(_idxIds, i, 1, &alloc);
2764 _env->SetIntArrayRegion(_primitives, i, 1, &prim);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002765 }
2766
2767 free(allocs);
2768 free(prims);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002769}
2770
Tim Murray56f9e6f2014-05-16 11:47:26 -07002771static jint
2772nSystemGetPointerSize(JNIEnv *_env, jobject _this) {
2773 return (jint)sizeof(void*);
2774}
2775
Miao Wang0facf022015-11-25 11:21:13 -08002776static jobject
2777nAllocationGetByteBuffer(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
2778 jlongArray strideArr, jint xBytesSize,
2779 jint dimY, jint dimZ) {
2780 if (kLogApi) {
2781 ALOGD("nAllocationGetByteBuffer, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
2782 }
Tim Murray56f9e6f2014-05-16 11:47:26 -07002783
Miao Wang0facf022015-11-25 11:21:13 -08002784 jlong *jStridePtr = _env->GetLongArrayElements(strideArr, nullptr);
2785 if (jStridePtr == nullptr) {
2786 ALOGE("Failed to get Java array elements: strideArr");
2787 return 0;
2788 }
2789
2790 size_t strideIn = xBytesSize;
2791 void* ptr = nullptr;
2792 if (alloc != 0) {
2793 ptr = rsAllocationGetPointer((RsContext)con, (RsAllocation)alloc, 0,
2794 RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, 0, 0,
2795 &strideIn, sizeof(size_t));
2796 }
2797
2798 jobject byteBuffer = nullptr;
2799 if (ptr != nullptr) {
2800 size_t bufferSize = strideIn;
2801 jStridePtr[0] = strideIn;
2802 if (dimY > 0) {
2803 bufferSize *= dimY;
2804 }
2805 if (dimZ > 0) {
2806 bufferSize *= dimZ;
2807 }
2808 byteBuffer = _env->NewDirectByteBuffer(ptr, (jlong) bufferSize);
2809 }
2810 _env->ReleaseLongArrayElements(strideArr, jStridePtr, 0);
2811 return byteBuffer;
2812}
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002813// ---------------------------------------------------------------------------
2814
Jason Samsd19f10d2009-05-22 14:03:28 -07002815
Jason Sams94d8e90a2009-06-10 16:09:05 -07002816static const char *classPathName = "android/renderscript/RenderScript";
Jason Samsd19f10d2009-05-22 14:03:28 -07002817
Daniel Micay76f6a862015-09-19 17:31:01 -04002818static const JNINativeMethod methods[] = {
Jason Sams1c415172010-11-08 17:06:46 -08002819{"_nInit", "()V", (void*)_nInit },
Jason Samsea84a7c2009-09-04 14:42:41 -07002820
Tim Murrayeff663f2013-11-15 13:08:30 -08002821{"nDeviceCreate", "()J", (void*)nDeviceCreate },
2822{"nDeviceDestroy", "(J)V", (void*)nDeviceDestroy },
2823{"nDeviceSetConfig", "(JII)V", (void*)nDeviceSetConfig },
2824{"nContextGetUserMessage", "(J[I)I", (void*)nContextGetUserMessage },
2825{"nContextGetErrorMessage", "(J)Ljava/lang/String;", (void*)nContextGetErrorMessage },
2826{"nContextPeekMessage", "(J[I)I", (void*)nContextPeekMessage },
Jason Sams1c415172010-11-08 17:06:46 -08002827
Tim Murrayeff663f2013-11-15 13:08:30 -08002828{"nContextInitToClient", "(J)V", (void*)nContextInitToClient },
2829{"nContextDeinitToClient", "(J)V", (void*)nContextDeinitToClient },
Jason Samsd19f10d2009-05-22 14:03:28 -07002830
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002831
Jason Sams2e1872f2010-08-17 16:25:41 -07002832// All methods below are thread protected in java.
Tim Murrayeff663f2013-11-15 13:08:30 -08002833{"rsnContextCreate", "(JIII)J", (void*)nContextCreate },
2834{"rsnContextCreateGL", "(JIIIIIIIIIIIIFI)J", (void*)nContextCreateGL },
2835{"rsnContextFinish", "(J)V", (void*)nContextFinish },
2836{"rsnContextSetPriority", "(JI)V", (void*)nContextSetPriority },
Tim Murray47f31582015-04-07 15:43:24 -07002837{"rsnContextSetCacheDir", "(JLjava/lang/String;)V", (void*)nContextSetCacheDir },
Tim Murrayeff663f2013-11-15 13:08:30 -08002838{"rsnContextSetSurface", "(JIILandroid/view/Surface;)V", (void*)nContextSetSurface },
2839{"rsnContextDestroy", "(J)V", (void*)nContextDestroy },
2840{"rsnContextDump", "(JI)V", (void*)nContextDump },
2841{"rsnContextPause", "(J)V", (void*)nContextPause },
2842{"rsnContextResume", "(J)V", (void*)nContextResume },
2843{"rsnContextSendMessage", "(JI[I)V", (void*)nContextSendMessage },
Yang Ni281c3252014-10-24 08:52:24 -07002844{"rsnClosureCreate", "(JJJ[J[J[I[J[J)J", (void*)nClosureCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002845{"rsnInvokeClosureCreate", "(JJ[B[J[J[I)J", (void*)nInvokeClosureCreate },
Yang Ni281c3252014-10-24 08:52:24 -07002846{"rsnClosureSetArg", "(JJIJI)V", (void*)nClosureSetArg },
2847{"rsnClosureSetGlobal", "(JJJJI)V", (void*)nClosureSetGlobal },
Tim Murray460a0492013-11-19 12:45:54 -08002848{"rsnAssignName", "(JJ[B)V", (void*)nAssignName },
2849{"rsnGetName", "(JJ)Ljava/lang/String;", (void*)nGetName },
2850{"rsnObjDestroy", "(JJ)V", (void*)nObjDestroy },
Jason Sams64676f32009-07-08 18:01:53 -07002851
Tim Murray460a0492013-11-19 12:45:54 -08002852{"rsnFileA3DCreateFromFile", "(JLjava/lang/String;)J", (void*)nFileA3DCreateFromFile },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002853{"rsnFileA3DCreateFromAssetStream", "(JJ)J", (void*)nFileA3DCreateFromAssetStream },
Tim Murray460a0492013-11-19 12:45:54 -08002854{"rsnFileA3DCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;)J", (void*)nFileA3DCreateFromAsset },
2855{"rsnFileA3DGetNumIndexEntries", "(JJ)I", (void*)nFileA3DGetNumIndexEntries },
2856{"rsnFileA3DGetIndexEntries", "(JJI[I[Ljava/lang/String;)V", (void*)nFileA3DGetIndexEntries },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002857{"rsnFileA3DGetEntryByIndex", "(JJI)J", (void*)nFileA3DGetEntryByIndex },
Jason Samsd19f10d2009-05-22 14:03:28 -07002858
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002859{"rsnFontCreateFromFile", "(JLjava/lang/String;FI)J", (void*)nFontCreateFromFile },
2860{"rsnFontCreateFromAssetStream", "(JLjava/lang/String;FIJ)J", (void*)nFontCreateFromAssetStream },
2861{"rsnFontCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;FI)J", (void*)nFontCreateFromAsset },
Jason Samsd19f10d2009-05-22 14:03:28 -07002862
Tim Murray460a0492013-11-19 12:45:54 -08002863{"rsnElementCreate", "(JJIZI)J", (void*)nElementCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002864{"rsnElementCreate2", "(J[J[Ljava/lang/String;[I)J", (void*)nElementCreate2 },
Tim Murray460a0492013-11-19 12:45:54 -08002865{"rsnElementGetNativeData", "(JJ[I)V", (void*)nElementGetNativeData },
Ashok Bhat98071552014-02-12 09:54:43 +00002866{"rsnElementGetSubElements", "(JJ[J[Ljava/lang/String;[I)V", (void*)nElementGetSubElements },
Jason Samsd19f10d2009-05-22 14:03:28 -07002867
Tim Murray460a0492013-11-19 12:45:54 -08002868{"rsnTypeCreate", "(JJIIIZZI)J", (void*)nTypeCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002869{"rsnTypeGetNativeData", "(JJ[J)V", (void*)nTypeGetNativeData },
Jason Samsd19f10d2009-05-22 14:03:28 -07002870
Ashok Bhat98071552014-02-12 09:54:43 +00002871{"rsnAllocationCreateTyped", "(JJIIJ)J", (void*)nAllocationCreateTyped },
Tim Murray460a0492013-11-19 12:45:54 -08002872{"rsnAllocationCreateFromBitmap", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateFromBitmap },
2873{"rsnAllocationCreateBitmapBackedAllocation", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateBitmapBackedAllocation },
2874{"rsnAllocationCubeCreateFromBitmap","(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCubeCreateFromBitmap },
Jason Sams5476b452010-12-08 16:14:36 -08002875
Tim Murray460a0492013-11-19 12:45:54 -08002876{"rsnAllocationCopyFromBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyFromBitmap },
2877{"rsnAllocationCopyToBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyToBitmap },
Jason Sams4ef66502010-12-10 16:03:15 -08002878
Tim Murray460a0492013-11-19 12:45:54 -08002879{"rsnAllocationSyncAll", "(JJI)V", (void*)nAllocationSyncAll },
Miao Wang8c150922015-10-26 17:44:10 -07002880{"rsnAllocationSetupBufferQueue", "(JJI)V", (void*)nAllocationSetupBufferQueue },
2881{"rsnAllocationShareBufferQueue", "(JJJ)V", (void*)nAllocationShareBufferQueue },
Tim Murray460a0492013-11-19 12:45:54 -08002882{"rsnAllocationGetSurface", "(JJ)Landroid/view/Surface;", (void*)nAllocationGetSurface },
2883{"rsnAllocationSetSurface", "(JJLandroid/view/Surface;)V", (void*)nAllocationSetSurface },
2884{"rsnAllocationIoSend", "(JJ)V", (void*)nAllocationIoSend },
Miao Wang8c150922015-10-26 17:44:10 -07002885{"rsnAllocationIoReceive", "(JJ)J", (void*)nAllocationIoReceive },
Miao Wang87e908d2015-03-02 15:15:15 -08002886{"rsnAllocationData1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData1D },
Miao Wangc8e237e2015-02-20 18:36:32 -08002887{"rsnAllocationElementData", "(JJIIIII[BI)V", (void*)nAllocationElementData },
Miao Wang87e908d2015-03-02 15:15:15 -08002888{"rsnAllocationData2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData2D },
Tim Murray460a0492013-11-19 12:45:54 -08002889{"rsnAllocationData2D", "(JJIIIIIIJIIII)V", (void*)nAllocationData2D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002890{"rsnAllocationData3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData3D },
Tim Murray460a0492013-11-19 12:45:54 -08002891{"rsnAllocationData3D", "(JJIIIIIIIJIIII)V", (void*)nAllocationData3D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002892{"rsnAllocationRead", "(JJLjava/lang/Object;IIZ)V", (void*)nAllocationRead },
2893{"rsnAllocationRead1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead1D },
Miao Wang45cec0a2015-03-04 16:40:21 -08002894{"rsnAllocationElementRead", "(JJIIIII[BI)V", (void*)nAllocationElementRead },
Miao Wang87e908d2015-03-02 15:15:15 -08002895{"rsnAllocationRead2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead2D },
2896{"rsnAllocationRead3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead3D },
Tim Murray460a0492013-11-19 12:45:54 -08002897{"rsnAllocationGetType", "(JJ)J", (void*)nAllocationGetType},
2898{"rsnAllocationResize1D", "(JJI)V", (void*)nAllocationResize1D },
2899{"rsnAllocationGenerateMipmaps", "(JJ)V", (void*)nAllocationGenerateMipmaps },
Jason Samsbd1c3ad2009-08-03 16:03:08 -07002900
Jason Sams46ba27e32015-02-06 17:45:15 -08002901{"rsnAllocationAdapterCreate", "(JJJ)J", (void*)nAllocationAdapterCreate },
2902{"rsnAllocationAdapterOffset", "(JJIIIIIIIII)V", (void*)nAllocationAdapterOffset },
2903
Tim Murray460a0492013-11-19 12:45:54 -08002904{"rsnScriptBindAllocation", "(JJJI)V", (void*)nScriptBindAllocation },
2905{"rsnScriptSetTimeZone", "(JJ[B)V", (void*)nScriptSetTimeZone },
2906{"rsnScriptInvoke", "(JJI)V", (void*)nScriptInvoke },
2907{"rsnScriptInvokeV", "(JJI[B)V", (void*)nScriptInvokeV },
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002908
2909{"rsnScriptForEach", "(JJI[JJ[B[I)V", (void*)nScriptForEach },
David Gross4a457852016-06-02 14:46:55 -07002910{"rsnScriptReduce", "(JJI[JJ[I)V", (void*)nScriptReduce },
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002911
Tim Murray460a0492013-11-19 12:45:54 -08002912{"rsnScriptSetVarI", "(JJII)V", (void*)nScriptSetVarI },
2913{"rsnScriptGetVarI", "(JJI)I", (void*)nScriptGetVarI },
2914{"rsnScriptSetVarJ", "(JJIJ)V", (void*)nScriptSetVarJ },
2915{"rsnScriptGetVarJ", "(JJI)J", (void*)nScriptGetVarJ },
2916{"rsnScriptSetVarF", "(JJIF)V", (void*)nScriptSetVarF },
2917{"rsnScriptGetVarF", "(JJI)F", (void*)nScriptGetVarF },
2918{"rsnScriptSetVarD", "(JJID)V", (void*)nScriptSetVarD },
2919{"rsnScriptGetVarD", "(JJI)D", (void*)nScriptGetVarD },
2920{"rsnScriptSetVarV", "(JJI[B)V", (void*)nScriptSetVarV },
2921{"rsnScriptGetVarV", "(JJI[B)V", (void*)nScriptGetVarV },
2922{"rsnScriptSetVarVE", "(JJI[BJ[I)V", (void*)nScriptSetVarVE },
2923{"rsnScriptSetVarObj", "(JJIJ)V", (void*)nScriptSetVarObj },
Jason Samsd19f10d2009-05-22 14:03:28 -07002924
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002925{"rsnScriptCCreate", "(JLjava/lang/String;Ljava/lang/String;[BI)J", (void*)nScriptCCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002926{"rsnScriptIntrinsicCreate", "(JIJ)J", (void*)nScriptIntrinsicCreate },
2927{"rsnScriptKernelIDCreate", "(JJII)J", (void*)nScriptKernelIDCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002928{"rsnScriptInvokeIDCreate", "(JJI)J", (void*)nScriptInvokeIDCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002929{"rsnScriptFieldIDCreate", "(JJI)J", (void*)nScriptFieldIDCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002930{"rsnScriptGroupCreate", "(J[J[J[J[J[J)J", (void*)nScriptGroupCreate },
Yang Ni35be56c2015-04-02 17:47:56 -07002931{"rsnScriptGroup2Create", "(JLjava/lang/String;Ljava/lang/String;[J)J", (void*)nScriptGroup2Create },
Tim Murray460a0492013-11-19 12:45:54 -08002932{"rsnScriptGroupSetInput", "(JJJJ)V", (void*)nScriptGroupSetInput },
2933{"rsnScriptGroupSetOutput", "(JJJJ)V", (void*)nScriptGroupSetOutput },
2934{"rsnScriptGroupExecute", "(JJ)V", (void*)nScriptGroupExecute },
Yang Ni281c3252014-10-24 08:52:24 -07002935{"rsnScriptGroup2Execute", "(JJ)V", (void*)nScriptGroup2Execute },
Jason Sams0011bcf2009-12-15 12:58:36 -08002936
Tim Murray25207df2015-01-12 16:47:56 -08002937{"rsnScriptIntrinsicBLAS_Single", "(JJIIIIIIIIIFJJFJIIII)V", (void*)nScriptIntrinsicBLAS_Single },
2938{"rsnScriptIntrinsicBLAS_Double", "(JJIIIIIIIIIDJJDJIIII)V", (void*)nScriptIntrinsicBLAS_Double },
2939{"rsnScriptIntrinsicBLAS_Complex", "(JJIIIIIIIIIFFJJFFJIIII)V", (void*)nScriptIntrinsicBLAS_Complex },
2940{"rsnScriptIntrinsicBLAS_Z", "(JJIIIIIIIIIDDJJDDJIIII)V", (void*)nScriptIntrinsicBLAS_Z },
2941
Tim Murray9cb16a22015-04-01 11:07:16 -07002942{"rsnScriptIntrinsicBLAS_BNNM", "(JJIIIJIJIJII)V", (void*)nScriptIntrinsicBLAS_BNNM },
2943
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002944{"rsnProgramStoreCreate", "(JZZZZZZIII)J", (void*)nProgramStoreCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002945
Tim Murray460a0492013-11-19 12:45:54 -08002946{"rsnProgramBindConstants", "(JJIJ)V", (void*)nProgramBindConstants },
2947{"rsnProgramBindTexture", "(JJIJ)V", (void*)nProgramBindTexture },
2948{"rsnProgramBindSampler", "(JJIJ)V", (void*)nProgramBindSampler },
Jason Samsebfb4362009-09-23 13:57:02 -07002949
Ashok Bhat98071552014-02-12 09:54:43 +00002950{"rsnProgramFragmentCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramFragmentCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002951{"rsnProgramRasterCreate", "(JZI)J", (void*)nProgramRasterCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002952{"rsnProgramVertexCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramVertexCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002953
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002954{"rsnContextBindRootScript", "(JJ)V", (void*)nContextBindRootScript },
2955{"rsnContextBindProgramStore", "(JJ)V", (void*)nContextBindProgramStore },
2956{"rsnContextBindProgramFragment", "(JJ)V", (void*)nContextBindProgramFragment },
2957{"rsnContextBindProgramVertex", "(JJ)V", (void*)nContextBindProgramVertex },
2958{"rsnContextBindProgramRaster", "(JJ)V", (void*)nContextBindProgramRaster },
Jason Sams02fb2cb2009-05-28 15:37:57 -07002959
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002960{"rsnSamplerCreate", "(JIIIIIF)J", (void*)nSamplerCreate },
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002961
Ashok Bhat98071552014-02-12 09:54:43 +00002962{"rsnMeshCreate", "(J[J[J[I)J", (void*)nMeshCreate },
Jason Sams2e1872f2010-08-17 16:25:41 -07002963
Tim Murray460a0492013-11-19 12:45:54 -08002964{"rsnMeshGetVertexBufferCount", "(JJ)I", (void*)nMeshGetVertexBufferCount },
2965{"rsnMeshGetIndexCount", "(JJ)I", (void*)nMeshGetIndexCount },
Ashok Bhat98071552014-02-12 09:54:43 +00002966{"rsnMeshGetVertices", "(JJ[JI)V", (void*)nMeshGetVertices },
2967{"rsnMeshGetIndices", "(JJ[J[II)V", (void*)nMeshGetIndices },
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002968
Tim Murray56f9e6f2014-05-16 11:47:26 -07002969{"rsnSystemGetPointerSize", "()I", (void*)nSystemGetPointerSize },
Miao Wang0facf022015-11-25 11:21:13 -08002970{"rsnAllocationGetByteBuffer", "(JJ[JIII)Ljava/nio/ByteBuffer;", (void*)nAllocationGetByteBuffer },
Jason Samsd19f10d2009-05-22 14:03:28 -07002971};
2972
2973static int registerFuncs(JNIEnv *_env)
2974{
2975 return android::AndroidRuntime::registerNativeMethods(
2976 _env, classPathName, methods, NELEM(methods));
2977}
2978
2979// ---------------------------------------------------------------------------
2980
2981jint JNI_OnLoad(JavaVM* vm, void* reserved)
2982{
Chris Wailes488230c32014-08-14 11:22:40 -07002983 JNIEnv* env = nullptr;
Jason Samsd19f10d2009-05-22 14:03:28 -07002984 jint result = -1;
2985
Jason Samsd19f10d2009-05-22 14:03:28 -07002986 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
Steve Block3762c312012-01-06 19:20:56 +00002987 ALOGE("ERROR: GetEnv failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07002988 goto bail;
2989 }
Chris Wailes488230c32014-08-14 11:22:40 -07002990 assert(env != nullptr);
Jason Samsd19f10d2009-05-22 14:03:28 -07002991
2992 if (registerFuncs(env) < 0) {
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002993 ALOGE("ERROR: Renderscript native registration failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07002994 goto bail;
2995 }
2996
2997 /* success -- return valid version number */
2998 result = JNI_VERSION_1_4;
2999
3000bail:
3001 return result;
3002}