blob: 4e667c6591331a546d352115c1323c357223a895 [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
Stephen Hines4cbe25a2012-01-18 18:46:27 -08002 * Copyright (C) 2011-2012 The Android Open Source Project
Jason Samsd19f10d2009-05-22 14:03:28 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jason Samsd1516df2015-05-05 18:00:34 -070017#define LOG_TAG "RenderScript_jni"
Jason Samsf29ca502009-06-23 12:22:47 -070018
Jason Samsd19f10d2009-05-22 14:03:28 -070019#include <stdlib.h>
20#include <stdio.h>
21#include <fcntl.h>
22#include <unistd.h>
23#include <math.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070024#include <utils/misc.h>
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +010025#include <inttypes.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070026
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080027#include <androidfw/Asset.h>
28#include <androidfw/AssetManager.h>
29#include <androidfw/ResourceTypes.h>
Jason Samsf29ca502009-06-23 12:22:47 -070030
Jason Samsd19f10d2009-05-22 14:03:28 -070031#include "jni.h"
32#include "JNIHelp.h"
33#include "android_runtime/AndroidRuntime.h"
Jim Milleree956052010-08-19 18:56:00 -070034#include "android_runtime/android_view_Surface.h"
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -080035#include "android_runtime/android_util_AssetManager.h"
John Reckf4faeac2015-03-05 13:50:31 -080036#include "android/graphics/GraphicsJNI.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070037
Jason Sams1d6983a2012-02-16 16:07:49 -080038#include <rs.h>
39#include <rsEnv.h>
Jason Samsfb9aa9f2012-03-28 15:30:07 -070040#include <gui/Surface.h>
Andy McFaddend47f7d82012-12-18 09:48:38 -080041#include <gui/GLConsumer.h>
Jason Samsfaa32b32011-06-20 16:58:04 -070042#include <android_runtime/android_graphics_SurfaceTexture.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070043
Steve Block3762c312012-01-06 19:20:56 +000044//#define LOG_API ALOGE
Andreas Gampe67333922014-11-10 20:35:59 -080045static constexpr bool kLogApi = false;
Jason Samsd19f10d2009-05-22 14:03:28 -070046
47using namespace android;
48
Andreas Gampe67333922014-11-10 20:35:59 -080049template <typename... T>
50void UNUSED(T... t) {}
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 Nainar85e8c512016-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
424 fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
425 if (fieldIDs == nullptr) {
426 goto exit;
427 }
428
429 for (size_t i = 0; i < numValues; i++) {
430 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
431 }
432
Yang Ni17c2d7a2015-04-30 16:13:54 -0700433 depClosures = (RsClosure*)alloca(sizeof(RsClosure) * numDependencies);
434 if (depClosures == nullptr) {
435 goto exit;
436 }
437
438 for (size_t i = 0; i < numDependencies; i++) {
439 depClosures[i] = (RsClosure)jDepClosures[i];
440 }
441
442 depFieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numDependencies);
443 if (depFieldIDs == nullptr) {
444 goto exit;
445 }
446
447 for (size_t i = 0; i < numDependencies; i++) {
Yang Ni281c3252014-10-24 08:52:24 -0700448 depFieldIDs[i] = (RsClosure)jDepFieldIDs[i];
449 }
450
Yang Ni17c2d7a2015-04-30 16:13:54 -0700451 ret = (jlong)(uintptr_t)rsClosureCreate(
Yang Ni281c3252014-10-24 08:52:24 -0700452 (RsContext)con, (RsScriptKernelID)kernelID, (RsAllocation)returnValue,
Yang Ni263cc902015-11-10 13:27:04 -0800453 fieldIDs, numValues, jValues, numValues,
Yang Ni17c2d7a2015-04-30 16:13:54 -0700454 (int*)jSizes, numValues,
455 depClosures, numDependencies,
456 depFieldIDs, numDependencies);
457
458exit:
459
460 _env->ReleaseLongArrayElements(depFieldIDArray, jDepFieldIDs, JNI_ABORT);
461 _env->ReleaseLongArrayElements(depClosureArray, jDepClosures, JNI_ABORT);
462 _env->ReleaseIntArrayElements (sizeArray, jSizes, JNI_ABORT);
463 _env->ReleaseLongArrayElements(valueArray, jValues, JNI_ABORT);
464 _env->ReleaseLongArrayElements(fieldIDArray, jFieldIDs, JNI_ABORT);
465
466 return ret;
Yang Ni281c3252014-10-24 08:52:24 -0700467}
468
Yang Nibe392ad2015-01-23 17:16:02 -0800469static jlong
470nInvokeClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong invokeID,
471 jbyteArray paramArray, jlongArray fieldIDArray, jlongArray valueArray,
472 jintArray sizeArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700473 jlong ret = 0;
474
Yang Nibe392ad2015-01-23 17:16:02 -0800475 jbyte* jParams = _env->GetByteArrayElements(paramArray, nullptr);
476 jsize jParamLength = _env->GetArrayLength(paramArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700477 if (jParams == nullptr) {
478 ALOGE("Failed to get Java array elements: params.");
479 return ret;
480 }
481
Yang Nibe392ad2015-01-23 17:16:02 -0800482 jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
483 jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700484 if (jFieldIDs == nullptr) {
485 ALOGE("Failed to get Java array elements: fieldIDs.");
486 return ret;
487 }
488
Yang Ni17c2d7a2015-04-30 16:13:54 -0700489 jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
490 jsize values_length = _env->GetArrayLength(valueArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700491 if (jValues == nullptr) {
492 ALOGE("Failed to get Java array elements: values.");
493 return ret;
494 }
495
Yang Ni17c2d7a2015-04-30 16:13:54 -0700496 jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
497 jsize sizes_length = _env->GetArrayLength(sizeArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700498 if (jSizes == nullptr) {
499 ALOGE("Failed to get Java array elements: sizes.");
500 return ret;
501 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700502
503 size_t numValues;
504 RsScriptFieldID* fieldIDs;
Yang Ni17c2d7a2015-04-30 16:13:54 -0700505
506 if (fieldIDs_length != values_length || values_length != sizes_length) {
507 ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
508 goto exit;
509 }
510
511 numValues = (size_t) fieldIDs_length;
512
Yang Ni7b2a46f2015-05-05 12:41:19 -0700513 if (numValues > RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700514 ALOGE("Too many arguments or globals in closure creation");
515 goto exit;
516 }
517
518 fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
519 if (fieldIDs == nullptr) {
520 goto exit;
521 }
522
523 for (size_t i = 0; i< numValues; i++) {
Yang Nibe392ad2015-01-23 17:16:02 -0800524 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
525 }
526
Yang Ni17c2d7a2015-04-30 16:13:54 -0700527 ret = (jlong)(uintptr_t)rsInvokeClosureCreate(
Yang Nibe392ad2015-01-23 17:16:02 -0800528 (RsContext)con, (RsScriptInvokeID)invokeID, jParams, jParamLength,
Yang Ni263cc902015-11-10 13:27:04 -0800529 fieldIDs, numValues, jValues, numValues,
Yang Ni17c2d7a2015-04-30 16:13:54 -0700530 (int*)jSizes, numValues);
531
532exit:
533
534 _env->ReleaseIntArrayElements (sizeArray, jSizes, JNI_ABORT);
535 _env->ReleaseLongArrayElements(valueArray, jValues, JNI_ABORT);
536 _env->ReleaseLongArrayElements(fieldIDArray, jFieldIDs, JNI_ABORT);
537 _env->ReleaseByteArrayElements(paramArray, jParams, JNI_ABORT);
538
539 return ret;
Yang Nibe392ad2015-01-23 17:16:02 -0800540}
541
Yang Ni281c3252014-10-24 08:52:24 -0700542static void
543nClosureSetArg(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
544 jint index, jlong value, jint size) {
Yang Ni263cc902015-11-10 13:27:04 -0800545 // Size is signed with -1 indicating the value is an Allocation
Yang Ni281c3252014-10-24 08:52:24 -0700546 rsClosureSetArg((RsContext)con, (RsClosure)closureID, (uint32_t)index,
Yang Ni263cc902015-11-10 13:27:04 -0800547 (uintptr_t)value, size);
Yang Ni281c3252014-10-24 08:52:24 -0700548}
549
550static void
551nClosureSetGlobal(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
552 jlong fieldID, jlong value, jint size) {
Yang Ni263cc902015-11-10 13:27:04 -0800553 // Size is signed with -1 indicating the value is an Allocation
Yang Ni281c3252014-10-24 08:52:24 -0700554 rsClosureSetGlobal((RsContext)con, (RsClosure)closureID,
Yang Ni263cc902015-11-10 13:27:04 -0800555 (RsScriptFieldID)fieldID, (int64_t)value, size);
Yang Ni281c3252014-10-24 08:52:24 -0700556}
557
558static long
Yang Ni35be56c2015-04-02 17:47:56 -0700559nScriptGroup2Create(JNIEnv *_env, jobject _this, jlong con, jstring name,
Yang Niebf63402015-01-16 11:06:26 -0800560 jstring cacheDir, jlongArray closureArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700561 jlong ret = 0;
562
Yang Ni35be56c2015-04-02 17:47:56 -0700563 AutoJavaStringToUTF8 nameUTF(_env, name);
Yang Niebf63402015-01-16 11:06:26 -0800564 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
565
Yang Ni281c3252014-10-24 08:52:24 -0700566 jlong* jClosures = _env->GetLongArrayElements(closureArray, nullptr);
567 jsize numClosures = _env->GetArrayLength(closureArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700568 if (jClosures == nullptr) {
569 ALOGE("Failed to get Java array elements: closures.");
570 return ret;
571 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700572
573 RsClosure* closures;
574
Yang Ni7b2a46f2015-05-05 12:41:19 -0700575 if (numClosures > (jsize) RS_SCRIPT_GROUP_MAX_NUMBER_CLOSURES) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700576 ALOGE("Too many closures in script group");
577 goto exit;
578 }
579
580 closures = (RsClosure*)alloca(sizeof(RsClosure) * numClosures);
581 if (closures == nullptr) {
582 goto exit;
583 }
584
Yang Ni281c3252014-10-24 08:52:24 -0700585 for (int i = 0; i < numClosures; i++) {
586 closures[i] = (RsClosure)jClosures[i];
587 }
588
Yang Ni17c2d7a2015-04-30 16:13:54 -0700589 ret = (jlong)(uintptr_t)rsScriptGroup2Create(
Yang Ni35be56c2015-04-02 17:47:56 -0700590 (RsContext)con, nameUTF.c_str(), nameUTF.length(),
591 cacheDirUTF.c_str(), cacheDirUTF.length(),
Yang Niebf63402015-01-16 11:06:26 -0800592 closures, numClosures);
Yang Ni17c2d7a2015-04-30 16:13:54 -0700593
594exit:
595
596 _env->ReleaseLongArrayElements(closureArray, jClosures, JNI_ABORT);
597
598 return ret;
Yang Ni281c3252014-10-24 08:52:24 -0700599}
600
601static void
602nScriptGroup2Execute(JNIEnv *_env, jobject _this, jlong con, jlong groupID) {
603 rsScriptGroupExecute((RsContext)con, (RsScriptGroup2)groupID);
604}
605
Jason Sams96ed4cf2010-06-15 12:15:57 -0700606static void
Tim Murray25207df2015-01-12 16:47:56 -0800607nScriptIntrinsicBLAS_Single(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
608 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
609 jfloat alpha, jlong A, jlong B, jfloat beta, jlong C, jint incX, jint incY,
610 jint KL, jint KU) {
611 RsBlasCall call;
612 memset(&call, 0, sizeof(call));
613 call.func = (RsBlasFunction)func;
614 call.transA = (RsBlasTranspose)TransA;
615 call.transB = (RsBlasTranspose)TransB;
616 call.side = (RsBlasSide)Side;
617 call.uplo = (RsBlasUplo)Uplo;
618 call.diag = (RsBlasDiag)Diag;
619 call.M = M;
620 call.N = N;
621 call.K = K;
622 call.alpha.f = alpha;
623 call.beta.f = beta;
624 call.incX = incX;
625 call.incY = incY;
626 call.KL = KL;
627 call.KU = KU;
628
629 RsAllocation in_allocs[3];
630 in_allocs[0] = (RsAllocation)A;
631 in_allocs[1] = (RsAllocation)B;
632 in_allocs[2] = (RsAllocation)C;
633
634 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
635 in_allocs, sizeof(in_allocs), nullptr,
636 &call, sizeof(call), nullptr, 0);
637}
638
639static void
640nScriptIntrinsicBLAS_Double(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
641 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
642 jdouble alpha, jlong A, jlong B, jdouble beta, jlong C, jint incX, jint incY,
643 jint KL, jint KU) {
644 RsBlasCall call;
645 memset(&call, 0, sizeof(call));
646 call.func = (RsBlasFunction)func;
647 call.transA = (RsBlasTranspose)TransA;
648 call.transB = (RsBlasTranspose)TransB;
649 call.side = (RsBlasSide)Side;
650 call.uplo = (RsBlasUplo)Uplo;
651 call.diag = (RsBlasDiag)Diag;
652 call.M = M;
653 call.N = N;
654 call.K = K;
655 call.alpha.d = alpha;
656 call.beta.d = beta;
657 call.incX = incX;
658 call.incY = incY;
659 call.KL = KL;
660 call.KU = KU;
661
662 RsAllocation in_allocs[3];
663 in_allocs[0] = (RsAllocation)A;
664 in_allocs[1] = (RsAllocation)B;
665 in_allocs[2] = (RsAllocation)C;
666
667 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700668 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800669 &call, sizeof(call), nullptr, 0);
670}
671
672static void
673nScriptIntrinsicBLAS_Complex(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
674 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
675 jfloat alphaX, jfloat alphaY, jlong A, jlong B, jfloat betaX,
676 jfloat betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
677 RsBlasCall call;
678 memset(&call, 0, sizeof(call));
679 call.func = (RsBlasFunction)func;
680 call.transA = (RsBlasTranspose)TransA;
681 call.transB = (RsBlasTranspose)TransB;
682 call.side = (RsBlasSide)Side;
683 call.uplo = (RsBlasUplo)Uplo;
684 call.diag = (RsBlasDiag)Diag;
685 call.M = M;
686 call.N = N;
687 call.K = K;
688 call.alpha.c.r = alphaX;
689 call.alpha.c.i = alphaY;
690 call.beta.c.r = betaX;
Miao Wang82585b32015-04-30 13:44:49 -0700691 call.beta.c.i = betaY;
Tim Murray25207df2015-01-12 16:47:56 -0800692 call.incX = incX;
693 call.incY = incY;
694 call.KL = KL;
695 call.KU = KU;
696
697 RsAllocation in_allocs[3];
698 in_allocs[0] = (RsAllocation)A;
699 in_allocs[1] = (RsAllocation)B;
700 in_allocs[2] = (RsAllocation)C;
701
702 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700703 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800704 &call, sizeof(call), nullptr, 0);
705}
706
707static void
708nScriptIntrinsicBLAS_Z(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
709 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
710 jdouble alphaX, jdouble alphaY, jlong A, jlong B, jdouble betaX,
711 jdouble betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
712 RsBlasCall call;
713 memset(&call, 0, sizeof(call));
714 call.func = (RsBlasFunction)func;
715 call.transA = (RsBlasTranspose)TransA;
716 call.transB = (RsBlasTranspose)TransB;
717 call.side = (RsBlasSide)Side;
718 call.uplo = (RsBlasUplo)Uplo;
719 call.diag = (RsBlasDiag)Diag;
720 call.M = M;
721 call.N = N;
722 call.K = K;
723 call.alpha.z.r = alphaX;
724 call.alpha.z.i = alphaY;
725 call.beta.z.r = betaX;
Miao Wang82585b32015-04-30 13:44:49 -0700726 call.beta.z.i = betaY;
Tim Murray25207df2015-01-12 16:47:56 -0800727 call.incX = incX;
728 call.incY = incY;
729 call.KL = KL;
730 call.KU = KU;
731
732 RsAllocation in_allocs[3];
733 in_allocs[0] = (RsAllocation)A;
734 in_allocs[1] = (RsAllocation)B;
735 in_allocs[2] = (RsAllocation)C;
736
737 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700738 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800739 &call, sizeof(call), nullptr, 0);
740}
741
742
743static void
Tim Murray9cb16a22015-04-01 11:07:16 -0700744nScriptIntrinsicBLAS_BNNM(JNIEnv *_env, jobject _this, jlong con, jlong id, jint M, jint N, jint K,
745 jlong A, jint a_offset, jlong B, jint b_offset, jlong C, jint c_offset,
746 jint c_mult_int) {
747 RsBlasCall call;
748 memset(&call, 0, sizeof(call));
749 call.func = RsBlas_bnnm;
750 call.M = M;
751 call.N = N;
752 call.K = K;
Miao Wang25148062015-06-29 17:43:03 -0700753 call.a_offset = a_offset & 0xFF;
754 call.b_offset = b_offset & 0xFF;
Tim Murray9cb16a22015-04-01 11:07:16 -0700755 call.c_offset = c_offset;
756 call.c_mult_int = c_mult_int;
757
758 RsAllocation in_allocs[3];
759 in_allocs[0] = (RsAllocation)A;
760 in_allocs[1] = (RsAllocation)B;
761 in_allocs[2] = (RsAllocation)C;
762
763 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700764 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray9cb16a22015-04-01 11:07:16 -0700765 &call, sizeof(call), nullptr, 0);
766}
767
768
769static void
Tim Murray460a0492013-11-19 12:45:54 -0800770nAssignName(JNIEnv *_env, jobject _this, jlong con, jlong obj, jbyteArray str)
Jason Sams3eaa3382009-06-10 15:04:38 -0700771{
Andreas Gampe67333922014-11-10 20:35:59 -0800772 if (kLogApi) {
773 ALOGD("nAssignName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
774 }
Jason Sams3eaa3382009-06-10 15:04:38 -0700775 jint len = _env->GetArrayLength(str);
776 jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
Miao Wangba8766c2015-10-12 17:24:13 -0700777 if (cptr == nullptr) {
778 ALOGE("Failed to get Java array elements");
779 return;
780 }
781
Tim Murrayeff663f2013-11-15 13:08:30 -0800782 rsAssignName((RsContext)con, (void *)obj, (const char *)cptr, len);
Jason Sams3eaa3382009-06-10 15:04:38 -0700783 _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
784}
785
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700786static jstring
Tim Murray460a0492013-11-19 12:45:54 -0800787nGetName(JNIEnv *_env, jobject _this, jlong con, jlong obj)
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700788{
Andreas Gampe67333922014-11-10 20:35:59 -0800789 if (kLogApi) {
790 ALOGD("nGetName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
791 }
Chris Wailes488230c32014-08-14 11:22:40 -0700792 const char *name = nullptr;
Tim Murrayeff663f2013-11-15 13:08:30 -0800793 rsaGetName((RsContext)con, (void *)obj, &name);
Chris Wailes488230c32014-08-14 11:22:40 -0700794 if(name == nullptr || strlen(name) == 0) {
795 return nullptr;
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700796 }
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700797 return _env->NewStringUTF(name);
798}
799
Jason Sams7ce033d2009-08-18 14:14:24 -0700800static void
Tim Murray460a0492013-11-19 12:45:54 -0800801nObjDestroy(JNIEnv *_env, jobject _this, jlong con, jlong obj)
Jason Sams7ce033d2009-08-18 14:14:24 -0700802{
Andreas Gampe67333922014-11-10 20:35:59 -0800803 if (kLogApi) {
804 ALOGD("nObjDestroy, con(%p) obj(%p)", (RsContext)con, (void *)obj);
805 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800806 rsObjDestroy((RsContext)con, (void *)obj);
Jason Sams7ce033d2009-08-18 14:14:24 -0700807}
808
Jason Sams3eaa3382009-06-10 15:04:38 -0700809// ---------------------------------------------------------------------------
810
Tim Murrayeff663f2013-11-15 13:08:30 -0800811static jlong
Jason Samsd19f10d2009-05-22 14:03:28 -0700812nDeviceCreate(JNIEnv *_env, jobject _this)
813{
Andreas Gampe67333922014-11-10 20:35:59 -0800814 if (kLogApi) {
815 ALOGD("nDeviceCreate");
816 }
Tim Murray3aa89c12014-08-18 17:51:22 -0700817 return (jlong)(uintptr_t)rsDeviceCreate();
Jason Samsd19f10d2009-05-22 14:03:28 -0700818}
819
820static void
Tim Murray5eaf4682014-01-10 11:25:52 -0800821nDeviceDestroy(JNIEnv *_env, jobject _this, jlong dev)
Jason Samsd19f10d2009-05-22 14:03:28 -0700822{
Andreas Gampe67333922014-11-10 20:35:59 -0800823 if (kLogApi) {
824 ALOGD("nDeviceDestroy");
825 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700826 return rsDeviceDestroy((RsDevice)dev);
827}
828
Jason Samsebfb4362009-09-23 13:57:02 -0700829static void
Tim Murray5eaf4682014-01-10 11:25:52 -0800830nDeviceSetConfig(JNIEnv *_env, jobject _this, jlong dev, jint p, jint value)
Jason Samsebfb4362009-09-23 13:57:02 -0700831{
Andreas Gampe67333922014-11-10 20:35:59 -0800832 if (kLogApi) {
833 ALOGD("nDeviceSetConfig dev(%p), param(%i), value(%i)", (void *)dev, p, value);
834 }
Jason Samsebfb4362009-09-23 13:57:02 -0700835 return rsDeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value);
836}
837
Tim Murrayeff663f2013-11-15 13:08:30 -0800838static jlong
Jason Sams81cd2b12014-12-02 12:36:43 -0800839nContextCreate(JNIEnv *_env, jobject _this, jlong dev, jint flags, jint sdkVer, jint contextType)
Jason Samsd19f10d2009-05-22 14:03:28 -0700840{
Andreas Gampe67333922014-11-10 20:35:59 -0800841 if (kLogApi) {
842 ALOGD("nContextCreate");
843 }
Jason Sams81cd2b12014-12-02 12:36:43 -0800844 return (jlong)(uintptr_t)rsContextCreate((RsDevice)dev, 0, sdkVer, (RsContextType)contextType, flags);
Jason Sams704ff642010-02-09 16:05:07 -0800845}
846
Tim Murrayeff663f2013-11-15 13:08:30 -0800847static jlong
Tim Murray5eaf4682014-01-10 11:25:52 -0800848nContextCreateGL(JNIEnv *_env, jobject _this, jlong dev, jint ver, jint sdkVer,
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000849 jint colorMin, jint colorPref,
850 jint alphaMin, jint alphaPref,
851 jint depthMin, jint depthPref,
852 jint stencilMin, jint stencilPref,
853 jint samplesMin, jint samplesPref, jfloat samplesQ,
854 jint dpi)
Jason Sams704ff642010-02-09 16:05:07 -0800855{
Jason Sams11c8af92010-10-13 15:31:10 -0700856 RsSurfaceConfig sc;
857 sc.alphaMin = alphaMin;
858 sc.alphaPref = alphaPref;
859 sc.colorMin = colorMin;
860 sc.colorPref = colorPref;
861 sc.depthMin = depthMin;
862 sc.depthPref = depthPref;
863 sc.samplesMin = samplesMin;
864 sc.samplesPref = samplesPref;
865 sc.samplesQ = samplesQ;
866
Andreas Gampe67333922014-11-10 20:35:59 -0800867 if (kLogApi) {
868 ALOGD("nContextCreateGL");
869 }
Tim Murray3aa89c12014-08-18 17:51:22 -0700870 return (jlong)(uintptr_t)rsContextCreateGL((RsDevice)dev, ver, sdkVer, sc, dpi);
Jason Samsd19f10d2009-05-22 14:03:28 -0700871}
872
873static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800874nContextSetPriority(JNIEnv *_env, jobject _this, jlong con, jint p)
Jason Sams7d787b42009-11-15 12:14:26 -0800875{
Andreas Gampe67333922014-11-10 20:35:59 -0800876 if (kLogApi) {
877 ALOGD("ContextSetPriority, con(%p), priority(%i)", (RsContext)con, p);
878 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800879 rsContextSetPriority((RsContext)con, p);
Jason Sams7d787b42009-11-15 12:14:26 -0800880}
881
Tim Murray47f31582015-04-07 15:43:24 -0700882static void
883nContextSetCacheDir(JNIEnv *_env, jobject _this, jlong con, jstring cacheDir)
884{
885 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
886
887 if (kLogApi) {
888 ALOGD("ContextSetCacheDir, con(%p), cacheDir(%s)", (RsContext)con, cacheDirUTF.c_str());
889 }
890 rsContextSetCacheDir((RsContext)con, cacheDirUTF.c_str(), cacheDirUTF.length());
891}
892
Jason Sams7d787b42009-11-15 12:14:26 -0800893
894
895static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800896nContextSetSurface(JNIEnv *_env, jobject _this, jlong con, jint width, jint height, jobject wnd)
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800897{
Andreas Gampe67333922014-11-10 20:35:59 -0800898 if (kLogApi) {
899 ALOGD("nContextSetSurface, con(%p), width(%i), height(%i), surface(%p)", (RsContext)con,
900 width, height, (Surface *)wnd);
901 }
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800902
Chris Wailes488230c32014-08-14 11:22:40 -0700903 ANativeWindow * window = nullptr;
904 if (wnd == nullptr) {
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800905
906 } else {
Jeff Brown64a55af2012-08-26 02:47:39 -0700907 window = android_view_Surface_getNativeWindow(_env, wnd).get();
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800908 }
909
Tim Murrayeff663f2013-11-15 13:08:30 -0800910 rsContextSetSurface((RsContext)con, width, height, window);
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800911}
912
913static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800914nContextDestroy(JNIEnv *_env, jobject _this, jlong con)
Jason Samsd19f10d2009-05-22 14:03:28 -0700915{
Andreas Gampe67333922014-11-10 20:35:59 -0800916 if (kLogApi) {
917 ALOGD("nContextDestroy, con(%p)", (RsContext)con);
918 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800919 rsContextDestroy((RsContext)con);
Jason Samsd19f10d2009-05-22 14:03:28 -0700920}
921
Jason Sams715333b2009-11-17 17:26:46 -0800922static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800923nContextDump(JNIEnv *_env, jobject _this, jlong con, jint bits)
Jason Sams715333b2009-11-17 17:26:46 -0800924{
Andreas Gampe67333922014-11-10 20:35:59 -0800925 if (kLogApi) {
926 ALOGD("nContextDump, con(%p) bits(%i)", (RsContext)con, bits);
927 }
Jason Sams715333b2009-11-17 17:26:46 -0800928 rsContextDump((RsContext)con, bits);
929}
Jason Samsd19f10d2009-05-22 14:03:28 -0700930
931static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800932nContextPause(JNIEnv *_env, jobject _this, jlong con)
Jason Sams65e7aa52009-09-24 17:38:20 -0700933{
Andreas Gampe67333922014-11-10 20:35:59 -0800934 if (kLogApi) {
935 ALOGD("nContextPause, con(%p)", (RsContext)con);
936 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800937 rsContextPause((RsContext)con);
Jason Sams65e7aa52009-09-24 17:38:20 -0700938}
939
940static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800941nContextResume(JNIEnv *_env, jobject _this, jlong con)
Jason Sams65e7aa52009-09-24 17:38:20 -0700942{
Andreas Gampe67333922014-11-10 20:35:59 -0800943 if (kLogApi) {
944 ALOGD("nContextResume, con(%p)", (RsContext)con);
945 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800946 rsContextResume((RsContext)con);
Jason Sams65e7aa52009-09-24 17:38:20 -0700947}
948
Jason Sams1c415172010-11-08 17:06:46 -0800949
950static jstring
Tim Murrayeff663f2013-11-15 13:08:30 -0800951nContextGetErrorMessage(JNIEnv *_env, jobject _this, jlong con)
Jason Sams1c415172010-11-08 17:06:46 -0800952{
Andreas Gampe67333922014-11-10 20:35:59 -0800953 if (kLogApi) {
954 ALOGD("nContextGetErrorMessage, con(%p)", (RsContext)con);
955 }
Jason Sams1c415172010-11-08 17:06:46 -0800956 char buf[1024];
957
958 size_t receiveLen;
959 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800960 int id = rsContextGetMessage((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -0700961 buf, sizeof(buf),
962 &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -0700963 &subID, sizeof(subID));
Jason Sams1c415172010-11-08 17:06:46 -0800964 if (!id && receiveLen) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +0100965 ALOGV("message receive buffer too small. %zu", receiveLen);
Jason Sams1c415172010-11-08 17:06:46 -0800966 }
967 return _env->NewStringUTF(buf);
968}
969
Jason Samsedbfabd2011-05-17 15:01:29 -0700970static jint
Tim Murrayeff663f2013-11-15 13:08:30 -0800971nContextGetUserMessage(JNIEnv *_env, jobject _this, jlong con, jintArray data)
Jason Sams516c3192009-10-06 13:58:47 -0700972{
Jason Sams516c3192009-10-06 13:58:47 -0700973 jint len = _env->GetArrayLength(data);
Andreas Gampe67333922014-11-10 20:35:59 -0800974 if (kLogApi) {
975 ALOGD("nContextGetMessage, con(%p), len(%i)", (RsContext)con, len);
976 }
Chris Wailes488230c32014-08-14 11:22:40 -0700977 jint *ptr = _env->GetIntArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -0700978 if (ptr == nullptr) {
979 ALOGE("Failed to get Java array elements");
980 return 0;
981 }
Jason Sams516c3192009-10-06 13:58:47 -0700982 size_t receiveLen;
Jason Sams1c415172010-11-08 17:06:46 -0800983 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800984 int id = rsContextGetMessage((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -0700985 ptr, len * 4,
986 &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -0700987 &subID, sizeof(subID));
Jason Sams516c3192009-10-06 13:58:47 -0700988 if (!id && receiveLen) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +0100989 ALOGV("message receive buffer too small. %zu", receiveLen);
Jason Sams516c3192009-10-06 13:58:47 -0700990 }
991 _env->ReleaseIntArrayElements(data, ptr, 0);
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000992 return (jint)id;
Jason Sams1c415172010-11-08 17:06:46 -0800993}
994
995static jint
Tim Murrayeff663f2013-11-15 13:08:30 -0800996nContextPeekMessage(JNIEnv *_env, jobject _this, jlong con, jintArray auxData)
Jason Sams1c415172010-11-08 17:06:46 -0800997{
Andreas Gampe67333922014-11-10 20:35:59 -0800998 if (kLogApi) {
999 ALOGD("nContextPeekMessage, con(%p)", (RsContext)con);
1000 }
Chris Wailes488230c32014-08-14 11:22:40 -07001001 jint *auxDataPtr = _env->GetIntArrayElements(auxData, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001002 if (auxDataPtr == nullptr) {
1003 ALOGE("Failed to get Java array elements");
1004 return 0;
1005 }
Jason Sams1c415172010-11-08 17:06:46 -08001006 size_t receiveLen;
1007 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -08001008 int id = rsContextPeekMessage((RsContext)con, &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -07001009 &subID, sizeof(subID));
Jason Sams1c415172010-11-08 17:06:46 -08001010 auxDataPtr[0] = (jint)subID;
1011 auxDataPtr[1] = (jint)receiveLen;
1012 _env->ReleaseIntArrayElements(auxData, auxDataPtr, 0);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001013 return (jint)id;
Jason Sams516c3192009-10-06 13:58:47 -07001014}
1015
Tim Murrayeff663f2013-11-15 13:08:30 -08001016static void nContextInitToClient(JNIEnv *_env, jobject _this, jlong con)
Jason Sams516c3192009-10-06 13:58:47 -07001017{
Andreas Gampe67333922014-11-10 20:35:59 -08001018 if (kLogApi) {
1019 ALOGD("nContextInitToClient, con(%p)", (RsContext)con);
1020 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001021 rsContextInitToClient((RsContext)con);
Jason Sams516c3192009-10-06 13:58:47 -07001022}
1023
Tim Murrayeff663f2013-11-15 13:08:30 -08001024static void nContextDeinitToClient(JNIEnv *_env, jobject _this, jlong con)
Jason Sams516c3192009-10-06 13:58:47 -07001025{
Andreas Gampe67333922014-11-10 20:35:59 -08001026 if (kLogApi) {
1027 ALOGD("nContextDeinitToClient, con(%p)", (RsContext)con);
1028 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001029 rsContextDeinitToClient((RsContext)con);
Jason Sams516c3192009-10-06 13:58:47 -07001030}
1031
Jason Sams455d6442013-02-05 19:20:18 -08001032static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001033nContextSendMessage(JNIEnv *_env, jobject _this, jlong con, jint id, jintArray data)
Jason Sams455d6442013-02-05 19:20:18 -08001034{
Chris Wailes488230c32014-08-14 11:22:40 -07001035 jint *ptr = nullptr;
Jason Sams455d6442013-02-05 19:20:18 -08001036 jint len = 0;
1037 if (data) {
1038 len = _env->GetArrayLength(data);
Stephen Hines4a043c12014-08-21 23:20:32 -07001039 ptr = _env->GetIntArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001040 if (ptr == nullptr) {
1041 ALOGE("Failed to get Java array elements");
1042 return;
1043 }
Jason Sams455d6442013-02-05 19:20:18 -08001044 }
Andreas Gampe67333922014-11-10 20:35:59 -08001045 if (kLogApi) {
1046 ALOGD("nContextSendMessage, con(%p), id(%i), len(%i)", (RsContext)con, id, len);
1047 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001048 rsContextSendMessage((RsContext)con, id, (const uint8_t *)ptr, len * sizeof(int));
Jason Sams455d6442013-02-05 19:20:18 -08001049 if (data) {
1050 _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
1051 }
1052}
1053
1054
Jason Sams516c3192009-10-06 13:58:47 -07001055
Tim Murray460a0492013-11-19 12:45:54 -08001056static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001057nElementCreate(JNIEnv *_env, jobject _this, jlong con, jlong type, jint kind, jboolean norm,
1058 jint size)
Jason Samsd19f10d2009-05-22 14:03:28 -07001059{
Andreas Gampe67333922014-11-10 20:35:59 -08001060 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001061 ALOGD("nElementCreate, con(%p), type(%" PRId64 "), kind(%i), norm(%i), size(%i)", (RsContext)con,
Andreas Gampe67333922014-11-10 20:35:59 -08001062 type, kind, norm, size);
1063 }
1064 return (jlong)(uintptr_t)rsElementCreate((RsContext)con, (RsDataType)type, (RsDataKind)kind,
Yang Ni3f3965e2016-03-08 20:59:48 +00001065 norm, size);
Jason Samsd19f10d2009-05-22 14:03:28 -07001066}
1067
Tim Murray460a0492013-11-19 12:45:54 -08001068static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001069nElementCreate2(JNIEnv *_env, jobject _this, jlong con,
Ashok Bhat98071552014-02-12 09:54:43 +00001070 jlongArray _ids, jobjectArray _names, jintArray _arraySizes)
Jason Samsd19f10d2009-05-22 14:03:28 -07001071{
Jason Sams718cd1f2009-12-23 14:35:29 -08001072 int fieldCount = _env->GetArrayLength(_ids);
Andreas Gampe67333922014-11-10 20:35:59 -08001073 if (kLogApi) {
1074 ALOGD("nElementCreate2, con(%p)", (RsContext)con);
1075 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001076
Chris Wailes488230c32014-08-14 11:22:40 -07001077 jlong *jIds = _env->GetLongArrayElements(_ids, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001078 if (jIds == nullptr) {
1079 ALOGE("Failed to get Java array elements: ids");
1080 return 0;
1081 }
Chris Wailes488230c32014-08-14 11:22:40 -07001082 jint *jArraySizes = _env->GetIntArrayElements(_arraySizes, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001083 if (jArraySizes == nullptr) {
1084 ALOGE("Failed to get Java array elements: arraySizes");
1085 return 0;
1086 }
Ashok Bhat98071552014-02-12 09:54:43 +00001087
1088 RsElement *ids = (RsElement*)malloc(fieldCount * sizeof(RsElement));
1089 uint32_t *arraySizes = (uint32_t *)malloc(fieldCount * sizeof(uint32_t));
1090
1091 for(int i = 0; i < fieldCount; i ++) {
1092 ids[i] = (RsElement)jIds[i];
1093 arraySizes[i] = (uint32_t)jArraySizes[i];
1094 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001095
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08001096 AutoJavaStringArrayToUTF8 names(_env, _names, fieldCount);
1097
1098 const char **nameArray = names.c_str();
1099 size_t *sizeArray = names.c_str_len();
1100
Tim Murray3aa89c12014-08-18 17:51:22 -07001101 jlong id = (jlong)(uintptr_t)rsElementCreate2((RsContext)con,
Ashok Bhat98071552014-02-12 09:54:43 +00001102 (const RsElement *)ids, fieldCount,
Jason Sams7a22e102011-05-06 14:14:30 -07001103 nameArray, fieldCount * sizeof(size_t), sizeArray,
Yang Ni3f3965e2016-03-08 20:59:48 +00001104 (const uint32_t *)arraySizes, fieldCount);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08001105
Ashok Bhat98071552014-02-12 09:54:43 +00001106 free(ids);
1107 free(arraySizes);
1108 _env->ReleaseLongArrayElements(_ids, jIds, JNI_ABORT);
1109 _env->ReleaseIntArrayElements(_arraySizes, jArraySizes, JNI_ABORT);
1110
Tim Murray3aa89c12014-08-18 17:51:22 -07001111 return (jlong)(uintptr_t)id;
Jason Samsd19f10d2009-05-22 14:03:28 -07001112}
1113
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001114static void
Tim Murray460a0492013-11-19 12:45:54 -08001115nElementGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jintArray _elementData)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001116{
1117 int dataSize = _env->GetArrayLength(_elementData);
Andreas Gampe67333922014-11-10 20:35:59 -08001118 if (kLogApi) {
1119 ALOGD("nElementGetNativeData, con(%p)", (RsContext)con);
1120 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001121
1122 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
1123 assert(dataSize == 5);
1124
Narayan Kamath78c0ce52014-03-19 10:15:51 +00001125 uintptr_t elementData[5];
Tim Murrayeff663f2013-11-15 13:08:30 -08001126 rsaElementGetNativeData((RsContext)con, (RsElement)id, elementData, dataSize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001127
1128 for(jint i = 0; i < dataSize; i ++) {
Ashok Bhat98071552014-02-12 09:54:43 +00001129 const jint data = (jint)elementData[i];
1130 _env->SetIntArrayRegion(_elementData, i, 1, &data);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001131 }
1132}
1133
1134
1135static void
Tim Murray460a0492013-11-19 12:45:54 -08001136nElementGetSubElements(JNIEnv *_env, jobject _this, jlong con, jlong id,
Ashok Bhat98071552014-02-12 09:54:43 +00001137 jlongArray _IDs,
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -07001138 jobjectArray _names,
1139 jintArray _arraySizes)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001140{
Ashok Bhat98071552014-02-12 09:54:43 +00001141 uint32_t dataSize = _env->GetArrayLength(_IDs);
Andreas Gampe67333922014-11-10 20:35:59 -08001142 if (kLogApi) {
1143 ALOGD("nElementGetSubElements, con(%p)", (RsContext)con);
1144 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001145
Ashok Bhat98071552014-02-12 09:54:43 +00001146 uintptr_t *ids = (uintptr_t*)malloc(dataSize * sizeof(uintptr_t));
1147 const char **names = (const char **)malloc(dataSize * sizeof(const char *));
Narayan Kamath78c0ce52014-03-19 10:15:51 +00001148 uint32_t *arraySizes = (uint32_t *)malloc(dataSize * sizeof(uint32_t));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001149
Andreas Gampe67333922014-11-10 20:35:59 -08001150 rsaElementGetSubElements((RsContext)con, (RsElement)id, ids, names, arraySizes,
1151 (uint32_t)dataSize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001152
Ashok Bhat98071552014-02-12 09:54:43 +00001153 for(uint32_t i = 0; i < dataSize; i++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07001154 const jlong id = (jlong)(uintptr_t)ids[i];
Ashok Bhat98071552014-02-12 09:54:43 +00001155 const jint arraySize = (jint)arraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001156 _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
Ashok Bhat98071552014-02-12 09:54:43 +00001157 _env->SetLongArrayRegion(_IDs, i, 1, &id);
1158 _env->SetIntArrayRegion(_arraySizes, i, 1, &arraySize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001159 }
1160
1161 free(ids);
1162 free(names);
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -07001163 free(arraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001164}
1165
Jason Samsd19f10d2009-05-22 14:03:28 -07001166// -----------------------------------
1167
Tim Murray460a0492013-11-19 12:45:54 -08001168static jlong
1169nTypeCreate(JNIEnv *_env, jobject _this, jlong con, jlong eid,
Jason Samsb109cc72013-01-07 18:20:12 -08001170 jint dimx, jint dimy, jint dimz, jboolean mips, jboolean faces, jint yuv)
Jason Samsd19f10d2009-05-22 14:03:28 -07001171{
Andreas Gampe67333922014-11-10 20:35:59 -08001172 if (kLogApi) {
1173 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 +01001174 (RsContext)con, (void*)eid, dimx, dimy, dimz, mips, faces, yuv);
Andreas Gampe67333922014-11-10 20:35:59 -08001175 }
Jason Sams3b9c52a2010-10-14 17:48:46 -07001176
Andreas Gampe67333922014-11-10 20:35:59 -08001177 return (jlong)(uintptr_t)rsTypeCreate((RsContext)con, (RsElement)eid, dimx, dimy, dimz, mips,
Yang Ni3f3965e2016-03-08 20:59:48 +00001178 faces, yuv);
Jason Samsd19f10d2009-05-22 14:03:28 -07001179}
1180
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001181static void
Ashok Bhat98071552014-02-12 09:54:43 +00001182nTypeGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jlongArray _typeData)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001183{
1184 // We are packing 6 items: mDimX; mDimY; mDimZ;
1185 // mDimLOD; mDimFaces; mElement; into typeData
1186 int elementCount = _env->GetArrayLength(_typeData);
1187
1188 assert(elementCount == 6);
Andreas Gampe67333922014-11-10 20:35:59 -08001189 if (kLogApi) {
1190 ALOGD("nTypeGetNativeData, con(%p)", (RsContext)con);
1191 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001192
Ashok Bhat98071552014-02-12 09:54:43 +00001193 uintptr_t typeData[6];
Tim Murrayeff663f2013-11-15 13:08:30 -08001194 rsaTypeGetNativeData((RsContext)con, (RsType)id, typeData, 6);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001195
1196 for(jint i = 0; i < elementCount; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07001197 const jlong data = (jlong)(uintptr_t)typeData[i];
Ashok Bhat98071552014-02-12 09:54:43 +00001198 _env->SetLongArrayRegion(_typeData, i, 1, &data);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001199 }
1200}
1201
Jason Samsd19f10d2009-05-22 14:03:28 -07001202// -----------------------------------
1203
Tim Murray460a0492013-11-19 12:45:54 -08001204static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001205nAllocationCreateTyped(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mips, jint usage,
1206 jlong pointer)
Jason Samsd19f10d2009-05-22 14:03:28 -07001207{
Andreas Gampe67333922014-11-10 20:35:59 -08001208 if (kLogApi) {
1209 ALOGD("nAllocationCreateTyped, con(%p), type(%p), mip(%i), usage(%i), ptr(%p)",
1210 (RsContext)con, (RsElement)type, mips, usage, (void *)pointer);
1211 }
1212 return (jlong)(uintptr_t) rsAllocationCreateTyped((RsContext)con, (RsType)type,
1213 (RsAllocationMipmapControl)mips,
Yang Ni3f3965e2016-03-08 20:59:48 +00001214 (uint32_t)usage, (uintptr_t)pointer);
Jason Samsd19f10d2009-05-22 14:03:28 -07001215}
1216
Jason Samsd19f10d2009-05-22 14:03:28 -07001217static void
Tim Murray460a0492013-11-19 12:45:54 -08001218nAllocationSyncAll(JNIEnv *_env, jobject _this, jlong con, jlong a, jint bits)
Jason Sams5476b452010-12-08 16:14:36 -08001219{
Andreas Gampe67333922014-11-10 20:35:59 -08001220 if (kLogApi) {
1221 ALOGD("nAllocationSyncAll, con(%p), a(%p), bits(0x%08x)", (RsContext)con, (RsAllocation)a,
1222 bits);
1223 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001224 rsAllocationSyncAll((RsContext)con, (RsAllocation)a, (RsAllocationUsageType)bits);
Jason Sams5476b452010-12-08 16:14:36 -08001225}
1226
Miao Wang8c150922015-10-26 17:44:10 -07001227static void
1228nAllocationSetupBufferQueue(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint numAlloc)
1229{
1230 if (kLogApi) {
1231 ALOGD("nAllocationSetupBufferQueue, con(%p), alloc(%p), numAlloc(%d)", (RsContext)con,
1232 (RsAllocation)alloc, numAlloc);
1233 }
1234 rsAllocationSetupBufferQueue((RsContext)con, (RsAllocation)alloc, (uint32_t)numAlloc);
1235}
1236
1237static void
1238nAllocationShareBufferQueue(JNIEnv *_env, jobject _this, jlong con, jlong alloc1, jlong alloc2)
1239{
1240 if (kLogApi) {
1241 ALOGD("nAllocationShareBufferQueue, con(%p), alloc1(%p), alloc2(%p)", (RsContext)con,
1242 (RsAllocation)alloc1, (RsAllocation)alloc2);
1243 }
1244
1245 rsAllocationShareBufferQueue((RsContext)con, (RsAllocation)alloc1, (RsAllocation)alloc2);
1246}
1247
Jason Sams72226e02013-02-22 12:45:54 -08001248static jobject
Tim Murray460a0492013-11-19 12:45:54 -08001249nAllocationGetSurface(JNIEnv *_env, jobject _this, jlong con, jlong a)
Jason Sams615e7ce2012-01-13 14:01:20 -08001250{
Andreas Gampe67333922014-11-10 20:35:59 -08001251 if (kLogApi) {
1252 ALOGD("nAllocationGetSurface, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1253 }
Jason Sams615e7ce2012-01-13 14:01:20 -08001254
Andreas Gampe67333922014-11-10 20:35:59 -08001255 IGraphicBufferProducer *v = (IGraphicBufferProducer *)rsAllocationGetSurface((RsContext)con,
1256 (RsAllocation)a);
Jason Sams72226e02013-02-22 12:45:54 -08001257 sp<IGraphicBufferProducer> bp = v;
Chris Wailes488230c32014-08-14 11:22:40 -07001258 v->decStrong(nullptr);
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001259
Jason Sams72226e02013-02-22 12:45:54 -08001260 jobject o = android_view_Surface_createFromIGraphicBufferProducer(_env, bp);
1261 return o;
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001262}
1263
1264static void
Tim Murray460a0492013-11-19 12:45:54 -08001265nAllocationSetSurface(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject sur)
Jason Sams163766c2012-02-15 12:04:24 -08001266{
Andreas Gampe67333922014-11-10 20:35:59 -08001267 if (kLogApi) {
1268 ALOGD("nAllocationSetSurface, con(%p), alloc(%p), surface(%p)", (RsContext)con,
1269 (RsAllocation)alloc, (Surface *)sur);
1270 }
Jason Sams163766c2012-02-15 12:04:24 -08001271
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001272 sp<Surface> s;
Jason Sams163766c2012-02-15 12:04:24 -08001273 if (sur != 0) {
Jeff Brown64a55af2012-08-26 02:47:39 -07001274 s = android_view_Surface_getSurface(_env, sur);
Jason Sams163766c2012-02-15 12:04:24 -08001275 }
1276
Andreas Gampe67333922014-11-10 20:35:59 -08001277 rsAllocationSetSurface((RsContext)con, (RsAllocation)alloc,
1278 static_cast<ANativeWindow *>(s.get()));
Jason Sams163766c2012-02-15 12:04:24 -08001279}
1280
1281static void
Tim Murray460a0492013-11-19 12:45:54 -08001282nAllocationIoSend(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Sams163766c2012-02-15 12:04:24 -08001283{
Andreas Gampe67333922014-11-10 20:35:59 -08001284 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001285 ALOGD("nAllocationIoSend, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
Andreas Gampe67333922014-11-10 20:35:59 -08001286 }
Tim Murray460a0492013-11-19 12:45:54 -08001287 rsAllocationIoSend((RsContext)con, (RsAllocation)alloc);
Jason Sams163766c2012-02-15 12:04:24 -08001288}
1289
Miao Wang8c150922015-10-26 17:44:10 -07001290static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001291nAllocationIoReceive(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Sams163766c2012-02-15 12:04:24 -08001292{
Andreas Gampe67333922014-11-10 20:35:59 -08001293 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001294 ALOGD("nAllocationIoReceive, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
Andreas Gampe67333922014-11-10 20:35:59 -08001295 }
Miao Wang8c150922015-10-26 17:44:10 -07001296 return (jlong) rsAllocationIoReceive((RsContext)con, (RsAllocation)alloc);
Jason Sams163766c2012-02-15 12:04:24 -08001297}
1298
Jason Sams163766c2012-02-15 12:04:24 -08001299static void
Tim Murray460a0492013-11-19 12:45:54 -08001300nAllocationGenerateMipmaps(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Samsf7086092011-01-12 13:28:37 -08001301{
Andreas Gampe67333922014-11-10 20:35:59 -08001302 if (kLogApi) {
1303 ALOGD("nAllocationGenerateMipmaps, con(%p), a(%p)", (RsContext)con, (RsAllocation)alloc);
1304 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001305 rsAllocationGenerateMipmaps((RsContext)con, (RsAllocation)alloc);
Jason Samsf7086092011-01-12 13:28:37 -08001306}
1307
Tim Murray460a0492013-11-19 12:45:54 -08001308static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001309nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1310 jobject jbitmap, jint usage)
Jason Samsffe9f482009-06-01 17:45:53 -07001311{
John Recked207b92015-04-10 13:52:57 -07001312 SkBitmap bitmap;
1313 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Samsffe9f482009-06-01 17:45:53 -07001314
Jason Sams5476b452010-12-08 16:14:36 -08001315 bitmap.lockPixels();
1316 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001317 jlong id = (jlong)(uintptr_t)rsAllocationCreateFromBitmap((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001318 (RsType)type, (RsAllocationMipmapControl)mip,
Yang Ni3f3965e2016-03-08 20:59:48 +00001319 ptr, bitmap.getSize(), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001320 bitmap.unlockPixels();
1321 return id;
Jason Samsffe9f482009-06-01 17:45:53 -07001322}
Jason Samsfe08d992009-05-27 14:45:32 -07001323
Tim Murray460a0492013-11-19 12:45:54 -08001324static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001325nAllocationCreateBitmapBackedAllocation(JNIEnv *_env, jobject _this, jlong con, jlong type,
1326 jint mip, jobject jbitmap, jint usage)
Tim Murraya3145512012-12-04 17:59:29 -08001327{
John Recked207b92015-04-10 13:52:57 -07001328 SkBitmap bitmap;
1329 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Tim Murraya3145512012-12-04 17:59:29 -08001330
1331 bitmap.lockPixels();
1332 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001333 jlong id = (jlong)(uintptr_t)rsAllocationCreateTyped((RsContext)con,
Tim Murraya3145512012-12-04 17:59:29 -08001334 (RsType)type, (RsAllocationMipmapControl)mip,
Yang Ni3f3965e2016-03-08 20:59:48 +00001335 (uint32_t)usage, (uintptr_t)ptr);
Tim Murraya3145512012-12-04 17:59:29 -08001336 bitmap.unlockPixels();
1337 return id;
1338}
1339
Tim Murray460a0492013-11-19 12:45:54 -08001340static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001341nAllocationCubeCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1342 jobject jbitmap, jint usage)
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001343{
John Recked207b92015-04-10 13:52:57 -07001344 SkBitmap bitmap;
1345 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001346
Jason Sams5476b452010-12-08 16:14:36 -08001347 bitmap.lockPixels();
1348 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001349 jlong id = (jlong)(uintptr_t)rsAllocationCubeCreateFromBitmap((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001350 (RsType)type, (RsAllocationMipmapControl)mip,
Yang Ni3f3965e2016-03-08 20:59:48 +00001351 ptr, bitmap.getSize(), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001352 bitmap.unlockPixels();
1353 return id;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001354}
1355
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001356static void
Tim Murray460a0492013-11-19 12:45:54 -08001357nAllocationCopyFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001358{
John Recked207b92015-04-10 13:52:57 -07001359 SkBitmap bitmap;
1360 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Samsf7086092011-01-12 13:28:37 -08001361 int w = bitmap.width();
1362 int h = bitmap.height();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001363
Jason Sams4ef66502010-12-10 16:03:15 -08001364 bitmap.lockPixels();
1365 const void* ptr = bitmap.getPixels();
Tim Murrayeff663f2013-11-15 13:08:30 -08001366 rsAllocation2DData((RsContext)con, (RsAllocation)alloc, 0, 0,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001367 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Tim Murray38faea32012-11-27 14:55:08 -08001368 w, h, ptr, bitmap.getSize(), 0);
Jason Sams4ef66502010-12-10 16:03:15 -08001369 bitmap.unlockPixels();
1370}
1371
1372static void
Tim Murray460a0492013-11-19 12:45:54 -08001373nAllocationCopyToBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Jason Sams4ef66502010-12-10 16:03:15 -08001374{
John Recked207b92015-04-10 13:52:57 -07001375 SkBitmap bitmap;
1376 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Sams4ef66502010-12-10 16:03:15 -08001377
1378 bitmap.lockPixels();
1379 void* ptr = bitmap.getPixels();
Tim Murrayeff663f2013-11-15 13:08:30 -08001380 rsAllocationCopyToBitmap((RsContext)con, (RsAllocation)alloc, ptr, bitmap.getSize());
Jason Sams4ef66502010-12-10 16:03:15 -08001381 bitmap.unlockPixels();
Alex Sakhartchouk835b8542011-07-20 14:33:10 -07001382 bitmap.notifyPixelsChanged();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001383}
1384
Stephen Hines414fa2c2014-04-17 01:02:42 -07001385// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Samsd19f10d2009-05-22 14:03:28 -07001386static void
Tim Murray460a0492013-11-19 12:45:54 -08001387nAllocationData1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001388 jint count, jobject data, jint sizeBytes, jint dataType, jint mSize,
1389 jboolean usePadding)
Jason Samsd19f10d2009-05-22 14:03:28 -07001390{
Jason Samse729a942013-11-06 11:22:02 -08001391 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001392 if (kLogApi) {
1393 ALOGD("nAllocation1DData, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1394 "dataType(%i)", (RsContext)con, (RsAllocation)alloc, offset, count, sizeBytes,
1395 dataType);
1396 }
Miao Wang87e908d2015-03-02 15:15:15 -08001397 PER_ARRAY_TYPE(nullptr, rsAllocation1DData, true,
1398 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -07001399}
1400
1401static void
Miao Wangc8e237e2015-02-20 18:36:32 -08001402nAllocationElementData(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1403 jint xoff, jint yoff, jint zoff,
1404 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Jason Sams49bdaf02010-08-31 13:50:42 -07001405{
1406 jint len = _env->GetArrayLength(data);
Andreas Gampe67333922014-11-10 20:35:59 -08001407 if (kLogApi) {
Miao Wangc8e237e2015-02-20 18:36:32 -08001408 ALOGD("nAllocationElementData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1409 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
Andreas Gampe67333922014-11-10 20:35:59 -08001410 sizeBytes);
1411 }
Chris Wailes488230c32014-08-14 11:22:40 -07001412 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001413 if (ptr == nullptr) {
1414 ALOGE("Failed to get Java array elements");
1415 return;
1416 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001417 rsAllocationElementData((RsContext)con, (RsAllocation)alloc,
1418 xoff, yoff, zoff,
1419 lod, ptr, sizeBytes, compIdx);
Jason Sams49bdaf02010-08-31 13:50:42 -07001420 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1421}
1422
Miao Wangc8e237e2015-02-20 18:36:32 -08001423
Stephen Hines414fa2c2014-04-17 01:02:42 -07001424// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Sams49bdaf02010-08-31 13:50:42 -07001425static void
Tim Murray460a0492013-11-19 12:45:54 -08001426nAllocationData2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001427 jint w, jint h, jobject data, jint sizeBytes, jint dataType, jint mSize,
1428 jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001429{
Jason Samse729a942013-11-06 11:22:02 -08001430 RsAllocation *alloc = (RsAllocation *)_alloc;
1431 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001432 if (kLogApi) {
1433 ALOGD("nAllocation2DData, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1434 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1435 }
Miao Wang87e908d2015-03-02 15:15:15 -08001436 int count = w * h;
1437 PER_ARRAY_TYPE(nullptr, rsAllocation2DData, true,
1438 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Samsd19f10d2009-05-22 14:03:28 -07001439}
1440
Stephen Hines414fa2c2014-04-17 01:02:42 -07001441// Copies from the Allocation pointed to by srcAlloc into the Allocation
1442// pointed to by dstAlloc.
Jason Sams40a29e82009-08-10 14:55:26 -07001443static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001444nAllocationData2D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001445 jlong dstAlloc, jint dstXoff, jint dstYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001446 jint dstMip, jint dstFace,
1447 jint width, jint height,
Tim Murray460a0492013-11-19 12:45:54 -08001448 jlong srcAlloc, jint srcXoff, jint srcYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001449 jint srcMip, jint srcFace)
1450{
Andreas Gampe67333922014-11-10 20:35:59 -08001451 if (kLogApi) {
1452 ALOGD("nAllocation2DData_s, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1453 " dstMip(%i), dstFace(%i), width(%i), height(%i),"
1454 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i), srcFace(%i)",
1455 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip, dstFace,
1456 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
1457 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001458
Tim Murrayeff663f2013-11-15 13:08:30 -08001459 rsAllocationCopy2DRange((RsContext)con,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001460 (RsAllocation)dstAlloc,
1461 dstXoff, dstYoff,
1462 dstMip, dstFace,
1463 width, height,
1464 (RsAllocation)srcAlloc,
1465 srcXoff, srcYoff,
1466 srcMip, srcFace);
1467}
1468
Stephen Hines414fa2c2014-04-17 01:02:42 -07001469// Copies from the Java object data into the Allocation pointed to by _alloc.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001470static void
Tim Murray460a0492013-11-19 12:45:54 -08001471nAllocationData3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001472 jint w, jint h, jint d, jobject data, jint sizeBytes, jint dataType,
1473 jint mSize, jboolean usePadding)
Jason Samsb05d6892013-04-09 15:59:24 -07001474{
Jason Samse729a942013-11-06 11:22:02 -08001475 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001476 if (kLogApi) {
1477 ALOGD("nAllocation3DData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1478 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1479 lod, w, h, d, sizeBytes);
1480 }
Miao Wang87e908d2015-03-02 15:15:15 -08001481 int count = w * h * d;
1482 PER_ARRAY_TYPE(nullptr, rsAllocation3DData, true,
1483 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Jason Samsb05d6892013-04-09 15:59:24 -07001484}
1485
Stephen Hines414fa2c2014-04-17 01:02:42 -07001486// Copies from the Allocation pointed to by srcAlloc into the Allocation
1487// pointed to by dstAlloc.
Jason Samsb05d6892013-04-09 15:59:24 -07001488static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001489nAllocationData3D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001490 jlong dstAlloc, jint dstXoff, jint dstYoff, jint dstZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001491 jint dstMip,
1492 jint width, jint height, jint depth,
Tim Murray460a0492013-11-19 12:45:54 -08001493 jlong srcAlloc, jint srcXoff, jint srcYoff, jint srcZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001494 jint srcMip)
1495{
Andreas Gampe67333922014-11-10 20:35:59 -08001496 if (kLogApi) {
1497 ALOGD("nAllocationData3D_alloc, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1498 " dstMip(%i), width(%i), height(%i),"
1499 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i)",
1500 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip,
1501 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip);
1502 }
Jason Samsb05d6892013-04-09 15:59:24 -07001503
Tim Murrayeff663f2013-11-15 13:08:30 -08001504 rsAllocationCopy3DRange((RsContext)con,
Jason Samsb05d6892013-04-09 15:59:24 -07001505 (RsAllocation)dstAlloc,
1506 dstXoff, dstYoff, dstZoff, dstMip,
1507 width, height, depth,
1508 (RsAllocation)srcAlloc,
1509 srcXoff, srcYoff, srcZoff, srcMip);
1510}
1511
Jason Sams21659ac2013-11-06 15:08:07 -08001512
Stephen Hines414fa2c2014-04-17 01:02:42 -07001513// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsb05d6892013-04-09 15:59:24 -07001514static void
Miao Wang87e908d2015-03-02 15:15:15 -08001515nAllocationRead(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jobject data, jint dataType,
1516 jint mSize, jboolean usePadding)
Jason Sams40a29e82009-08-10 14:55:26 -07001517{
Jason Sams21659ac2013-11-06 15:08:07 -08001518 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001519 if (kLogApi) {
1520 ALOGD("nAllocationRead, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
1521 }
Miao Wang87e908d2015-03-02 15:15:15 -08001522 int count = 0;
1523 PER_ARRAY_TYPE(0, rsAllocationRead, false,
1524 (RsContext)con, alloc, ptr, len * typeBytes);
Jason Sams40a29e82009-08-10 14:55:26 -07001525}
1526
Stephen Hines414fa2c2014-04-17 01:02:42 -07001527// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Sams40a29e82009-08-10 14:55:26 -07001528static void
Tim Murray460a0492013-11-19 12:45:54 -08001529nAllocationRead1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001530 jint count, jobject data, jint sizeBytes, jint dataType,
1531 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001532{
Jason Sams21659ac2013-11-06 15:08:07 -08001533 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001534 if (kLogApi) {
1535 ALOGD("nAllocation1DRead, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1536 "dataType(%i)", (RsContext)con, alloc, offset, count, sizeBytes, dataType);
1537 }
Miao Wang87e908d2015-03-02 15:15:15 -08001538 PER_ARRAY_TYPE(0, rsAllocation1DRead, false,
1539 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsfb9f82c2011-01-12 14:53:25 -08001540}
1541
Miao Wangc8e237e2015-02-20 18:36:32 -08001542// Copies from the Element in the Allocation pointed to by _alloc into the Java array data.
1543static void
Miao Wang45cec0a2015-03-04 16:40:21 -08001544nAllocationElementRead(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
Miao Wangc8e237e2015-02-20 18:36:32 -08001545 jint xoff, jint yoff, jint zoff,
Miao Wang45cec0a2015-03-04 16:40:21 -08001546 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Miao Wangc8e237e2015-02-20 18:36:32 -08001547{
Miao Wang45cec0a2015-03-04 16:40:21 -08001548 jint len = _env->GetArrayLength(data);
Miao Wangc8e237e2015-02-20 18:36:32 -08001549 if (kLogApi) {
Miao Wang45cec0a2015-03-04 16:40:21 -08001550 ALOGD("nAllocationElementRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1551 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
1552 sizeBytes);
Miao Wangc8e237e2015-02-20 18:36:32 -08001553 }
Miao Wang45cec0a2015-03-04 16:40:21 -08001554 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001555 if (ptr == nullptr) {
1556 ALOGE("Failed to get Java array elements");
1557 return;
1558 }
Miao Wang45cec0a2015-03-04 16:40:21 -08001559 rsAllocationElementRead((RsContext)con, (RsAllocation)alloc,
1560 xoff, yoff, zoff,
Jason Samsa7e25092015-03-11 11:00:00 -07001561 lod, ptr, sizeBytes, compIdx);
Miao Wangbfa5e652015-05-04 15:29:25 -07001562 _env->ReleaseByteArrayElements(data, ptr, 0);
Miao Wangc8e237e2015-02-20 18:36:32 -08001563}
1564
Stephen Hines414fa2c2014-04-17 01:02:42 -07001565// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsfb9f82c2011-01-12 14:53:25 -08001566static void
Tim Murray460a0492013-11-19 12:45:54 -08001567nAllocationRead2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001568 jint w, jint h, jobject data, jint sizeBytes, jint dataType,
1569 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001570{
Jason Sams21659ac2013-11-06 15:08:07 -08001571 RsAllocation *alloc = (RsAllocation *)_alloc;
1572 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001573 if (kLogApi) {
1574 ALOGD("nAllocation2DRead, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1575 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1576 }
Miao Wang87e908d2015-03-02 15:15:15 -08001577 int count = w * h;
1578 PER_ARRAY_TYPE(0, rsAllocation2DRead, false,
1579 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Sams40a29e82009-08-10 14:55:26 -07001580}
Miao Wang87e908d2015-03-02 15:15:15 -08001581
Miao Wangc8e237e2015-02-20 18:36:32 -08001582// Copies from the Allocation pointed to by _alloc into the Java object data.
1583static void
1584nAllocationRead3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001585 jint w, jint h, jint d, jobject data, int sizeBytes, int dataType,
1586 jint mSize, jboolean usePadding)
Miao Wangc8e237e2015-02-20 18:36:32 -08001587{
1588 RsAllocation *alloc = (RsAllocation *)_alloc;
1589 if (kLogApi) {
1590 ALOGD("nAllocation3DRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1591 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1592 lod, w, h, d, sizeBytes);
1593 }
Miao Wang87e908d2015-03-02 15:15:15 -08001594 int count = w * h * d;
1595 PER_ARRAY_TYPE(nullptr, rsAllocation3DRead, false,
1596 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Miao Wangc8e237e2015-02-20 18:36:32 -08001597}
Jason Samsd19f10d2009-05-22 14:03:28 -07001598
Tim Murray460a0492013-11-19 12:45:54 -08001599static jlong
1600nAllocationGetType(JNIEnv *_env, jobject _this, jlong con, jlong a)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001601{
Andreas Gampe67333922014-11-10 20:35:59 -08001602 if (kLogApi) {
1603 ALOGD("nAllocationGetType, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1604 }
Tim Murray3aa89c12014-08-18 17:51:22 -07001605 return (jlong)(uintptr_t) rsaAllocationGetType((RsContext)con, (RsAllocation)a);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001606}
1607
Jason Sams5edc6082010-10-05 13:32:49 -07001608static void
Tim Murray460a0492013-11-19 12:45:54 -08001609nAllocationResize1D(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint dimX)
Jason Sams5edc6082010-10-05 13:32:49 -07001610{
Andreas Gampe67333922014-11-10 20:35:59 -08001611 if (kLogApi) {
1612 ALOGD("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", (RsContext)con,
1613 (RsAllocation)alloc, dimX);
1614 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001615 rsAllocationResize1D((RsContext)con, (RsAllocation)alloc, dimX);
Jason Sams5edc6082010-10-05 13:32:49 -07001616}
1617
Jason Sams46ba27e32015-02-06 17:45:15 -08001618
1619static jlong
1620nAllocationAdapterCreate(JNIEnv *_env, jobject _this, jlong con, jlong basealloc, jlong type)
1621{
1622 if (kLogApi) {
1623 ALOGD("nAllocationAdapterCreate, con(%p), base(%p), type(%p)",
1624 (RsContext)con, (RsAllocation)basealloc, (RsElement)type);
1625 }
1626 return (jlong)(uintptr_t) rsAllocationAdapterCreate((RsContext)con, (RsType)type,
1627 (RsAllocation)basealloc);
1628
1629}
1630
1631static void
1632nAllocationAdapterOffset(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1633 jint x, jint y, jint z, jint face, jint lod,
1634 jint a1, jint a2, jint a3, jint a4)
1635{
1636 uint32_t params[] = {
1637 (uint32_t)x, (uint32_t)y, (uint32_t)z, (uint32_t)face,
1638 (uint32_t)lod, (uint32_t)a1, (uint32_t)a2, (uint32_t)a3, (uint32_t)a4
1639 };
1640 if (kLogApi) {
1641 ALOGD("nAllocationAdapterOffset, con(%p), alloc(%p), x(%i), y(%i), z(%i), face(%i), lod(%i), arrays(%i %i %i %i)",
1642 (RsContext)con, (RsAllocation)alloc, x, y, z, face, lod, a1, a2, a3, a4);
1643 }
1644 rsAllocationAdapterOffset((RsContext)con, (RsAllocation)alloc,
1645 params, sizeof(params));
1646}
1647
1648
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001649// -----------------------------------
1650
Tim Murray460a0492013-11-19 12:45:54 -08001651static jlong
1652nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con, jlong native_asset)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001653{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001654 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001655 ALOGV("______nFileA3D %p", asset);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001656
Tim Murray3aa89c12014-08-18 17:51:22 -07001657 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromMemory((RsContext)con, asset->getBuffer(false), asset->getLength());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001658 return id;
1659}
1660
Tim Murray460a0492013-11-19 12:45:54 -08001661static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001662nFileA3DCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001663{
1664 AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001665 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001666 return 0;
1667 }
1668
1669 AutoJavaStringToUTF8 str(_env, _path);
1670 Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
Chris Wailes488230c32014-08-14 11:22:40 -07001671 if (asset == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001672 return 0;
1673 }
1674
Tim Murray3aa89c12014-08-18 17:51:22 -07001675 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromAsset((RsContext)con, asset);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001676 return id;
1677}
1678
Tim Murray460a0492013-11-19 12:45:54 -08001679static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001680nFileA3DCreateFromFile(JNIEnv *_env, jobject _this, jlong con, jstring fileName)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001681{
1682 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001683 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromFile((RsContext)con, fileNameUTF.c_str());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001684
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001685 return id;
1686}
1687
Tim Murray460a0492013-11-19 12:45:54 -08001688static jint
1689nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001690{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001691 int32_t numEntries = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001692 rsaFileA3DGetNumIndexEntries((RsContext)con, &numEntries, (RsFile)fileA3D);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001693 return (jint)numEntries;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001694}
1695
1696static void
Tim Murray460a0492013-11-19 12:45:54 -08001697nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001698{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001699 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001700 RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
1701
Tim Murrayeff663f2013-11-15 13:08:30 -08001702 rsaFileA3DGetIndexEntries((RsContext)con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001703
1704 for(jint i = 0; i < numEntries; i ++) {
1705 _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
1706 _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
1707 }
1708
1709 free(fileEntries);
1710}
1711
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001712static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001713nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint index)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001714{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001715 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Tim Murray3aa89c12014-08-18 17:51:22 -07001716 jlong id = (jlong)(uintptr_t)rsaFileA3DGetEntryByIndex((RsContext)con, (uint32_t)index, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001717 return id;
1718}
Jason Samsd19f10d2009-05-22 14:03:28 -07001719
1720// -----------------------------------
1721
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001722static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001723nFontCreateFromFile(JNIEnv *_env, jobject _this, jlong con,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001724 jstring fileName, jfloat fontSize, jint dpi)
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001725{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001726 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001727 jlong id = (jlong)(uintptr_t)rsFontCreateFromFile((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001728 fileNameUTF.c_str(), fileNameUTF.length(),
1729 fontSize, dpi);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001730
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001731 return id;
1732}
1733
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001734static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001735nFontCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con,
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001736 jstring name, jfloat fontSize, jint dpi, jlong native_asset)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001737{
1738 Asset* asset = reinterpret_cast<Asset*>(native_asset);
1739 AutoJavaStringToUTF8 nameUTF(_env, name);
1740
Tim Murray3aa89c12014-08-18 17:51:22 -07001741 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001742 nameUTF.c_str(), nameUTF.length(),
1743 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001744 asset->getBuffer(false), asset->getLength());
1745 return id;
1746}
1747
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001748static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001749nFontCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001750 jfloat fontSize, jint dpi)
1751{
1752 AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001753 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001754 return 0;
1755 }
1756
1757 AutoJavaStringToUTF8 str(_env, _path);
1758 Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
Chris Wailes488230c32014-08-14 11:22:40 -07001759 if (asset == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001760 return 0;
1761 }
1762
Tim Murray3aa89c12014-08-18 17:51:22 -07001763 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001764 str.c_str(), str.length(),
1765 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001766 asset->getBuffer(false), asset->getLength());
1767 delete asset;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001768 return id;
1769}
1770
Jason Samsbd1c3ad2009-08-03 16:03:08 -07001771// -----------------------------------
1772
1773static void
Tim Murray460a0492013-11-19 12:45:54 -08001774nScriptBindAllocation(JNIEnv *_env, jobject _this, jlong con, jlong script, jlong alloc, jint slot)
Jason Samsd19f10d2009-05-22 14:03:28 -07001775{
Andreas Gampe67333922014-11-10 20:35:59 -08001776 if (kLogApi) {
1777 ALOGD("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", (RsContext)con,
1778 (RsScript)script, (RsAllocation)alloc, slot);
1779 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001780 rsScriptBindAllocation((RsContext)con, (RsScript)script, (RsAllocation)alloc, slot);
Jason Samsd19f10d2009-05-22 14:03:28 -07001781}
1782
1783static void
Tim Murray460a0492013-11-19 12:45:54 -08001784nScriptSetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jint val)
Jason Sams4d339932010-05-11 14:03:58 -07001785{
Andreas Gampe67333922014-11-10 20:35:59 -08001786 if (kLogApi) {
1787 ALOGD("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", (RsContext)con, (void *)script,
1788 slot, val);
1789 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001790 rsScriptSetVarI((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001791}
1792
Tim Murray7c4caad2013-04-10 16:21:40 -07001793static jint
Tim Murray460a0492013-11-19 12:45:54 -08001794nScriptGetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001795{
Andreas Gampe67333922014-11-10 20:35:59 -08001796 if (kLogApi) {
1797 ALOGD("nScriptGetVarI, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1798 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001799 int value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001800 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001801 return value;
1802}
1803
Jason Sams4d339932010-05-11 14:03:58 -07001804static void
Tim Murray460a0492013-11-19 12:45:54 -08001805nScriptSetVarObj(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001806{
Andreas Gampe67333922014-11-10 20:35:59 -08001807 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001808 ALOGD("nScriptSetVarObj, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001809 slot, val);
1810 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001811 rsScriptSetVarObj((RsContext)con, (RsScript)script, slot, (RsObjectBase)val);
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001812}
1813
1814static void
Tim Murray460a0492013-11-19 12:45:54 -08001815nScriptSetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Stephen Hines031ec58c2010-10-11 10:54:21 -07001816{
Andreas Gampe67333922014-11-10 20:35:59 -08001817 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001818 ALOGD("nScriptSetVarJ, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001819 slot, val);
1820 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001821 rsScriptSetVarJ((RsContext)con, (RsScript)script, slot, val);
Stephen Hines031ec58c2010-10-11 10:54:21 -07001822}
1823
Tim Murray7c4caad2013-04-10 16:21:40 -07001824static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001825nScriptGetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001826{
Andreas Gampe67333922014-11-10 20:35:59 -08001827 if (kLogApi) {
1828 ALOGD("nScriptGetVarJ, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1829 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001830 jlong value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001831 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001832 return value;
1833}
1834
Stephen Hines031ec58c2010-10-11 10:54:21 -07001835static void
Tim Murray460a0492013-11-19 12:45:54 -08001836nScriptSetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, float val)
Jason Sams4d339932010-05-11 14:03:58 -07001837{
Andreas Gampe67333922014-11-10 20:35:59 -08001838 if (kLogApi) {
1839 ALOGD("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", (RsContext)con, (void *)script,
1840 slot, val);
1841 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001842 rsScriptSetVarF((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001843}
1844
Tim Murray7c4caad2013-04-10 16:21:40 -07001845static jfloat
Tim Murray460a0492013-11-19 12:45:54 -08001846nScriptGetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001847{
Andreas Gampe67333922014-11-10 20:35:59 -08001848 if (kLogApi) {
1849 ALOGD("nScriptGetVarF, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1850 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001851 jfloat value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001852 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001853 return value;
1854}
1855
Jason Sams4d339932010-05-11 14:03:58 -07001856static void
Tim Murray460a0492013-11-19 12:45:54 -08001857nScriptSetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, double val)
Stephen Hinesca54ec32010-09-20 17:20:30 -07001858{
Andreas Gampe67333922014-11-10 20:35:59 -08001859 if (kLogApi) {
1860 ALOGD("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", (RsContext)con, (void *)script,
1861 slot, val);
1862 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001863 rsScriptSetVarD((RsContext)con, (RsScript)script, slot, val);
Stephen Hinesca54ec32010-09-20 17:20:30 -07001864}
1865
Tim Murray7c4caad2013-04-10 16:21:40 -07001866static jdouble
Tim Murray460a0492013-11-19 12:45:54 -08001867nScriptGetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001868{
Andreas Gampe67333922014-11-10 20:35:59 -08001869 if (kLogApi) {
1870 ALOGD("nScriptGetVarD, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1871 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001872 jdouble value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001873 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001874 return value;
1875}
1876
Stephen Hinesca54ec32010-09-20 17:20:30 -07001877static void
Tim Murray460a0492013-11-19 12:45:54 -08001878nScriptSetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001879{
Andreas Gampe67333922014-11-10 20:35:59 -08001880 if (kLogApi) {
1881 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1882 }
Jason Sams4d339932010-05-11 14:03:58 -07001883 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001884 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001885 if (ptr == nullptr) {
1886 ALOGE("Failed to get Java array elements");
1887 return;
1888 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001889 rsScriptSetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001890 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1891}
1892
Stephen Hinesadeb8092012-04-20 14:26:06 -07001893static void
Tim Murray460a0492013-11-19 12:45:54 -08001894nScriptGetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Tim Murray7c4caad2013-04-10 16:21:40 -07001895{
Andreas Gampe67333922014-11-10 20:35:59 -08001896 if (kLogApi) {
1897 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1898 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001899 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001900 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001901 if (ptr == nullptr) {
1902 ALOGE("Failed to get Java array elements");
1903 return;
1904 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001905 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Stephen Hines414fa2c2014-04-17 01:02:42 -07001906 _env->ReleaseByteArrayElements(data, ptr, 0);
Tim Murray7c4caad2013-04-10 16:21:40 -07001907}
1908
1909static void
Andreas Gampe67333922014-11-10 20:35:59 -08001910nScriptSetVarVE(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data,
1911 jlong elem, jintArray dims)
Stephen Hinesadeb8092012-04-20 14:26:06 -07001912{
Andreas Gampe67333922014-11-10 20:35:59 -08001913 if (kLogApi) {
1914 ALOGD("nScriptSetVarVE, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1915 }
Stephen Hinesadeb8092012-04-20 14:26:06 -07001916 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001917 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001918 if (ptr == nullptr) {
1919 ALOGE("Failed to get Java array elements");
1920 return;
1921 }
Stephen Hinesadeb8092012-04-20 14:26:06 -07001922 jint dimsLen = _env->GetArrayLength(dims) * sizeof(int);
Chris Wailes488230c32014-08-14 11:22:40 -07001923 jint *dimsPtr = _env->GetIntArrayElements(dims, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001924 if (dimsPtr == nullptr) {
1925 ALOGE("Failed to get Java array elements");
1926 return;
1927 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001928 rsScriptSetVarVE((RsContext)con, (RsScript)script, slot, ptr, len, (RsElement)elem,
Stephen Hinesbc5d3ee2014-06-25 00:03:39 -07001929 (const uint32_t*) dimsPtr, dimsLen);
Stephen Hinesadeb8092012-04-20 14:26:06 -07001930 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1931 _env->ReleaseIntArrayElements(dims, dimsPtr, JNI_ABORT);
1932}
1933
Jason Samsd19f10d2009-05-22 14:03:28 -07001934
1935static void
Tim Murray460a0492013-11-19 12:45:54 -08001936nScriptSetTimeZone(JNIEnv *_env, jobject _this, jlong con, jlong script, jbyteArray timeZone)
Jason Samsd19f10d2009-05-22 14:03:28 -07001937{
Andreas Gampe67333922014-11-10 20:35:59 -08001938 if (kLogApi) {
1939 ALOGD("nScriptCSetTimeZone, con(%p), s(%p)", (RsContext)con, (void *)script);
1940 }
Romain Guy584a3752009-07-30 18:45:01 -07001941
1942 jint length = _env->GetArrayLength(timeZone);
1943 jbyte* timeZone_ptr;
1944 timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
Miao Wangba8766c2015-10-12 17:24:13 -07001945 if (timeZone_ptr == nullptr) {
1946 ALOGE("Failed to get Java array elements");
1947 return;
1948 }
Romain Guy584a3752009-07-30 18:45:01 -07001949
Tim Murrayeff663f2013-11-15 13:08:30 -08001950 rsScriptSetTimeZone((RsContext)con, (RsScript)script, (const char *)timeZone_ptr, length);
Romain Guy584a3752009-07-30 18:45:01 -07001951
1952 if (timeZone_ptr) {
1953 _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
1954 }
1955}
1956
Jason Samsfbf0b9e2009-08-13 12:59:04 -07001957static void
Tim Murray460a0492013-11-19 12:45:54 -08001958nScriptInvoke(JNIEnv *_env, jobject _this, jlong con, jlong obj, jint slot)
Jason Samsbe2e8412009-09-16 15:04:38 -07001959{
Andreas Gampe67333922014-11-10 20:35:59 -08001960 if (kLogApi) {
1961 ALOGD("nScriptInvoke, con(%p), script(%p)", (RsContext)con, (void *)obj);
1962 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001963 rsScriptInvoke((RsContext)con, (RsScript)obj, slot);
Jason Samsbe2e8412009-09-16 15:04:38 -07001964}
1965
1966static void
Tim Murray460a0492013-11-19 12:45:54 -08001967nScriptInvokeV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001968{
Andreas Gampe67333922014-11-10 20:35:59 -08001969 if (kLogApi) {
1970 ALOGD("nScriptInvokeV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1971 }
Jason Sams4d339932010-05-11 14:03:58 -07001972 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001973 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001974 if (ptr == nullptr) {
1975 ALOGE("Failed to get Java array elements");
1976 return;
1977 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001978 rsScriptInvokeV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001979 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1980}
1981
Jason Sams6e494d32011-04-27 16:33:11 -07001982static void
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001983nScriptForEach(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
1984 jlongArray ains, jlong aout, jbyteArray params,
1985 jintArray limits)
Jason Sams6e494d32011-04-27 16:33:11 -07001986{
Andreas Gampe67333922014-11-10 20:35:59 -08001987 if (kLogApi) {
Chih-Hung Hsieh9eb9dd32015-05-06 14:42:04 -07001988 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 -08001989 }
Jason Sams6e494d32011-04-27 16:33:11 -07001990
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001991 jint in_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07001992 jlong *in_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07001993
Chris Wailes488230c32014-08-14 11:22:40 -07001994 RsAllocation *in_allocs = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07001995
Chris Wailes488230c32014-08-14 11:22:40 -07001996 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001997 in_len = _env->GetArrayLength(ains);
Yang Ni7b2a46f2015-05-05 12:41:19 -07001998 if (in_len > (jint)RS_KERNEL_MAX_ARGUMENTS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -07001999 ALOGE("Too many arguments in kernel launch.");
2000 // TODO (b/20758983): Report back to Java and throw an exception
2001 return;
2002 }
Chris Wailes94961062014-06-11 12:01:28 -07002003
Yang Ni17c2d7a2015-04-30 16:13:54 -07002004 in_ptr = _env->GetLongArrayElements(ains, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002005 if (in_ptr == nullptr) {
2006 ALOGE("Failed to get Java array elements");
2007 return;
2008 }
2009
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002010 if (sizeof(RsAllocation) == sizeof(jlong)) {
2011 in_allocs = (RsAllocation*)in_ptr;
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002012 } else {
2013 // Convert from 64-bit jlong types to the native pointer type.
Chris Wailes94961062014-06-11 12:01:28 -07002014
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002015 in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
Yang Ni17c2d7a2015-04-30 16:13:54 -07002016 if (in_allocs == nullptr) {
2017 ALOGE("Failed launching kernel for lack of memory.");
2018 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2019 return;
2020 }
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002021
2022 for (int index = in_len; --index >= 0;) {
2023 in_allocs[index] = (RsAllocation)in_ptr[index];
2024 }
2025 }
Chris Wailes94961062014-06-11 12:01:28 -07002026 }
2027
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002028 jint param_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002029 jbyte *param_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002030
Chris Wailes488230c32014-08-14 11:22:40 -07002031 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002032 param_len = _env->GetArrayLength(params);
Chris Wailes488230c32014-08-14 11:22:40 -07002033 param_ptr = _env->GetByteArrayElements(params, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002034 if (param_ptr == nullptr) {
2035 ALOGE("Failed to get Java array elements");
2036 return;
2037 }
Chris Wailes94961062014-06-11 12:01:28 -07002038 }
2039
Chris Wailes488230c32014-08-14 11:22:40 -07002040 RsScriptCall sc, *sca = nullptr;
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002041 uint32_t sc_size = 0;
Chris Wailes94961062014-06-11 12:01:28 -07002042
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002043 jint limit_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002044 jint *limit_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002045
Chris Wailes488230c32014-08-14 11:22:40 -07002046 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002047 limit_len = _env->GetArrayLength(limits);
Chris Wailes488230c32014-08-14 11:22:40 -07002048 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002049 if (limit_ptr == nullptr) {
2050 ALOGE("Failed to get Java array elements");
2051 return;
2052 }
Chris Wailes94961062014-06-11 12:01:28 -07002053
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002054 assert(limit_len == 6);
Andreas Gampe67333922014-11-10 20:35:59 -08002055 UNUSED(limit_len); // As the assert might not be compiled.
Chris Wailes94961062014-06-11 12:01:28 -07002056
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002057 sc.xStart = limit_ptr[0];
2058 sc.xEnd = limit_ptr[1];
2059 sc.yStart = limit_ptr[2];
2060 sc.yEnd = limit_ptr[3];
2061 sc.zStart = limit_ptr[4];
2062 sc.zEnd = limit_ptr[5];
2063 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
Jason Sams14331ab2015-01-26 18:14:36 -08002064 sc.arrayStart = 0;
2065 sc.arrayEnd = 0;
2066 sc.array2Start = 0;
2067 sc.array2End = 0;
2068 sc.array3Start = 0;
2069 sc.array3End = 0;
2070 sc.array4Start = 0;
2071 sc.array4End = 0;
Chris Wailes94961062014-06-11 12:01:28 -07002072
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002073 sca = &sc;
Chris Wailes94961062014-06-11 12:01:28 -07002074 }
2075
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002076 rsScriptForEachMulti((RsContext)con, (RsScript)script, slot,
2077 in_allocs, in_len, (RsAllocation)aout,
2078 param_ptr, param_len, sca, sc_size);
Chris Wailes94961062014-06-11 12:01:28 -07002079
Chris Wailes488230c32014-08-14 11:22:40 -07002080 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002081 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
Chris Wailes94961062014-06-11 12:01:28 -07002082 }
2083
Chris Wailes488230c32014-08-14 11:22:40 -07002084 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002085 _env->ReleaseByteArrayElements(params, param_ptr, JNI_ABORT);
2086 }
2087
Chris Wailes488230c32014-08-14 11:22:40 -07002088 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002089 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
2090 }
Chris Wailes94961062014-06-11 12:01:28 -07002091}
2092
Matt Wala36eb1f72015-07-20 15:35:27 -07002093static void
2094nScriptReduce(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
2095 jlong ain, jlong aout, jintArray limits)
2096{
2097 if (kLogApi) {
2098 ALOGD("nScriptReduce, con(%p), s(%p), slot(%i) ain(%" PRId64 ") aout(%" PRId64 ")", (RsContext)con, (void *)script, slot, ain, aout);
2099 }
2100
2101 RsScriptCall sc, *sca = nullptr;
2102 uint32_t sc_size = 0;
2103
2104 jint limit_len = 0;
2105 jint *limit_ptr = nullptr;
2106
2107 // If the caller passed limits, reflect them in the RsScriptCall.
2108 if (limits != nullptr) {
2109 limit_len = _env->GetArrayLength(limits);
2110 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002111 if (limit_ptr == nullptr) {
2112 ALOGE("Failed to get Java array elements");
2113 return;
2114 }
Matt Wala36eb1f72015-07-20 15:35:27 -07002115
2116 // We expect to be passed an array [x1, x2] which specifies
2117 // the sub-range for a 1-dimensional reduction.
2118 assert(limit_len == 2);
2119 UNUSED(limit_len); // As the assert might not be compiled.
2120
2121 sc.xStart = limit_ptr[0];
2122 sc.xEnd = limit_ptr[1];
2123 sc.yStart = 0;
2124 sc.yEnd = 0;
2125 sc.zStart = 0;
2126 sc.zEnd = 0;
2127 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
2128 sc.arrayStart = 0;
2129 sc.arrayEnd = 0;
2130 sc.array2Start = 0;
2131 sc.array2End = 0;
2132 sc.array3Start = 0;
2133 sc.array3End = 0;
2134 sc.array4Start = 0;
2135 sc.array4End = 0;
2136
2137 sca = &sc;
2138 sc_size = sizeof(sc);
2139 }
2140
2141 rsScriptReduce((RsContext)con, (RsScript)script, slot,
2142 (RsAllocation)ain, (RsAllocation)aout,
2143 sca, sc_size);
2144
2145 if (limits != nullptr) {
2146 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
2147 }
2148}
2149
David Gross26ef7a732016-01-12 12:19:15 -08002150static void
2151nScriptReduceNew(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
2152 jlongArray ains, jlong aout, jintArray limits)
2153{
2154 if (kLogApi) {
2155 ALOGD("nScriptReduceNew, con(%p), s(%p), slot(%i) ains(%p) aout(%" PRId64 ")", (RsContext)con, (void *)script, slot, ains, aout);
2156 }
2157
2158 if (ains == nullptr) {
2159 ALOGE("At least one input required.");
2160 // TODO (b/20758983): Report back to Java and throw an exception
2161 return;
2162 }
2163 jint in_len = _env->GetArrayLength(ains);
2164 if (in_len > (jint)RS_KERNEL_MAX_ARGUMENTS) {
2165 ALOGE("Too many arguments in kernel launch.");
2166 // TODO (b/20758983): Report back to Java and throw an exception
2167 return;
2168 }
2169
2170 jlong *in_ptr = _env->GetLongArrayElements(ains, nullptr);
2171 if (in_ptr == nullptr) {
2172 ALOGE("Failed to get Java array elements");
2173 // TODO (b/20758983): Report back to Java and throw an exception
2174 return;
2175 }
2176
2177 RsAllocation *in_allocs = nullptr;
2178 if (sizeof(RsAllocation) == sizeof(jlong)) {
2179 in_allocs = (RsAllocation*)in_ptr;
2180 } else {
2181 // Convert from 64-bit jlong types to the native pointer type.
2182
2183 in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
2184 if (in_allocs == nullptr) {
2185 ALOGE("Failed launching kernel for lack of memory.");
2186 // TODO (b/20758983): Report back to Java and throw an exception
2187 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2188 return;
2189 }
2190
2191 for (int index = in_len; --index >= 0;) {
2192 in_allocs[index] = (RsAllocation)in_ptr[index];
2193 }
2194 }
2195
2196 RsScriptCall sc, *sca = nullptr;
2197 uint32_t sc_size = 0;
2198
2199 jint limit_len = 0;
2200 jint *limit_ptr = nullptr;
2201
2202 if (limits != nullptr) {
2203 limit_len = _env->GetArrayLength(limits);
2204 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
2205 if (limit_ptr == nullptr) {
2206 ALOGE("Failed to get Java array elements");
2207 // TODO (b/20758983): Report back to Java and throw an exception
2208 return;
2209 }
2210
2211 assert(limit_len == 6);
2212 UNUSED(limit_len); // As the assert might not be compiled.
2213
2214 sc.xStart = limit_ptr[0];
2215 sc.xEnd = limit_ptr[1];
2216 sc.yStart = limit_ptr[2];
2217 sc.yEnd = limit_ptr[3];
2218 sc.zStart = limit_ptr[4];
2219 sc.zEnd = limit_ptr[5];
2220 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
2221 sc.arrayStart = 0;
2222 sc.arrayEnd = 0;
2223 sc.array2Start = 0;
2224 sc.array2End = 0;
2225 sc.array3Start = 0;
2226 sc.array3End = 0;
2227 sc.array4Start = 0;
2228 sc.array4End = 0;
2229
2230 sca = &sc;
2231 sc_size = sizeof(sc);
2232 }
2233
2234 rsScriptReduceNew((RsContext)con, (RsScript)script, slot,
2235 in_allocs, in_len, (RsAllocation)aout,
2236 sca, sc_size);
2237
2238 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2239
2240 if (limits != nullptr) {
2241 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
2242 }
2243}
2244
Jason Sams22534172009-08-04 16:58:20 -07002245// -----------------------------------
2246
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002247static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002248nScriptCCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Samse4a06c52011-03-16 16:29:28 -07002249 jstring resName, jstring cacheDir,
2250 jbyteArray scriptRef, jint length)
Jason Sams22534172009-08-04 16:58:20 -07002251{
Andreas Gampe67333922014-11-10 20:35:59 -08002252 if (kLogApi) {
2253 ALOGD("nScriptCCreate, con(%p)", (RsContext)con);
2254 }
Jason Sams22534172009-08-04 16:58:20 -07002255
Jason Samse4a06c52011-03-16 16:29:28 -07002256 AutoJavaStringToUTF8 resNameUTF(_env, resName);
2257 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002258 jlong ret = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002259 jbyte* script_ptr = nullptr;
Jack Palevich43702d82009-05-28 13:38:16 -07002260 jint _exception = 0;
2261 jint remaining;
Jack Palevich43702d82009-05-28 13:38:16 -07002262 if (!scriptRef) {
2263 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002264 //jniThrowException(_env, "java/lang/IllegalArgumentException", "script == null");
Jack Palevich43702d82009-05-28 13:38:16 -07002265 goto exit;
2266 }
Jack Palevich43702d82009-05-28 13:38:16 -07002267 if (length < 0) {
2268 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002269 //jniThrowException(_env, "java/lang/IllegalArgumentException", "length < 0");
Jack Palevich43702d82009-05-28 13:38:16 -07002270 goto exit;
2271 }
Jason Samse4a06c52011-03-16 16:29:28 -07002272 remaining = _env->GetArrayLength(scriptRef);
Jack Palevich43702d82009-05-28 13:38:16 -07002273 if (remaining < length) {
2274 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002275 //jniThrowException(_env, "java/lang/IllegalArgumentException",
2276 // "length > script.length - offset");
Jack Palevich43702d82009-05-28 13:38:16 -07002277 goto exit;
2278 }
Jason Samse4a06c52011-03-16 16:29:28 -07002279 script_ptr = (jbyte *)
Jack Palevich43702d82009-05-28 13:38:16 -07002280 _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
Miao Wangba8766c2015-10-12 17:24:13 -07002281 if (script_ptr == nullptr) {
2282 ALOGE("Failed to get Java array elements");
2283 return ret;
2284 }
Jack Palevich43702d82009-05-28 13:38:16 -07002285
Tim Murrayeff663f2013-11-15 13:08:30 -08002286 //rsScriptCSetText((RsContext)con, (const char *)script_ptr, length);
Jason Samse4a06c52011-03-16 16:29:28 -07002287
Tim Murray3aa89c12014-08-18 17:51:22 -07002288 ret = (jlong)(uintptr_t)rsScriptCCreate((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07002289 resNameUTF.c_str(), resNameUTF.length(),
2290 cacheDirUTF.c_str(), cacheDirUTF.length(),
Jason Samse4a06c52011-03-16 16:29:28 -07002291 (const char *)script_ptr, length);
Jason Sams39ddc9502009-06-05 17:35:09 -07002292
Jack Palevich43702d82009-05-28 13:38:16 -07002293exit:
Jason Samse4a06c52011-03-16 16:29:28 -07002294 if (script_ptr) {
2295 _env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr,
Jack Palevich43702d82009-05-28 13:38:16 -07002296 _exception ? JNI_ABORT: 0);
2297 }
Jason Samsd19f10d2009-05-22 14:03:28 -07002298
Tim Murray3aa89c12014-08-18 17:51:22 -07002299 return (jlong)(uintptr_t)ret;
Jason Samsd19f10d2009-05-22 14:03:28 -07002300}
2301
Tim Murray460a0492013-11-19 12:45:54 -08002302static jlong
2303nScriptIntrinsicCreate(JNIEnv *_env, jobject _this, jlong con, jint id, jlong eid)
Jason Sams6ab97682012-08-10 12:09:43 -07002304{
Andreas Gampe67333922014-11-10 20:35:59 -08002305 if (kLogApi) {
2306 ALOGD("nScriptIntrinsicCreate, con(%p) id(%i) element(%p)", (RsContext)con, id,
2307 (void *)eid);
2308 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002309 return (jlong)(uintptr_t)rsScriptIntrinsicCreate((RsContext)con, id, (RsElement)eid);
Jason Sams6ab97682012-08-10 12:09:43 -07002310}
2311
Tim Murray460a0492013-11-19 12:45:54 -08002312static jlong
2313nScriptKernelIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot, jint sig)
Jason Sams08a81582012-09-18 12:32:10 -07002314{
Andreas Gampe67333922014-11-10 20:35:59 -08002315 if (kLogApi) {
2316 ALOGD("nScriptKernelIDCreate, con(%p) script(%p), slot(%i), sig(%i)", (RsContext)con,
2317 (void *)sid, slot, sig);
2318 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002319 return (jlong)(uintptr_t)rsScriptKernelIDCreate((RsContext)con, (RsScript)sid, slot, sig);
Jason Sams08a81582012-09-18 12:32:10 -07002320}
2321
Tim Murray460a0492013-11-19 12:45:54 -08002322static jlong
Yang Nibe392ad2015-01-23 17:16:02 -08002323nScriptInvokeIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
2324{
2325 if (kLogApi) {
Elliott Hughes7ff53fa2015-02-05 21:36:10 -08002326 ALOGD("nScriptInvokeIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con,
Yang Nibe392ad2015-01-23 17:16:02 -08002327 (void *)sid, slot);
2328 }
2329 return (jlong)(uintptr_t)rsScriptInvokeIDCreate((RsContext)con, (RsScript)sid, slot);
2330}
2331
2332static jlong
Tim Murray460a0492013-11-19 12:45:54 -08002333nScriptFieldIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
Jason Sams08a81582012-09-18 12:32:10 -07002334{
Andreas Gampe67333922014-11-10 20:35:59 -08002335 if (kLogApi) {
2336 ALOGD("nScriptFieldIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con, (void *)sid,
2337 slot);
2338 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002339 return (jlong)(uintptr_t)rsScriptFieldIDCreate((RsContext)con, (RsScript)sid, slot);
Jason Sams08a81582012-09-18 12:32:10 -07002340}
2341
Tim Murray460a0492013-11-19 12:45:54 -08002342static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002343nScriptGroupCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _kernels, jlongArray _src,
2344 jlongArray _dstk, jlongArray _dstf, jlongArray _types)
Jason Sams08a81582012-09-18 12:32:10 -07002345{
Andreas Gampe67333922014-11-10 20:35:59 -08002346 if (kLogApi) {
2347 ALOGD("nScriptGroupCreate, con(%p)", (RsContext)con);
2348 }
Jason Sams08a81582012-09-18 12:32:10 -07002349
Miao Wangf94e77d2016-02-11 12:32:39 -08002350 jlong id = 0;
2351
2352 RsScriptKernelID* kernelsPtr;
Ashok Bhat98071552014-02-12 09:54:43 +00002353 jint kernelsLen = _env->GetArrayLength(_kernels);
Chris Wailes488230c32014-08-14 11:22:40 -07002354 jlong *jKernelsPtr = _env->GetLongArrayElements(_kernels, nullptr);
Miao Wangf94e77d2016-02-11 12:32:39 -08002355
2356 RsScriptKernelID* srcPtr;
2357 jint srcLen = _env->GetArrayLength(_src);
2358 jlong *jSrcPtr = _env->GetLongArrayElements(_src, nullptr);
2359
2360 RsScriptKernelID* dstkPtr;
2361 jint dstkLen = _env->GetArrayLength(_dstk);
2362 jlong *jDstkPtr = _env->GetLongArrayElements(_dstk, nullptr);
2363
2364 RsScriptKernelID* dstfPtr;
2365 jint dstfLen = _env->GetArrayLength(_dstf);
2366 jlong *jDstfPtr = _env->GetLongArrayElements(_dstf, nullptr);
2367
2368 RsType* typesPtr;
2369 jint typesLen = _env->GetArrayLength(_types);
2370 jlong *jTypesPtr = _env->GetLongArrayElements(_types, nullptr);
2371
Miao Wangba8766c2015-10-12 17:24:13 -07002372 if (jKernelsPtr == nullptr) {
2373 ALOGE("Failed to get Java array elements: kernels");
Miao Wangf94e77d2016-02-11 12:32:39 -08002374 goto cleanup;
Miao Wangba8766c2015-10-12 17:24:13 -07002375 }
Miao Wangf94e77d2016-02-11 12:32:39 -08002376 if (jSrcPtr == nullptr) {
2377 ALOGE("Failed to get Java array elements: src");
2378 goto cleanup;
2379 }
2380 if (jDstkPtr == nullptr) {
2381 ALOGE("Failed to get Java array elements: dstk");
2382 goto cleanup;
2383 }
2384 if (jDstfPtr == nullptr) {
2385 ALOGE("Failed to get Java array elements: dstf");
2386 goto cleanup;
2387 }
2388 if (jTypesPtr == nullptr) {
2389 ALOGE("Failed to get Java array elements: types");
2390 goto cleanup;
2391 }
2392
2393 kernelsPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * kernelsLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002394 for(int i = 0; i < kernelsLen; ++i) {
2395 kernelsPtr[i] = (RsScriptKernelID)jKernelsPtr[i];
2396 }
Jason Sams08a81582012-09-18 12:32:10 -07002397
Miao Wangf94e77d2016-02-11 12:32:39 -08002398 srcPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * srcLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002399 for(int i = 0; i < srcLen; ++i) {
2400 srcPtr[i] = (RsScriptKernelID)jSrcPtr[i];
2401 }
Jason Sams08a81582012-09-18 12:32:10 -07002402
Miao Wangf94e77d2016-02-11 12:32:39 -08002403 dstkPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstkLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002404 for(int i = 0; i < dstkLen; ++i) {
2405 dstkPtr[i] = (RsScriptKernelID)jDstkPtr[i];
2406 }
2407
Miao Wangf94e77d2016-02-11 12:32:39 -08002408 dstfPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstfLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002409 for(int i = 0; i < dstfLen; ++i) {
2410 dstfPtr[i] = (RsScriptKernelID)jDstfPtr[i];
2411 }
2412
Miao Wangf94e77d2016-02-11 12:32:39 -08002413 typesPtr = (RsType*) malloc(sizeof(RsType) * typesLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002414 for(int i = 0; i < typesLen; ++i) {
2415 typesPtr[i] = (RsType)jTypesPtr[i];
2416 }
2417
Miao Wangf94e77d2016-02-11 12:32:39 -08002418 id = (jlong)(uintptr_t)rsScriptGroupCreate((RsContext)con,
Ashok Bhat98071552014-02-12 09:54:43 +00002419 (RsScriptKernelID *)kernelsPtr, kernelsLen * sizeof(RsScriptKernelID),
2420 (RsScriptKernelID *)srcPtr, srcLen * sizeof(RsScriptKernelID),
2421 (RsScriptKernelID *)dstkPtr, dstkLen * sizeof(RsScriptKernelID),
2422 (RsScriptFieldID *)dstfPtr, dstfLen * sizeof(RsScriptKernelID),
2423 (RsType *)typesPtr, typesLen * sizeof(RsType));
2424
2425 free(kernelsPtr);
2426 free(srcPtr);
2427 free(dstkPtr);
2428 free(dstfPtr);
2429 free(typesPtr);
Miao Wangf94e77d2016-02-11 12:32:39 -08002430
2431cleanup:
2432 if (jKernelsPtr != nullptr) {
2433 _env->ReleaseLongArrayElements(_kernels, jKernelsPtr, 0);
2434 }
2435 if (jSrcPtr != nullptr) {
2436 _env->ReleaseLongArrayElements(_src, jSrcPtr, 0);
2437 }
2438 if (jDstkPtr != nullptr) {
2439 _env->ReleaseLongArrayElements(_dstk, jDstkPtr, 0);
2440 }
2441 if (jDstfPtr != nullptr) {
2442 _env->ReleaseLongArrayElements(_dstf, jDstfPtr, 0);
2443 }
2444 if (jTypesPtr != nullptr) {
2445 _env->ReleaseLongArrayElements(_types, jTypesPtr, 0);
2446 }
2447
Jason Sams08a81582012-09-18 12:32:10 -07002448 return id;
2449}
2450
2451static void
Tim Murray460a0492013-11-19 12:45:54 -08002452nScriptGroupSetInput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002453{
Andreas Gampe67333922014-11-10 20:35:59 -08002454 if (kLogApi) {
2455 ALOGD("nScriptGroupSetInput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2456 (void *)gid, (void *)kid, (void *)alloc);
2457 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002458 rsScriptGroupSetInput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002459}
2460
2461static void
Tim Murray460a0492013-11-19 12:45:54 -08002462nScriptGroupSetOutput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002463{
Andreas Gampe67333922014-11-10 20:35:59 -08002464 if (kLogApi) {
2465 ALOGD("nScriptGroupSetOutput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2466 (void *)gid, (void *)kid, (void *)alloc);
2467 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002468 rsScriptGroupSetOutput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002469}
2470
2471static void
Tim Murray460a0492013-11-19 12:45:54 -08002472nScriptGroupExecute(JNIEnv *_env, jobject _this, jlong con, jlong gid)
Jason Sams08a81582012-09-18 12:32:10 -07002473{
Andreas Gampe67333922014-11-10 20:35:59 -08002474 if (kLogApi) {
2475 ALOGD("nScriptGroupSetOutput, con(%p) group(%p)", (RsContext)con, (void *)gid);
2476 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002477 rsScriptGroupExecute((RsContext)con, (RsScriptGroup)gid);
Jason Sams08a81582012-09-18 12:32:10 -07002478}
2479
Jason Samsd19f10d2009-05-22 14:03:28 -07002480// ---------------------------------------------------------------------------
2481
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002482static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002483nProgramStoreCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Sams331bf9b2011-04-06 11:23:54 -07002484 jboolean colorMaskR, jboolean colorMaskG, jboolean colorMaskB, jboolean colorMaskA,
2485 jboolean depthMask, jboolean ditherEnable,
2486 jint srcFunc, jint destFunc,
2487 jint depthFunc)
Jason Samsd19f10d2009-05-22 14:03:28 -07002488{
Andreas Gampe67333922014-11-10 20:35:59 -08002489 if (kLogApi) {
2490 ALOGD("nProgramStoreCreate, con(%p)", (RsContext)con);
2491 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002492 return (jlong)(uintptr_t)rsProgramStoreCreate((RsContext)con, colorMaskR, colorMaskG, colorMaskB, colorMaskA,
Jason Sams331bf9b2011-04-06 11:23:54 -07002493 depthMask, ditherEnable, (RsBlendSrcFunc)srcFunc,
2494 (RsBlendDstFunc)destFunc, (RsDepthFunc)depthFunc);
Jason Samsd19f10d2009-05-22 14:03:28 -07002495}
2496
Jason Sams0011bcf2009-12-15 12:58:36 -08002497// ---------------------------------------------------------------------------
2498
2499static void
Tim Murray460a0492013-11-19 12:45:54 -08002500nProgramBindConstants(JNIEnv *_env, jobject _this, jlong con, jlong vpv, jint slot, jlong a)
Jason Sams0011bcf2009-12-15 12:58:36 -08002501{
Andreas Gampe67333922014-11-10 20:35:59 -08002502 if (kLogApi) {
2503 ALOGD("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", (RsContext)con,
2504 (RsProgramVertex)vpv, slot, (RsAllocation)a);
2505 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002506 rsProgramBindConstants((RsContext)con, (RsProgram)vpv, slot, (RsAllocation)a);
Jason Sams0011bcf2009-12-15 12:58:36 -08002507}
Jason Sams54c0ec12009-11-30 14:49:55 -08002508
Jason Sams68afd012009-12-17 16:55:08 -08002509static void
Tim Murray460a0492013-11-19 12:45:54 -08002510nProgramBindTexture(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002511{
Andreas Gampe67333922014-11-10 20:35:59 -08002512 if (kLogApi) {
2513 ALOGD("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2514 (RsProgramFragment)vpf, slot, (RsAllocation)a);
2515 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002516 rsProgramBindTexture((RsContext)con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
Jason Sams68afd012009-12-17 16:55:08 -08002517}
2518
2519static void
Tim Murray460a0492013-11-19 12:45:54 -08002520nProgramBindSampler(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002521{
Andreas Gampe67333922014-11-10 20:35:59 -08002522 if (kLogApi) {
2523 ALOGD("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2524 (RsProgramFragment)vpf, slot, (RsSampler)a);
2525 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002526 rsProgramBindSampler((RsContext)con, (RsProgramFragment)vpf, slot, (RsSampler)a);
Jason Sams68afd012009-12-17 16:55:08 -08002527}
2528
Jason Samsd19f10d2009-05-22 14:03:28 -07002529// ---------------------------------------------------------------------------
2530
Tim Murray460a0492013-11-19 12:45:54 -08002531static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002532nProgramFragmentCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002533 jobjectArray texNames, jlongArray params)
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002534{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002535 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002536 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002537 jint paramLen = _env->GetArrayLength(params);
Miao Wangba8766c2015-10-12 17:24:13 -07002538 if (jParamPtr == nullptr) {
2539 ALOGE("Failed to get Java array elements");
2540 return 0;
2541 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002542
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002543 int texCount = _env->GetArrayLength(texNames);
2544 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2545 const char ** nameArray = names.c_str();
2546 size_t* sizeArray = names.c_str_len();
2547
Andreas Gampe67333922014-11-10 20:35:59 -08002548 if (kLogApi) {
2549 ALOGD("nProgramFragmentCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2550 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002551
Ashok Bhat98071552014-02-12 09:54:43 +00002552 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2553 for(int i = 0; i < paramLen; ++i) {
2554 paramPtr[i] = (uintptr_t)jParamPtr[i];
2555 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002556 jlong ret = (jlong)(uintptr_t)rsProgramFragmentCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002557 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002558 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002559
Ashok Bhat98071552014-02-12 09:54:43 +00002560 free(paramPtr);
2561 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002562 return ret;
2563}
2564
2565
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002566// ---------------------------------------------------------------------------
2567
Tim Murray460a0492013-11-19 12:45:54 -08002568static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002569nProgramVertexCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002570 jobjectArray texNames, jlongArray params)
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002571{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002572 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002573 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams0011bcf2009-12-15 12:58:36 -08002574 jint paramLen = _env->GetArrayLength(params);
Miao Wangba8766c2015-10-12 17:24:13 -07002575 if (jParamPtr == nullptr) {
2576 ALOGE("Failed to get Java array elements");
2577 return 0;
2578 }
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002579
Andreas Gampe67333922014-11-10 20:35:59 -08002580 if (kLogApi) {
2581 ALOGD("nProgramVertexCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2582 }
Jason Sams0011bcf2009-12-15 12:58:36 -08002583
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002584 int texCount = _env->GetArrayLength(texNames);
2585 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2586 const char ** nameArray = names.c_str();
2587 size_t* sizeArray = names.c_str_len();
2588
Ashok Bhat98071552014-02-12 09:54:43 +00002589 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2590 for(int i = 0; i < paramLen; ++i) {
2591 paramPtr[i] = (uintptr_t)jParamPtr[i];
2592 }
2593
Tim Murray3aa89c12014-08-18 17:51:22 -07002594 jlong ret = (jlong)(uintptr_t)rsProgramVertexCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002595 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002596 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002597
Ashok Bhat98071552014-02-12 09:54:43 +00002598 free(paramPtr);
2599 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams0011bcf2009-12-15 12:58:36 -08002600 return ret;
2601}
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002602
Jason Samsebfb4362009-09-23 13:57:02 -07002603// ---------------------------------------------------------------------------
2604
Tim Murray460a0492013-11-19 12:45:54 -08002605static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002606nProgramRasterCreate(JNIEnv *_env, jobject _this, jlong con, jboolean pointSprite, jint cull)
Jason Samsebfb4362009-09-23 13:57:02 -07002607{
Andreas Gampe67333922014-11-10 20:35:59 -08002608 if (kLogApi) {
2609 ALOGD("nProgramRasterCreate, con(%p), pointSprite(%i), cull(%i)", (RsContext)con,
2610 pointSprite, cull);
2611 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002612 return (jlong)(uintptr_t)rsProgramRasterCreate((RsContext)con, pointSprite, (RsCullMode)cull);
Jason Samsebfb4362009-09-23 13:57:02 -07002613}
2614
Jason Samsd19f10d2009-05-22 14:03:28 -07002615
2616// ---------------------------------------------------------------------------
2617
2618static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002619nContextBindRootScript(JNIEnv *_env, jobject _this, jlong con, jlong script)
Jason Samsd19f10d2009-05-22 14:03:28 -07002620{
Andreas Gampe67333922014-11-10 20:35:59 -08002621 if (kLogApi) {
2622 ALOGD("nContextBindRootScript, con(%p), script(%p)", (RsContext)con, (RsScript)script);
2623 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002624 rsContextBindRootScript((RsContext)con, (RsScript)script);
Jason Samsd19f10d2009-05-22 14:03:28 -07002625}
2626
2627static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002628nContextBindProgramStore(JNIEnv *_env, jobject _this, jlong con, jlong pfs)
Jason Samsd19f10d2009-05-22 14:03:28 -07002629{
Andreas Gampe67333922014-11-10 20:35:59 -08002630 if (kLogApi) {
2631 ALOGD("nContextBindProgramStore, con(%p), pfs(%p)", (RsContext)con, (RsProgramStore)pfs);
2632 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002633 rsContextBindProgramStore((RsContext)con, (RsProgramStore)pfs);
Jason Samsd19f10d2009-05-22 14:03:28 -07002634}
2635
2636static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002637nContextBindProgramFragment(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsd19f10d2009-05-22 14:03:28 -07002638{
Andreas Gampe67333922014-11-10 20:35:59 -08002639 if (kLogApi) {
2640 ALOGD("nContextBindProgramFragment, con(%p), pf(%p)", (RsContext)con,
2641 (RsProgramFragment)pf);
2642 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002643 rsContextBindProgramFragment((RsContext)con, (RsProgramFragment)pf);
Jason Samsd19f10d2009-05-22 14:03:28 -07002644}
2645
Jason Sams0826a6f2009-06-15 19:04:56 -07002646static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002647nContextBindProgramVertex(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Sams0826a6f2009-06-15 19:04:56 -07002648{
Andreas Gampe67333922014-11-10 20:35:59 -08002649 if (kLogApi) {
2650 ALOGD("nContextBindProgramVertex, con(%p), pf(%p)", (RsContext)con, (RsProgramVertex)pf);
2651 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002652 rsContextBindProgramVertex((RsContext)con, (RsProgramVertex)pf);
Jason Sams0826a6f2009-06-15 19:04:56 -07002653}
2654
Joe Onoratod7b37742009-08-09 22:57:44 -07002655static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002656nContextBindProgramRaster(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsebfb4362009-09-23 13:57:02 -07002657{
Andreas Gampe67333922014-11-10 20:35:59 -08002658 if (kLogApi) {
2659 ALOGD("nContextBindProgramRaster, con(%p), pf(%p)", (RsContext)con, (RsProgramRaster)pf);
2660 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002661 rsContextBindProgramRaster((RsContext)con, (RsProgramRaster)pf);
Jason Samsebfb4362009-09-23 13:57:02 -07002662}
2663
Joe Onoratod7b37742009-08-09 22:57:44 -07002664
Jason Sams02fb2cb2009-05-28 15:37:57 -07002665// ---------------------------------------------------------------------------
2666
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002667static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002668nSamplerCreate(JNIEnv *_env, jobject _this, jlong con, jint magFilter, jint minFilter,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002669 jint wrapS, jint wrapT, jint wrapR, jfloat aniso)
Jason Sams02fb2cb2009-05-28 15:37:57 -07002670{
Andreas Gampe67333922014-11-10 20:35:59 -08002671 if (kLogApi) {
2672 ALOGD("nSamplerCreate, con(%p)", (RsContext)con);
2673 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002674 return (jlong)(uintptr_t)rsSamplerCreate((RsContext)con,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002675 (RsSamplerValue)magFilter,
2676 (RsSamplerValue)minFilter,
2677 (RsSamplerValue)wrapS,
2678 (RsSamplerValue)wrapT,
2679 (RsSamplerValue)wrapR,
2680 aniso);
Jason Sams02fb2cb2009-05-28 15:37:57 -07002681}
2682
Jason Samsbba134c2009-06-22 15:49:21 -07002683// ---------------------------------------------------------------------------
2684
Tim Murray460a0492013-11-19 12:45:54 -08002685static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002686nMeshCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _vtx, jlongArray _idx, jintArray _prim)
Jason Samsbba134c2009-06-22 15:49:21 -07002687{
Andreas Gampe67333922014-11-10 20:35:59 -08002688 if (kLogApi) {
2689 ALOGD("nMeshCreate, con(%p)", (RsContext)con);
2690 }
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002691
Miao Wangf94e77d2016-02-11 12:32:39 -08002692 jlong id = 0;
2693
2694 RsAllocation* vtxPtr;
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002695 jint vtxLen = _env->GetArrayLength(_vtx);
Chris Wailes488230c32014-08-14 11:22:40 -07002696 jlong *jVtxPtr = _env->GetLongArrayElements(_vtx, nullptr);
Miao Wangf94e77d2016-02-11 12:32:39 -08002697
2698 RsAllocation* idxPtr;
2699 jint idxLen = _env->GetArrayLength(_idx);
2700 jlong *jIdxPtr = _env->GetLongArrayElements(_idx, nullptr);
2701
2702 jint primLen = _env->GetArrayLength(_prim);
2703 jint *primPtr = _env->GetIntArrayElements(_prim, nullptr);
2704
Miao Wangba8766c2015-10-12 17:24:13 -07002705 if (jVtxPtr == nullptr) {
2706 ALOGE("Failed to get Java array elements: vtx");
Miao Wangf94e77d2016-02-11 12:32:39 -08002707 goto cleanupMesh;
Miao Wangba8766c2015-10-12 17:24:13 -07002708 }
Miao Wangf94e77d2016-02-11 12:32:39 -08002709 if (jIdxPtr == nullptr) {
2710 ALOGE("Failed to get Java array elements: idx");
2711 goto cleanupMesh;
2712 }
2713 if (primPtr == nullptr) {
2714 ALOGE("Failed to get Java array elements: prim");
2715 goto cleanupMesh;
2716 }
2717
2718 vtxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * vtxLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002719 for(int i = 0; i < vtxLen; ++i) {
2720 vtxPtr[i] = (RsAllocation)(uintptr_t)jVtxPtr[i];
2721 }
2722
Miao Wangf94e77d2016-02-11 12:32:39 -08002723 idxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * idxLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002724 for(int i = 0; i < idxLen; ++i) {
2725 idxPtr[i] = (RsAllocation)(uintptr_t)jIdxPtr[i];
2726 }
2727
Miao Wangf94e77d2016-02-11 12:32:39 -08002728 id = (jlong)(uintptr_t)rsMeshCreate((RsContext)con,
2729 (RsAllocation *)vtxPtr, vtxLen,
2730 (RsAllocation *)idxPtr, idxLen,
2731 (uint32_t *)primPtr, primLen);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002732
Ashok Bhat98071552014-02-12 09:54:43 +00002733 free(vtxPtr);
2734 free(idxPtr);
Miao Wangf94e77d2016-02-11 12:32:39 -08002735
2736cleanupMesh:
2737 if (jVtxPtr != nullptr) {
2738 _env->ReleaseLongArrayElements(_vtx, jVtxPtr, 0);
2739 }
2740 if (jIdxPtr != nullptr) {
2741 _env->ReleaseLongArrayElements(_idx, jIdxPtr, 0);
2742 }
2743 if (primPtr != nullptr) {
2744 _env->ReleaseIntArrayElements(_prim, primPtr, 0);
2745 }
2746
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002747 return id;
2748}
2749
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002750static jint
Tim Murray460a0492013-11-19 12:45:54 -08002751nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002752{
Andreas Gampe67333922014-11-10 20:35:59 -08002753 if (kLogApi) {
2754 ALOGD("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2755 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002756 jint vtxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002757 rsaMeshGetVertexBufferCount((RsContext)con, (RsMesh)mesh, &vtxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002758 return vtxCount;
2759}
2760
2761static jint
Tim Murray460a0492013-11-19 12:45:54 -08002762nMeshGetIndexCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002763{
Andreas Gampe67333922014-11-10 20:35:59 -08002764 if (kLogApi) {
2765 ALOGD("nMeshGetIndexCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2766 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002767 jint idxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002768 rsaMeshGetIndexCount((RsContext)con, (RsMesh)mesh, &idxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002769 return idxCount;
2770}
2771
2772static void
Ashok Bhat98071552014-02-12 09:54:43 +00002773nMeshGetVertices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _ids, jint numVtxIDs)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002774{
Andreas Gampe67333922014-11-10 20:35:59 -08002775 if (kLogApi) {
2776 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2777 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002778
2779 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
Tim Murrayeff663f2013-11-15 13:08:30 -08002780 rsaMeshGetVertices((RsContext)con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002781
2782 for(jint i = 0; i < numVtxIDs; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002783 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002784 _env->SetLongArrayRegion(_ids, i, 1, &alloc);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002785 }
2786
2787 free(allocs);
2788}
2789
2790static void
Ashok Bhat98071552014-02-12 09:54:43 +00002791nMeshGetIndices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _idxIds, jintArray _primitives, jint numIndices)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002792{
Andreas Gampe67333922014-11-10 20:35:59 -08002793 if (kLogApi) {
2794 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2795 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002796
2797 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
2798 uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
2799
Tim Murrayeff663f2013-11-15 13:08:30 -08002800 rsaMeshGetIndices((RsContext)con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002801
2802 for(jint i = 0; i < numIndices; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002803 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002804 const jint prim = (jint)prims[i];
2805 _env->SetLongArrayRegion(_idxIds, i, 1, &alloc);
2806 _env->SetIntArrayRegion(_primitives, i, 1, &prim);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002807 }
2808
2809 free(allocs);
2810 free(prims);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002811}
2812
Tim Murray56f9e6f2014-05-16 11:47:26 -07002813static jint
2814nSystemGetPointerSize(JNIEnv *_env, jobject _this) {
2815 return (jint)sizeof(void*);
2816}
2817
Miao Wang0facf022015-11-25 11:21:13 -08002818static jobject
2819nAllocationGetByteBuffer(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
2820 jlongArray strideArr, jint xBytesSize,
2821 jint dimY, jint dimZ) {
2822 if (kLogApi) {
2823 ALOGD("nAllocationGetByteBuffer, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
2824 }
Tim Murray56f9e6f2014-05-16 11:47:26 -07002825
Miao Wang0facf022015-11-25 11:21:13 -08002826 jlong *jStridePtr = _env->GetLongArrayElements(strideArr, nullptr);
2827 if (jStridePtr == nullptr) {
2828 ALOGE("Failed to get Java array elements: strideArr");
2829 return 0;
2830 }
2831
2832 size_t strideIn = xBytesSize;
2833 void* ptr = nullptr;
2834 if (alloc != 0) {
2835 ptr = rsAllocationGetPointer((RsContext)con, (RsAllocation)alloc, 0,
2836 RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, 0, 0,
2837 &strideIn, sizeof(size_t));
2838 }
2839
2840 jobject byteBuffer = nullptr;
2841 if (ptr != nullptr) {
2842 size_t bufferSize = strideIn;
2843 jStridePtr[0] = strideIn;
2844 if (dimY > 0) {
2845 bufferSize *= dimY;
2846 }
2847 if (dimZ > 0) {
2848 bufferSize *= dimZ;
2849 }
2850 byteBuffer = _env->NewDirectByteBuffer(ptr, (jlong) bufferSize);
2851 }
2852 _env->ReleaseLongArrayElements(strideArr, jStridePtr, 0);
2853 return byteBuffer;
2854}
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002855// ---------------------------------------------------------------------------
2856
Jason Samsd19f10d2009-05-22 14:03:28 -07002857
Jason Sams94d8e90a2009-06-10 16:09:05 -07002858static const char *classPathName = "android/renderscript/RenderScript";
Jason Samsd19f10d2009-05-22 14:03:28 -07002859
Daniel Micay76f6a862015-09-19 17:31:01 -04002860static const JNINativeMethod methods[] = {
Jason Sams1c415172010-11-08 17:06:46 -08002861{"_nInit", "()V", (void*)_nInit },
Jason Samsea84a7c2009-09-04 14:42:41 -07002862
Tim Murrayeff663f2013-11-15 13:08:30 -08002863{"nDeviceCreate", "()J", (void*)nDeviceCreate },
2864{"nDeviceDestroy", "(J)V", (void*)nDeviceDestroy },
2865{"nDeviceSetConfig", "(JII)V", (void*)nDeviceSetConfig },
2866{"nContextGetUserMessage", "(J[I)I", (void*)nContextGetUserMessage },
2867{"nContextGetErrorMessage", "(J)Ljava/lang/String;", (void*)nContextGetErrorMessage },
2868{"nContextPeekMessage", "(J[I)I", (void*)nContextPeekMessage },
Jason Sams1c415172010-11-08 17:06:46 -08002869
Tim Murrayeff663f2013-11-15 13:08:30 -08002870{"nContextInitToClient", "(J)V", (void*)nContextInitToClient },
2871{"nContextDeinitToClient", "(J)V", (void*)nContextDeinitToClient },
Jason Samsd19f10d2009-05-22 14:03:28 -07002872
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002873
Jason Sams2e1872f2010-08-17 16:25:41 -07002874// All methods below are thread protected in java.
Tim Murrayeff663f2013-11-15 13:08:30 -08002875{"rsnContextCreate", "(JIII)J", (void*)nContextCreate },
2876{"rsnContextCreateGL", "(JIIIIIIIIIIIIFI)J", (void*)nContextCreateGL },
2877{"rsnContextFinish", "(J)V", (void*)nContextFinish },
2878{"rsnContextSetPriority", "(JI)V", (void*)nContextSetPriority },
Tim Murray47f31582015-04-07 15:43:24 -07002879{"rsnContextSetCacheDir", "(JLjava/lang/String;)V", (void*)nContextSetCacheDir },
Tim Murrayeff663f2013-11-15 13:08:30 -08002880{"rsnContextSetSurface", "(JIILandroid/view/Surface;)V", (void*)nContextSetSurface },
2881{"rsnContextDestroy", "(J)V", (void*)nContextDestroy },
2882{"rsnContextDump", "(JI)V", (void*)nContextDump },
2883{"rsnContextPause", "(J)V", (void*)nContextPause },
2884{"rsnContextResume", "(J)V", (void*)nContextResume },
2885{"rsnContextSendMessage", "(JI[I)V", (void*)nContextSendMessage },
Yang Ni281c3252014-10-24 08:52:24 -07002886{"rsnClosureCreate", "(JJJ[J[J[I[J[J)J", (void*)nClosureCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002887{"rsnInvokeClosureCreate", "(JJ[B[J[J[I)J", (void*)nInvokeClosureCreate },
Yang Ni281c3252014-10-24 08:52:24 -07002888{"rsnClosureSetArg", "(JJIJI)V", (void*)nClosureSetArg },
2889{"rsnClosureSetGlobal", "(JJJJI)V", (void*)nClosureSetGlobal },
Tim Murray460a0492013-11-19 12:45:54 -08002890{"rsnAssignName", "(JJ[B)V", (void*)nAssignName },
2891{"rsnGetName", "(JJ)Ljava/lang/String;", (void*)nGetName },
2892{"rsnObjDestroy", "(JJ)V", (void*)nObjDestroy },
Jason Sams64676f32009-07-08 18:01:53 -07002893
Tim Murray460a0492013-11-19 12:45:54 -08002894{"rsnFileA3DCreateFromFile", "(JLjava/lang/String;)J", (void*)nFileA3DCreateFromFile },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002895{"rsnFileA3DCreateFromAssetStream", "(JJ)J", (void*)nFileA3DCreateFromAssetStream },
Tim Murray460a0492013-11-19 12:45:54 -08002896{"rsnFileA3DCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;)J", (void*)nFileA3DCreateFromAsset },
2897{"rsnFileA3DGetNumIndexEntries", "(JJ)I", (void*)nFileA3DGetNumIndexEntries },
2898{"rsnFileA3DGetIndexEntries", "(JJI[I[Ljava/lang/String;)V", (void*)nFileA3DGetIndexEntries },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002899{"rsnFileA3DGetEntryByIndex", "(JJI)J", (void*)nFileA3DGetEntryByIndex },
Jason Samsd19f10d2009-05-22 14:03:28 -07002900
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002901{"rsnFontCreateFromFile", "(JLjava/lang/String;FI)J", (void*)nFontCreateFromFile },
2902{"rsnFontCreateFromAssetStream", "(JLjava/lang/String;FIJ)J", (void*)nFontCreateFromAssetStream },
2903{"rsnFontCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;FI)J", (void*)nFontCreateFromAsset },
Jason Samsd19f10d2009-05-22 14:03:28 -07002904
Tim Murray460a0492013-11-19 12:45:54 -08002905{"rsnElementCreate", "(JJIZI)J", (void*)nElementCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002906{"rsnElementCreate2", "(J[J[Ljava/lang/String;[I)J", (void*)nElementCreate2 },
Tim Murray460a0492013-11-19 12:45:54 -08002907{"rsnElementGetNativeData", "(JJ[I)V", (void*)nElementGetNativeData },
Ashok Bhat98071552014-02-12 09:54:43 +00002908{"rsnElementGetSubElements", "(JJ[J[Ljava/lang/String;[I)V", (void*)nElementGetSubElements },
Jason Samsd19f10d2009-05-22 14:03:28 -07002909
Tim Murray460a0492013-11-19 12:45:54 -08002910{"rsnTypeCreate", "(JJIIIZZI)J", (void*)nTypeCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002911{"rsnTypeGetNativeData", "(JJ[J)V", (void*)nTypeGetNativeData },
Jason Samsd19f10d2009-05-22 14:03:28 -07002912
Ashok Bhat98071552014-02-12 09:54:43 +00002913{"rsnAllocationCreateTyped", "(JJIIJ)J", (void*)nAllocationCreateTyped },
Tim Murray460a0492013-11-19 12:45:54 -08002914{"rsnAllocationCreateFromBitmap", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateFromBitmap },
2915{"rsnAllocationCreateBitmapBackedAllocation", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateBitmapBackedAllocation },
2916{"rsnAllocationCubeCreateFromBitmap","(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCubeCreateFromBitmap },
Jason Sams5476b452010-12-08 16:14:36 -08002917
Tim Murray460a0492013-11-19 12:45:54 -08002918{"rsnAllocationCopyFromBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyFromBitmap },
2919{"rsnAllocationCopyToBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyToBitmap },
Jason Sams4ef66502010-12-10 16:03:15 -08002920
Tim Murray460a0492013-11-19 12:45:54 -08002921{"rsnAllocationSyncAll", "(JJI)V", (void*)nAllocationSyncAll },
Miao Wang8c150922015-10-26 17:44:10 -07002922{"rsnAllocationSetupBufferQueue", "(JJI)V", (void*)nAllocationSetupBufferQueue },
2923{"rsnAllocationShareBufferQueue", "(JJJ)V", (void*)nAllocationShareBufferQueue },
Tim Murray460a0492013-11-19 12:45:54 -08002924{"rsnAllocationGetSurface", "(JJ)Landroid/view/Surface;", (void*)nAllocationGetSurface },
2925{"rsnAllocationSetSurface", "(JJLandroid/view/Surface;)V", (void*)nAllocationSetSurface },
2926{"rsnAllocationIoSend", "(JJ)V", (void*)nAllocationIoSend },
Miao Wang8c150922015-10-26 17:44:10 -07002927{"rsnAllocationIoReceive", "(JJ)J", (void*)nAllocationIoReceive },
Miao Wang87e908d2015-03-02 15:15:15 -08002928{"rsnAllocationData1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData1D },
Miao Wangc8e237e2015-02-20 18:36:32 -08002929{"rsnAllocationElementData", "(JJIIIII[BI)V", (void*)nAllocationElementData },
Miao Wang87e908d2015-03-02 15:15:15 -08002930{"rsnAllocationData2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData2D },
Tim Murray460a0492013-11-19 12:45:54 -08002931{"rsnAllocationData2D", "(JJIIIIIIJIIII)V", (void*)nAllocationData2D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002932{"rsnAllocationData3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData3D },
Tim Murray460a0492013-11-19 12:45:54 -08002933{"rsnAllocationData3D", "(JJIIIIIIIJIIII)V", (void*)nAllocationData3D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002934{"rsnAllocationRead", "(JJLjava/lang/Object;IIZ)V", (void*)nAllocationRead },
2935{"rsnAllocationRead1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead1D },
Miao Wang45cec0a2015-03-04 16:40:21 -08002936{"rsnAllocationElementRead", "(JJIIIII[BI)V", (void*)nAllocationElementRead },
Miao Wang87e908d2015-03-02 15:15:15 -08002937{"rsnAllocationRead2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead2D },
2938{"rsnAllocationRead3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead3D },
Tim Murray460a0492013-11-19 12:45:54 -08002939{"rsnAllocationGetType", "(JJ)J", (void*)nAllocationGetType},
2940{"rsnAllocationResize1D", "(JJI)V", (void*)nAllocationResize1D },
2941{"rsnAllocationGenerateMipmaps", "(JJ)V", (void*)nAllocationGenerateMipmaps },
Jason Samsbd1c3ad2009-08-03 16:03:08 -07002942
Jason Sams46ba27e32015-02-06 17:45:15 -08002943{"rsnAllocationAdapterCreate", "(JJJ)J", (void*)nAllocationAdapterCreate },
2944{"rsnAllocationAdapterOffset", "(JJIIIIIIIII)V", (void*)nAllocationAdapterOffset },
2945
Tim Murray460a0492013-11-19 12:45:54 -08002946{"rsnScriptBindAllocation", "(JJJI)V", (void*)nScriptBindAllocation },
2947{"rsnScriptSetTimeZone", "(JJ[B)V", (void*)nScriptSetTimeZone },
2948{"rsnScriptInvoke", "(JJI)V", (void*)nScriptInvoke },
2949{"rsnScriptInvokeV", "(JJI[B)V", (void*)nScriptInvokeV },
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002950
2951{"rsnScriptForEach", "(JJI[JJ[B[I)V", (void*)nScriptForEach },
Matt Wala36eb1f72015-07-20 15:35:27 -07002952{"rsnScriptReduce", "(JJIJJ[I)V", (void*)nScriptReduce },
David Gross26ef7a732016-01-12 12:19:15 -08002953{"rsnScriptReduceNew", "(JJI[JJ[I)V", (void*)nScriptReduceNew },
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002954
Tim Murray460a0492013-11-19 12:45:54 -08002955{"rsnScriptSetVarI", "(JJII)V", (void*)nScriptSetVarI },
2956{"rsnScriptGetVarI", "(JJI)I", (void*)nScriptGetVarI },
2957{"rsnScriptSetVarJ", "(JJIJ)V", (void*)nScriptSetVarJ },
2958{"rsnScriptGetVarJ", "(JJI)J", (void*)nScriptGetVarJ },
2959{"rsnScriptSetVarF", "(JJIF)V", (void*)nScriptSetVarF },
2960{"rsnScriptGetVarF", "(JJI)F", (void*)nScriptGetVarF },
2961{"rsnScriptSetVarD", "(JJID)V", (void*)nScriptSetVarD },
2962{"rsnScriptGetVarD", "(JJI)D", (void*)nScriptGetVarD },
2963{"rsnScriptSetVarV", "(JJI[B)V", (void*)nScriptSetVarV },
2964{"rsnScriptGetVarV", "(JJI[B)V", (void*)nScriptGetVarV },
2965{"rsnScriptSetVarVE", "(JJI[BJ[I)V", (void*)nScriptSetVarVE },
2966{"rsnScriptSetVarObj", "(JJIJ)V", (void*)nScriptSetVarObj },
Jason Samsd19f10d2009-05-22 14:03:28 -07002967
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002968{"rsnScriptCCreate", "(JLjava/lang/String;Ljava/lang/String;[BI)J", (void*)nScriptCCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002969{"rsnScriptIntrinsicCreate", "(JIJ)J", (void*)nScriptIntrinsicCreate },
2970{"rsnScriptKernelIDCreate", "(JJII)J", (void*)nScriptKernelIDCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002971{"rsnScriptInvokeIDCreate", "(JJI)J", (void*)nScriptInvokeIDCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002972{"rsnScriptFieldIDCreate", "(JJI)J", (void*)nScriptFieldIDCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002973{"rsnScriptGroupCreate", "(J[J[J[J[J[J)J", (void*)nScriptGroupCreate },
Yang Ni35be56c2015-04-02 17:47:56 -07002974{"rsnScriptGroup2Create", "(JLjava/lang/String;Ljava/lang/String;[J)J", (void*)nScriptGroup2Create },
Tim Murray460a0492013-11-19 12:45:54 -08002975{"rsnScriptGroupSetInput", "(JJJJ)V", (void*)nScriptGroupSetInput },
2976{"rsnScriptGroupSetOutput", "(JJJJ)V", (void*)nScriptGroupSetOutput },
2977{"rsnScriptGroupExecute", "(JJ)V", (void*)nScriptGroupExecute },
Yang Ni281c3252014-10-24 08:52:24 -07002978{"rsnScriptGroup2Execute", "(JJ)V", (void*)nScriptGroup2Execute },
Jason Sams0011bcf2009-12-15 12:58:36 -08002979
Tim Murray25207df2015-01-12 16:47:56 -08002980{"rsnScriptIntrinsicBLAS_Single", "(JJIIIIIIIIIFJJFJIIII)V", (void*)nScriptIntrinsicBLAS_Single },
2981{"rsnScriptIntrinsicBLAS_Double", "(JJIIIIIIIIIDJJDJIIII)V", (void*)nScriptIntrinsicBLAS_Double },
2982{"rsnScriptIntrinsicBLAS_Complex", "(JJIIIIIIIIIFFJJFFJIIII)V", (void*)nScriptIntrinsicBLAS_Complex },
2983{"rsnScriptIntrinsicBLAS_Z", "(JJIIIIIIIIIDDJJDDJIIII)V", (void*)nScriptIntrinsicBLAS_Z },
2984
Tim Murray9cb16a22015-04-01 11:07:16 -07002985{"rsnScriptIntrinsicBLAS_BNNM", "(JJIIIJIJIJII)V", (void*)nScriptIntrinsicBLAS_BNNM },
2986
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002987{"rsnProgramStoreCreate", "(JZZZZZZIII)J", (void*)nProgramStoreCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002988
Tim Murray460a0492013-11-19 12:45:54 -08002989{"rsnProgramBindConstants", "(JJIJ)V", (void*)nProgramBindConstants },
2990{"rsnProgramBindTexture", "(JJIJ)V", (void*)nProgramBindTexture },
2991{"rsnProgramBindSampler", "(JJIJ)V", (void*)nProgramBindSampler },
Jason Samsebfb4362009-09-23 13:57:02 -07002992
Ashok Bhat98071552014-02-12 09:54:43 +00002993{"rsnProgramFragmentCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramFragmentCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002994{"rsnProgramRasterCreate", "(JZI)J", (void*)nProgramRasterCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002995{"rsnProgramVertexCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramVertexCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002996
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002997{"rsnContextBindRootScript", "(JJ)V", (void*)nContextBindRootScript },
2998{"rsnContextBindProgramStore", "(JJ)V", (void*)nContextBindProgramStore },
2999{"rsnContextBindProgramFragment", "(JJ)V", (void*)nContextBindProgramFragment },
3000{"rsnContextBindProgramVertex", "(JJ)V", (void*)nContextBindProgramVertex },
3001{"rsnContextBindProgramRaster", "(JJ)V", (void*)nContextBindProgramRaster },
Jason Sams02fb2cb2009-05-28 15:37:57 -07003002
Ashok Bhat0e0c0882014-02-04 14:57:58 +00003003{"rsnSamplerCreate", "(JIIIIIF)J", (void*)nSamplerCreate },
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07003004
Ashok Bhat98071552014-02-12 09:54:43 +00003005{"rsnMeshCreate", "(J[J[J[I)J", (void*)nMeshCreate },
Jason Sams2e1872f2010-08-17 16:25:41 -07003006
Tim Murray460a0492013-11-19 12:45:54 -08003007{"rsnMeshGetVertexBufferCount", "(JJ)I", (void*)nMeshGetVertexBufferCount },
3008{"rsnMeshGetIndexCount", "(JJ)I", (void*)nMeshGetIndexCount },
Ashok Bhat98071552014-02-12 09:54:43 +00003009{"rsnMeshGetVertices", "(JJ[JI)V", (void*)nMeshGetVertices },
3010{"rsnMeshGetIndices", "(JJ[J[II)V", (void*)nMeshGetIndices },
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07003011
Tim Murray56f9e6f2014-05-16 11:47:26 -07003012{"rsnSystemGetPointerSize", "()I", (void*)nSystemGetPointerSize },
Miao Wang0facf022015-11-25 11:21:13 -08003013{"rsnAllocationGetByteBuffer", "(JJ[JIII)Ljava/nio/ByteBuffer;", (void*)nAllocationGetByteBuffer },
Jason Samsd19f10d2009-05-22 14:03:28 -07003014};
3015
3016static int registerFuncs(JNIEnv *_env)
3017{
3018 return android::AndroidRuntime::registerNativeMethods(
3019 _env, classPathName, methods, NELEM(methods));
3020}
3021
3022// ---------------------------------------------------------------------------
3023
3024jint JNI_OnLoad(JavaVM* vm, void* reserved)
3025{
Chris Wailes488230c32014-08-14 11:22:40 -07003026 JNIEnv* env = nullptr;
Jason Samsd19f10d2009-05-22 14:03:28 -07003027 jint result = -1;
3028
Jason Samsd19f10d2009-05-22 14:03:28 -07003029 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
Steve Block3762c312012-01-06 19:20:56 +00003030 ALOGE("ERROR: GetEnv failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07003031 goto bail;
3032 }
Chris Wailes488230c32014-08-14 11:22:40 -07003033 assert(env != nullptr);
Jason Samsd19f10d2009-05-22 14:03:28 -07003034
3035 if (registerFuncs(env) < 0) {
Ashok Bhat0e0c0882014-02-04 14:57:58 +00003036 ALOGE("ERROR: Renderscript native registration failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07003037 goto bail;
3038 }
3039
3040 /* success -- return valid version number */
3041 result = JNI_VERSION_1_4;
3042
3043bail:
3044 return result;
3045}