blob: c532efb79345e14f1a2377c04c0985a09708a7b9 [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
Stephen Hines414fa2c2014-04-17 01:02:42 -070051#define PER_ARRAY_TYPE(flag, fnc, readonly, ...) { \
Jason Samse729a942013-11-06 11:22:02 -080052 jint len = 0; \
Chris Wailes488230c32014-08-14 11:22:40 -070053 void *ptr = nullptr; \
Miao Wang87e908d2015-03-02 15:15:15 -080054 void *srcPtr = nullptr; \
Jason Sams21659ac2013-11-06 15:08:07 -080055 size_t typeBytes = 0; \
Stephen Hines414fa2c2014-04-17 01:02:42 -070056 jint relFlag = 0; \
57 if (readonly) { \
58 /* The on-release mode should only be JNI_ABORT for read-only accesses. */ \
Miao Wang87e908d2015-03-02 15:15:15 -080059 /* readonly = true, also indicates we are copying to the allocation . */ \
Stephen Hines414fa2c2014-04-17 01:02:42 -070060 relFlag = JNI_ABORT; \
61 } \
Jason Samse729a942013-11-06 11:22:02 -080062 switch(dataType) { \
63 case RS_TYPE_FLOAT_32: \
64 len = _env->GetArrayLength((jfloatArray)data); \
65 ptr = _env->GetFloatArrayElements((jfloatArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -070066 if (ptr == nullptr) { \
67 ALOGE("Failed to get Java array elements."); \
68 return; \
69 } \
Jason Sams21659ac2013-11-06 15:08:07 -080070 typeBytes = 4; \
Miao Wang87e908d2015-03-02 15:15:15 -080071 if (usePadding) { \
72 srcPtr = ptr; \
73 len = len / 3 * 4; \
74 if (count == 0) { \
75 count = len / 4; \
76 } \
77 ptr = malloc (len * typeBytes); \
78 if (readonly) { \
79 copyWithPadding(ptr, srcPtr, mSize, count); \
80 fnc(__VA_ARGS__); \
81 } else { \
82 fnc(__VA_ARGS__); \
83 copyWithUnPadding(srcPtr, ptr, mSize, count); \
84 } \
85 free(ptr); \
86 ptr = srcPtr; \
87 } else { \
88 fnc(__VA_ARGS__); \
89 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -070090 _env->ReleaseFloatArrayElements((jfloatArray)data, (jfloat *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -080091 return; \
92 case RS_TYPE_FLOAT_64: \
93 len = _env->GetArrayLength((jdoubleArray)data); \
94 ptr = _env->GetDoubleArrayElements((jdoubleArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -070095 if (ptr == nullptr) { \
96 ALOGE("Failed to get Java array elements."); \
97 return; \
98 } \
Jason Sams21659ac2013-11-06 15:08:07 -080099 typeBytes = 8; \
Miao Wang87e908d2015-03-02 15:15:15 -0800100 if (usePadding) { \
101 srcPtr = ptr; \
102 len = len / 3 * 4; \
103 if (count == 0) { \
104 count = len / 4; \
105 } \
106 ptr = malloc (len * typeBytes); \
107 if (readonly) { \
108 copyWithPadding(ptr, srcPtr, mSize, count); \
109 fnc(__VA_ARGS__); \
110 } else { \
111 fnc(__VA_ARGS__); \
112 copyWithUnPadding(srcPtr, ptr, mSize, count); \
113 } \
114 free(ptr); \
115 ptr = srcPtr; \
116 } else { \
117 fnc(__VA_ARGS__); \
118 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700119 _env->ReleaseDoubleArrayElements((jdoubleArray)data, (jdouble *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800120 return; \
121 case RS_TYPE_SIGNED_8: \
122 case RS_TYPE_UNSIGNED_8: \
123 len = _env->GetArrayLength((jbyteArray)data); \
124 ptr = _env->GetByteArrayElements((jbyteArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700125 if (ptr == nullptr) { \
126 ALOGE("Failed to get Java array elements."); \
127 return; \
128 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800129 typeBytes = 1; \
Miao Wang87e908d2015-03-02 15:15:15 -0800130 if (usePadding) { \
131 srcPtr = ptr; \
132 len = len / 3 * 4; \
133 if (count == 0) { \
134 count = len / 4; \
135 } \
136 ptr = malloc (len * typeBytes); \
137 if (readonly) { \
138 copyWithPadding(ptr, srcPtr, mSize, count); \
139 fnc(__VA_ARGS__); \
140 } else { \
141 fnc(__VA_ARGS__); \
142 copyWithUnPadding(srcPtr, ptr, mSize, count); \
143 } \
144 free(ptr); \
145 ptr = srcPtr; \
146 } else { \
147 fnc(__VA_ARGS__); \
148 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700149 _env->ReleaseByteArrayElements((jbyteArray)data, (jbyte*)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800150 return; \
151 case RS_TYPE_SIGNED_16: \
152 case RS_TYPE_UNSIGNED_16: \
Pirama Arumuga Nainar13332152016-03-01 20:37:19 -0800153 case RS_TYPE_FLOAT_16: \
Jason Samse729a942013-11-06 11:22:02 -0800154 len = _env->GetArrayLength((jshortArray)data); \
155 ptr = _env->GetShortArrayElements((jshortArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700156 if (ptr == nullptr) { \
157 ALOGE("Failed to get Java array elements."); \
158 return; \
159 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800160 typeBytes = 2; \
Miao Wang87e908d2015-03-02 15:15:15 -0800161 if (usePadding) { \
162 srcPtr = ptr; \
163 len = len / 3 * 4; \
164 if (count == 0) { \
165 count = len / 4; \
166 } \
167 ptr = malloc (len * typeBytes); \
168 if (readonly) { \
169 copyWithPadding(ptr, srcPtr, mSize, count); \
170 fnc(__VA_ARGS__); \
171 } else { \
172 fnc(__VA_ARGS__); \
173 copyWithUnPadding(srcPtr, ptr, mSize, count); \
174 } \
175 free(ptr); \
176 ptr = srcPtr; \
177 } else { \
178 fnc(__VA_ARGS__); \
179 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700180 _env->ReleaseShortArrayElements((jshortArray)data, (jshort *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800181 return; \
182 case RS_TYPE_SIGNED_32: \
183 case RS_TYPE_UNSIGNED_32: \
184 len = _env->GetArrayLength((jintArray)data); \
185 ptr = _env->GetIntArrayElements((jintArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700186 if (ptr == nullptr) { \
187 ALOGE("Failed to get Java array elements."); \
188 return; \
189 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800190 typeBytes = 4; \
Miao Wang87e908d2015-03-02 15:15:15 -0800191 if (usePadding) { \
192 srcPtr = ptr; \
193 len = len / 3 * 4; \
194 if (count == 0) { \
195 count = len / 4; \
196 } \
197 ptr = malloc (len * typeBytes); \
198 if (readonly) { \
199 copyWithPadding(ptr, srcPtr, mSize, count); \
200 fnc(__VA_ARGS__); \
201 } else { \
202 fnc(__VA_ARGS__); \
203 copyWithUnPadding(srcPtr, ptr, mSize, count); \
204 } \
205 free(ptr); \
206 ptr = srcPtr; \
207 } else { \
208 fnc(__VA_ARGS__); \
209 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700210 _env->ReleaseIntArrayElements((jintArray)data, (jint *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800211 return; \
212 case RS_TYPE_SIGNED_64: \
213 case RS_TYPE_UNSIGNED_64: \
214 len = _env->GetArrayLength((jlongArray)data); \
215 ptr = _env->GetLongArrayElements((jlongArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700216 if (ptr == nullptr) { \
217 ALOGE("Failed to get Java array elements."); \
218 return; \
219 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800220 typeBytes = 8; \
Miao Wang87e908d2015-03-02 15:15:15 -0800221 if (usePadding) { \
222 srcPtr = ptr; \
223 len = len / 3 * 4; \
224 if (count == 0) { \
225 count = len / 4; \
226 } \
227 ptr = malloc (len * typeBytes); \
228 if (readonly) { \
229 copyWithPadding(ptr, srcPtr, mSize, count); \
230 fnc(__VA_ARGS__); \
231 } else { \
232 fnc(__VA_ARGS__); \
233 copyWithUnPadding(srcPtr, ptr, mSize, count); \
234 } \
235 free(ptr); \
236 ptr = srcPtr; \
237 } else { \
238 fnc(__VA_ARGS__); \
239 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700240 _env->ReleaseLongArrayElements((jlongArray)data, (jlong *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800241 return; \
242 default: \
243 break; \
244 } \
Miao Wang87e908d2015-03-02 15:15:15 -0800245 UNUSED(len, ptr, srcPtr, typeBytes, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800246}
247
248
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800249class AutoJavaStringToUTF8 {
250public:
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800251 AutoJavaStringToUTF8(JNIEnv* env, jstring str) : fEnv(env), fJStr(str) {
Chris Wailes488230c32014-08-14 11:22:40 -0700252 fCStr = env->GetStringUTFChars(str, nullptr);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800253 fLength = env->GetStringUTFLength(str);
254 }
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800255 ~AutoJavaStringToUTF8() {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800256 fEnv->ReleaseStringUTFChars(fJStr, fCStr);
257 }
258 const char* c_str() const { return fCStr; }
259 jsize length() const { return fLength; }
260
261private:
262 JNIEnv* fEnv;
263 jstring fJStr;
264 const char* fCStr;
265 jsize fLength;
266};
267
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800268class AutoJavaStringArrayToUTF8 {
269public:
270 AutoJavaStringArrayToUTF8(JNIEnv* env, jobjectArray strings, jsize stringsLength)
271 : mEnv(env), mStrings(strings), mStringsLength(stringsLength) {
Chris Wailes488230c32014-08-14 11:22:40 -0700272 mCStrings = nullptr;
273 mSizeArray = nullptr;
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800274 if (stringsLength > 0) {
275 mCStrings = (const char **)calloc(stringsLength, sizeof(char *));
276 mSizeArray = (size_t*)calloc(stringsLength, sizeof(size_t));
277 for (jsize ct = 0; ct < stringsLength; ct ++) {
278 jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
Chris Wailes488230c32014-08-14 11:22:40 -0700279 mCStrings[ct] = mEnv->GetStringUTFChars(s, nullptr);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800280 mSizeArray[ct] = mEnv->GetStringUTFLength(s);
281 }
282 }
283 }
284 ~AutoJavaStringArrayToUTF8() {
285 for (jsize ct=0; ct < mStringsLength; ct++) {
286 jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
287 mEnv->ReleaseStringUTFChars(s, mCStrings[ct]);
288 }
289 free(mCStrings);
290 free(mSizeArray);
291 }
292 const char **c_str() const { return mCStrings; }
293 size_t *c_str_len() const { return mSizeArray; }
294 jsize length() const { return mStringsLength; }
295
296private:
297 JNIEnv *mEnv;
298 jobjectArray mStrings;
299 const char **mCStrings;
300 size_t *mSizeArray;
301 jsize mStringsLength;
302};
303
Jason Samsd19f10d2009-05-22 14:03:28 -0700304// ---------------------------------------------------------------------------
305
Jason Samsffe9f482009-06-01 17:45:53 -0700306static jfieldID gContextId = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -0700307
308static void _nInit(JNIEnv *_env, jclass _this)
309{
Tim Murrayeff663f2013-11-15 13:08:30 -0800310 gContextId = _env->GetFieldID(_this, "mContext", "J");
Jason Samsd19f10d2009-05-22 14:03:28 -0700311}
312
Jason Samsd19f10d2009-05-22 14:03:28 -0700313// ---------------------------------------------------------------------------
314
Miao Wang87e908d2015-03-02 15:15:15 -0800315static void copyWithPadding(void* ptr, void* srcPtr, int mSize, int count) {
316 int sizeBytesPad = mSize * 4;
317 int sizeBytes = mSize * 3;
318 uint8_t *dst = static_cast<uint8_t *>(ptr);
319 uint8_t *src = static_cast<uint8_t *>(srcPtr);
320 for (int i = 0; i < count; i++) {
321 memcpy(dst, src, sizeBytes);
322 dst += sizeBytesPad;
323 src += sizeBytes;
324 }
325}
326
327static void copyWithUnPadding(void* ptr, void* srcPtr, int mSize, int count) {
328 int sizeBytesPad = mSize * 4;
329 int sizeBytes = mSize * 3;
330 uint8_t *dst = static_cast<uint8_t *>(ptr);
331 uint8_t *src = static_cast<uint8_t *>(srcPtr);
332 for (int i = 0; i < count; i++) {
333 memcpy(dst, src, sizeBytes);
334 dst += sizeBytes;
335 src += sizeBytesPad;
336 }
337}
338
339
340// ---------------------------------------------------------------------------
Jason Sams3eaa338e2009-06-10 15:04:38 -0700341static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800342nContextFinish(JNIEnv *_env, jobject _this, jlong con)
Jason Sams96ed4cf2010-06-15 12:15:57 -0700343{
Andreas Gampe67333922014-11-10 20:35:59 -0800344 if (kLogApi) {
345 ALOGD("nContextFinish, con(%p)", (RsContext)con);
346 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800347 rsContextFinish((RsContext)con);
Jason Sams96ed4cf2010-06-15 12:15:57 -0700348}
349
Yang Ni281c3252014-10-24 08:52:24 -0700350static jlong
351nClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong kernelID,
352 jlong returnValue, jlongArray fieldIDArray,
353 jlongArray valueArray, jintArray sizeArray,
354 jlongArray depClosureArray, jlongArray depFieldIDArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700355 jlong ret = 0;
356
Yang Ni281c3252014-10-24 08:52:24 -0700357 jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
358 jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700359 if (jFieldIDs == nullptr) {
360 ALOGE("Failed to get Java array elements: fieldIDs.");
361 return ret;
362 }
363
Yang Ni281c3252014-10-24 08:52:24 -0700364 jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
365 jsize values_length = _env->GetArrayLength(valueArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700366 if (jValues == nullptr) {
367 ALOGE("Failed to get Java array elements: values.");
368 return ret;
369 }
370
Yang Ni17c2d7a2015-04-30 16:13:54 -0700371 jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
Yang Ni281c3252014-10-24 08:52:24 -0700372 jsize sizes_length = _env->GetArrayLength(sizeArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700373 if (jSizes == nullptr) {
374 ALOGE("Failed to get Java array elements: sizes.");
375 return ret;
376 }
377
Yang Ni281c3252014-10-24 08:52:24 -0700378 jlong* jDepClosures =
379 _env->GetLongArrayElements(depClosureArray, nullptr);
380 jsize depClosures_length = _env->GetArrayLength(depClosureArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700381 if (jDepClosures == nullptr) {
382 ALOGE("Failed to get Java array elements: depClosures.");
383 return ret;
384 }
385
Yang Ni281c3252014-10-24 08:52:24 -0700386 jlong* jDepFieldIDs =
387 _env->GetLongArrayElements(depFieldIDArray, nullptr);
388 jsize depFieldIDs_length = _env->GetArrayLength(depFieldIDArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700389 if (jDepFieldIDs == nullptr) {
390 ALOGE("Failed to get Java array elements: depFieldIDs.");
391 return ret;
392 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700393
394 size_t numValues, numDependencies;
395 RsScriptFieldID* fieldIDs;
Yang Ni17c2d7a2015-04-30 16:13:54 -0700396 RsClosure* depClosures;
397 RsScriptFieldID* depFieldIDs;
398
399 if (fieldIDs_length != values_length || values_length != sizes_length) {
400 ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
401 goto exit;
402 }
403
404 numValues = (size_t)fieldIDs_length;
405
406 if (depClosures_length != depFieldIDs_length) {
407 ALOGE("Unmatched closures and field IDs for dependencies in closure creation.");
408 goto exit;
409 }
410
411 numDependencies = (size_t)depClosures_length;
412
413 if (numDependencies > numValues) {
414 ALOGE("Unexpected number of dependencies in closure creation");
415 goto exit;
416 }
417
Yang Ni7b2a46f2015-05-05 12:41:19 -0700418 if (numValues > RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700419 ALOGE("Too many arguments or globals in closure creation");
420 goto exit;
421 }
422
Yang Ni86c5c2d2016-03-25 15:49:07 -0700423 if (numValues > 0) {
424 fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
425 if (fieldIDs == nullptr) {
426 goto exit;
427 }
428 } else {
429 // numValues == 0
430 // alloca(0) implementation is platform-dependent.
431 fieldIDs = nullptr;
Yang Ni17c2d7a2015-04-30 16:13:54 -0700432 }
433
434 for (size_t i = 0; i < numValues; i++) {
435 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
436 }
437
Yang Ni86c5c2d2016-03-25 15:49:07 -0700438 if (numDependencies > 0) {
439 depClosures = (RsClosure*)alloca(sizeof(RsClosure) * numDependencies);
440 if (depClosures == nullptr) {
441 goto exit;
442 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700443
Yang Ni86c5c2d2016-03-25 15:49:07 -0700444 for (size_t i = 0; i < numDependencies; i++) {
445 depClosures[i] = (RsClosure)jDepClosures[i];
446 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700447
Yang Ni86c5c2d2016-03-25 15:49:07 -0700448 depFieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numDependencies);
449 if (depFieldIDs == nullptr) {
450 goto exit;
451 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700452
Yang Ni86c5c2d2016-03-25 15:49:07 -0700453 for (size_t i = 0; i < numDependencies; i++) {
454 depFieldIDs[i] = (RsClosure)jDepFieldIDs[i];
455 }
456 } else {
457 // alloca(0) implementation is platform-dependent.
458 depClosures = nullptr;
459 depFieldIDs = nullptr;
Yang Ni281c3252014-10-24 08:52:24 -0700460 }
461
Yang Ni17c2d7a2015-04-30 16:13:54 -0700462 ret = (jlong)(uintptr_t)rsClosureCreate(
Yang Ni281c3252014-10-24 08:52:24 -0700463 (RsContext)con, (RsScriptKernelID)kernelID, (RsAllocation)returnValue,
Yang Ni263cc902015-11-10 13:27:04 -0800464 fieldIDs, numValues, jValues, numValues,
Yang Ni17c2d7a2015-04-30 16:13:54 -0700465 (int*)jSizes, numValues,
466 depClosures, numDependencies,
467 depFieldIDs, numDependencies);
468
469exit:
470
471 _env->ReleaseLongArrayElements(depFieldIDArray, jDepFieldIDs, JNI_ABORT);
472 _env->ReleaseLongArrayElements(depClosureArray, jDepClosures, JNI_ABORT);
473 _env->ReleaseIntArrayElements (sizeArray, jSizes, JNI_ABORT);
474 _env->ReleaseLongArrayElements(valueArray, jValues, JNI_ABORT);
475 _env->ReleaseLongArrayElements(fieldIDArray, jFieldIDs, JNI_ABORT);
476
477 return ret;
Yang Ni281c3252014-10-24 08:52:24 -0700478}
479
Yang Nibe392ad2015-01-23 17:16:02 -0800480static jlong
481nInvokeClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong invokeID,
482 jbyteArray paramArray, jlongArray fieldIDArray, jlongArray valueArray,
483 jintArray sizeArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700484 jlong ret = 0;
485
Yang Nibe392ad2015-01-23 17:16:02 -0800486 jbyte* jParams = _env->GetByteArrayElements(paramArray, nullptr);
487 jsize jParamLength = _env->GetArrayLength(paramArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700488 if (jParams == nullptr) {
489 ALOGE("Failed to get Java array elements: params.");
490 return ret;
491 }
492
Yang Nibe392ad2015-01-23 17:16:02 -0800493 jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
494 jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700495 if (jFieldIDs == nullptr) {
496 ALOGE("Failed to get Java array elements: fieldIDs.");
497 return ret;
498 }
499
Yang Ni17c2d7a2015-04-30 16:13:54 -0700500 jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
501 jsize values_length = _env->GetArrayLength(valueArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700502 if (jValues == nullptr) {
503 ALOGE("Failed to get Java array elements: values.");
504 return ret;
505 }
506
Yang Ni17c2d7a2015-04-30 16:13:54 -0700507 jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
508 jsize sizes_length = _env->GetArrayLength(sizeArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700509 if (jSizes == nullptr) {
510 ALOGE("Failed to get Java array elements: sizes.");
511 return ret;
512 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700513
514 size_t numValues;
515 RsScriptFieldID* fieldIDs;
Yang Ni17c2d7a2015-04-30 16:13:54 -0700516
517 if (fieldIDs_length != values_length || values_length != sizes_length) {
518 ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
519 goto exit;
520 }
521
522 numValues = (size_t) fieldIDs_length;
523
Yang Ni7b2a46f2015-05-05 12:41:19 -0700524 if (numValues > RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700525 ALOGE("Too many arguments or globals in closure creation");
526 goto exit;
527 }
528
529 fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
530 if (fieldIDs == nullptr) {
531 goto exit;
532 }
533
534 for (size_t i = 0; i< numValues; i++) {
Yang Nibe392ad2015-01-23 17:16:02 -0800535 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
536 }
537
Yang Ni17c2d7a2015-04-30 16:13:54 -0700538 ret = (jlong)(uintptr_t)rsInvokeClosureCreate(
Yang Nibe392ad2015-01-23 17:16:02 -0800539 (RsContext)con, (RsScriptInvokeID)invokeID, jParams, jParamLength,
Yang Ni263cc902015-11-10 13:27:04 -0800540 fieldIDs, numValues, jValues, numValues,
Yang Ni17c2d7a2015-04-30 16:13:54 -0700541 (int*)jSizes, numValues);
542
543exit:
544
545 _env->ReleaseIntArrayElements (sizeArray, jSizes, JNI_ABORT);
546 _env->ReleaseLongArrayElements(valueArray, jValues, JNI_ABORT);
547 _env->ReleaseLongArrayElements(fieldIDArray, jFieldIDs, JNI_ABORT);
548 _env->ReleaseByteArrayElements(paramArray, jParams, JNI_ABORT);
549
550 return ret;
Yang Nibe392ad2015-01-23 17:16:02 -0800551}
552
Yang Ni281c3252014-10-24 08:52:24 -0700553static void
554nClosureSetArg(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
555 jint index, jlong value, jint size) {
Yang Ni263cc902015-11-10 13:27:04 -0800556 // Size is signed with -1 indicating the value is an Allocation
Yang Ni281c3252014-10-24 08:52:24 -0700557 rsClosureSetArg((RsContext)con, (RsClosure)closureID, (uint32_t)index,
Yang Ni263cc902015-11-10 13:27:04 -0800558 (uintptr_t)value, size);
Yang Ni281c3252014-10-24 08:52:24 -0700559}
560
561static void
562nClosureSetGlobal(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
563 jlong fieldID, jlong value, jint size) {
Yang Ni263cc902015-11-10 13:27:04 -0800564 // Size is signed with -1 indicating the value is an Allocation
Yang Ni281c3252014-10-24 08:52:24 -0700565 rsClosureSetGlobal((RsContext)con, (RsClosure)closureID,
Yang Ni263cc902015-11-10 13:27:04 -0800566 (RsScriptFieldID)fieldID, (int64_t)value, size);
Yang Ni281c3252014-10-24 08:52:24 -0700567}
568
569static long
Yang Ni35be56c2015-04-02 17:47:56 -0700570nScriptGroup2Create(JNIEnv *_env, jobject _this, jlong con, jstring name,
Yang Niebf63402015-01-16 11:06:26 -0800571 jstring cacheDir, jlongArray closureArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700572 jlong ret = 0;
573
Yang Ni35be56c2015-04-02 17:47:56 -0700574 AutoJavaStringToUTF8 nameUTF(_env, name);
Yang Niebf63402015-01-16 11:06:26 -0800575 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
576
Yang Ni281c3252014-10-24 08:52:24 -0700577 jlong* jClosures = _env->GetLongArrayElements(closureArray, nullptr);
578 jsize numClosures = _env->GetArrayLength(closureArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700579 if (jClosures == nullptr) {
580 ALOGE("Failed to get Java array elements: closures.");
581 return ret;
582 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700583
584 RsClosure* closures;
585
Yang Ni7b2a46f2015-05-05 12:41:19 -0700586 if (numClosures > (jsize) RS_SCRIPT_GROUP_MAX_NUMBER_CLOSURES) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700587 ALOGE("Too many closures in script group");
588 goto exit;
589 }
590
591 closures = (RsClosure*)alloca(sizeof(RsClosure) * numClosures);
592 if (closures == nullptr) {
593 goto exit;
594 }
595
Yang Ni281c3252014-10-24 08:52:24 -0700596 for (int i = 0; i < numClosures; i++) {
597 closures[i] = (RsClosure)jClosures[i];
598 }
599
Yang Ni17c2d7a2015-04-30 16:13:54 -0700600 ret = (jlong)(uintptr_t)rsScriptGroup2Create(
Yang Ni35be56c2015-04-02 17:47:56 -0700601 (RsContext)con, nameUTF.c_str(), nameUTF.length(),
602 cacheDirUTF.c_str(), cacheDirUTF.length(),
Yang Niebf63402015-01-16 11:06:26 -0800603 closures, numClosures);
Yang Ni17c2d7a2015-04-30 16:13:54 -0700604
605exit:
606
607 _env->ReleaseLongArrayElements(closureArray, jClosures, JNI_ABORT);
608
609 return ret;
Yang Ni281c3252014-10-24 08:52:24 -0700610}
611
612static void
613nScriptGroup2Execute(JNIEnv *_env, jobject _this, jlong con, jlong groupID) {
614 rsScriptGroupExecute((RsContext)con, (RsScriptGroup2)groupID);
615}
616
Jason Sams96ed4cf2010-06-15 12:15:57 -0700617static void
Tim Murray25207df2015-01-12 16:47:56 -0800618nScriptIntrinsicBLAS_Single(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
619 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
620 jfloat alpha, jlong A, jlong B, jfloat beta, jlong C, jint incX, jint incY,
621 jint KL, jint KU) {
622 RsBlasCall call;
623 memset(&call, 0, sizeof(call));
624 call.func = (RsBlasFunction)func;
625 call.transA = (RsBlasTranspose)TransA;
626 call.transB = (RsBlasTranspose)TransB;
627 call.side = (RsBlasSide)Side;
628 call.uplo = (RsBlasUplo)Uplo;
629 call.diag = (RsBlasDiag)Diag;
630 call.M = M;
631 call.N = N;
632 call.K = K;
633 call.alpha.f = alpha;
634 call.beta.f = beta;
635 call.incX = incX;
636 call.incY = incY;
637 call.KL = KL;
638 call.KU = KU;
639
640 RsAllocation in_allocs[3];
641 in_allocs[0] = (RsAllocation)A;
642 in_allocs[1] = (RsAllocation)B;
643 in_allocs[2] = (RsAllocation)C;
644
645 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wangb742fcc2016-10-06 10:45:42 -0700646 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800647 &call, sizeof(call), nullptr, 0);
648}
649
650static void
651nScriptIntrinsicBLAS_Double(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
652 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
653 jdouble alpha, jlong A, jlong B, jdouble beta, jlong C, jint incX, jint incY,
654 jint KL, jint KU) {
655 RsBlasCall call;
656 memset(&call, 0, sizeof(call));
657 call.func = (RsBlasFunction)func;
658 call.transA = (RsBlasTranspose)TransA;
659 call.transB = (RsBlasTranspose)TransB;
660 call.side = (RsBlasSide)Side;
661 call.uplo = (RsBlasUplo)Uplo;
662 call.diag = (RsBlasDiag)Diag;
663 call.M = M;
664 call.N = N;
665 call.K = K;
666 call.alpha.d = alpha;
667 call.beta.d = beta;
668 call.incX = incX;
669 call.incY = incY;
670 call.KL = KL;
671 call.KU = KU;
672
673 RsAllocation in_allocs[3];
674 in_allocs[0] = (RsAllocation)A;
675 in_allocs[1] = (RsAllocation)B;
676 in_allocs[2] = (RsAllocation)C;
677
678 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700679 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800680 &call, sizeof(call), nullptr, 0);
681}
682
683static void
684nScriptIntrinsicBLAS_Complex(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
685 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
686 jfloat alphaX, jfloat alphaY, jlong A, jlong B, jfloat betaX,
687 jfloat betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
688 RsBlasCall call;
689 memset(&call, 0, sizeof(call));
690 call.func = (RsBlasFunction)func;
691 call.transA = (RsBlasTranspose)TransA;
692 call.transB = (RsBlasTranspose)TransB;
693 call.side = (RsBlasSide)Side;
694 call.uplo = (RsBlasUplo)Uplo;
695 call.diag = (RsBlasDiag)Diag;
696 call.M = M;
697 call.N = N;
698 call.K = K;
699 call.alpha.c.r = alphaX;
700 call.alpha.c.i = alphaY;
701 call.beta.c.r = betaX;
Miao Wang82585b32015-04-30 13:44:49 -0700702 call.beta.c.i = betaY;
Tim Murray25207df2015-01-12 16:47:56 -0800703 call.incX = incX;
704 call.incY = incY;
705 call.KL = KL;
706 call.KU = KU;
707
708 RsAllocation in_allocs[3];
709 in_allocs[0] = (RsAllocation)A;
710 in_allocs[1] = (RsAllocation)B;
711 in_allocs[2] = (RsAllocation)C;
712
713 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700714 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800715 &call, sizeof(call), nullptr, 0);
716}
717
718static void
719nScriptIntrinsicBLAS_Z(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
720 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
721 jdouble alphaX, jdouble alphaY, jlong A, jlong B, jdouble betaX,
722 jdouble betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
723 RsBlasCall call;
724 memset(&call, 0, sizeof(call));
725 call.func = (RsBlasFunction)func;
726 call.transA = (RsBlasTranspose)TransA;
727 call.transB = (RsBlasTranspose)TransB;
728 call.side = (RsBlasSide)Side;
729 call.uplo = (RsBlasUplo)Uplo;
730 call.diag = (RsBlasDiag)Diag;
731 call.M = M;
732 call.N = N;
733 call.K = K;
734 call.alpha.z.r = alphaX;
735 call.alpha.z.i = alphaY;
736 call.beta.z.r = betaX;
Miao Wang82585b32015-04-30 13:44:49 -0700737 call.beta.z.i = betaY;
Tim Murray25207df2015-01-12 16:47:56 -0800738 call.incX = incX;
739 call.incY = incY;
740 call.KL = KL;
741 call.KU = KU;
742
743 RsAllocation in_allocs[3];
744 in_allocs[0] = (RsAllocation)A;
745 in_allocs[1] = (RsAllocation)B;
746 in_allocs[2] = (RsAllocation)C;
747
748 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700749 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800750 &call, sizeof(call), nullptr, 0);
751}
752
753
754static void
Tim Murray9cb16a22015-04-01 11:07:16 -0700755nScriptIntrinsicBLAS_BNNM(JNIEnv *_env, jobject _this, jlong con, jlong id, jint M, jint N, jint K,
756 jlong A, jint a_offset, jlong B, jint b_offset, jlong C, jint c_offset,
757 jint c_mult_int) {
758 RsBlasCall call;
759 memset(&call, 0, sizeof(call));
760 call.func = RsBlas_bnnm;
761 call.M = M;
762 call.N = N;
763 call.K = K;
Miao Wang25148062015-06-29 17:43:03 -0700764 call.a_offset = a_offset & 0xFF;
765 call.b_offset = b_offset & 0xFF;
Tim Murray9cb16a22015-04-01 11:07:16 -0700766 call.c_offset = c_offset;
767 call.c_mult_int = c_mult_int;
768
769 RsAllocation in_allocs[3];
770 in_allocs[0] = (RsAllocation)A;
771 in_allocs[1] = (RsAllocation)B;
772 in_allocs[2] = (RsAllocation)C;
773
774 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700775 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray9cb16a22015-04-01 11:07:16 -0700776 &call, sizeof(call), nullptr, 0);
777}
778
779
780static void
Tim Murray460a0492013-11-19 12:45:54 -0800781nAssignName(JNIEnv *_env, jobject _this, jlong con, jlong obj, jbyteArray str)
Jason Sams3eaa338e2009-06-10 15:04:38 -0700782{
Andreas Gampe67333922014-11-10 20:35:59 -0800783 if (kLogApi) {
784 ALOGD("nAssignName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
785 }
Jason Sams3eaa338e2009-06-10 15:04:38 -0700786 jint len = _env->GetArrayLength(str);
787 jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
Miao Wangba8766c2015-10-12 17:24:13 -0700788 if (cptr == nullptr) {
789 ALOGE("Failed to get Java array elements");
790 return;
791 }
792
Tim Murrayeff663f2013-11-15 13:08:30 -0800793 rsAssignName((RsContext)con, (void *)obj, (const char *)cptr, len);
Jason Sams3eaa338e2009-06-10 15:04:38 -0700794 _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
795}
796
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700797static jstring
Tim Murray460a0492013-11-19 12:45:54 -0800798nGetName(JNIEnv *_env, jobject _this, jlong con, jlong obj)
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700799{
Andreas Gampe67333922014-11-10 20:35:59 -0800800 if (kLogApi) {
801 ALOGD("nGetName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
802 }
Chris Wailes488230c32014-08-14 11:22:40 -0700803 const char *name = nullptr;
Tim Murrayeff663f2013-11-15 13:08:30 -0800804 rsaGetName((RsContext)con, (void *)obj, &name);
Chris Wailes488230c32014-08-14 11:22:40 -0700805 if(name == nullptr || strlen(name) == 0) {
806 return nullptr;
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700807 }
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700808 return _env->NewStringUTF(name);
809}
810
Jason Sams7ce033d2009-08-18 14:14:24 -0700811static void
Tim Murray460a0492013-11-19 12:45:54 -0800812nObjDestroy(JNIEnv *_env, jobject _this, jlong con, jlong obj)
Jason Sams7ce033d2009-08-18 14:14:24 -0700813{
Andreas Gampe67333922014-11-10 20:35:59 -0800814 if (kLogApi) {
815 ALOGD("nObjDestroy, con(%p) obj(%p)", (RsContext)con, (void *)obj);
816 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800817 rsObjDestroy((RsContext)con, (void *)obj);
Jason Sams7ce033d2009-08-18 14:14:24 -0700818}
819
Jason Sams3eaa338e2009-06-10 15:04:38 -0700820// ---------------------------------------------------------------------------
821
Tim Murrayeff663f2013-11-15 13:08:30 -0800822static jlong
Jason Samsd19f10d2009-05-22 14:03:28 -0700823nDeviceCreate(JNIEnv *_env, jobject _this)
824{
Andreas Gampe67333922014-11-10 20:35:59 -0800825 if (kLogApi) {
826 ALOGD("nDeviceCreate");
827 }
Tim Murray3aa89c12014-08-18 17:51:22 -0700828 return (jlong)(uintptr_t)rsDeviceCreate();
Jason Samsd19f10d2009-05-22 14:03:28 -0700829}
830
831static void
Tim Murray5eaf4682014-01-10 11:25:52 -0800832nDeviceDestroy(JNIEnv *_env, jobject _this, jlong dev)
Jason Samsd19f10d2009-05-22 14:03:28 -0700833{
Andreas Gampe67333922014-11-10 20:35:59 -0800834 if (kLogApi) {
835 ALOGD("nDeviceDestroy");
836 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700837 return rsDeviceDestroy((RsDevice)dev);
838}
839
Jason Samsebfb4362009-09-23 13:57:02 -0700840static void
Tim Murray5eaf4682014-01-10 11:25:52 -0800841nDeviceSetConfig(JNIEnv *_env, jobject _this, jlong dev, jint p, jint value)
Jason Samsebfb4362009-09-23 13:57:02 -0700842{
Andreas Gampe67333922014-11-10 20:35:59 -0800843 if (kLogApi) {
844 ALOGD("nDeviceSetConfig dev(%p), param(%i), value(%i)", (void *)dev, p, value);
845 }
Jason Samsebfb4362009-09-23 13:57:02 -0700846 return rsDeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value);
847}
848
Tim Murrayeff663f2013-11-15 13:08:30 -0800849static jlong
Jason Sams81cd2b12014-12-02 12:36:43 -0800850nContextCreate(JNIEnv *_env, jobject _this, jlong dev, jint flags, jint sdkVer, jint contextType)
Jason Samsd19f10d2009-05-22 14:03:28 -0700851{
Andreas Gampe67333922014-11-10 20:35:59 -0800852 if (kLogApi) {
853 ALOGD("nContextCreate");
854 }
Jason Sams81cd2b12014-12-02 12:36:43 -0800855 return (jlong)(uintptr_t)rsContextCreate((RsDevice)dev, 0, sdkVer, (RsContextType)contextType, flags);
Jason Sams704ff642010-02-09 16:05:07 -0800856}
857
Tim Murrayeff663f2013-11-15 13:08:30 -0800858static jlong
Tim Murray5eaf4682014-01-10 11:25:52 -0800859nContextCreateGL(JNIEnv *_env, jobject _this, jlong dev, jint ver, jint sdkVer,
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000860 jint colorMin, jint colorPref,
861 jint alphaMin, jint alphaPref,
862 jint depthMin, jint depthPref,
863 jint stencilMin, jint stencilPref,
864 jint samplesMin, jint samplesPref, jfloat samplesQ,
865 jint dpi)
Jason Sams704ff642010-02-09 16:05:07 -0800866{
Yang Ni86c5c2d2016-03-25 15:49:07 -0700867 RsSurfaceConfig sc = {};
Jason Sams11c8af92010-10-13 15:31:10 -0700868 sc.alphaMin = alphaMin;
869 sc.alphaPref = alphaPref;
870 sc.colorMin = colorMin;
871 sc.colorPref = colorPref;
872 sc.depthMin = depthMin;
873 sc.depthPref = depthPref;
874 sc.samplesMin = samplesMin;
875 sc.samplesPref = samplesPref;
876 sc.samplesQ = samplesQ;
877
Andreas Gampe67333922014-11-10 20:35:59 -0800878 if (kLogApi) {
879 ALOGD("nContextCreateGL");
880 }
Tim Murray3aa89c12014-08-18 17:51:22 -0700881 return (jlong)(uintptr_t)rsContextCreateGL((RsDevice)dev, ver, sdkVer, sc, dpi);
Jason Samsd19f10d2009-05-22 14:03:28 -0700882}
883
884static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800885nContextSetPriority(JNIEnv *_env, jobject _this, jlong con, jint p)
Jason Sams7d787b42009-11-15 12:14:26 -0800886{
Andreas Gampe67333922014-11-10 20:35:59 -0800887 if (kLogApi) {
888 ALOGD("ContextSetPriority, con(%p), priority(%i)", (RsContext)con, p);
889 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800890 rsContextSetPriority((RsContext)con, p);
Jason Sams7d787b42009-11-15 12:14:26 -0800891}
892
Tim Murray47f31582015-04-07 15:43:24 -0700893static void
894nContextSetCacheDir(JNIEnv *_env, jobject _this, jlong con, jstring cacheDir)
895{
896 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
897
898 if (kLogApi) {
899 ALOGD("ContextSetCacheDir, con(%p), cacheDir(%s)", (RsContext)con, cacheDirUTF.c_str());
900 }
901 rsContextSetCacheDir((RsContext)con, cacheDirUTF.c_str(), cacheDirUTF.length());
902}
903
Jason Sams7d787b42009-11-15 12:14:26 -0800904
905
906static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800907nContextSetSurface(JNIEnv *_env, jobject _this, jlong con, jint width, jint height, jobject wnd)
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800908{
Andreas Gampe67333922014-11-10 20:35:59 -0800909 if (kLogApi) {
910 ALOGD("nContextSetSurface, con(%p), width(%i), height(%i), surface(%p)", (RsContext)con,
911 width, height, (Surface *)wnd);
912 }
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800913
Chris Wailes488230c32014-08-14 11:22:40 -0700914 ANativeWindow * window = nullptr;
915 if (wnd == nullptr) {
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800916
917 } else {
Jeff Brown64a55af2012-08-26 02:47:39 -0700918 window = android_view_Surface_getNativeWindow(_env, wnd).get();
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800919 }
920
Tim Murrayeff663f2013-11-15 13:08:30 -0800921 rsContextSetSurface((RsContext)con, width, height, window);
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800922}
923
924static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800925nContextDestroy(JNIEnv *_env, jobject _this, jlong con)
Jason Samsd19f10d2009-05-22 14:03:28 -0700926{
Andreas Gampe67333922014-11-10 20:35:59 -0800927 if (kLogApi) {
928 ALOGD("nContextDestroy, con(%p)", (RsContext)con);
929 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800930 rsContextDestroy((RsContext)con);
Jason Samsd19f10d2009-05-22 14:03:28 -0700931}
932
Jason Sams715333b2009-11-17 17:26:46 -0800933static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800934nContextDump(JNIEnv *_env, jobject _this, jlong con, jint bits)
Jason Sams715333b2009-11-17 17:26:46 -0800935{
Andreas Gampe67333922014-11-10 20:35:59 -0800936 if (kLogApi) {
937 ALOGD("nContextDump, con(%p) bits(%i)", (RsContext)con, bits);
938 }
Jason Sams715333b2009-11-17 17:26:46 -0800939 rsContextDump((RsContext)con, bits);
940}
Jason Samsd19f10d2009-05-22 14:03:28 -0700941
942static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800943nContextPause(JNIEnv *_env, jobject _this, jlong con)
Jason Sams65e7aa52009-09-24 17:38:20 -0700944{
Andreas Gampe67333922014-11-10 20:35:59 -0800945 if (kLogApi) {
946 ALOGD("nContextPause, con(%p)", (RsContext)con);
947 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800948 rsContextPause((RsContext)con);
Jason Sams65e7aa52009-09-24 17:38:20 -0700949}
950
951static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800952nContextResume(JNIEnv *_env, jobject _this, jlong con)
Jason Sams65e7aa52009-09-24 17:38:20 -0700953{
Andreas Gampe67333922014-11-10 20:35:59 -0800954 if (kLogApi) {
955 ALOGD("nContextResume, con(%p)", (RsContext)con);
956 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800957 rsContextResume((RsContext)con);
Jason Sams65e7aa52009-09-24 17:38:20 -0700958}
959
Jason Sams1c415172010-11-08 17:06:46 -0800960
961static jstring
Tim Murrayeff663f2013-11-15 13:08:30 -0800962nContextGetErrorMessage(JNIEnv *_env, jobject _this, jlong con)
Jason Sams1c415172010-11-08 17:06:46 -0800963{
Andreas Gampe67333922014-11-10 20:35:59 -0800964 if (kLogApi) {
965 ALOGD("nContextGetErrorMessage, con(%p)", (RsContext)con);
966 }
Jason Sams1c415172010-11-08 17:06:46 -0800967 char buf[1024];
968
969 size_t receiveLen;
970 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800971 int id = rsContextGetMessage((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -0700972 buf, sizeof(buf),
973 &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -0700974 &subID, sizeof(subID));
Jason Sams1c415172010-11-08 17:06:46 -0800975 if (!id && receiveLen) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +0100976 ALOGV("message receive buffer too small. %zu", receiveLen);
Jason Sams1c415172010-11-08 17:06:46 -0800977 }
978 return _env->NewStringUTF(buf);
979}
980
Jason Samsedbfabd2011-05-17 15:01:29 -0700981static jint
Tim Murrayeff663f2013-11-15 13:08:30 -0800982nContextGetUserMessage(JNIEnv *_env, jobject _this, jlong con, jintArray data)
Jason Sams516c3192009-10-06 13:58:47 -0700983{
Jason Sams516c3192009-10-06 13:58:47 -0700984 jint len = _env->GetArrayLength(data);
Andreas Gampe67333922014-11-10 20:35:59 -0800985 if (kLogApi) {
986 ALOGD("nContextGetMessage, con(%p), len(%i)", (RsContext)con, len);
987 }
Chris Wailes488230c32014-08-14 11:22:40 -0700988 jint *ptr = _env->GetIntArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -0700989 if (ptr == nullptr) {
990 ALOGE("Failed to get Java array elements");
991 return 0;
992 }
Jason Sams516c3192009-10-06 13:58:47 -0700993 size_t receiveLen;
Jason Sams1c415172010-11-08 17:06:46 -0800994 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800995 int id = rsContextGetMessage((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -0700996 ptr, len * 4,
997 &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -0700998 &subID, sizeof(subID));
Jason Sams516c3192009-10-06 13:58:47 -0700999 if (!id && receiveLen) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001000 ALOGV("message receive buffer too small. %zu", receiveLen);
Jason Sams516c3192009-10-06 13:58:47 -07001001 }
1002 _env->ReleaseIntArrayElements(data, ptr, 0);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001003 return (jint)id;
Jason Sams1c415172010-11-08 17:06:46 -08001004}
1005
1006static jint
Tim Murrayeff663f2013-11-15 13:08:30 -08001007nContextPeekMessage(JNIEnv *_env, jobject _this, jlong con, jintArray auxData)
Jason Sams1c415172010-11-08 17:06:46 -08001008{
Andreas Gampe67333922014-11-10 20:35:59 -08001009 if (kLogApi) {
1010 ALOGD("nContextPeekMessage, con(%p)", (RsContext)con);
1011 }
Chris Wailes488230c32014-08-14 11:22:40 -07001012 jint *auxDataPtr = _env->GetIntArrayElements(auxData, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001013 if (auxDataPtr == nullptr) {
1014 ALOGE("Failed to get Java array elements");
1015 return 0;
1016 }
Jason Sams1c415172010-11-08 17:06:46 -08001017 size_t receiveLen;
1018 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -08001019 int id = rsContextPeekMessage((RsContext)con, &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -07001020 &subID, sizeof(subID));
Jason Sams1c415172010-11-08 17:06:46 -08001021 auxDataPtr[0] = (jint)subID;
1022 auxDataPtr[1] = (jint)receiveLen;
1023 _env->ReleaseIntArrayElements(auxData, auxDataPtr, 0);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001024 return (jint)id;
Jason Sams516c3192009-10-06 13:58:47 -07001025}
1026
Tim Murrayeff663f2013-11-15 13:08:30 -08001027static void nContextInitToClient(JNIEnv *_env, jobject _this, jlong con)
Jason Sams516c3192009-10-06 13:58:47 -07001028{
Andreas Gampe67333922014-11-10 20:35:59 -08001029 if (kLogApi) {
1030 ALOGD("nContextInitToClient, con(%p)", (RsContext)con);
1031 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001032 rsContextInitToClient((RsContext)con);
Jason Sams516c3192009-10-06 13:58:47 -07001033}
1034
Tim Murrayeff663f2013-11-15 13:08:30 -08001035static void nContextDeinitToClient(JNIEnv *_env, jobject _this, jlong con)
Jason Sams516c3192009-10-06 13:58:47 -07001036{
Andreas Gampe67333922014-11-10 20:35:59 -08001037 if (kLogApi) {
1038 ALOGD("nContextDeinitToClient, con(%p)", (RsContext)con);
1039 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001040 rsContextDeinitToClient((RsContext)con);
Jason Sams516c3192009-10-06 13:58:47 -07001041}
1042
Jason Sams455d6442013-02-05 19:20:18 -08001043static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001044nContextSendMessage(JNIEnv *_env, jobject _this, jlong con, jint id, jintArray data)
Jason Sams455d6442013-02-05 19:20:18 -08001045{
Chris Wailes488230c32014-08-14 11:22:40 -07001046 jint *ptr = nullptr;
Jason Sams455d6442013-02-05 19:20:18 -08001047 jint len = 0;
1048 if (data) {
1049 len = _env->GetArrayLength(data);
Stephen Hines4a043c12014-08-21 23:20:32 -07001050 ptr = _env->GetIntArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001051 if (ptr == nullptr) {
1052 ALOGE("Failed to get Java array elements");
1053 return;
1054 }
Jason Sams455d6442013-02-05 19:20:18 -08001055 }
Andreas Gampe67333922014-11-10 20:35:59 -08001056 if (kLogApi) {
1057 ALOGD("nContextSendMessage, con(%p), id(%i), len(%i)", (RsContext)con, id, len);
1058 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001059 rsContextSendMessage((RsContext)con, id, (const uint8_t *)ptr, len * sizeof(int));
Jason Sams455d6442013-02-05 19:20:18 -08001060 if (data) {
1061 _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
1062 }
1063}
1064
1065
Jason Sams516c3192009-10-06 13:58:47 -07001066
Tim Murray460a0492013-11-19 12:45:54 -08001067static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001068nElementCreate(JNIEnv *_env, jobject _this, jlong con, jlong type, jint kind, jboolean norm,
1069 jint size)
Jason Samsd19f10d2009-05-22 14:03:28 -07001070{
Andreas Gampe67333922014-11-10 20:35:59 -08001071 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001072 ALOGD("nElementCreate, con(%p), type(%" PRId64 "), kind(%i), norm(%i), size(%i)", (RsContext)con,
Andreas Gampe67333922014-11-10 20:35:59 -08001073 type, kind, norm, size);
1074 }
1075 return (jlong)(uintptr_t)rsElementCreate((RsContext)con, (RsDataType)type, (RsDataKind)kind,
Yang Ni8c8daea2016-03-08 21:01:54 +00001076 norm, size);
Jason Samsd19f10d2009-05-22 14:03:28 -07001077}
1078
Tim Murray460a0492013-11-19 12:45:54 -08001079static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001080nElementCreate2(JNIEnv *_env, jobject _this, jlong con,
Ashok Bhat98071552014-02-12 09:54:43 +00001081 jlongArray _ids, jobjectArray _names, jintArray _arraySizes)
Jason Samsd19f10d2009-05-22 14:03:28 -07001082{
Jason Sams718cd1f2009-12-23 14:35:29 -08001083 int fieldCount = _env->GetArrayLength(_ids);
Andreas Gampe67333922014-11-10 20:35:59 -08001084 if (kLogApi) {
1085 ALOGD("nElementCreate2, con(%p)", (RsContext)con);
1086 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001087
Chris Wailes488230c32014-08-14 11:22:40 -07001088 jlong *jIds = _env->GetLongArrayElements(_ids, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001089 if (jIds == nullptr) {
1090 ALOGE("Failed to get Java array elements: ids");
1091 return 0;
1092 }
Chris Wailes488230c32014-08-14 11:22:40 -07001093 jint *jArraySizes = _env->GetIntArrayElements(_arraySizes, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001094 if (jArraySizes == nullptr) {
1095 ALOGE("Failed to get Java array elements: arraySizes");
1096 return 0;
1097 }
Ashok Bhat98071552014-02-12 09:54:43 +00001098
1099 RsElement *ids = (RsElement*)malloc(fieldCount * sizeof(RsElement));
1100 uint32_t *arraySizes = (uint32_t *)malloc(fieldCount * sizeof(uint32_t));
1101
1102 for(int i = 0; i < fieldCount; i ++) {
1103 ids[i] = (RsElement)jIds[i];
1104 arraySizes[i] = (uint32_t)jArraySizes[i];
1105 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001106
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08001107 AutoJavaStringArrayToUTF8 names(_env, _names, fieldCount);
1108
1109 const char **nameArray = names.c_str();
1110 size_t *sizeArray = names.c_str_len();
1111
Tim Murray3aa89c12014-08-18 17:51:22 -07001112 jlong id = (jlong)(uintptr_t)rsElementCreate2((RsContext)con,
Ashok Bhat98071552014-02-12 09:54:43 +00001113 (const RsElement *)ids, fieldCount,
Jason Sams7a22e102011-05-06 14:14:30 -07001114 nameArray, fieldCount * sizeof(size_t), sizeArray,
Yang Ni8c8daea2016-03-08 21:01:54 +00001115 (const uint32_t *)arraySizes, fieldCount);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08001116
Ashok Bhat98071552014-02-12 09:54:43 +00001117 free(ids);
1118 free(arraySizes);
1119 _env->ReleaseLongArrayElements(_ids, jIds, JNI_ABORT);
1120 _env->ReleaseIntArrayElements(_arraySizes, jArraySizes, JNI_ABORT);
1121
Tim Murray3aa89c12014-08-18 17:51:22 -07001122 return (jlong)(uintptr_t)id;
Jason Samsd19f10d2009-05-22 14:03:28 -07001123}
1124
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001125static void
Tim Murray460a0492013-11-19 12:45:54 -08001126nElementGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jintArray _elementData)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001127{
1128 int dataSize = _env->GetArrayLength(_elementData);
Andreas Gampe67333922014-11-10 20:35:59 -08001129 if (kLogApi) {
1130 ALOGD("nElementGetNativeData, con(%p)", (RsContext)con);
1131 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001132
1133 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
1134 assert(dataSize == 5);
1135
Miao Wangcbb02062017-01-24 18:58:17 -08001136 uint32_t elementData[5];
Tim Murrayeff663f2013-11-15 13:08:30 -08001137 rsaElementGetNativeData((RsContext)con, (RsElement)id, elementData, dataSize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001138
1139 for(jint i = 0; i < dataSize; i ++) {
Ashok Bhat98071552014-02-12 09:54:43 +00001140 const jint data = (jint)elementData[i];
1141 _env->SetIntArrayRegion(_elementData, i, 1, &data);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001142 }
1143}
1144
1145
1146static void
Tim Murray460a0492013-11-19 12:45:54 -08001147nElementGetSubElements(JNIEnv *_env, jobject _this, jlong con, jlong id,
Ashok Bhat98071552014-02-12 09:54:43 +00001148 jlongArray _IDs,
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -07001149 jobjectArray _names,
1150 jintArray _arraySizes)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001151{
Ashok Bhat98071552014-02-12 09:54:43 +00001152 uint32_t dataSize = _env->GetArrayLength(_IDs);
Andreas Gampe67333922014-11-10 20:35:59 -08001153 if (kLogApi) {
1154 ALOGD("nElementGetSubElements, con(%p)", (RsContext)con);
1155 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001156
Ashok Bhat98071552014-02-12 09:54:43 +00001157 uintptr_t *ids = (uintptr_t*)malloc(dataSize * sizeof(uintptr_t));
1158 const char **names = (const char **)malloc(dataSize * sizeof(const char *));
Miao Wangcbb02062017-01-24 18:58:17 -08001159 size_t *arraySizes = (size_t *)malloc(dataSize * sizeof(size_t));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001160
Andreas Gampe67333922014-11-10 20:35:59 -08001161 rsaElementGetSubElements((RsContext)con, (RsElement)id, ids, names, arraySizes,
1162 (uint32_t)dataSize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001163
Ashok Bhat98071552014-02-12 09:54:43 +00001164 for(uint32_t i = 0; i < dataSize; i++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07001165 const jlong id = (jlong)(uintptr_t)ids[i];
Ashok Bhat98071552014-02-12 09:54:43 +00001166 const jint arraySize = (jint)arraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001167 _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
Ashok Bhat98071552014-02-12 09:54:43 +00001168 _env->SetLongArrayRegion(_IDs, i, 1, &id);
1169 _env->SetIntArrayRegion(_arraySizes, i, 1, &arraySize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001170 }
1171
1172 free(ids);
1173 free(names);
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -07001174 free(arraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001175}
1176
Jason Samsd19f10d2009-05-22 14:03:28 -07001177// -----------------------------------
1178
Tim Murray460a0492013-11-19 12:45:54 -08001179static jlong
1180nTypeCreate(JNIEnv *_env, jobject _this, jlong con, jlong eid,
Jason Samsb109cc72013-01-07 18:20:12 -08001181 jint dimx, jint dimy, jint dimz, jboolean mips, jboolean faces, jint yuv)
Jason Samsd19f10d2009-05-22 14:03:28 -07001182{
Andreas Gampe67333922014-11-10 20:35:59 -08001183 if (kLogApi) {
1184 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 +01001185 (RsContext)con, (void*)eid, dimx, dimy, dimz, mips, faces, yuv);
Andreas Gampe67333922014-11-10 20:35:59 -08001186 }
Jason Sams3b9c52a2010-10-14 17:48:46 -07001187
Andreas Gampe67333922014-11-10 20:35:59 -08001188 return (jlong)(uintptr_t)rsTypeCreate((RsContext)con, (RsElement)eid, dimx, dimy, dimz, mips,
Yang Ni8c8daea2016-03-08 21:01:54 +00001189 faces, yuv);
Jason Samsd19f10d2009-05-22 14:03:28 -07001190}
1191
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001192static void
Ashok Bhat98071552014-02-12 09:54:43 +00001193nTypeGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jlongArray _typeData)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001194{
1195 // We are packing 6 items: mDimX; mDimY; mDimZ;
1196 // mDimLOD; mDimFaces; mElement; into typeData
1197 int elementCount = _env->GetArrayLength(_typeData);
1198
1199 assert(elementCount == 6);
Andreas Gampe67333922014-11-10 20:35:59 -08001200 if (kLogApi) {
1201 ALOGD("nTypeGetNativeData, con(%p)", (RsContext)con);
1202 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001203
Ashok Bhat98071552014-02-12 09:54:43 +00001204 uintptr_t typeData[6];
Tim Murrayeff663f2013-11-15 13:08:30 -08001205 rsaTypeGetNativeData((RsContext)con, (RsType)id, typeData, 6);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001206
1207 for(jint i = 0; i < elementCount; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07001208 const jlong data = (jlong)(uintptr_t)typeData[i];
Ashok Bhat98071552014-02-12 09:54:43 +00001209 _env->SetLongArrayRegion(_typeData, i, 1, &data);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001210 }
1211}
1212
Jason Samsd19f10d2009-05-22 14:03:28 -07001213// -----------------------------------
1214
Tim Murray460a0492013-11-19 12:45:54 -08001215static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001216nAllocationCreateTyped(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mips, jint usage,
1217 jlong pointer)
Jason Samsd19f10d2009-05-22 14:03:28 -07001218{
Andreas Gampe67333922014-11-10 20:35:59 -08001219 if (kLogApi) {
1220 ALOGD("nAllocationCreateTyped, con(%p), type(%p), mip(%i), usage(%i), ptr(%p)",
1221 (RsContext)con, (RsElement)type, mips, usage, (void *)pointer);
1222 }
1223 return (jlong)(uintptr_t) rsAllocationCreateTyped((RsContext)con, (RsType)type,
1224 (RsAllocationMipmapControl)mips,
Yang Ni8c8daea2016-03-08 21:01:54 +00001225 (uint32_t)usage, (uintptr_t)pointer);
Jason Samsd19f10d2009-05-22 14:03:28 -07001226}
1227
Jason Samsd19f10d2009-05-22 14:03:28 -07001228static void
Tim Murray460a0492013-11-19 12:45:54 -08001229nAllocationSyncAll(JNIEnv *_env, jobject _this, jlong con, jlong a, jint bits)
Jason Sams5476b452010-12-08 16:14:36 -08001230{
Andreas Gampe67333922014-11-10 20:35:59 -08001231 if (kLogApi) {
1232 ALOGD("nAllocationSyncAll, con(%p), a(%p), bits(0x%08x)", (RsContext)con, (RsAllocation)a,
1233 bits);
1234 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001235 rsAllocationSyncAll((RsContext)con, (RsAllocation)a, (RsAllocationUsageType)bits);
Jason Sams5476b452010-12-08 16:14:36 -08001236}
1237
Miao Wang8c150922015-10-26 17:44:10 -07001238static void
1239nAllocationSetupBufferQueue(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint numAlloc)
1240{
1241 if (kLogApi) {
1242 ALOGD("nAllocationSetupBufferQueue, con(%p), alloc(%p), numAlloc(%d)", (RsContext)con,
1243 (RsAllocation)alloc, numAlloc);
1244 }
1245 rsAllocationSetupBufferQueue((RsContext)con, (RsAllocation)alloc, (uint32_t)numAlloc);
1246}
1247
1248static void
1249nAllocationShareBufferQueue(JNIEnv *_env, jobject _this, jlong con, jlong alloc1, jlong alloc2)
1250{
1251 if (kLogApi) {
1252 ALOGD("nAllocationShareBufferQueue, con(%p), alloc1(%p), alloc2(%p)", (RsContext)con,
1253 (RsAllocation)alloc1, (RsAllocation)alloc2);
1254 }
1255
1256 rsAllocationShareBufferQueue((RsContext)con, (RsAllocation)alloc1, (RsAllocation)alloc2);
1257}
1258
Jason Sams72226e02013-02-22 12:45:54 -08001259static jobject
Tim Murray460a0492013-11-19 12:45:54 -08001260nAllocationGetSurface(JNIEnv *_env, jobject _this, jlong con, jlong a)
Jason Sams615e7ce2012-01-13 14:01:20 -08001261{
Andreas Gampe67333922014-11-10 20:35:59 -08001262 if (kLogApi) {
1263 ALOGD("nAllocationGetSurface, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1264 }
Jason Sams615e7ce2012-01-13 14:01:20 -08001265
Miao Wang1e95fc82017-03-04 16:28:56 -08001266 ANativeWindow *anw = (ANativeWindow *)rsAllocationGetSurface((RsContext)con, (RsAllocation)a);
1267
1268 sp<Surface> surface(static_cast<Surface*>(anw));
1269 sp<IGraphicBufferProducer> bp = surface->getIGraphicBufferProducer();
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001270
Jason Sams72226e02013-02-22 12:45:54 -08001271 jobject o = android_view_Surface_createFromIGraphicBufferProducer(_env, bp);
1272 return o;
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001273}
1274
1275static void
Tim Murray460a0492013-11-19 12:45:54 -08001276nAllocationSetSurface(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject sur)
Jason Sams163766c2012-02-15 12:04:24 -08001277{
Andreas Gampe67333922014-11-10 20:35:59 -08001278 if (kLogApi) {
1279 ALOGD("nAllocationSetSurface, con(%p), alloc(%p), surface(%p)", (RsContext)con,
1280 (RsAllocation)alloc, (Surface *)sur);
1281 }
Jason Sams163766c2012-02-15 12:04:24 -08001282
Miao Wang33287e82017-03-06 09:31:32 -08001283 ANativeWindow *anw = nullptr;
Jason Sams163766c2012-02-15 12:04:24 -08001284 if (sur != 0) {
Miao Wang33287e82017-03-06 09:31:32 -08001285 anw = ANativeWindow_fromSurface(_env, sur);
Jason Sams163766c2012-02-15 12:04:24 -08001286 }
1287
Miao Wang33287e82017-03-06 09:31:32 -08001288 rsAllocationSetSurface((RsContext)con, (RsAllocation)alloc, anw);
Jason Sams163766c2012-02-15 12:04:24 -08001289}
1290
1291static void
Tim Murray460a0492013-11-19 12:45:54 -08001292nAllocationIoSend(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Sams163766c2012-02-15 12:04:24 -08001293{
Andreas Gampe67333922014-11-10 20:35:59 -08001294 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001295 ALOGD("nAllocationIoSend, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
Andreas Gampe67333922014-11-10 20:35:59 -08001296 }
Tim Murray460a0492013-11-19 12:45:54 -08001297 rsAllocationIoSend((RsContext)con, (RsAllocation)alloc);
Jason Sams163766c2012-02-15 12:04:24 -08001298}
1299
Miao Wang8c150922015-10-26 17:44:10 -07001300static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001301nAllocationIoReceive(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Sams163766c2012-02-15 12:04:24 -08001302{
Andreas Gampe67333922014-11-10 20:35:59 -08001303 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001304 ALOGD("nAllocationIoReceive, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
Andreas Gampe67333922014-11-10 20:35:59 -08001305 }
Miao Wang8c150922015-10-26 17:44:10 -07001306 return (jlong) rsAllocationIoReceive((RsContext)con, (RsAllocation)alloc);
Jason Sams163766c2012-02-15 12:04:24 -08001307}
1308
Jason Sams163766c2012-02-15 12:04:24 -08001309static void
Tim Murray460a0492013-11-19 12:45:54 -08001310nAllocationGenerateMipmaps(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Samsf7086092011-01-12 13:28:37 -08001311{
Andreas Gampe67333922014-11-10 20:35:59 -08001312 if (kLogApi) {
1313 ALOGD("nAllocationGenerateMipmaps, con(%p), a(%p)", (RsContext)con, (RsAllocation)alloc);
1314 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001315 rsAllocationGenerateMipmaps((RsContext)con, (RsAllocation)alloc);
Jason Samsf7086092011-01-12 13:28:37 -08001316}
1317
Tim Murray460a0492013-11-19 12:45:54 -08001318static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001319nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1320 jobject jbitmap, jint usage)
Jason Samsffe9f482009-06-01 17:45:53 -07001321{
John Recked207b92015-04-10 13:52:57 -07001322 SkBitmap bitmap;
1323 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Samsffe9f482009-06-01 17:45:53 -07001324
Jason Sams5476b452010-12-08 16:14:36 -08001325 bitmap.lockPixels();
1326 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001327 jlong id = (jlong)(uintptr_t)rsAllocationCreateFromBitmap((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001328 (RsType)type, (RsAllocationMipmapControl)mip,
Yang Ni8c8daea2016-03-08 21:01:54 +00001329 ptr, bitmap.getSize(), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001330 bitmap.unlockPixels();
1331 return id;
Jason Samsffe9f482009-06-01 17:45:53 -07001332}
Jason Samsfe08d992009-05-27 14:45:32 -07001333
Tim Murray460a0492013-11-19 12:45:54 -08001334static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001335nAllocationCreateBitmapBackedAllocation(JNIEnv *_env, jobject _this, jlong con, jlong type,
1336 jint mip, jobject jbitmap, jint usage)
Tim Murraya3145512012-12-04 17:59:29 -08001337{
John Recked207b92015-04-10 13:52:57 -07001338 SkBitmap bitmap;
1339 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Tim Murraya3145512012-12-04 17:59:29 -08001340
1341 bitmap.lockPixels();
1342 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001343 jlong id = (jlong)(uintptr_t)rsAllocationCreateTyped((RsContext)con,
Tim Murraya3145512012-12-04 17:59:29 -08001344 (RsType)type, (RsAllocationMipmapControl)mip,
Yang Ni8c8daea2016-03-08 21:01:54 +00001345 (uint32_t)usage, (uintptr_t)ptr);
Tim Murraya3145512012-12-04 17:59:29 -08001346 bitmap.unlockPixels();
1347 return id;
1348}
1349
Tim Murray460a0492013-11-19 12:45:54 -08001350static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001351nAllocationCubeCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1352 jobject jbitmap, jint usage)
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001353{
John Recked207b92015-04-10 13:52:57 -07001354 SkBitmap bitmap;
1355 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001356
Jason Sams5476b452010-12-08 16:14:36 -08001357 bitmap.lockPixels();
1358 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001359 jlong id = (jlong)(uintptr_t)rsAllocationCubeCreateFromBitmap((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001360 (RsType)type, (RsAllocationMipmapControl)mip,
Yang Ni8c8daea2016-03-08 21:01:54 +00001361 ptr, bitmap.getSize(), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001362 bitmap.unlockPixels();
1363 return id;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001364}
1365
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001366static void
Tim Murray460a0492013-11-19 12:45:54 -08001367nAllocationCopyFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001368{
John Recked207b92015-04-10 13:52:57 -07001369 SkBitmap bitmap;
1370 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Samsf7086092011-01-12 13:28:37 -08001371 int w = bitmap.width();
1372 int h = bitmap.height();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001373
Jason Sams4ef66502010-12-10 16:03:15 -08001374 bitmap.lockPixels();
1375 const void* ptr = bitmap.getPixels();
Tim Murrayeff663f2013-11-15 13:08:30 -08001376 rsAllocation2DData((RsContext)con, (RsAllocation)alloc, 0, 0,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001377 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Tim Murray38faea302012-11-27 14:55:08 -08001378 w, h, ptr, bitmap.getSize(), 0);
Jason Sams4ef66502010-12-10 16:03:15 -08001379 bitmap.unlockPixels();
1380}
1381
1382static void
Tim Murray460a0492013-11-19 12:45:54 -08001383nAllocationCopyToBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Jason Sams4ef66502010-12-10 16:03:15 -08001384{
John Recked207b92015-04-10 13:52:57 -07001385 SkBitmap bitmap;
1386 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Sams4ef66502010-12-10 16:03:15 -08001387
1388 bitmap.lockPixels();
1389 void* ptr = bitmap.getPixels();
Tim Murrayeff663f2013-11-15 13:08:30 -08001390 rsAllocationCopyToBitmap((RsContext)con, (RsAllocation)alloc, ptr, bitmap.getSize());
Jason Sams4ef66502010-12-10 16:03:15 -08001391 bitmap.unlockPixels();
Alex Sakhartchouk835b8542011-07-20 14:33:10 -07001392 bitmap.notifyPixelsChanged();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001393}
1394
Stephen Hines414fa2c2014-04-17 01:02:42 -07001395// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Samsd19f10d2009-05-22 14:03:28 -07001396static void
Tim Murray460a0492013-11-19 12:45:54 -08001397nAllocationData1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001398 jint count, jobject data, jint sizeBytes, jint dataType, jint mSize,
1399 jboolean usePadding)
Jason Samsd19f10d2009-05-22 14:03:28 -07001400{
Jason Samse729a942013-11-06 11:22:02 -08001401 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001402 if (kLogApi) {
1403 ALOGD("nAllocation1DData, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1404 "dataType(%i)", (RsContext)con, (RsAllocation)alloc, offset, count, sizeBytes,
1405 dataType);
1406 }
Miao Wang87e908d2015-03-02 15:15:15 -08001407 PER_ARRAY_TYPE(nullptr, rsAllocation1DData, true,
1408 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -07001409}
1410
1411static void
Miao Wangc8e237e2015-02-20 18:36:32 -08001412nAllocationElementData(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1413 jint xoff, jint yoff, jint zoff,
1414 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Jason Sams49bdaf02010-08-31 13:50:42 -07001415{
Andreas Gampe67333922014-11-10 20:35:59 -08001416 if (kLogApi) {
Yang Ni86c5c2d2016-03-25 15:49:07 -07001417 jint len = _env->GetArrayLength(data);
Miao Wangc8e237e2015-02-20 18:36:32 -08001418 ALOGD("nAllocationElementData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1419 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
Andreas Gampe67333922014-11-10 20:35:59 -08001420 sizeBytes);
1421 }
Chris Wailes488230c32014-08-14 11:22:40 -07001422 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001423 if (ptr == nullptr) {
1424 ALOGE("Failed to get Java array elements");
1425 return;
1426 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001427 rsAllocationElementData((RsContext)con, (RsAllocation)alloc,
1428 xoff, yoff, zoff,
1429 lod, ptr, sizeBytes, compIdx);
Jason Sams49bdaf02010-08-31 13:50:42 -07001430 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1431}
1432
Miao Wangc8e237e2015-02-20 18:36:32 -08001433
Stephen Hines414fa2c2014-04-17 01:02:42 -07001434// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Sams49bdaf02010-08-31 13:50:42 -07001435static void
Tim Murray460a0492013-11-19 12:45:54 -08001436nAllocationData2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001437 jint w, jint h, jobject data, jint sizeBytes, jint dataType, jint mSize,
1438 jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001439{
Jason Samse729a942013-11-06 11:22:02 -08001440 RsAllocation *alloc = (RsAllocation *)_alloc;
1441 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001442 if (kLogApi) {
1443 ALOGD("nAllocation2DData, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1444 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1445 }
Miao Wang87e908d2015-03-02 15:15:15 -08001446 int count = w * h;
1447 PER_ARRAY_TYPE(nullptr, rsAllocation2DData, true,
1448 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Samsd19f10d2009-05-22 14:03:28 -07001449}
1450
Stephen Hines414fa2c2014-04-17 01:02:42 -07001451// Copies from the Allocation pointed to by srcAlloc into the Allocation
1452// pointed to by dstAlloc.
Jason Sams40a29e82009-08-10 14:55:26 -07001453static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001454nAllocationData2D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001455 jlong dstAlloc, jint dstXoff, jint dstYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001456 jint dstMip, jint dstFace,
1457 jint width, jint height,
Tim Murray460a0492013-11-19 12:45:54 -08001458 jlong srcAlloc, jint srcXoff, jint srcYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001459 jint srcMip, jint srcFace)
1460{
Andreas Gampe67333922014-11-10 20:35:59 -08001461 if (kLogApi) {
1462 ALOGD("nAllocation2DData_s, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1463 " dstMip(%i), dstFace(%i), width(%i), height(%i),"
1464 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i), srcFace(%i)",
1465 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip, dstFace,
1466 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
1467 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001468
Tim Murrayeff663f2013-11-15 13:08:30 -08001469 rsAllocationCopy2DRange((RsContext)con,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001470 (RsAllocation)dstAlloc,
1471 dstXoff, dstYoff,
1472 dstMip, dstFace,
1473 width, height,
1474 (RsAllocation)srcAlloc,
1475 srcXoff, srcYoff,
1476 srcMip, srcFace);
1477}
1478
Stephen Hines414fa2c2014-04-17 01:02:42 -07001479// Copies from the Java object data into the Allocation pointed to by _alloc.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001480static void
Tim Murray460a0492013-11-19 12:45:54 -08001481nAllocationData3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001482 jint w, jint h, jint d, jobject data, jint sizeBytes, jint dataType,
1483 jint mSize, jboolean usePadding)
Jason Samsb05d6892013-04-09 15:59:24 -07001484{
Jason Samse729a942013-11-06 11:22:02 -08001485 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001486 if (kLogApi) {
1487 ALOGD("nAllocation3DData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1488 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1489 lod, w, h, d, sizeBytes);
1490 }
Miao Wang87e908d2015-03-02 15:15:15 -08001491 int count = w * h * d;
1492 PER_ARRAY_TYPE(nullptr, rsAllocation3DData, true,
1493 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Jason Samsb05d6892013-04-09 15:59:24 -07001494}
1495
Stephen Hines414fa2c2014-04-17 01:02:42 -07001496// Copies from the Allocation pointed to by srcAlloc into the Allocation
1497// pointed to by dstAlloc.
Jason Samsb05d6892013-04-09 15:59:24 -07001498static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001499nAllocationData3D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001500 jlong dstAlloc, jint dstXoff, jint dstYoff, jint dstZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001501 jint dstMip,
1502 jint width, jint height, jint depth,
Tim Murray460a0492013-11-19 12:45:54 -08001503 jlong srcAlloc, jint srcXoff, jint srcYoff, jint srcZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001504 jint srcMip)
1505{
Andreas Gampe67333922014-11-10 20:35:59 -08001506 if (kLogApi) {
1507 ALOGD("nAllocationData3D_alloc, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1508 " dstMip(%i), width(%i), height(%i),"
1509 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i)",
1510 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip,
1511 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip);
1512 }
Jason Samsb05d6892013-04-09 15:59:24 -07001513
Tim Murrayeff663f2013-11-15 13:08:30 -08001514 rsAllocationCopy3DRange((RsContext)con,
Jason Samsb05d6892013-04-09 15:59:24 -07001515 (RsAllocation)dstAlloc,
1516 dstXoff, dstYoff, dstZoff, dstMip,
1517 width, height, depth,
1518 (RsAllocation)srcAlloc,
1519 srcXoff, srcYoff, srcZoff, srcMip);
1520}
1521
Jason Sams21659ac2013-11-06 15:08:07 -08001522
Stephen Hines414fa2c2014-04-17 01:02:42 -07001523// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsb05d6892013-04-09 15:59:24 -07001524static void
Miao Wang87e908d2015-03-02 15:15:15 -08001525nAllocationRead(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jobject data, jint dataType,
1526 jint mSize, jboolean usePadding)
Jason Sams40a29e82009-08-10 14:55:26 -07001527{
Jason Sams21659ac2013-11-06 15:08:07 -08001528 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001529 if (kLogApi) {
1530 ALOGD("nAllocationRead, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
1531 }
Miao Wang87e908d2015-03-02 15:15:15 -08001532 int count = 0;
1533 PER_ARRAY_TYPE(0, rsAllocationRead, false,
1534 (RsContext)con, alloc, ptr, len * typeBytes);
Jason Sams40a29e82009-08-10 14:55:26 -07001535}
1536
Stephen Hines414fa2c2014-04-17 01:02:42 -07001537// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Sams40a29e82009-08-10 14:55:26 -07001538static void
Tim Murray460a0492013-11-19 12:45:54 -08001539nAllocationRead1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001540 jint count, jobject data, jint sizeBytes, jint dataType,
1541 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001542{
Jason Sams21659ac2013-11-06 15:08:07 -08001543 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001544 if (kLogApi) {
1545 ALOGD("nAllocation1DRead, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1546 "dataType(%i)", (RsContext)con, alloc, offset, count, sizeBytes, dataType);
1547 }
Miao Wang87e908d2015-03-02 15:15:15 -08001548 PER_ARRAY_TYPE(0, rsAllocation1DRead, false,
1549 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsfb9f82c2011-01-12 14:53:25 -08001550}
1551
Miao Wangc8e237e2015-02-20 18:36:32 -08001552// Copies from the Element in the Allocation pointed to by _alloc into the Java array data.
1553static void
Miao Wang45cec0a2015-03-04 16:40:21 -08001554nAllocationElementRead(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
Miao Wangc8e237e2015-02-20 18:36:32 -08001555 jint xoff, jint yoff, jint zoff,
Miao Wang45cec0a2015-03-04 16:40:21 -08001556 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Miao Wangc8e237e2015-02-20 18:36:32 -08001557{
Miao Wangc8e237e2015-02-20 18:36:32 -08001558 if (kLogApi) {
Yang Ni86c5c2d2016-03-25 15:49:07 -07001559 jint len = _env->GetArrayLength(data);
Miao Wang45cec0a2015-03-04 16:40:21 -08001560 ALOGD("nAllocationElementRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1561 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
1562 sizeBytes);
Miao Wangc8e237e2015-02-20 18:36:32 -08001563 }
Miao Wang45cec0a2015-03-04 16:40:21 -08001564 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001565 if (ptr == nullptr) {
1566 ALOGE("Failed to get Java array elements");
1567 return;
1568 }
Miao Wang45cec0a2015-03-04 16:40:21 -08001569 rsAllocationElementRead((RsContext)con, (RsAllocation)alloc,
1570 xoff, yoff, zoff,
Jason Samsa7e25092015-03-11 11:00:00 -07001571 lod, ptr, sizeBytes, compIdx);
Miao Wangbfa5e652015-05-04 15:29:25 -07001572 _env->ReleaseByteArrayElements(data, ptr, 0);
Miao Wangc8e237e2015-02-20 18:36:32 -08001573}
1574
Stephen Hines414fa2c2014-04-17 01:02:42 -07001575// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsfb9f82c2011-01-12 14:53:25 -08001576static void
Tim Murray460a0492013-11-19 12:45:54 -08001577nAllocationRead2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001578 jint w, jint h, jobject data, jint sizeBytes, jint dataType,
1579 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001580{
Jason Sams21659ac2013-11-06 15:08:07 -08001581 RsAllocation *alloc = (RsAllocation *)_alloc;
1582 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001583 if (kLogApi) {
1584 ALOGD("nAllocation2DRead, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1585 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1586 }
Miao Wang87e908d2015-03-02 15:15:15 -08001587 int count = w * h;
1588 PER_ARRAY_TYPE(0, rsAllocation2DRead, false,
1589 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Sams40a29e82009-08-10 14:55:26 -07001590}
Miao Wang87e908d2015-03-02 15:15:15 -08001591
Miao Wangc8e237e2015-02-20 18:36:32 -08001592// Copies from the Allocation pointed to by _alloc into the Java object data.
1593static void
1594nAllocationRead3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001595 jint w, jint h, jint d, jobject data, int sizeBytes, int dataType,
1596 jint mSize, jboolean usePadding)
Miao Wangc8e237e2015-02-20 18:36:32 -08001597{
1598 RsAllocation *alloc = (RsAllocation *)_alloc;
1599 if (kLogApi) {
1600 ALOGD("nAllocation3DRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1601 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1602 lod, w, h, d, sizeBytes);
1603 }
Miao Wang87e908d2015-03-02 15:15:15 -08001604 int count = w * h * d;
1605 PER_ARRAY_TYPE(nullptr, rsAllocation3DRead, false,
1606 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Miao Wangc8e237e2015-02-20 18:36:32 -08001607}
Jason Samsd19f10d2009-05-22 14:03:28 -07001608
Tim Murray460a0492013-11-19 12:45:54 -08001609static jlong
1610nAllocationGetType(JNIEnv *_env, jobject _this, jlong con, jlong a)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001611{
Andreas Gampe67333922014-11-10 20:35:59 -08001612 if (kLogApi) {
1613 ALOGD("nAllocationGetType, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1614 }
Tim Murray3aa89c12014-08-18 17:51:22 -07001615 return (jlong)(uintptr_t) rsaAllocationGetType((RsContext)con, (RsAllocation)a);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001616}
1617
Jason Sams5edc6082010-10-05 13:32:49 -07001618static void
Tim Murray460a0492013-11-19 12:45:54 -08001619nAllocationResize1D(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint dimX)
Jason Sams5edc6082010-10-05 13:32:49 -07001620{
Andreas Gampe67333922014-11-10 20:35:59 -08001621 if (kLogApi) {
1622 ALOGD("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", (RsContext)con,
1623 (RsAllocation)alloc, dimX);
1624 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001625 rsAllocationResize1D((RsContext)con, (RsAllocation)alloc, dimX);
Jason Sams5edc6082010-10-05 13:32:49 -07001626}
1627
Jason Sams46ba27e32015-02-06 17:45:15 -08001628
1629static jlong
1630nAllocationAdapterCreate(JNIEnv *_env, jobject _this, jlong con, jlong basealloc, jlong type)
1631{
1632 if (kLogApi) {
1633 ALOGD("nAllocationAdapterCreate, con(%p), base(%p), type(%p)",
1634 (RsContext)con, (RsAllocation)basealloc, (RsElement)type);
1635 }
1636 return (jlong)(uintptr_t) rsAllocationAdapterCreate((RsContext)con, (RsType)type,
1637 (RsAllocation)basealloc);
1638
1639}
1640
1641static void
1642nAllocationAdapterOffset(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1643 jint x, jint y, jint z, jint face, jint lod,
1644 jint a1, jint a2, jint a3, jint a4)
1645{
1646 uint32_t params[] = {
1647 (uint32_t)x, (uint32_t)y, (uint32_t)z, (uint32_t)face,
1648 (uint32_t)lod, (uint32_t)a1, (uint32_t)a2, (uint32_t)a3, (uint32_t)a4
1649 };
1650 if (kLogApi) {
1651 ALOGD("nAllocationAdapterOffset, con(%p), alloc(%p), x(%i), y(%i), z(%i), face(%i), lod(%i), arrays(%i %i %i %i)",
1652 (RsContext)con, (RsAllocation)alloc, x, y, z, face, lod, a1, a2, a3, a4);
1653 }
1654 rsAllocationAdapterOffset((RsContext)con, (RsAllocation)alloc,
1655 params, sizeof(params));
1656}
1657
1658
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001659// -----------------------------------
1660
Tim Murray460a0492013-11-19 12:45:54 -08001661static jlong
1662nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con, jlong native_asset)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001663{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001664 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001665 ALOGV("______nFileA3D %p", asset);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001666
Tim Murray3aa89c12014-08-18 17:51:22 -07001667 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromMemory((RsContext)con, asset->getBuffer(false), asset->getLength());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001668 return id;
1669}
1670
Tim Murray460a0492013-11-19 12:45:54 -08001671static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001672nFileA3DCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001673{
1674 AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001675 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001676 return 0;
1677 }
1678
1679 AutoJavaStringToUTF8 str(_env, _path);
1680 Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
Chris Wailes488230c32014-08-14 11:22:40 -07001681 if (asset == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001682 return 0;
1683 }
1684
Tim Murray3aa89c12014-08-18 17:51:22 -07001685 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromAsset((RsContext)con, asset);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001686 return id;
1687}
1688
Tim Murray460a0492013-11-19 12:45:54 -08001689static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001690nFileA3DCreateFromFile(JNIEnv *_env, jobject _this, jlong con, jstring fileName)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001691{
1692 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001693 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromFile((RsContext)con, fileNameUTF.c_str());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001694
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001695 return id;
1696}
1697
Tim Murray460a0492013-11-19 12:45:54 -08001698static jint
1699nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001700{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001701 int32_t numEntries = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001702 rsaFileA3DGetNumIndexEntries((RsContext)con, &numEntries, (RsFile)fileA3D);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001703 return (jint)numEntries;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001704}
1705
1706static void
Tim Murray460a0492013-11-19 12:45:54 -08001707nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001708{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001709 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001710 RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
1711
Tim Murrayeff663f2013-11-15 13:08:30 -08001712 rsaFileA3DGetIndexEntries((RsContext)con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001713
1714 for(jint i = 0; i < numEntries; i ++) {
1715 _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
1716 _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
1717 }
1718
1719 free(fileEntries);
1720}
1721
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001722static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001723nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint index)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001724{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001725 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Tim Murray3aa89c12014-08-18 17:51:22 -07001726 jlong id = (jlong)(uintptr_t)rsaFileA3DGetEntryByIndex((RsContext)con, (uint32_t)index, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001727 return id;
1728}
Jason Samsd19f10d2009-05-22 14:03:28 -07001729
1730// -----------------------------------
1731
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001732static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001733nFontCreateFromFile(JNIEnv *_env, jobject _this, jlong con,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001734 jstring fileName, jfloat fontSize, jint dpi)
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001735{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001736 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001737 jlong id = (jlong)(uintptr_t)rsFontCreateFromFile((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001738 fileNameUTF.c_str(), fileNameUTF.length(),
1739 fontSize, dpi);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001740
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001741 return id;
1742}
1743
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001744static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001745nFontCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con,
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001746 jstring name, jfloat fontSize, jint dpi, jlong native_asset)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001747{
1748 Asset* asset = reinterpret_cast<Asset*>(native_asset);
1749 AutoJavaStringToUTF8 nameUTF(_env, name);
1750
Tim Murray3aa89c12014-08-18 17:51:22 -07001751 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001752 nameUTF.c_str(), nameUTF.length(),
1753 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001754 asset->getBuffer(false), asset->getLength());
1755 return id;
1756}
1757
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001758static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001759nFontCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001760 jfloat fontSize, jint dpi)
1761{
1762 AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001763 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001764 return 0;
1765 }
1766
1767 AutoJavaStringToUTF8 str(_env, _path);
1768 Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
Chris Wailes488230c32014-08-14 11:22:40 -07001769 if (asset == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001770 return 0;
1771 }
1772
Tim Murray3aa89c12014-08-18 17:51:22 -07001773 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001774 str.c_str(), str.length(),
1775 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001776 asset->getBuffer(false), asset->getLength());
1777 delete asset;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001778 return id;
1779}
1780
Jason Samsbd1c3ad2009-08-03 16:03:08 -07001781// -----------------------------------
1782
1783static void
Tim Murray460a0492013-11-19 12:45:54 -08001784nScriptBindAllocation(JNIEnv *_env, jobject _this, jlong con, jlong script, jlong alloc, jint slot)
Jason Samsd19f10d2009-05-22 14:03:28 -07001785{
Andreas Gampe67333922014-11-10 20:35:59 -08001786 if (kLogApi) {
1787 ALOGD("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", (RsContext)con,
1788 (RsScript)script, (RsAllocation)alloc, slot);
1789 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001790 rsScriptBindAllocation((RsContext)con, (RsScript)script, (RsAllocation)alloc, slot);
Jason Samsd19f10d2009-05-22 14:03:28 -07001791}
1792
1793static void
Tim Murray460a0492013-11-19 12:45:54 -08001794nScriptSetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jint val)
Jason Sams4d339932010-05-11 14:03:58 -07001795{
Andreas Gampe67333922014-11-10 20:35:59 -08001796 if (kLogApi) {
1797 ALOGD("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", (RsContext)con, (void *)script,
1798 slot, val);
1799 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001800 rsScriptSetVarI((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001801}
1802
Tim Murray7c4caad2013-04-10 16:21:40 -07001803static jint
Tim Murray460a0492013-11-19 12:45:54 -08001804nScriptGetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001805{
Andreas Gampe67333922014-11-10 20:35:59 -08001806 if (kLogApi) {
1807 ALOGD("nScriptGetVarI, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1808 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001809 int value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001810 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001811 return value;
1812}
1813
Jason Sams4d339932010-05-11 14:03:58 -07001814static void
Tim Murray460a0492013-11-19 12:45:54 -08001815nScriptSetVarObj(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001816{
Andreas Gampe67333922014-11-10 20:35:59 -08001817 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001818 ALOGD("nScriptSetVarObj, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001819 slot, val);
1820 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001821 rsScriptSetVarObj((RsContext)con, (RsScript)script, slot, (RsObjectBase)val);
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001822}
1823
1824static void
Tim Murray460a0492013-11-19 12:45:54 -08001825nScriptSetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Stephen Hines031ec58c2010-10-11 10:54:21 -07001826{
Andreas Gampe67333922014-11-10 20:35:59 -08001827 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001828 ALOGD("nScriptSetVarJ, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001829 slot, val);
1830 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001831 rsScriptSetVarJ((RsContext)con, (RsScript)script, slot, val);
Stephen Hines031ec58c2010-10-11 10:54:21 -07001832}
1833
Tim Murray7c4caad2013-04-10 16:21:40 -07001834static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001835nScriptGetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001836{
Andreas Gampe67333922014-11-10 20:35:59 -08001837 if (kLogApi) {
1838 ALOGD("nScriptGetVarJ, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1839 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001840 jlong value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001841 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001842 return value;
1843}
1844
Stephen Hines031ec58c2010-10-11 10:54:21 -07001845static void
Tim Murray460a0492013-11-19 12:45:54 -08001846nScriptSetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, float val)
Jason Sams4d339932010-05-11 14:03:58 -07001847{
Andreas Gampe67333922014-11-10 20:35:59 -08001848 if (kLogApi) {
1849 ALOGD("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", (RsContext)con, (void *)script,
1850 slot, val);
1851 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001852 rsScriptSetVarF((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001853}
1854
Tim Murray7c4caad2013-04-10 16:21:40 -07001855static jfloat
Tim Murray460a0492013-11-19 12:45:54 -08001856nScriptGetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001857{
Andreas Gampe67333922014-11-10 20:35:59 -08001858 if (kLogApi) {
1859 ALOGD("nScriptGetVarF, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1860 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001861 jfloat value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001862 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001863 return value;
1864}
1865
Jason Sams4d339932010-05-11 14:03:58 -07001866static void
Tim Murray460a0492013-11-19 12:45:54 -08001867nScriptSetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, double val)
Stephen Hinesca54ec32010-09-20 17:20:30 -07001868{
Andreas Gampe67333922014-11-10 20:35:59 -08001869 if (kLogApi) {
1870 ALOGD("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", (RsContext)con, (void *)script,
1871 slot, val);
1872 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001873 rsScriptSetVarD((RsContext)con, (RsScript)script, slot, val);
Stephen Hinesca54ec32010-09-20 17:20:30 -07001874}
1875
Tim Murray7c4caad2013-04-10 16:21:40 -07001876static jdouble
Tim Murray460a0492013-11-19 12:45:54 -08001877nScriptGetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001878{
Andreas Gampe67333922014-11-10 20:35:59 -08001879 if (kLogApi) {
1880 ALOGD("nScriptGetVarD, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1881 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001882 jdouble value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001883 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001884 return value;
1885}
1886
Stephen Hinesca54ec32010-09-20 17:20:30 -07001887static void
Tim Murray460a0492013-11-19 12:45:54 -08001888nScriptSetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001889{
Andreas Gampe67333922014-11-10 20:35:59 -08001890 if (kLogApi) {
1891 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1892 }
Jason Sams4d339932010-05-11 14:03:58 -07001893 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001894 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001895 if (ptr == nullptr) {
1896 ALOGE("Failed to get Java array elements");
1897 return;
1898 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001899 rsScriptSetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001900 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1901}
1902
Stephen Hinesadeb8092012-04-20 14:26:06 -07001903static void
Tim Murray460a0492013-11-19 12:45:54 -08001904nScriptGetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Tim Murray7c4caad2013-04-10 16:21:40 -07001905{
Andreas Gampe67333922014-11-10 20:35:59 -08001906 if (kLogApi) {
1907 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1908 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001909 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001910 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001911 if (ptr == nullptr) {
1912 ALOGE("Failed to get Java array elements");
1913 return;
1914 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001915 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Stephen Hines414fa2c2014-04-17 01:02:42 -07001916 _env->ReleaseByteArrayElements(data, ptr, 0);
Tim Murray7c4caad2013-04-10 16:21:40 -07001917}
1918
1919static void
Andreas Gampe67333922014-11-10 20:35:59 -08001920nScriptSetVarVE(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data,
1921 jlong elem, jintArray dims)
Stephen Hinesadeb8092012-04-20 14:26:06 -07001922{
Andreas Gampe67333922014-11-10 20:35:59 -08001923 if (kLogApi) {
1924 ALOGD("nScriptSetVarVE, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1925 }
Stephen Hinesadeb8092012-04-20 14:26:06 -07001926 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001927 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001928 if (ptr == nullptr) {
1929 ALOGE("Failed to get Java array elements");
1930 return;
1931 }
Stephen Hinesadeb8092012-04-20 14:26:06 -07001932 jint dimsLen = _env->GetArrayLength(dims) * sizeof(int);
Chris Wailes488230c32014-08-14 11:22:40 -07001933 jint *dimsPtr = _env->GetIntArrayElements(dims, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001934 if (dimsPtr == nullptr) {
1935 ALOGE("Failed to get Java array elements");
1936 return;
1937 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001938 rsScriptSetVarVE((RsContext)con, (RsScript)script, slot, ptr, len, (RsElement)elem,
Stephen Hinesbc5d3ee2014-06-25 00:03:39 -07001939 (const uint32_t*) dimsPtr, dimsLen);
Stephen Hinesadeb8092012-04-20 14:26:06 -07001940 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1941 _env->ReleaseIntArrayElements(dims, dimsPtr, JNI_ABORT);
1942}
1943
Jason Samsd19f10d2009-05-22 14:03:28 -07001944
1945static void
Tim Murray460a0492013-11-19 12:45:54 -08001946nScriptSetTimeZone(JNIEnv *_env, jobject _this, jlong con, jlong script, jbyteArray timeZone)
Jason Samsd19f10d2009-05-22 14:03:28 -07001947{
Andreas Gampe67333922014-11-10 20:35:59 -08001948 if (kLogApi) {
1949 ALOGD("nScriptCSetTimeZone, con(%p), s(%p)", (RsContext)con, (void *)script);
1950 }
Romain Guy584a3752009-07-30 18:45:01 -07001951
1952 jint length = _env->GetArrayLength(timeZone);
1953 jbyte* timeZone_ptr;
1954 timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
Miao Wangba8766c2015-10-12 17:24:13 -07001955 if (timeZone_ptr == nullptr) {
1956 ALOGE("Failed to get Java array elements");
1957 return;
1958 }
Romain Guy584a3752009-07-30 18:45:01 -07001959
Tim Murrayeff663f2013-11-15 13:08:30 -08001960 rsScriptSetTimeZone((RsContext)con, (RsScript)script, (const char *)timeZone_ptr, length);
Romain Guy584a3752009-07-30 18:45:01 -07001961
1962 if (timeZone_ptr) {
1963 _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
1964 }
1965}
1966
Jason Samsfbf0b9e2009-08-13 12:59:04 -07001967static void
Tim Murray460a0492013-11-19 12:45:54 -08001968nScriptInvoke(JNIEnv *_env, jobject _this, jlong con, jlong obj, jint slot)
Jason Samsbe2e8412009-09-16 15:04:38 -07001969{
Andreas Gampe67333922014-11-10 20:35:59 -08001970 if (kLogApi) {
1971 ALOGD("nScriptInvoke, con(%p), script(%p)", (RsContext)con, (void *)obj);
1972 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001973 rsScriptInvoke((RsContext)con, (RsScript)obj, slot);
Jason Samsbe2e8412009-09-16 15:04:38 -07001974}
1975
1976static void
Tim Murray460a0492013-11-19 12:45:54 -08001977nScriptInvokeV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001978{
Andreas Gampe67333922014-11-10 20:35:59 -08001979 if (kLogApi) {
1980 ALOGD("nScriptInvokeV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1981 }
Jason Sams4d339932010-05-11 14:03:58 -07001982 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001983 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001984 if (ptr == nullptr) {
1985 ALOGE("Failed to get Java array elements");
1986 return;
1987 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001988 rsScriptInvokeV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001989 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1990}
1991
Jason Sams6e494d32011-04-27 16:33:11 -07001992static void
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001993nScriptForEach(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
1994 jlongArray ains, jlong aout, jbyteArray params,
1995 jintArray limits)
Jason Sams6e494d32011-04-27 16:33:11 -07001996{
Andreas Gampe67333922014-11-10 20:35:59 -08001997 if (kLogApi) {
Chih-Hung Hsieh9eb9dd32015-05-06 14:42:04 -07001998 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 -08001999 }
Jason Sams6e494d32011-04-27 16:33:11 -07002000
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002001 jint in_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002002 jlong *in_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002003
Chris Wailes488230c32014-08-14 11:22:40 -07002004 RsAllocation *in_allocs = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002005
Chris Wailes488230c32014-08-14 11:22:40 -07002006 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002007 in_len = _env->GetArrayLength(ains);
Yang Ni7b2a46f2015-05-05 12:41:19 -07002008 if (in_len > (jint)RS_KERNEL_MAX_ARGUMENTS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -07002009 ALOGE("Too many arguments in kernel launch.");
2010 // TODO (b/20758983): Report back to Java and throw an exception
2011 return;
2012 }
Chris Wailes94961062014-06-11 12:01:28 -07002013
Yang Ni17c2d7a2015-04-30 16:13:54 -07002014 in_ptr = _env->GetLongArrayElements(ains, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002015 if (in_ptr == nullptr) {
2016 ALOGE("Failed to get Java array elements");
2017 return;
2018 }
2019
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002020 if (sizeof(RsAllocation) == sizeof(jlong)) {
2021 in_allocs = (RsAllocation*)in_ptr;
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002022 } else {
2023 // Convert from 64-bit jlong types to the native pointer type.
Chris Wailes94961062014-06-11 12:01:28 -07002024
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002025 in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
Yang Ni17c2d7a2015-04-30 16:13:54 -07002026 if (in_allocs == nullptr) {
2027 ALOGE("Failed launching kernel for lack of memory.");
2028 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2029 return;
2030 }
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002031
2032 for (int index = in_len; --index >= 0;) {
2033 in_allocs[index] = (RsAllocation)in_ptr[index];
2034 }
2035 }
Chris Wailes94961062014-06-11 12:01:28 -07002036 }
2037
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002038 jint param_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002039 jbyte *param_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002040
Chris Wailes488230c32014-08-14 11:22:40 -07002041 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002042 param_len = _env->GetArrayLength(params);
Chris Wailes488230c32014-08-14 11:22:40 -07002043 param_ptr = _env->GetByteArrayElements(params, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002044 if (param_ptr == nullptr) {
2045 ALOGE("Failed to get Java array elements");
2046 return;
2047 }
Chris Wailes94961062014-06-11 12:01:28 -07002048 }
2049
Chris Wailes488230c32014-08-14 11:22:40 -07002050 RsScriptCall sc, *sca = nullptr;
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002051 uint32_t sc_size = 0;
Chris Wailes94961062014-06-11 12:01:28 -07002052
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002053 jint limit_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002054 jint *limit_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002055
Chris Wailes488230c32014-08-14 11:22:40 -07002056 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002057 limit_len = _env->GetArrayLength(limits);
Chris Wailes488230c32014-08-14 11:22:40 -07002058 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002059 if (limit_ptr == nullptr) {
2060 ALOGE("Failed to get Java array elements");
2061 return;
2062 }
Chris Wailes94961062014-06-11 12:01:28 -07002063
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002064 assert(limit_len == 6);
Andreas Gampe67333922014-11-10 20:35:59 -08002065 UNUSED(limit_len); // As the assert might not be compiled.
Chris Wailes94961062014-06-11 12:01:28 -07002066
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002067 sc.xStart = limit_ptr[0];
2068 sc.xEnd = limit_ptr[1];
2069 sc.yStart = limit_ptr[2];
2070 sc.yEnd = limit_ptr[3];
2071 sc.zStart = limit_ptr[4];
2072 sc.zEnd = limit_ptr[5];
2073 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
Jason Sams14331ab2015-01-26 18:14:36 -08002074 sc.arrayStart = 0;
2075 sc.arrayEnd = 0;
2076 sc.array2Start = 0;
2077 sc.array2End = 0;
2078 sc.array3Start = 0;
2079 sc.array3End = 0;
2080 sc.array4Start = 0;
2081 sc.array4End = 0;
Chris Wailes94961062014-06-11 12:01:28 -07002082
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002083 sca = &sc;
Yang Nie8f2e442016-03-15 16:00:02 -07002084 // sc_size is required, but unused, by the runtime and drivers.
2085 sc_size = sizeof(sc);
Chris Wailes94961062014-06-11 12:01:28 -07002086 }
2087
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002088 rsScriptForEachMulti((RsContext)con, (RsScript)script, slot,
2089 in_allocs, in_len, (RsAllocation)aout,
2090 param_ptr, param_len, sca, sc_size);
Chris Wailes94961062014-06-11 12:01:28 -07002091
Chris Wailes488230c32014-08-14 11:22:40 -07002092 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002093 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
Chris Wailes94961062014-06-11 12:01:28 -07002094 }
2095
Chris Wailes488230c32014-08-14 11:22:40 -07002096 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002097 _env->ReleaseByteArrayElements(params, param_ptr, JNI_ABORT);
2098 }
2099
Chris Wailes488230c32014-08-14 11:22:40 -07002100 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002101 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
2102 }
Chris Wailes94961062014-06-11 12:01:28 -07002103}
2104
Matt Wala36eb1f72015-07-20 15:35:27 -07002105static void
2106nScriptReduce(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
David Gross4a457852016-06-02 14:46:55 -07002107 jlongArray ains, jlong aout, jintArray limits)
Matt Wala36eb1f72015-07-20 15:35:27 -07002108{
2109 if (kLogApi) {
David Gross4a457852016-06-02 14:46:55 -07002110 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 -08002111 }
2112
2113 if (ains == nullptr) {
2114 ALOGE("At least one input required.");
2115 // TODO (b/20758983): Report back to Java and throw an exception
2116 return;
2117 }
2118 jint in_len = _env->GetArrayLength(ains);
2119 if (in_len > (jint)RS_KERNEL_MAX_ARGUMENTS) {
2120 ALOGE("Too many arguments in kernel launch.");
2121 // TODO (b/20758983): Report back to Java and throw an exception
2122 return;
2123 }
2124
2125 jlong *in_ptr = _env->GetLongArrayElements(ains, nullptr);
2126 if (in_ptr == nullptr) {
2127 ALOGE("Failed to get Java array elements");
2128 // TODO (b/20758983): Report back to Java and throw an exception
2129 return;
2130 }
2131
2132 RsAllocation *in_allocs = nullptr;
2133 if (sizeof(RsAllocation) == sizeof(jlong)) {
2134 in_allocs = (RsAllocation*)in_ptr;
2135 } else {
2136 // Convert from 64-bit jlong types to the native pointer type.
2137
2138 in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
2139 if (in_allocs == nullptr) {
2140 ALOGE("Failed launching kernel for lack of memory.");
2141 // TODO (b/20758983): Report back to Java and throw an exception
2142 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2143 return;
2144 }
2145
2146 for (int index = in_len; --index >= 0;) {
2147 in_allocs[index] = (RsAllocation)in_ptr[index];
2148 }
2149 }
2150
2151 RsScriptCall sc, *sca = nullptr;
2152 uint32_t sc_size = 0;
2153
2154 jint limit_len = 0;
2155 jint *limit_ptr = nullptr;
2156
2157 if (limits != nullptr) {
2158 limit_len = _env->GetArrayLength(limits);
2159 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
2160 if (limit_ptr == nullptr) {
2161 ALOGE("Failed to get Java array elements");
2162 // TODO (b/20758983): Report back to Java and throw an exception
2163 return;
2164 }
2165
2166 assert(limit_len == 6);
2167 UNUSED(limit_len); // As the assert might not be compiled.
2168
2169 sc.xStart = limit_ptr[0];
2170 sc.xEnd = limit_ptr[1];
2171 sc.yStart = limit_ptr[2];
2172 sc.yEnd = limit_ptr[3];
2173 sc.zStart = limit_ptr[4];
2174 sc.zEnd = limit_ptr[5];
2175 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
2176 sc.arrayStart = 0;
2177 sc.arrayEnd = 0;
2178 sc.array2Start = 0;
2179 sc.array2End = 0;
2180 sc.array3Start = 0;
2181 sc.array3End = 0;
2182 sc.array4Start = 0;
2183 sc.array4End = 0;
2184
2185 sca = &sc;
2186 sc_size = sizeof(sc);
2187 }
2188
David Gross4a457852016-06-02 14:46:55 -07002189 rsScriptReduce((RsContext)con, (RsScript)script, slot,
2190 in_allocs, in_len, (RsAllocation)aout,
2191 sca, sc_size);
David Gross26ef7a732016-01-12 12:19:15 -08002192
2193 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2194
2195 if (limits != nullptr) {
2196 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
2197 }
2198}
2199
Jason Sams22534172009-08-04 16:58:20 -07002200// -----------------------------------
2201
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002202static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002203nScriptCCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Samse4a06c52011-03-16 16:29:28 -07002204 jstring resName, jstring cacheDir,
2205 jbyteArray scriptRef, jint length)
Jason Sams22534172009-08-04 16:58:20 -07002206{
Andreas Gampe67333922014-11-10 20:35:59 -08002207 if (kLogApi) {
2208 ALOGD("nScriptCCreate, con(%p)", (RsContext)con);
2209 }
Jason Sams22534172009-08-04 16:58:20 -07002210
Jason Samse4a06c52011-03-16 16:29:28 -07002211 AutoJavaStringToUTF8 resNameUTF(_env, resName);
2212 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002213 jlong ret = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002214 jbyte* script_ptr = nullptr;
Jack Palevich43702d82009-05-28 13:38:16 -07002215 jint _exception = 0;
2216 jint remaining;
Jack Palevich43702d82009-05-28 13:38:16 -07002217 if (!scriptRef) {
2218 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002219 //jniThrowException(_env, "java/lang/IllegalArgumentException", "script == null");
Jack Palevich43702d82009-05-28 13:38:16 -07002220 goto exit;
2221 }
Jack Palevich43702d82009-05-28 13:38:16 -07002222 if (length < 0) {
2223 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002224 //jniThrowException(_env, "java/lang/IllegalArgumentException", "length < 0");
Jack Palevich43702d82009-05-28 13:38:16 -07002225 goto exit;
2226 }
Jason Samse4a06c52011-03-16 16:29:28 -07002227 remaining = _env->GetArrayLength(scriptRef);
Jack Palevich43702d82009-05-28 13:38:16 -07002228 if (remaining < length) {
2229 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002230 //jniThrowException(_env, "java/lang/IllegalArgumentException",
2231 // "length > script.length - offset");
Jack Palevich43702d82009-05-28 13:38:16 -07002232 goto exit;
2233 }
Jason Samse4a06c52011-03-16 16:29:28 -07002234 script_ptr = (jbyte *)
Jack Palevich43702d82009-05-28 13:38:16 -07002235 _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
Miao Wangba8766c2015-10-12 17:24:13 -07002236 if (script_ptr == nullptr) {
2237 ALOGE("Failed to get Java array elements");
2238 return ret;
2239 }
Jack Palevich43702d82009-05-28 13:38:16 -07002240
Tim Murrayeff663f2013-11-15 13:08:30 -08002241 //rsScriptCSetText((RsContext)con, (const char *)script_ptr, length);
Jason Samse4a06c52011-03-16 16:29:28 -07002242
Tim Murray3aa89c12014-08-18 17:51:22 -07002243 ret = (jlong)(uintptr_t)rsScriptCCreate((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07002244 resNameUTF.c_str(), resNameUTF.length(),
2245 cacheDirUTF.c_str(), cacheDirUTF.length(),
Jason Samse4a06c52011-03-16 16:29:28 -07002246 (const char *)script_ptr, length);
Jason Sams39ddc9502009-06-05 17:35:09 -07002247
Jack Palevich43702d82009-05-28 13:38:16 -07002248exit:
Jason Samse4a06c52011-03-16 16:29:28 -07002249 if (script_ptr) {
2250 _env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr,
Jack Palevich43702d82009-05-28 13:38:16 -07002251 _exception ? JNI_ABORT: 0);
2252 }
Jason Samsd19f10d2009-05-22 14:03:28 -07002253
Tim Murray3aa89c12014-08-18 17:51:22 -07002254 return (jlong)(uintptr_t)ret;
Jason Samsd19f10d2009-05-22 14:03:28 -07002255}
2256
Tim Murray460a0492013-11-19 12:45:54 -08002257static jlong
2258nScriptIntrinsicCreate(JNIEnv *_env, jobject _this, jlong con, jint id, jlong eid)
Jason Sams6ab97682012-08-10 12:09:43 -07002259{
Andreas Gampe67333922014-11-10 20:35:59 -08002260 if (kLogApi) {
2261 ALOGD("nScriptIntrinsicCreate, con(%p) id(%i) element(%p)", (RsContext)con, id,
2262 (void *)eid);
2263 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002264 return (jlong)(uintptr_t)rsScriptIntrinsicCreate((RsContext)con, id, (RsElement)eid);
Jason Sams6ab97682012-08-10 12:09:43 -07002265}
2266
Tim Murray460a0492013-11-19 12:45:54 -08002267static jlong
2268nScriptKernelIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot, jint sig)
Jason Sams08a81582012-09-18 12:32:10 -07002269{
Andreas Gampe67333922014-11-10 20:35:59 -08002270 if (kLogApi) {
2271 ALOGD("nScriptKernelIDCreate, con(%p) script(%p), slot(%i), sig(%i)", (RsContext)con,
2272 (void *)sid, slot, sig);
2273 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002274 return (jlong)(uintptr_t)rsScriptKernelIDCreate((RsContext)con, (RsScript)sid, slot, sig);
Jason Sams08a81582012-09-18 12:32:10 -07002275}
2276
Tim Murray460a0492013-11-19 12:45:54 -08002277static jlong
Yang Nibe392ad2015-01-23 17:16:02 -08002278nScriptInvokeIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
2279{
2280 if (kLogApi) {
Elliott Hughes7ff53fa2015-02-05 21:36:10 -08002281 ALOGD("nScriptInvokeIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con,
Yang Nibe392ad2015-01-23 17:16:02 -08002282 (void *)sid, slot);
2283 }
2284 return (jlong)(uintptr_t)rsScriptInvokeIDCreate((RsContext)con, (RsScript)sid, slot);
2285}
2286
2287static jlong
Tim Murray460a0492013-11-19 12:45:54 -08002288nScriptFieldIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
Jason Sams08a81582012-09-18 12:32:10 -07002289{
Andreas Gampe67333922014-11-10 20:35:59 -08002290 if (kLogApi) {
2291 ALOGD("nScriptFieldIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con, (void *)sid,
2292 slot);
2293 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002294 return (jlong)(uintptr_t)rsScriptFieldIDCreate((RsContext)con, (RsScript)sid, slot);
Jason Sams08a81582012-09-18 12:32:10 -07002295}
2296
Tim Murray460a0492013-11-19 12:45:54 -08002297static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002298nScriptGroupCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _kernels, jlongArray _src,
2299 jlongArray _dstk, jlongArray _dstf, jlongArray _types)
Jason Sams08a81582012-09-18 12:32:10 -07002300{
Andreas Gampe67333922014-11-10 20:35:59 -08002301 if (kLogApi) {
2302 ALOGD("nScriptGroupCreate, con(%p)", (RsContext)con);
2303 }
Jason Sams08a81582012-09-18 12:32:10 -07002304
Miao Wanga4ad5f82016-02-11 12:32:39 -08002305 jlong id = 0;
2306
2307 RsScriptKernelID* kernelsPtr;
Ashok Bhat98071552014-02-12 09:54:43 +00002308 jint kernelsLen = _env->GetArrayLength(_kernels);
Chris Wailes488230c32014-08-14 11:22:40 -07002309 jlong *jKernelsPtr = _env->GetLongArrayElements(_kernels, nullptr);
Miao Wanga4ad5f82016-02-11 12:32:39 -08002310
2311 RsScriptKernelID* srcPtr;
2312 jint srcLen = _env->GetArrayLength(_src);
2313 jlong *jSrcPtr = _env->GetLongArrayElements(_src, nullptr);
2314
2315 RsScriptKernelID* dstkPtr;
2316 jint dstkLen = _env->GetArrayLength(_dstk);
2317 jlong *jDstkPtr = _env->GetLongArrayElements(_dstk, nullptr);
2318
2319 RsScriptKernelID* dstfPtr;
2320 jint dstfLen = _env->GetArrayLength(_dstf);
2321 jlong *jDstfPtr = _env->GetLongArrayElements(_dstf, nullptr);
2322
2323 RsType* typesPtr;
2324 jint typesLen = _env->GetArrayLength(_types);
2325 jlong *jTypesPtr = _env->GetLongArrayElements(_types, nullptr);
2326
Miao Wangba8766c2015-10-12 17:24:13 -07002327 if (jKernelsPtr == nullptr) {
2328 ALOGE("Failed to get Java array elements: kernels");
Miao Wanga4ad5f82016-02-11 12:32:39 -08002329 goto cleanup;
Miao Wangba8766c2015-10-12 17:24:13 -07002330 }
Miao Wanga4ad5f82016-02-11 12:32:39 -08002331 if (jSrcPtr == nullptr) {
2332 ALOGE("Failed to get Java array elements: src");
2333 goto cleanup;
2334 }
2335 if (jDstkPtr == nullptr) {
2336 ALOGE("Failed to get Java array elements: dstk");
2337 goto cleanup;
2338 }
2339 if (jDstfPtr == nullptr) {
2340 ALOGE("Failed to get Java array elements: dstf");
2341 goto cleanup;
2342 }
2343 if (jTypesPtr == nullptr) {
2344 ALOGE("Failed to get Java array elements: types");
2345 goto cleanup;
2346 }
2347
2348 kernelsPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * kernelsLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002349 for(int i = 0; i < kernelsLen; ++i) {
2350 kernelsPtr[i] = (RsScriptKernelID)jKernelsPtr[i];
2351 }
Jason Sams08a81582012-09-18 12:32:10 -07002352
Miao Wanga4ad5f82016-02-11 12:32:39 -08002353 srcPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * srcLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002354 for(int i = 0; i < srcLen; ++i) {
2355 srcPtr[i] = (RsScriptKernelID)jSrcPtr[i];
2356 }
Jason Sams08a81582012-09-18 12:32:10 -07002357
Miao Wanga4ad5f82016-02-11 12:32:39 -08002358 dstkPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstkLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002359 for(int i = 0; i < dstkLen; ++i) {
2360 dstkPtr[i] = (RsScriptKernelID)jDstkPtr[i];
2361 }
2362
Miao Wanga4ad5f82016-02-11 12:32:39 -08002363 dstfPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstfLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002364 for(int i = 0; i < dstfLen; ++i) {
2365 dstfPtr[i] = (RsScriptKernelID)jDstfPtr[i];
2366 }
2367
Miao Wanga4ad5f82016-02-11 12:32:39 -08002368 typesPtr = (RsType*) malloc(sizeof(RsType) * typesLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002369 for(int i = 0; i < typesLen; ++i) {
2370 typesPtr[i] = (RsType)jTypesPtr[i];
2371 }
2372
Miao Wanga4ad5f82016-02-11 12:32:39 -08002373 id = (jlong)(uintptr_t)rsScriptGroupCreate((RsContext)con,
Ashok Bhat98071552014-02-12 09:54:43 +00002374 (RsScriptKernelID *)kernelsPtr, kernelsLen * sizeof(RsScriptKernelID),
2375 (RsScriptKernelID *)srcPtr, srcLen * sizeof(RsScriptKernelID),
2376 (RsScriptKernelID *)dstkPtr, dstkLen * sizeof(RsScriptKernelID),
2377 (RsScriptFieldID *)dstfPtr, dstfLen * sizeof(RsScriptKernelID),
2378 (RsType *)typesPtr, typesLen * sizeof(RsType));
2379
2380 free(kernelsPtr);
2381 free(srcPtr);
2382 free(dstkPtr);
2383 free(dstfPtr);
2384 free(typesPtr);
Miao Wanga4ad5f82016-02-11 12:32:39 -08002385
2386cleanup:
2387 if (jKernelsPtr != nullptr) {
2388 _env->ReleaseLongArrayElements(_kernels, jKernelsPtr, 0);
2389 }
2390 if (jSrcPtr != nullptr) {
2391 _env->ReleaseLongArrayElements(_src, jSrcPtr, 0);
2392 }
2393 if (jDstkPtr != nullptr) {
2394 _env->ReleaseLongArrayElements(_dstk, jDstkPtr, 0);
2395 }
2396 if (jDstfPtr != nullptr) {
2397 _env->ReleaseLongArrayElements(_dstf, jDstfPtr, 0);
2398 }
2399 if (jTypesPtr != nullptr) {
2400 _env->ReleaseLongArrayElements(_types, jTypesPtr, 0);
2401 }
2402
Jason Sams08a81582012-09-18 12:32:10 -07002403 return id;
2404}
2405
2406static void
Tim Murray460a0492013-11-19 12:45:54 -08002407nScriptGroupSetInput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002408{
Andreas Gampe67333922014-11-10 20:35:59 -08002409 if (kLogApi) {
2410 ALOGD("nScriptGroupSetInput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2411 (void *)gid, (void *)kid, (void *)alloc);
2412 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002413 rsScriptGroupSetInput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002414}
2415
2416static void
Tim Murray460a0492013-11-19 12:45:54 -08002417nScriptGroupSetOutput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002418{
Andreas Gampe67333922014-11-10 20:35:59 -08002419 if (kLogApi) {
2420 ALOGD("nScriptGroupSetOutput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2421 (void *)gid, (void *)kid, (void *)alloc);
2422 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002423 rsScriptGroupSetOutput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002424}
2425
2426static void
Tim Murray460a0492013-11-19 12:45:54 -08002427nScriptGroupExecute(JNIEnv *_env, jobject _this, jlong con, jlong gid)
Jason Sams08a81582012-09-18 12:32:10 -07002428{
Andreas Gampe67333922014-11-10 20:35:59 -08002429 if (kLogApi) {
2430 ALOGD("nScriptGroupSetOutput, con(%p) group(%p)", (RsContext)con, (void *)gid);
2431 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002432 rsScriptGroupExecute((RsContext)con, (RsScriptGroup)gid);
Jason Sams08a81582012-09-18 12:32:10 -07002433}
2434
Jason Samsd19f10d2009-05-22 14:03:28 -07002435// ---------------------------------------------------------------------------
2436
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002437static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002438nProgramStoreCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Sams331bf9b2011-04-06 11:23:54 -07002439 jboolean colorMaskR, jboolean colorMaskG, jboolean colorMaskB, jboolean colorMaskA,
2440 jboolean depthMask, jboolean ditherEnable,
2441 jint srcFunc, jint destFunc,
2442 jint depthFunc)
Jason Samsd19f10d2009-05-22 14:03:28 -07002443{
Andreas Gampe67333922014-11-10 20:35:59 -08002444 if (kLogApi) {
2445 ALOGD("nProgramStoreCreate, con(%p)", (RsContext)con);
2446 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002447 return (jlong)(uintptr_t)rsProgramStoreCreate((RsContext)con, colorMaskR, colorMaskG, colorMaskB, colorMaskA,
Jason Sams331bf9b2011-04-06 11:23:54 -07002448 depthMask, ditherEnable, (RsBlendSrcFunc)srcFunc,
2449 (RsBlendDstFunc)destFunc, (RsDepthFunc)depthFunc);
Jason Samsd19f10d2009-05-22 14:03:28 -07002450}
2451
Jason Sams0011bcf2009-12-15 12:58:36 -08002452// ---------------------------------------------------------------------------
2453
2454static void
Tim Murray460a0492013-11-19 12:45:54 -08002455nProgramBindConstants(JNIEnv *_env, jobject _this, jlong con, jlong vpv, jint slot, jlong a)
Jason Sams0011bcf2009-12-15 12:58:36 -08002456{
Andreas Gampe67333922014-11-10 20:35:59 -08002457 if (kLogApi) {
2458 ALOGD("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", (RsContext)con,
2459 (RsProgramVertex)vpv, slot, (RsAllocation)a);
2460 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002461 rsProgramBindConstants((RsContext)con, (RsProgram)vpv, slot, (RsAllocation)a);
Jason Sams0011bcf2009-12-15 12:58:36 -08002462}
Jason Sams54c0ec12009-11-30 14:49:55 -08002463
Jason Sams68afd012009-12-17 16:55:08 -08002464static void
Tim Murray460a0492013-11-19 12:45:54 -08002465nProgramBindTexture(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002466{
Andreas Gampe67333922014-11-10 20:35:59 -08002467 if (kLogApi) {
2468 ALOGD("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2469 (RsProgramFragment)vpf, slot, (RsAllocation)a);
2470 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002471 rsProgramBindTexture((RsContext)con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
Jason Sams68afd012009-12-17 16:55:08 -08002472}
2473
2474static void
Tim Murray460a0492013-11-19 12:45:54 -08002475nProgramBindSampler(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002476{
Andreas Gampe67333922014-11-10 20:35:59 -08002477 if (kLogApi) {
2478 ALOGD("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2479 (RsProgramFragment)vpf, slot, (RsSampler)a);
2480 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002481 rsProgramBindSampler((RsContext)con, (RsProgramFragment)vpf, slot, (RsSampler)a);
Jason Sams68afd012009-12-17 16:55:08 -08002482}
2483
Jason Samsd19f10d2009-05-22 14:03:28 -07002484// ---------------------------------------------------------------------------
2485
Tim Murray460a0492013-11-19 12:45:54 -08002486static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002487nProgramFragmentCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002488 jobjectArray texNames, jlongArray params)
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002489{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002490 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002491 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002492 jint paramLen = _env->GetArrayLength(params);
Miao Wangba8766c2015-10-12 17:24:13 -07002493 if (jParamPtr == nullptr) {
2494 ALOGE("Failed to get Java array elements");
2495 return 0;
2496 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002497
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002498 int texCount = _env->GetArrayLength(texNames);
2499 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2500 const char ** nameArray = names.c_str();
2501 size_t* sizeArray = names.c_str_len();
2502
Andreas Gampe67333922014-11-10 20:35:59 -08002503 if (kLogApi) {
2504 ALOGD("nProgramFragmentCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2505 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002506
Ashok Bhat98071552014-02-12 09:54:43 +00002507 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2508 for(int i = 0; i < paramLen; ++i) {
2509 paramPtr[i] = (uintptr_t)jParamPtr[i];
2510 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002511 jlong ret = (jlong)(uintptr_t)rsProgramFragmentCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002512 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002513 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002514
Ashok Bhat98071552014-02-12 09:54:43 +00002515 free(paramPtr);
2516 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002517 return ret;
2518}
2519
2520
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002521// ---------------------------------------------------------------------------
2522
Tim Murray460a0492013-11-19 12:45:54 -08002523static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002524nProgramVertexCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002525 jobjectArray texNames, jlongArray params)
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002526{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002527 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002528 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams0011bcf2009-12-15 12:58:36 -08002529 jint paramLen = _env->GetArrayLength(params);
Miao Wangba8766c2015-10-12 17:24:13 -07002530 if (jParamPtr == nullptr) {
2531 ALOGE("Failed to get Java array elements");
2532 return 0;
2533 }
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002534
Andreas Gampe67333922014-11-10 20:35:59 -08002535 if (kLogApi) {
2536 ALOGD("nProgramVertexCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2537 }
Jason Sams0011bcf2009-12-15 12:58:36 -08002538
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002539 int texCount = _env->GetArrayLength(texNames);
2540 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2541 const char ** nameArray = names.c_str();
2542 size_t* sizeArray = names.c_str_len();
2543
Ashok Bhat98071552014-02-12 09:54:43 +00002544 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2545 for(int i = 0; i < paramLen; ++i) {
2546 paramPtr[i] = (uintptr_t)jParamPtr[i];
2547 }
2548
Tim Murray3aa89c12014-08-18 17:51:22 -07002549 jlong ret = (jlong)(uintptr_t)rsProgramVertexCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002550 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002551 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002552
Ashok Bhat98071552014-02-12 09:54:43 +00002553 free(paramPtr);
2554 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams0011bcf2009-12-15 12:58:36 -08002555 return ret;
2556}
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002557
Jason Samsebfb4362009-09-23 13:57:02 -07002558// ---------------------------------------------------------------------------
2559
Tim Murray460a0492013-11-19 12:45:54 -08002560static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002561nProgramRasterCreate(JNIEnv *_env, jobject _this, jlong con, jboolean pointSprite, jint cull)
Jason Samsebfb4362009-09-23 13:57:02 -07002562{
Andreas Gampe67333922014-11-10 20:35:59 -08002563 if (kLogApi) {
2564 ALOGD("nProgramRasterCreate, con(%p), pointSprite(%i), cull(%i)", (RsContext)con,
2565 pointSprite, cull);
2566 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002567 return (jlong)(uintptr_t)rsProgramRasterCreate((RsContext)con, pointSprite, (RsCullMode)cull);
Jason Samsebfb4362009-09-23 13:57:02 -07002568}
2569
Jason Samsd19f10d2009-05-22 14:03:28 -07002570
2571// ---------------------------------------------------------------------------
2572
2573static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002574nContextBindRootScript(JNIEnv *_env, jobject _this, jlong con, jlong script)
Jason Samsd19f10d2009-05-22 14:03:28 -07002575{
Andreas Gampe67333922014-11-10 20:35:59 -08002576 if (kLogApi) {
2577 ALOGD("nContextBindRootScript, con(%p), script(%p)", (RsContext)con, (RsScript)script);
2578 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002579 rsContextBindRootScript((RsContext)con, (RsScript)script);
Jason Samsd19f10d2009-05-22 14:03:28 -07002580}
2581
2582static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002583nContextBindProgramStore(JNIEnv *_env, jobject _this, jlong con, jlong pfs)
Jason Samsd19f10d2009-05-22 14:03:28 -07002584{
Andreas Gampe67333922014-11-10 20:35:59 -08002585 if (kLogApi) {
2586 ALOGD("nContextBindProgramStore, con(%p), pfs(%p)", (RsContext)con, (RsProgramStore)pfs);
2587 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002588 rsContextBindProgramStore((RsContext)con, (RsProgramStore)pfs);
Jason Samsd19f10d2009-05-22 14:03:28 -07002589}
2590
2591static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002592nContextBindProgramFragment(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsd19f10d2009-05-22 14:03:28 -07002593{
Andreas Gampe67333922014-11-10 20:35:59 -08002594 if (kLogApi) {
2595 ALOGD("nContextBindProgramFragment, con(%p), pf(%p)", (RsContext)con,
2596 (RsProgramFragment)pf);
2597 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002598 rsContextBindProgramFragment((RsContext)con, (RsProgramFragment)pf);
Jason Samsd19f10d2009-05-22 14:03:28 -07002599}
2600
Jason Sams0826a6f2009-06-15 19:04:56 -07002601static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002602nContextBindProgramVertex(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Sams0826a6f2009-06-15 19:04:56 -07002603{
Andreas Gampe67333922014-11-10 20:35:59 -08002604 if (kLogApi) {
2605 ALOGD("nContextBindProgramVertex, con(%p), pf(%p)", (RsContext)con, (RsProgramVertex)pf);
2606 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002607 rsContextBindProgramVertex((RsContext)con, (RsProgramVertex)pf);
Jason Sams0826a6f2009-06-15 19:04:56 -07002608}
2609
Joe Onoratod7b37742009-08-09 22:57:44 -07002610static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002611nContextBindProgramRaster(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsebfb4362009-09-23 13:57:02 -07002612{
Andreas Gampe67333922014-11-10 20:35:59 -08002613 if (kLogApi) {
2614 ALOGD("nContextBindProgramRaster, con(%p), pf(%p)", (RsContext)con, (RsProgramRaster)pf);
2615 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002616 rsContextBindProgramRaster((RsContext)con, (RsProgramRaster)pf);
Jason Samsebfb4362009-09-23 13:57:02 -07002617}
2618
Joe Onoratod7b37742009-08-09 22:57:44 -07002619
Jason Sams02fb2cb2009-05-28 15:37:57 -07002620// ---------------------------------------------------------------------------
2621
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002622static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002623nSamplerCreate(JNIEnv *_env, jobject _this, jlong con, jint magFilter, jint minFilter,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002624 jint wrapS, jint wrapT, jint wrapR, jfloat aniso)
Jason Sams02fb2cb2009-05-28 15:37:57 -07002625{
Andreas Gampe67333922014-11-10 20:35:59 -08002626 if (kLogApi) {
2627 ALOGD("nSamplerCreate, con(%p)", (RsContext)con);
2628 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002629 return (jlong)(uintptr_t)rsSamplerCreate((RsContext)con,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002630 (RsSamplerValue)magFilter,
2631 (RsSamplerValue)minFilter,
2632 (RsSamplerValue)wrapS,
2633 (RsSamplerValue)wrapT,
2634 (RsSamplerValue)wrapR,
2635 aniso);
Jason Sams02fb2cb2009-05-28 15:37:57 -07002636}
2637
Jason Samsbba134c2009-06-22 15:49:21 -07002638// ---------------------------------------------------------------------------
2639
Tim Murray460a0492013-11-19 12:45:54 -08002640static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002641nMeshCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _vtx, jlongArray _idx, jintArray _prim)
Jason Samsbba134c2009-06-22 15:49:21 -07002642{
Andreas Gampe67333922014-11-10 20:35:59 -08002643 if (kLogApi) {
2644 ALOGD("nMeshCreate, con(%p)", (RsContext)con);
2645 }
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002646
Miao Wanga4ad5f82016-02-11 12:32:39 -08002647 jlong id = 0;
2648
2649 RsAllocation* vtxPtr;
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002650 jint vtxLen = _env->GetArrayLength(_vtx);
Chris Wailes488230c32014-08-14 11:22:40 -07002651 jlong *jVtxPtr = _env->GetLongArrayElements(_vtx, nullptr);
Miao Wanga4ad5f82016-02-11 12:32:39 -08002652
2653 RsAllocation* idxPtr;
2654 jint idxLen = _env->GetArrayLength(_idx);
2655 jlong *jIdxPtr = _env->GetLongArrayElements(_idx, nullptr);
2656
2657 jint primLen = _env->GetArrayLength(_prim);
2658 jint *primPtr = _env->GetIntArrayElements(_prim, nullptr);
2659
Miao Wangba8766c2015-10-12 17:24:13 -07002660 if (jVtxPtr == nullptr) {
2661 ALOGE("Failed to get Java array elements: vtx");
Miao Wanga4ad5f82016-02-11 12:32:39 -08002662 goto cleanupMesh;
Miao Wangba8766c2015-10-12 17:24:13 -07002663 }
Miao Wanga4ad5f82016-02-11 12:32:39 -08002664 if (jIdxPtr == nullptr) {
2665 ALOGE("Failed to get Java array elements: idx");
2666 goto cleanupMesh;
2667 }
2668 if (primPtr == nullptr) {
2669 ALOGE("Failed to get Java array elements: prim");
2670 goto cleanupMesh;
2671 }
2672
2673 vtxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * vtxLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002674 for(int i = 0; i < vtxLen; ++i) {
2675 vtxPtr[i] = (RsAllocation)(uintptr_t)jVtxPtr[i];
2676 }
2677
Miao Wanga4ad5f82016-02-11 12:32:39 -08002678 idxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * idxLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002679 for(int i = 0; i < idxLen; ++i) {
2680 idxPtr[i] = (RsAllocation)(uintptr_t)jIdxPtr[i];
2681 }
2682
Miao Wanga4ad5f82016-02-11 12:32:39 -08002683 id = (jlong)(uintptr_t)rsMeshCreate((RsContext)con,
2684 (RsAllocation *)vtxPtr, vtxLen,
2685 (RsAllocation *)idxPtr, idxLen,
2686 (uint32_t *)primPtr, primLen);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002687
Ashok Bhat98071552014-02-12 09:54:43 +00002688 free(vtxPtr);
2689 free(idxPtr);
Miao Wanga4ad5f82016-02-11 12:32:39 -08002690
2691cleanupMesh:
2692 if (jVtxPtr != nullptr) {
2693 _env->ReleaseLongArrayElements(_vtx, jVtxPtr, 0);
2694 }
2695 if (jIdxPtr != nullptr) {
2696 _env->ReleaseLongArrayElements(_idx, jIdxPtr, 0);
2697 }
2698 if (primPtr != nullptr) {
2699 _env->ReleaseIntArrayElements(_prim, primPtr, 0);
2700 }
2701
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002702 return id;
2703}
2704
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002705static jint
Tim Murray460a0492013-11-19 12:45:54 -08002706nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002707{
Andreas Gampe67333922014-11-10 20:35:59 -08002708 if (kLogApi) {
2709 ALOGD("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2710 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002711 jint vtxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002712 rsaMeshGetVertexBufferCount((RsContext)con, (RsMesh)mesh, &vtxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002713 return vtxCount;
2714}
2715
2716static jint
Tim Murray460a0492013-11-19 12:45:54 -08002717nMeshGetIndexCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002718{
Andreas Gampe67333922014-11-10 20:35:59 -08002719 if (kLogApi) {
2720 ALOGD("nMeshGetIndexCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2721 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002722 jint idxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002723 rsaMeshGetIndexCount((RsContext)con, (RsMesh)mesh, &idxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002724 return idxCount;
2725}
2726
2727static void
Ashok Bhat98071552014-02-12 09:54:43 +00002728nMeshGetVertices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _ids, jint numVtxIDs)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002729{
Andreas Gampe67333922014-11-10 20:35:59 -08002730 if (kLogApi) {
2731 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2732 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002733
2734 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
Tim Murrayeff663f2013-11-15 13:08:30 -08002735 rsaMeshGetVertices((RsContext)con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002736
2737 for(jint i = 0; i < numVtxIDs; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002738 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002739 _env->SetLongArrayRegion(_ids, i, 1, &alloc);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002740 }
2741
2742 free(allocs);
2743}
2744
2745static void
Ashok Bhat98071552014-02-12 09:54:43 +00002746nMeshGetIndices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _idxIds, jintArray _primitives, jint numIndices)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002747{
Andreas Gampe67333922014-11-10 20:35:59 -08002748 if (kLogApi) {
2749 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2750 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002751
2752 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
2753 uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
2754
Tim Murrayeff663f2013-11-15 13:08:30 -08002755 rsaMeshGetIndices((RsContext)con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002756
2757 for(jint i = 0; i < numIndices; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002758 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002759 const jint prim = (jint)prims[i];
2760 _env->SetLongArrayRegion(_idxIds, i, 1, &alloc);
2761 _env->SetIntArrayRegion(_primitives, i, 1, &prim);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002762 }
2763
2764 free(allocs);
2765 free(prims);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002766}
2767
Tim Murray56f9e6f2014-05-16 11:47:26 -07002768static jint
2769nSystemGetPointerSize(JNIEnv *_env, jobject _this) {
2770 return (jint)sizeof(void*);
2771}
2772
Miao Wang0facf022015-11-25 11:21:13 -08002773static jobject
2774nAllocationGetByteBuffer(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
2775 jlongArray strideArr, jint xBytesSize,
2776 jint dimY, jint dimZ) {
2777 if (kLogApi) {
2778 ALOGD("nAllocationGetByteBuffer, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
2779 }
Tim Murray56f9e6f2014-05-16 11:47:26 -07002780
Miao Wang0facf022015-11-25 11:21:13 -08002781 jlong *jStridePtr = _env->GetLongArrayElements(strideArr, nullptr);
2782 if (jStridePtr == nullptr) {
2783 ALOGE("Failed to get Java array elements: strideArr");
2784 return 0;
2785 }
2786
2787 size_t strideIn = xBytesSize;
2788 void* ptr = nullptr;
2789 if (alloc != 0) {
2790 ptr = rsAllocationGetPointer((RsContext)con, (RsAllocation)alloc, 0,
2791 RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, 0, 0,
2792 &strideIn, sizeof(size_t));
2793 }
2794
2795 jobject byteBuffer = nullptr;
2796 if (ptr != nullptr) {
2797 size_t bufferSize = strideIn;
2798 jStridePtr[0] = strideIn;
2799 if (dimY > 0) {
2800 bufferSize *= dimY;
2801 }
2802 if (dimZ > 0) {
2803 bufferSize *= dimZ;
2804 }
2805 byteBuffer = _env->NewDirectByteBuffer(ptr, (jlong) bufferSize);
2806 }
2807 _env->ReleaseLongArrayElements(strideArr, jStridePtr, 0);
2808 return byteBuffer;
2809}
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002810// ---------------------------------------------------------------------------
2811
Jason Samsd19f10d2009-05-22 14:03:28 -07002812
Jason Sams94d8e90a2009-06-10 16:09:05 -07002813static const char *classPathName = "android/renderscript/RenderScript";
Jason Samsd19f10d2009-05-22 14:03:28 -07002814
Daniel Micay76f6a862015-09-19 17:31:01 -04002815static const JNINativeMethod methods[] = {
Jason Sams1c415172010-11-08 17:06:46 -08002816{"_nInit", "()V", (void*)_nInit },
Jason Samsea84a7c2009-09-04 14:42:41 -07002817
Tim Murrayeff663f2013-11-15 13:08:30 -08002818{"nDeviceCreate", "()J", (void*)nDeviceCreate },
2819{"nDeviceDestroy", "(J)V", (void*)nDeviceDestroy },
2820{"nDeviceSetConfig", "(JII)V", (void*)nDeviceSetConfig },
2821{"nContextGetUserMessage", "(J[I)I", (void*)nContextGetUserMessage },
2822{"nContextGetErrorMessage", "(J)Ljava/lang/String;", (void*)nContextGetErrorMessage },
2823{"nContextPeekMessage", "(J[I)I", (void*)nContextPeekMessage },
Jason Sams1c415172010-11-08 17:06:46 -08002824
Tim Murrayeff663f2013-11-15 13:08:30 -08002825{"nContextInitToClient", "(J)V", (void*)nContextInitToClient },
2826{"nContextDeinitToClient", "(J)V", (void*)nContextDeinitToClient },
Jason Samsd19f10d2009-05-22 14:03:28 -07002827
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002828
Jason Sams2e1872f2010-08-17 16:25:41 -07002829// All methods below are thread protected in java.
Tim Murrayeff663f2013-11-15 13:08:30 -08002830{"rsnContextCreate", "(JIII)J", (void*)nContextCreate },
2831{"rsnContextCreateGL", "(JIIIIIIIIIIIIFI)J", (void*)nContextCreateGL },
2832{"rsnContextFinish", "(J)V", (void*)nContextFinish },
2833{"rsnContextSetPriority", "(JI)V", (void*)nContextSetPriority },
Tim Murray47f31582015-04-07 15:43:24 -07002834{"rsnContextSetCacheDir", "(JLjava/lang/String;)V", (void*)nContextSetCacheDir },
Tim Murrayeff663f2013-11-15 13:08:30 -08002835{"rsnContextSetSurface", "(JIILandroid/view/Surface;)V", (void*)nContextSetSurface },
2836{"rsnContextDestroy", "(J)V", (void*)nContextDestroy },
2837{"rsnContextDump", "(JI)V", (void*)nContextDump },
2838{"rsnContextPause", "(J)V", (void*)nContextPause },
2839{"rsnContextResume", "(J)V", (void*)nContextResume },
2840{"rsnContextSendMessage", "(JI[I)V", (void*)nContextSendMessage },
Yang Ni281c3252014-10-24 08:52:24 -07002841{"rsnClosureCreate", "(JJJ[J[J[I[J[J)J", (void*)nClosureCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002842{"rsnInvokeClosureCreate", "(JJ[B[J[J[I)J", (void*)nInvokeClosureCreate },
Yang Ni281c3252014-10-24 08:52:24 -07002843{"rsnClosureSetArg", "(JJIJI)V", (void*)nClosureSetArg },
2844{"rsnClosureSetGlobal", "(JJJJI)V", (void*)nClosureSetGlobal },
Tim Murray460a0492013-11-19 12:45:54 -08002845{"rsnAssignName", "(JJ[B)V", (void*)nAssignName },
2846{"rsnGetName", "(JJ)Ljava/lang/String;", (void*)nGetName },
2847{"rsnObjDestroy", "(JJ)V", (void*)nObjDestroy },
Jason Sams64676f32009-07-08 18:01:53 -07002848
Tim Murray460a0492013-11-19 12:45:54 -08002849{"rsnFileA3DCreateFromFile", "(JLjava/lang/String;)J", (void*)nFileA3DCreateFromFile },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002850{"rsnFileA3DCreateFromAssetStream", "(JJ)J", (void*)nFileA3DCreateFromAssetStream },
Tim Murray460a0492013-11-19 12:45:54 -08002851{"rsnFileA3DCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;)J", (void*)nFileA3DCreateFromAsset },
2852{"rsnFileA3DGetNumIndexEntries", "(JJ)I", (void*)nFileA3DGetNumIndexEntries },
2853{"rsnFileA3DGetIndexEntries", "(JJI[I[Ljava/lang/String;)V", (void*)nFileA3DGetIndexEntries },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002854{"rsnFileA3DGetEntryByIndex", "(JJI)J", (void*)nFileA3DGetEntryByIndex },
Jason Samsd19f10d2009-05-22 14:03:28 -07002855
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002856{"rsnFontCreateFromFile", "(JLjava/lang/String;FI)J", (void*)nFontCreateFromFile },
2857{"rsnFontCreateFromAssetStream", "(JLjava/lang/String;FIJ)J", (void*)nFontCreateFromAssetStream },
2858{"rsnFontCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;FI)J", (void*)nFontCreateFromAsset },
Jason Samsd19f10d2009-05-22 14:03:28 -07002859
Tim Murray460a0492013-11-19 12:45:54 -08002860{"rsnElementCreate", "(JJIZI)J", (void*)nElementCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002861{"rsnElementCreate2", "(J[J[Ljava/lang/String;[I)J", (void*)nElementCreate2 },
Tim Murray460a0492013-11-19 12:45:54 -08002862{"rsnElementGetNativeData", "(JJ[I)V", (void*)nElementGetNativeData },
Ashok Bhat98071552014-02-12 09:54:43 +00002863{"rsnElementGetSubElements", "(JJ[J[Ljava/lang/String;[I)V", (void*)nElementGetSubElements },
Jason Samsd19f10d2009-05-22 14:03:28 -07002864
Tim Murray460a0492013-11-19 12:45:54 -08002865{"rsnTypeCreate", "(JJIIIZZI)J", (void*)nTypeCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002866{"rsnTypeGetNativeData", "(JJ[J)V", (void*)nTypeGetNativeData },
Jason Samsd19f10d2009-05-22 14:03:28 -07002867
Ashok Bhat98071552014-02-12 09:54:43 +00002868{"rsnAllocationCreateTyped", "(JJIIJ)J", (void*)nAllocationCreateTyped },
Tim Murray460a0492013-11-19 12:45:54 -08002869{"rsnAllocationCreateFromBitmap", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateFromBitmap },
2870{"rsnAllocationCreateBitmapBackedAllocation", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateBitmapBackedAllocation },
2871{"rsnAllocationCubeCreateFromBitmap","(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCubeCreateFromBitmap },
Jason Sams5476b452010-12-08 16:14:36 -08002872
Tim Murray460a0492013-11-19 12:45:54 -08002873{"rsnAllocationCopyFromBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyFromBitmap },
2874{"rsnAllocationCopyToBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyToBitmap },
Jason Sams4ef66502010-12-10 16:03:15 -08002875
Tim Murray460a0492013-11-19 12:45:54 -08002876{"rsnAllocationSyncAll", "(JJI)V", (void*)nAllocationSyncAll },
Miao Wang8c150922015-10-26 17:44:10 -07002877{"rsnAllocationSetupBufferQueue", "(JJI)V", (void*)nAllocationSetupBufferQueue },
2878{"rsnAllocationShareBufferQueue", "(JJJ)V", (void*)nAllocationShareBufferQueue },
Tim Murray460a0492013-11-19 12:45:54 -08002879{"rsnAllocationGetSurface", "(JJ)Landroid/view/Surface;", (void*)nAllocationGetSurface },
2880{"rsnAllocationSetSurface", "(JJLandroid/view/Surface;)V", (void*)nAllocationSetSurface },
2881{"rsnAllocationIoSend", "(JJ)V", (void*)nAllocationIoSend },
Miao Wang8c150922015-10-26 17:44:10 -07002882{"rsnAllocationIoReceive", "(JJ)J", (void*)nAllocationIoReceive },
Miao Wang87e908d2015-03-02 15:15:15 -08002883{"rsnAllocationData1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData1D },
Miao Wangc8e237e2015-02-20 18:36:32 -08002884{"rsnAllocationElementData", "(JJIIIII[BI)V", (void*)nAllocationElementData },
Miao Wang87e908d2015-03-02 15:15:15 -08002885{"rsnAllocationData2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData2D },
Tim Murray460a0492013-11-19 12:45:54 -08002886{"rsnAllocationData2D", "(JJIIIIIIJIIII)V", (void*)nAllocationData2D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002887{"rsnAllocationData3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData3D },
Tim Murray460a0492013-11-19 12:45:54 -08002888{"rsnAllocationData3D", "(JJIIIIIIIJIIII)V", (void*)nAllocationData3D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002889{"rsnAllocationRead", "(JJLjava/lang/Object;IIZ)V", (void*)nAllocationRead },
2890{"rsnAllocationRead1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead1D },
Miao Wang45cec0a2015-03-04 16:40:21 -08002891{"rsnAllocationElementRead", "(JJIIIII[BI)V", (void*)nAllocationElementRead },
Miao Wang87e908d2015-03-02 15:15:15 -08002892{"rsnAllocationRead2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead2D },
2893{"rsnAllocationRead3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead3D },
Tim Murray460a0492013-11-19 12:45:54 -08002894{"rsnAllocationGetType", "(JJ)J", (void*)nAllocationGetType},
2895{"rsnAllocationResize1D", "(JJI)V", (void*)nAllocationResize1D },
2896{"rsnAllocationGenerateMipmaps", "(JJ)V", (void*)nAllocationGenerateMipmaps },
Jason Samsbd1c3ad2009-08-03 16:03:08 -07002897
Jason Sams46ba27e32015-02-06 17:45:15 -08002898{"rsnAllocationAdapterCreate", "(JJJ)J", (void*)nAllocationAdapterCreate },
2899{"rsnAllocationAdapterOffset", "(JJIIIIIIIII)V", (void*)nAllocationAdapterOffset },
2900
Tim Murray460a0492013-11-19 12:45:54 -08002901{"rsnScriptBindAllocation", "(JJJI)V", (void*)nScriptBindAllocation },
2902{"rsnScriptSetTimeZone", "(JJ[B)V", (void*)nScriptSetTimeZone },
2903{"rsnScriptInvoke", "(JJI)V", (void*)nScriptInvoke },
2904{"rsnScriptInvokeV", "(JJI[B)V", (void*)nScriptInvokeV },
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002905
2906{"rsnScriptForEach", "(JJI[JJ[B[I)V", (void*)nScriptForEach },
David Gross4a457852016-06-02 14:46:55 -07002907{"rsnScriptReduce", "(JJI[JJ[I)V", (void*)nScriptReduce },
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002908
Tim Murray460a0492013-11-19 12:45:54 -08002909{"rsnScriptSetVarI", "(JJII)V", (void*)nScriptSetVarI },
2910{"rsnScriptGetVarI", "(JJI)I", (void*)nScriptGetVarI },
2911{"rsnScriptSetVarJ", "(JJIJ)V", (void*)nScriptSetVarJ },
2912{"rsnScriptGetVarJ", "(JJI)J", (void*)nScriptGetVarJ },
2913{"rsnScriptSetVarF", "(JJIF)V", (void*)nScriptSetVarF },
2914{"rsnScriptGetVarF", "(JJI)F", (void*)nScriptGetVarF },
2915{"rsnScriptSetVarD", "(JJID)V", (void*)nScriptSetVarD },
2916{"rsnScriptGetVarD", "(JJI)D", (void*)nScriptGetVarD },
2917{"rsnScriptSetVarV", "(JJI[B)V", (void*)nScriptSetVarV },
2918{"rsnScriptGetVarV", "(JJI[B)V", (void*)nScriptGetVarV },
2919{"rsnScriptSetVarVE", "(JJI[BJ[I)V", (void*)nScriptSetVarVE },
2920{"rsnScriptSetVarObj", "(JJIJ)V", (void*)nScriptSetVarObj },
Jason Samsd19f10d2009-05-22 14:03:28 -07002921
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002922{"rsnScriptCCreate", "(JLjava/lang/String;Ljava/lang/String;[BI)J", (void*)nScriptCCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002923{"rsnScriptIntrinsicCreate", "(JIJ)J", (void*)nScriptIntrinsicCreate },
2924{"rsnScriptKernelIDCreate", "(JJII)J", (void*)nScriptKernelIDCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002925{"rsnScriptInvokeIDCreate", "(JJI)J", (void*)nScriptInvokeIDCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002926{"rsnScriptFieldIDCreate", "(JJI)J", (void*)nScriptFieldIDCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002927{"rsnScriptGroupCreate", "(J[J[J[J[J[J)J", (void*)nScriptGroupCreate },
Yang Ni35be56c2015-04-02 17:47:56 -07002928{"rsnScriptGroup2Create", "(JLjava/lang/String;Ljava/lang/String;[J)J", (void*)nScriptGroup2Create },
Tim Murray460a0492013-11-19 12:45:54 -08002929{"rsnScriptGroupSetInput", "(JJJJ)V", (void*)nScriptGroupSetInput },
2930{"rsnScriptGroupSetOutput", "(JJJJ)V", (void*)nScriptGroupSetOutput },
2931{"rsnScriptGroupExecute", "(JJ)V", (void*)nScriptGroupExecute },
Yang Ni281c3252014-10-24 08:52:24 -07002932{"rsnScriptGroup2Execute", "(JJ)V", (void*)nScriptGroup2Execute },
Jason Sams0011bcf2009-12-15 12:58:36 -08002933
Tim Murray25207df2015-01-12 16:47:56 -08002934{"rsnScriptIntrinsicBLAS_Single", "(JJIIIIIIIIIFJJFJIIII)V", (void*)nScriptIntrinsicBLAS_Single },
2935{"rsnScriptIntrinsicBLAS_Double", "(JJIIIIIIIIIDJJDJIIII)V", (void*)nScriptIntrinsicBLAS_Double },
2936{"rsnScriptIntrinsicBLAS_Complex", "(JJIIIIIIIIIFFJJFFJIIII)V", (void*)nScriptIntrinsicBLAS_Complex },
2937{"rsnScriptIntrinsicBLAS_Z", "(JJIIIIIIIIIDDJJDDJIIII)V", (void*)nScriptIntrinsicBLAS_Z },
2938
Tim Murray9cb16a22015-04-01 11:07:16 -07002939{"rsnScriptIntrinsicBLAS_BNNM", "(JJIIIJIJIJII)V", (void*)nScriptIntrinsicBLAS_BNNM },
2940
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002941{"rsnProgramStoreCreate", "(JZZZZZZIII)J", (void*)nProgramStoreCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002942
Tim Murray460a0492013-11-19 12:45:54 -08002943{"rsnProgramBindConstants", "(JJIJ)V", (void*)nProgramBindConstants },
2944{"rsnProgramBindTexture", "(JJIJ)V", (void*)nProgramBindTexture },
2945{"rsnProgramBindSampler", "(JJIJ)V", (void*)nProgramBindSampler },
Jason Samsebfb4362009-09-23 13:57:02 -07002946
Ashok Bhat98071552014-02-12 09:54:43 +00002947{"rsnProgramFragmentCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramFragmentCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002948{"rsnProgramRasterCreate", "(JZI)J", (void*)nProgramRasterCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002949{"rsnProgramVertexCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramVertexCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002950
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002951{"rsnContextBindRootScript", "(JJ)V", (void*)nContextBindRootScript },
2952{"rsnContextBindProgramStore", "(JJ)V", (void*)nContextBindProgramStore },
2953{"rsnContextBindProgramFragment", "(JJ)V", (void*)nContextBindProgramFragment },
2954{"rsnContextBindProgramVertex", "(JJ)V", (void*)nContextBindProgramVertex },
2955{"rsnContextBindProgramRaster", "(JJ)V", (void*)nContextBindProgramRaster },
Jason Sams02fb2cb2009-05-28 15:37:57 -07002956
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002957{"rsnSamplerCreate", "(JIIIIIF)J", (void*)nSamplerCreate },
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002958
Ashok Bhat98071552014-02-12 09:54:43 +00002959{"rsnMeshCreate", "(J[J[J[I)J", (void*)nMeshCreate },
Jason Sams2e1872f2010-08-17 16:25:41 -07002960
Tim Murray460a0492013-11-19 12:45:54 -08002961{"rsnMeshGetVertexBufferCount", "(JJ)I", (void*)nMeshGetVertexBufferCount },
2962{"rsnMeshGetIndexCount", "(JJ)I", (void*)nMeshGetIndexCount },
Ashok Bhat98071552014-02-12 09:54:43 +00002963{"rsnMeshGetVertices", "(JJ[JI)V", (void*)nMeshGetVertices },
2964{"rsnMeshGetIndices", "(JJ[J[II)V", (void*)nMeshGetIndices },
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002965
Tim Murray56f9e6f2014-05-16 11:47:26 -07002966{"rsnSystemGetPointerSize", "()I", (void*)nSystemGetPointerSize },
Miao Wang0facf022015-11-25 11:21:13 -08002967{"rsnAllocationGetByteBuffer", "(JJ[JIII)Ljava/nio/ByteBuffer;", (void*)nAllocationGetByteBuffer },
Jason Samsd19f10d2009-05-22 14:03:28 -07002968};
2969
2970static int registerFuncs(JNIEnv *_env)
2971{
2972 return android::AndroidRuntime::registerNativeMethods(
2973 _env, classPathName, methods, NELEM(methods));
2974}
2975
2976// ---------------------------------------------------------------------------
2977
2978jint JNI_OnLoad(JavaVM* vm, void* reserved)
2979{
Chris Wailes488230c32014-08-14 11:22:40 -07002980 JNIEnv* env = nullptr;
Jason Samsd19f10d2009-05-22 14:03:28 -07002981 jint result = -1;
2982
Jason Samsd19f10d2009-05-22 14:03:28 -07002983 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
Steve Block3762c312012-01-06 19:20:56 +00002984 ALOGE("ERROR: GetEnv failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07002985 goto bail;
2986 }
Chris Wailes488230c32014-08-14 11:22:40 -07002987 assert(env != nullptr);
Jason Samsd19f10d2009-05-22 14:03:28 -07002988
2989 if (registerFuncs(env) < 0) {
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002990 ALOGE("ERROR: Renderscript native registration failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07002991 goto bail;
2992 }
2993
2994 /* success -- return valid version number */
2995 result = JNI_VERSION_1_4;
2996
2997bail:
2998 return result;
2999}