blob: ee48289731a8330d45e12ffa6509f4e18597880c [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>
Steven Morelanddc01e932017-05-01 12:56:08 -070030#include <android-base/macros.h>
Jason Samsf29ca502009-06-23 12:22:47 -070031
Jason Samsd19f10d2009-05-22 14:03:28 -070032#include "jni.h"
33#include "JNIHelp.h"
34#include "android_runtime/AndroidRuntime.h"
Jim Milleree956052010-08-19 18:56:00 -070035#include "android_runtime/android_view_Surface.h"
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -080036#include "android_runtime/android_util_AssetManager.h"
John Reckf4faeac2015-03-05 13:50:31 -080037#include "android/graphics/GraphicsJNI.h"
Miao Wang33287e82017-03-06 09:31:32 -080038#include "android/native_window.h"
39#include "android/native_window_jni.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070040
Jason Sams1d6983a2012-02-16 16:07:49 -080041#include <rsEnv.h>
Miao Wangcbb02062017-01-24 18:58:17 -080042#include <rsApiStubs.h>
Jason Samsfb9aa9f2012-03-28 15:30:07 -070043#include <gui/Surface.h>
Andy McFaddend47f7d82012-12-18 09:48:38 -080044#include <gui/GLConsumer.h>
Jason Samsfaa32b32011-06-20 16:58:04 -070045#include <android_runtime/android_graphics_SurfaceTexture.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070046
Steve Block3762c312012-01-06 19:20:56 +000047//#define LOG_API ALOGE
Andreas Gampe67333922014-11-10 20:35:59 -080048static constexpr bool kLogApi = false;
Jason Samsd19f10d2009-05-22 14:03:28 -070049
50using namespace android;
51
Stephen Hines414fa2c2014-04-17 01:02:42 -070052#define PER_ARRAY_TYPE(flag, fnc, readonly, ...) { \
Jason Samse729a942013-11-06 11:22:02 -080053 jint len = 0; \
Chris Wailes488230c32014-08-14 11:22:40 -070054 void *ptr = nullptr; \
Miao Wang87e908d2015-03-02 15:15:15 -080055 void *srcPtr = nullptr; \
Jason Sams21659ac2013-11-06 15:08:07 -080056 size_t typeBytes = 0; \
Stephen Hines414fa2c2014-04-17 01:02:42 -070057 jint relFlag = 0; \
58 if (readonly) { \
59 /* The on-release mode should only be JNI_ABORT for read-only accesses. */ \
Miao Wang87e908d2015-03-02 15:15:15 -080060 /* readonly = true, also indicates we are copying to the allocation . */ \
Stephen Hines414fa2c2014-04-17 01:02:42 -070061 relFlag = JNI_ABORT; \
62 } \
Jason Samse729a942013-11-06 11:22:02 -080063 switch(dataType) { \
64 case RS_TYPE_FLOAT_32: \
65 len = _env->GetArrayLength((jfloatArray)data); \
66 ptr = _env->GetFloatArrayElements((jfloatArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -070067 if (ptr == nullptr) { \
68 ALOGE("Failed to get Java array elements."); \
69 return; \
70 } \
Jason Sams21659ac2013-11-06 15:08:07 -080071 typeBytes = 4; \
Miao Wang87e908d2015-03-02 15:15:15 -080072 if (usePadding) { \
73 srcPtr = ptr; \
74 len = len / 3 * 4; \
75 if (count == 0) { \
76 count = len / 4; \
77 } \
78 ptr = malloc (len * typeBytes); \
79 if (readonly) { \
80 copyWithPadding(ptr, srcPtr, mSize, count); \
81 fnc(__VA_ARGS__); \
82 } else { \
83 fnc(__VA_ARGS__); \
84 copyWithUnPadding(srcPtr, ptr, mSize, count); \
85 } \
86 free(ptr); \
87 ptr = srcPtr; \
88 } else { \
89 fnc(__VA_ARGS__); \
90 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -070091 _env->ReleaseFloatArrayElements((jfloatArray)data, (jfloat *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -080092 return; \
93 case RS_TYPE_FLOAT_64: \
94 len = _env->GetArrayLength((jdoubleArray)data); \
95 ptr = _env->GetDoubleArrayElements((jdoubleArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -070096 if (ptr == nullptr) { \
97 ALOGE("Failed to get Java array elements."); \
98 return; \
99 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800100 typeBytes = 8; \
Miao Wang87e908d2015-03-02 15:15:15 -0800101 if (usePadding) { \
102 srcPtr = ptr; \
103 len = len / 3 * 4; \
104 if (count == 0) { \
105 count = len / 4; \
106 } \
107 ptr = malloc (len * typeBytes); \
108 if (readonly) { \
109 copyWithPadding(ptr, srcPtr, mSize, count); \
110 fnc(__VA_ARGS__); \
111 } else { \
112 fnc(__VA_ARGS__); \
113 copyWithUnPadding(srcPtr, ptr, mSize, count); \
114 } \
115 free(ptr); \
116 ptr = srcPtr; \
117 } else { \
118 fnc(__VA_ARGS__); \
119 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700120 _env->ReleaseDoubleArrayElements((jdoubleArray)data, (jdouble *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800121 return; \
122 case RS_TYPE_SIGNED_8: \
123 case RS_TYPE_UNSIGNED_8: \
124 len = _env->GetArrayLength((jbyteArray)data); \
125 ptr = _env->GetByteArrayElements((jbyteArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700126 if (ptr == nullptr) { \
127 ALOGE("Failed to get Java array elements."); \
128 return; \
129 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800130 typeBytes = 1; \
Miao Wang87e908d2015-03-02 15:15:15 -0800131 if (usePadding) { \
132 srcPtr = ptr; \
133 len = len / 3 * 4; \
134 if (count == 0) { \
135 count = len / 4; \
136 } \
137 ptr = malloc (len * typeBytes); \
138 if (readonly) { \
139 copyWithPadding(ptr, srcPtr, mSize, count); \
140 fnc(__VA_ARGS__); \
141 } else { \
142 fnc(__VA_ARGS__); \
143 copyWithUnPadding(srcPtr, ptr, mSize, count); \
144 } \
145 free(ptr); \
146 ptr = srcPtr; \
147 } else { \
148 fnc(__VA_ARGS__); \
149 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700150 _env->ReleaseByteArrayElements((jbyteArray)data, (jbyte*)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800151 return; \
152 case RS_TYPE_SIGNED_16: \
153 case RS_TYPE_UNSIGNED_16: \
Pirama Arumuga Nainar13332152016-03-01 20:37:19 -0800154 case RS_TYPE_FLOAT_16: \
Jason Samse729a942013-11-06 11:22:02 -0800155 len = _env->GetArrayLength((jshortArray)data); \
156 ptr = _env->GetShortArrayElements((jshortArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700157 if (ptr == nullptr) { \
158 ALOGE("Failed to get Java array elements."); \
159 return; \
160 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800161 typeBytes = 2; \
Miao Wang87e908d2015-03-02 15:15:15 -0800162 if (usePadding) { \
163 srcPtr = ptr; \
164 len = len / 3 * 4; \
165 if (count == 0) { \
166 count = len / 4; \
167 } \
168 ptr = malloc (len * typeBytes); \
169 if (readonly) { \
170 copyWithPadding(ptr, srcPtr, mSize, count); \
171 fnc(__VA_ARGS__); \
172 } else { \
173 fnc(__VA_ARGS__); \
174 copyWithUnPadding(srcPtr, ptr, mSize, count); \
175 } \
176 free(ptr); \
177 ptr = srcPtr; \
178 } else { \
179 fnc(__VA_ARGS__); \
180 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700181 _env->ReleaseShortArrayElements((jshortArray)data, (jshort *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800182 return; \
183 case RS_TYPE_SIGNED_32: \
184 case RS_TYPE_UNSIGNED_32: \
185 len = _env->GetArrayLength((jintArray)data); \
186 ptr = _env->GetIntArrayElements((jintArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700187 if (ptr == nullptr) { \
188 ALOGE("Failed to get Java array elements."); \
189 return; \
190 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800191 typeBytes = 4; \
Miao Wang87e908d2015-03-02 15:15:15 -0800192 if (usePadding) { \
193 srcPtr = ptr; \
194 len = len / 3 * 4; \
195 if (count == 0) { \
196 count = len / 4; \
197 } \
198 ptr = malloc (len * typeBytes); \
199 if (readonly) { \
200 copyWithPadding(ptr, srcPtr, mSize, count); \
201 fnc(__VA_ARGS__); \
202 } else { \
203 fnc(__VA_ARGS__); \
204 copyWithUnPadding(srcPtr, ptr, mSize, count); \
205 } \
206 free(ptr); \
207 ptr = srcPtr; \
208 } else { \
209 fnc(__VA_ARGS__); \
210 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700211 _env->ReleaseIntArrayElements((jintArray)data, (jint *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800212 return; \
213 case RS_TYPE_SIGNED_64: \
214 case RS_TYPE_UNSIGNED_64: \
215 len = _env->GetArrayLength((jlongArray)data); \
216 ptr = _env->GetLongArrayElements((jlongArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700217 if (ptr == nullptr) { \
218 ALOGE("Failed to get Java array elements."); \
219 return; \
220 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800221 typeBytes = 8; \
Miao Wang87e908d2015-03-02 15:15:15 -0800222 if (usePadding) { \
223 srcPtr = ptr; \
224 len = len / 3 * 4; \
225 if (count == 0) { \
226 count = len / 4; \
227 } \
228 ptr = malloc (len * typeBytes); \
229 if (readonly) { \
230 copyWithPadding(ptr, srcPtr, mSize, count); \
231 fnc(__VA_ARGS__); \
232 } else { \
233 fnc(__VA_ARGS__); \
234 copyWithUnPadding(srcPtr, ptr, mSize, count); \
235 } \
236 free(ptr); \
237 ptr = srcPtr; \
238 } else { \
239 fnc(__VA_ARGS__); \
240 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700241 _env->ReleaseLongArrayElements((jlongArray)data, (jlong *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800242 return; \
243 default: \
244 break; \
245 } \
Miao Wang87e908d2015-03-02 15:15:15 -0800246 UNUSED(len, ptr, srcPtr, typeBytes, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800247}
248
249
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800250class AutoJavaStringToUTF8 {
251public:
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800252 AutoJavaStringToUTF8(JNIEnv* env, jstring str) : fEnv(env), fJStr(str) {
Chris Wailes488230c32014-08-14 11:22:40 -0700253 fCStr = env->GetStringUTFChars(str, nullptr);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800254 fLength = env->GetStringUTFLength(str);
255 }
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800256 ~AutoJavaStringToUTF8() {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800257 fEnv->ReleaseStringUTFChars(fJStr, fCStr);
258 }
259 const char* c_str() const { return fCStr; }
260 jsize length() const { return fLength; }
261
262private:
263 JNIEnv* fEnv;
264 jstring fJStr;
265 const char* fCStr;
266 jsize fLength;
267};
268
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800269class AutoJavaStringArrayToUTF8 {
270public:
271 AutoJavaStringArrayToUTF8(JNIEnv* env, jobjectArray strings, jsize stringsLength)
272 : mEnv(env), mStrings(strings), mStringsLength(stringsLength) {
Chris Wailes488230c32014-08-14 11:22:40 -0700273 mCStrings = nullptr;
274 mSizeArray = nullptr;
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800275 if (stringsLength > 0) {
276 mCStrings = (const char **)calloc(stringsLength, sizeof(char *));
277 mSizeArray = (size_t*)calloc(stringsLength, sizeof(size_t));
278 for (jsize ct = 0; ct < stringsLength; ct ++) {
279 jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
Chris Wailes488230c32014-08-14 11:22:40 -0700280 mCStrings[ct] = mEnv->GetStringUTFChars(s, nullptr);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800281 mSizeArray[ct] = mEnv->GetStringUTFLength(s);
282 }
283 }
284 }
285 ~AutoJavaStringArrayToUTF8() {
286 for (jsize ct=0; ct < mStringsLength; ct++) {
287 jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
288 mEnv->ReleaseStringUTFChars(s, mCStrings[ct]);
289 }
290 free(mCStrings);
291 free(mSizeArray);
292 }
293 const char **c_str() const { return mCStrings; }
294 size_t *c_str_len() const { return mSizeArray; }
295 jsize length() const { return mStringsLength; }
296
297private:
298 JNIEnv *mEnv;
299 jobjectArray mStrings;
300 const char **mCStrings;
301 size_t *mSizeArray;
302 jsize mStringsLength;
303};
304
Jason Samsd19f10d2009-05-22 14:03:28 -0700305// ---------------------------------------------------------------------------
306
Jason Samsffe9f482009-06-01 17:45:53 -0700307static jfieldID gContextId = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -0700308
309static void _nInit(JNIEnv *_env, jclass _this)
310{
Tim Murrayeff663f2013-11-15 13:08:30 -0800311 gContextId = _env->GetFieldID(_this, "mContext", "J");
Jason Samsd19f10d2009-05-22 14:03:28 -0700312}
313
Jason Samsd19f10d2009-05-22 14:03:28 -0700314// ---------------------------------------------------------------------------
315
Miao Wang87e908d2015-03-02 15:15:15 -0800316static void copyWithPadding(void* ptr, void* srcPtr, int mSize, int count) {
317 int sizeBytesPad = mSize * 4;
318 int sizeBytes = mSize * 3;
319 uint8_t *dst = static_cast<uint8_t *>(ptr);
320 uint8_t *src = static_cast<uint8_t *>(srcPtr);
321 for (int i = 0; i < count; i++) {
322 memcpy(dst, src, sizeBytes);
323 dst += sizeBytesPad;
324 src += sizeBytes;
325 }
326}
327
328static void copyWithUnPadding(void* ptr, void* srcPtr, int mSize, int count) {
329 int sizeBytesPad = mSize * 4;
330 int sizeBytes = mSize * 3;
331 uint8_t *dst = static_cast<uint8_t *>(ptr);
332 uint8_t *src = static_cast<uint8_t *>(srcPtr);
333 for (int i = 0; i < count; i++) {
334 memcpy(dst, src, sizeBytes);
335 dst += sizeBytes;
336 src += sizeBytesPad;
337 }
338}
339
340
341// ---------------------------------------------------------------------------
Jason Sams3eaa3382009-06-10 15:04:38 -0700342static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800343nContextFinish(JNIEnv *_env, jobject _this, jlong con)
Jason Sams96ed4cf2010-06-15 12:15:57 -0700344{
Andreas Gampe67333922014-11-10 20:35:59 -0800345 if (kLogApi) {
346 ALOGD("nContextFinish, con(%p)", (RsContext)con);
347 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800348 rsContextFinish((RsContext)con);
Jason Sams96ed4cf2010-06-15 12:15:57 -0700349}
350
Yang Ni281c3252014-10-24 08:52:24 -0700351static jlong
352nClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong kernelID,
353 jlong returnValue, jlongArray fieldIDArray,
354 jlongArray valueArray, jintArray sizeArray,
355 jlongArray depClosureArray, jlongArray depFieldIDArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700356 jlong ret = 0;
357
Yang Ni281c3252014-10-24 08:52:24 -0700358 jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
359 jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700360 if (jFieldIDs == nullptr) {
361 ALOGE("Failed to get Java array elements: fieldIDs.");
362 return ret;
363 }
364
Yang Ni281c3252014-10-24 08:52:24 -0700365 jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
366 jsize values_length = _env->GetArrayLength(valueArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700367 if (jValues == nullptr) {
368 ALOGE("Failed to get Java array elements: values.");
369 return ret;
370 }
371
Yang Ni17c2d7a2015-04-30 16:13:54 -0700372 jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
Yang Ni281c3252014-10-24 08:52:24 -0700373 jsize sizes_length = _env->GetArrayLength(sizeArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700374 if (jSizes == nullptr) {
375 ALOGE("Failed to get Java array elements: sizes.");
376 return ret;
377 }
378
Yang Ni281c3252014-10-24 08:52:24 -0700379 jlong* jDepClosures =
380 _env->GetLongArrayElements(depClosureArray, nullptr);
381 jsize depClosures_length = _env->GetArrayLength(depClosureArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700382 if (jDepClosures == nullptr) {
383 ALOGE("Failed to get Java array elements: depClosures.");
384 return ret;
385 }
386
Yang Ni281c3252014-10-24 08:52:24 -0700387 jlong* jDepFieldIDs =
388 _env->GetLongArrayElements(depFieldIDArray, nullptr);
389 jsize depFieldIDs_length = _env->GetArrayLength(depFieldIDArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700390 if (jDepFieldIDs == nullptr) {
391 ALOGE("Failed to get Java array elements: depFieldIDs.");
392 return ret;
393 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700394
395 size_t numValues, numDependencies;
396 RsScriptFieldID* fieldIDs;
Yang Ni17c2d7a2015-04-30 16:13:54 -0700397 RsClosure* depClosures;
398 RsScriptFieldID* depFieldIDs;
399
400 if (fieldIDs_length != values_length || values_length != sizes_length) {
401 ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
402 goto exit;
403 }
404
405 numValues = (size_t)fieldIDs_length;
406
407 if (depClosures_length != depFieldIDs_length) {
408 ALOGE("Unmatched closures and field IDs for dependencies in closure creation.");
409 goto exit;
410 }
411
412 numDependencies = (size_t)depClosures_length;
413
414 if (numDependencies > numValues) {
415 ALOGE("Unexpected number of dependencies in closure creation");
416 goto exit;
417 }
418
Yang Ni7b2a46f2015-05-05 12:41:19 -0700419 if (numValues > RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700420 ALOGE("Too many arguments or globals in closure creation");
421 goto exit;
422 }
423
Yang Ni86c5c2d2016-03-25 15:49:07 -0700424 if (numValues > 0) {
425 fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
426 if (fieldIDs == nullptr) {
427 goto exit;
428 }
429 } else {
430 // numValues == 0
431 // alloca(0) implementation is platform-dependent.
432 fieldIDs = nullptr;
Yang Ni17c2d7a2015-04-30 16:13:54 -0700433 }
434
435 for (size_t i = 0; i < numValues; i++) {
436 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
437 }
438
Yang Ni86c5c2d2016-03-25 15:49:07 -0700439 if (numDependencies > 0) {
440 depClosures = (RsClosure*)alloca(sizeof(RsClosure) * numDependencies);
441 if (depClosures == nullptr) {
442 goto exit;
443 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700444
Yang Ni86c5c2d2016-03-25 15:49:07 -0700445 for (size_t i = 0; i < numDependencies; i++) {
446 depClosures[i] = (RsClosure)jDepClosures[i];
447 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700448
Yang Ni86c5c2d2016-03-25 15:49:07 -0700449 depFieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numDependencies);
450 if (depFieldIDs == nullptr) {
451 goto exit;
452 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700453
Yang Ni86c5c2d2016-03-25 15:49:07 -0700454 for (size_t i = 0; i < numDependencies; i++) {
455 depFieldIDs[i] = (RsClosure)jDepFieldIDs[i];
456 }
457 } else {
458 // alloca(0) implementation is platform-dependent.
459 depClosures = nullptr;
460 depFieldIDs = nullptr;
Yang Ni281c3252014-10-24 08:52:24 -0700461 }
462
Yang Ni17c2d7a2015-04-30 16:13:54 -0700463 ret = (jlong)(uintptr_t)rsClosureCreate(
Yang Ni281c3252014-10-24 08:52:24 -0700464 (RsContext)con, (RsScriptKernelID)kernelID, (RsAllocation)returnValue,
Yang Ni263cc902015-11-10 13:27:04 -0800465 fieldIDs, numValues, jValues, numValues,
Yang Ni17c2d7a2015-04-30 16:13:54 -0700466 (int*)jSizes, numValues,
467 depClosures, numDependencies,
468 depFieldIDs, numDependencies);
469
470exit:
471
472 _env->ReleaseLongArrayElements(depFieldIDArray, jDepFieldIDs, JNI_ABORT);
473 _env->ReleaseLongArrayElements(depClosureArray, jDepClosures, JNI_ABORT);
474 _env->ReleaseIntArrayElements (sizeArray, jSizes, JNI_ABORT);
475 _env->ReleaseLongArrayElements(valueArray, jValues, JNI_ABORT);
476 _env->ReleaseLongArrayElements(fieldIDArray, jFieldIDs, JNI_ABORT);
477
478 return ret;
Yang Ni281c3252014-10-24 08:52:24 -0700479}
480
Yang Nibe392ad2015-01-23 17:16:02 -0800481static jlong
482nInvokeClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong invokeID,
483 jbyteArray paramArray, jlongArray fieldIDArray, jlongArray valueArray,
484 jintArray sizeArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700485 jlong ret = 0;
486
Yang Nibe392ad2015-01-23 17:16:02 -0800487 jbyte* jParams = _env->GetByteArrayElements(paramArray, nullptr);
488 jsize jParamLength = _env->GetArrayLength(paramArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700489 if (jParams == nullptr) {
490 ALOGE("Failed to get Java array elements: params.");
491 return ret;
492 }
493
Yang Nibe392ad2015-01-23 17:16:02 -0800494 jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
495 jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700496 if (jFieldIDs == nullptr) {
497 ALOGE("Failed to get Java array elements: fieldIDs.");
498 return ret;
499 }
500
Yang Ni17c2d7a2015-04-30 16:13:54 -0700501 jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
502 jsize values_length = _env->GetArrayLength(valueArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700503 if (jValues == nullptr) {
504 ALOGE("Failed to get Java array elements: values.");
505 return ret;
506 }
507
Yang Ni17c2d7a2015-04-30 16:13:54 -0700508 jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
509 jsize sizes_length = _env->GetArrayLength(sizeArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700510 if (jSizes == nullptr) {
511 ALOGE("Failed to get Java array elements: sizes.");
512 return ret;
513 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700514
515 size_t numValues;
516 RsScriptFieldID* fieldIDs;
Yang Ni17c2d7a2015-04-30 16:13:54 -0700517
518 if (fieldIDs_length != values_length || values_length != sizes_length) {
519 ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
520 goto exit;
521 }
522
523 numValues = (size_t) fieldIDs_length;
524
Yang Ni7b2a46f2015-05-05 12:41:19 -0700525 if (numValues > RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700526 ALOGE("Too many arguments or globals in closure creation");
527 goto exit;
528 }
529
530 fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
531 if (fieldIDs == nullptr) {
532 goto exit;
533 }
534
535 for (size_t i = 0; i< numValues; i++) {
Yang Nibe392ad2015-01-23 17:16:02 -0800536 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
537 }
538
Yang Ni17c2d7a2015-04-30 16:13:54 -0700539 ret = (jlong)(uintptr_t)rsInvokeClosureCreate(
Yang Nibe392ad2015-01-23 17:16:02 -0800540 (RsContext)con, (RsScriptInvokeID)invokeID, jParams, jParamLength,
Yang Ni263cc902015-11-10 13:27:04 -0800541 fieldIDs, numValues, jValues, numValues,
Yang Ni17c2d7a2015-04-30 16:13:54 -0700542 (int*)jSizes, numValues);
543
544exit:
545
546 _env->ReleaseIntArrayElements (sizeArray, jSizes, JNI_ABORT);
547 _env->ReleaseLongArrayElements(valueArray, jValues, JNI_ABORT);
548 _env->ReleaseLongArrayElements(fieldIDArray, jFieldIDs, JNI_ABORT);
549 _env->ReleaseByteArrayElements(paramArray, jParams, JNI_ABORT);
550
551 return ret;
Yang Nibe392ad2015-01-23 17:16:02 -0800552}
553
Yang Ni281c3252014-10-24 08:52:24 -0700554static void
555nClosureSetArg(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
556 jint index, jlong value, jint size) {
Yang Ni263cc902015-11-10 13:27:04 -0800557 // Size is signed with -1 indicating the value is an Allocation
Yang Ni281c3252014-10-24 08:52:24 -0700558 rsClosureSetArg((RsContext)con, (RsClosure)closureID, (uint32_t)index,
Yang Ni263cc902015-11-10 13:27:04 -0800559 (uintptr_t)value, size);
Yang Ni281c3252014-10-24 08:52:24 -0700560}
561
562static void
563nClosureSetGlobal(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
564 jlong fieldID, jlong value, jint size) {
Yang Ni263cc902015-11-10 13:27:04 -0800565 // Size is signed with -1 indicating the value is an Allocation
Yang Ni281c3252014-10-24 08:52:24 -0700566 rsClosureSetGlobal((RsContext)con, (RsClosure)closureID,
Yang Ni263cc902015-11-10 13:27:04 -0800567 (RsScriptFieldID)fieldID, (int64_t)value, size);
Yang Ni281c3252014-10-24 08:52:24 -0700568}
569
570static long
Yang Ni35be56c2015-04-02 17:47:56 -0700571nScriptGroup2Create(JNIEnv *_env, jobject _this, jlong con, jstring name,
Yang Niebf63402015-01-16 11:06:26 -0800572 jstring cacheDir, jlongArray closureArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700573 jlong ret = 0;
574
Yang Ni35be56c2015-04-02 17:47:56 -0700575 AutoJavaStringToUTF8 nameUTF(_env, name);
Yang Niebf63402015-01-16 11:06:26 -0800576 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
577
Yang Ni281c3252014-10-24 08:52:24 -0700578 jlong* jClosures = _env->GetLongArrayElements(closureArray, nullptr);
579 jsize numClosures = _env->GetArrayLength(closureArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700580 if (jClosures == nullptr) {
581 ALOGE("Failed to get Java array elements: closures.");
582 return ret;
583 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700584
585 RsClosure* closures;
586
Yang Ni7b2a46f2015-05-05 12:41:19 -0700587 if (numClosures > (jsize) RS_SCRIPT_GROUP_MAX_NUMBER_CLOSURES) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700588 ALOGE("Too many closures in script group");
589 goto exit;
590 }
591
592 closures = (RsClosure*)alloca(sizeof(RsClosure) * numClosures);
593 if (closures == nullptr) {
594 goto exit;
595 }
596
Yang Ni281c3252014-10-24 08:52:24 -0700597 for (int i = 0; i < numClosures; i++) {
598 closures[i] = (RsClosure)jClosures[i];
599 }
600
Yang Ni17c2d7a2015-04-30 16:13:54 -0700601 ret = (jlong)(uintptr_t)rsScriptGroup2Create(
Yang Ni35be56c2015-04-02 17:47:56 -0700602 (RsContext)con, nameUTF.c_str(), nameUTF.length(),
603 cacheDirUTF.c_str(), cacheDirUTF.length(),
Yang Niebf63402015-01-16 11:06:26 -0800604 closures, numClosures);
Yang Ni17c2d7a2015-04-30 16:13:54 -0700605
606exit:
607
608 _env->ReleaseLongArrayElements(closureArray, jClosures, JNI_ABORT);
609
610 return ret;
Yang Ni281c3252014-10-24 08:52:24 -0700611}
612
613static void
614nScriptGroup2Execute(JNIEnv *_env, jobject _this, jlong con, jlong groupID) {
615 rsScriptGroupExecute((RsContext)con, (RsScriptGroup2)groupID);
616}
617
Jason Sams96ed4cf2010-06-15 12:15:57 -0700618static void
Tim Murray25207df2015-01-12 16:47:56 -0800619nScriptIntrinsicBLAS_Single(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
620 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
621 jfloat alpha, jlong A, jlong B, jfloat beta, jlong C, jint incX, jint incY,
622 jint KL, jint KU) {
623 RsBlasCall call;
624 memset(&call, 0, sizeof(call));
625 call.func = (RsBlasFunction)func;
626 call.transA = (RsBlasTranspose)TransA;
627 call.transB = (RsBlasTranspose)TransB;
628 call.side = (RsBlasSide)Side;
629 call.uplo = (RsBlasUplo)Uplo;
630 call.diag = (RsBlasDiag)Diag;
631 call.M = M;
632 call.N = N;
633 call.K = K;
634 call.alpha.f = alpha;
635 call.beta.f = beta;
636 call.incX = incX;
637 call.incY = incY;
638 call.KL = KL;
639 call.KU = KU;
640
641 RsAllocation in_allocs[3];
642 in_allocs[0] = (RsAllocation)A;
643 in_allocs[1] = (RsAllocation)B;
644 in_allocs[2] = (RsAllocation)C;
645
646 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wangb742fcc2016-10-06 10:45:42 -0700647 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800648 &call, sizeof(call), nullptr, 0);
649}
650
651static void
652nScriptIntrinsicBLAS_Double(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
653 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
654 jdouble alpha, jlong A, jlong B, jdouble beta, jlong C, jint incX, jint incY,
655 jint KL, jint KU) {
656 RsBlasCall call;
657 memset(&call, 0, sizeof(call));
658 call.func = (RsBlasFunction)func;
659 call.transA = (RsBlasTranspose)TransA;
660 call.transB = (RsBlasTranspose)TransB;
661 call.side = (RsBlasSide)Side;
662 call.uplo = (RsBlasUplo)Uplo;
663 call.diag = (RsBlasDiag)Diag;
664 call.M = M;
665 call.N = N;
666 call.K = K;
667 call.alpha.d = alpha;
668 call.beta.d = beta;
669 call.incX = incX;
670 call.incY = incY;
671 call.KL = KL;
672 call.KU = KU;
673
674 RsAllocation in_allocs[3];
675 in_allocs[0] = (RsAllocation)A;
676 in_allocs[1] = (RsAllocation)B;
677 in_allocs[2] = (RsAllocation)C;
678
679 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700680 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800681 &call, sizeof(call), nullptr, 0);
682}
683
684static void
685nScriptIntrinsicBLAS_Complex(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
686 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
687 jfloat alphaX, jfloat alphaY, jlong A, jlong B, jfloat betaX,
688 jfloat betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
689 RsBlasCall call;
690 memset(&call, 0, sizeof(call));
691 call.func = (RsBlasFunction)func;
692 call.transA = (RsBlasTranspose)TransA;
693 call.transB = (RsBlasTranspose)TransB;
694 call.side = (RsBlasSide)Side;
695 call.uplo = (RsBlasUplo)Uplo;
696 call.diag = (RsBlasDiag)Diag;
697 call.M = M;
698 call.N = N;
699 call.K = K;
700 call.alpha.c.r = alphaX;
701 call.alpha.c.i = alphaY;
702 call.beta.c.r = betaX;
Miao Wang82585b32015-04-30 13:44:49 -0700703 call.beta.c.i = betaY;
Tim Murray25207df2015-01-12 16:47:56 -0800704 call.incX = incX;
705 call.incY = incY;
706 call.KL = KL;
707 call.KU = KU;
708
709 RsAllocation in_allocs[3];
710 in_allocs[0] = (RsAllocation)A;
711 in_allocs[1] = (RsAllocation)B;
712 in_allocs[2] = (RsAllocation)C;
713
714 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700715 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800716 &call, sizeof(call), nullptr, 0);
717}
718
719static void
720nScriptIntrinsicBLAS_Z(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
721 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
722 jdouble alphaX, jdouble alphaY, jlong A, jlong B, jdouble betaX,
723 jdouble betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
724 RsBlasCall call;
725 memset(&call, 0, sizeof(call));
726 call.func = (RsBlasFunction)func;
727 call.transA = (RsBlasTranspose)TransA;
728 call.transB = (RsBlasTranspose)TransB;
729 call.side = (RsBlasSide)Side;
730 call.uplo = (RsBlasUplo)Uplo;
731 call.diag = (RsBlasDiag)Diag;
732 call.M = M;
733 call.N = N;
734 call.K = K;
735 call.alpha.z.r = alphaX;
736 call.alpha.z.i = alphaY;
737 call.beta.z.r = betaX;
Miao Wang82585b32015-04-30 13:44:49 -0700738 call.beta.z.i = betaY;
Tim Murray25207df2015-01-12 16:47:56 -0800739 call.incX = incX;
740 call.incY = incY;
741 call.KL = KL;
742 call.KU = KU;
743
744 RsAllocation in_allocs[3];
745 in_allocs[0] = (RsAllocation)A;
746 in_allocs[1] = (RsAllocation)B;
747 in_allocs[2] = (RsAllocation)C;
748
749 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700750 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800751 &call, sizeof(call), nullptr, 0);
752}
753
754
755static void
Tim Murray9cb16a22015-04-01 11:07:16 -0700756nScriptIntrinsicBLAS_BNNM(JNIEnv *_env, jobject _this, jlong con, jlong id, jint M, jint N, jint K,
757 jlong A, jint a_offset, jlong B, jint b_offset, jlong C, jint c_offset,
758 jint c_mult_int) {
759 RsBlasCall call;
760 memset(&call, 0, sizeof(call));
761 call.func = RsBlas_bnnm;
762 call.M = M;
763 call.N = N;
764 call.K = K;
Miao Wang25148062015-06-29 17:43:03 -0700765 call.a_offset = a_offset & 0xFF;
766 call.b_offset = b_offset & 0xFF;
Tim Murray9cb16a22015-04-01 11:07:16 -0700767 call.c_offset = c_offset;
768 call.c_mult_int = c_mult_int;
769
770 RsAllocation in_allocs[3];
771 in_allocs[0] = (RsAllocation)A;
772 in_allocs[1] = (RsAllocation)B;
773 in_allocs[2] = (RsAllocation)C;
774
775 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700776 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray9cb16a22015-04-01 11:07:16 -0700777 &call, sizeof(call), nullptr, 0);
778}
779
780
781static void
Tim Murray460a0492013-11-19 12:45:54 -0800782nAssignName(JNIEnv *_env, jobject _this, jlong con, jlong obj, jbyteArray str)
Jason Sams3eaa3382009-06-10 15:04:38 -0700783{
Andreas Gampe67333922014-11-10 20:35:59 -0800784 if (kLogApi) {
785 ALOGD("nAssignName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
786 }
Jason Sams3eaa3382009-06-10 15:04:38 -0700787 jint len = _env->GetArrayLength(str);
788 jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
Miao Wangba8766c2015-10-12 17:24:13 -0700789 if (cptr == nullptr) {
790 ALOGE("Failed to get Java array elements");
791 return;
792 }
793
Tim Murrayeff663f2013-11-15 13:08:30 -0800794 rsAssignName((RsContext)con, (void *)obj, (const char *)cptr, len);
Jason Sams3eaa3382009-06-10 15:04:38 -0700795 _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
796}
797
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700798static jstring
Tim Murray460a0492013-11-19 12:45:54 -0800799nGetName(JNIEnv *_env, jobject _this, jlong con, jlong obj)
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700800{
Andreas Gampe67333922014-11-10 20:35:59 -0800801 if (kLogApi) {
802 ALOGD("nGetName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
803 }
Chris Wailes488230c32014-08-14 11:22:40 -0700804 const char *name = nullptr;
Tim Murrayeff663f2013-11-15 13:08:30 -0800805 rsaGetName((RsContext)con, (void *)obj, &name);
Chris Wailes488230c32014-08-14 11:22:40 -0700806 if(name == nullptr || strlen(name) == 0) {
807 return nullptr;
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700808 }
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700809 return _env->NewStringUTF(name);
810}
811
Jason Sams7ce033d2009-08-18 14:14:24 -0700812static void
Tim Murray460a0492013-11-19 12:45:54 -0800813nObjDestroy(JNIEnv *_env, jobject _this, jlong con, jlong obj)
Jason Sams7ce033d2009-08-18 14:14:24 -0700814{
Andreas Gampe67333922014-11-10 20:35:59 -0800815 if (kLogApi) {
816 ALOGD("nObjDestroy, con(%p) obj(%p)", (RsContext)con, (void *)obj);
817 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800818 rsObjDestroy((RsContext)con, (void *)obj);
Jason Sams7ce033d2009-08-18 14:14:24 -0700819}
820
Jason Sams3eaa3382009-06-10 15:04:38 -0700821// ---------------------------------------------------------------------------
822
Tim Murrayeff663f2013-11-15 13:08:30 -0800823static jlong
Jason Samsd19f10d2009-05-22 14:03:28 -0700824nDeviceCreate(JNIEnv *_env, jobject _this)
825{
Andreas Gampe67333922014-11-10 20:35:59 -0800826 if (kLogApi) {
827 ALOGD("nDeviceCreate");
828 }
Tim Murray3aa89c12014-08-18 17:51:22 -0700829 return (jlong)(uintptr_t)rsDeviceCreate();
Jason Samsd19f10d2009-05-22 14:03:28 -0700830}
831
832static void
Tim Murray5eaf4682014-01-10 11:25:52 -0800833nDeviceDestroy(JNIEnv *_env, jobject _this, jlong dev)
Jason Samsd19f10d2009-05-22 14:03:28 -0700834{
Andreas Gampe67333922014-11-10 20:35:59 -0800835 if (kLogApi) {
836 ALOGD("nDeviceDestroy");
837 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700838 return rsDeviceDestroy((RsDevice)dev);
839}
840
Jason Samsebfb4362009-09-23 13:57:02 -0700841static void
Tim Murray5eaf4682014-01-10 11:25:52 -0800842nDeviceSetConfig(JNIEnv *_env, jobject _this, jlong dev, jint p, jint value)
Jason Samsebfb4362009-09-23 13:57:02 -0700843{
Andreas Gampe67333922014-11-10 20:35:59 -0800844 if (kLogApi) {
845 ALOGD("nDeviceSetConfig dev(%p), param(%i), value(%i)", (void *)dev, p, value);
846 }
Jason Samsebfb4362009-09-23 13:57:02 -0700847 return rsDeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value);
848}
849
Tim Murrayeff663f2013-11-15 13:08:30 -0800850static jlong
Jason Sams81cd2b12014-12-02 12:36:43 -0800851nContextCreate(JNIEnv *_env, jobject _this, jlong dev, jint flags, jint sdkVer, jint contextType)
Jason Samsd19f10d2009-05-22 14:03:28 -0700852{
Andreas Gampe67333922014-11-10 20:35:59 -0800853 if (kLogApi) {
854 ALOGD("nContextCreate");
855 }
Jason Sams81cd2b12014-12-02 12:36:43 -0800856 return (jlong)(uintptr_t)rsContextCreate((RsDevice)dev, 0, sdkVer, (RsContextType)contextType, flags);
Jason Sams704ff642010-02-09 16:05:07 -0800857}
858
Tim Murrayeff663f2013-11-15 13:08:30 -0800859static jlong
Tim Murray5eaf4682014-01-10 11:25:52 -0800860nContextCreateGL(JNIEnv *_env, jobject _this, jlong dev, jint ver, jint sdkVer,
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000861 jint colorMin, jint colorPref,
862 jint alphaMin, jint alphaPref,
863 jint depthMin, jint depthPref,
864 jint stencilMin, jint stencilPref,
865 jint samplesMin, jint samplesPref, jfloat samplesQ,
866 jint dpi)
Jason Sams704ff642010-02-09 16:05:07 -0800867{
Yang Ni86c5c2d2016-03-25 15:49:07 -0700868 RsSurfaceConfig sc = {};
Jason Sams11c8af92010-10-13 15:31:10 -0700869 sc.alphaMin = alphaMin;
870 sc.alphaPref = alphaPref;
871 sc.colorMin = colorMin;
872 sc.colorPref = colorPref;
873 sc.depthMin = depthMin;
874 sc.depthPref = depthPref;
875 sc.samplesMin = samplesMin;
876 sc.samplesPref = samplesPref;
877 sc.samplesQ = samplesQ;
878
Andreas Gampe67333922014-11-10 20:35:59 -0800879 if (kLogApi) {
880 ALOGD("nContextCreateGL");
881 }
Tim Murray3aa89c12014-08-18 17:51:22 -0700882 return (jlong)(uintptr_t)rsContextCreateGL((RsDevice)dev, ver, sdkVer, sc, dpi);
Jason Samsd19f10d2009-05-22 14:03:28 -0700883}
884
885static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800886nContextSetPriority(JNIEnv *_env, jobject _this, jlong con, jint p)
Jason Sams7d787b42009-11-15 12:14:26 -0800887{
Andreas Gampe67333922014-11-10 20:35:59 -0800888 if (kLogApi) {
889 ALOGD("ContextSetPriority, con(%p), priority(%i)", (RsContext)con, p);
890 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800891 rsContextSetPriority((RsContext)con, p);
Jason Sams7d787b42009-11-15 12:14:26 -0800892}
893
Tim Murray47f31582015-04-07 15:43:24 -0700894static void
895nContextSetCacheDir(JNIEnv *_env, jobject _this, jlong con, jstring cacheDir)
896{
897 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
898
899 if (kLogApi) {
900 ALOGD("ContextSetCacheDir, con(%p), cacheDir(%s)", (RsContext)con, cacheDirUTF.c_str());
901 }
902 rsContextSetCacheDir((RsContext)con, cacheDirUTF.c_str(), cacheDirUTF.length());
903}
904
Jason Sams7d787b42009-11-15 12:14:26 -0800905
906
907static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800908nContextSetSurface(JNIEnv *_env, jobject _this, jlong con, jint width, jint height, jobject wnd)
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800909{
Andreas Gampe67333922014-11-10 20:35:59 -0800910 if (kLogApi) {
911 ALOGD("nContextSetSurface, con(%p), width(%i), height(%i), surface(%p)", (RsContext)con,
912 width, height, (Surface *)wnd);
913 }
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800914
Chris Wailes488230c32014-08-14 11:22:40 -0700915 ANativeWindow * window = nullptr;
916 if (wnd == nullptr) {
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800917
918 } else {
Jeff Brown64a55af2012-08-26 02:47:39 -0700919 window = android_view_Surface_getNativeWindow(_env, wnd).get();
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800920 }
921
Tim Murrayeff663f2013-11-15 13:08:30 -0800922 rsContextSetSurface((RsContext)con, width, height, window);
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800923}
924
925static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800926nContextDestroy(JNIEnv *_env, jobject _this, jlong con)
Jason Samsd19f10d2009-05-22 14:03:28 -0700927{
Andreas Gampe67333922014-11-10 20:35:59 -0800928 if (kLogApi) {
929 ALOGD("nContextDestroy, con(%p)", (RsContext)con);
930 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800931 rsContextDestroy((RsContext)con);
Jason Samsd19f10d2009-05-22 14:03:28 -0700932}
933
Jason Sams715333b2009-11-17 17:26:46 -0800934static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800935nContextDump(JNIEnv *_env, jobject _this, jlong con, jint bits)
Jason Sams715333b2009-11-17 17:26:46 -0800936{
Andreas Gampe67333922014-11-10 20:35:59 -0800937 if (kLogApi) {
938 ALOGD("nContextDump, con(%p) bits(%i)", (RsContext)con, bits);
939 }
Jason Sams715333b2009-11-17 17:26:46 -0800940 rsContextDump((RsContext)con, bits);
941}
Jason Samsd19f10d2009-05-22 14:03:28 -0700942
943static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800944nContextPause(JNIEnv *_env, jobject _this, jlong con)
Jason Sams65e7aa52009-09-24 17:38:20 -0700945{
Andreas Gampe67333922014-11-10 20:35:59 -0800946 if (kLogApi) {
947 ALOGD("nContextPause, con(%p)", (RsContext)con);
948 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800949 rsContextPause((RsContext)con);
Jason Sams65e7aa52009-09-24 17:38:20 -0700950}
951
952static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800953nContextResume(JNIEnv *_env, jobject _this, jlong con)
Jason Sams65e7aa52009-09-24 17:38:20 -0700954{
Andreas Gampe67333922014-11-10 20:35:59 -0800955 if (kLogApi) {
956 ALOGD("nContextResume, con(%p)", (RsContext)con);
957 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800958 rsContextResume((RsContext)con);
Jason Sams65e7aa52009-09-24 17:38:20 -0700959}
960
Jason Sams1c415172010-11-08 17:06:46 -0800961
962static jstring
Tim Murrayeff663f2013-11-15 13:08:30 -0800963nContextGetErrorMessage(JNIEnv *_env, jobject _this, jlong con)
Jason Sams1c415172010-11-08 17:06:46 -0800964{
Andreas Gampe67333922014-11-10 20:35:59 -0800965 if (kLogApi) {
966 ALOGD("nContextGetErrorMessage, con(%p)", (RsContext)con);
967 }
Jason Sams1c415172010-11-08 17:06:46 -0800968 char buf[1024];
969
970 size_t receiveLen;
971 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800972 int id = rsContextGetMessage((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -0700973 buf, sizeof(buf),
974 &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -0700975 &subID, sizeof(subID));
Jason Sams1c415172010-11-08 17:06:46 -0800976 if (!id && receiveLen) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +0100977 ALOGV("message receive buffer too small. %zu", receiveLen);
Jason Sams1c415172010-11-08 17:06:46 -0800978 }
979 return _env->NewStringUTF(buf);
980}
981
Jason Samsedbfabd2011-05-17 15:01:29 -0700982static jint
Tim Murrayeff663f2013-11-15 13:08:30 -0800983nContextGetUserMessage(JNIEnv *_env, jobject _this, jlong con, jintArray data)
Jason Sams516c3192009-10-06 13:58:47 -0700984{
Jason Sams516c3192009-10-06 13:58:47 -0700985 jint len = _env->GetArrayLength(data);
Andreas Gampe67333922014-11-10 20:35:59 -0800986 if (kLogApi) {
987 ALOGD("nContextGetMessage, con(%p), len(%i)", (RsContext)con, len);
988 }
Chris Wailes488230c32014-08-14 11:22:40 -0700989 jint *ptr = _env->GetIntArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -0700990 if (ptr == nullptr) {
991 ALOGE("Failed to get Java array elements");
992 return 0;
993 }
Jason Sams516c3192009-10-06 13:58:47 -0700994 size_t receiveLen;
Jason Sams1c415172010-11-08 17:06:46 -0800995 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800996 int id = rsContextGetMessage((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -0700997 ptr, len * 4,
998 &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -0700999 &subID, sizeof(subID));
Jason Sams516c3192009-10-06 13:58:47 -07001000 if (!id && receiveLen) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001001 ALOGV("message receive buffer too small. %zu", receiveLen);
Jason Sams516c3192009-10-06 13:58:47 -07001002 }
1003 _env->ReleaseIntArrayElements(data, ptr, 0);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001004 return (jint)id;
Jason Sams1c415172010-11-08 17:06:46 -08001005}
1006
1007static jint
Tim Murrayeff663f2013-11-15 13:08:30 -08001008nContextPeekMessage(JNIEnv *_env, jobject _this, jlong con, jintArray auxData)
Jason Sams1c415172010-11-08 17:06:46 -08001009{
Andreas Gampe67333922014-11-10 20:35:59 -08001010 if (kLogApi) {
1011 ALOGD("nContextPeekMessage, con(%p)", (RsContext)con);
1012 }
Chris Wailes488230c32014-08-14 11:22:40 -07001013 jint *auxDataPtr = _env->GetIntArrayElements(auxData, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001014 if (auxDataPtr == nullptr) {
1015 ALOGE("Failed to get Java array elements");
1016 return 0;
1017 }
Jason Sams1c415172010-11-08 17:06:46 -08001018 size_t receiveLen;
1019 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -08001020 int id = rsContextPeekMessage((RsContext)con, &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -07001021 &subID, sizeof(subID));
Jason Sams1c415172010-11-08 17:06:46 -08001022 auxDataPtr[0] = (jint)subID;
1023 auxDataPtr[1] = (jint)receiveLen;
1024 _env->ReleaseIntArrayElements(auxData, auxDataPtr, 0);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001025 return (jint)id;
Jason Sams516c3192009-10-06 13:58:47 -07001026}
1027
Tim Murrayeff663f2013-11-15 13:08:30 -08001028static void nContextInitToClient(JNIEnv *_env, jobject _this, jlong con)
Jason Sams516c3192009-10-06 13:58:47 -07001029{
Andreas Gampe67333922014-11-10 20:35:59 -08001030 if (kLogApi) {
1031 ALOGD("nContextInitToClient, con(%p)", (RsContext)con);
1032 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001033 rsContextInitToClient((RsContext)con);
Jason Sams516c3192009-10-06 13:58:47 -07001034}
1035
Tim Murrayeff663f2013-11-15 13:08:30 -08001036static void nContextDeinitToClient(JNIEnv *_env, jobject _this, jlong con)
Jason Sams516c3192009-10-06 13:58:47 -07001037{
Andreas Gampe67333922014-11-10 20:35:59 -08001038 if (kLogApi) {
1039 ALOGD("nContextDeinitToClient, con(%p)", (RsContext)con);
1040 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001041 rsContextDeinitToClient((RsContext)con);
Jason Sams516c3192009-10-06 13:58:47 -07001042}
1043
Jason Sams455d6442013-02-05 19:20:18 -08001044static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001045nContextSendMessage(JNIEnv *_env, jobject _this, jlong con, jint id, jintArray data)
Jason Sams455d6442013-02-05 19:20:18 -08001046{
Chris Wailes488230c32014-08-14 11:22:40 -07001047 jint *ptr = nullptr;
Jason Sams455d6442013-02-05 19:20:18 -08001048 jint len = 0;
1049 if (data) {
1050 len = _env->GetArrayLength(data);
Stephen Hines4a043c12014-08-21 23:20:32 -07001051 ptr = _env->GetIntArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001052 if (ptr == nullptr) {
1053 ALOGE("Failed to get Java array elements");
1054 return;
1055 }
Jason Sams455d6442013-02-05 19:20:18 -08001056 }
Andreas Gampe67333922014-11-10 20:35:59 -08001057 if (kLogApi) {
1058 ALOGD("nContextSendMessage, con(%p), id(%i), len(%i)", (RsContext)con, id, len);
1059 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001060 rsContextSendMessage((RsContext)con, id, (const uint8_t *)ptr, len * sizeof(int));
Jason Sams455d6442013-02-05 19:20:18 -08001061 if (data) {
1062 _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
1063 }
1064}
1065
1066
Jason Sams516c3192009-10-06 13:58:47 -07001067
Tim Murray460a0492013-11-19 12:45:54 -08001068static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001069nElementCreate(JNIEnv *_env, jobject _this, jlong con, jlong type, jint kind, jboolean norm,
1070 jint size)
Jason Samsd19f10d2009-05-22 14:03:28 -07001071{
Andreas Gampe67333922014-11-10 20:35:59 -08001072 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001073 ALOGD("nElementCreate, con(%p), type(%" PRId64 "), kind(%i), norm(%i), size(%i)", (RsContext)con,
Andreas Gampe67333922014-11-10 20:35:59 -08001074 type, kind, norm, size);
1075 }
1076 return (jlong)(uintptr_t)rsElementCreate((RsContext)con, (RsDataType)type, (RsDataKind)kind,
Yang Ni8c8daea2016-03-08 21:01:54 +00001077 norm, size);
Jason Samsd19f10d2009-05-22 14:03:28 -07001078}
1079
Tim Murray460a0492013-11-19 12:45:54 -08001080static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001081nElementCreate2(JNIEnv *_env, jobject _this, jlong con,
Ashok Bhat98071552014-02-12 09:54:43 +00001082 jlongArray _ids, jobjectArray _names, jintArray _arraySizes)
Jason Samsd19f10d2009-05-22 14:03:28 -07001083{
Jason Sams718cd1f2009-12-23 14:35:29 -08001084 int fieldCount = _env->GetArrayLength(_ids);
Andreas Gampe67333922014-11-10 20:35:59 -08001085 if (kLogApi) {
1086 ALOGD("nElementCreate2, con(%p)", (RsContext)con);
1087 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001088
Chris Wailes488230c32014-08-14 11:22:40 -07001089 jlong *jIds = _env->GetLongArrayElements(_ids, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001090 if (jIds == nullptr) {
1091 ALOGE("Failed to get Java array elements: ids");
1092 return 0;
1093 }
Chris Wailes488230c32014-08-14 11:22:40 -07001094 jint *jArraySizes = _env->GetIntArrayElements(_arraySizes, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001095 if (jArraySizes == nullptr) {
1096 ALOGE("Failed to get Java array elements: arraySizes");
1097 return 0;
1098 }
Ashok Bhat98071552014-02-12 09:54:43 +00001099
1100 RsElement *ids = (RsElement*)malloc(fieldCount * sizeof(RsElement));
1101 uint32_t *arraySizes = (uint32_t *)malloc(fieldCount * sizeof(uint32_t));
1102
1103 for(int i = 0; i < fieldCount; i ++) {
1104 ids[i] = (RsElement)jIds[i];
1105 arraySizes[i] = (uint32_t)jArraySizes[i];
1106 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001107
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08001108 AutoJavaStringArrayToUTF8 names(_env, _names, fieldCount);
1109
1110 const char **nameArray = names.c_str();
1111 size_t *sizeArray = names.c_str_len();
1112
Tim Murray3aa89c12014-08-18 17:51:22 -07001113 jlong id = (jlong)(uintptr_t)rsElementCreate2((RsContext)con,
Ashok Bhat98071552014-02-12 09:54:43 +00001114 (const RsElement *)ids, fieldCount,
Jason Sams7a22e102011-05-06 14:14:30 -07001115 nameArray, fieldCount * sizeof(size_t), sizeArray,
Yang Ni8c8daea2016-03-08 21:01:54 +00001116 (const uint32_t *)arraySizes, fieldCount);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08001117
Ashok Bhat98071552014-02-12 09:54:43 +00001118 free(ids);
1119 free(arraySizes);
1120 _env->ReleaseLongArrayElements(_ids, jIds, JNI_ABORT);
1121 _env->ReleaseIntArrayElements(_arraySizes, jArraySizes, JNI_ABORT);
1122
Tim Murray3aa89c12014-08-18 17:51:22 -07001123 return (jlong)(uintptr_t)id;
Jason Samsd19f10d2009-05-22 14:03:28 -07001124}
1125
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001126static void
Tim Murray460a0492013-11-19 12:45:54 -08001127nElementGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jintArray _elementData)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001128{
1129 int dataSize = _env->GetArrayLength(_elementData);
Andreas Gampe67333922014-11-10 20:35:59 -08001130 if (kLogApi) {
1131 ALOGD("nElementGetNativeData, con(%p)", (RsContext)con);
1132 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001133
1134 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
1135 assert(dataSize == 5);
1136
Miao Wangcbb02062017-01-24 18:58:17 -08001137 uint32_t elementData[5];
Tim Murrayeff663f2013-11-15 13:08:30 -08001138 rsaElementGetNativeData((RsContext)con, (RsElement)id, elementData, dataSize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001139
1140 for(jint i = 0; i < dataSize; i ++) {
Ashok Bhat98071552014-02-12 09:54:43 +00001141 const jint data = (jint)elementData[i];
1142 _env->SetIntArrayRegion(_elementData, i, 1, &data);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001143 }
1144}
1145
1146
1147static void
Tim Murray460a0492013-11-19 12:45:54 -08001148nElementGetSubElements(JNIEnv *_env, jobject _this, jlong con, jlong id,
Ashok Bhat98071552014-02-12 09:54:43 +00001149 jlongArray _IDs,
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -07001150 jobjectArray _names,
1151 jintArray _arraySizes)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001152{
Ashok Bhat98071552014-02-12 09:54:43 +00001153 uint32_t dataSize = _env->GetArrayLength(_IDs);
Andreas Gampe67333922014-11-10 20:35:59 -08001154 if (kLogApi) {
1155 ALOGD("nElementGetSubElements, con(%p)", (RsContext)con);
1156 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001157
Ashok Bhat98071552014-02-12 09:54:43 +00001158 uintptr_t *ids = (uintptr_t*)malloc(dataSize * sizeof(uintptr_t));
1159 const char **names = (const char **)malloc(dataSize * sizeof(const char *));
Miao Wangcbb02062017-01-24 18:58:17 -08001160 size_t *arraySizes = (size_t *)malloc(dataSize * sizeof(size_t));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001161
Andreas Gampe67333922014-11-10 20:35:59 -08001162 rsaElementGetSubElements((RsContext)con, (RsElement)id, ids, names, arraySizes,
1163 (uint32_t)dataSize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001164
Ashok Bhat98071552014-02-12 09:54:43 +00001165 for(uint32_t i = 0; i < dataSize; i++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07001166 const jlong id = (jlong)(uintptr_t)ids[i];
Ashok Bhat98071552014-02-12 09:54:43 +00001167 const jint arraySize = (jint)arraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001168 _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
Ashok Bhat98071552014-02-12 09:54:43 +00001169 _env->SetLongArrayRegion(_IDs, i, 1, &id);
1170 _env->SetIntArrayRegion(_arraySizes, i, 1, &arraySize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001171 }
1172
1173 free(ids);
1174 free(names);
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -07001175 free(arraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001176}
1177
Jason Samsd19f10d2009-05-22 14:03:28 -07001178// -----------------------------------
1179
Tim Murray460a0492013-11-19 12:45:54 -08001180static jlong
1181nTypeCreate(JNIEnv *_env, jobject _this, jlong con, jlong eid,
Jason Samsb109cc72013-01-07 18:20:12 -08001182 jint dimx, jint dimy, jint dimz, jboolean mips, jboolean faces, jint yuv)
Jason Samsd19f10d2009-05-22 14:03:28 -07001183{
Andreas Gampe67333922014-11-10 20:35:59 -08001184 if (kLogApi) {
1185 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 +01001186 (RsContext)con, (void*)eid, dimx, dimy, dimz, mips, faces, yuv);
Andreas Gampe67333922014-11-10 20:35:59 -08001187 }
Jason Sams3b9c52a2010-10-14 17:48:46 -07001188
Andreas Gampe67333922014-11-10 20:35:59 -08001189 return (jlong)(uintptr_t)rsTypeCreate((RsContext)con, (RsElement)eid, dimx, dimy, dimz, mips,
Yang Ni8c8daea2016-03-08 21:01:54 +00001190 faces, yuv);
Jason Samsd19f10d2009-05-22 14:03:28 -07001191}
1192
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001193static void
Ashok Bhat98071552014-02-12 09:54:43 +00001194nTypeGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jlongArray _typeData)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001195{
1196 // We are packing 6 items: mDimX; mDimY; mDimZ;
1197 // mDimLOD; mDimFaces; mElement; into typeData
1198 int elementCount = _env->GetArrayLength(_typeData);
1199
1200 assert(elementCount == 6);
Andreas Gampe67333922014-11-10 20:35:59 -08001201 if (kLogApi) {
1202 ALOGD("nTypeGetNativeData, con(%p)", (RsContext)con);
1203 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001204
Ashok Bhat98071552014-02-12 09:54:43 +00001205 uintptr_t typeData[6];
Tim Murrayeff663f2013-11-15 13:08:30 -08001206 rsaTypeGetNativeData((RsContext)con, (RsType)id, typeData, 6);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001207
1208 for(jint i = 0; i < elementCount; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07001209 const jlong data = (jlong)(uintptr_t)typeData[i];
Ashok Bhat98071552014-02-12 09:54:43 +00001210 _env->SetLongArrayRegion(_typeData, i, 1, &data);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001211 }
1212}
1213
Jason Samsd19f10d2009-05-22 14:03:28 -07001214// -----------------------------------
1215
Tim Murray460a0492013-11-19 12:45:54 -08001216static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001217nAllocationCreateTyped(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mips, jint usage,
1218 jlong pointer)
Jason Samsd19f10d2009-05-22 14:03:28 -07001219{
Andreas Gampe67333922014-11-10 20:35:59 -08001220 if (kLogApi) {
1221 ALOGD("nAllocationCreateTyped, con(%p), type(%p), mip(%i), usage(%i), ptr(%p)",
1222 (RsContext)con, (RsElement)type, mips, usage, (void *)pointer);
1223 }
1224 return (jlong)(uintptr_t) rsAllocationCreateTyped((RsContext)con, (RsType)type,
1225 (RsAllocationMipmapControl)mips,
Yang Ni8c8daea2016-03-08 21:01:54 +00001226 (uint32_t)usage, (uintptr_t)pointer);
Jason Samsd19f10d2009-05-22 14:03:28 -07001227}
1228
Jason Samsd19f10d2009-05-22 14:03:28 -07001229static void
Tim Murray460a0492013-11-19 12:45:54 -08001230nAllocationSyncAll(JNIEnv *_env, jobject _this, jlong con, jlong a, jint bits)
Jason Sams5476b452010-12-08 16:14:36 -08001231{
Andreas Gampe67333922014-11-10 20:35:59 -08001232 if (kLogApi) {
1233 ALOGD("nAllocationSyncAll, con(%p), a(%p), bits(0x%08x)", (RsContext)con, (RsAllocation)a,
1234 bits);
1235 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001236 rsAllocationSyncAll((RsContext)con, (RsAllocation)a, (RsAllocationUsageType)bits);
Jason Sams5476b452010-12-08 16:14:36 -08001237}
1238
Miao Wang8c150922015-10-26 17:44:10 -07001239static void
1240nAllocationSetupBufferQueue(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint numAlloc)
1241{
1242 if (kLogApi) {
1243 ALOGD("nAllocationSetupBufferQueue, con(%p), alloc(%p), numAlloc(%d)", (RsContext)con,
1244 (RsAllocation)alloc, numAlloc);
1245 }
1246 rsAllocationSetupBufferQueue((RsContext)con, (RsAllocation)alloc, (uint32_t)numAlloc);
1247}
1248
1249static void
1250nAllocationShareBufferQueue(JNIEnv *_env, jobject _this, jlong con, jlong alloc1, jlong alloc2)
1251{
1252 if (kLogApi) {
1253 ALOGD("nAllocationShareBufferQueue, con(%p), alloc1(%p), alloc2(%p)", (RsContext)con,
1254 (RsAllocation)alloc1, (RsAllocation)alloc2);
1255 }
1256
1257 rsAllocationShareBufferQueue((RsContext)con, (RsAllocation)alloc1, (RsAllocation)alloc2);
1258}
1259
Jason Sams72226e02013-02-22 12:45:54 -08001260static jobject
Tim Murray460a0492013-11-19 12:45:54 -08001261nAllocationGetSurface(JNIEnv *_env, jobject _this, jlong con, jlong a)
Jason Sams615e7ce2012-01-13 14:01:20 -08001262{
Andreas Gampe67333922014-11-10 20:35:59 -08001263 if (kLogApi) {
1264 ALOGD("nAllocationGetSurface, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1265 }
Jason Sams615e7ce2012-01-13 14:01:20 -08001266
Miao Wang1e95fc82017-03-04 16:28:56 -08001267 ANativeWindow *anw = (ANativeWindow *)rsAllocationGetSurface((RsContext)con, (RsAllocation)a);
1268
1269 sp<Surface> surface(static_cast<Surface*>(anw));
1270 sp<IGraphicBufferProducer> bp = surface->getIGraphicBufferProducer();
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001271
Jason Sams72226e02013-02-22 12:45:54 -08001272 jobject o = android_view_Surface_createFromIGraphicBufferProducer(_env, bp);
1273 return o;
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001274}
1275
1276static void
Tim Murray460a0492013-11-19 12:45:54 -08001277nAllocationSetSurface(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject sur)
Jason Sams163766c2012-02-15 12:04:24 -08001278{
Andreas Gampe67333922014-11-10 20:35:59 -08001279 if (kLogApi) {
1280 ALOGD("nAllocationSetSurface, con(%p), alloc(%p), surface(%p)", (RsContext)con,
1281 (RsAllocation)alloc, (Surface *)sur);
1282 }
Jason Sams163766c2012-02-15 12:04:24 -08001283
Miao Wang33287e82017-03-06 09:31:32 -08001284 ANativeWindow *anw = nullptr;
Jason Sams163766c2012-02-15 12:04:24 -08001285 if (sur != 0) {
Miao Wangf35ddc92017-04-03 16:42:03 -07001286 // Connect the native window handle to buffer queue.
Miao Wang33287e82017-03-06 09:31:32 -08001287 anw = ANativeWindow_fromSurface(_env, sur);
Miao Wangf35ddc92017-04-03 16:42:03 -07001288 native_window_api_connect(anw, NATIVE_WINDOW_API_CPU);
Jason Sams163766c2012-02-15 12:04:24 -08001289 }
1290
Miao Wang33287e82017-03-06 09:31:32 -08001291 rsAllocationSetSurface((RsContext)con, (RsAllocation)alloc, anw);
Jason Sams163766c2012-02-15 12:04:24 -08001292}
1293
1294static void
Tim Murray460a0492013-11-19 12:45:54 -08001295nAllocationIoSend(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Sams163766c2012-02-15 12:04:24 -08001296{
Andreas Gampe67333922014-11-10 20:35:59 -08001297 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001298 ALOGD("nAllocationIoSend, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
Andreas Gampe67333922014-11-10 20:35:59 -08001299 }
Tim Murray460a0492013-11-19 12:45:54 -08001300 rsAllocationIoSend((RsContext)con, (RsAllocation)alloc);
Jason Sams163766c2012-02-15 12:04:24 -08001301}
1302
Miao Wang8c150922015-10-26 17:44:10 -07001303static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001304nAllocationIoReceive(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Sams163766c2012-02-15 12:04:24 -08001305{
Andreas Gampe67333922014-11-10 20:35:59 -08001306 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001307 ALOGD("nAllocationIoReceive, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
Andreas Gampe67333922014-11-10 20:35:59 -08001308 }
Miao Wang8c150922015-10-26 17:44:10 -07001309 return (jlong) rsAllocationIoReceive((RsContext)con, (RsAllocation)alloc);
Jason Sams163766c2012-02-15 12:04:24 -08001310}
1311
Jason Sams163766c2012-02-15 12:04:24 -08001312static void
Tim Murray460a0492013-11-19 12:45:54 -08001313nAllocationGenerateMipmaps(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Samsf7086092011-01-12 13:28:37 -08001314{
Andreas Gampe67333922014-11-10 20:35:59 -08001315 if (kLogApi) {
1316 ALOGD("nAllocationGenerateMipmaps, con(%p), a(%p)", (RsContext)con, (RsAllocation)alloc);
1317 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001318 rsAllocationGenerateMipmaps((RsContext)con, (RsAllocation)alloc);
Jason Samsf7086092011-01-12 13:28:37 -08001319}
1320
Tim Murray460a0492013-11-19 12:45:54 -08001321static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001322nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1323 jobject jbitmap, jint usage)
Jason Samsffe9f482009-06-01 17:45:53 -07001324{
John Recked207b92015-04-10 13:52:57 -07001325 SkBitmap bitmap;
1326 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Samsffe9f482009-06-01 17:45:53 -07001327
Jason Sams5476b452010-12-08 16:14:36 -08001328 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001329 jlong id = (jlong)(uintptr_t)rsAllocationCreateFromBitmap((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001330 (RsType)type, (RsAllocationMipmapControl)mip,
Yang Ni8c8daea2016-03-08 21:01:54 +00001331 ptr, bitmap.getSize(), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001332 return id;
Jason Samsffe9f482009-06-01 17:45:53 -07001333}
Jason Samsfe08d992009-05-27 14:45:32 -07001334
Tim Murray460a0492013-11-19 12:45:54 -08001335static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001336nAllocationCreateBitmapBackedAllocation(JNIEnv *_env, jobject _this, jlong con, jlong type,
1337 jint mip, jobject jbitmap, jint usage)
Tim Murraya3145512012-12-04 17:59:29 -08001338{
John Recked207b92015-04-10 13:52:57 -07001339 SkBitmap bitmap;
1340 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Tim Murraya3145512012-12-04 17:59:29 -08001341
Tim Murraya3145512012-12-04 17:59:29 -08001342 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 return id;
1347}
1348
Tim Murray460a0492013-11-19 12:45:54 -08001349static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001350nAllocationCubeCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1351 jobject jbitmap, jint usage)
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001352{
John Recked207b92015-04-10 13:52:57 -07001353 SkBitmap bitmap;
1354 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001355
Jason Sams5476b452010-12-08 16:14:36 -08001356 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001357 jlong id = (jlong)(uintptr_t)rsAllocationCubeCreateFromBitmap((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001358 (RsType)type, (RsAllocationMipmapControl)mip,
Yang Ni8c8daea2016-03-08 21:01:54 +00001359 ptr, bitmap.getSize(), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001360 return id;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001361}
1362
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001363static void
Tim Murray460a0492013-11-19 12:45:54 -08001364nAllocationCopyFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001365{
John Recked207b92015-04-10 13:52:57 -07001366 SkBitmap bitmap;
1367 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Samsf7086092011-01-12 13:28:37 -08001368 int w = bitmap.width();
1369 int h = bitmap.height();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001370
Jason Sams4ef66502010-12-10 16:03:15 -08001371 const void* ptr = bitmap.getPixels();
Tim Murrayeff663f2013-11-15 13:08:30 -08001372 rsAllocation2DData((RsContext)con, (RsAllocation)alloc, 0, 0,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001373 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Tim Murray38faea32012-11-27 14:55:08 -08001374 w, h, ptr, bitmap.getSize(), 0);
Jason Sams4ef66502010-12-10 16:03:15 -08001375}
1376
1377static void
Tim Murray460a0492013-11-19 12:45:54 -08001378nAllocationCopyToBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Jason Sams4ef66502010-12-10 16:03:15 -08001379{
John Recked207b92015-04-10 13:52:57 -07001380 SkBitmap bitmap;
1381 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Sams4ef66502010-12-10 16:03:15 -08001382
Jason Sams4ef66502010-12-10 16:03:15 -08001383 void* ptr = bitmap.getPixels();
Tim Murrayeff663f2013-11-15 13:08:30 -08001384 rsAllocationCopyToBitmap((RsContext)con, (RsAllocation)alloc, ptr, bitmap.getSize());
Alex Sakhartchouk835b8542011-07-20 14:33:10 -07001385 bitmap.notifyPixelsChanged();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001386}
1387
Stephen Hines414fa2c2014-04-17 01:02:42 -07001388// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Samsd19f10d2009-05-22 14:03:28 -07001389static void
Tim Murray460a0492013-11-19 12:45:54 -08001390nAllocationData1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001391 jint count, jobject data, jint sizeBytes, jint dataType, jint mSize,
1392 jboolean usePadding)
Jason Samsd19f10d2009-05-22 14:03:28 -07001393{
Jason Samse729a942013-11-06 11:22:02 -08001394 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001395 if (kLogApi) {
1396 ALOGD("nAllocation1DData, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1397 "dataType(%i)", (RsContext)con, (RsAllocation)alloc, offset, count, sizeBytes,
1398 dataType);
1399 }
Miao Wang87e908d2015-03-02 15:15:15 -08001400 PER_ARRAY_TYPE(nullptr, rsAllocation1DData, true,
1401 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -07001402}
1403
1404static void
Miao Wangc8e237e2015-02-20 18:36:32 -08001405nAllocationElementData(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1406 jint xoff, jint yoff, jint zoff,
1407 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Jason Sams49bdaf02010-08-31 13:50:42 -07001408{
Andreas Gampe67333922014-11-10 20:35:59 -08001409 if (kLogApi) {
Yang Ni86c5c2d2016-03-25 15:49:07 -07001410 jint len = _env->GetArrayLength(data);
Miao Wangc8e237e2015-02-20 18:36:32 -08001411 ALOGD("nAllocationElementData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1412 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
Andreas Gampe67333922014-11-10 20:35:59 -08001413 sizeBytes);
1414 }
Chris Wailes488230c32014-08-14 11:22:40 -07001415 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001416 if (ptr == nullptr) {
1417 ALOGE("Failed to get Java array elements");
1418 return;
1419 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001420 rsAllocationElementData((RsContext)con, (RsAllocation)alloc,
1421 xoff, yoff, zoff,
1422 lod, ptr, sizeBytes, compIdx);
Jason Sams49bdaf02010-08-31 13:50:42 -07001423 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1424}
1425
Miao Wangc8e237e2015-02-20 18:36:32 -08001426
Stephen Hines414fa2c2014-04-17 01:02:42 -07001427// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Sams49bdaf02010-08-31 13:50:42 -07001428static void
Tim Murray460a0492013-11-19 12:45:54 -08001429nAllocationData2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001430 jint w, jint h, jobject data, jint sizeBytes, jint dataType, jint mSize,
1431 jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001432{
Jason Samse729a942013-11-06 11:22:02 -08001433 RsAllocation *alloc = (RsAllocation *)_alloc;
1434 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001435 if (kLogApi) {
1436 ALOGD("nAllocation2DData, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1437 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1438 }
Miao Wang87e908d2015-03-02 15:15:15 -08001439 int count = w * h;
1440 PER_ARRAY_TYPE(nullptr, rsAllocation2DData, true,
1441 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Samsd19f10d2009-05-22 14:03:28 -07001442}
1443
Stephen Hines414fa2c2014-04-17 01:02:42 -07001444// Copies from the Allocation pointed to by srcAlloc into the Allocation
1445// pointed to by dstAlloc.
Jason Sams40a29e82009-08-10 14:55:26 -07001446static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001447nAllocationData2D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001448 jlong dstAlloc, jint dstXoff, jint dstYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001449 jint dstMip, jint dstFace,
1450 jint width, jint height,
Tim Murray460a0492013-11-19 12:45:54 -08001451 jlong srcAlloc, jint srcXoff, jint srcYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001452 jint srcMip, jint srcFace)
1453{
Andreas Gampe67333922014-11-10 20:35:59 -08001454 if (kLogApi) {
1455 ALOGD("nAllocation2DData_s, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1456 " dstMip(%i), dstFace(%i), width(%i), height(%i),"
1457 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i), srcFace(%i)",
1458 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip, dstFace,
1459 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
1460 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001461
Tim Murrayeff663f2013-11-15 13:08:30 -08001462 rsAllocationCopy2DRange((RsContext)con,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001463 (RsAllocation)dstAlloc,
1464 dstXoff, dstYoff,
1465 dstMip, dstFace,
1466 width, height,
1467 (RsAllocation)srcAlloc,
1468 srcXoff, srcYoff,
1469 srcMip, srcFace);
1470}
1471
Stephen Hines414fa2c2014-04-17 01:02:42 -07001472// Copies from the Java object data into the Allocation pointed to by _alloc.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001473static void
Tim Murray460a0492013-11-19 12:45:54 -08001474nAllocationData3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001475 jint w, jint h, jint d, jobject data, jint sizeBytes, jint dataType,
1476 jint mSize, jboolean usePadding)
Jason Samsb05d6892013-04-09 15:59:24 -07001477{
Jason Samse729a942013-11-06 11:22:02 -08001478 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001479 if (kLogApi) {
1480 ALOGD("nAllocation3DData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1481 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1482 lod, w, h, d, sizeBytes);
1483 }
Miao Wang87e908d2015-03-02 15:15:15 -08001484 int count = w * h * d;
1485 PER_ARRAY_TYPE(nullptr, rsAllocation3DData, true,
1486 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Jason Samsb05d6892013-04-09 15:59:24 -07001487}
1488
Stephen Hines414fa2c2014-04-17 01:02:42 -07001489// Copies from the Allocation pointed to by srcAlloc into the Allocation
1490// pointed to by dstAlloc.
Jason Samsb05d6892013-04-09 15:59:24 -07001491static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001492nAllocationData3D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001493 jlong dstAlloc, jint dstXoff, jint dstYoff, jint dstZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001494 jint dstMip,
1495 jint width, jint height, jint depth,
Tim Murray460a0492013-11-19 12:45:54 -08001496 jlong srcAlloc, jint srcXoff, jint srcYoff, jint srcZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001497 jint srcMip)
1498{
Andreas Gampe67333922014-11-10 20:35:59 -08001499 if (kLogApi) {
1500 ALOGD("nAllocationData3D_alloc, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1501 " dstMip(%i), width(%i), height(%i),"
1502 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i)",
1503 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip,
1504 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip);
1505 }
Jason Samsb05d6892013-04-09 15:59:24 -07001506
Tim Murrayeff663f2013-11-15 13:08:30 -08001507 rsAllocationCopy3DRange((RsContext)con,
Jason Samsb05d6892013-04-09 15:59:24 -07001508 (RsAllocation)dstAlloc,
1509 dstXoff, dstYoff, dstZoff, dstMip,
1510 width, height, depth,
1511 (RsAllocation)srcAlloc,
1512 srcXoff, srcYoff, srcZoff, srcMip);
1513}
1514
Jason Sams21659ac2013-11-06 15:08:07 -08001515
Stephen Hines414fa2c2014-04-17 01:02:42 -07001516// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsb05d6892013-04-09 15:59:24 -07001517static void
Miao Wang87e908d2015-03-02 15:15:15 -08001518nAllocationRead(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jobject data, jint dataType,
1519 jint mSize, jboolean usePadding)
Jason Sams40a29e82009-08-10 14:55:26 -07001520{
Jason Sams21659ac2013-11-06 15:08:07 -08001521 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001522 if (kLogApi) {
1523 ALOGD("nAllocationRead, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
1524 }
Miao Wang87e908d2015-03-02 15:15:15 -08001525 int count = 0;
1526 PER_ARRAY_TYPE(0, rsAllocationRead, false,
1527 (RsContext)con, alloc, ptr, len * typeBytes);
Jason Sams40a29e82009-08-10 14:55:26 -07001528}
1529
Stephen Hines414fa2c2014-04-17 01:02:42 -07001530// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Sams40a29e82009-08-10 14:55:26 -07001531static void
Tim Murray460a0492013-11-19 12:45:54 -08001532nAllocationRead1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001533 jint count, jobject data, jint sizeBytes, jint dataType,
1534 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001535{
Jason Sams21659ac2013-11-06 15:08:07 -08001536 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001537 if (kLogApi) {
1538 ALOGD("nAllocation1DRead, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1539 "dataType(%i)", (RsContext)con, alloc, offset, count, sizeBytes, dataType);
1540 }
Miao Wang87e908d2015-03-02 15:15:15 -08001541 PER_ARRAY_TYPE(0, rsAllocation1DRead, false,
1542 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsfb9f82c2011-01-12 14:53:25 -08001543}
1544
Miao Wangc8e237e2015-02-20 18:36:32 -08001545// Copies from the Element in the Allocation pointed to by _alloc into the Java array data.
1546static void
Miao Wang45cec0a2015-03-04 16:40:21 -08001547nAllocationElementRead(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
Miao Wangc8e237e2015-02-20 18:36:32 -08001548 jint xoff, jint yoff, jint zoff,
Miao Wang45cec0a2015-03-04 16:40:21 -08001549 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Miao Wangc8e237e2015-02-20 18:36:32 -08001550{
Miao Wangc8e237e2015-02-20 18:36:32 -08001551 if (kLogApi) {
Yang Ni86c5c2d2016-03-25 15:49:07 -07001552 jint len = _env->GetArrayLength(data);
Miao Wang45cec0a2015-03-04 16:40:21 -08001553 ALOGD("nAllocationElementRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1554 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
1555 sizeBytes);
Miao Wangc8e237e2015-02-20 18:36:32 -08001556 }
Miao Wang45cec0a2015-03-04 16:40:21 -08001557 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001558 if (ptr == nullptr) {
1559 ALOGE("Failed to get Java array elements");
1560 return;
1561 }
Miao Wang45cec0a2015-03-04 16:40:21 -08001562 rsAllocationElementRead((RsContext)con, (RsAllocation)alloc,
1563 xoff, yoff, zoff,
Jason Samsa7e25092015-03-11 11:00:00 -07001564 lod, ptr, sizeBytes, compIdx);
Miao Wangbfa5e652015-05-04 15:29:25 -07001565 _env->ReleaseByteArrayElements(data, ptr, 0);
Miao Wangc8e237e2015-02-20 18:36:32 -08001566}
1567
Stephen Hines414fa2c2014-04-17 01:02:42 -07001568// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsfb9f82c2011-01-12 14:53:25 -08001569static void
Tim Murray460a0492013-11-19 12:45:54 -08001570nAllocationRead2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001571 jint w, jint h, jobject data, jint sizeBytes, jint dataType,
1572 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001573{
Jason Sams21659ac2013-11-06 15:08:07 -08001574 RsAllocation *alloc = (RsAllocation *)_alloc;
1575 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001576 if (kLogApi) {
1577 ALOGD("nAllocation2DRead, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1578 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1579 }
Miao Wang87e908d2015-03-02 15:15:15 -08001580 int count = w * h;
1581 PER_ARRAY_TYPE(0, rsAllocation2DRead, false,
1582 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Sams40a29e82009-08-10 14:55:26 -07001583}
Miao Wang87e908d2015-03-02 15:15:15 -08001584
Miao Wangc8e237e2015-02-20 18:36:32 -08001585// Copies from the Allocation pointed to by _alloc into the Java object data.
1586static void
1587nAllocationRead3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001588 jint w, jint h, jint d, jobject data, int sizeBytes, int dataType,
1589 jint mSize, jboolean usePadding)
Miao Wangc8e237e2015-02-20 18:36:32 -08001590{
1591 RsAllocation *alloc = (RsAllocation *)_alloc;
1592 if (kLogApi) {
1593 ALOGD("nAllocation3DRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1594 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1595 lod, w, h, d, sizeBytes);
1596 }
Miao Wang87e908d2015-03-02 15:15:15 -08001597 int count = w * h * d;
1598 PER_ARRAY_TYPE(nullptr, rsAllocation3DRead, false,
1599 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Miao Wangc8e237e2015-02-20 18:36:32 -08001600}
Jason Samsd19f10d2009-05-22 14:03:28 -07001601
Tim Murray460a0492013-11-19 12:45:54 -08001602static jlong
1603nAllocationGetType(JNIEnv *_env, jobject _this, jlong con, jlong a)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001604{
Andreas Gampe67333922014-11-10 20:35:59 -08001605 if (kLogApi) {
1606 ALOGD("nAllocationGetType, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1607 }
Tim Murray3aa89c12014-08-18 17:51:22 -07001608 return (jlong)(uintptr_t) rsaAllocationGetType((RsContext)con, (RsAllocation)a);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001609}
1610
Jason Sams5edc6082010-10-05 13:32:49 -07001611static void
Tim Murray460a0492013-11-19 12:45:54 -08001612nAllocationResize1D(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint dimX)
Jason Sams5edc6082010-10-05 13:32:49 -07001613{
Andreas Gampe67333922014-11-10 20:35:59 -08001614 if (kLogApi) {
1615 ALOGD("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", (RsContext)con,
1616 (RsAllocation)alloc, dimX);
1617 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001618 rsAllocationResize1D((RsContext)con, (RsAllocation)alloc, dimX);
Jason Sams5edc6082010-10-05 13:32:49 -07001619}
1620
Jason Sams46ba27e32015-02-06 17:45:15 -08001621
1622static jlong
1623nAllocationAdapterCreate(JNIEnv *_env, jobject _this, jlong con, jlong basealloc, jlong type)
1624{
1625 if (kLogApi) {
1626 ALOGD("nAllocationAdapterCreate, con(%p), base(%p), type(%p)",
1627 (RsContext)con, (RsAllocation)basealloc, (RsElement)type);
1628 }
1629 return (jlong)(uintptr_t) rsAllocationAdapterCreate((RsContext)con, (RsType)type,
1630 (RsAllocation)basealloc);
1631
1632}
1633
1634static void
1635nAllocationAdapterOffset(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1636 jint x, jint y, jint z, jint face, jint lod,
1637 jint a1, jint a2, jint a3, jint a4)
1638{
1639 uint32_t params[] = {
1640 (uint32_t)x, (uint32_t)y, (uint32_t)z, (uint32_t)face,
1641 (uint32_t)lod, (uint32_t)a1, (uint32_t)a2, (uint32_t)a3, (uint32_t)a4
1642 };
1643 if (kLogApi) {
1644 ALOGD("nAllocationAdapterOffset, con(%p), alloc(%p), x(%i), y(%i), z(%i), face(%i), lod(%i), arrays(%i %i %i %i)",
1645 (RsContext)con, (RsAllocation)alloc, x, y, z, face, lod, a1, a2, a3, a4);
1646 }
1647 rsAllocationAdapterOffset((RsContext)con, (RsAllocation)alloc,
1648 params, sizeof(params));
1649}
1650
1651
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001652// -----------------------------------
1653
Tim Murray460a0492013-11-19 12:45:54 -08001654static jlong
1655nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con, jlong native_asset)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001656{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001657 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001658 ALOGV("______nFileA3D %p", asset);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001659
Tim Murray3aa89c12014-08-18 17:51:22 -07001660 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromMemory((RsContext)con, asset->getBuffer(false), asset->getLength());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001661 return id;
1662}
1663
Tim Murray460a0492013-11-19 12:45:54 -08001664static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001665nFileA3DCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001666{
1667 AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001668 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001669 return 0;
1670 }
1671
1672 AutoJavaStringToUTF8 str(_env, _path);
1673 Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
Chris Wailes488230c32014-08-14 11:22:40 -07001674 if (asset == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001675 return 0;
1676 }
1677
Tim Murray3aa89c12014-08-18 17:51:22 -07001678 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromAsset((RsContext)con, asset);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001679 return id;
1680}
1681
Tim Murray460a0492013-11-19 12:45:54 -08001682static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001683nFileA3DCreateFromFile(JNIEnv *_env, jobject _this, jlong con, jstring fileName)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001684{
1685 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001686 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromFile((RsContext)con, fileNameUTF.c_str());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001687
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001688 return id;
1689}
1690
Tim Murray460a0492013-11-19 12:45:54 -08001691static jint
1692nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001693{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001694 int32_t numEntries = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001695 rsaFileA3DGetNumIndexEntries((RsContext)con, &numEntries, (RsFile)fileA3D);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001696 return (jint)numEntries;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001697}
1698
1699static void
Tim Murray460a0492013-11-19 12:45:54 -08001700nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001701{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001702 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001703 RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
1704
Tim Murrayeff663f2013-11-15 13:08:30 -08001705 rsaFileA3DGetIndexEntries((RsContext)con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001706
1707 for(jint i = 0; i < numEntries; i ++) {
1708 _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
1709 _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
1710 }
1711
1712 free(fileEntries);
1713}
1714
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001715static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001716nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint index)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001717{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001718 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Tim Murray3aa89c12014-08-18 17:51:22 -07001719 jlong id = (jlong)(uintptr_t)rsaFileA3DGetEntryByIndex((RsContext)con, (uint32_t)index, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001720 return id;
1721}
Jason Samsd19f10d2009-05-22 14:03:28 -07001722
1723// -----------------------------------
1724
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001725static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001726nFontCreateFromFile(JNIEnv *_env, jobject _this, jlong con,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001727 jstring fileName, jfloat fontSize, jint dpi)
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001728{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001729 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001730 jlong id = (jlong)(uintptr_t)rsFontCreateFromFile((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001731 fileNameUTF.c_str(), fileNameUTF.length(),
1732 fontSize, dpi);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001733
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001734 return id;
1735}
1736
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001737static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001738nFontCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con,
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001739 jstring name, jfloat fontSize, jint dpi, jlong native_asset)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001740{
1741 Asset* asset = reinterpret_cast<Asset*>(native_asset);
1742 AutoJavaStringToUTF8 nameUTF(_env, name);
1743
Tim Murray3aa89c12014-08-18 17:51:22 -07001744 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001745 nameUTF.c_str(), nameUTF.length(),
1746 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001747 asset->getBuffer(false), asset->getLength());
1748 return id;
1749}
1750
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001751static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001752nFontCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001753 jfloat fontSize, jint dpi)
1754{
1755 AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001756 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001757 return 0;
1758 }
1759
1760 AutoJavaStringToUTF8 str(_env, _path);
1761 Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
Chris Wailes488230c32014-08-14 11:22:40 -07001762 if (asset == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001763 return 0;
1764 }
1765
Tim Murray3aa89c12014-08-18 17:51:22 -07001766 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001767 str.c_str(), str.length(),
1768 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001769 asset->getBuffer(false), asset->getLength());
1770 delete asset;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001771 return id;
1772}
1773
Jason Samsbd1c3ad2009-08-03 16:03:08 -07001774// -----------------------------------
1775
1776static void
Tim Murray460a0492013-11-19 12:45:54 -08001777nScriptBindAllocation(JNIEnv *_env, jobject _this, jlong con, jlong script, jlong alloc, jint slot)
Jason Samsd19f10d2009-05-22 14:03:28 -07001778{
Andreas Gampe67333922014-11-10 20:35:59 -08001779 if (kLogApi) {
1780 ALOGD("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", (RsContext)con,
1781 (RsScript)script, (RsAllocation)alloc, slot);
1782 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001783 rsScriptBindAllocation((RsContext)con, (RsScript)script, (RsAllocation)alloc, slot);
Jason Samsd19f10d2009-05-22 14:03:28 -07001784}
1785
1786static void
Tim Murray460a0492013-11-19 12:45:54 -08001787nScriptSetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jint val)
Jason Sams4d339932010-05-11 14:03:58 -07001788{
Andreas Gampe67333922014-11-10 20:35:59 -08001789 if (kLogApi) {
1790 ALOGD("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", (RsContext)con, (void *)script,
1791 slot, val);
1792 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001793 rsScriptSetVarI((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001794}
1795
Tim Murray7c4caad2013-04-10 16:21:40 -07001796static jint
Tim Murray460a0492013-11-19 12:45:54 -08001797nScriptGetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001798{
Andreas Gampe67333922014-11-10 20:35:59 -08001799 if (kLogApi) {
1800 ALOGD("nScriptGetVarI, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1801 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001802 int value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001803 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001804 return value;
1805}
1806
Jason Sams4d339932010-05-11 14:03:58 -07001807static void
Tim Murray460a0492013-11-19 12:45:54 -08001808nScriptSetVarObj(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001809{
Andreas Gampe67333922014-11-10 20:35:59 -08001810 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001811 ALOGD("nScriptSetVarObj, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001812 slot, val);
1813 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001814 rsScriptSetVarObj((RsContext)con, (RsScript)script, slot, (RsObjectBase)val);
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001815}
1816
1817static void
Tim Murray460a0492013-11-19 12:45:54 -08001818nScriptSetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Stephen Hines031ec58c2010-10-11 10:54:21 -07001819{
Andreas Gampe67333922014-11-10 20:35:59 -08001820 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001821 ALOGD("nScriptSetVarJ, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001822 slot, val);
1823 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001824 rsScriptSetVarJ((RsContext)con, (RsScript)script, slot, val);
Stephen Hines031ec58c2010-10-11 10:54:21 -07001825}
1826
Tim Murray7c4caad2013-04-10 16:21:40 -07001827static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001828nScriptGetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001829{
Andreas Gampe67333922014-11-10 20:35:59 -08001830 if (kLogApi) {
1831 ALOGD("nScriptGetVarJ, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1832 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001833 jlong value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001834 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001835 return value;
1836}
1837
Stephen Hines031ec58c2010-10-11 10:54:21 -07001838static void
Tim Murray460a0492013-11-19 12:45:54 -08001839nScriptSetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, float val)
Jason Sams4d339932010-05-11 14:03:58 -07001840{
Andreas Gampe67333922014-11-10 20:35:59 -08001841 if (kLogApi) {
1842 ALOGD("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", (RsContext)con, (void *)script,
1843 slot, val);
1844 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001845 rsScriptSetVarF((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001846}
1847
Tim Murray7c4caad2013-04-10 16:21:40 -07001848static jfloat
Tim Murray460a0492013-11-19 12:45:54 -08001849nScriptGetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001850{
Andreas Gampe67333922014-11-10 20:35:59 -08001851 if (kLogApi) {
1852 ALOGD("nScriptGetVarF, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1853 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001854 jfloat value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001855 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001856 return value;
1857}
1858
Jason Sams4d339932010-05-11 14:03:58 -07001859static void
Tim Murray460a0492013-11-19 12:45:54 -08001860nScriptSetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, double val)
Stephen Hinesca54ec32010-09-20 17:20:30 -07001861{
Andreas Gampe67333922014-11-10 20:35:59 -08001862 if (kLogApi) {
1863 ALOGD("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", (RsContext)con, (void *)script,
1864 slot, val);
1865 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001866 rsScriptSetVarD((RsContext)con, (RsScript)script, slot, val);
Stephen Hinesca54ec32010-09-20 17:20:30 -07001867}
1868
Tim Murray7c4caad2013-04-10 16:21:40 -07001869static jdouble
Tim Murray460a0492013-11-19 12:45:54 -08001870nScriptGetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001871{
Andreas Gampe67333922014-11-10 20:35:59 -08001872 if (kLogApi) {
1873 ALOGD("nScriptGetVarD, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1874 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001875 jdouble value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001876 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001877 return value;
1878}
1879
Stephen Hinesca54ec32010-09-20 17:20:30 -07001880static void
Tim Murray460a0492013-11-19 12:45:54 -08001881nScriptSetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001882{
Andreas Gampe67333922014-11-10 20:35:59 -08001883 if (kLogApi) {
1884 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1885 }
Jason Sams4d339932010-05-11 14:03:58 -07001886 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001887 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001888 if (ptr == nullptr) {
1889 ALOGE("Failed to get Java array elements");
1890 return;
1891 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001892 rsScriptSetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001893 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1894}
1895
Stephen Hinesadeb8092012-04-20 14:26:06 -07001896static void
Tim Murray460a0492013-11-19 12:45:54 -08001897nScriptGetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Tim Murray7c4caad2013-04-10 16:21:40 -07001898{
Andreas Gampe67333922014-11-10 20:35:59 -08001899 if (kLogApi) {
1900 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1901 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001902 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001903 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001904 if (ptr == nullptr) {
1905 ALOGE("Failed to get Java array elements");
1906 return;
1907 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001908 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Stephen Hines414fa2c2014-04-17 01:02:42 -07001909 _env->ReleaseByteArrayElements(data, ptr, 0);
Tim Murray7c4caad2013-04-10 16:21:40 -07001910}
1911
1912static void
Andreas Gampe67333922014-11-10 20:35:59 -08001913nScriptSetVarVE(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data,
1914 jlong elem, jintArray dims)
Stephen Hinesadeb8092012-04-20 14:26:06 -07001915{
Andreas Gampe67333922014-11-10 20:35:59 -08001916 if (kLogApi) {
1917 ALOGD("nScriptSetVarVE, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1918 }
Stephen Hinesadeb8092012-04-20 14:26:06 -07001919 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001920 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001921 if (ptr == nullptr) {
1922 ALOGE("Failed to get Java array elements");
1923 return;
1924 }
Stephen Hinesadeb8092012-04-20 14:26:06 -07001925 jint dimsLen = _env->GetArrayLength(dims) * sizeof(int);
Chris Wailes488230c32014-08-14 11:22:40 -07001926 jint *dimsPtr = _env->GetIntArrayElements(dims, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001927 if (dimsPtr == nullptr) {
1928 ALOGE("Failed to get Java array elements");
1929 return;
1930 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001931 rsScriptSetVarVE((RsContext)con, (RsScript)script, slot, ptr, len, (RsElement)elem,
Stephen Hinesbc5d3ee2014-06-25 00:03:39 -07001932 (const uint32_t*) dimsPtr, dimsLen);
Stephen Hinesadeb8092012-04-20 14:26:06 -07001933 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1934 _env->ReleaseIntArrayElements(dims, dimsPtr, JNI_ABORT);
1935}
1936
Jason Samsd19f10d2009-05-22 14:03:28 -07001937
1938static void
Tim Murray460a0492013-11-19 12:45:54 -08001939nScriptSetTimeZone(JNIEnv *_env, jobject _this, jlong con, jlong script, jbyteArray timeZone)
Jason Samsd19f10d2009-05-22 14:03:28 -07001940{
Andreas Gampe67333922014-11-10 20:35:59 -08001941 if (kLogApi) {
1942 ALOGD("nScriptCSetTimeZone, con(%p), s(%p)", (RsContext)con, (void *)script);
1943 }
Romain Guy584a3752009-07-30 18:45:01 -07001944
1945 jint length = _env->GetArrayLength(timeZone);
1946 jbyte* timeZone_ptr;
1947 timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
Miao Wangba8766c2015-10-12 17:24:13 -07001948 if (timeZone_ptr == nullptr) {
1949 ALOGE("Failed to get Java array elements");
1950 return;
1951 }
Romain Guy584a3752009-07-30 18:45:01 -07001952
Tim Murrayeff663f2013-11-15 13:08:30 -08001953 rsScriptSetTimeZone((RsContext)con, (RsScript)script, (const char *)timeZone_ptr, length);
Romain Guy584a3752009-07-30 18:45:01 -07001954
1955 if (timeZone_ptr) {
1956 _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
1957 }
1958}
1959
Jason Samsfbf0b9e2009-08-13 12:59:04 -07001960static void
Tim Murray460a0492013-11-19 12:45:54 -08001961nScriptInvoke(JNIEnv *_env, jobject _this, jlong con, jlong obj, jint slot)
Jason Samsbe2e8412009-09-16 15:04:38 -07001962{
Andreas Gampe67333922014-11-10 20:35:59 -08001963 if (kLogApi) {
1964 ALOGD("nScriptInvoke, con(%p), script(%p)", (RsContext)con, (void *)obj);
1965 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001966 rsScriptInvoke((RsContext)con, (RsScript)obj, slot);
Jason Samsbe2e8412009-09-16 15:04:38 -07001967}
1968
1969static void
Tim Murray460a0492013-11-19 12:45:54 -08001970nScriptInvokeV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001971{
Andreas Gampe67333922014-11-10 20:35:59 -08001972 if (kLogApi) {
1973 ALOGD("nScriptInvokeV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1974 }
Jason Sams4d339932010-05-11 14:03:58 -07001975 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001976 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001977 if (ptr == nullptr) {
1978 ALOGE("Failed to get Java array elements");
1979 return;
1980 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001981 rsScriptInvokeV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001982 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1983}
1984
Jason Sams6e494d32011-04-27 16:33:11 -07001985static void
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001986nScriptForEach(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
1987 jlongArray ains, jlong aout, jbyteArray params,
1988 jintArray limits)
Jason Sams6e494d32011-04-27 16:33:11 -07001989{
Andreas Gampe67333922014-11-10 20:35:59 -08001990 if (kLogApi) {
Chih-Hung Hsieh9eb9dd32015-05-06 14:42:04 -07001991 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 -08001992 }
Jason Sams6e494d32011-04-27 16:33:11 -07001993
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001994 jint in_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07001995 jlong *in_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07001996
Chris Wailes488230c32014-08-14 11:22:40 -07001997 RsAllocation *in_allocs = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07001998
Chris Wailes488230c32014-08-14 11:22:40 -07001999 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002000 in_len = _env->GetArrayLength(ains);
Yang Ni7b2a46f2015-05-05 12:41:19 -07002001 if (in_len > (jint)RS_KERNEL_MAX_ARGUMENTS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -07002002 ALOGE("Too many arguments in kernel launch.");
2003 // TODO (b/20758983): Report back to Java and throw an exception
2004 return;
2005 }
Chris Wailes94961062014-06-11 12:01:28 -07002006
Yang Ni17c2d7a2015-04-30 16:13:54 -07002007 in_ptr = _env->GetLongArrayElements(ains, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002008 if (in_ptr == nullptr) {
2009 ALOGE("Failed to get Java array elements");
2010 return;
2011 }
2012
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002013 if (sizeof(RsAllocation) == sizeof(jlong)) {
2014 in_allocs = (RsAllocation*)in_ptr;
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002015 } else {
2016 // Convert from 64-bit jlong types to the native pointer type.
Chris Wailes94961062014-06-11 12:01:28 -07002017
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002018 in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
Yang Ni17c2d7a2015-04-30 16:13:54 -07002019 if (in_allocs == nullptr) {
2020 ALOGE("Failed launching kernel for lack of memory.");
2021 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2022 return;
2023 }
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002024
2025 for (int index = in_len; --index >= 0;) {
2026 in_allocs[index] = (RsAllocation)in_ptr[index];
2027 }
2028 }
Chris Wailes94961062014-06-11 12:01:28 -07002029 }
2030
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002031 jint param_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002032 jbyte *param_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002033
Chris Wailes488230c32014-08-14 11:22:40 -07002034 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002035 param_len = _env->GetArrayLength(params);
Chris Wailes488230c32014-08-14 11:22:40 -07002036 param_ptr = _env->GetByteArrayElements(params, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002037 if (param_ptr == nullptr) {
2038 ALOGE("Failed to get Java array elements");
2039 return;
2040 }
Chris Wailes94961062014-06-11 12:01:28 -07002041 }
2042
Chris Wailes488230c32014-08-14 11:22:40 -07002043 RsScriptCall sc, *sca = nullptr;
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002044 uint32_t sc_size = 0;
Chris Wailes94961062014-06-11 12:01:28 -07002045
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002046 jint limit_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002047 jint *limit_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002048
Chris Wailes488230c32014-08-14 11:22:40 -07002049 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002050 limit_len = _env->GetArrayLength(limits);
Chris Wailes488230c32014-08-14 11:22:40 -07002051 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002052 if (limit_ptr == nullptr) {
2053 ALOGE("Failed to get Java array elements");
2054 return;
2055 }
Chris Wailes94961062014-06-11 12:01:28 -07002056
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002057 assert(limit_len == 6);
Andreas Gampe67333922014-11-10 20:35:59 -08002058 UNUSED(limit_len); // As the assert might not be compiled.
Chris Wailes94961062014-06-11 12:01:28 -07002059
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002060 sc.xStart = limit_ptr[0];
2061 sc.xEnd = limit_ptr[1];
2062 sc.yStart = limit_ptr[2];
2063 sc.yEnd = limit_ptr[3];
2064 sc.zStart = limit_ptr[4];
2065 sc.zEnd = limit_ptr[5];
2066 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
Jason Sams14331ab2015-01-26 18:14:36 -08002067 sc.arrayStart = 0;
2068 sc.arrayEnd = 0;
2069 sc.array2Start = 0;
2070 sc.array2End = 0;
2071 sc.array3Start = 0;
2072 sc.array3End = 0;
2073 sc.array4Start = 0;
2074 sc.array4End = 0;
Chris Wailes94961062014-06-11 12:01:28 -07002075
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002076 sca = &sc;
Yang Nie8f2e442016-03-15 16:00:02 -07002077 // sc_size is required, but unused, by the runtime and drivers.
2078 sc_size = sizeof(sc);
Chris Wailes94961062014-06-11 12:01:28 -07002079 }
2080
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002081 rsScriptForEachMulti((RsContext)con, (RsScript)script, slot,
2082 in_allocs, in_len, (RsAllocation)aout,
2083 param_ptr, param_len, sca, sc_size);
Chris Wailes94961062014-06-11 12:01:28 -07002084
Chris Wailes488230c32014-08-14 11:22:40 -07002085 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002086 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
Chris Wailes94961062014-06-11 12:01:28 -07002087 }
2088
Chris Wailes488230c32014-08-14 11:22:40 -07002089 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002090 _env->ReleaseByteArrayElements(params, param_ptr, JNI_ABORT);
2091 }
2092
Chris Wailes488230c32014-08-14 11:22:40 -07002093 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002094 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
2095 }
Chris Wailes94961062014-06-11 12:01:28 -07002096}
2097
Matt Wala36eb1f72015-07-20 15:35:27 -07002098static void
2099nScriptReduce(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
David Gross4a457852016-06-02 14:46:55 -07002100 jlongArray ains, jlong aout, jintArray limits)
Matt Wala36eb1f72015-07-20 15:35:27 -07002101{
2102 if (kLogApi) {
David Gross4a457852016-06-02 14:46:55 -07002103 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 -08002104 }
2105
2106 if (ains == nullptr) {
2107 ALOGE("At least one input required.");
2108 // TODO (b/20758983): Report back to Java and throw an exception
2109 return;
2110 }
2111 jint in_len = _env->GetArrayLength(ains);
2112 if (in_len > (jint)RS_KERNEL_MAX_ARGUMENTS) {
2113 ALOGE("Too many arguments in kernel launch.");
2114 // TODO (b/20758983): Report back to Java and throw an exception
2115 return;
2116 }
2117
2118 jlong *in_ptr = _env->GetLongArrayElements(ains, nullptr);
2119 if (in_ptr == nullptr) {
2120 ALOGE("Failed to get Java array elements");
2121 // TODO (b/20758983): Report back to Java and throw an exception
2122 return;
2123 }
2124
2125 RsAllocation *in_allocs = nullptr;
2126 if (sizeof(RsAllocation) == sizeof(jlong)) {
2127 in_allocs = (RsAllocation*)in_ptr;
2128 } else {
2129 // Convert from 64-bit jlong types to the native pointer type.
2130
2131 in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
2132 if (in_allocs == nullptr) {
2133 ALOGE("Failed launching kernel for lack of memory.");
2134 // TODO (b/20758983): Report back to Java and throw an exception
2135 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2136 return;
2137 }
2138
2139 for (int index = in_len; --index >= 0;) {
2140 in_allocs[index] = (RsAllocation)in_ptr[index];
2141 }
2142 }
2143
2144 RsScriptCall sc, *sca = nullptr;
2145 uint32_t sc_size = 0;
2146
2147 jint limit_len = 0;
2148 jint *limit_ptr = nullptr;
2149
2150 if (limits != nullptr) {
2151 limit_len = _env->GetArrayLength(limits);
2152 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
2153 if (limit_ptr == nullptr) {
2154 ALOGE("Failed to get Java array elements");
2155 // TODO (b/20758983): Report back to Java and throw an exception
2156 return;
2157 }
2158
2159 assert(limit_len == 6);
2160 UNUSED(limit_len); // As the assert might not be compiled.
2161
2162 sc.xStart = limit_ptr[0];
2163 sc.xEnd = limit_ptr[1];
2164 sc.yStart = limit_ptr[2];
2165 sc.yEnd = limit_ptr[3];
2166 sc.zStart = limit_ptr[4];
2167 sc.zEnd = limit_ptr[5];
2168 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
2169 sc.arrayStart = 0;
2170 sc.arrayEnd = 0;
2171 sc.array2Start = 0;
2172 sc.array2End = 0;
2173 sc.array3Start = 0;
2174 sc.array3End = 0;
2175 sc.array4Start = 0;
2176 sc.array4End = 0;
2177
2178 sca = &sc;
2179 sc_size = sizeof(sc);
2180 }
2181
David Gross4a457852016-06-02 14:46:55 -07002182 rsScriptReduce((RsContext)con, (RsScript)script, slot,
2183 in_allocs, in_len, (RsAllocation)aout,
2184 sca, sc_size);
David Gross26ef7a732016-01-12 12:19:15 -08002185
2186 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2187
2188 if (limits != nullptr) {
2189 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
2190 }
2191}
2192
Jason Sams22534172009-08-04 16:58:20 -07002193// -----------------------------------
2194
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002195static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002196nScriptCCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Samse4a06c52011-03-16 16:29:28 -07002197 jstring resName, jstring cacheDir,
2198 jbyteArray scriptRef, jint length)
Jason Sams22534172009-08-04 16:58:20 -07002199{
Andreas Gampe67333922014-11-10 20:35:59 -08002200 if (kLogApi) {
2201 ALOGD("nScriptCCreate, con(%p)", (RsContext)con);
2202 }
Jason Sams22534172009-08-04 16:58:20 -07002203
Jason Samse4a06c52011-03-16 16:29:28 -07002204 AutoJavaStringToUTF8 resNameUTF(_env, resName);
2205 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002206 jlong ret = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002207 jbyte* script_ptr = nullptr;
Jack Palevich43702d82009-05-28 13:38:16 -07002208 jint _exception = 0;
2209 jint remaining;
Jack Palevich43702d82009-05-28 13:38:16 -07002210 if (!scriptRef) {
2211 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002212 //jniThrowException(_env, "java/lang/IllegalArgumentException", "script == null");
Jack Palevich43702d82009-05-28 13:38:16 -07002213 goto exit;
2214 }
Jack Palevich43702d82009-05-28 13:38:16 -07002215 if (length < 0) {
2216 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002217 //jniThrowException(_env, "java/lang/IllegalArgumentException", "length < 0");
Jack Palevich43702d82009-05-28 13:38:16 -07002218 goto exit;
2219 }
Jason Samse4a06c52011-03-16 16:29:28 -07002220 remaining = _env->GetArrayLength(scriptRef);
Jack Palevich43702d82009-05-28 13:38:16 -07002221 if (remaining < length) {
2222 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002223 //jniThrowException(_env, "java/lang/IllegalArgumentException",
2224 // "length > script.length - offset");
Jack Palevich43702d82009-05-28 13:38:16 -07002225 goto exit;
2226 }
Jason Samse4a06c52011-03-16 16:29:28 -07002227 script_ptr = (jbyte *)
Jack Palevich43702d82009-05-28 13:38:16 -07002228 _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
Miao Wangba8766c2015-10-12 17:24:13 -07002229 if (script_ptr == nullptr) {
2230 ALOGE("Failed to get Java array elements");
2231 return ret;
2232 }
Jack Palevich43702d82009-05-28 13:38:16 -07002233
Tim Murrayeff663f2013-11-15 13:08:30 -08002234 //rsScriptCSetText((RsContext)con, (const char *)script_ptr, length);
Jason Samse4a06c52011-03-16 16:29:28 -07002235
Tim Murray3aa89c12014-08-18 17:51:22 -07002236 ret = (jlong)(uintptr_t)rsScriptCCreate((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07002237 resNameUTF.c_str(), resNameUTF.length(),
2238 cacheDirUTF.c_str(), cacheDirUTF.length(),
Jason Samse4a06c52011-03-16 16:29:28 -07002239 (const char *)script_ptr, length);
Jason Sams39ddc9502009-06-05 17:35:09 -07002240
Jack Palevich43702d82009-05-28 13:38:16 -07002241exit:
Jason Samse4a06c52011-03-16 16:29:28 -07002242 if (script_ptr) {
2243 _env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr,
Jack Palevich43702d82009-05-28 13:38:16 -07002244 _exception ? JNI_ABORT: 0);
2245 }
Jason Samsd19f10d2009-05-22 14:03:28 -07002246
Tim Murray3aa89c12014-08-18 17:51:22 -07002247 return (jlong)(uintptr_t)ret;
Jason Samsd19f10d2009-05-22 14:03:28 -07002248}
2249
Tim Murray460a0492013-11-19 12:45:54 -08002250static jlong
2251nScriptIntrinsicCreate(JNIEnv *_env, jobject _this, jlong con, jint id, jlong eid)
Jason Sams6ab97682012-08-10 12:09:43 -07002252{
Andreas Gampe67333922014-11-10 20:35:59 -08002253 if (kLogApi) {
2254 ALOGD("nScriptIntrinsicCreate, con(%p) id(%i) element(%p)", (RsContext)con, id,
2255 (void *)eid);
2256 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002257 return (jlong)(uintptr_t)rsScriptIntrinsicCreate((RsContext)con, id, (RsElement)eid);
Jason Sams6ab97682012-08-10 12:09:43 -07002258}
2259
Tim Murray460a0492013-11-19 12:45:54 -08002260static jlong
2261nScriptKernelIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot, jint sig)
Jason Sams08a81582012-09-18 12:32:10 -07002262{
Andreas Gampe67333922014-11-10 20:35:59 -08002263 if (kLogApi) {
2264 ALOGD("nScriptKernelIDCreate, con(%p) script(%p), slot(%i), sig(%i)", (RsContext)con,
2265 (void *)sid, slot, sig);
2266 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002267 return (jlong)(uintptr_t)rsScriptKernelIDCreate((RsContext)con, (RsScript)sid, slot, sig);
Jason Sams08a81582012-09-18 12:32:10 -07002268}
2269
Tim Murray460a0492013-11-19 12:45:54 -08002270static jlong
Yang Nibe392ad2015-01-23 17:16:02 -08002271nScriptInvokeIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
2272{
2273 if (kLogApi) {
Elliott Hughes7ff53fa2015-02-05 21:36:10 -08002274 ALOGD("nScriptInvokeIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con,
Yang Nibe392ad2015-01-23 17:16:02 -08002275 (void *)sid, slot);
2276 }
2277 return (jlong)(uintptr_t)rsScriptInvokeIDCreate((RsContext)con, (RsScript)sid, slot);
2278}
2279
2280static jlong
Tim Murray460a0492013-11-19 12:45:54 -08002281nScriptFieldIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
Jason Sams08a81582012-09-18 12:32:10 -07002282{
Andreas Gampe67333922014-11-10 20:35:59 -08002283 if (kLogApi) {
2284 ALOGD("nScriptFieldIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con, (void *)sid,
2285 slot);
2286 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002287 return (jlong)(uintptr_t)rsScriptFieldIDCreate((RsContext)con, (RsScript)sid, slot);
Jason Sams08a81582012-09-18 12:32:10 -07002288}
2289
Tim Murray460a0492013-11-19 12:45:54 -08002290static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002291nScriptGroupCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _kernels, jlongArray _src,
2292 jlongArray _dstk, jlongArray _dstf, jlongArray _types)
Jason Sams08a81582012-09-18 12:32:10 -07002293{
Andreas Gampe67333922014-11-10 20:35:59 -08002294 if (kLogApi) {
2295 ALOGD("nScriptGroupCreate, con(%p)", (RsContext)con);
2296 }
Jason Sams08a81582012-09-18 12:32:10 -07002297
Miao Wanga4ad5f82016-02-11 12:32:39 -08002298 jlong id = 0;
2299
2300 RsScriptKernelID* kernelsPtr;
Ashok Bhat98071552014-02-12 09:54:43 +00002301 jint kernelsLen = _env->GetArrayLength(_kernels);
Chris Wailes488230c32014-08-14 11:22:40 -07002302 jlong *jKernelsPtr = _env->GetLongArrayElements(_kernels, nullptr);
Miao Wanga4ad5f82016-02-11 12:32:39 -08002303
2304 RsScriptKernelID* srcPtr;
2305 jint srcLen = _env->GetArrayLength(_src);
2306 jlong *jSrcPtr = _env->GetLongArrayElements(_src, nullptr);
2307
2308 RsScriptKernelID* dstkPtr;
2309 jint dstkLen = _env->GetArrayLength(_dstk);
2310 jlong *jDstkPtr = _env->GetLongArrayElements(_dstk, nullptr);
2311
2312 RsScriptKernelID* dstfPtr;
2313 jint dstfLen = _env->GetArrayLength(_dstf);
2314 jlong *jDstfPtr = _env->GetLongArrayElements(_dstf, nullptr);
2315
2316 RsType* typesPtr;
2317 jint typesLen = _env->GetArrayLength(_types);
2318 jlong *jTypesPtr = _env->GetLongArrayElements(_types, nullptr);
2319
Miao Wangba8766c2015-10-12 17:24:13 -07002320 if (jKernelsPtr == nullptr) {
2321 ALOGE("Failed to get Java array elements: kernels");
Miao Wanga4ad5f82016-02-11 12:32:39 -08002322 goto cleanup;
Miao Wangba8766c2015-10-12 17:24:13 -07002323 }
Miao Wanga4ad5f82016-02-11 12:32:39 -08002324 if (jSrcPtr == nullptr) {
2325 ALOGE("Failed to get Java array elements: src");
2326 goto cleanup;
2327 }
2328 if (jDstkPtr == nullptr) {
2329 ALOGE("Failed to get Java array elements: dstk");
2330 goto cleanup;
2331 }
2332 if (jDstfPtr == nullptr) {
2333 ALOGE("Failed to get Java array elements: dstf");
2334 goto cleanup;
2335 }
2336 if (jTypesPtr == nullptr) {
2337 ALOGE("Failed to get Java array elements: types");
2338 goto cleanup;
2339 }
2340
2341 kernelsPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * kernelsLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002342 for(int i = 0; i < kernelsLen; ++i) {
2343 kernelsPtr[i] = (RsScriptKernelID)jKernelsPtr[i];
2344 }
Jason Sams08a81582012-09-18 12:32:10 -07002345
Miao Wanga4ad5f82016-02-11 12:32:39 -08002346 srcPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * srcLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002347 for(int i = 0; i < srcLen; ++i) {
2348 srcPtr[i] = (RsScriptKernelID)jSrcPtr[i];
2349 }
Jason Sams08a81582012-09-18 12:32:10 -07002350
Miao Wanga4ad5f82016-02-11 12:32:39 -08002351 dstkPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstkLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002352 for(int i = 0; i < dstkLen; ++i) {
2353 dstkPtr[i] = (RsScriptKernelID)jDstkPtr[i];
2354 }
2355
Miao Wanga4ad5f82016-02-11 12:32:39 -08002356 dstfPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstfLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002357 for(int i = 0; i < dstfLen; ++i) {
2358 dstfPtr[i] = (RsScriptKernelID)jDstfPtr[i];
2359 }
2360
Miao Wanga4ad5f82016-02-11 12:32:39 -08002361 typesPtr = (RsType*) malloc(sizeof(RsType) * typesLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002362 for(int i = 0; i < typesLen; ++i) {
2363 typesPtr[i] = (RsType)jTypesPtr[i];
2364 }
2365
Miao Wanga4ad5f82016-02-11 12:32:39 -08002366 id = (jlong)(uintptr_t)rsScriptGroupCreate((RsContext)con,
Ashok Bhat98071552014-02-12 09:54:43 +00002367 (RsScriptKernelID *)kernelsPtr, kernelsLen * sizeof(RsScriptKernelID),
2368 (RsScriptKernelID *)srcPtr, srcLen * sizeof(RsScriptKernelID),
2369 (RsScriptKernelID *)dstkPtr, dstkLen * sizeof(RsScriptKernelID),
2370 (RsScriptFieldID *)dstfPtr, dstfLen * sizeof(RsScriptKernelID),
2371 (RsType *)typesPtr, typesLen * sizeof(RsType));
2372
2373 free(kernelsPtr);
2374 free(srcPtr);
2375 free(dstkPtr);
2376 free(dstfPtr);
2377 free(typesPtr);
Miao Wanga4ad5f82016-02-11 12:32:39 -08002378
2379cleanup:
2380 if (jKernelsPtr != nullptr) {
2381 _env->ReleaseLongArrayElements(_kernels, jKernelsPtr, 0);
2382 }
2383 if (jSrcPtr != nullptr) {
2384 _env->ReleaseLongArrayElements(_src, jSrcPtr, 0);
2385 }
2386 if (jDstkPtr != nullptr) {
2387 _env->ReleaseLongArrayElements(_dstk, jDstkPtr, 0);
2388 }
2389 if (jDstfPtr != nullptr) {
2390 _env->ReleaseLongArrayElements(_dstf, jDstfPtr, 0);
2391 }
2392 if (jTypesPtr != nullptr) {
2393 _env->ReleaseLongArrayElements(_types, jTypesPtr, 0);
2394 }
2395
Jason Sams08a81582012-09-18 12:32:10 -07002396 return id;
2397}
2398
2399static void
Tim Murray460a0492013-11-19 12:45:54 -08002400nScriptGroupSetInput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002401{
Andreas Gampe67333922014-11-10 20:35:59 -08002402 if (kLogApi) {
2403 ALOGD("nScriptGroupSetInput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2404 (void *)gid, (void *)kid, (void *)alloc);
2405 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002406 rsScriptGroupSetInput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002407}
2408
2409static void
Tim Murray460a0492013-11-19 12:45:54 -08002410nScriptGroupSetOutput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002411{
Andreas Gampe67333922014-11-10 20:35:59 -08002412 if (kLogApi) {
2413 ALOGD("nScriptGroupSetOutput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2414 (void *)gid, (void *)kid, (void *)alloc);
2415 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002416 rsScriptGroupSetOutput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002417}
2418
2419static void
Tim Murray460a0492013-11-19 12:45:54 -08002420nScriptGroupExecute(JNIEnv *_env, jobject _this, jlong con, jlong gid)
Jason Sams08a81582012-09-18 12:32:10 -07002421{
Andreas Gampe67333922014-11-10 20:35:59 -08002422 if (kLogApi) {
2423 ALOGD("nScriptGroupSetOutput, con(%p) group(%p)", (RsContext)con, (void *)gid);
2424 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002425 rsScriptGroupExecute((RsContext)con, (RsScriptGroup)gid);
Jason Sams08a81582012-09-18 12:32:10 -07002426}
2427
Jason Samsd19f10d2009-05-22 14:03:28 -07002428// ---------------------------------------------------------------------------
2429
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002430static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002431nProgramStoreCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Sams331bf9b2011-04-06 11:23:54 -07002432 jboolean colorMaskR, jboolean colorMaskG, jboolean colorMaskB, jboolean colorMaskA,
2433 jboolean depthMask, jboolean ditherEnable,
2434 jint srcFunc, jint destFunc,
2435 jint depthFunc)
Jason Samsd19f10d2009-05-22 14:03:28 -07002436{
Andreas Gampe67333922014-11-10 20:35:59 -08002437 if (kLogApi) {
2438 ALOGD("nProgramStoreCreate, con(%p)", (RsContext)con);
2439 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002440 return (jlong)(uintptr_t)rsProgramStoreCreate((RsContext)con, colorMaskR, colorMaskG, colorMaskB, colorMaskA,
Jason Sams331bf9b2011-04-06 11:23:54 -07002441 depthMask, ditherEnable, (RsBlendSrcFunc)srcFunc,
2442 (RsBlendDstFunc)destFunc, (RsDepthFunc)depthFunc);
Jason Samsd19f10d2009-05-22 14:03:28 -07002443}
2444
Jason Sams0011bcf2009-12-15 12:58:36 -08002445// ---------------------------------------------------------------------------
2446
2447static void
Tim Murray460a0492013-11-19 12:45:54 -08002448nProgramBindConstants(JNIEnv *_env, jobject _this, jlong con, jlong vpv, jint slot, jlong a)
Jason Sams0011bcf2009-12-15 12:58:36 -08002449{
Andreas Gampe67333922014-11-10 20:35:59 -08002450 if (kLogApi) {
2451 ALOGD("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", (RsContext)con,
2452 (RsProgramVertex)vpv, slot, (RsAllocation)a);
2453 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002454 rsProgramBindConstants((RsContext)con, (RsProgram)vpv, slot, (RsAllocation)a);
Jason Sams0011bcf2009-12-15 12:58:36 -08002455}
Jason Sams54c0ec12009-11-30 14:49:55 -08002456
Jason Sams68afd012009-12-17 16:55:08 -08002457static void
Tim Murray460a0492013-11-19 12:45:54 -08002458nProgramBindTexture(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002459{
Andreas Gampe67333922014-11-10 20:35:59 -08002460 if (kLogApi) {
2461 ALOGD("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2462 (RsProgramFragment)vpf, slot, (RsAllocation)a);
2463 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002464 rsProgramBindTexture((RsContext)con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
Jason Sams68afd012009-12-17 16:55:08 -08002465}
2466
2467static void
Tim Murray460a0492013-11-19 12:45:54 -08002468nProgramBindSampler(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002469{
Andreas Gampe67333922014-11-10 20:35:59 -08002470 if (kLogApi) {
2471 ALOGD("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2472 (RsProgramFragment)vpf, slot, (RsSampler)a);
2473 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002474 rsProgramBindSampler((RsContext)con, (RsProgramFragment)vpf, slot, (RsSampler)a);
Jason Sams68afd012009-12-17 16:55:08 -08002475}
2476
Jason Samsd19f10d2009-05-22 14:03:28 -07002477// ---------------------------------------------------------------------------
2478
Tim Murray460a0492013-11-19 12:45:54 -08002479static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002480nProgramFragmentCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002481 jobjectArray texNames, jlongArray params)
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002482{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002483 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002484 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002485 jint paramLen = _env->GetArrayLength(params);
Miao Wangba8766c2015-10-12 17:24:13 -07002486 if (jParamPtr == nullptr) {
2487 ALOGE("Failed to get Java array elements");
2488 return 0;
2489 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002490
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002491 int texCount = _env->GetArrayLength(texNames);
2492 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2493 const char ** nameArray = names.c_str();
2494 size_t* sizeArray = names.c_str_len();
2495
Andreas Gampe67333922014-11-10 20:35:59 -08002496 if (kLogApi) {
2497 ALOGD("nProgramFragmentCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2498 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002499
Ashok Bhat98071552014-02-12 09:54:43 +00002500 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2501 for(int i = 0; i < paramLen; ++i) {
2502 paramPtr[i] = (uintptr_t)jParamPtr[i];
2503 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002504 jlong ret = (jlong)(uintptr_t)rsProgramFragmentCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002505 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002506 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002507
Ashok Bhat98071552014-02-12 09:54:43 +00002508 free(paramPtr);
2509 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002510 return ret;
2511}
2512
2513
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002514// ---------------------------------------------------------------------------
2515
Tim Murray460a0492013-11-19 12:45:54 -08002516static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002517nProgramVertexCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002518 jobjectArray texNames, jlongArray params)
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002519{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002520 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002521 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams0011bcf2009-12-15 12:58:36 -08002522 jint paramLen = _env->GetArrayLength(params);
Miao Wangba8766c2015-10-12 17:24:13 -07002523 if (jParamPtr == nullptr) {
2524 ALOGE("Failed to get Java array elements");
2525 return 0;
2526 }
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002527
Andreas Gampe67333922014-11-10 20:35:59 -08002528 if (kLogApi) {
2529 ALOGD("nProgramVertexCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2530 }
Jason Sams0011bcf2009-12-15 12:58:36 -08002531
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002532 int texCount = _env->GetArrayLength(texNames);
2533 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2534 const char ** nameArray = names.c_str();
2535 size_t* sizeArray = names.c_str_len();
2536
Ashok Bhat98071552014-02-12 09:54:43 +00002537 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2538 for(int i = 0; i < paramLen; ++i) {
2539 paramPtr[i] = (uintptr_t)jParamPtr[i];
2540 }
2541
Tim Murray3aa89c12014-08-18 17:51:22 -07002542 jlong ret = (jlong)(uintptr_t)rsProgramVertexCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002543 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002544 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002545
Ashok Bhat98071552014-02-12 09:54:43 +00002546 free(paramPtr);
2547 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams0011bcf2009-12-15 12:58:36 -08002548 return ret;
2549}
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002550
Jason Samsebfb4362009-09-23 13:57:02 -07002551// ---------------------------------------------------------------------------
2552
Tim Murray460a0492013-11-19 12:45:54 -08002553static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002554nProgramRasterCreate(JNIEnv *_env, jobject _this, jlong con, jboolean pointSprite, jint cull)
Jason Samsebfb4362009-09-23 13:57:02 -07002555{
Andreas Gampe67333922014-11-10 20:35:59 -08002556 if (kLogApi) {
2557 ALOGD("nProgramRasterCreate, con(%p), pointSprite(%i), cull(%i)", (RsContext)con,
2558 pointSprite, cull);
2559 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002560 return (jlong)(uintptr_t)rsProgramRasterCreate((RsContext)con, pointSprite, (RsCullMode)cull);
Jason Samsebfb4362009-09-23 13:57:02 -07002561}
2562
Jason Samsd19f10d2009-05-22 14:03:28 -07002563
2564// ---------------------------------------------------------------------------
2565
2566static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002567nContextBindRootScript(JNIEnv *_env, jobject _this, jlong con, jlong script)
Jason Samsd19f10d2009-05-22 14:03:28 -07002568{
Andreas Gampe67333922014-11-10 20:35:59 -08002569 if (kLogApi) {
2570 ALOGD("nContextBindRootScript, con(%p), script(%p)", (RsContext)con, (RsScript)script);
2571 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002572 rsContextBindRootScript((RsContext)con, (RsScript)script);
Jason Samsd19f10d2009-05-22 14:03:28 -07002573}
2574
2575static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002576nContextBindProgramStore(JNIEnv *_env, jobject _this, jlong con, jlong pfs)
Jason Samsd19f10d2009-05-22 14:03:28 -07002577{
Andreas Gampe67333922014-11-10 20:35:59 -08002578 if (kLogApi) {
2579 ALOGD("nContextBindProgramStore, con(%p), pfs(%p)", (RsContext)con, (RsProgramStore)pfs);
2580 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002581 rsContextBindProgramStore((RsContext)con, (RsProgramStore)pfs);
Jason Samsd19f10d2009-05-22 14:03:28 -07002582}
2583
2584static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002585nContextBindProgramFragment(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsd19f10d2009-05-22 14:03:28 -07002586{
Andreas Gampe67333922014-11-10 20:35:59 -08002587 if (kLogApi) {
2588 ALOGD("nContextBindProgramFragment, con(%p), pf(%p)", (RsContext)con,
2589 (RsProgramFragment)pf);
2590 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002591 rsContextBindProgramFragment((RsContext)con, (RsProgramFragment)pf);
Jason Samsd19f10d2009-05-22 14:03:28 -07002592}
2593
Jason Sams0826a6f2009-06-15 19:04:56 -07002594static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002595nContextBindProgramVertex(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Sams0826a6f2009-06-15 19:04:56 -07002596{
Andreas Gampe67333922014-11-10 20:35:59 -08002597 if (kLogApi) {
2598 ALOGD("nContextBindProgramVertex, con(%p), pf(%p)", (RsContext)con, (RsProgramVertex)pf);
2599 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002600 rsContextBindProgramVertex((RsContext)con, (RsProgramVertex)pf);
Jason Sams0826a6f2009-06-15 19:04:56 -07002601}
2602
Joe Onoratod7b37742009-08-09 22:57:44 -07002603static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002604nContextBindProgramRaster(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsebfb4362009-09-23 13:57:02 -07002605{
Andreas Gampe67333922014-11-10 20:35:59 -08002606 if (kLogApi) {
2607 ALOGD("nContextBindProgramRaster, con(%p), pf(%p)", (RsContext)con, (RsProgramRaster)pf);
2608 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002609 rsContextBindProgramRaster((RsContext)con, (RsProgramRaster)pf);
Jason Samsebfb4362009-09-23 13:57:02 -07002610}
2611
Joe Onoratod7b37742009-08-09 22:57:44 -07002612
Jason Sams02fb2cb2009-05-28 15:37:57 -07002613// ---------------------------------------------------------------------------
2614
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002615static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002616nSamplerCreate(JNIEnv *_env, jobject _this, jlong con, jint magFilter, jint minFilter,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002617 jint wrapS, jint wrapT, jint wrapR, jfloat aniso)
Jason Sams02fb2cb2009-05-28 15:37:57 -07002618{
Andreas Gampe67333922014-11-10 20:35:59 -08002619 if (kLogApi) {
2620 ALOGD("nSamplerCreate, con(%p)", (RsContext)con);
2621 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002622 return (jlong)(uintptr_t)rsSamplerCreate((RsContext)con,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002623 (RsSamplerValue)magFilter,
2624 (RsSamplerValue)minFilter,
2625 (RsSamplerValue)wrapS,
2626 (RsSamplerValue)wrapT,
2627 (RsSamplerValue)wrapR,
2628 aniso);
Jason Sams02fb2cb2009-05-28 15:37:57 -07002629}
2630
Jason Samsbba134c2009-06-22 15:49:21 -07002631// ---------------------------------------------------------------------------
2632
Tim Murray460a0492013-11-19 12:45:54 -08002633static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002634nMeshCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _vtx, jlongArray _idx, jintArray _prim)
Jason Samsbba134c2009-06-22 15:49:21 -07002635{
Andreas Gampe67333922014-11-10 20:35:59 -08002636 if (kLogApi) {
2637 ALOGD("nMeshCreate, con(%p)", (RsContext)con);
2638 }
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002639
Miao Wanga4ad5f82016-02-11 12:32:39 -08002640 jlong id = 0;
2641
2642 RsAllocation* vtxPtr;
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002643 jint vtxLen = _env->GetArrayLength(_vtx);
Chris Wailes488230c32014-08-14 11:22:40 -07002644 jlong *jVtxPtr = _env->GetLongArrayElements(_vtx, nullptr);
Miao Wanga4ad5f82016-02-11 12:32:39 -08002645
2646 RsAllocation* idxPtr;
2647 jint idxLen = _env->GetArrayLength(_idx);
2648 jlong *jIdxPtr = _env->GetLongArrayElements(_idx, nullptr);
2649
2650 jint primLen = _env->GetArrayLength(_prim);
2651 jint *primPtr = _env->GetIntArrayElements(_prim, nullptr);
2652
Miao Wangba8766c2015-10-12 17:24:13 -07002653 if (jVtxPtr == nullptr) {
2654 ALOGE("Failed to get Java array elements: vtx");
Miao Wanga4ad5f82016-02-11 12:32:39 -08002655 goto cleanupMesh;
Miao Wangba8766c2015-10-12 17:24:13 -07002656 }
Miao Wanga4ad5f82016-02-11 12:32:39 -08002657 if (jIdxPtr == nullptr) {
2658 ALOGE("Failed to get Java array elements: idx");
2659 goto cleanupMesh;
2660 }
2661 if (primPtr == nullptr) {
2662 ALOGE("Failed to get Java array elements: prim");
2663 goto cleanupMesh;
2664 }
2665
2666 vtxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * vtxLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002667 for(int i = 0; i < vtxLen; ++i) {
2668 vtxPtr[i] = (RsAllocation)(uintptr_t)jVtxPtr[i];
2669 }
2670
Miao Wanga4ad5f82016-02-11 12:32:39 -08002671 idxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * idxLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002672 for(int i = 0; i < idxLen; ++i) {
2673 idxPtr[i] = (RsAllocation)(uintptr_t)jIdxPtr[i];
2674 }
2675
Miao Wanga4ad5f82016-02-11 12:32:39 -08002676 id = (jlong)(uintptr_t)rsMeshCreate((RsContext)con,
2677 (RsAllocation *)vtxPtr, vtxLen,
2678 (RsAllocation *)idxPtr, idxLen,
2679 (uint32_t *)primPtr, primLen);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002680
Ashok Bhat98071552014-02-12 09:54:43 +00002681 free(vtxPtr);
2682 free(idxPtr);
Miao Wanga4ad5f82016-02-11 12:32:39 -08002683
2684cleanupMesh:
2685 if (jVtxPtr != nullptr) {
2686 _env->ReleaseLongArrayElements(_vtx, jVtxPtr, 0);
2687 }
2688 if (jIdxPtr != nullptr) {
2689 _env->ReleaseLongArrayElements(_idx, jIdxPtr, 0);
2690 }
2691 if (primPtr != nullptr) {
2692 _env->ReleaseIntArrayElements(_prim, primPtr, 0);
2693 }
2694
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002695 return id;
2696}
2697
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002698static jint
Tim Murray460a0492013-11-19 12:45:54 -08002699nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002700{
Andreas Gampe67333922014-11-10 20:35:59 -08002701 if (kLogApi) {
2702 ALOGD("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2703 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002704 jint vtxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002705 rsaMeshGetVertexBufferCount((RsContext)con, (RsMesh)mesh, &vtxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002706 return vtxCount;
2707}
2708
2709static jint
Tim Murray460a0492013-11-19 12:45:54 -08002710nMeshGetIndexCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002711{
Andreas Gampe67333922014-11-10 20:35:59 -08002712 if (kLogApi) {
2713 ALOGD("nMeshGetIndexCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2714 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002715 jint idxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002716 rsaMeshGetIndexCount((RsContext)con, (RsMesh)mesh, &idxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002717 return idxCount;
2718}
2719
2720static void
Ashok Bhat98071552014-02-12 09:54:43 +00002721nMeshGetVertices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _ids, jint numVtxIDs)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002722{
Andreas Gampe67333922014-11-10 20:35:59 -08002723 if (kLogApi) {
2724 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2725 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002726
2727 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
Tim Murrayeff663f2013-11-15 13:08:30 -08002728 rsaMeshGetVertices((RsContext)con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002729
2730 for(jint i = 0; i < numVtxIDs; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002731 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002732 _env->SetLongArrayRegion(_ids, i, 1, &alloc);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002733 }
2734
2735 free(allocs);
2736}
2737
2738static void
Ashok Bhat98071552014-02-12 09:54:43 +00002739nMeshGetIndices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _idxIds, jintArray _primitives, jint numIndices)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002740{
Andreas Gampe67333922014-11-10 20:35:59 -08002741 if (kLogApi) {
2742 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2743 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002744
2745 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
2746 uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
2747
Tim Murrayeff663f2013-11-15 13:08:30 -08002748 rsaMeshGetIndices((RsContext)con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002749
2750 for(jint i = 0; i < numIndices; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002751 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002752 const jint prim = (jint)prims[i];
2753 _env->SetLongArrayRegion(_idxIds, i, 1, &alloc);
2754 _env->SetIntArrayRegion(_primitives, i, 1, &prim);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002755 }
2756
2757 free(allocs);
2758 free(prims);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002759}
2760
Tim Murray56f9e6f2014-05-16 11:47:26 -07002761static jint
2762nSystemGetPointerSize(JNIEnv *_env, jobject _this) {
2763 return (jint)sizeof(void*);
2764}
2765
Miao Wang0facf022015-11-25 11:21:13 -08002766static jobject
2767nAllocationGetByteBuffer(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
2768 jlongArray strideArr, jint xBytesSize,
2769 jint dimY, jint dimZ) {
2770 if (kLogApi) {
2771 ALOGD("nAllocationGetByteBuffer, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
2772 }
Tim Murray56f9e6f2014-05-16 11:47:26 -07002773
Miao Wang0facf022015-11-25 11:21:13 -08002774 jlong *jStridePtr = _env->GetLongArrayElements(strideArr, nullptr);
2775 if (jStridePtr == nullptr) {
2776 ALOGE("Failed to get Java array elements: strideArr");
2777 return 0;
2778 }
2779
2780 size_t strideIn = xBytesSize;
2781 void* ptr = nullptr;
2782 if (alloc != 0) {
2783 ptr = rsAllocationGetPointer((RsContext)con, (RsAllocation)alloc, 0,
2784 RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, 0, 0,
2785 &strideIn, sizeof(size_t));
2786 }
2787
2788 jobject byteBuffer = nullptr;
2789 if (ptr != nullptr) {
2790 size_t bufferSize = strideIn;
2791 jStridePtr[0] = strideIn;
2792 if (dimY > 0) {
2793 bufferSize *= dimY;
2794 }
2795 if (dimZ > 0) {
2796 bufferSize *= dimZ;
2797 }
2798 byteBuffer = _env->NewDirectByteBuffer(ptr, (jlong) bufferSize);
2799 }
2800 _env->ReleaseLongArrayElements(strideArr, jStridePtr, 0);
2801 return byteBuffer;
2802}
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002803// ---------------------------------------------------------------------------
2804
Jason Samsd19f10d2009-05-22 14:03:28 -07002805
Jason Sams94d8e90a2009-06-10 16:09:05 -07002806static const char *classPathName = "android/renderscript/RenderScript";
Jason Samsd19f10d2009-05-22 14:03:28 -07002807
Daniel Micay76f6a862015-09-19 17:31:01 -04002808static const JNINativeMethod methods[] = {
Jason Sams1c415172010-11-08 17:06:46 -08002809{"_nInit", "()V", (void*)_nInit },
Jason Samsea84a7c2009-09-04 14:42:41 -07002810
Tim Murrayeff663f2013-11-15 13:08:30 -08002811{"nDeviceCreate", "()J", (void*)nDeviceCreate },
2812{"nDeviceDestroy", "(J)V", (void*)nDeviceDestroy },
2813{"nDeviceSetConfig", "(JII)V", (void*)nDeviceSetConfig },
2814{"nContextGetUserMessage", "(J[I)I", (void*)nContextGetUserMessage },
2815{"nContextGetErrorMessage", "(J)Ljava/lang/String;", (void*)nContextGetErrorMessage },
2816{"nContextPeekMessage", "(J[I)I", (void*)nContextPeekMessage },
Jason Sams1c415172010-11-08 17:06:46 -08002817
Tim Murrayeff663f2013-11-15 13:08:30 -08002818{"nContextInitToClient", "(J)V", (void*)nContextInitToClient },
2819{"nContextDeinitToClient", "(J)V", (void*)nContextDeinitToClient },
Jason Samsd19f10d2009-05-22 14:03:28 -07002820
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002821
Jason Sams2e1872f2010-08-17 16:25:41 -07002822// All methods below are thread protected in java.
Tim Murrayeff663f2013-11-15 13:08:30 -08002823{"rsnContextCreate", "(JIII)J", (void*)nContextCreate },
2824{"rsnContextCreateGL", "(JIIIIIIIIIIIIFI)J", (void*)nContextCreateGL },
2825{"rsnContextFinish", "(J)V", (void*)nContextFinish },
2826{"rsnContextSetPriority", "(JI)V", (void*)nContextSetPriority },
Tim Murray47f31582015-04-07 15:43:24 -07002827{"rsnContextSetCacheDir", "(JLjava/lang/String;)V", (void*)nContextSetCacheDir },
Tim Murrayeff663f2013-11-15 13:08:30 -08002828{"rsnContextSetSurface", "(JIILandroid/view/Surface;)V", (void*)nContextSetSurface },
2829{"rsnContextDestroy", "(J)V", (void*)nContextDestroy },
2830{"rsnContextDump", "(JI)V", (void*)nContextDump },
2831{"rsnContextPause", "(J)V", (void*)nContextPause },
2832{"rsnContextResume", "(J)V", (void*)nContextResume },
2833{"rsnContextSendMessage", "(JI[I)V", (void*)nContextSendMessage },
Yang Ni281c3252014-10-24 08:52:24 -07002834{"rsnClosureCreate", "(JJJ[J[J[I[J[J)J", (void*)nClosureCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002835{"rsnInvokeClosureCreate", "(JJ[B[J[J[I)J", (void*)nInvokeClosureCreate },
Yang Ni281c3252014-10-24 08:52:24 -07002836{"rsnClosureSetArg", "(JJIJI)V", (void*)nClosureSetArg },
2837{"rsnClosureSetGlobal", "(JJJJI)V", (void*)nClosureSetGlobal },
Tim Murray460a0492013-11-19 12:45:54 -08002838{"rsnAssignName", "(JJ[B)V", (void*)nAssignName },
2839{"rsnGetName", "(JJ)Ljava/lang/String;", (void*)nGetName },
2840{"rsnObjDestroy", "(JJ)V", (void*)nObjDestroy },
Jason Sams64676f32009-07-08 18:01:53 -07002841
Tim Murray460a0492013-11-19 12:45:54 -08002842{"rsnFileA3DCreateFromFile", "(JLjava/lang/String;)J", (void*)nFileA3DCreateFromFile },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002843{"rsnFileA3DCreateFromAssetStream", "(JJ)J", (void*)nFileA3DCreateFromAssetStream },
Tim Murray460a0492013-11-19 12:45:54 -08002844{"rsnFileA3DCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;)J", (void*)nFileA3DCreateFromAsset },
2845{"rsnFileA3DGetNumIndexEntries", "(JJ)I", (void*)nFileA3DGetNumIndexEntries },
2846{"rsnFileA3DGetIndexEntries", "(JJI[I[Ljava/lang/String;)V", (void*)nFileA3DGetIndexEntries },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002847{"rsnFileA3DGetEntryByIndex", "(JJI)J", (void*)nFileA3DGetEntryByIndex },
Jason Samsd19f10d2009-05-22 14:03:28 -07002848
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002849{"rsnFontCreateFromFile", "(JLjava/lang/String;FI)J", (void*)nFontCreateFromFile },
2850{"rsnFontCreateFromAssetStream", "(JLjava/lang/String;FIJ)J", (void*)nFontCreateFromAssetStream },
2851{"rsnFontCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;FI)J", (void*)nFontCreateFromAsset },
Jason Samsd19f10d2009-05-22 14:03:28 -07002852
Tim Murray460a0492013-11-19 12:45:54 -08002853{"rsnElementCreate", "(JJIZI)J", (void*)nElementCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002854{"rsnElementCreate2", "(J[J[Ljava/lang/String;[I)J", (void*)nElementCreate2 },
Tim Murray460a0492013-11-19 12:45:54 -08002855{"rsnElementGetNativeData", "(JJ[I)V", (void*)nElementGetNativeData },
Ashok Bhat98071552014-02-12 09:54:43 +00002856{"rsnElementGetSubElements", "(JJ[J[Ljava/lang/String;[I)V", (void*)nElementGetSubElements },
Jason Samsd19f10d2009-05-22 14:03:28 -07002857
Tim Murray460a0492013-11-19 12:45:54 -08002858{"rsnTypeCreate", "(JJIIIZZI)J", (void*)nTypeCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002859{"rsnTypeGetNativeData", "(JJ[J)V", (void*)nTypeGetNativeData },
Jason Samsd19f10d2009-05-22 14:03:28 -07002860
Ashok Bhat98071552014-02-12 09:54:43 +00002861{"rsnAllocationCreateTyped", "(JJIIJ)J", (void*)nAllocationCreateTyped },
Tim Murray460a0492013-11-19 12:45:54 -08002862{"rsnAllocationCreateFromBitmap", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateFromBitmap },
2863{"rsnAllocationCreateBitmapBackedAllocation", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateBitmapBackedAllocation },
2864{"rsnAllocationCubeCreateFromBitmap","(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCubeCreateFromBitmap },
Jason Sams5476b452010-12-08 16:14:36 -08002865
Tim Murray460a0492013-11-19 12:45:54 -08002866{"rsnAllocationCopyFromBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyFromBitmap },
2867{"rsnAllocationCopyToBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyToBitmap },
Jason Sams4ef66502010-12-10 16:03:15 -08002868
Tim Murray460a0492013-11-19 12:45:54 -08002869{"rsnAllocationSyncAll", "(JJI)V", (void*)nAllocationSyncAll },
Miao Wang8c150922015-10-26 17:44:10 -07002870{"rsnAllocationSetupBufferQueue", "(JJI)V", (void*)nAllocationSetupBufferQueue },
2871{"rsnAllocationShareBufferQueue", "(JJJ)V", (void*)nAllocationShareBufferQueue },
Tim Murray460a0492013-11-19 12:45:54 -08002872{"rsnAllocationGetSurface", "(JJ)Landroid/view/Surface;", (void*)nAllocationGetSurface },
2873{"rsnAllocationSetSurface", "(JJLandroid/view/Surface;)V", (void*)nAllocationSetSurface },
2874{"rsnAllocationIoSend", "(JJ)V", (void*)nAllocationIoSend },
Miao Wang8c150922015-10-26 17:44:10 -07002875{"rsnAllocationIoReceive", "(JJ)J", (void*)nAllocationIoReceive },
Miao Wang87e908d2015-03-02 15:15:15 -08002876{"rsnAllocationData1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData1D },
Miao Wangc8e237e2015-02-20 18:36:32 -08002877{"rsnAllocationElementData", "(JJIIIII[BI)V", (void*)nAllocationElementData },
Miao Wang87e908d2015-03-02 15:15:15 -08002878{"rsnAllocationData2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData2D },
Tim Murray460a0492013-11-19 12:45:54 -08002879{"rsnAllocationData2D", "(JJIIIIIIJIIII)V", (void*)nAllocationData2D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002880{"rsnAllocationData3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData3D },
Tim Murray460a0492013-11-19 12:45:54 -08002881{"rsnAllocationData3D", "(JJIIIIIIIJIIII)V", (void*)nAllocationData3D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002882{"rsnAllocationRead", "(JJLjava/lang/Object;IIZ)V", (void*)nAllocationRead },
2883{"rsnAllocationRead1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead1D },
Miao Wang45cec0a2015-03-04 16:40:21 -08002884{"rsnAllocationElementRead", "(JJIIIII[BI)V", (void*)nAllocationElementRead },
Miao Wang87e908d2015-03-02 15:15:15 -08002885{"rsnAllocationRead2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead2D },
2886{"rsnAllocationRead3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead3D },
Tim Murray460a0492013-11-19 12:45:54 -08002887{"rsnAllocationGetType", "(JJ)J", (void*)nAllocationGetType},
2888{"rsnAllocationResize1D", "(JJI)V", (void*)nAllocationResize1D },
2889{"rsnAllocationGenerateMipmaps", "(JJ)V", (void*)nAllocationGenerateMipmaps },
Jason Samsbd1c3ad2009-08-03 16:03:08 -07002890
Jason Sams46ba27e32015-02-06 17:45:15 -08002891{"rsnAllocationAdapterCreate", "(JJJ)J", (void*)nAllocationAdapterCreate },
2892{"rsnAllocationAdapterOffset", "(JJIIIIIIIII)V", (void*)nAllocationAdapterOffset },
2893
Tim Murray460a0492013-11-19 12:45:54 -08002894{"rsnScriptBindAllocation", "(JJJI)V", (void*)nScriptBindAllocation },
2895{"rsnScriptSetTimeZone", "(JJ[B)V", (void*)nScriptSetTimeZone },
2896{"rsnScriptInvoke", "(JJI)V", (void*)nScriptInvoke },
2897{"rsnScriptInvokeV", "(JJI[B)V", (void*)nScriptInvokeV },
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002898
2899{"rsnScriptForEach", "(JJI[JJ[B[I)V", (void*)nScriptForEach },
David Gross4a457852016-06-02 14:46:55 -07002900{"rsnScriptReduce", "(JJI[JJ[I)V", (void*)nScriptReduce },
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002901
Tim Murray460a0492013-11-19 12:45:54 -08002902{"rsnScriptSetVarI", "(JJII)V", (void*)nScriptSetVarI },
2903{"rsnScriptGetVarI", "(JJI)I", (void*)nScriptGetVarI },
2904{"rsnScriptSetVarJ", "(JJIJ)V", (void*)nScriptSetVarJ },
2905{"rsnScriptGetVarJ", "(JJI)J", (void*)nScriptGetVarJ },
2906{"rsnScriptSetVarF", "(JJIF)V", (void*)nScriptSetVarF },
2907{"rsnScriptGetVarF", "(JJI)F", (void*)nScriptGetVarF },
2908{"rsnScriptSetVarD", "(JJID)V", (void*)nScriptSetVarD },
2909{"rsnScriptGetVarD", "(JJI)D", (void*)nScriptGetVarD },
2910{"rsnScriptSetVarV", "(JJI[B)V", (void*)nScriptSetVarV },
2911{"rsnScriptGetVarV", "(JJI[B)V", (void*)nScriptGetVarV },
2912{"rsnScriptSetVarVE", "(JJI[BJ[I)V", (void*)nScriptSetVarVE },
2913{"rsnScriptSetVarObj", "(JJIJ)V", (void*)nScriptSetVarObj },
Jason Samsd19f10d2009-05-22 14:03:28 -07002914
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002915{"rsnScriptCCreate", "(JLjava/lang/String;Ljava/lang/String;[BI)J", (void*)nScriptCCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002916{"rsnScriptIntrinsicCreate", "(JIJ)J", (void*)nScriptIntrinsicCreate },
2917{"rsnScriptKernelIDCreate", "(JJII)J", (void*)nScriptKernelIDCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002918{"rsnScriptInvokeIDCreate", "(JJI)J", (void*)nScriptInvokeIDCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002919{"rsnScriptFieldIDCreate", "(JJI)J", (void*)nScriptFieldIDCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002920{"rsnScriptGroupCreate", "(J[J[J[J[J[J)J", (void*)nScriptGroupCreate },
Yang Ni35be56c2015-04-02 17:47:56 -07002921{"rsnScriptGroup2Create", "(JLjava/lang/String;Ljava/lang/String;[J)J", (void*)nScriptGroup2Create },
Tim Murray460a0492013-11-19 12:45:54 -08002922{"rsnScriptGroupSetInput", "(JJJJ)V", (void*)nScriptGroupSetInput },
2923{"rsnScriptGroupSetOutput", "(JJJJ)V", (void*)nScriptGroupSetOutput },
2924{"rsnScriptGroupExecute", "(JJ)V", (void*)nScriptGroupExecute },
Yang Ni281c3252014-10-24 08:52:24 -07002925{"rsnScriptGroup2Execute", "(JJ)V", (void*)nScriptGroup2Execute },
Jason Sams0011bcf2009-12-15 12:58:36 -08002926
Tim Murray25207df2015-01-12 16:47:56 -08002927{"rsnScriptIntrinsicBLAS_Single", "(JJIIIIIIIIIFJJFJIIII)V", (void*)nScriptIntrinsicBLAS_Single },
2928{"rsnScriptIntrinsicBLAS_Double", "(JJIIIIIIIIIDJJDJIIII)V", (void*)nScriptIntrinsicBLAS_Double },
2929{"rsnScriptIntrinsicBLAS_Complex", "(JJIIIIIIIIIFFJJFFJIIII)V", (void*)nScriptIntrinsicBLAS_Complex },
2930{"rsnScriptIntrinsicBLAS_Z", "(JJIIIIIIIIIDDJJDDJIIII)V", (void*)nScriptIntrinsicBLAS_Z },
2931
Tim Murray9cb16a22015-04-01 11:07:16 -07002932{"rsnScriptIntrinsicBLAS_BNNM", "(JJIIIJIJIJII)V", (void*)nScriptIntrinsicBLAS_BNNM },
2933
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002934{"rsnProgramStoreCreate", "(JZZZZZZIII)J", (void*)nProgramStoreCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002935
Tim Murray460a0492013-11-19 12:45:54 -08002936{"rsnProgramBindConstants", "(JJIJ)V", (void*)nProgramBindConstants },
2937{"rsnProgramBindTexture", "(JJIJ)V", (void*)nProgramBindTexture },
2938{"rsnProgramBindSampler", "(JJIJ)V", (void*)nProgramBindSampler },
Jason Samsebfb4362009-09-23 13:57:02 -07002939
Ashok Bhat98071552014-02-12 09:54:43 +00002940{"rsnProgramFragmentCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramFragmentCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002941{"rsnProgramRasterCreate", "(JZI)J", (void*)nProgramRasterCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002942{"rsnProgramVertexCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramVertexCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002943
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002944{"rsnContextBindRootScript", "(JJ)V", (void*)nContextBindRootScript },
2945{"rsnContextBindProgramStore", "(JJ)V", (void*)nContextBindProgramStore },
2946{"rsnContextBindProgramFragment", "(JJ)V", (void*)nContextBindProgramFragment },
2947{"rsnContextBindProgramVertex", "(JJ)V", (void*)nContextBindProgramVertex },
2948{"rsnContextBindProgramRaster", "(JJ)V", (void*)nContextBindProgramRaster },
Jason Sams02fb2cb2009-05-28 15:37:57 -07002949
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002950{"rsnSamplerCreate", "(JIIIIIF)J", (void*)nSamplerCreate },
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002951
Ashok Bhat98071552014-02-12 09:54:43 +00002952{"rsnMeshCreate", "(J[J[J[I)J", (void*)nMeshCreate },
Jason Sams2e1872f2010-08-17 16:25:41 -07002953
Tim Murray460a0492013-11-19 12:45:54 -08002954{"rsnMeshGetVertexBufferCount", "(JJ)I", (void*)nMeshGetVertexBufferCount },
2955{"rsnMeshGetIndexCount", "(JJ)I", (void*)nMeshGetIndexCount },
Ashok Bhat98071552014-02-12 09:54:43 +00002956{"rsnMeshGetVertices", "(JJ[JI)V", (void*)nMeshGetVertices },
2957{"rsnMeshGetIndices", "(JJ[J[II)V", (void*)nMeshGetIndices },
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002958
Tim Murray56f9e6f2014-05-16 11:47:26 -07002959{"rsnSystemGetPointerSize", "()I", (void*)nSystemGetPointerSize },
Miao Wang0facf022015-11-25 11:21:13 -08002960{"rsnAllocationGetByteBuffer", "(JJ[JIII)Ljava/nio/ByteBuffer;", (void*)nAllocationGetByteBuffer },
Jason Samsd19f10d2009-05-22 14:03:28 -07002961};
2962
2963static int registerFuncs(JNIEnv *_env)
2964{
2965 return android::AndroidRuntime::registerNativeMethods(
2966 _env, classPathName, methods, NELEM(methods));
2967}
2968
2969// ---------------------------------------------------------------------------
2970
2971jint JNI_OnLoad(JavaVM* vm, void* reserved)
2972{
Chris Wailes488230c32014-08-14 11:22:40 -07002973 JNIEnv* env = nullptr;
Jason Samsd19f10d2009-05-22 14:03:28 -07002974 jint result = -1;
2975
Jason Samsd19f10d2009-05-22 14:03:28 -07002976 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
Steve Block3762c312012-01-06 19:20:56 +00002977 ALOGE("ERROR: GetEnv failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07002978 goto bail;
2979 }
Chris Wailes488230c32014-08-14 11:22:40 -07002980 assert(env != nullptr);
Jason Samsd19f10d2009-05-22 14:03:28 -07002981
2982 if (registerFuncs(env) < 0) {
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002983 ALOGE("ERROR: Renderscript native registration failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07002984 goto bail;
2985 }
2986
2987 /* success -- return valid version number */
2988 result = JNI_VERSION_1_4;
2989
2990bail:
2991 return result;
2992}