blob: 3bef19e01e8ea82c305677b56b2c9d0d583352e4 [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: \
154 len = _env->GetArrayLength((jshortArray)data); \
155 ptr = _env->GetShortArrayElements((jshortArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700156 if (ptr == nullptr) { \
157 ALOGE("Failed to get Java array elements."); \
158 return; \
159 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800160 typeBytes = 2; \
Miao Wang87e908d2015-03-02 15:15:15 -0800161 if (usePadding) { \
162 srcPtr = ptr; \
163 len = len / 3 * 4; \
164 if (count == 0) { \
165 count = len / 4; \
166 } \
167 ptr = malloc (len * typeBytes); \
168 if (readonly) { \
169 copyWithPadding(ptr, srcPtr, mSize, count); \
170 fnc(__VA_ARGS__); \
171 } else { \
172 fnc(__VA_ARGS__); \
173 copyWithUnPadding(srcPtr, ptr, mSize, count); \
174 } \
175 free(ptr); \
176 ptr = srcPtr; \
177 } else { \
178 fnc(__VA_ARGS__); \
179 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700180 _env->ReleaseShortArrayElements((jshortArray)data, (jshort *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800181 return; \
182 case RS_TYPE_SIGNED_32: \
183 case RS_TYPE_UNSIGNED_32: \
184 len = _env->GetArrayLength((jintArray)data); \
185 ptr = _env->GetIntArrayElements((jintArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700186 if (ptr == nullptr) { \
187 ALOGE("Failed to get Java array elements."); \
188 return; \
189 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800190 typeBytes = 4; \
Miao Wang87e908d2015-03-02 15:15:15 -0800191 if (usePadding) { \
192 srcPtr = ptr; \
193 len = len / 3 * 4; \
194 if (count == 0) { \
195 count = len / 4; \
196 } \
197 ptr = malloc (len * typeBytes); \
198 if (readonly) { \
199 copyWithPadding(ptr, srcPtr, mSize, count); \
200 fnc(__VA_ARGS__); \
201 } else { \
202 fnc(__VA_ARGS__); \
203 copyWithUnPadding(srcPtr, ptr, mSize, count); \
204 } \
205 free(ptr); \
206 ptr = srcPtr; \
207 } else { \
208 fnc(__VA_ARGS__); \
209 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700210 _env->ReleaseIntArrayElements((jintArray)data, (jint *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800211 return; \
212 case RS_TYPE_SIGNED_64: \
213 case RS_TYPE_UNSIGNED_64: \
214 len = _env->GetArrayLength((jlongArray)data); \
215 ptr = _env->GetLongArrayElements((jlongArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700216 if (ptr == nullptr) { \
217 ALOGE("Failed to get Java array elements."); \
218 return; \
219 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800220 typeBytes = 8; \
Miao Wang87e908d2015-03-02 15:15:15 -0800221 if (usePadding) { \
222 srcPtr = ptr; \
223 len = len / 3 * 4; \
224 if (count == 0) { \
225 count = len / 4; \
226 } \
227 ptr = malloc (len * typeBytes); \
228 if (readonly) { \
229 copyWithPadding(ptr, srcPtr, mSize, count); \
230 fnc(__VA_ARGS__); \
231 } else { \
232 fnc(__VA_ARGS__); \
233 copyWithUnPadding(srcPtr, ptr, mSize, count); \
234 } \
235 free(ptr); \
236 ptr = srcPtr; \
237 } else { \
238 fnc(__VA_ARGS__); \
239 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700240 _env->ReleaseLongArrayElements((jlongArray)data, (jlong *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800241 return; \
242 default: \
243 break; \
244 } \
Miao Wang87e908d2015-03-02 15:15:15 -0800245 UNUSED(len, ptr, srcPtr, typeBytes, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800246}
247
248
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800249class AutoJavaStringToUTF8 {
250public:
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800251 AutoJavaStringToUTF8(JNIEnv* env, jstring str) : fEnv(env), fJStr(str) {
Chris Wailes488230c32014-08-14 11:22:40 -0700252 fCStr = env->GetStringUTFChars(str, nullptr);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800253 fLength = env->GetStringUTFLength(str);
254 }
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800255 ~AutoJavaStringToUTF8() {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800256 fEnv->ReleaseStringUTFChars(fJStr, fCStr);
257 }
258 const char* c_str() const { return fCStr; }
259 jsize length() const { return fLength; }
260
261private:
262 JNIEnv* fEnv;
263 jstring fJStr;
264 const char* fCStr;
265 jsize fLength;
266};
267
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800268class AutoJavaStringArrayToUTF8 {
269public:
270 AutoJavaStringArrayToUTF8(JNIEnv* env, jobjectArray strings, jsize stringsLength)
271 : mEnv(env), mStrings(strings), mStringsLength(stringsLength) {
Chris Wailes488230c32014-08-14 11:22:40 -0700272 mCStrings = nullptr;
273 mSizeArray = nullptr;
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800274 if (stringsLength > 0) {
275 mCStrings = (const char **)calloc(stringsLength, sizeof(char *));
276 mSizeArray = (size_t*)calloc(stringsLength, sizeof(size_t));
277 for (jsize ct = 0; ct < stringsLength; ct ++) {
278 jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
Chris Wailes488230c32014-08-14 11:22:40 -0700279 mCStrings[ct] = mEnv->GetStringUTFChars(s, nullptr);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800280 mSizeArray[ct] = mEnv->GetStringUTFLength(s);
281 }
282 }
283 }
284 ~AutoJavaStringArrayToUTF8() {
285 for (jsize ct=0; ct < mStringsLength; ct++) {
286 jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
287 mEnv->ReleaseStringUTFChars(s, mCStrings[ct]);
288 }
289 free(mCStrings);
290 free(mSizeArray);
291 }
292 const char **c_str() const { return mCStrings; }
293 size_t *c_str_len() const { return mSizeArray; }
294 jsize length() const { return mStringsLength; }
295
296private:
297 JNIEnv *mEnv;
298 jobjectArray mStrings;
299 const char **mCStrings;
300 size_t *mSizeArray;
301 jsize mStringsLength;
302};
303
Jason Samsd19f10d2009-05-22 14:03:28 -0700304// ---------------------------------------------------------------------------
305
Jason Samsffe9f482009-06-01 17:45:53 -0700306static jfieldID gContextId = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -0700307
308static void _nInit(JNIEnv *_env, jclass _this)
309{
Tim Murrayeff663f2013-11-15 13:08:30 -0800310 gContextId = _env->GetFieldID(_this, "mContext", "J");
Jason Samsd19f10d2009-05-22 14:03:28 -0700311}
312
Jason Samsd19f10d2009-05-22 14:03:28 -0700313// ---------------------------------------------------------------------------
314
Miao Wang87e908d2015-03-02 15:15:15 -0800315static void copyWithPadding(void* ptr, void* srcPtr, int mSize, int count) {
316 int sizeBytesPad = mSize * 4;
317 int sizeBytes = mSize * 3;
318 uint8_t *dst = static_cast<uint8_t *>(ptr);
319 uint8_t *src = static_cast<uint8_t *>(srcPtr);
320 for (int i = 0; i < count; i++) {
321 memcpy(dst, src, sizeBytes);
322 dst += sizeBytesPad;
323 src += sizeBytes;
324 }
325}
326
327static void copyWithUnPadding(void* ptr, void* srcPtr, int mSize, int count) {
328 int sizeBytesPad = mSize * 4;
329 int sizeBytes = mSize * 3;
330 uint8_t *dst = static_cast<uint8_t *>(ptr);
331 uint8_t *src = static_cast<uint8_t *>(srcPtr);
332 for (int i = 0; i < count; i++) {
333 memcpy(dst, src, sizeBytes);
334 dst += sizeBytes;
335 src += sizeBytesPad;
336 }
337}
338
339
340// ---------------------------------------------------------------------------
Jason Sams3eaa338e2009-06-10 15:04:38 -0700341static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800342nContextFinish(JNIEnv *_env, jobject _this, jlong con)
Jason Sams96ed4cf2010-06-15 12:15:57 -0700343{
Andreas Gampe67333922014-11-10 20:35:59 -0800344 if (kLogApi) {
345 ALOGD("nContextFinish, con(%p)", (RsContext)con);
346 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800347 rsContextFinish((RsContext)con);
Jason Sams96ed4cf2010-06-15 12:15:57 -0700348}
349
Yang Ni281c3252014-10-24 08:52:24 -0700350static jlong
351nClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong kernelID,
352 jlong returnValue, jlongArray fieldIDArray,
353 jlongArray valueArray, jintArray sizeArray,
354 jlongArray depClosureArray, jlongArray depFieldIDArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700355 jlong ret = 0;
356
Yang Ni281c3252014-10-24 08:52:24 -0700357 jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
358 jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700359 if (jFieldIDs == nullptr) {
360 ALOGE("Failed to get Java array elements: fieldIDs.");
361 return ret;
362 }
363
Yang Ni281c3252014-10-24 08:52:24 -0700364 jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
365 jsize values_length = _env->GetArrayLength(valueArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700366 if (jValues == nullptr) {
367 ALOGE("Failed to get Java array elements: values.");
368 return ret;
369 }
370
Yang Ni17c2d7a2015-04-30 16:13:54 -0700371 jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
Yang Ni281c3252014-10-24 08:52:24 -0700372 jsize sizes_length = _env->GetArrayLength(sizeArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700373 if (jSizes == nullptr) {
374 ALOGE("Failed to get Java array elements: sizes.");
375 return ret;
376 }
377
Yang Ni281c3252014-10-24 08:52:24 -0700378 jlong* jDepClosures =
379 _env->GetLongArrayElements(depClosureArray, nullptr);
380 jsize depClosures_length = _env->GetArrayLength(depClosureArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700381 if (jDepClosures == nullptr) {
382 ALOGE("Failed to get Java array elements: depClosures.");
383 return ret;
384 }
385
Yang Ni281c3252014-10-24 08:52:24 -0700386 jlong* jDepFieldIDs =
387 _env->GetLongArrayElements(depFieldIDArray, nullptr);
388 jsize depFieldIDs_length = _env->GetArrayLength(depFieldIDArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700389 if (jDepFieldIDs == nullptr) {
390 ALOGE("Failed to get Java array elements: depFieldIDs.");
391 return ret;
392 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700393
394 size_t numValues, numDependencies;
395 RsScriptFieldID* fieldIDs;
Yang Ni17c2d7a2015-04-30 16:13:54 -0700396 RsClosure* depClosures;
397 RsScriptFieldID* depFieldIDs;
398
399 if (fieldIDs_length != values_length || values_length != sizes_length) {
400 ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
401 goto exit;
402 }
403
404 numValues = (size_t)fieldIDs_length;
405
406 if (depClosures_length != depFieldIDs_length) {
407 ALOGE("Unmatched closures and field IDs for dependencies in closure creation.");
408 goto exit;
409 }
410
411 numDependencies = (size_t)depClosures_length;
412
413 if (numDependencies > numValues) {
414 ALOGE("Unexpected number of dependencies in closure creation");
415 goto exit;
416 }
417
Yang Ni7b2a46f2015-05-05 12:41:19 -0700418 if (numValues > RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700419 ALOGE("Too many arguments or globals in closure creation");
420 goto exit;
421 }
422
423 fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
424 if (fieldIDs == nullptr) {
425 goto exit;
426 }
427
428 for (size_t i = 0; i < numValues; i++) {
429 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
430 }
431
Yang Ni17c2d7a2015-04-30 16:13:54 -0700432 depClosures = (RsClosure*)alloca(sizeof(RsClosure) * numDependencies);
433 if (depClosures == nullptr) {
434 goto exit;
435 }
436
437 for (size_t i = 0; i < numDependencies; i++) {
438 depClosures[i] = (RsClosure)jDepClosures[i];
439 }
440
441 depFieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numDependencies);
442 if (depFieldIDs == nullptr) {
443 goto exit;
444 }
445
446 for (size_t i = 0; i < numDependencies; i++) {
Yang Ni281c3252014-10-24 08:52:24 -0700447 depFieldIDs[i] = (RsClosure)jDepFieldIDs[i];
448 }
449
Yang Ni17c2d7a2015-04-30 16:13:54 -0700450 ret = (jlong)(uintptr_t)rsClosureCreate(
Yang Ni281c3252014-10-24 08:52:24 -0700451 (RsContext)con, (RsScriptKernelID)kernelID, (RsAllocation)returnValue,
Yang Ni263cc902015-11-10 13:27:04 -0800452 fieldIDs, numValues, jValues, numValues,
Yang Ni17c2d7a2015-04-30 16:13:54 -0700453 (int*)jSizes, numValues,
454 depClosures, numDependencies,
455 depFieldIDs, numDependencies);
456
457exit:
458
459 _env->ReleaseLongArrayElements(depFieldIDArray, jDepFieldIDs, JNI_ABORT);
460 _env->ReleaseLongArrayElements(depClosureArray, jDepClosures, JNI_ABORT);
461 _env->ReleaseIntArrayElements (sizeArray, jSizes, JNI_ABORT);
462 _env->ReleaseLongArrayElements(valueArray, jValues, JNI_ABORT);
463 _env->ReleaseLongArrayElements(fieldIDArray, jFieldIDs, JNI_ABORT);
464
465 return ret;
Yang Ni281c3252014-10-24 08:52:24 -0700466}
467
Yang Nibe392ad2015-01-23 17:16:02 -0800468static jlong
469nInvokeClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong invokeID,
470 jbyteArray paramArray, jlongArray fieldIDArray, jlongArray valueArray,
471 jintArray sizeArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700472 jlong ret = 0;
473
Yang Nibe392ad2015-01-23 17:16:02 -0800474 jbyte* jParams = _env->GetByteArrayElements(paramArray, nullptr);
475 jsize jParamLength = _env->GetArrayLength(paramArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700476 if (jParams == nullptr) {
477 ALOGE("Failed to get Java array elements: params.");
478 return ret;
479 }
480
Yang Nibe392ad2015-01-23 17:16:02 -0800481 jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
482 jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700483 if (jFieldIDs == nullptr) {
484 ALOGE("Failed to get Java array elements: fieldIDs.");
485 return ret;
486 }
487
Yang Ni17c2d7a2015-04-30 16:13:54 -0700488 jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
489 jsize values_length = _env->GetArrayLength(valueArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700490 if (jValues == nullptr) {
491 ALOGE("Failed to get Java array elements: values.");
492 return ret;
493 }
494
Yang Ni17c2d7a2015-04-30 16:13:54 -0700495 jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
496 jsize sizes_length = _env->GetArrayLength(sizeArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700497 if (jSizes == nullptr) {
498 ALOGE("Failed to get Java array elements: sizes.");
499 return ret;
500 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700501
502 size_t numValues;
503 RsScriptFieldID* fieldIDs;
Yang Ni17c2d7a2015-04-30 16:13:54 -0700504
505 if (fieldIDs_length != values_length || values_length != sizes_length) {
506 ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
507 goto exit;
508 }
509
510 numValues = (size_t) fieldIDs_length;
511
Yang Ni7b2a46f2015-05-05 12:41:19 -0700512 if (numValues > RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700513 ALOGE("Too many arguments or globals in closure creation");
514 goto exit;
515 }
516
517 fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
518 if (fieldIDs == nullptr) {
519 goto exit;
520 }
521
522 for (size_t i = 0; i< numValues; i++) {
Yang Nibe392ad2015-01-23 17:16:02 -0800523 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
524 }
525
Yang Ni17c2d7a2015-04-30 16:13:54 -0700526 ret = (jlong)(uintptr_t)rsInvokeClosureCreate(
Yang Nibe392ad2015-01-23 17:16:02 -0800527 (RsContext)con, (RsScriptInvokeID)invokeID, jParams, jParamLength,
Yang Ni263cc902015-11-10 13:27:04 -0800528 fieldIDs, numValues, jValues, numValues,
Yang Ni17c2d7a2015-04-30 16:13:54 -0700529 (int*)jSizes, numValues);
530
531exit:
532
533 _env->ReleaseIntArrayElements (sizeArray, jSizes, JNI_ABORT);
534 _env->ReleaseLongArrayElements(valueArray, jValues, JNI_ABORT);
535 _env->ReleaseLongArrayElements(fieldIDArray, jFieldIDs, JNI_ABORT);
536 _env->ReleaseByteArrayElements(paramArray, jParams, JNI_ABORT);
537
538 return ret;
Yang Nibe392ad2015-01-23 17:16:02 -0800539}
540
Yang Ni281c3252014-10-24 08:52:24 -0700541static void
542nClosureSetArg(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
543 jint index, jlong value, jint size) {
Yang Ni263cc902015-11-10 13:27:04 -0800544 // Size is signed with -1 indicating the value is an Allocation
Yang Ni281c3252014-10-24 08:52:24 -0700545 rsClosureSetArg((RsContext)con, (RsClosure)closureID, (uint32_t)index,
Yang Ni263cc902015-11-10 13:27:04 -0800546 (uintptr_t)value, size);
Yang Ni281c3252014-10-24 08:52:24 -0700547}
548
549static void
550nClosureSetGlobal(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
551 jlong fieldID, jlong value, jint size) {
Yang Ni263cc902015-11-10 13:27:04 -0800552 // Size is signed with -1 indicating the value is an Allocation
Yang Ni281c3252014-10-24 08:52:24 -0700553 rsClosureSetGlobal((RsContext)con, (RsClosure)closureID,
Yang Ni263cc902015-11-10 13:27:04 -0800554 (RsScriptFieldID)fieldID, (int64_t)value, size);
Yang Ni281c3252014-10-24 08:52:24 -0700555}
556
557static long
Yang Ni35be56c2015-04-02 17:47:56 -0700558nScriptGroup2Create(JNIEnv *_env, jobject _this, jlong con, jstring name,
Yang Niebf63402015-01-16 11:06:26 -0800559 jstring cacheDir, jlongArray closureArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700560 jlong ret = 0;
561
Yang Ni35be56c2015-04-02 17:47:56 -0700562 AutoJavaStringToUTF8 nameUTF(_env, name);
Yang Niebf63402015-01-16 11:06:26 -0800563 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
564
Yang Ni281c3252014-10-24 08:52:24 -0700565 jlong* jClosures = _env->GetLongArrayElements(closureArray, nullptr);
566 jsize numClosures = _env->GetArrayLength(closureArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700567 if (jClosures == nullptr) {
568 ALOGE("Failed to get Java array elements: closures.");
569 return ret;
570 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700571
572 RsClosure* closures;
573
Yang Ni7b2a46f2015-05-05 12:41:19 -0700574 if (numClosures > (jsize) RS_SCRIPT_GROUP_MAX_NUMBER_CLOSURES) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700575 ALOGE("Too many closures in script group");
576 goto exit;
577 }
578
579 closures = (RsClosure*)alloca(sizeof(RsClosure) * numClosures);
580 if (closures == nullptr) {
581 goto exit;
582 }
583
Yang Ni281c3252014-10-24 08:52:24 -0700584 for (int i = 0; i < numClosures; i++) {
585 closures[i] = (RsClosure)jClosures[i];
586 }
587
Yang Ni17c2d7a2015-04-30 16:13:54 -0700588 ret = (jlong)(uintptr_t)rsScriptGroup2Create(
Yang Ni35be56c2015-04-02 17:47:56 -0700589 (RsContext)con, nameUTF.c_str(), nameUTF.length(),
590 cacheDirUTF.c_str(), cacheDirUTF.length(),
Yang Niebf63402015-01-16 11:06:26 -0800591 closures, numClosures);
Yang Ni17c2d7a2015-04-30 16:13:54 -0700592
593exit:
594
595 _env->ReleaseLongArrayElements(closureArray, jClosures, JNI_ABORT);
596
597 return ret;
Yang Ni281c3252014-10-24 08:52:24 -0700598}
599
600static void
601nScriptGroup2Execute(JNIEnv *_env, jobject _this, jlong con, jlong groupID) {
602 rsScriptGroupExecute((RsContext)con, (RsScriptGroup2)groupID);
603}
604
Jason Sams96ed4cf2010-06-15 12:15:57 -0700605static void
Tim Murray25207df2015-01-12 16:47:56 -0800606nScriptIntrinsicBLAS_Single(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
607 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
608 jfloat alpha, jlong A, jlong B, jfloat beta, jlong C, jint incX, jint incY,
609 jint KL, jint KU) {
610 RsBlasCall call;
611 memset(&call, 0, sizeof(call));
612 call.func = (RsBlasFunction)func;
613 call.transA = (RsBlasTranspose)TransA;
614 call.transB = (RsBlasTranspose)TransB;
615 call.side = (RsBlasSide)Side;
616 call.uplo = (RsBlasUplo)Uplo;
617 call.diag = (RsBlasDiag)Diag;
618 call.M = M;
619 call.N = N;
620 call.K = K;
621 call.alpha.f = alpha;
622 call.beta.f = beta;
623 call.incX = incX;
624 call.incY = incY;
625 call.KL = KL;
626 call.KU = KU;
627
628 RsAllocation in_allocs[3];
629 in_allocs[0] = (RsAllocation)A;
630 in_allocs[1] = (RsAllocation)B;
631 in_allocs[2] = (RsAllocation)C;
632
633 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
634 in_allocs, sizeof(in_allocs), nullptr,
635 &call, sizeof(call), nullptr, 0);
636}
637
638static void
639nScriptIntrinsicBLAS_Double(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
640 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
641 jdouble alpha, jlong A, jlong B, jdouble beta, jlong C, jint incX, jint incY,
642 jint KL, jint KU) {
643 RsBlasCall call;
644 memset(&call, 0, sizeof(call));
645 call.func = (RsBlasFunction)func;
646 call.transA = (RsBlasTranspose)TransA;
647 call.transB = (RsBlasTranspose)TransB;
648 call.side = (RsBlasSide)Side;
649 call.uplo = (RsBlasUplo)Uplo;
650 call.diag = (RsBlasDiag)Diag;
651 call.M = M;
652 call.N = N;
653 call.K = K;
654 call.alpha.d = alpha;
655 call.beta.d = beta;
656 call.incX = incX;
657 call.incY = incY;
658 call.KL = KL;
659 call.KU = KU;
660
661 RsAllocation in_allocs[3];
662 in_allocs[0] = (RsAllocation)A;
663 in_allocs[1] = (RsAllocation)B;
664 in_allocs[2] = (RsAllocation)C;
665
666 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700667 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800668 &call, sizeof(call), nullptr, 0);
669}
670
671static void
672nScriptIntrinsicBLAS_Complex(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
673 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
674 jfloat alphaX, jfloat alphaY, jlong A, jlong B, jfloat betaX,
675 jfloat betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
676 RsBlasCall call;
677 memset(&call, 0, sizeof(call));
678 call.func = (RsBlasFunction)func;
679 call.transA = (RsBlasTranspose)TransA;
680 call.transB = (RsBlasTranspose)TransB;
681 call.side = (RsBlasSide)Side;
682 call.uplo = (RsBlasUplo)Uplo;
683 call.diag = (RsBlasDiag)Diag;
684 call.M = M;
685 call.N = N;
686 call.K = K;
687 call.alpha.c.r = alphaX;
688 call.alpha.c.i = alphaY;
689 call.beta.c.r = betaX;
Miao Wang82585b32015-04-30 13:44:49 -0700690 call.beta.c.i = betaY;
Tim Murray25207df2015-01-12 16:47:56 -0800691 call.incX = incX;
692 call.incY = incY;
693 call.KL = KL;
694 call.KU = KU;
695
696 RsAllocation in_allocs[3];
697 in_allocs[0] = (RsAllocation)A;
698 in_allocs[1] = (RsAllocation)B;
699 in_allocs[2] = (RsAllocation)C;
700
701 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700702 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800703 &call, sizeof(call), nullptr, 0);
704}
705
706static void
707nScriptIntrinsicBLAS_Z(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
708 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
709 jdouble alphaX, jdouble alphaY, jlong A, jlong B, jdouble betaX,
710 jdouble betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
711 RsBlasCall call;
712 memset(&call, 0, sizeof(call));
713 call.func = (RsBlasFunction)func;
714 call.transA = (RsBlasTranspose)TransA;
715 call.transB = (RsBlasTranspose)TransB;
716 call.side = (RsBlasSide)Side;
717 call.uplo = (RsBlasUplo)Uplo;
718 call.diag = (RsBlasDiag)Diag;
719 call.M = M;
720 call.N = N;
721 call.K = K;
722 call.alpha.z.r = alphaX;
723 call.alpha.z.i = alphaY;
724 call.beta.z.r = betaX;
Miao Wang82585b32015-04-30 13:44:49 -0700725 call.beta.z.i = betaY;
Tim Murray25207df2015-01-12 16:47:56 -0800726 call.incX = incX;
727 call.incY = incY;
728 call.KL = KL;
729 call.KU = KU;
730
731 RsAllocation in_allocs[3];
732 in_allocs[0] = (RsAllocation)A;
733 in_allocs[1] = (RsAllocation)B;
734 in_allocs[2] = (RsAllocation)C;
735
736 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700737 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800738 &call, sizeof(call), nullptr, 0);
739}
740
741
742static void
Tim Murray9cb16a22015-04-01 11:07:16 -0700743nScriptIntrinsicBLAS_BNNM(JNIEnv *_env, jobject _this, jlong con, jlong id, jint M, jint N, jint K,
744 jlong A, jint a_offset, jlong B, jint b_offset, jlong C, jint c_offset,
745 jint c_mult_int) {
746 RsBlasCall call;
747 memset(&call, 0, sizeof(call));
748 call.func = RsBlas_bnnm;
749 call.M = M;
750 call.N = N;
751 call.K = K;
Miao Wang25148062015-06-29 17:43:03 -0700752 call.a_offset = a_offset & 0xFF;
753 call.b_offset = b_offset & 0xFF;
Tim Murray9cb16a22015-04-01 11:07:16 -0700754 call.c_offset = c_offset;
755 call.c_mult_int = c_mult_int;
756
757 RsAllocation in_allocs[3];
758 in_allocs[0] = (RsAllocation)A;
759 in_allocs[1] = (RsAllocation)B;
760 in_allocs[2] = (RsAllocation)C;
761
762 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700763 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray9cb16a22015-04-01 11:07:16 -0700764 &call, sizeof(call), nullptr, 0);
765}
766
767
768static void
Tim Murray460a0492013-11-19 12:45:54 -0800769nAssignName(JNIEnv *_env, jobject _this, jlong con, jlong obj, jbyteArray str)
Jason Sams3eaa338e2009-06-10 15:04:38 -0700770{
Andreas Gampe67333922014-11-10 20:35:59 -0800771 if (kLogApi) {
772 ALOGD("nAssignName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
773 }
Jason Sams3eaa338e2009-06-10 15:04:38 -0700774 jint len = _env->GetArrayLength(str);
775 jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
Miao Wangba8766c2015-10-12 17:24:13 -0700776 if (cptr == nullptr) {
777 ALOGE("Failed to get Java array elements");
778 return;
779 }
780
Tim Murrayeff663f2013-11-15 13:08:30 -0800781 rsAssignName((RsContext)con, (void *)obj, (const char *)cptr, len);
Jason Sams3eaa338e2009-06-10 15:04:38 -0700782 _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
783}
784
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700785static jstring
Tim Murray460a0492013-11-19 12:45:54 -0800786nGetName(JNIEnv *_env, jobject _this, jlong con, jlong obj)
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700787{
Andreas Gampe67333922014-11-10 20:35:59 -0800788 if (kLogApi) {
789 ALOGD("nGetName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
790 }
Chris Wailes488230c32014-08-14 11:22:40 -0700791 const char *name = nullptr;
Tim Murrayeff663f2013-11-15 13:08:30 -0800792 rsaGetName((RsContext)con, (void *)obj, &name);
Chris Wailes488230c32014-08-14 11:22:40 -0700793 if(name == nullptr || strlen(name) == 0) {
794 return nullptr;
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700795 }
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700796 return _env->NewStringUTF(name);
797}
798
Jason Sams7ce033d2009-08-18 14:14:24 -0700799static void
Tim Murray460a0492013-11-19 12:45:54 -0800800nObjDestroy(JNIEnv *_env, jobject _this, jlong con, jlong obj)
Jason Sams7ce033d2009-08-18 14:14:24 -0700801{
Andreas Gampe67333922014-11-10 20:35:59 -0800802 if (kLogApi) {
803 ALOGD("nObjDestroy, con(%p) obj(%p)", (RsContext)con, (void *)obj);
804 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800805 rsObjDestroy((RsContext)con, (void *)obj);
Jason Sams7ce033d2009-08-18 14:14:24 -0700806}
807
Jason Sams3eaa338e2009-06-10 15:04:38 -0700808// ---------------------------------------------------------------------------
809
Tim Murrayeff663f2013-11-15 13:08:30 -0800810static jlong
Jason Samsd19f10d2009-05-22 14:03:28 -0700811nDeviceCreate(JNIEnv *_env, jobject _this)
812{
Andreas Gampe67333922014-11-10 20:35:59 -0800813 if (kLogApi) {
814 ALOGD("nDeviceCreate");
815 }
Tim Murray3aa89c12014-08-18 17:51:22 -0700816 return (jlong)(uintptr_t)rsDeviceCreate();
Jason Samsd19f10d2009-05-22 14:03:28 -0700817}
818
819static void
Tim Murray5eaf4682014-01-10 11:25:52 -0800820nDeviceDestroy(JNIEnv *_env, jobject _this, jlong dev)
Jason Samsd19f10d2009-05-22 14:03:28 -0700821{
Andreas Gampe67333922014-11-10 20:35:59 -0800822 if (kLogApi) {
823 ALOGD("nDeviceDestroy");
824 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700825 return rsDeviceDestroy((RsDevice)dev);
826}
827
Jason Samsebfb4362009-09-23 13:57:02 -0700828static void
Tim Murray5eaf4682014-01-10 11:25:52 -0800829nDeviceSetConfig(JNIEnv *_env, jobject _this, jlong dev, jint p, jint value)
Jason Samsebfb4362009-09-23 13:57:02 -0700830{
Andreas Gampe67333922014-11-10 20:35:59 -0800831 if (kLogApi) {
832 ALOGD("nDeviceSetConfig dev(%p), param(%i), value(%i)", (void *)dev, p, value);
833 }
Jason Samsebfb4362009-09-23 13:57:02 -0700834 return rsDeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value);
835}
836
Tim Murrayeff663f2013-11-15 13:08:30 -0800837static jlong
Jason Sams81cd2b12014-12-02 12:36:43 -0800838nContextCreate(JNIEnv *_env, jobject _this, jlong dev, jint flags, jint sdkVer, jint contextType)
Jason Samsd19f10d2009-05-22 14:03:28 -0700839{
Andreas Gampe67333922014-11-10 20:35:59 -0800840 if (kLogApi) {
841 ALOGD("nContextCreate");
842 }
Jason Sams81cd2b12014-12-02 12:36:43 -0800843 return (jlong)(uintptr_t)rsContextCreate((RsDevice)dev, 0, sdkVer, (RsContextType)contextType, flags);
Jason Sams704ff642010-02-09 16:05:07 -0800844}
845
Tim Murrayeff663f2013-11-15 13:08:30 -0800846static jlong
Tim Murray5eaf4682014-01-10 11:25:52 -0800847nContextCreateGL(JNIEnv *_env, jobject _this, jlong dev, jint ver, jint sdkVer,
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000848 jint colorMin, jint colorPref,
849 jint alphaMin, jint alphaPref,
850 jint depthMin, jint depthPref,
851 jint stencilMin, jint stencilPref,
852 jint samplesMin, jint samplesPref, jfloat samplesQ,
853 jint dpi)
Jason Sams704ff642010-02-09 16:05:07 -0800854{
Jason Sams11c8af92010-10-13 15:31:10 -0700855 RsSurfaceConfig sc;
856 sc.alphaMin = alphaMin;
857 sc.alphaPref = alphaPref;
858 sc.colorMin = colorMin;
859 sc.colorPref = colorPref;
860 sc.depthMin = depthMin;
861 sc.depthPref = depthPref;
862 sc.samplesMin = samplesMin;
863 sc.samplesPref = samplesPref;
864 sc.samplesQ = samplesQ;
865
Andreas Gampe67333922014-11-10 20:35:59 -0800866 if (kLogApi) {
867 ALOGD("nContextCreateGL");
868 }
Tim Murray3aa89c12014-08-18 17:51:22 -0700869 return (jlong)(uintptr_t)rsContextCreateGL((RsDevice)dev, ver, sdkVer, sc, dpi);
Jason Samsd19f10d2009-05-22 14:03:28 -0700870}
871
872static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800873nContextSetPriority(JNIEnv *_env, jobject _this, jlong con, jint p)
Jason Sams7d787b42009-11-15 12:14:26 -0800874{
Andreas Gampe67333922014-11-10 20:35:59 -0800875 if (kLogApi) {
876 ALOGD("ContextSetPriority, con(%p), priority(%i)", (RsContext)con, p);
877 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800878 rsContextSetPriority((RsContext)con, p);
Jason Sams7d787b42009-11-15 12:14:26 -0800879}
880
Tim Murray47f31582015-04-07 15:43:24 -0700881static void
882nContextSetCacheDir(JNIEnv *_env, jobject _this, jlong con, jstring cacheDir)
883{
884 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
885
886 if (kLogApi) {
887 ALOGD("ContextSetCacheDir, con(%p), cacheDir(%s)", (RsContext)con, cacheDirUTF.c_str());
888 }
889 rsContextSetCacheDir((RsContext)con, cacheDirUTF.c_str(), cacheDirUTF.length());
890}
891
Jason Sams7d787b42009-11-15 12:14:26 -0800892
893
894static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800895nContextSetSurface(JNIEnv *_env, jobject _this, jlong con, jint width, jint height, jobject wnd)
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800896{
Andreas Gampe67333922014-11-10 20:35:59 -0800897 if (kLogApi) {
898 ALOGD("nContextSetSurface, con(%p), width(%i), height(%i), surface(%p)", (RsContext)con,
899 width, height, (Surface *)wnd);
900 }
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800901
Chris Wailes488230c32014-08-14 11:22:40 -0700902 ANativeWindow * window = nullptr;
903 if (wnd == nullptr) {
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800904
905 } else {
Jeff Brown64a55af2012-08-26 02:47:39 -0700906 window = android_view_Surface_getNativeWindow(_env, wnd).get();
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800907 }
908
Tim Murrayeff663f2013-11-15 13:08:30 -0800909 rsContextSetSurface((RsContext)con, width, height, window);
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800910}
911
912static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800913nContextDestroy(JNIEnv *_env, jobject _this, jlong con)
Jason Samsd19f10d2009-05-22 14:03:28 -0700914{
Andreas Gampe67333922014-11-10 20:35:59 -0800915 if (kLogApi) {
916 ALOGD("nContextDestroy, con(%p)", (RsContext)con);
917 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800918 rsContextDestroy((RsContext)con);
Jason Samsd19f10d2009-05-22 14:03:28 -0700919}
920
Jason Sams715333b2009-11-17 17:26:46 -0800921static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800922nContextDump(JNIEnv *_env, jobject _this, jlong con, jint bits)
Jason Sams715333b2009-11-17 17:26:46 -0800923{
Andreas Gampe67333922014-11-10 20:35:59 -0800924 if (kLogApi) {
925 ALOGD("nContextDump, con(%p) bits(%i)", (RsContext)con, bits);
926 }
Jason Sams715333b2009-11-17 17:26:46 -0800927 rsContextDump((RsContext)con, bits);
928}
Jason Samsd19f10d2009-05-22 14:03:28 -0700929
930static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800931nContextPause(JNIEnv *_env, jobject _this, jlong con)
Jason Sams65e7aa52009-09-24 17:38:20 -0700932{
Andreas Gampe67333922014-11-10 20:35:59 -0800933 if (kLogApi) {
934 ALOGD("nContextPause, con(%p)", (RsContext)con);
935 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800936 rsContextPause((RsContext)con);
Jason Sams65e7aa52009-09-24 17:38:20 -0700937}
938
939static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800940nContextResume(JNIEnv *_env, jobject _this, jlong con)
Jason Sams65e7aa52009-09-24 17:38:20 -0700941{
Andreas Gampe67333922014-11-10 20:35:59 -0800942 if (kLogApi) {
943 ALOGD("nContextResume, con(%p)", (RsContext)con);
944 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800945 rsContextResume((RsContext)con);
Jason Sams65e7aa52009-09-24 17:38:20 -0700946}
947
Jason Sams1c415172010-11-08 17:06:46 -0800948
949static jstring
Tim Murrayeff663f2013-11-15 13:08:30 -0800950nContextGetErrorMessage(JNIEnv *_env, jobject _this, jlong con)
Jason Sams1c415172010-11-08 17:06:46 -0800951{
Andreas Gampe67333922014-11-10 20:35:59 -0800952 if (kLogApi) {
953 ALOGD("nContextGetErrorMessage, con(%p)", (RsContext)con);
954 }
Jason Sams1c415172010-11-08 17:06:46 -0800955 char buf[1024];
956
957 size_t receiveLen;
958 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800959 int id = rsContextGetMessage((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -0700960 buf, sizeof(buf),
961 &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -0700962 &subID, sizeof(subID));
Jason Sams1c415172010-11-08 17:06:46 -0800963 if (!id && receiveLen) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +0100964 ALOGV("message receive buffer too small. %zu", receiveLen);
Jason Sams1c415172010-11-08 17:06:46 -0800965 }
966 return _env->NewStringUTF(buf);
967}
968
Jason Samsedbfabd2011-05-17 15:01:29 -0700969static jint
Tim Murrayeff663f2013-11-15 13:08:30 -0800970nContextGetUserMessage(JNIEnv *_env, jobject _this, jlong con, jintArray data)
Jason Sams516c3192009-10-06 13:58:47 -0700971{
Jason Sams516c3192009-10-06 13:58:47 -0700972 jint len = _env->GetArrayLength(data);
Andreas Gampe67333922014-11-10 20:35:59 -0800973 if (kLogApi) {
974 ALOGD("nContextGetMessage, con(%p), len(%i)", (RsContext)con, len);
975 }
Chris Wailes488230c32014-08-14 11:22:40 -0700976 jint *ptr = _env->GetIntArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -0700977 if (ptr == nullptr) {
978 ALOGE("Failed to get Java array elements");
979 return 0;
980 }
Jason Sams516c3192009-10-06 13:58:47 -0700981 size_t receiveLen;
Jason Sams1c415172010-11-08 17:06:46 -0800982 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800983 int id = rsContextGetMessage((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -0700984 ptr, len * 4,
985 &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -0700986 &subID, sizeof(subID));
Jason Sams516c3192009-10-06 13:58:47 -0700987 if (!id && receiveLen) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +0100988 ALOGV("message receive buffer too small. %zu", receiveLen);
Jason Sams516c3192009-10-06 13:58:47 -0700989 }
990 _env->ReleaseIntArrayElements(data, ptr, 0);
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000991 return (jint)id;
Jason Sams1c415172010-11-08 17:06:46 -0800992}
993
994static jint
Tim Murrayeff663f2013-11-15 13:08:30 -0800995nContextPeekMessage(JNIEnv *_env, jobject _this, jlong con, jintArray auxData)
Jason Sams1c415172010-11-08 17:06:46 -0800996{
Andreas Gampe67333922014-11-10 20:35:59 -0800997 if (kLogApi) {
998 ALOGD("nContextPeekMessage, con(%p)", (RsContext)con);
999 }
Chris Wailes488230c32014-08-14 11:22:40 -07001000 jint *auxDataPtr = _env->GetIntArrayElements(auxData, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001001 if (auxDataPtr == nullptr) {
1002 ALOGE("Failed to get Java array elements");
1003 return 0;
1004 }
Jason Sams1c415172010-11-08 17:06:46 -08001005 size_t receiveLen;
1006 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -08001007 int id = rsContextPeekMessage((RsContext)con, &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -07001008 &subID, sizeof(subID));
Jason Sams1c415172010-11-08 17:06:46 -08001009 auxDataPtr[0] = (jint)subID;
1010 auxDataPtr[1] = (jint)receiveLen;
1011 _env->ReleaseIntArrayElements(auxData, auxDataPtr, 0);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001012 return (jint)id;
Jason Sams516c3192009-10-06 13:58:47 -07001013}
1014
Tim Murrayeff663f2013-11-15 13:08:30 -08001015static void nContextInitToClient(JNIEnv *_env, jobject _this, jlong con)
Jason Sams516c3192009-10-06 13:58:47 -07001016{
Andreas Gampe67333922014-11-10 20:35:59 -08001017 if (kLogApi) {
1018 ALOGD("nContextInitToClient, con(%p)", (RsContext)con);
1019 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001020 rsContextInitToClient((RsContext)con);
Jason Sams516c3192009-10-06 13:58:47 -07001021}
1022
Tim Murrayeff663f2013-11-15 13:08:30 -08001023static void nContextDeinitToClient(JNIEnv *_env, jobject _this, jlong con)
Jason Sams516c3192009-10-06 13:58:47 -07001024{
Andreas Gampe67333922014-11-10 20:35:59 -08001025 if (kLogApi) {
1026 ALOGD("nContextDeinitToClient, con(%p)", (RsContext)con);
1027 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001028 rsContextDeinitToClient((RsContext)con);
Jason Sams516c3192009-10-06 13:58:47 -07001029}
1030
Jason Sams455d6442013-02-05 19:20:18 -08001031static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001032nContextSendMessage(JNIEnv *_env, jobject _this, jlong con, jint id, jintArray data)
Jason Sams455d6442013-02-05 19:20:18 -08001033{
Chris Wailes488230c32014-08-14 11:22:40 -07001034 jint *ptr = nullptr;
Jason Sams455d6442013-02-05 19:20:18 -08001035 jint len = 0;
1036 if (data) {
1037 len = _env->GetArrayLength(data);
Stephen Hines4a043c12014-08-21 23:20:32 -07001038 ptr = _env->GetIntArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001039 if (ptr == nullptr) {
1040 ALOGE("Failed to get Java array elements");
1041 return;
1042 }
Jason Sams455d6442013-02-05 19:20:18 -08001043 }
Andreas Gampe67333922014-11-10 20:35:59 -08001044 if (kLogApi) {
1045 ALOGD("nContextSendMessage, con(%p), id(%i), len(%i)", (RsContext)con, id, len);
1046 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001047 rsContextSendMessage((RsContext)con, id, (const uint8_t *)ptr, len * sizeof(int));
Jason Sams455d6442013-02-05 19:20:18 -08001048 if (data) {
1049 _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
1050 }
1051}
1052
1053
Jason Sams516c3192009-10-06 13:58:47 -07001054
Tim Murray460a0492013-11-19 12:45:54 -08001055static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001056nElementCreate(JNIEnv *_env, jobject _this, jlong con, jlong type, jint kind, jboolean norm,
1057 jint size)
Jason Samsd19f10d2009-05-22 14:03:28 -07001058{
Andreas Gampe67333922014-11-10 20:35:59 -08001059 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001060 ALOGD("nElementCreate, con(%p), type(%" PRId64 "), kind(%i), norm(%i), size(%i)", (RsContext)con,
Andreas Gampe67333922014-11-10 20:35:59 -08001061 type, kind, norm, size);
1062 }
1063 return (jlong)(uintptr_t)rsElementCreate((RsContext)con, (RsDataType)type, (RsDataKind)kind,
1064 norm, size);
Jason Samsd19f10d2009-05-22 14:03:28 -07001065}
1066
Tim Murray460a0492013-11-19 12:45:54 -08001067static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001068nElementCreate2(JNIEnv *_env, jobject _this, jlong con,
Ashok Bhat98071552014-02-12 09:54:43 +00001069 jlongArray _ids, jobjectArray _names, jintArray _arraySizes)
Jason Samsd19f10d2009-05-22 14:03:28 -07001070{
Jason Sams718cd1f2009-12-23 14:35:29 -08001071 int fieldCount = _env->GetArrayLength(_ids);
Andreas Gampe67333922014-11-10 20:35:59 -08001072 if (kLogApi) {
1073 ALOGD("nElementCreate2, con(%p)", (RsContext)con);
1074 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001075
Chris Wailes488230c32014-08-14 11:22:40 -07001076 jlong *jIds = _env->GetLongArrayElements(_ids, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001077 if (jIds == nullptr) {
1078 ALOGE("Failed to get Java array elements: ids");
1079 return 0;
1080 }
Chris Wailes488230c32014-08-14 11:22:40 -07001081 jint *jArraySizes = _env->GetIntArrayElements(_arraySizes, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001082 if (jArraySizes == nullptr) {
1083 ALOGE("Failed to get Java array elements: arraySizes");
1084 return 0;
1085 }
Ashok Bhat98071552014-02-12 09:54:43 +00001086
1087 RsElement *ids = (RsElement*)malloc(fieldCount * sizeof(RsElement));
1088 uint32_t *arraySizes = (uint32_t *)malloc(fieldCount * sizeof(uint32_t));
1089
1090 for(int i = 0; i < fieldCount; i ++) {
1091 ids[i] = (RsElement)jIds[i];
1092 arraySizes[i] = (uint32_t)jArraySizes[i];
1093 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001094
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08001095 AutoJavaStringArrayToUTF8 names(_env, _names, fieldCount);
1096
1097 const char **nameArray = names.c_str();
1098 size_t *sizeArray = names.c_str_len();
1099
Tim Murray3aa89c12014-08-18 17:51:22 -07001100 jlong id = (jlong)(uintptr_t)rsElementCreate2((RsContext)con,
Ashok Bhat98071552014-02-12 09:54:43 +00001101 (const RsElement *)ids, fieldCount,
Jason Sams7a22e102011-05-06 14:14:30 -07001102 nameArray, fieldCount * sizeof(size_t), sizeArray,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001103 (const uint32_t *)arraySizes, fieldCount);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08001104
Ashok Bhat98071552014-02-12 09:54:43 +00001105 free(ids);
1106 free(arraySizes);
1107 _env->ReleaseLongArrayElements(_ids, jIds, JNI_ABORT);
1108 _env->ReleaseIntArrayElements(_arraySizes, jArraySizes, JNI_ABORT);
1109
Tim Murray3aa89c12014-08-18 17:51:22 -07001110 return (jlong)(uintptr_t)id;
Jason Samsd19f10d2009-05-22 14:03:28 -07001111}
1112
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001113static void
Tim Murray460a0492013-11-19 12:45:54 -08001114nElementGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jintArray _elementData)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001115{
1116 int dataSize = _env->GetArrayLength(_elementData);
Andreas Gampe67333922014-11-10 20:35:59 -08001117 if (kLogApi) {
1118 ALOGD("nElementGetNativeData, con(%p)", (RsContext)con);
1119 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001120
1121 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
1122 assert(dataSize == 5);
1123
Narayan Kamath78c0ce52014-03-19 10:15:51 +00001124 uintptr_t elementData[5];
Tim Murrayeff663f2013-11-15 13:08:30 -08001125 rsaElementGetNativeData((RsContext)con, (RsElement)id, elementData, dataSize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001126
1127 for(jint i = 0; i < dataSize; i ++) {
Ashok Bhat98071552014-02-12 09:54:43 +00001128 const jint data = (jint)elementData[i];
1129 _env->SetIntArrayRegion(_elementData, i, 1, &data);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001130 }
1131}
1132
1133
1134static void
Tim Murray460a0492013-11-19 12:45:54 -08001135nElementGetSubElements(JNIEnv *_env, jobject _this, jlong con, jlong id,
Ashok Bhat98071552014-02-12 09:54:43 +00001136 jlongArray _IDs,
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -07001137 jobjectArray _names,
1138 jintArray _arraySizes)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001139{
Ashok Bhat98071552014-02-12 09:54:43 +00001140 uint32_t dataSize = _env->GetArrayLength(_IDs);
Andreas Gampe67333922014-11-10 20:35:59 -08001141 if (kLogApi) {
1142 ALOGD("nElementGetSubElements, con(%p)", (RsContext)con);
1143 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001144
Ashok Bhat98071552014-02-12 09:54:43 +00001145 uintptr_t *ids = (uintptr_t*)malloc(dataSize * sizeof(uintptr_t));
1146 const char **names = (const char **)malloc(dataSize * sizeof(const char *));
Narayan Kamath78c0ce52014-03-19 10:15:51 +00001147 uint32_t *arraySizes = (uint32_t *)malloc(dataSize * sizeof(uint32_t));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001148
Andreas Gampe67333922014-11-10 20:35:59 -08001149 rsaElementGetSubElements((RsContext)con, (RsElement)id, ids, names, arraySizes,
1150 (uint32_t)dataSize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001151
Ashok Bhat98071552014-02-12 09:54:43 +00001152 for(uint32_t i = 0; i < dataSize; i++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07001153 const jlong id = (jlong)(uintptr_t)ids[i];
Ashok Bhat98071552014-02-12 09:54:43 +00001154 const jint arraySize = (jint)arraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001155 _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
Ashok Bhat98071552014-02-12 09:54:43 +00001156 _env->SetLongArrayRegion(_IDs, i, 1, &id);
1157 _env->SetIntArrayRegion(_arraySizes, i, 1, &arraySize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001158 }
1159
1160 free(ids);
1161 free(names);
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -07001162 free(arraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001163}
1164
Jason Samsd19f10d2009-05-22 14:03:28 -07001165// -----------------------------------
1166
Tim Murray460a0492013-11-19 12:45:54 -08001167static jlong
1168nTypeCreate(JNIEnv *_env, jobject _this, jlong con, jlong eid,
Jason Samsb109cc72013-01-07 18:20:12 -08001169 jint dimx, jint dimy, jint dimz, jboolean mips, jboolean faces, jint yuv)
Jason Samsd19f10d2009-05-22 14:03:28 -07001170{
Andreas Gampe67333922014-11-10 20:35:59 -08001171 if (kLogApi) {
1172 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 +01001173 (RsContext)con, (void*)eid, dimx, dimy, dimz, mips, faces, yuv);
Andreas Gampe67333922014-11-10 20:35:59 -08001174 }
Jason Sams3b9c52a2010-10-14 17:48:46 -07001175
Andreas Gampe67333922014-11-10 20:35:59 -08001176 return (jlong)(uintptr_t)rsTypeCreate((RsContext)con, (RsElement)eid, dimx, dimy, dimz, mips,
1177 faces, yuv);
Jason Samsd19f10d2009-05-22 14:03:28 -07001178}
1179
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001180static void
Ashok Bhat98071552014-02-12 09:54:43 +00001181nTypeGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jlongArray _typeData)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001182{
1183 // We are packing 6 items: mDimX; mDimY; mDimZ;
1184 // mDimLOD; mDimFaces; mElement; into typeData
1185 int elementCount = _env->GetArrayLength(_typeData);
1186
1187 assert(elementCount == 6);
Andreas Gampe67333922014-11-10 20:35:59 -08001188 if (kLogApi) {
1189 ALOGD("nTypeGetNativeData, con(%p)", (RsContext)con);
1190 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001191
Ashok Bhat98071552014-02-12 09:54:43 +00001192 uintptr_t typeData[6];
Tim Murrayeff663f2013-11-15 13:08:30 -08001193 rsaTypeGetNativeData((RsContext)con, (RsType)id, typeData, 6);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001194
1195 for(jint i = 0; i < elementCount; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07001196 const jlong data = (jlong)(uintptr_t)typeData[i];
Ashok Bhat98071552014-02-12 09:54:43 +00001197 _env->SetLongArrayRegion(_typeData, i, 1, &data);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001198 }
1199}
1200
Jason Samsd19f10d2009-05-22 14:03:28 -07001201// -----------------------------------
1202
Tim Murray460a0492013-11-19 12:45:54 -08001203static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001204nAllocationCreateTyped(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mips, jint usage,
1205 jlong pointer)
Jason Samsd19f10d2009-05-22 14:03:28 -07001206{
Andreas Gampe67333922014-11-10 20:35:59 -08001207 if (kLogApi) {
1208 ALOGD("nAllocationCreateTyped, con(%p), type(%p), mip(%i), usage(%i), ptr(%p)",
1209 (RsContext)con, (RsElement)type, mips, usage, (void *)pointer);
1210 }
1211 return (jlong)(uintptr_t) rsAllocationCreateTyped((RsContext)con, (RsType)type,
1212 (RsAllocationMipmapControl)mips,
1213 (uint32_t)usage, (uintptr_t)pointer);
Jason Samsd19f10d2009-05-22 14:03:28 -07001214}
1215
Jason Samsd19f10d2009-05-22 14:03:28 -07001216static void
Tim Murray460a0492013-11-19 12:45:54 -08001217nAllocationSyncAll(JNIEnv *_env, jobject _this, jlong con, jlong a, jint bits)
Jason Sams5476b452010-12-08 16:14:36 -08001218{
Andreas Gampe67333922014-11-10 20:35:59 -08001219 if (kLogApi) {
1220 ALOGD("nAllocationSyncAll, con(%p), a(%p), bits(0x%08x)", (RsContext)con, (RsAllocation)a,
1221 bits);
1222 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001223 rsAllocationSyncAll((RsContext)con, (RsAllocation)a, (RsAllocationUsageType)bits);
Jason Sams5476b452010-12-08 16:14:36 -08001224}
1225
Miao Wang8c150922015-10-26 17:44:10 -07001226static void
1227nAllocationSetupBufferQueue(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint numAlloc)
1228{
1229 if (kLogApi) {
1230 ALOGD("nAllocationSetupBufferQueue, con(%p), alloc(%p), numAlloc(%d)", (RsContext)con,
1231 (RsAllocation)alloc, numAlloc);
1232 }
1233 rsAllocationSetupBufferQueue((RsContext)con, (RsAllocation)alloc, (uint32_t)numAlloc);
1234}
1235
1236static void
1237nAllocationShareBufferQueue(JNIEnv *_env, jobject _this, jlong con, jlong alloc1, jlong alloc2)
1238{
1239 if (kLogApi) {
1240 ALOGD("nAllocationShareBufferQueue, con(%p), alloc1(%p), alloc2(%p)", (RsContext)con,
1241 (RsAllocation)alloc1, (RsAllocation)alloc2);
1242 }
1243
1244 rsAllocationShareBufferQueue((RsContext)con, (RsAllocation)alloc1, (RsAllocation)alloc2);
1245}
1246
Jason Sams72226e02013-02-22 12:45:54 -08001247static jobject
Tim Murray460a0492013-11-19 12:45:54 -08001248nAllocationGetSurface(JNIEnv *_env, jobject _this, jlong con, jlong a)
Jason Sams615e7ce2012-01-13 14:01:20 -08001249{
Andreas Gampe67333922014-11-10 20:35:59 -08001250 if (kLogApi) {
1251 ALOGD("nAllocationGetSurface, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1252 }
Jason Sams615e7ce2012-01-13 14:01:20 -08001253
Andreas Gampe67333922014-11-10 20:35:59 -08001254 IGraphicBufferProducer *v = (IGraphicBufferProducer *)rsAllocationGetSurface((RsContext)con,
1255 (RsAllocation)a);
Jason Sams72226e02013-02-22 12:45:54 -08001256 sp<IGraphicBufferProducer> bp = v;
Chris Wailes488230c32014-08-14 11:22:40 -07001257 v->decStrong(nullptr);
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001258
Jason Sams72226e02013-02-22 12:45:54 -08001259 jobject o = android_view_Surface_createFromIGraphicBufferProducer(_env, bp);
1260 return o;
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001261}
1262
1263static void
Tim Murray460a0492013-11-19 12:45:54 -08001264nAllocationSetSurface(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject sur)
Jason Sams163766c2012-02-15 12:04:24 -08001265{
Andreas Gampe67333922014-11-10 20:35:59 -08001266 if (kLogApi) {
1267 ALOGD("nAllocationSetSurface, con(%p), alloc(%p), surface(%p)", (RsContext)con,
1268 (RsAllocation)alloc, (Surface *)sur);
1269 }
Jason Sams163766c2012-02-15 12:04:24 -08001270
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001271 sp<Surface> s;
Jason Sams163766c2012-02-15 12:04:24 -08001272 if (sur != 0) {
Jeff Brown64a55af2012-08-26 02:47:39 -07001273 s = android_view_Surface_getSurface(_env, sur);
Jason Sams163766c2012-02-15 12:04:24 -08001274 }
1275
Andreas Gampe67333922014-11-10 20:35:59 -08001276 rsAllocationSetSurface((RsContext)con, (RsAllocation)alloc,
1277 static_cast<ANativeWindow *>(s.get()));
Jason Sams163766c2012-02-15 12:04:24 -08001278}
1279
1280static void
Tim Murray460a0492013-11-19 12:45:54 -08001281nAllocationIoSend(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Sams163766c2012-02-15 12:04:24 -08001282{
Andreas Gampe67333922014-11-10 20:35:59 -08001283 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001284 ALOGD("nAllocationIoSend, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
Andreas Gampe67333922014-11-10 20:35:59 -08001285 }
Tim Murray460a0492013-11-19 12:45:54 -08001286 rsAllocationIoSend((RsContext)con, (RsAllocation)alloc);
Jason Sams163766c2012-02-15 12:04:24 -08001287}
1288
Miao Wang8c150922015-10-26 17:44:10 -07001289static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001290nAllocationIoReceive(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Sams163766c2012-02-15 12:04:24 -08001291{
Andreas Gampe67333922014-11-10 20:35:59 -08001292 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001293 ALOGD("nAllocationIoReceive, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
Andreas Gampe67333922014-11-10 20:35:59 -08001294 }
Miao Wang8c150922015-10-26 17:44:10 -07001295 return (jlong) rsAllocationIoReceive((RsContext)con, (RsAllocation)alloc);
Jason Sams163766c2012-02-15 12:04:24 -08001296}
1297
Jason Sams163766c2012-02-15 12:04:24 -08001298static void
Tim Murray460a0492013-11-19 12:45:54 -08001299nAllocationGenerateMipmaps(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Samsf7086092011-01-12 13:28:37 -08001300{
Andreas Gampe67333922014-11-10 20:35:59 -08001301 if (kLogApi) {
1302 ALOGD("nAllocationGenerateMipmaps, con(%p), a(%p)", (RsContext)con, (RsAllocation)alloc);
1303 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001304 rsAllocationGenerateMipmaps((RsContext)con, (RsAllocation)alloc);
Jason Samsf7086092011-01-12 13:28:37 -08001305}
1306
Tim Murray460a0492013-11-19 12:45:54 -08001307static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001308nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1309 jobject jbitmap, jint usage)
Jason Samsffe9f482009-06-01 17:45:53 -07001310{
John Recked207b92015-04-10 13:52:57 -07001311 SkBitmap bitmap;
1312 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Samsffe9f482009-06-01 17:45:53 -07001313
Jason Sams5476b452010-12-08 16:14:36 -08001314 bitmap.lockPixels();
1315 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001316 jlong id = (jlong)(uintptr_t)rsAllocationCreateFromBitmap((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001317 (RsType)type, (RsAllocationMipmapControl)mip,
1318 ptr, bitmap.getSize(), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001319 bitmap.unlockPixels();
1320 return id;
Jason Samsffe9f482009-06-01 17:45:53 -07001321}
Jason Samsfe08d992009-05-27 14:45:32 -07001322
Tim Murray460a0492013-11-19 12:45:54 -08001323static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001324nAllocationCreateBitmapBackedAllocation(JNIEnv *_env, jobject _this, jlong con, jlong type,
1325 jint mip, jobject jbitmap, jint usage)
Tim Murraya3145512012-12-04 17:59:29 -08001326{
John Recked207b92015-04-10 13:52:57 -07001327 SkBitmap bitmap;
1328 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Tim Murraya3145512012-12-04 17:59:29 -08001329
1330 bitmap.lockPixels();
1331 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001332 jlong id = (jlong)(uintptr_t)rsAllocationCreateTyped((RsContext)con,
Tim Murraya3145512012-12-04 17:59:29 -08001333 (RsType)type, (RsAllocationMipmapControl)mip,
Ashok Bhat98071552014-02-12 09:54:43 +00001334 (uint32_t)usage, (uintptr_t)ptr);
Tim Murraya3145512012-12-04 17:59:29 -08001335 bitmap.unlockPixels();
1336 return id;
1337}
1338
Tim Murray460a0492013-11-19 12:45:54 -08001339static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001340nAllocationCubeCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1341 jobject jbitmap, jint usage)
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001342{
John Recked207b92015-04-10 13:52:57 -07001343 SkBitmap bitmap;
1344 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001345
Jason Sams5476b452010-12-08 16:14:36 -08001346 bitmap.lockPixels();
1347 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001348 jlong id = (jlong)(uintptr_t)rsAllocationCubeCreateFromBitmap((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001349 (RsType)type, (RsAllocationMipmapControl)mip,
1350 ptr, bitmap.getSize(), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001351 bitmap.unlockPixels();
1352 return id;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001353}
1354
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001355static void
Tim Murray460a0492013-11-19 12:45:54 -08001356nAllocationCopyFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001357{
John Recked207b92015-04-10 13:52:57 -07001358 SkBitmap bitmap;
1359 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Samsf7086092011-01-12 13:28:37 -08001360 int w = bitmap.width();
1361 int h = bitmap.height();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001362
Jason Sams4ef66502010-12-10 16:03:15 -08001363 bitmap.lockPixels();
1364 const void* ptr = bitmap.getPixels();
Tim Murrayeff663f2013-11-15 13:08:30 -08001365 rsAllocation2DData((RsContext)con, (RsAllocation)alloc, 0, 0,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001366 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Tim Murray38faea302012-11-27 14:55:08 -08001367 w, h, ptr, bitmap.getSize(), 0);
Jason Sams4ef66502010-12-10 16:03:15 -08001368 bitmap.unlockPixels();
1369}
1370
1371static void
Tim Murray460a0492013-11-19 12:45:54 -08001372nAllocationCopyToBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Jason Sams4ef66502010-12-10 16:03:15 -08001373{
John Recked207b92015-04-10 13:52:57 -07001374 SkBitmap bitmap;
1375 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Sams4ef66502010-12-10 16:03:15 -08001376
1377 bitmap.lockPixels();
1378 void* ptr = bitmap.getPixels();
Tim Murrayeff663f2013-11-15 13:08:30 -08001379 rsAllocationCopyToBitmap((RsContext)con, (RsAllocation)alloc, ptr, bitmap.getSize());
Jason Sams4ef66502010-12-10 16:03:15 -08001380 bitmap.unlockPixels();
Alex Sakhartchouk835b8542011-07-20 14:33:10 -07001381 bitmap.notifyPixelsChanged();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001382}
1383
Stephen Hines414fa2c2014-04-17 01:02:42 -07001384// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Samsd19f10d2009-05-22 14:03:28 -07001385static void
Tim Murray460a0492013-11-19 12:45:54 -08001386nAllocationData1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001387 jint count, jobject data, jint sizeBytes, jint dataType, jint mSize,
1388 jboolean usePadding)
Jason Samsd19f10d2009-05-22 14:03:28 -07001389{
Jason Samse729a942013-11-06 11:22:02 -08001390 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001391 if (kLogApi) {
1392 ALOGD("nAllocation1DData, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1393 "dataType(%i)", (RsContext)con, (RsAllocation)alloc, offset, count, sizeBytes,
1394 dataType);
1395 }
Miao Wang87e908d2015-03-02 15:15:15 -08001396 PER_ARRAY_TYPE(nullptr, rsAllocation1DData, true,
1397 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -07001398}
1399
1400static void
Miao Wangc8e237e2015-02-20 18:36:32 -08001401nAllocationElementData(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1402 jint xoff, jint yoff, jint zoff,
1403 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Jason Sams49bdaf02010-08-31 13:50:42 -07001404{
1405 jint len = _env->GetArrayLength(data);
Andreas Gampe67333922014-11-10 20:35:59 -08001406 if (kLogApi) {
Miao Wangc8e237e2015-02-20 18:36:32 -08001407 ALOGD("nAllocationElementData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1408 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
Andreas Gampe67333922014-11-10 20:35:59 -08001409 sizeBytes);
1410 }
Chris Wailes488230c32014-08-14 11:22:40 -07001411 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001412 if (ptr == nullptr) {
1413 ALOGE("Failed to get Java array elements");
1414 return;
1415 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001416 rsAllocationElementData((RsContext)con, (RsAllocation)alloc,
1417 xoff, yoff, zoff,
1418 lod, ptr, sizeBytes, compIdx);
Jason Sams49bdaf02010-08-31 13:50:42 -07001419 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1420}
1421
Miao Wangc8e237e2015-02-20 18:36:32 -08001422
Stephen Hines414fa2c2014-04-17 01:02:42 -07001423// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Sams49bdaf02010-08-31 13:50:42 -07001424static void
Tim Murray460a0492013-11-19 12:45:54 -08001425nAllocationData2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001426 jint w, jint h, jobject data, jint sizeBytes, jint dataType, jint mSize,
1427 jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001428{
Jason Samse729a942013-11-06 11:22:02 -08001429 RsAllocation *alloc = (RsAllocation *)_alloc;
1430 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001431 if (kLogApi) {
1432 ALOGD("nAllocation2DData, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1433 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1434 }
Miao Wang87e908d2015-03-02 15:15:15 -08001435 int count = w * h;
1436 PER_ARRAY_TYPE(nullptr, rsAllocation2DData, true,
1437 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Samsd19f10d2009-05-22 14:03:28 -07001438}
1439
Stephen Hines414fa2c2014-04-17 01:02:42 -07001440// Copies from the Allocation pointed to by srcAlloc into the Allocation
1441// pointed to by dstAlloc.
Jason Sams40a29e82009-08-10 14:55:26 -07001442static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001443nAllocationData2D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001444 jlong dstAlloc, jint dstXoff, jint dstYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001445 jint dstMip, jint dstFace,
1446 jint width, jint height,
Tim Murray460a0492013-11-19 12:45:54 -08001447 jlong srcAlloc, jint srcXoff, jint srcYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001448 jint srcMip, jint srcFace)
1449{
Andreas Gampe67333922014-11-10 20:35:59 -08001450 if (kLogApi) {
1451 ALOGD("nAllocation2DData_s, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1452 " dstMip(%i), dstFace(%i), width(%i), height(%i),"
1453 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i), srcFace(%i)",
1454 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip, dstFace,
1455 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
1456 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001457
Tim Murrayeff663f2013-11-15 13:08:30 -08001458 rsAllocationCopy2DRange((RsContext)con,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001459 (RsAllocation)dstAlloc,
1460 dstXoff, dstYoff,
1461 dstMip, dstFace,
1462 width, height,
1463 (RsAllocation)srcAlloc,
1464 srcXoff, srcYoff,
1465 srcMip, srcFace);
1466}
1467
Stephen Hines414fa2c2014-04-17 01:02:42 -07001468// Copies from the Java object data into the Allocation pointed to by _alloc.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001469static void
Tim Murray460a0492013-11-19 12:45:54 -08001470nAllocationData3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001471 jint w, jint h, jint d, jobject data, jint sizeBytes, jint dataType,
1472 jint mSize, jboolean usePadding)
Jason Samsb05d6892013-04-09 15:59:24 -07001473{
Jason Samse729a942013-11-06 11:22:02 -08001474 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001475 if (kLogApi) {
1476 ALOGD("nAllocation3DData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1477 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1478 lod, w, h, d, sizeBytes);
1479 }
Miao Wang87e908d2015-03-02 15:15:15 -08001480 int count = w * h * d;
1481 PER_ARRAY_TYPE(nullptr, rsAllocation3DData, true,
1482 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Jason Samsb05d6892013-04-09 15:59:24 -07001483}
1484
Stephen Hines414fa2c2014-04-17 01:02:42 -07001485// Copies from the Allocation pointed to by srcAlloc into the Allocation
1486// pointed to by dstAlloc.
Jason Samsb05d6892013-04-09 15:59:24 -07001487static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001488nAllocationData3D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001489 jlong dstAlloc, jint dstXoff, jint dstYoff, jint dstZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001490 jint dstMip,
1491 jint width, jint height, jint depth,
Tim Murray460a0492013-11-19 12:45:54 -08001492 jlong srcAlloc, jint srcXoff, jint srcYoff, jint srcZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001493 jint srcMip)
1494{
Andreas Gampe67333922014-11-10 20:35:59 -08001495 if (kLogApi) {
1496 ALOGD("nAllocationData3D_alloc, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1497 " dstMip(%i), width(%i), height(%i),"
1498 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i)",
1499 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip,
1500 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip);
1501 }
Jason Samsb05d6892013-04-09 15:59:24 -07001502
Tim Murrayeff663f2013-11-15 13:08:30 -08001503 rsAllocationCopy3DRange((RsContext)con,
Jason Samsb05d6892013-04-09 15:59:24 -07001504 (RsAllocation)dstAlloc,
1505 dstXoff, dstYoff, dstZoff, dstMip,
1506 width, height, depth,
1507 (RsAllocation)srcAlloc,
1508 srcXoff, srcYoff, srcZoff, srcMip);
1509}
1510
Jason Sams21659ac2013-11-06 15:08:07 -08001511
Stephen Hines414fa2c2014-04-17 01:02:42 -07001512// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsb05d6892013-04-09 15:59:24 -07001513static void
Miao Wang87e908d2015-03-02 15:15:15 -08001514nAllocationRead(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jobject data, jint dataType,
1515 jint mSize, jboolean usePadding)
Jason Sams40a29e82009-08-10 14:55:26 -07001516{
Jason Sams21659ac2013-11-06 15:08:07 -08001517 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001518 if (kLogApi) {
1519 ALOGD("nAllocationRead, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
1520 }
Miao Wang87e908d2015-03-02 15:15:15 -08001521 int count = 0;
1522 PER_ARRAY_TYPE(0, rsAllocationRead, false,
1523 (RsContext)con, alloc, ptr, len * typeBytes);
Jason Sams40a29e82009-08-10 14:55:26 -07001524}
1525
Stephen Hines414fa2c2014-04-17 01:02:42 -07001526// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Sams40a29e82009-08-10 14:55:26 -07001527static void
Tim Murray460a0492013-11-19 12:45:54 -08001528nAllocationRead1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001529 jint count, jobject data, jint sizeBytes, jint dataType,
1530 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001531{
Jason Sams21659ac2013-11-06 15:08:07 -08001532 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001533 if (kLogApi) {
1534 ALOGD("nAllocation1DRead, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1535 "dataType(%i)", (RsContext)con, alloc, offset, count, sizeBytes, dataType);
1536 }
Miao Wang87e908d2015-03-02 15:15:15 -08001537 PER_ARRAY_TYPE(0, rsAllocation1DRead, false,
1538 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsfb9f82c2011-01-12 14:53:25 -08001539}
1540
Miao Wangc8e237e2015-02-20 18:36:32 -08001541// Copies from the Element in the Allocation pointed to by _alloc into the Java array data.
1542static void
Miao Wang45cec0a2015-03-04 16:40:21 -08001543nAllocationElementRead(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
Miao Wangc8e237e2015-02-20 18:36:32 -08001544 jint xoff, jint yoff, jint zoff,
Miao Wang45cec0a2015-03-04 16:40:21 -08001545 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Miao Wangc8e237e2015-02-20 18:36:32 -08001546{
Miao Wang45cec0a2015-03-04 16:40:21 -08001547 jint len = _env->GetArrayLength(data);
Miao Wangc8e237e2015-02-20 18:36:32 -08001548 if (kLogApi) {
Miao Wang45cec0a2015-03-04 16:40:21 -08001549 ALOGD("nAllocationElementRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1550 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
1551 sizeBytes);
Miao Wangc8e237e2015-02-20 18:36:32 -08001552 }
Miao Wang45cec0a2015-03-04 16:40:21 -08001553 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001554 if (ptr == nullptr) {
1555 ALOGE("Failed to get Java array elements");
1556 return;
1557 }
Miao Wang45cec0a2015-03-04 16:40:21 -08001558 rsAllocationElementRead((RsContext)con, (RsAllocation)alloc,
1559 xoff, yoff, zoff,
Jason Samsa7e25092015-03-11 11:00:00 -07001560 lod, ptr, sizeBytes, compIdx);
Miao Wangbfa5e652015-05-04 15:29:25 -07001561 _env->ReleaseByteArrayElements(data, ptr, 0);
Miao Wangc8e237e2015-02-20 18:36:32 -08001562}
1563
Stephen Hines414fa2c2014-04-17 01:02:42 -07001564// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsfb9f82c2011-01-12 14:53:25 -08001565static void
Tim Murray460a0492013-11-19 12:45:54 -08001566nAllocationRead2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001567 jint w, jint h, jobject data, jint sizeBytes, jint dataType,
1568 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001569{
Jason Sams21659ac2013-11-06 15:08:07 -08001570 RsAllocation *alloc = (RsAllocation *)_alloc;
1571 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001572 if (kLogApi) {
1573 ALOGD("nAllocation2DRead, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1574 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1575 }
Miao Wang87e908d2015-03-02 15:15:15 -08001576 int count = w * h;
1577 PER_ARRAY_TYPE(0, rsAllocation2DRead, false,
1578 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Sams40a29e82009-08-10 14:55:26 -07001579}
Miao Wang87e908d2015-03-02 15:15:15 -08001580
Miao Wangc8e237e2015-02-20 18:36:32 -08001581// Copies from the Allocation pointed to by _alloc into the Java object data.
1582static void
1583nAllocationRead3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001584 jint w, jint h, jint d, jobject data, int sizeBytes, int dataType,
1585 jint mSize, jboolean usePadding)
Miao Wangc8e237e2015-02-20 18:36:32 -08001586{
1587 RsAllocation *alloc = (RsAllocation *)_alloc;
1588 if (kLogApi) {
1589 ALOGD("nAllocation3DRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1590 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1591 lod, w, h, d, sizeBytes);
1592 }
Miao Wang87e908d2015-03-02 15:15:15 -08001593 int count = w * h * d;
1594 PER_ARRAY_TYPE(nullptr, rsAllocation3DRead, false,
1595 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Miao Wangc8e237e2015-02-20 18:36:32 -08001596}
Jason Samsd19f10d2009-05-22 14:03:28 -07001597
Tim Murray460a0492013-11-19 12:45:54 -08001598static jlong
1599nAllocationGetType(JNIEnv *_env, jobject _this, jlong con, jlong a)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001600{
Andreas Gampe67333922014-11-10 20:35:59 -08001601 if (kLogApi) {
1602 ALOGD("nAllocationGetType, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1603 }
Tim Murray3aa89c12014-08-18 17:51:22 -07001604 return (jlong)(uintptr_t) rsaAllocationGetType((RsContext)con, (RsAllocation)a);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001605}
1606
Jason Sams5edc6082010-10-05 13:32:49 -07001607static void
Tim Murray460a0492013-11-19 12:45:54 -08001608nAllocationResize1D(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint dimX)
Jason Sams5edc6082010-10-05 13:32:49 -07001609{
Andreas Gampe67333922014-11-10 20:35:59 -08001610 if (kLogApi) {
1611 ALOGD("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", (RsContext)con,
1612 (RsAllocation)alloc, dimX);
1613 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001614 rsAllocationResize1D((RsContext)con, (RsAllocation)alloc, dimX);
Jason Sams5edc6082010-10-05 13:32:49 -07001615}
1616
Jason Sams46ba27e32015-02-06 17:45:15 -08001617
1618static jlong
1619nAllocationAdapterCreate(JNIEnv *_env, jobject _this, jlong con, jlong basealloc, jlong type)
1620{
1621 if (kLogApi) {
1622 ALOGD("nAllocationAdapterCreate, con(%p), base(%p), type(%p)",
1623 (RsContext)con, (RsAllocation)basealloc, (RsElement)type);
1624 }
1625 return (jlong)(uintptr_t) rsAllocationAdapterCreate((RsContext)con, (RsType)type,
1626 (RsAllocation)basealloc);
1627
1628}
1629
1630static void
1631nAllocationAdapterOffset(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1632 jint x, jint y, jint z, jint face, jint lod,
1633 jint a1, jint a2, jint a3, jint a4)
1634{
1635 uint32_t params[] = {
1636 (uint32_t)x, (uint32_t)y, (uint32_t)z, (uint32_t)face,
1637 (uint32_t)lod, (uint32_t)a1, (uint32_t)a2, (uint32_t)a3, (uint32_t)a4
1638 };
1639 if (kLogApi) {
1640 ALOGD("nAllocationAdapterOffset, con(%p), alloc(%p), x(%i), y(%i), z(%i), face(%i), lod(%i), arrays(%i %i %i %i)",
1641 (RsContext)con, (RsAllocation)alloc, x, y, z, face, lod, a1, a2, a3, a4);
1642 }
1643 rsAllocationAdapterOffset((RsContext)con, (RsAllocation)alloc,
1644 params, sizeof(params));
1645}
1646
1647
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001648// -----------------------------------
1649
Tim Murray460a0492013-11-19 12:45:54 -08001650static jlong
1651nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con, jlong native_asset)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001652{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001653 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001654 ALOGV("______nFileA3D %p", asset);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001655
Tim Murray3aa89c12014-08-18 17:51:22 -07001656 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromMemory((RsContext)con, asset->getBuffer(false), asset->getLength());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001657 return id;
1658}
1659
Tim Murray460a0492013-11-19 12:45:54 -08001660static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001661nFileA3DCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001662{
1663 AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001664 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001665 return 0;
1666 }
1667
1668 AutoJavaStringToUTF8 str(_env, _path);
1669 Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
Chris Wailes488230c32014-08-14 11:22:40 -07001670 if (asset == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001671 return 0;
1672 }
1673
Tim Murray3aa89c12014-08-18 17:51:22 -07001674 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromAsset((RsContext)con, asset);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001675 return id;
1676}
1677
Tim Murray460a0492013-11-19 12:45:54 -08001678static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001679nFileA3DCreateFromFile(JNIEnv *_env, jobject _this, jlong con, jstring fileName)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001680{
1681 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001682 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromFile((RsContext)con, fileNameUTF.c_str());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001683
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001684 return id;
1685}
1686
Tim Murray460a0492013-11-19 12:45:54 -08001687static jint
1688nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001689{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001690 int32_t numEntries = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001691 rsaFileA3DGetNumIndexEntries((RsContext)con, &numEntries, (RsFile)fileA3D);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001692 return (jint)numEntries;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001693}
1694
1695static void
Tim Murray460a0492013-11-19 12:45:54 -08001696nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001697{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001698 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001699 RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
1700
Tim Murrayeff663f2013-11-15 13:08:30 -08001701 rsaFileA3DGetIndexEntries((RsContext)con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001702
1703 for(jint i = 0; i < numEntries; i ++) {
1704 _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
1705 _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
1706 }
1707
1708 free(fileEntries);
1709}
1710
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001711static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001712nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint index)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001713{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001714 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Tim Murray3aa89c12014-08-18 17:51:22 -07001715 jlong id = (jlong)(uintptr_t)rsaFileA3DGetEntryByIndex((RsContext)con, (uint32_t)index, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001716 return id;
1717}
Jason Samsd19f10d2009-05-22 14:03:28 -07001718
1719// -----------------------------------
1720
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001721static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001722nFontCreateFromFile(JNIEnv *_env, jobject _this, jlong con,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001723 jstring fileName, jfloat fontSize, jint dpi)
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001724{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001725 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001726 jlong id = (jlong)(uintptr_t)rsFontCreateFromFile((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001727 fileNameUTF.c_str(), fileNameUTF.length(),
1728 fontSize, dpi);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001729
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001730 return id;
1731}
1732
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001733static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001734nFontCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con,
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001735 jstring name, jfloat fontSize, jint dpi, jlong native_asset)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001736{
1737 Asset* asset = reinterpret_cast<Asset*>(native_asset);
1738 AutoJavaStringToUTF8 nameUTF(_env, name);
1739
Tim Murray3aa89c12014-08-18 17:51:22 -07001740 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001741 nameUTF.c_str(), nameUTF.length(),
1742 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001743 asset->getBuffer(false), asset->getLength());
1744 return id;
1745}
1746
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001747static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001748nFontCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001749 jfloat fontSize, jint dpi)
1750{
1751 AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001752 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001753 return 0;
1754 }
1755
1756 AutoJavaStringToUTF8 str(_env, _path);
1757 Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
Chris Wailes488230c32014-08-14 11:22:40 -07001758 if (asset == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001759 return 0;
1760 }
1761
Tim Murray3aa89c12014-08-18 17:51:22 -07001762 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001763 str.c_str(), str.length(),
1764 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001765 asset->getBuffer(false), asset->getLength());
1766 delete asset;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001767 return id;
1768}
1769
Jason Samsbd1c3ad2009-08-03 16:03:08 -07001770// -----------------------------------
1771
1772static void
Tim Murray460a0492013-11-19 12:45:54 -08001773nScriptBindAllocation(JNIEnv *_env, jobject _this, jlong con, jlong script, jlong alloc, jint slot)
Jason Samsd19f10d2009-05-22 14:03:28 -07001774{
Andreas Gampe67333922014-11-10 20:35:59 -08001775 if (kLogApi) {
1776 ALOGD("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", (RsContext)con,
1777 (RsScript)script, (RsAllocation)alloc, slot);
1778 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001779 rsScriptBindAllocation((RsContext)con, (RsScript)script, (RsAllocation)alloc, slot);
Jason Samsd19f10d2009-05-22 14:03:28 -07001780}
1781
1782static void
Tim Murray460a0492013-11-19 12:45:54 -08001783nScriptSetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jint val)
Jason Sams4d339932010-05-11 14:03:58 -07001784{
Andreas Gampe67333922014-11-10 20:35:59 -08001785 if (kLogApi) {
1786 ALOGD("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", (RsContext)con, (void *)script,
1787 slot, val);
1788 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001789 rsScriptSetVarI((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001790}
1791
Tim Murray7c4caad2013-04-10 16:21:40 -07001792static jint
Tim Murray460a0492013-11-19 12:45:54 -08001793nScriptGetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001794{
Andreas Gampe67333922014-11-10 20:35:59 -08001795 if (kLogApi) {
1796 ALOGD("nScriptGetVarI, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1797 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001798 int value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001799 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001800 return value;
1801}
1802
Jason Sams4d339932010-05-11 14:03:58 -07001803static void
Tim Murray460a0492013-11-19 12:45:54 -08001804nScriptSetVarObj(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001805{
Andreas Gampe67333922014-11-10 20:35:59 -08001806 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001807 ALOGD("nScriptSetVarObj, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001808 slot, val);
1809 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001810 rsScriptSetVarObj((RsContext)con, (RsScript)script, slot, (RsObjectBase)val);
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001811}
1812
1813static void
Tim Murray460a0492013-11-19 12:45:54 -08001814nScriptSetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Stephen Hines031ec58c2010-10-11 10:54:21 -07001815{
Andreas Gampe67333922014-11-10 20:35:59 -08001816 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001817 ALOGD("nScriptSetVarJ, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001818 slot, val);
1819 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001820 rsScriptSetVarJ((RsContext)con, (RsScript)script, slot, val);
Stephen Hines031ec58c2010-10-11 10:54:21 -07001821}
1822
Tim Murray7c4caad2013-04-10 16:21:40 -07001823static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001824nScriptGetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001825{
Andreas Gampe67333922014-11-10 20:35:59 -08001826 if (kLogApi) {
1827 ALOGD("nScriptGetVarJ, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1828 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001829 jlong value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001830 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001831 return value;
1832}
1833
Stephen Hines031ec58c2010-10-11 10:54:21 -07001834static void
Tim Murray460a0492013-11-19 12:45:54 -08001835nScriptSetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, float val)
Jason Sams4d339932010-05-11 14:03:58 -07001836{
Andreas Gampe67333922014-11-10 20:35:59 -08001837 if (kLogApi) {
1838 ALOGD("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", (RsContext)con, (void *)script,
1839 slot, val);
1840 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001841 rsScriptSetVarF((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001842}
1843
Tim Murray7c4caad2013-04-10 16:21:40 -07001844static jfloat
Tim Murray460a0492013-11-19 12:45:54 -08001845nScriptGetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001846{
Andreas Gampe67333922014-11-10 20:35:59 -08001847 if (kLogApi) {
1848 ALOGD("nScriptGetVarF, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1849 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001850 jfloat value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001851 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001852 return value;
1853}
1854
Jason Sams4d339932010-05-11 14:03:58 -07001855static void
Tim Murray460a0492013-11-19 12:45:54 -08001856nScriptSetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, double val)
Stephen Hinesca54ec32010-09-20 17:20:30 -07001857{
Andreas Gampe67333922014-11-10 20:35:59 -08001858 if (kLogApi) {
1859 ALOGD("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", (RsContext)con, (void *)script,
1860 slot, val);
1861 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001862 rsScriptSetVarD((RsContext)con, (RsScript)script, slot, val);
Stephen Hinesca54ec32010-09-20 17:20:30 -07001863}
1864
Tim Murray7c4caad2013-04-10 16:21:40 -07001865static jdouble
Tim Murray460a0492013-11-19 12:45:54 -08001866nScriptGetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001867{
Andreas Gampe67333922014-11-10 20:35:59 -08001868 if (kLogApi) {
1869 ALOGD("nScriptGetVarD, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1870 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001871 jdouble value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001872 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001873 return value;
1874}
1875
Stephen Hinesca54ec32010-09-20 17:20:30 -07001876static void
Tim Murray460a0492013-11-19 12:45:54 -08001877nScriptSetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001878{
Andreas Gampe67333922014-11-10 20:35:59 -08001879 if (kLogApi) {
1880 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1881 }
Jason Sams4d339932010-05-11 14:03:58 -07001882 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001883 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001884 if (ptr == nullptr) {
1885 ALOGE("Failed to get Java array elements");
1886 return;
1887 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001888 rsScriptSetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001889 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1890}
1891
Stephen Hinesadeb8092012-04-20 14:26:06 -07001892static void
Tim Murray460a0492013-11-19 12:45:54 -08001893nScriptGetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Tim Murray7c4caad2013-04-10 16:21:40 -07001894{
Andreas Gampe67333922014-11-10 20:35:59 -08001895 if (kLogApi) {
1896 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1897 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001898 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001899 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001900 if (ptr == nullptr) {
1901 ALOGE("Failed to get Java array elements");
1902 return;
1903 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001904 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Stephen Hines414fa2c2014-04-17 01:02:42 -07001905 _env->ReleaseByteArrayElements(data, ptr, 0);
Tim Murray7c4caad2013-04-10 16:21:40 -07001906}
1907
1908static void
Andreas Gampe67333922014-11-10 20:35:59 -08001909nScriptSetVarVE(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data,
1910 jlong elem, jintArray dims)
Stephen Hinesadeb8092012-04-20 14:26:06 -07001911{
Andreas Gampe67333922014-11-10 20:35:59 -08001912 if (kLogApi) {
1913 ALOGD("nScriptSetVarVE, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1914 }
Stephen Hinesadeb8092012-04-20 14:26:06 -07001915 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001916 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001917 if (ptr == nullptr) {
1918 ALOGE("Failed to get Java array elements");
1919 return;
1920 }
Stephen Hinesadeb8092012-04-20 14:26:06 -07001921 jint dimsLen = _env->GetArrayLength(dims) * sizeof(int);
Chris Wailes488230c32014-08-14 11:22:40 -07001922 jint *dimsPtr = _env->GetIntArrayElements(dims, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001923 if (dimsPtr == nullptr) {
1924 ALOGE("Failed to get Java array elements");
1925 return;
1926 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001927 rsScriptSetVarVE((RsContext)con, (RsScript)script, slot, ptr, len, (RsElement)elem,
Stephen Hinesbc5d3ee2014-06-25 00:03:39 -07001928 (const uint32_t*) dimsPtr, dimsLen);
Stephen Hinesadeb8092012-04-20 14:26:06 -07001929 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1930 _env->ReleaseIntArrayElements(dims, dimsPtr, JNI_ABORT);
1931}
1932
Jason Samsd19f10d2009-05-22 14:03:28 -07001933
1934static void
Tim Murray460a0492013-11-19 12:45:54 -08001935nScriptSetTimeZone(JNIEnv *_env, jobject _this, jlong con, jlong script, jbyteArray timeZone)
Jason Samsd19f10d2009-05-22 14:03:28 -07001936{
Andreas Gampe67333922014-11-10 20:35:59 -08001937 if (kLogApi) {
1938 ALOGD("nScriptCSetTimeZone, con(%p), s(%p)", (RsContext)con, (void *)script);
1939 }
Romain Guy584a3752009-07-30 18:45:01 -07001940
1941 jint length = _env->GetArrayLength(timeZone);
1942 jbyte* timeZone_ptr;
1943 timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
Miao Wangba8766c2015-10-12 17:24:13 -07001944 if (timeZone_ptr == nullptr) {
1945 ALOGE("Failed to get Java array elements");
1946 return;
1947 }
Romain Guy584a3752009-07-30 18:45:01 -07001948
Tim Murrayeff663f2013-11-15 13:08:30 -08001949 rsScriptSetTimeZone((RsContext)con, (RsScript)script, (const char *)timeZone_ptr, length);
Romain Guy584a3752009-07-30 18:45:01 -07001950
1951 if (timeZone_ptr) {
1952 _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
1953 }
1954}
1955
Jason Samsfbf0b9e2009-08-13 12:59:04 -07001956static void
Tim Murray460a0492013-11-19 12:45:54 -08001957nScriptInvoke(JNIEnv *_env, jobject _this, jlong con, jlong obj, jint slot)
Jason Samsbe2e8412009-09-16 15:04:38 -07001958{
Andreas Gampe67333922014-11-10 20:35:59 -08001959 if (kLogApi) {
1960 ALOGD("nScriptInvoke, con(%p), script(%p)", (RsContext)con, (void *)obj);
1961 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001962 rsScriptInvoke((RsContext)con, (RsScript)obj, slot);
Jason Samsbe2e8412009-09-16 15:04:38 -07001963}
1964
1965static void
Tim Murray460a0492013-11-19 12:45:54 -08001966nScriptInvokeV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001967{
Andreas Gampe67333922014-11-10 20:35:59 -08001968 if (kLogApi) {
1969 ALOGD("nScriptInvokeV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1970 }
Jason Sams4d339932010-05-11 14:03:58 -07001971 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001972 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001973 if (ptr == nullptr) {
1974 ALOGE("Failed to get Java array elements");
1975 return;
1976 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001977 rsScriptInvokeV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001978 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1979}
1980
Jason Sams6e494d32011-04-27 16:33:11 -07001981static void
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001982nScriptForEach(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
1983 jlongArray ains, jlong aout, jbyteArray params,
1984 jintArray limits)
Jason Sams6e494d32011-04-27 16:33:11 -07001985{
Andreas Gampe67333922014-11-10 20:35:59 -08001986 if (kLogApi) {
Chih-Hung Hsieh9eb9dd32015-05-06 14:42:04 -07001987 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 -08001988 }
Jason Sams6e494d32011-04-27 16:33:11 -07001989
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001990 jint in_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07001991 jlong *in_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07001992
Chris Wailes488230c32014-08-14 11:22:40 -07001993 RsAllocation *in_allocs = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07001994
Chris Wailes488230c32014-08-14 11:22:40 -07001995 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001996 in_len = _env->GetArrayLength(ains);
Yang Ni7b2a46f2015-05-05 12:41:19 -07001997 if (in_len > (jint)RS_KERNEL_MAX_ARGUMENTS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -07001998 ALOGE("Too many arguments in kernel launch.");
1999 // TODO (b/20758983): Report back to Java and throw an exception
2000 return;
2001 }
Chris Wailes94961062014-06-11 12:01:28 -07002002
Yang Ni17c2d7a2015-04-30 16:13:54 -07002003 in_ptr = _env->GetLongArrayElements(ains, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002004 if (in_ptr == nullptr) {
2005 ALOGE("Failed to get Java array elements");
2006 return;
2007 }
2008
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002009 if (sizeof(RsAllocation) == sizeof(jlong)) {
2010 in_allocs = (RsAllocation*)in_ptr;
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002011 } else {
2012 // Convert from 64-bit jlong types to the native pointer type.
Chris Wailes94961062014-06-11 12:01:28 -07002013
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002014 in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
Yang Ni17c2d7a2015-04-30 16:13:54 -07002015 if (in_allocs == nullptr) {
2016 ALOGE("Failed launching kernel for lack of memory.");
2017 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2018 return;
2019 }
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002020
2021 for (int index = in_len; --index >= 0;) {
2022 in_allocs[index] = (RsAllocation)in_ptr[index];
2023 }
2024 }
Chris Wailes94961062014-06-11 12:01:28 -07002025 }
2026
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002027 jint param_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002028 jbyte *param_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002029
Chris Wailes488230c32014-08-14 11:22:40 -07002030 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002031 param_len = _env->GetArrayLength(params);
Chris Wailes488230c32014-08-14 11:22:40 -07002032 param_ptr = _env->GetByteArrayElements(params, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002033 if (param_ptr == nullptr) {
2034 ALOGE("Failed to get Java array elements");
2035 return;
2036 }
Chris Wailes94961062014-06-11 12:01:28 -07002037 }
2038
Chris Wailes488230c32014-08-14 11:22:40 -07002039 RsScriptCall sc, *sca = nullptr;
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002040 uint32_t sc_size = 0;
Chris Wailes94961062014-06-11 12:01:28 -07002041
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002042 jint limit_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002043 jint *limit_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002044
Chris Wailes488230c32014-08-14 11:22:40 -07002045 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002046 limit_len = _env->GetArrayLength(limits);
Chris Wailes488230c32014-08-14 11:22:40 -07002047 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002048 if (limit_ptr == nullptr) {
2049 ALOGE("Failed to get Java array elements");
2050 return;
2051 }
Chris Wailes94961062014-06-11 12:01:28 -07002052
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002053 assert(limit_len == 6);
Andreas Gampe67333922014-11-10 20:35:59 -08002054 UNUSED(limit_len); // As the assert might not be compiled.
Chris Wailes94961062014-06-11 12:01:28 -07002055
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002056 sc.xStart = limit_ptr[0];
2057 sc.xEnd = limit_ptr[1];
2058 sc.yStart = limit_ptr[2];
2059 sc.yEnd = limit_ptr[3];
2060 sc.zStart = limit_ptr[4];
2061 sc.zEnd = limit_ptr[5];
2062 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
Jason Sams14331ab2015-01-26 18:14:36 -08002063 sc.arrayStart = 0;
2064 sc.arrayEnd = 0;
2065 sc.array2Start = 0;
2066 sc.array2End = 0;
2067 sc.array3Start = 0;
2068 sc.array3End = 0;
2069 sc.array4Start = 0;
2070 sc.array4End = 0;
Chris Wailes94961062014-06-11 12:01:28 -07002071
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002072 sca = &sc;
Chris Wailes94961062014-06-11 12:01:28 -07002073 }
2074
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002075 rsScriptForEachMulti((RsContext)con, (RsScript)script, slot,
2076 in_allocs, in_len, (RsAllocation)aout,
2077 param_ptr, param_len, sca, sc_size);
Chris Wailes94961062014-06-11 12:01:28 -07002078
Chris Wailes488230c32014-08-14 11:22:40 -07002079 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002080 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
Chris Wailes94961062014-06-11 12:01:28 -07002081 }
2082
Chris Wailes488230c32014-08-14 11:22:40 -07002083 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002084 _env->ReleaseByteArrayElements(params, param_ptr, JNI_ABORT);
2085 }
2086
Chris Wailes488230c32014-08-14 11:22:40 -07002087 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002088 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
2089 }
Chris Wailes94961062014-06-11 12:01:28 -07002090}
2091
Matt Wala36eb1f72015-07-20 15:35:27 -07002092static void
2093nScriptReduce(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
2094 jlong ain, jlong aout, jintArray limits)
2095{
2096 if (kLogApi) {
2097 ALOGD("nScriptReduce, con(%p), s(%p), slot(%i) ain(%" PRId64 ") aout(%" PRId64 ")", (RsContext)con, (void *)script, slot, ain, aout);
2098 }
2099
2100 RsScriptCall sc, *sca = nullptr;
2101 uint32_t sc_size = 0;
2102
2103 jint limit_len = 0;
2104 jint *limit_ptr = nullptr;
2105
2106 // If the caller passed limits, reflect them in the RsScriptCall.
2107 if (limits != nullptr) {
2108 limit_len = _env->GetArrayLength(limits);
2109 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002110 if (limit_ptr == nullptr) {
2111 ALOGE("Failed to get Java array elements");
2112 return;
2113 }
Matt Wala36eb1f72015-07-20 15:35:27 -07002114
2115 // We expect to be passed an array [x1, x2] which specifies
2116 // the sub-range for a 1-dimensional reduction.
2117 assert(limit_len == 2);
2118 UNUSED(limit_len); // As the assert might not be compiled.
2119
2120 sc.xStart = limit_ptr[0];
2121 sc.xEnd = limit_ptr[1];
2122 sc.yStart = 0;
2123 sc.yEnd = 0;
2124 sc.zStart = 0;
2125 sc.zEnd = 0;
2126 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
2127 sc.arrayStart = 0;
2128 sc.arrayEnd = 0;
2129 sc.array2Start = 0;
2130 sc.array2End = 0;
2131 sc.array3Start = 0;
2132 sc.array3End = 0;
2133 sc.array4Start = 0;
2134 sc.array4End = 0;
2135
2136 sca = &sc;
2137 sc_size = sizeof(sc);
2138 }
2139
2140 rsScriptReduce((RsContext)con, (RsScript)script, slot,
2141 (RsAllocation)ain, (RsAllocation)aout,
2142 sca, sc_size);
2143
2144 if (limits != nullptr) {
2145 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
2146 }
2147}
2148
David Gross26ef7a732016-01-12 12:19:15 -08002149static void
2150nScriptReduceNew(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
2151 jlongArray ains, jlong aout, jintArray limits)
2152{
2153 if (kLogApi) {
2154 ALOGD("nScriptReduceNew, con(%p), s(%p), slot(%i) ains(%p) aout(%" PRId64 ")", (RsContext)con, (void *)script, slot, ains, aout);
2155 }
2156
2157 if (ains == nullptr) {
2158 ALOGE("At least one input required.");
2159 // TODO (b/20758983): Report back to Java and throw an exception
2160 return;
2161 }
2162 jint in_len = _env->GetArrayLength(ains);
2163 if (in_len > (jint)RS_KERNEL_MAX_ARGUMENTS) {
2164 ALOGE("Too many arguments in kernel launch.");
2165 // TODO (b/20758983): Report back to Java and throw an exception
2166 return;
2167 }
2168
2169 jlong *in_ptr = _env->GetLongArrayElements(ains, nullptr);
2170 if (in_ptr == nullptr) {
2171 ALOGE("Failed to get Java array elements");
2172 // TODO (b/20758983): Report back to Java and throw an exception
2173 return;
2174 }
2175
2176 RsAllocation *in_allocs = nullptr;
2177 if (sizeof(RsAllocation) == sizeof(jlong)) {
2178 in_allocs = (RsAllocation*)in_ptr;
2179 } else {
2180 // Convert from 64-bit jlong types to the native pointer type.
2181
2182 in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
2183 if (in_allocs == nullptr) {
2184 ALOGE("Failed launching kernel for lack of memory.");
2185 // TODO (b/20758983): Report back to Java and throw an exception
2186 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2187 return;
2188 }
2189
2190 for (int index = in_len; --index >= 0;) {
2191 in_allocs[index] = (RsAllocation)in_ptr[index];
2192 }
2193 }
2194
2195 RsScriptCall sc, *sca = nullptr;
2196 uint32_t sc_size = 0;
2197
2198 jint limit_len = 0;
2199 jint *limit_ptr = nullptr;
2200
2201 if (limits != nullptr) {
2202 limit_len = _env->GetArrayLength(limits);
2203 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
2204 if (limit_ptr == nullptr) {
2205 ALOGE("Failed to get Java array elements");
2206 // TODO (b/20758983): Report back to Java and throw an exception
2207 return;
2208 }
2209
2210 assert(limit_len == 6);
2211 UNUSED(limit_len); // As the assert might not be compiled.
2212
2213 sc.xStart = limit_ptr[0];
2214 sc.xEnd = limit_ptr[1];
2215 sc.yStart = limit_ptr[2];
2216 sc.yEnd = limit_ptr[3];
2217 sc.zStart = limit_ptr[4];
2218 sc.zEnd = limit_ptr[5];
2219 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
2220 sc.arrayStart = 0;
2221 sc.arrayEnd = 0;
2222 sc.array2Start = 0;
2223 sc.array2End = 0;
2224 sc.array3Start = 0;
2225 sc.array3End = 0;
2226 sc.array4Start = 0;
2227 sc.array4End = 0;
2228
2229 sca = &sc;
2230 sc_size = sizeof(sc);
2231 }
2232
2233 rsScriptReduceNew((RsContext)con, (RsScript)script, slot,
2234 in_allocs, in_len, (RsAllocation)aout,
2235 sca, sc_size);
2236
2237 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2238
2239 if (limits != nullptr) {
2240 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
2241 }
2242}
2243
Jason Sams22534172009-08-04 16:58:20 -07002244// -----------------------------------
2245
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002246static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002247nScriptCCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Samse4a06c52011-03-16 16:29:28 -07002248 jstring resName, jstring cacheDir,
2249 jbyteArray scriptRef, jint length)
Jason Sams22534172009-08-04 16:58:20 -07002250{
Andreas Gampe67333922014-11-10 20:35:59 -08002251 if (kLogApi) {
2252 ALOGD("nScriptCCreate, con(%p)", (RsContext)con);
2253 }
Jason Sams22534172009-08-04 16:58:20 -07002254
Jason Samse4a06c52011-03-16 16:29:28 -07002255 AutoJavaStringToUTF8 resNameUTF(_env, resName);
2256 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002257 jlong ret = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002258 jbyte* script_ptr = nullptr;
Jack Palevich43702d82009-05-28 13:38:16 -07002259 jint _exception = 0;
2260 jint remaining;
Jack Palevich43702d82009-05-28 13:38:16 -07002261 if (!scriptRef) {
2262 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002263 //jniThrowException(_env, "java/lang/IllegalArgumentException", "script == null");
Jack Palevich43702d82009-05-28 13:38:16 -07002264 goto exit;
2265 }
Jack Palevich43702d82009-05-28 13:38:16 -07002266 if (length < 0) {
2267 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002268 //jniThrowException(_env, "java/lang/IllegalArgumentException", "length < 0");
Jack Palevich43702d82009-05-28 13:38:16 -07002269 goto exit;
2270 }
Jason Samse4a06c52011-03-16 16:29:28 -07002271 remaining = _env->GetArrayLength(scriptRef);
Jack Palevich43702d82009-05-28 13:38:16 -07002272 if (remaining < length) {
2273 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002274 //jniThrowException(_env, "java/lang/IllegalArgumentException",
2275 // "length > script.length - offset");
Jack Palevich43702d82009-05-28 13:38:16 -07002276 goto exit;
2277 }
Jason Samse4a06c52011-03-16 16:29:28 -07002278 script_ptr = (jbyte *)
Jack Palevich43702d82009-05-28 13:38:16 -07002279 _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
Miao Wangba8766c2015-10-12 17:24:13 -07002280 if (script_ptr == nullptr) {
2281 ALOGE("Failed to get Java array elements");
2282 return ret;
2283 }
Jack Palevich43702d82009-05-28 13:38:16 -07002284
Tim Murrayeff663f2013-11-15 13:08:30 -08002285 //rsScriptCSetText((RsContext)con, (const char *)script_ptr, length);
Jason Samse4a06c52011-03-16 16:29:28 -07002286
Tim Murray3aa89c12014-08-18 17:51:22 -07002287 ret = (jlong)(uintptr_t)rsScriptCCreate((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07002288 resNameUTF.c_str(), resNameUTF.length(),
2289 cacheDirUTF.c_str(), cacheDirUTF.length(),
Jason Samse4a06c52011-03-16 16:29:28 -07002290 (const char *)script_ptr, length);
Jason Sams39ddc9502009-06-05 17:35:09 -07002291
Jack Palevich43702d82009-05-28 13:38:16 -07002292exit:
Jason Samse4a06c52011-03-16 16:29:28 -07002293 if (script_ptr) {
2294 _env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr,
Jack Palevich43702d82009-05-28 13:38:16 -07002295 _exception ? JNI_ABORT: 0);
2296 }
Jason Samsd19f10d2009-05-22 14:03:28 -07002297
Tim Murray3aa89c12014-08-18 17:51:22 -07002298 return (jlong)(uintptr_t)ret;
Jason Samsd19f10d2009-05-22 14:03:28 -07002299}
2300
Tim Murray460a0492013-11-19 12:45:54 -08002301static jlong
2302nScriptIntrinsicCreate(JNIEnv *_env, jobject _this, jlong con, jint id, jlong eid)
Jason Sams6ab97682012-08-10 12:09:43 -07002303{
Andreas Gampe67333922014-11-10 20:35:59 -08002304 if (kLogApi) {
2305 ALOGD("nScriptIntrinsicCreate, con(%p) id(%i) element(%p)", (RsContext)con, id,
2306 (void *)eid);
2307 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002308 return (jlong)(uintptr_t)rsScriptIntrinsicCreate((RsContext)con, id, (RsElement)eid);
Jason Sams6ab97682012-08-10 12:09:43 -07002309}
2310
Tim Murray460a0492013-11-19 12:45:54 -08002311static jlong
2312nScriptKernelIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot, jint sig)
Jason Sams08a81582012-09-18 12:32:10 -07002313{
Andreas Gampe67333922014-11-10 20:35:59 -08002314 if (kLogApi) {
2315 ALOGD("nScriptKernelIDCreate, con(%p) script(%p), slot(%i), sig(%i)", (RsContext)con,
2316 (void *)sid, slot, sig);
2317 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002318 return (jlong)(uintptr_t)rsScriptKernelIDCreate((RsContext)con, (RsScript)sid, slot, sig);
Jason Sams08a81582012-09-18 12:32:10 -07002319}
2320
Tim Murray460a0492013-11-19 12:45:54 -08002321static jlong
Yang Nibe392ad2015-01-23 17:16:02 -08002322nScriptInvokeIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
2323{
2324 if (kLogApi) {
Elliott Hughes7ff53fa2015-02-05 21:36:10 -08002325 ALOGD("nScriptInvokeIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con,
Yang Nibe392ad2015-01-23 17:16:02 -08002326 (void *)sid, slot);
2327 }
2328 return (jlong)(uintptr_t)rsScriptInvokeIDCreate((RsContext)con, (RsScript)sid, slot);
2329}
2330
2331static jlong
Tim Murray460a0492013-11-19 12:45:54 -08002332nScriptFieldIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
Jason Sams08a81582012-09-18 12:32:10 -07002333{
Andreas Gampe67333922014-11-10 20:35:59 -08002334 if (kLogApi) {
2335 ALOGD("nScriptFieldIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con, (void *)sid,
2336 slot);
2337 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002338 return (jlong)(uintptr_t)rsScriptFieldIDCreate((RsContext)con, (RsScript)sid, slot);
Jason Sams08a81582012-09-18 12:32:10 -07002339}
2340
Tim Murray460a0492013-11-19 12:45:54 -08002341static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002342nScriptGroupCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _kernels, jlongArray _src,
2343 jlongArray _dstk, jlongArray _dstf, jlongArray _types)
Jason Sams08a81582012-09-18 12:32:10 -07002344{
Andreas Gampe67333922014-11-10 20:35:59 -08002345 if (kLogApi) {
2346 ALOGD("nScriptGroupCreate, con(%p)", (RsContext)con);
2347 }
Jason Sams08a81582012-09-18 12:32:10 -07002348
Miao Wanga4ad5f82016-02-11 12:32:39 -08002349 jlong id = 0;
2350
2351 RsScriptKernelID* kernelsPtr;
Ashok Bhat98071552014-02-12 09:54:43 +00002352 jint kernelsLen = _env->GetArrayLength(_kernels);
Chris Wailes488230c32014-08-14 11:22:40 -07002353 jlong *jKernelsPtr = _env->GetLongArrayElements(_kernels, nullptr);
Miao Wanga4ad5f82016-02-11 12:32:39 -08002354
2355 RsScriptKernelID* srcPtr;
2356 jint srcLen = _env->GetArrayLength(_src);
2357 jlong *jSrcPtr = _env->GetLongArrayElements(_src, nullptr);
2358
2359 RsScriptKernelID* dstkPtr;
2360 jint dstkLen = _env->GetArrayLength(_dstk);
2361 jlong *jDstkPtr = _env->GetLongArrayElements(_dstk, nullptr);
2362
2363 RsScriptKernelID* dstfPtr;
2364 jint dstfLen = _env->GetArrayLength(_dstf);
2365 jlong *jDstfPtr = _env->GetLongArrayElements(_dstf, nullptr);
2366
2367 RsType* typesPtr;
2368 jint typesLen = _env->GetArrayLength(_types);
2369 jlong *jTypesPtr = _env->GetLongArrayElements(_types, nullptr);
2370
Miao Wangba8766c2015-10-12 17:24:13 -07002371 if (jKernelsPtr == nullptr) {
2372 ALOGE("Failed to get Java array elements: kernels");
Miao Wanga4ad5f82016-02-11 12:32:39 -08002373 goto cleanup;
Miao Wangba8766c2015-10-12 17:24:13 -07002374 }
Miao Wanga4ad5f82016-02-11 12:32:39 -08002375 if (jSrcPtr == nullptr) {
2376 ALOGE("Failed to get Java array elements: src");
2377 goto cleanup;
2378 }
2379 if (jDstkPtr == nullptr) {
2380 ALOGE("Failed to get Java array elements: dstk");
2381 goto cleanup;
2382 }
2383 if (jDstfPtr == nullptr) {
2384 ALOGE("Failed to get Java array elements: dstf");
2385 goto cleanup;
2386 }
2387 if (jTypesPtr == nullptr) {
2388 ALOGE("Failed to get Java array elements: types");
2389 goto cleanup;
2390 }
2391
2392 kernelsPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * kernelsLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002393 for(int i = 0; i < kernelsLen; ++i) {
2394 kernelsPtr[i] = (RsScriptKernelID)jKernelsPtr[i];
2395 }
Jason Sams08a81582012-09-18 12:32:10 -07002396
Miao Wanga4ad5f82016-02-11 12:32:39 -08002397 srcPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * srcLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002398 for(int i = 0; i < srcLen; ++i) {
2399 srcPtr[i] = (RsScriptKernelID)jSrcPtr[i];
2400 }
Jason Sams08a81582012-09-18 12:32:10 -07002401
Miao Wanga4ad5f82016-02-11 12:32:39 -08002402 dstkPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstkLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002403 for(int i = 0; i < dstkLen; ++i) {
2404 dstkPtr[i] = (RsScriptKernelID)jDstkPtr[i];
2405 }
2406
Miao Wanga4ad5f82016-02-11 12:32:39 -08002407 dstfPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstfLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002408 for(int i = 0; i < dstfLen; ++i) {
2409 dstfPtr[i] = (RsScriptKernelID)jDstfPtr[i];
2410 }
2411
Miao Wanga4ad5f82016-02-11 12:32:39 -08002412 typesPtr = (RsType*) malloc(sizeof(RsType) * typesLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002413 for(int i = 0; i < typesLen; ++i) {
2414 typesPtr[i] = (RsType)jTypesPtr[i];
2415 }
2416
Miao Wanga4ad5f82016-02-11 12:32:39 -08002417 id = (jlong)(uintptr_t)rsScriptGroupCreate((RsContext)con,
Ashok Bhat98071552014-02-12 09:54:43 +00002418 (RsScriptKernelID *)kernelsPtr, kernelsLen * sizeof(RsScriptKernelID),
2419 (RsScriptKernelID *)srcPtr, srcLen * sizeof(RsScriptKernelID),
2420 (RsScriptKernelID *)dstkPtr, dstkLen * sizeof(RsScriptKernelID),
2421 (RsScriptFieldID *)dstfPtr, dstfLen * sizeof(RsScriptKernelID),
2422 (RsType *)typesPtr, typesLen * sizeof(RsType));
2423
2424 free(kernelsPtr);
2425 free(srcPtr);
2426 free(dstkPtr);
2427 free(dstfPtr);
2428 free(typesPtr);
Miao Wanga4ad5f82016-02-11 12:32:39 -08002429
2430cleanup:
2431 if (jKernelsPtr != nullptr) {
2432 _env->ReleaseLongArrayElements(_kernels, jKernelsPtr, 0);
2433 }
2434 if (jSrcPtr != nullptr) {
2435 _env->ReleaseLongArrayElements(_src, jSrcPtr, 0);
2436 }
2437 if (jDstkPtr != nullptr) {
2438 _env->ReleaseLongArrayElements(_dstk, jDstkPtr, 0);
2439 }
2440 if (jDstfPtr != nullptr) {
2441 _env->ReleaseLongArrayElements(_dstf, jDstfPtr, 0);
2442 }
2443 if (jTypesPtr != nullptr) {
2444 _env->ReleaseLongArrayElements(_types, jTypesPtr, 0);
2445 }
2446
Jason Sams08a81582012-09-18 12:32:10 -07002447 return id;
2448}
2449
2450static void
Tim Murray460a0492013-11-19 12:45:54 -08002451nScriptGroupSetInput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002452{
Andreas Gampe67333922014-11-10 20:35:59 -08002453 if (kLogApi) {
2454 ALOGD("nScriptGroupSetInput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2455 (void *)gid, (void *)kid, (void *)alloc);
2456 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002457 rsScriptGroupSetInput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002458}
2459
2460static void
Tim Murray460a0492013-11-19 12:45:54 -08002461nScriptGroupSetOutput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002462{
Andreas Gampe67333922014-11-10 20:35:59 -08002463 if (kLogApi) {
2464 ALOGD("nScriptGroupSetOutput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2465 (void *)gid, (void *)kid, (void *)alloc);
2466 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002467 rsScriptGroupSetOutput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002468}
2469
2470static void
Tim Murray460a0492013-11-19 12:45:54 -08002471nScriptGroupExecute(JNIEnv *_env, jobject _this, jlong con, jlong gid)
Jason Sams08a81582012-09-18 12:32:10 -07002472{
Andreas Gampe67333922014-11-10 20:35:59 -08002473 if (kLogApi) {
2474 ALOGD("nScriptGroupSetOutput, con(%p) group(%p)", (RsContext)con, (void *)gid);
2475 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002476 rsScriptGroupExecute((RsContext)con, (RsScriptGroup)gid);
Jason Sams08a81582012-09-18 12:32:10 -07002477}
2478
Jason Samsd19f10d2009-05-22 14:03:28 -07002479// ---------------------------------------------------------------------------
2480
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002481static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002482nProgramStoreCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Sams331bf9b2011-04-06 11:23:54 -07002483 jboolean colorMaskR, jboolean colorMaskG, jboolean colorMaskB, jboolean colorMaskA,
2484 jboolean depthMask, jboolean ditherEnable,
2485 jint srcFunc, jint destFunc,
2486 jint depthFunc)
Jason Samsd19f10d2009-05-22 14:03:28 -07002487{
Andreas Gampe67333922014-11-10 20:35:59 -08002488 if (kLogApi) {
2489 ALOGD("nProgramStoreCreate, con(%p)", (RsContext)con);
2490 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002491 return (jlong)(uintptr_t)rsProgramStoreCreate((RsContext)con, colorMaskR, colorMaskG, colorMaskB, colorMaskA,
Jason Sams331bf9b2011-04-06 11:23:54 -07002492 depthMask, ditherEnable, (RsBlendSrcFunc)srcFunc,
2493 (RsBlendDstFunc)destFunc, (RsDepthFunc)depthFunc);
Jason Samsd19f10d2009-05-22 14:03:28 -07002494}
2495
Jason Sams0011bcf2009-12-15 12:58:36 -08002496// ---------------------------------------------------------------------------
2497
2498static void
Tim Murray460a0492013-11-19 12:45:54 -08002499nProgramBindConstants(JNIEnv *_env, jobject _this, jlong con, jlong vpv, jint slot, jlong a)
Jason Sams0011bcf2009-12-15 12:58:36 -08002500{
Andreas Gampe67333922014-11-10 20:35:59 -08002501 if (kLogApi) {
2502 ALOGD("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", (RsContext)con,
2503 (RsProgramVertex)vpv, slot, (RsAllocation)a);
2504 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002505 rsProgramBindConstants((RsContext)con, (RsProgram)vpv, slot, (RsAllocation)a);
Jason Sams0011bcf2009-12-15 12:58:36 -08002506}
Jason Sams54c0ec12009-11-30 14:49:55 -08002507
Jason Sams68afd012009-12-17 16:55:08 -08002508static void
Tim Murray460a0492013-11-19 12:45:54 -08002509nProgramBindTexture(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002510{
Andreas Gampe67333922014-11-10 20:35:59 -08002511 if (kLogApi) {
2512 ALOGD("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2513 (RsProgramFragment)vpf, slot, (RsAllocation)a);
2514 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002515 rsProgramBindTexture((RsContext)con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
Jason Sams68afd012009-12-17 16:55:08 -08002516}
2517
2518static void
Tim Murray460a0492013-11-19 12:45:54 -08002519nProgramBindSampler(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002520{
Andreas Gampe67333922014-11-10 20:35:59 -08002521 if (kLogApi) {
2522 ALOGD("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2523 (RsProgramFragment)vpf, slot, (RsSampler)a);
2524 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002525 rsProgramBindSampler((RsContext)con, (RsProgramFragment)vpf, slot, (RsSampler)a);
Jason Sams68afd012009-12-17 16:55:08 -08002526}
2527
Jason Samsd19f10d2009-05-22 14:03:28 -07002528// ---------------------------------------------------------------------------
2529
Tim Murray460a0492013-11-19 12:45:54 -08002530static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002531nProgramFragmentCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002532 jobjectArray texNames, jlongArray params)
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002533{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002534 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002535 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002536 jint paramLen = _env->GetArrayLength(params);
Miao Wangba8766c2015-10-12 17:24:13 -07002537 if (jParamPtr == nullptr) {
2538 ALOGE("Failed to get Java array elements");
2539 return 0;
2540 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002541
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002542 int texCount = _env->GetArrayLength(texNames);
2543 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2544 const char ** nameArray = names.c_str();
2545 size_t* sizeArray = names.c_str_len();
2546
Andreas Gampe67333922014-11-10 20:35:59 -08002547 if (kLogApi) {
2548 ALOGD("nProgramFragmentCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2549 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002550
Ashok Bhat98071552014-02-12 09:54:43 +00002551 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2552 for(int i = 0; i < paramLen; ++i) {
2553 paramPtr[i] = (uintptr_t)jParamPtr[i];
2554 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002555 jlong ret = (jlong)(uintptr_t)rsProgramFragmentCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002556 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002557 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002558
Ashok Bhat98071552014-02-12 09:54:43 +00002559 free(paramPtr);
2560 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002561 return ret;
2562}
2563
2564
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002565// ---------------------------------------------------------------------------
2566
Tim Murray460a0492013-11-19 12:45:54 -08002567static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002568nProgramVertexCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002569 jobjectArray texNames, jlongArray params)
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002570{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002571 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002572 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams0011bcf2009-12-15 12:58:36 -08002573 jint paramLen = _env->GetArrayLength(params);
Miao Wangba8766c2015-10-12 17:24:13 -07002574 if (jParamPtr == nullptr) {
2575 ALOGE("Failed to get Java array elements");
2576 return 0;
2577 }
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002578
Andreas Gampe67333922014-11-10 20:35:59 -08002579 if (kLogApi) {
2580 ALOGD("nProgramVertexCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2581 }
Jason Sams0011bcf2009-12-15 12:58:36 -08002582
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002583 int texCount = _env->GetArrayLength(texNames);
2584 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2585 const char ** nameArray = names.c_str();
2586 size_t* sizeArray = names.c_str_len();
2587
Ashok Bhat98071552014-02-12 09:54:43 +00002588 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2589 for(int i = 0; i < paramLen; ++i) {
2590 paramPtr[i] = (uintptr_t)jParamPtr[i];
2591 }
2592
Tim Murray3aa89c12014-08-18 17:51:22 -07002593 jlong ret = (jlong)(uintptr_t)rsProgramVertexCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002594 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002595 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002596
Ashok Bhat98071552014-02-12 09:54:43 +00002597 free(paramPtr);
2598 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams0011bcf2009-12-15 12:58:36 -08002599 return ret;
2600}
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002601
Jason Samsebfb4362009-09-23 13:57:02 -07002602// ---------------------------------------------------------------------------
2603
Tim Murray460a0492013-11-19 12:45:54 -08002604static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002605nProgramRasterCreate(JNIEnv *_env, jobject _this, jlong con, jboolean pointSprite, jint cull)
Jason Samsebfb4362009-09-23 13:57:02 -07002606{
Andreas Gampe67333922014-11-10 20:35:59 -08002607 if (kLogApi) {
2608 ALOGD("nProgramRasterCreate, con(%p), pointSprite(%i), cull(%i)", (RsContext)con,
2609 pointSprite, cull);
2610 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002611 return (jlong)(uintptr_t)rsProgramRasterCreate((RsContext)con, pointSprite, (RsCullMode)cull);
Jason Samsebfb4362009-09-23 13:57:02 -07002612}
2613
Jason Samsd19f10d2009-05-22 14:03:28 -07002614
2615// ---------------------------------------------------------------------------
2616
2617static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002618nContextBindRootScript(JNIEnv *_env, jobject _this, jlong con, jlong script)
Jason Samsd19f10d2009-05-22 14:03:28 -07002619{
Andreas Gampe67333922014-11-10 20:35:59 -08002620 if (kLogApi) {
2621 ALOGD("nContextBindRootScript, con(%p), script(%p)", (RsContext)con, (RsScript)script);
2622 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002623 rsContextBindRootScript((RsContext)con, (RsScript)script);
Jason Samsd19f10d2009-05-22 14:03:28 -07002624}
2625
2626static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002627nContextBindProgramStore(JNIEnv *_env, jobject _this, jlong con, jlong pfs)
Jason Samsd19f10d2009-05-22 14:03:28 -07002628{
Andreas Gampe67333922014-11-10 20:35:59 -08002629 if (kLogApi) {
2630 ALOGD("nContextBindProgramStore, con(%p), pfs(%p)", (RsContext)con, (RsProgramStore)pfs);
2631 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002632 rsContextBindProgramStore((RsContext)con, (RsProgramStore)pfs);
Jason Samsd19f10d2009-05-22 14:03:28 -07002633}
2634
2635static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002636nContextBindProgramFragment(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsd19f10d2009-05-22 14:03:28 -07002637{
Andreas Gampe67333922014-11-10 20:35:59 -08002638 if (kLogApi) {
2639 ALOGD("nContextBindProgramFragment, con(%p), pf(%p)", (RsContext)con,
2640 (RsProgramFragment)pf);
2641 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002642 rsContextBindProgramFragment((RsContext)con, (RsProgramFragment)pf);
Jason Samsd19f10d2009-05-22 14:03:28 -07002643}
2644
Jason Sams0826a6f2009-06-15 19:04:56 -07002645static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002646nContextBindProgramVertex(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Sams0826a6f2009-06-15 19:04:56 -07002647{
Andreas Gampe67333922014-11-10 20:35:59 -08002648 if (kLogApi) {
2649 ALOGD("nContextBindProgramVertex, con(%p), pf(%p)", (RsContext)con, (RsProgramVertex)pf);
2650 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002651 rsContextBindProgramVertex((RsContext)con, (RsProgramVertex)pf);
Jason Sams0826a6f2009-06-15 19:04:56 -07002652}
2653
Joe Onoratod7b37742009-08-09 22:57:44 -07002654static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002655nContextBindProgramRaster(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsebfb4362009-09-23 13:57:02 -07002656{
Andreas Gampe67333922014-11-10 20:35:59 -08002657 if (kLogApi) {
2658 ALOGD("nContextBindProgramRaster, con(%p), pf(%p)", (RsContext)con, (RsProgramRaster)pf);
2659 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002660 rsContextBindProgramRaster((RsContext)con, (RsProgramRaster)pf);
Jason Samsebfb4362009-09-23 13:57:02 -07002661}
2662
Joe Onoratod7b37742009-08-09 22:57:44 -07002663
Jason Sams02fb2cb2009-05-28 15:37:57 -07002664// ---------------------------------------------------------------------------
2665
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002666static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002667nSamplerCreate(JNIEnv *_env, jobject _this, jlong con, jint magFilter, jint minFilter,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002668 jint wrapS, jint wrapT, jint wrapR, jfloat aniso)
Jason Sams02fb2cb2009-05-28 15:37:57 -07002669{
Andreas Gampe67333922014-11-10 20:35:59 -08002670 if (kLogApi) {
2671 ALOGD("nSamplerCreate, con(%p)", (RsContext)con);
2672 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002673 return (jlong)(uintptr_t)rsSamplerCreate((RsContext)con,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002674 (RsSamplerValue)magFilter,
2675 (RsSamplerValue)minFilter,
2676 (RsSamplerValue)wrapS,
2677 (RsSamplerValue)wrapT,
2678 (RsSamplerValue)wrapR,
2679 aniso);
Jason Sams02fb2cb2009-05-28 15:37:57 -07002680}
2681
Jason Samsbba134c2009-06-22 15:49:21 -07002682// ---------------------------------------------------------------------------
2683
Tim Murray460a0492013-11-19 12:45:54 -08002684static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002685nMeshCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _vtx, jlongArray _idx, jintArray _prim)
Jason Samsbba134c2009-06-22 15:49:21 -07002686{
Andreas Gampe67333922014-11-10 20:35:59 -08002687 if (kLogApi) {
2688 ALOGD("nMeshCreate, con(%p)", (RsContext)con);
2689 }
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002690
Miao Wanga4ad5f82016-02-11 12:32:39 -08002691 jlong id = 0;
2692
2693 RsAllocation* vtxPtr;
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002694 jint vtxLen = _env->GetArrayLength(_vtx);
Chris Wailes488230c32014-08-14 11:22:40 -07002695 jlong *jVtxPtr = _env->GetLongArrayElements(_vtx, nullptr);
Miao Wanga4ad5f82016-02-11 12:32:39 -08002696
2697 RsAllocation* idxPtr;
2698 jint idxLen = _env->GetArrayLength(_idx);
2699 jlong *jIdxPtr = _env->GetLongArrayElements(_idx, nullptr);
2700
2701 jint primLen = _env->GetArrayLength(_prim);
2702 jint *primPtr = _env->GetIntArrayElements(_prim, nullptr);
2703
Miao Wangba8766c2015-10-12 17:24:13 -07002704 if (jVtxPtr == nullptr) {
2705 ALOGE("Failed to get Java array elements: vtx");
Miao Wanga4ad5f82016-02-11 12:32:39 -08002706 goto cleanupMesh;
Miao Wangba8766c2015-10-12 17:24:13 -07002707 }
Miao Wanga4ad5f82016-02-11 12:32:39 -08002708 if (jIdxPtr == nullptr) {
2709 ALOGE("Failed to get Java array elements: idx");
2710 goto cleanupMesh;
2711 }
2712 if (primPtr == nullptr) {
2713 ALOGE("Failed to get Java array elements: prim");
2714 goto cleanupMesh;
2715 }
2716
2717 vtxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * vtxLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002718 for(int i = 0; i < vtxLen; ++i) {
2719 vtxPtr[i] = (RsAllocation)(uintptr_t)jVtxPtr[i];
2720 }
2721
Miao Wanga4ad5f82016-02-11 12:32:39 -08002722 idxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * idxLen);
Ashok Bhat98071552014-02-12 09:54:43 +00002723 for(int i = 0; i < idxLen; ++i) {
2724 idxPtr[i] = (RsAllocation)(uintptr_t)jIdxPtr[i];
2725 }
2726
Miao Wanga4ad5f82016-02-11 12:32:39 -08002727 id = (jlong)(uintptr_t)rsMeshCreate((RsContext)con,
2728 (RsAllocation *)vtxPtr, vtxLen,
2729 (RsAllocation *)idxPtr, idxLen,
2730 (uint32_t *)primPtr, primLen);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002731
Ashok Bhat98071552014-02-12 09:54:43 +00002732 free(vtxPtr);
2733 free(idxPtr);
Miao Wanga4ad5f82016-02-11 12:32:39 -08002734
2735cleanupMesh:
2736 if (jVtxPtr != nullptr) {
2737 _env->ReleaseLongArrayElements(_vtx, jVtxPtr, 0);
2738 }
2739 if (jIdxPtr != nullptr) {
2740 _env->ReleaseLongArrayElements(_idx, jIdxPtr, 0);
2741 }
2742 if (primPtr != nullptr) {
2743 _env->ReleaseIntArrayElements(_prim, primPtr, 0);
2744 }
2745
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002746 return id;
2747}
2748
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002749static jint
Tim Murray460a0492013-11-19 12:45:54 -08002750nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002751{
Andreas Gampe67333922014-11-10 20:35:59 -08002752 if (kLogApi) {
2753 ALOGD("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2754 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002755 jint vtxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002756 rsaMeshGetVertexBufferCount((RsContext)con, (RsMesh)mesh, &vtxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002757 return vtxCount;
2758}
2759
2760static jint
Tim Murray460a0492013-11-19 12:45:54 -08002761nMeshGetIndexCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002762{
Andreas Gampe67333922014-11-10 20:35:59 -08002763 if (kLogApi) {
2764 ALOGD("nMeshGetIndexCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2765 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002766 jint idxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002767 rsaMeshGetIndexCount((RsContext)con, (RsMesh)mesh, &idxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002768 return idxCount;
2769}
2770
2771static void
Ashok Bhat98071552014-02-12 09:54:43 +00002772nMeshGetVertices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _ids, jint numVtxIDs)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002773{
Andreas Gampe67333922014-11-10 20:35:59 -08002774 if (kLogApi) {
2775 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2776 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002777
2778 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
Tim Murrayeff663f2013-11-15 13:08:30 -08002779 rsaMeshGetVertices((RsContext)con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002780
2781 for(jint i = 0; i < numVtxIDs; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002782 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002783 _env->SetLongArrayRegion(_ids, i, 1, &alloc);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002784 }
2785
2786 free(allocs);
2787}
2788
2789static void
Ashok Bhat98071552014-02-12 09:54:43 +00002790nMeshGetIndices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _idxIds, jintArray _primitives, jint numIndices)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002791{
Andreas Gampe67333922014-11-10 20:35:59 -08002792 if (kLogApi) {
2793 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2794 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002795
2796 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
2797 uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
2798
Tim Murrayeff663f2013-11-15 13:08:30 -08002799 rsaMeshGetIndices((RsContext)con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002800
2801 for(jint i = 0; i < numIndices; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002802 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002803 const jint prim = (jint)prims[i];
2804 _env->SetLongArrayRegion(_idxIds, i, 1, &alloc);
2805 _env->SetIntArrayRegion(_primitives, i, 1, &prim);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002806 }
2807
2808 free(allocs);
2809 free(prims);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002810}
2811
Tim Murray56f9e6f2014-05-16 11:47:26 -07002812static jint
2813nSystemGetPointerSize(JNIEnv *_env, jobject _this) {
2814 return (jint)sizeof(void*);
2815}
2816
Miao Wang0facf022015-11-25 11:21:13 -08002817static jobject
2818nAllocationGetByteBuffer(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
2819 jlongArray strideArr, jint xBytesSize,
2820 jint dimY, jint dimZ) {
2821 if (kLogApi) {
2822 ALOGD("nAllocationGetByteBuffer, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
2823 }
Tim Murray56f9e6f2014-05-16 11:47:26 -07002824
Miao Wang0facf022015-11-25 11:21:13 -08002825 jlong *jStridePtr = _env->GetLongArrayElements(strideArr, nullptr);
2826 if (jStridePtr == nullptr) {
2827 ALOGE("Failed to get Java array elements: strideArr");
2828 return 0;
2829 }
2830
2831 size_t strideIn = xBytesSize;
2832 void* ptr = nullptr;
2833 if (alloc != 0) {
2834 ptr = rsAllocationGetPointer((RsContext)con, (RsAllocation)alloc, 0,
2835 RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, 0, 0,
2836 &strideIn, sizeof(size_t));
2837 }
2838
2839 jobject byteBuffer = nullptr;
2840 if (ptr != nullptr) {
2841 size_t bufferSize = strideIn;
2842 jStridePtr[0] = strideIn;
2843 if (dimY > 0) {
2844 bufferSize *= dimY;
2845 }
2846 if (dimZ > 0) {
2847 bufferSize *= dimZ;
2848 }
2849 byteBuffer = _env->NewDirectByteBuffer(ptr, (jlong) bufferSize);
2850 }
2851 _env->ReleaseLongArrayElements(strideArr, jStridePtr, 0);
2852 return byteBuffer;
2853}
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002854// ---------------------------------------------------------------------------
2855
Jason Samsd19f10d2009-05-22 14:03:28 -07002856
Jason Sams94d8e90a2009-06-10 16:09:05 -07002857static const char *classPathName = "android/renderscript/RenderScript";
Jason Samsd19f10d2009-05-22 14:03:28 -07002858
Daniel Micay76f6a862015-09-19 17:31:01 -04002859static const JNINativeMethod methods[] = {
Jason Sams1c415172010-11-08 17:06:46 -08002860{"_nInit", "()V", (void*)_nInit },
Jason Samsea84a7c2009-09-04 14:42:41 -07002861
Tim Murrayeff663f2013-11-15 13:08:30 -08002862{"nDeviceCreate", "()J", (void*)nDeviceCreate },
2863{"nDeviceDestroy", "(J)V", (void*)nDeviceDestroy },
2864{"nDeviceSetConfig", "(JII)V", (void*)nDeviceSetConfig },
2865{"nContextGetUserMessage", "(J[I)I", (void*)nContextGetUserMessage },
2866{"nContextGetErrorMessage", "(J)Ljava/lang/String;", (void*)nContextGetErrorMessage },
2867{"nContextPeekMessage", "(J[I)I", (void*)nContextPeekMessage },
Jason Sams1c415172010-11-08 17:06:46 -08002868
Tim Murrayeff663f2013-11-15 13:08:30 -08002869{"nContextInitToClient", "(J)V", (void*)nContextInitToClient },
2870{"nContextDeinitToClient", "(J)V", (void*)nContextDeinitToClient },
Jason Samsd19f10d2009-05-22 14:03:28 -07002871
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002872
Jason Sams2e1872f2010-08-17 16:25:41 -07002873// All methods below are thread protected in java.
Tim Murrayeff663f2013-11-15 13:08:30 -08002874{"rsnContextCreate", "(JIII)J", (void*)nContextCreate },
2875{"rsnContextCreateGL", "(JIIIIIIIIIIIIFI)J", (void*)nContextCreateGL },
2876{"rsnContextFinish", "(J)V", (void*)nContextFinish },
2877{"rsnContextSetPriority", "(JI)V", (void*)nContextSetPriority },
Tim Murray47f31582015-04-07 15:43:24 -07002878{"rsnContextSetCacheDir", "(JLjava/lang/String;)V", (void*)nContextSetCacheDir },
Tim Murrayeff663f2013-11-15 13:08:30 -08002879{"rsnContextSetSurface", "(JIILandroid/view/Surface;)V", (void*)nContextSetSurface },
2880{"rsnContextDestroy", "(J)V", (void*)nContextDestroy },
2881{"rsnContextDump", "(JI)V", (void*)nContextDump },
2882{"rsnContextPause", "(J)V", (void*)nContextPause },
2883{"rsnContextResume", "(J)V", (void*)nContextResume },
2884{"rsnContextSendMessage", "(JI[I)V", (void*)nContextSendMessage },
Yang Ni281c3252014-10-24 08:52:24 -07002885{"rsnClosureCreate", "(JJJ[J[J[I[J[J)J", (void*)nClosureCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002886{"rsnInvokeClosureCreate", "(JJ[B[J[J[I)J", (void*)nInvokeClosureCreate },
Yang Ni281c3252014-10-24 08:52:24 -07002887{"rsnClosureSetArg", "(JJIJI)V", (void*)nClosureSetArg },
2888{"rsnClosureSetGlobal", "(JJJJI)V", (void*)nClosureSetGlobal },
Tim Murray460a0492013-11-19 12:45:54 -08002889{"rsnAssignName", "(JJ[B)V", (void*)nAssignName },
2890{"rsnGetName", "(JJ)Ljava/lang/String;", (void*)nGetName },
2891{"rsnObjDestroy", "(JJ)V", (void*)nObjDestroy },
Jason Sams64676f32009-07-08 18:01:53 -07002892
Tim Murray460a0492013-11-19 12:45:54 -08002893{"rsnFileA3DCreateFromFile", "(JLjava/lang/String;)J", (void*)nFileA3DCreateFromFile },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002894{"rsnFileA3DCreateFromAssetStream", "(JJ)J", (void*)nFileA3DCreateFromAssetStream },
Tim Murray460a0492013-11-19 12:45:54 -08002895{"rsnFileA3DCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;)J", (void*)nFileA3DCreateFromAsset },
2896{"rsnFileA3DGetNumIndexEntries", "(JJ)I", (void*)nFileA3DGetNumIndexEntries },
2897{"rsnFileA3DGetIndexEntries", "(JJI[I[Ljava/lang/String;)V", (void*)nFileA3DGetIndexEntries },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002898{"rsnFileA3DGetEntryByIndex", "(JJI)J", (void*)nFileA3DGetEntryByIndex },
Jason Samsd19f10d2009-05-22 14:03:28 -07002899
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002900{"rsnFontCreateFromFile", "(JLjava/lang/String;FI)J", (void*)nFontCreateFromFile },
2901{"rsnFontCreateFromAssetStream", "(JLjava/lang/String;FIJ)J", (void*)nFontCreateFromAssetStream },
2902{"rsnFontCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;FI)J", (void*)nFontCreateFromAsset },
Jason Samsd19f10d2009-05-22 14:03:28 -07002903
Tim Murray460a0492013-11-19 12:45:54 -08002904{"rsnElementCreate", "(JJIZI)J", (void*)nElementCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002905{"rsnElementCreate2", "(J[J[Ljava/lang/String;[I)J", (void*)nElementCreate2 },
Tim Murray460a0492013-11-19 12:45:54 -08002906{"rsnElementGetNativeData", "(JJ[I)V", (void*)nElementGetNativeData },
Ashok Bhat98071552014-02-12 09:54:43 +00002907{"rsnElementGetSubElements", "(JJ[J[Ljava/lang/String;[I)V", (void*)nElementGetSubElements },
Jason Samsd19f10d2009-05-22 14:03:28 -07002908
Tim Murray460a0492013-11-19 12:45:54 -08002909{"rsnTypeCreate", "(JJIIIZZI)J", (void*)nTypeCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002910{"rsnTypeGetNativeData", "(JJ[J)V", (void*)nTypeGetNativeData },
Jason Samsd19f10d2009-05-22 14:03:28 -07002911
Ashok Bhat98071552014-02-12 09:54:43 +00002912{"rsnAllocationCreateTyped", "(JJIIJ)J", (void*)nAllocationCreateTyped },
Tim Murray460a0492013-11-19 12:45:54 -08002913{"rsnAllocationCreateFromBitmap", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateFromBitmap },
2914{"rsnAllocationCreateBitmapBackedAllocation", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateBitmapBackedAllocation },
2915{"rsnAllocationCubeCreateFromBitmap","(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCubeCreateFromBitmap },
Jason Sams5476b452010-12-08 16:14:36 -08002916
Tim Murray460a0492013-11-19 12:45:54 -08002917{"rsnAllocationCopyFromBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyFromBitmap },
2918{"rsnAllocationCopyToBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyToBitmap },
Jason Sams4ef66502010-12-10 16:03:15 -08002919
Tim Murray460a0492013-11-19 12:45:54 -08002920{"rsnAllocationSyncAll", "(JJI)V", (void*)nAllocationSyncAll },
Miao Wang8c150922015-10-26 17:44:10 -07002921{"rsnAllocationSetupBufferQueue", "(JJI)V", (void*)nAllocationSetupBufferQueue },
2922{"rsnAllocationShareBufferQueue", "(JJJ)V", (void*)nAllocationShareBufferQueue },
Tim Murray460a0492013-11-19 12:45:54 -08002923{"rsnAllocationGetSurface", "(JJ)Landroid/view/Surface;", (void*)nAllocationGetSurface },
2924{"rsnAllocationSetSurface", "(JJLandroid/view/Surface;)V", (void*)nAllocationSetSurface },
2925{"rsnAllocationIoSend", "(JJ)V", (void*)nAllocationIoSend },
Miao Wang8c150922015-10-26 17:44:10 -07002926{"rsnAllocationIoReceive", "(JJ)J", (void*)nAllocationIoReceive },
Miao Wang87e908d2015-03-02 15:15:15 -08002927{"rsnAllocationData1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData1D },
Miao Wangc8e237e2015-02-20 18:36:32 -08002928{"rsnAllocationElementData", "(JJIIIII[BI)V", (void*)nAllocationElementData },
Miao Wang87e908d2015-03-02 15:15:15 -08002929{"rsnAllocationData2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData2D },
Tim Murray460a0492013-11-19 12:45:54 -08002930{"rsnAllocationData2D", "(JJIIIIIIJIIII)V", (void*)nAllocationData2D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002931{"rsnAllocationData3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData3D },
Tim Murray460a0492013-11-19 12:45:54 -08002932{"rsnAllocationData3D", "(JJIIIIIIIJIIII)V", (void*)nAllocationData3D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002933{"rsnAllocationRead", "(JJLjava/lang/Object;IIZ)V", (void*)nAllocationRead },
2934{"rsnAllocationRead1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead1D },
Miao Wang45cec0a2015-03-04 16:40:21 -08002935{"rsnAllocationElementRead", "(JJIIIII[BI)V", (void*)nAllocationElementRead },
Miao Wang87e908d2015-03-02 15:15:15 -08002936{"rsnAllocationRead2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead2D },
2937{"rsnAllocationRead3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead3D },
Tim Murray460a0492013-11-19 12:45:54 -08002938{"rsnAllocationGetType", "(JJ)J", (void*)nAllocationGetType},
2939{"rsnAllocationResize1D", "(JJI)V", (void*)nAllocationResize1D },
2940{"rsnAllocationGenerateMipmaps", "(JJ)V", (void*)nAllocationGenerateMipmaps },
Jason Samsbd1c3ad2009-08-03 16:03:08 -07002941
Jason Sams46ba27e32015-02-06 17:45:15 -08002942{"rsnAllocationAdapterCreate", "(JJJ)J", (void*)nAllocationAdapterCreate },
2943{"rsnAllocationAdapterOffset", "(JJIIIIIIIII)V", (void*)nAllocationAdapterOffset },
2944
Tim Murray460a0492013-11-19 12:45:54 -08002945{"rsnScriptBindAllocation", "(JJJI)V", (void*)nScriptBindAllocation },
2946{"rsnScriptSetTimeZone", "(JJ[B)V", (void*)nScriptSetTimeZone },
2947{"rsnScriptInvoke", "(JJI)V", (void*)nScriptInvoke },
2948{"rsnScriptInvokeV", "(JJI[B)V", (void*)nScriptInvokeV },
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002949
2950{"rsnScriptForEach", "(JJI[JJ[B[I)V", (void*)nScriptForEach },
Matt Wala36eb1f72015-07-20 15:35:27 -07002951{"rsnScriptReduce", "(JJIJJ[I)V", (void*)nScriptReduce },
David Gross26ef7a732016-01-12 12:19:15 -08002952{"rsnScriptReduceNew", "(JJI[JJ[I)V", (void*)nScriptReduceNew },
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002953
Tim Murray460a0492013-11-19 12:45:54 -08002954{"rsnScriptSetVarI", "(JJII)V", (void*)nScriptSetVarI },
2955{"rsnScriptGetVarI", "(JJI)I", (void*)nScriptGetVarI },
2956{"rsnScriptSetVarJ", "(JJIJ)V", (void*)nScriptSetVarJ },
2957{"rsnScriptGetVarJ", "(JJI)J", (void*)nScriptGetVarJ },
2958{"rsnScriptSetVarF", "(JJIF)V", (void*)nScriptSetVarF },
2959{"rsnScriptGetVarF", "(JJI)F", (void*)nScriptGetVarF },
2960{"rsnScriptSetVarD", "(JJID)V", (void*)nScriptSetVarD },
2961{"rsnScriptGetVarD", "(JJI)D", (void*)nScriptGetVarD },
2962{"rsnScriptSetVarV", "(JJI[B)V", (void*)nScriptSetVarV },
2963{"rsnScriptGetVarV", "(JJI[B)V", (void*)nScriptGetVarV },
2964{"rsnScriptSetVarVE", "(JJI[BJ[I)V", (void*)nScriptSetVarVE },
2965{"rsnScriptSetVarObj", "(JJIJ)V", (void*)nScriptSetVarObj },
Jason Samsd19f10d2009-05-22 14:03:28 -07002966
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002967{"rsnScriptCCreate", "(JLjava/lang/String;Ljava/lang/String;[BI)J", (void*)nScriptCCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002968{"rsnScriptIntrinsicCreate", "(JIJ)J", (void*)nScriptIntrinsicCreate },
2969{"rsnScriptKernelIDCreate", "(JJII)J", (void*)nScriptKernelIDCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002970{"rsnScriptInvokeIDCreate", "(JJI)J", (void*)nScriptInvokeIDCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002971{"rsnScriptFieldIDCreate", "(JJI)J", (void*)nScriptFieldIDCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002972{"rsnScriptGroupCreate", "(J[J[J[J[J[J)J", (void*)nScriptGroupCreate },
Yang Ni35be56c2015-04-02 17:47:56 -07002973{"rsnScriptGroup2Create", "(JLjava/lang/String;Ljava/lang/String;[J)J", (void*)nScriptGroup2Create },
Tim Murray460a0492013-11-19 12:45:54 -08002974{"rsnScriptGroupSetInput", "(JJJJ)V", (void*)nScriptGroupSetInput },
2975{"rsnScriptGroupSetOutput", "(JJJJ)V", (void*)nScriptGroupSetOutput },
2976{"rsnScriptGroupExecute", "(JJ)V", (void*)nScriptGroupExecute },
Yang Ni281c3252014-10-24 08:52:24 -07002977{"rsnScriptGroup2Execute", "(JJ)V", (void*)nScriptGroup2Execute },
Jason Sams0011bcf2009-12-15 12:58:36 -08002978
Tim Murray25207df2015-01-12 16:47:56 -08002979{"rsnScriptIntrinsicBLAS_Single", "(JJIIIIIIIIIFJJFJIIII)V", (void*)nScriptIntrinsicBLAS_Single },
2980{"rsnScriptIntrinsicBLAS_Double", "(JJIIIIIIIIIDJJDJIIII)V", (void*)nScriptIntrinsicBLAS_Double },
2981{"rsnScriptIntrinsicBLAS_Complex", "(JJIIIIIIIIIFFJJFFJIIII)V", (void*)nScriptIntrinsicBLAS_Complex },
2982{"rsnScriptIntrinsicBLAS_Z", "(JJIIIIIIIIIDDJJDDJIIII)V", (void*)nScriptIntrinsicBLAS_Z },
2983
Tim Murray9cb16a22015-04-01 11:07:16 -07002984{"rsnScriptIntrinsicBLAS_BNNM", "(JJIIIJIJIJII)V", (void*)nScriptIntrinsicBLAS_BNNM },
2985
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002986{"rsnProgramStoreCreate", "(JZZZZZZIII)J", (void*)nProgramStoreCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002987
Tim Murray460a0492013-11-19 12:45:54 -08002988{"rsnProgramBindConstants", "(JJIJ)V", (void*)nProgramBindConstants },
2989{"rsnProgramBindTexture", "(JJIJ)V", (void*)nProgramBindTexture },
2990{"rsnProgramBindSampler", "(JJIJ)V", (void*)nProgramBindSampler },
Jason Samsebfb4362009-09-23 13:57:02 -07002991
Ashok Bhat98071552014-02-12 09:54:43 +00002992{"rsnProgramFragmentCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramFragmentCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002993{"rsnProgramRasterCreate", "(JZI)J", (void*)nProgramRasterCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002994{"rsnProgramVertexCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramVertexCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002995
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002996{"rsnContextBindRootScript", "(JJ)V", (void*)nContextBindRootScript },
2997{"rsnContextBindProgramStore", "(JJ)V", (void*)nContextBindProgramStore },
2998{"rsnContextBindProgramFragment", "(JJ)V", (void*)nContextBindProgramFragment },
2999{"rsnContextBindProgramVertex", "(JJ)V", (void*)nContextBindProgramVertex },
3000{"rsnContextBindProgramRaster", "(JJ)V", (void*)nContextBindProgramRaster },
Jason Sams02fb2cb2009-05-28 15:37:57 -07003001
Ashok Bhat0e0c0882014-02-04 14:57:58 +00003002{"rsnSamplerCreate", "(JIIIIIF)J", (void*)nSamplerCreate },
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07003003
Ashok Bhat98071552014-02-12 09:54:43 +00003004{"rsnMeshCreate", "(J[J[J[I)J", (void*)nMeshCreate },
Jason Sams2e1872f2010-08-17 16:25:41 -07003005
Tim Murray460a0492013-11-19 12:45:54 -08003006{"rsnMeshGetVertexBufferCount", "(JJ)I", (void*)nMeshGetVertexBufferCount },
3007{"rsnMeshGetIndexCount", "(JJ)I", (void*)nMeshGetIndexCount },
Ashok Bhat98071552014-02-12 09:54:43 +00003008{"rsnMeshGetVertices", "(JJ[JI)V", (void*)nMeshGetVertices },
3009{"rsnMeshGetIndices", "(JJ[J[II)V", (void*)nMeshGetIndices },
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07003010
Tim Murray56f9e6f2014-05-16 11:47:26 -07003011{"rsnSystemGetPointerSize", "()I", (void*)nSystemGetPointerSize },
Miao Wang0facf022015-11-25 11:21:13 -08003012{"rsnAllocationGetByteBuffer", "(JJ[JIII)Ljava/nio/ByteBuffer;", (void*)nAllocationGetByteBuffer },
Jason Samsd19f10d2009-05-22 14:03:28 -07003013};
3014
3015static int registerFuncs(JNIEnv *_env)
3016{
3017 return android::AndroidRuntime::registerNativeMethods(
3018 _env, classPathName, methods, NELEM(methods));
3019}
3020
3021// ---------------------------------------------------------------------------
3022
3023jint JNI_OnLoad(JavaVM* vm, void* reserved)
3024{
Chris Wailes488230c32014-08-14 11:22:40 -07003025 JNIEnv* env = nullptr;
Jason Samsd19f10d2009-05-22 14:03:28 -07003026 jint result = -1;
3027
Jason Samsd19f10d2009-05-22 14:03:28 -07003028 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
Steve Block3762c312012-01-06 19:20:56 +00003029 ALOGE("ERROR: GetEnv failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07003030 goto bail;
3031 }
Chris Wailes488230c32014-08-14 11:22:40 -07003032 assert(env != nullptr);
Jason Samsd19f10d2009-05-22 14:03:28 -07003033
3034 if (registerFuncs(env) < 0) {
Ashok Bhat0e0c0882014-02-04 14:57:58 +00003035 ALOGE("ERROR: Renderscript native registration failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07003036 goto bail;
3037 }
3038
3039 /* success -- return valid version number */
3040 result = JNI_VERSION_1_4;
3041
3042bail:
3043 return result;
3044}