blob: be7071e05044219c2564bd65da49d0c8392e6e6c [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 Sams3eaa3382009-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;
396 uintptr_t* values;
397 RsClosure* depClosures;
398 RsScriptFieldID* depFieldIDs;
399
400 if (fieldIDs_length != values_length || values_length != sizes_length) {
401 ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
402 goto exit;
403 }
404
405 numValues = (size_t)fieldIDs_length;
406
407 if (depClosures_length != depFieldIDs_length) {
408 ALOGE("Unmatched closures and field IDs for dependencies in closure creation.");
409 goto exit;
410 }
411
412 numDependencies = (size_t)depClosures_length;
413
414 if (numDependencies > numValues) {
415 ALOGE("Unexpected number of dependencies in closure creation");
416 goto exit;
417 }
418
Yang Ni7b2a46f2015-05-05 12:41:19 -0700419 if (numValues > RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700420 ALOGE("Too many arguments or globals in closure creation");
421 goto exit;
422 }
423
424 fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
425 if (fieldIDs == nullptr) {
426 goto exit;
427 }
428
429 for (size_t i = 0; i < numValues; i++) {
430 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
431 }
432
433 values = (uintptr_t*)alloca(sizeof(uintptr_t) * numValues);
434 if (values == nullptr) {
435 goto exit;
436 }
437
438 for (size_t i = 0; i < numValues; i++) {
439 values[i] = (uintptr_t)jValues[i];
440 }
441
442 depClosures = (RsClosure*)alloca(sizeof(RsClosure) * numDependencies);
443 if (depClosures == nullptr) {
444 goto exit;
445 }
446
447 for (size_t i = 0; i < numDependencies; i++) {
448 depClosures[i] = (RsClosure)jDepClosures[i];
449 }
450
451 depFieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numDependencies);
452 if (depFieldIDs == nullptr) {
453 goto exit;
454 }
455
456 for (size_t i = 0; i < numDependencies; i++) {
Yang Ni281c3252014-10-24 08:52:24 -0700457 depFieldIDs[i] = (RsClosure)jDepFieldIDs[i];
458 }
459
Yang Ni17c2d7a2015-04-30 16:13:54 -0700460 ret = (jlong)(uintptr_t)rsClosureCreate(
Yang Ni281c3252014-10-24 08:52:24 -0700461 (RsContext)con, (RsScriptKernelID)kernelID, (RsAllocation)returnValue,
Yang Ni17c2d7a2015-04-30 16:13:54 -0700462 fieldIDs, numValues, values, numValues,
463 (int*)jSizes, numValues,
464 depClosures, numDependencies,
465 depFieldIDs, numDependencies);
466
467exit:
468
469 _env->ReleaseLongArrayElements(depFieldIDArray, jDepFieldIDs, JNI_ABORT);
470 _env->ReleaseLongArrayElements(depClosureArray, jDepClosures, JNI_ABORT);
471 _env->ReleaseIntArrayElements (sizeArray, jSizes, JNI_ABORT);
472 _env->ReleaseLongArrayElements(valueArray, jValues, JNI_ABORT);
473 _env->ReleaseLongArrayElements(fieldIDArray, jFieldIDs, JNI_ABORT);
474
475 return ret;
Yang Ni281c3252014-10-24 08:52:24 -0700476}
477
Yang Nibe392ad2015-01-23 17:16:02 -0800478static jlong
479nInvokeClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong invokeID,
480 jbyteArray paramArray, jlongArray fieldIDArray, jlongArray valueArray,
481 jintArray sizeArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700482 jlong ret = 0;
483
Yang Nibe392ad2015-01-23 17:16:02 -0800484 jbyte* jParams = _env->GetByteArrayElements(paramArray, nullptr);
485 jsize jParamLength = _env->GetArrayLength(paramArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700486 if (jParams == nullptr) {
487 ALOGE("Failed to get Java array elements: params.");
488 return ret;
489 }
490
Yang Nibe392ad2015-01-23 17:16:02 -0800491 jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
492 jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700493 if (jFieldIDs == nullptr) {
494 ALOGE("Failed to get Java array elements: fieldIDs.");
495 return ret;
496 }
497
Yang Ni17c2d7a2015-04-30 16:13:54 -0700498 jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
499 jsize values_length = _env->GetArrayLength(valueArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700500 if (jValues == nullptr) {
501 ALOGE("Failed to get Java array elements: values.");
502 return ret;
503 }
504
Yang Ni17c2d7a2015-04-30 16:13:54 -0700505 jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
506 jsize sizes_length = _env->GetArrayLength(sizeArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700507 if (jSizes == nullptr) {
508 ALOGE("Failed to get Java array elements: sizes.");
509 return ret;
510 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700511
512 size_t numValues;
513 RsScriptFieldID* fieldIDs;
514 uintptr_t* values;
515
516 if (fieldIDs_length != values_length || values_length != sizes_length) {
517 ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
518 goto exit;
519 }
520
521 numValues = (size_t) fieldIDs_length;
522
Yang Ni7b2a46f2015-05-05 12:41:19 -0700523 if (numValues > RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700524 ALOGE("Too many arguments or globals in closure creation");
525 goto exit;
526 }
527
528 fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
529 if (fieldIDs == nullptr) {
530 goto exit;
531 }
532
533 for (size_t i = 0; i< numValues; i++) {
Yang Nibe392ad2015-01-23 17:16:02 -0800534 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
535 }
536
Yang Ni17c2d7a2015-04-30 16:13:54 -0700537 values = (uintptr_t*)alloca(sizeof(uintptr_t) * numValues);
538 if (values == nullptr) {
539 goto exit;
540 }
541
542 for (size_t i = 0; i < numValues; i++) {
Yang Nibe392ad2015-01-23 17:16:02 -0800543 values[i] = (uintptr_t)jValues[i];
544 }
545
Yang Ni17c2d7a2015-04-30 16:13:54 -0700546 ret = (jlong)(uintptr_t)rsInvokeClosureCreate(
Yang Nibe392ad2015-01-23 17:16:02 -0800547 (RsContext)con, (RsScriptInvokeID)invokeID, jParams, jParamLength,
Yang Ni17c2d7a2015-04-30 16:13:54 -0700548 fieldIDs, numValues, values, numValues,
549 (int*)jSizes, numValues);
550
551exit:
552
553 _env->ReleaseIntArrayElements (sizeArray, jSizes, JNI_ABORT);
554 _env->ReleaseLongArrayElements(valueArray, jValues, JNI_ABORT);
555 _env->ReleaseLongArrayElements(fieldIDArray, jFieldIDs, JNI_ABORT);
556 _env->ReleaseByteArrayElements(paramArray, jParams, JNI_ABORT);
557
558 return ret;
Yang Nibe392ad2015-01-23 17:16:02 -0800559}
560
Yang Ni281c3252014-10-24 08:52:24 -0700561static void
562nClosureSetArg(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
563 jint index, jlong value, jint size) {
564 rsClosureSetArg((RsContext)con, (RsClosure)closureID, (uint32_t)index,
565 (uintptr_t)value, (size_t)size);
566}
567
568static void
569nClosureSetGlobal(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
570 jlong fieldID, jlong value, jint size) {
571 rsClosureSetGlobal((RsContext)con, (RsClosure)closureID,
572 (RsScriptFieldID)fieldID, (uintptr_t)value, (size_t)size);
573}
574
575static long
Yang Ni35be56c2015-04-02 17:47:56 -0700576nScriptGroup2Create(JNIEnv *_env, jobject _this, jlong con, jstring name,
Yang Niebf63402015-01-16 11:06:26 -0800577 jstring cacheDir, jlongArray closureArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700578 jlong ret = 0;
579
Yang Ni35be56c2015-04-02 17:47:56 -0700580 AutoJavaStringToUTF8 nameUTF(_env, name);
Yang Niebf63402015-01-16 11:06:26 -0800581 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
582
Yang Ni281c3252014-10-24 08:52:24 -0700583 jlong* jClosures = _env->GetLongArrayElements(closureArray, nullptr);
584 jsize numClosures = _env->GetArrayLength(closureArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700585 if (jClosures == nullptr) {
586 ALOGE("Failed to get Java array elements: closures.");
587 return ret;
588 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700589
590 RsClosure* closures;
591
Yang Ni7b2a46f2015-05-05 12:41:19 -0700592 if (numClosures > (jsize) RS_SCRIPT_GROUP_MAX_NUMBER_CLOSURES) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700593 ALOGE("Too many closures in script group");
594 goto exit;
595 }
596
597 closures = (RsClosure*)alloca(sizeof(RsClosure) * numClosures);
598 if (closures == nullptr) {
599 goto exit;
600 }
601
Yang Ni281c3252014-10-24 08:52:24 -0700602 for (int i = 0; i < numClosures; i++) {
603 closures[i] = (RsClosure)jClosures[i];
604 }
605
Yang Ni17c2d7a2015-04-30 16:13:54 -0700606 ret = (jlong)(uintptr_t)rsScriptGroup2Create(
Yang Ni35be56c2015-04-02 17:47:56 -0700607 (RsContext)con, nameUTF.c_str(), nameUTF.length(),
608 cacheDirUTF.c_str(), cacheDirUTF.length(),
Yang Niebf63402015-01-16 11:06:26 -0800609 closures, numClosures);
Yang Ni17c2d7a2015-04-30 16:13:54 -0700610
611exit:
612
613 _env->ReleaseLongArrayElements(closureArray, jClosures, JNI_ABORT);
614
615 return ret;
Yang Ni281c3252014-10-24 08:52:24 -0700616}
617
618static void
619nScriptGroup2Execute(JNIEnv *_env, jobject _this, jlong con, jlong groupID) {
620 rsScriptGroupExecute((RsContext)con, (RsScriptGroup2)groupID);
621}
622
Jason Sams96ed4cf2010-06-15 12:15:57 -0700623static void
Tim Murray25207df2015-01-12 16:47:56 -0800624nScriptIntrinsicBLAS_Single(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
625 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
626 jfloat alpha, jlong A, jlong B, jfloat beta, jlong C, jint incX, jint incY,
627 jint KL, jint KU) {
628 RsBlasCall call;
629 memset(&call, 0, sizeof(call));
630 call.func = (RsBlasFunction)func;
631 call.transA = (RsBlasTranspose)TransA;
632 call.transB = (RsBlasTranspose)TransB;
633 call.side = (RsBlasSide)Side;
634 call.uplo = (RsBlasUplo)Uplo;
635 call.diag = (RsBlasDiag)Diag;
636 call.M = M;
637 call.N = N;
638 call.K = K;
639 call.alpha.f = alpha;
640 call.beta.f = beta;
641 call.incX = incX;
642 call.incY = incY;
643 call.KL = KL;
644 call.KU = KU;
645
646 RsAllocation in_allocs[3];
647 in_allocs[0] = (RsAllocation)A;
648 in_allocs[1] = (RsAllocation)B;
649 in_allocs[2] = (RsAllocation)C;
650
651 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
652 in_allocs, sizeof(in_allocs), nullptr,
653 &call, sizeof(call), nullptr, 0);
654}
655
656static void
657nScriptIntrinsicBLAS_Double(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
658 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
659 jdouble alpha, jlong A, jlong B, jdouble beta, jlong C, jint incX, jint incY,
660 jint KL, jint KU) {
661 RsBlasCall call;
662 memset(&call, 0, sizeof(call));
663 call.func = (RsBlasFunction)func;
664 call.transA = (RsBlasTranspose)TransA;
665 call.transB = (RsBlasTranspose)TransB;
666 call.side = (RsBlasSide)Side;
667 call.uplo = (RsBlasUplo)Uplo;
668 call.diag = (RsBlasDiag)Diag;
669 call.M = M;
670 call.N = N;
671 call.K = K;
672 call.alpha.d = alpha;
673 call.beta.d = beta;
674 call.incX = incX;
675 call.incY = incY;
676 call.KL = KL;
677 call.KU = KU;
678
679 RsAllocation in_allocs[3];
680 in_allocs[0] = (RsAllocation)A;
681 in_allocs[1] = (RsAllocation)B;
682 in_allocs[2] = (RsAllocation)C;
683
684 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700685 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800686 &call, sizeof(call), nullptr, 0);
687}
688
689static void
690nScriptIntrinsicBLAS_Complex(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
691 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
692 jfloat alphaX, jfloat alphaY, jlong A, jlong B, jfloat betaX,
693 jfloat betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
694 RsBlasCall call;
695 memset(&call, 0, sizeof(call));
696 call.func = (RsBlasFunction)func;
697 call.transA = (RsBlasTranspose)TransA;
698 call.transB = (RsBlasTranspose)TransB;
699 call.side = (RsBlasSide)Side;
700 call.uplo = (RsBlasUplo)Uplo;
701 call.diag = (RsBlasDiag)Diag;
702 call.M = M;
703 call.N = N;
704 call.K = K;
705 call.alpha.c.r = alphaX;
706 call.alpha.c.i = alphaY;
707 call.beta.c.r = betaX;
Miao Wang82585b32015-04-30 13:44:49 -0700708 call.beta.c.i = betaY;
Tim Murray25207df2015-01-12 16:47:56 -0800709 call.incX = incX;
710 call.incY = incY;
711 call.KL = KL;
712 call.KU = KU;
713
714 RsAllocation in_allocs[3];
715 in_allocs[0] = (RsAllocation)A;
716 in_allocs[1] = (RsAllocation)B;
717 in_allocs[2] = (RsAllocation)C;
718
719 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700720 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800721 &call, sizeof(call), nullptr, 0);
722}
723
724static void
725nScriptIntrinsicBLAS_Z(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
726 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
727 jdouble alphaX, jdouble alphaY, jlong A, jlong B, jdouble betaX,
728 jdouble betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
729 RsBlasCall call;
730 memset(&call, 0, sizeof(call));
731 call.func = (RsBlasFunction)func;
732 call.transA = (RsBlasTranspose)TransA;
733 call.transB = (RsBlasTranspose)TransB;
734 call.side = (RsBlasSide)Side;
735 call.uplo = (RsBlasUplo)Uplo;
736 call.diag = (RsBlasDiag)Diag;
737 call.M = M;
738 call.N = N;
739 call.K = K;
740 call.alpha.z.r = alphaX;
741 call.alpha.z.i = alphaY;
742 call.beta.z.r = betaX;
Miao Wang82585b32015-04-30 13:44:49 -0700743 call.beta.z.i = betaY;
Tim Murray25207df2015-01-12 16:47:56 -0800744 call.incX = incX;
745 call.incY = incY;
746 call.KL = KL;
747 call.KU = KU;
748
749 RsAllocation in_allocs[3];
750 in_allocs[0] = (RsAllocation)A;
751 in_allocs[1] = (RsAllocation)B;
752 in_allocs[2] = (RsAllocation)C;
753
754 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700755 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800756 &call, sizeof(call), nullptr, 0);
757}
758
759
760static void
Tim Murray9cb16a22015-04-01 11:07:16 -0700761nScriptIntrinsicBLAS_BNNM(JNIEnv *_env, jobject _this, jlong con, jlong id, jint M, jint N, jint K,
762 jlong A, jint a_offset, jlong B, jint b_offset, jlong C, jint c_offset,
763 jint c_mult_int) {
764 RsBlasCall call;
765 memset(&call, 0, sizeof(call));
766 call.func = RsBlas_bnnm;
767 call.M = M;
768 call.N = N;
769 call.K = K;
Miao Wang25148062015-06-29 17:43:03 -0700770 call.a_offset = a_offset & 0xFF;
771 call.b_offset = b_offset & 0xFF;
Tim Murray9cb16a22015-04-01 11:07:16 -0700772 call.c_offset = c_offset;
773 call.c_mult_int = c_mult_int;
774
775 RsAllocation in_allocs[3];
776 in_allocs[0] = (RsAllocation)A;
777 in_allocs[1] = (RsAllocation)B;
778 in_allocs[2] = (RsAllocation)C;
779
780 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700781 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray9cb16a22015-04-01 11:07:16 -0700782 &call, sizeof(call), nullptr, 0);
783}
784
785
786static void
Tim Murray460a0492013-11-19 12:45:54 -0800787nAssignName(JNIEnv *_env, jobject _this, jlong con, jlong obj, jbyteArray str)
Jason Sams3eaa3382009-06-10 15:04:38 -0700788{
Andreas Gampe67333922014-11-10 20:35:59 -0800789 if (kLogApi) {
790 ALOGD("nAssignName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
791 }
Jason Sams3eaa3382009-06-10 15:04:38 -0700792 jint len = _env->GetArrayLength(str);
793 jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
Miao Wangba8766c2015-10-12 17:24:13 -0700794 if (cptr == nullptr) {
795 ALOGE("Failed to get Java array elements");
796 return;
797 }
798
Tim Murrayeff663f2013-11-15 13:08:30 -0800799 rsAssignName((RsContext)con, (void *)obj, (const char *)cptr, len);
Jason Sams3eaa3382009-06-10 15:04:38 -0700800 _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
801}
802
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700803static jstring
Tim Murray460a0492013-11-19 12:45:54 -0800804nGetName(JNIEnv *_env, jobject _this, jlong con, jlong obj)
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700805{
Andreas Gampe67333922014-11-10 20:35:59 -0800806 if (kLogApi) {
807 ALOGD("nGetName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
808 }
Chris Wailes488230c32014-08-14 11:22:40 -0700809 const char *name = nullptr;
Tim Murrayeff663f2013-11-15 13:08:30 -0800810 rsaGetName((RsContext)con, (void *)obj, &name);
Chris Wailes488230c32014-08-14 11:22:40 -0700811 if(name == nullptr || strlen(name) == 0) {
812 return nullptr;
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700813 }
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700814 return _env->NewStringUTF(name);
815}
816
Jason Sams7ce033d2009-08-18 14:14:24 -0700817static void
Tim Murray460a0492013-11-19 12:45:54 -0800818nObjDestroy(JNIEnv *_env, jobject _this, jlong con, jlong obj)
Jason Sams7ce033d2009-08-18 14:14:24 -0700819{
Andreas Gampe67333922014-11-10 20:35:59 -0800820 if (kLogApi) {
821 ALOGD("nObjDestroy, con(%p) obj(%p)", (RsContext)con, (void *)obj);
822 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800823 rsObjDestroy((RsContext)con, (void *)obj);
Jason Sams7ce033d2009-08-18 14:14:24 -0700824}
825
Jason Sams3eaa3382009-06-10 15:04:38 -0700826// ---------------------------------------------------------------------------
827
Tim Murrayeff663f2013-11-15 13:08:30 -0800828static jlong
Jason Samsd19f10d2009-05-22 14:03:28 -0700829nDeviceCreate(JNIEnv *_env, jobject _this)
830{
Andreas Gampe67333922014-11-10 20:35:59 -0800831 if (kLogApi) {
832 ALOGD("nDeviceCreate");
833 }
Tim Murray3aa89c12014-08-18 17:51:22 -0700834 return (jlong)(uintptr_t)rsDeviceCreate();
Jason Samsd19f10d2009-05-22 14:03:28 -0700835}
836
837static void
Tim Murray5eaf4682014-01-10 11:25:52 -0800838nDeviceDestroy(JNIEnv *_env, jobject _this, jlong dev)
Jason Samsd19f10d2009-05-22 14:03:28 -0700839{
Andreas Gampe67333922014-11-10 20:35:59 -0800840 if (kLogApi) {
841 ALOGD("nDeviceDestroy");
842 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700843 return rsDeviceDestroy((RsDevice)dev);
844}
845
Jason Samsebfb4362009-09-23 13:57:02 -0700846static void
Tim Murray5eaf4682014-01-10 11:25:52 -0800847nDeviceSetConfig(JNIEnv *_env, jobject _this, jlong dev, jint p, jint value)
Jason Samsebfb4362009-09-23 13:57:02 -0700848{
Andreas Gampe67333922014-11-10 20:35:59 -0800849 if (kLogApi) {
850 ALOGD("nDeviceSetConfig dev(%p), param(%i), value(%i)", (void *)dev, p, value);
851 }
Jason Samsebfb4362009-09-23 13:57:02 -0700852 return rsDeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value);
853}
854
Tim Murrayeff663f2013-11-15 13:08:30 -0800855static jlong
Jason Sams81cd2b12014-12-02 12:36:43 -0800856nContextCreate(JNIEnv *_env, jobject _this, jlong dev, jint flags, jint sdkVer, jint contextType)
Jason Samsd19f10d2009-05-22 14:03:28 -0700857{
Andreas Gampe67333922014-11-10 20:35:59 -0800858 if (kLogApi) {
859 ALOGD("nContextCreate");
860 }
Jason Sams81cd2b12014-12-02 12:36:43 -0800861 return (jlong)(uintptr_t)rsContextCreate((RsDevice)dev, 0, sdkVer, (RsContextType)contextType, flags);
Jason Sams704ff642010-02-09 16:05:07 -0800862}
863
Tim Murrayeff663f2013-11-15 13:08:30 -0800864static jlong
Tim Murray5eaf4682014-01-10 11:25:52 -0800865nContextCreateGL(JNIEnv *_env, jobject _this, jlong dev, jint ver, jint sdkVer,
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000866 jint colorMin, jint colorPref,
867 jint alphaMin, jint alphaPref,
868 jint depthMin, jint depthPref,
869 jint stencilMin, jint stencilPref,
870 jint samplesMin, jint samplesPref, jfloat samplesQ,
871 jint dpi)
Jason Sams704ff642010-02-09 16:05:07 -0800872{
Jason Sams11c8af92010-10-13 15:31:10 -0700873 RsSurfaceConfig sc;
874 sc.alphaMin = alphaMin;
875 sc.alphaPref = alphaPref;
876 sc.colorMin = colorMin;
877 sc.colorPref = colorPref;
878 sc.depthMin = depthMin;
879 sc.depthPref = depthPref;
880 sc.samplesMin = samplesMin;
881 sc.samplesPref = samplesPref;
882 sc.samplesQ = samplesQ;
883
Andreas Gampe67333922014-11-10 20:35:59 -0800884 if (kLogApi) {
885 ALOGD("nContextCreateGL");
886 }
Tim Murray3aa89c12014-08-18 17:51:22 -0700887 return (jlong)(uintptr_t)rsContextCreateGL((RsDevice)dev, ver, sdkVer, sc, dpi);
Jason Samsd19f10d2009-05-22 14:03:28 -0700888}
889
890static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800891nContextSetPriority(JNIEnv *_env, jobject _this, jlong con, jint p)
Jason Sams7d787b42009-11-15 12:14:26 -0800892{
Andreas Gampe67333922014-11-10 20:35:59 -0800893 if (kLogApi) {
894 ALOGD("ContextSetPriority, con(%p), priority(%i)", (RsContext)con, p);
895 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800896 rsContextSetPriority((RsContext)con, p);
Jason Sams7d787b42009-11-15 12:14:26 -0800897}
898
Tim Murray47f31582015-04-07 15:43:24 -0700899static void
900nContextSetCacheDir(JNIEnv *_env, jobject _this, jlong con, jstring cacheDir)
901{
902 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
903
904 if (kLogApi) {
905 ALOGD("ContextSetCacheDir, con(%p), cacheDir(%s)", (RsContext)con, cacheDirUTF.c_str());
906 }
907 rsContextSetCacheDir((RsContext)con, cacheDirUTF.c_str(), cacheDirUTF.length());
908}
909
Jason Sams7d787b42009-11-15 12:14:26 -0800910
911
912static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800913nContextSetSurface(JNIEnv *_env, jobject _this, jlong con, jint width, jint height, jobject wnd)
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800914{
Andreas Gampe67333922014-11-10 20:35:59 -0800915 if (kLogApi) {
916 ALOGD("nContextSetSurface, con(%p), width(%i), height(%i), surface(%p)", (RsContext)con,
917 width, height, (Surface *)wnd);
918 }
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800919
Chris Wailes488230c32014-08-14 11:22:40 -0700920 ANativeWindow * window = nullptr;
921 if (wnd == nullptr) {
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800922
923 } else {
Jeff Brown64a55af2012-08-26 02:47:39 -0700924 window = android_view_Surface_getNativeWindow(_env, wnd).get();
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800925 }
926
Tim Murrayeff663f2013-11-15 13:08:30 -0800927 rsContextSetSurface((RsContext)con, width, height, window);
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800928}
929
930static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800931nContextDestroy(JNIEnv *_env, jobject _this, jlong con)
Jason Samsd19f10d2009-05-22 14:03:28 -0700932{
Andreas Gampe67333922014-11-10 20:35:59 -0800933 if (kLogApi) {
934 ALOGD("nContextDestroy, con(%p)", (RsContext)con);
935 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800936 rsContextDestroy((RsContext)con);
Jason Samsd19f10d2009-05-22 14:03:28 -0700937}
938
Jason Sams715333b2009-11-17 17:26:46 -0800939static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800940nContextDump(JNIEnv *_env, jobject _this, jlong con, jint bits)
Jason Sams715333b2009-11-17 17:26:46 -0800941{
Andreas Gampe67333922014-11-10 20:35:59 -0800942 if (kLogApi) {
943 ALOGD("nContextDump, con(%p) bits(%i)", (RsContext)con, bits);
944 }
Jason Sams715333b2009-11-17 17:26:46 -0800945 rsContextDump((RsContext)con, bits);
946}
Jason Samsd19f10d2009-05-22 14:03:28 -0700947
948static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800949nContextPause(JNIEnv *_env, jobject _this, jlong con)
Jason Sams65e7aa52009-09-24 17:38:20 -0700950{
Andreas Gampe67333922014-11-10 20:35:59 -0800951 if (kLogApi) {
952 ALOGD("nContextPause, con(%p)", (RsContext)con);
953 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800954 rsContextPause((RsContext)con);
Jason Sams65e7aa52009-09-24 17:38:20 -0700955}
956
957static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800958nContextResume(JNIEnv *_env, jobject _this, jlong con)
Jason Sams65e7aa52009-09-24 17:38:20 -0700959{
Andreas Gampe67333922014-11-10 20:35:59 -0800960 if (kLogApi) {
961 ALOGD("nContextResume, con(%p)", (RsContext)con);
962 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800963 rsContextResume((RsContext)con);
Jason Sams65e7aa52009-09-24 17:38:20 -0700964}
965
Jason Sams1c415172010-11-08 17:06:46 -0800966
967static jstring
Tim Murrayeff663f2013-11-15 13:08:30 -0800968nContextGetErrorMessage(JNIEnv *_env, jobject _this, jlong con)
Jason Sams1c415172010-11-08 17:06:46 -0800969{
Andreas Gampe67333922014-11-10 20:35:59 -0800970 if (kLogApi) {
971 ALOGD("nContextGetErrorMessage, con(%p)", (RsContext)con);
972 }
Jason Sams1c415172010-11-08 17:06:46 -0800973 char buf[1024];
974
975 size_t receiveLen;
976 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800977 int id = rsContextGetMessage((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -0700978 buf, sizeof(buf),
979 &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -0700980 &subID, sizeof(subID));
Jason Sams1c415172010-11-08 17:06:46 -0800981 if (!id && receiveLen) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +0100982 ALOGV("message receive buffer too small. %zu", receiveLen);
Jason Sams1c415172010-11-08 17:06:46 -0800983 }
984 return _env->NewStringUTF(buf);
985}
986
Jason Samsedbfabd2011-05-17 15:01:29 -0700987static jint
Tim Murrayeff663f2013-11-15 13:08:30 -0800988nContextGetUserMessage(JNIEnv *_env, jobject _this, jlong con, jintArray data)
Jason Sams516c3192009-10-06 13:58:47 -0700989{
Jason Sams516c3192009-10-06 13:58:47 -0700990 jint len = _env->GetArrayLength(data);
Andreas Gampe67333922014-11-10 20:35:59 -0800991 if (kLogApi) {
992 ALOGD("nContextGetMessage, con(%p), len(%i)", (RsContext)con, len);
993 }
Chris Wailes488230c32014-08-14 11:22:40 -0700994 jint *ptr = _env->GetIntArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -0700995 if (ptr == nullptr) {
996 ALOGE("Failed to get Java array elements");
997 return 0;
998 }
Jason Sams516c3192009-10-06 13:58:47 -0700999 size_t receiveLen;
Jason Sams1c415172010-11-08 17:06:46 -08001000 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -08001001 int id = rsContextGetMessage((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001002 ptr, len * 4,
1003 &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -07001004 &subID, sizeof(subID));
Jason Sams516c3192009-10-06 13:58:47 -07001005 if (!id && receiveLen) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001006 ALOGV("message receive buffer too small. %zu", receiveLen);
Jason Sams516c3192009-10-06 13:58:47 -07001007 }
1008 _env->ReleaseIntArrayElements(data, ptr, 0);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001009 return (jint)id;
Jason Sams1c415172010-11-08 17:06:46 -08001010}
1011
1012static jint
Tim Murrayeff663f2013-11-15 13:08:30 -08001013nContextPeekMessage(JNIEnv *_env, jobject _this, jlong con, jintArray auxData)
Jason Sams1c415172010-11-08 17:06:46 -08001014{
Andreas Gampe67333922014-11-10 20:35:59 -08001015 if (kLogApi) {
1016 ALOGD("nContextPeekMessage, con(%p)", (RsContext)con);
1017 }
Chris Wailes488230c32014-08-14 11:22:40 -07001018 jint *auxDataPtr = _env->GetIntArrayElements(auxData, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001019 if (auxDataPtr == nullptr) {
1020 ALOGE("Failed to get Java array elements");
1021 return 0;
1022 }
Jason Sams1c415172010-11-08 17:06:46 -08001023 size_t receiveLen;
1024 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -08001025 int id = rsContextPeekMessage((RsContext)con, &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -07001026 &subID, sizeof(subID));
Jason Sams1c415172010-11-08 17:06:46 -08001027 auxDataPtr[0] = (jint)subID;
1028 auxDataPtr[1] = (jint)receiveLen;
1029 _env->ReleaseIntArrayElements(auxData, auxDataPtr, 0);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001030 return (jint)id;
Jason Sams516c3192009-10-06 13:58:47 -07001031}
1032
Tim Murrayeff663f2013-11-15 13:08:30 -08001033static void nContextInitToClient(JNIEnv *_env, jobject _this, jlong con)
Jason Sams516c3192009-10-06 13:58:47 -07001034{
Andreas Gampe67333922014-11-10 20:35:59 -08001035 if (kLogApi) {
1036 ALOGD("nContextInitToClient, con(%p)", (RsContext)con);
1037 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001038 rsContextInitToClient((RsContext)con);
Jason Sams516c3192009-10-06 13:58:47 -07001039}
1040
Tim Murrayeff663f2013-11-15 13:08:30 -08001041static void nContextDeinitToClient(JNIEnv *_env, jobject _this, jlong con)
Jason Sams516c3192009-10-06 13:58:47 -07001042{
Andreas Gampe67333922014-11-10 20:35:59 -08001043 if (kLogApi) {
1044 ALOGD("nContextDeinitToClient, con(%p)", (RsContext)con);
1045 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001046 rsContextDeinitToClient((RsContext)con);
Jason Sams516c3192009-10-06 13:58:47 -07001047}
1048
Jason Sams455d6442013-02-05 19:20:18 -08001049static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001050nContextSendMessage(JNIEnv *_env, jobject _this, jlong con, jint id, jintArray data)
Jason Sams455d6442013-02-05 19:20:18 -08001051{
Chris Wailes488230c32014-08-14 11:22:40 -07001052 jint *ptr = nullptr;
Jason Sams455d6442013-02-05 19:20:18 -08001053 jint len = 0;
1054 if (data) {
1055 len = _env->GetArrayLength(data);
Stephen Hines4a043c12014-08-21 23:20:32 -07001056 ptr = _env->GetIntArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001057 if (ptr == nullptr) {
1058 ALOGE("Failed to get Java array elements");
1059 return;
1060 }
Jason Sams455d6442013-02-05 19:20:18 -08001061 }
Andreas Gampe67333922014-11-10 20:35:59 -08001062 if (kLogApi) {
1063 ALOGD("nContextSendMessage, con(%p), id(%i), len(%i)", (RsContext)con, id, len);
1064 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001065 rsContextSendMessage((RsContext)con, id, (const uint8_t *)ptr, len * sizeof(int));
Jason Sams455d6442013-02-05 19:20:18 -08001066 if (data) {
1067 _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
1068 }
1069}
1070
1071
Jason Sams516c3192009-10-06 13:58:47 -07001072
Tim Murray460a0492013-11-19 12:45:54 -08001073static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001074nElementCreate(JNIEnv *_env, jobject _this, jlong con, jlong type, jint kind, jboolean norm,
1075 jint size)
Jason Samsd19f10d2009-05-22 14:03:28 -07001076{
Andreas Gampe67333922014-11-10 20:35:59 -08001077 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001078 ALOGD("nElementCreate, con(%p), type(%" PRId64 "), kind(%i), norm(%i), size(%i)", (RsContext)con,
Andreas Gampe67333922014-11-10 20:35:59 -08001079 type, kind, norm, size);
1080 }
1081 return (jlong)(uintptr_t)rsElementCreate((RsContext)con, (RsDataType)type, (RsDataKind)kind,
1082 norm, size);
Jason Samsd19f10d2009-05-22 14:03:28 -07001083}
1084
Tim Murray460a0492013-11-19 12:45:54 -08001085static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001086nElementCreate2(JNIEnv *_env, jobject _this, jlong con,
Ashok Bhat98071552014-02-12 09:54:43 +00001087 jlongArray _ids, jobjectArray _names, jintArray _arraySizes)
Jason Samsd19f10d2009-05-22 14:03:28 -07001088{
Jason Sams718cd1f2009-12-23 14:35:29 -08001089 int fieldCount = _env->GetArrayLength(_ids);
Andreas Gampe67333922014-11-10 20:35:59 -08001090 if (kLogApi) {
1091 ALOGD("nElementCreate2, con(%p)", (RsContext)con);
1092 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001093
Chris Wailes488230c32014-08-14 11:22:40 -07001094 jlong *jIds = _env->GetLongArrayElements(_ids, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001095 if (jIds == nullptr) {
1096 ALOGE("Failed to get Java array elements: ids");
1097 return 0;
1098 }
Chris Wailes488230c32014-08-14 11:22:40 -07001099 jint *jArraySizes = _env->GetIntArrayElements(_arraySizes, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001100 if (jArraySizes == nullptr) {
1101 ALOGE("Failed to get Java array elements: arraySizes");
1102 return 0;
1103 }
Ashok Bhat98071552014-02-12 09:54:43 +00001104
1105 RsElement *ids = (RsElement*)malloc(fieldCount * sizeof(RsElement));
1106 uint32_t *arraySizes = (uint32_t *)malloc(fieldCount * sizeof(uint32_t));
1107
1108 for(int i = 0; i < fieldCount; i ++) {
1109 ids[i] = (RsElement)jIds[i];
1110 arraySizes[i] = (uint32_t)jArraySizes[i];
1111 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001112
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08001113 AutoJavaStringArrayToUTF8 names(_env, _names, fieldCount);
1114
1115 const char **nameArray = names.c_str();
1116 size_t *sizeArray = names.c_str_len();
1117
Tim Murray3aa89c12014-08-18 17:51:22 -07001118 jlong id = (jlong)(uintptr_t)rsElementCreate2((RsContext)con,
Ashok Bhat98071552014-02-12 09:54:43 +00001119 (const RsElement *)ids, fieldCount,
Jason Sams7a22e102011-05-06 14:14:30 -07001120 nameArray, fieldCount * sizeof(size_t), sizeArray,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001121 (const uint32_t *)arraySizes, fieldCount);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08001122
Ashok Bhat98071552014-02-12 09:54:43 +00001123 free(ids);
1124 free(arraySizes);
1125 _env->ReleaseLongArrayElements(_ids, jIds, JNI_ABORT);
1126 _env->ReleaseIntArrayElements(_arraySizes, jArraySizes, JNI_ABORT);
1127
Tim Murray3aa89c12014-08-18 17:51:22 -07001128 return (jlong)(uintptr_t)id;
Jason Samsd19f10d2009-05-22 14:03:28 -07001129}
1130
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001131static void
Tim Murray460a0492013-11-19 12:45:54 -08001132nElementGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jintArray _elementData)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001133{
1134 int dataSize = _env->GetArrayLength(_elementData);
Andreas Gampe67333922014-11-10 20:35:59 -08001135 if (kLogApi) {
1136 ALOGD("nElementGetNativeData, con(%p)", (RsContext)con);
1137 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001138
1139 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
1140 assert(dataSize == 5);
1141
Narayan Kamath78c0ce52014-03-19 10:15:51 +00001142 uintptr_t elementData[5];
Tim Murrayeff663f2013-11-15 13:08:30 -08001143 rsaElementGetNativeData((RsContext)con, (RsElement)id, elementData, dataSize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001144
1145 for(jint i = 0; i < dataSize; i ++) {
Ashok Bhat98071552014-02-12 09:54:43 +00001146 const jint data = (jint)elementData[i];
1147 _env->SetIntArrayRegion(_elementData, i, 1, &data);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001148 }
1149}
1150
1151
1152static void
Tim Murray460a0492013-11-19 12:45:54 -08001153nElementGetSubElements(JNIEnv *_env, jobject _this, jlong con, jlong id,
Ashok Bhat98071552014-02-12 09:54:43 +00001154 jlongArray _IDs,
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -07001155 jobjectArray _names,
1156 jintArray _arraySizes)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001157{
Ashok Bhat98071552014-02-12 09:54:43 +00001158 uint32_t dataSize = _env->GetArrayLength(_IDs);
Andreas Gampe67333922014-11-10 20:35:59 -08001159 if (kLogApi) {
1160 ALOGD("nElementGetSubElements, con(%p)", (RsContext)con);
1161 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001162
Ashok Bhat98071552014-02-12 09:54:43 +00001163 uintptr_t *ids = (uintptr_t*)malloc(dataSize * sizeof(uintptr_t));
1164 const char **names = (const char **)malloc(dataSize * sizeof(const char *));
Narayan Kamath78c0ce52014-03-19 10:15:51 +00001165 uint32_t *arraySizes = (uint32_t *)malloc(dataSize * sizeof(uint32_t));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001166
Andreas Gampe67333922014-11-10 20:35:59 -08001167 rsaElementGetSubElements((RsContext)con, (RsElement)id, ids, names, arraySizes,
1168 (uint32_t)dataSize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001169
Ashok Bhat98071552014-02-12 09:54:43 +00001170 for(uint32_t i = 0; i < dataSize; i++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07001171 const jlong id = (jlong)(uintptr_t)ids[i];
Ashok Bhat98071552014-02-12 09:54:43 +00001172 const jint arraySize = (jint)arraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001173 _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
Ashok Bhat98071552014-02-12 09:54:43 +00001174 _env->SetLongArrayRegion(_IDs, i, 1, &id);
1175 _env->SetIntArrayRegion(_arraySizes, i, 1, &arraySize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001176 }
1177
1178 free(ids);
1179 free(names);
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -07001180 free(arraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001181}
1182
Jason Samsd19f10d2009-05-22 14:03:28 -07001183// -----------------------------------
1184
Tim Murray460a0492013-11-19 12:45:54 -08001185static jlong
1186nTypeCreate(JNIEnv *_env, jobject _this, jlong con, jlong eid,
Jason Samsb109cc72013-01-07 18:20:12 -08001187 jint dimx, jint dimy, jint dimz, jboolean mips, jboolean faces, jint yuv)
Jason Samsd19f10d2009-05-22 14:03:28 -07001188{
Andreas Gampe67333922014-11-10 20:35:59 -08001189 if (kLogApi) {
1190 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 +01001191 (RsContext)con, (void*)eid, dimx, dimy, dimz, mips, faces, yuv);
Andreas Gampe67333922014-11-10 20:35:59 -08001192 }
Jason Sams3b9c52a2010-10-14 17:48:46 -07001193
Andreas Gampe67333922014-11-10 20:35:59 -08001194 return (jlong)(uintptr_t)rsTypeCreate((RsContext)con, (RsElement)eid, dimx, dimy, dimz, mips,
1195 faces, yuv);
Jason Samsd19f10d2009-05-22 14:03:28 -07001196}
1197
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001198static void
Ashok Bhat98071552014-02-12 09:54:43 +00001199nTypeGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jlongArray _typeData)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001200{
1201 // We are packing 6 items: mDimX; mDimY; mDimZ;
1202 // mDimLOD; mDimFaces; mElement; into typeData
1203 int elementCount = _env->GetArrayLength(_typeData);
1204
1205 assert(elementCount == 6);
Andreas Gampe67333922014-11-10 20:35:59 -08001206 if (kLogApi) {
1207 ALOGD("nTypeGetNativeData, con(%p)", (RsContext)con);
1208 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001209
Ashok Bhat98071552014-02-12 09:54:43 +00001210 uintptr_t typeData[6];
Tim Murrayeff663f2013-11-15 13:08:30 -08001211 rsaTypeGetNativeData((RsContext)con, (RsType)id, typeData, 6);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001212
1213 for(jint i = 0; i < elementCount; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07001214 const jlong data = (jlong)(uintptr_t)typeData[i];
Ashok Bhat98071552014-02-12 09:54:43 +00001215 _env->SetLongArrayRegion(_typeData, i, 1, &data);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001216 }
1217}
1218
Jason Samsd19f10d2009-05-22 14:03:28 -07001219// -----------------------------------
1220
Tim Murray460a0492013-11-19 12:45:54 -08001221static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001222nAllocationCreateTyped(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mips, jint usage,
1223 jlong pointer)
Jason Samsd19f10d2009-05-22 14:03:28 -07001224{
Andreas Gampe67333922014-11-10 20:35:59 -08001225 if (kLogApi) {
1226 ALOGD("nAllocationCreateTyped, con(%p), type(%p), mip(%i), usage(%i), ptr(%p)",
1227 (RsContext)con, (RsElement)type, mips, usage, (void *)pointer);
1228 }
1229 return (jlong)(uintptr_t) rsAllocationCreateTyped((RsContext)con, (RsType)type,
1230 (RsAllocationMipmapControl)mips,
1231 (uint32_t)usage, (uintptr_t)pointer);
Jason Samsd19f10d2009-05-22 14:03:28 -07001232}
1233
Jason Samsd19f10d2009-05-22 14:03:28 -07001234static void
Tim Murray460a0492013-11-19 12:45:54 -08001235nAllocationSyncAll(JNIEnv *_env, jobject _this, jlong con, jlong a, jint bits)
Jason Sams5476b452010-12-08 16:14:36 -08001236{
Andreas Gampe67333922014-11-10 20:35:59 -08001237 if (kLogApi) {
1238 ALOGD("nAllocationSyncAll, con(%p), a(%p), bits(0x%08x)", (RsContext)con, (RsAllocation)a,
1239 bits);
1240 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001241 rsAllocationSyncAll((RsContext)con, (RsAllocation)a, (RsAllocationUsageType)bits);
Jason Sams5476b452010-12-08 16:14:36 -08001242}
1243
Jason Sams72226e02013-02-22 12:45:54 -08001244static jobject
Tim Murray460a0492013-11-19 12:45:54 -08001245nAllocationGetSurface(JNIEnv *_env, jobject _this, jlong con, jlong a)
Jason Sams615e7ce2012-01-13 14:01:20 -08001246{
Andreas Gampe67333922014-11-10 20:35:59 -08001247 if (kLogApi) {
1248 ALOGD("nAllocationGetSurface, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1249 }
Jason Sams615e7ce2012-01-13 14:01:20 -08001250
Andreas Gampe67333922014-11-10 20:35:59 -08001251 IGraphicBufferProducer *v = (IGraphicBufferProducer *)rsAllocationGetSurface((RsContext)con,
1252 (RsAllocation)a);
Jason Sams72226e02013-02-22 12:45:54 -08001253 sp<IGraphicBufferProducer> bp = v;
Chris Wailes488230c32014-08-14 11:22:40 -07001254 v->decStrong(nullptr);
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001255
Jason Sams72226e02013-02-22 12:45:54 -08001256 jobject o = android_view_Surface_createFromIGraphicBufferProducer(_env, bp);
1257 return o;
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001258}
1259
1260static void
Tim Murray460a0492013-11-19 12:45:54 -08001261nAllocationSetSurface(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject sur)
Jason Sams163766c2012-02-15 12:04:24 -08001262{
Andreas Gampe67333922014-11-10 20:35:59 -08001263 if (kLogApi) {
1264 ALOGD("nAllocationSetSurface, con(%p), alloc(%p), surface(%p)", (RsContext)con,
1265 (RsAllocation)alloc, (Surface *)sur);
1266 }
Jason Sams163766c2012-02-15 12:04:24 -08001267
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001268 sp<Surface> s;
Jason Sams163766c2012-02-15 12:04:24 -08001269 if (sur != 0) {
Jeff Brown64a55af2012-08-26 02:47:39 -07001270 s = android_view_Surface_getSurface(_env, sur);
Jason Sams163766c2012-02-15 12:04:24 -08001271 }
1272
Andreas Gampe67333922014-11-10 20:35:59 -08001273 rsAllocationSetSurface((RsContext)con, (RsAllocation)alloc,
1274 static_cast<ANativeWindow *>(s.get()));
Jason Sams163766c2012-02-15 12:04:24 -08001275}
1276
1277static void
Tim Murray460a0492013-11-19 12:45:54 -08001278nAllocationIoSend(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Sams163766c2012-02-15 12:04:24 -08001279{
Andreas Gampe67333922014-11-10 20:35:59 -08001280 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001281 ALOGD("nAllocationIoSend, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
Andreas Gampe67333922014-11-10 20:35:59 -08001282 }
Tim Murray460a0492013-11-19 12:45:54 -08001283 rsAllocationIoSend((RsContext)con, (RsAllocation)alloc);
Jason Sams163766c2012-02-15 12:04:24 -08001284}
1285
1286static void
Tim Murray460a0492013-11-19 12:45:54 -08001287nAllocationIoReceive(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Sams163766c2012-02-15 12:04:24 -08001288{
Andreas Gampe67333922014-11-10 20:35:59 -08001289 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001290 ALOGD("nAllocationIoReceive, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
Andreas Gampe67333922014-11-10 20:35:59 -08001291 }
Tim Murray460a0492013-11-19 12:45:54 -08001292 rsAllocationIoReceive((RsContext)con, (RsAllocation)alloc);
Jason Sams163766c2012-02-15 12:04:24 -08001293}
1294
1295
1296static void
Tim Murray460a0492013-11-19 12:45:54 -08001297nAllocationGenerateMipmaps(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Samsf7086092011-01-12 13:28:37 -08001298{
Andreas Gampe67333922014-11-10 20:35:59 -08001299 if (kLogApi) {
1300 ALOGD("nAllocationGenerateMipmaps, con(%p), a(%p)", (RsContext)con, (RsAllocation)alloc);
1301 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001302 rsAllocationGenerateMipmaps((RsContext)con, (RsAllocation)alloc);
Jason Samsf7086092011-01-12 13:28:37 -08001303}
1304
Tim Murray460a0492013-11-19 12:45:54 -08001305static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001306nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1307 jobject jbitmap, jint usage)
Jason Samsffe9f482009-06-01 17:45:53 -07001308{
John Recked207b92015-04-10 13:52:57 -07001309 SkBitmap bitmap;
1310 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Samsffe9f482009-06-01 17:45:53 -07001311
Jason Sams5476b452010-12-08 16:14:36 -08001312 bitmap.lockPixels();
1313 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001314 jlong id = (jlong)(uintptr_t)rsAllocationCreateFromBitmap((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001315 (RsType)type, (RsAllocationMipmapControl)mip,
1316 ptr, bitmap.getSize(), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001317 bitmap.unlockPixels();
1318 return id;
Jason Samsffe9f482009-06-01 17:45:53 -07001319}
Jason Samsfe08d992009-05-27 14:45:32 -07001320
Tim Murray460a0492013-11-19 12:45:54 -08001321static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001322nAllocationCreateBitmapBackedAllocation(JNIEnv *_env, jobject _this, jlong con, jlong type,
1323 jint mip, jobject jbitmap, jint usage)
Tim Murraya3145512012-12-04 17:59:29 -08001324{
John Recked207b92015-04-10 13:52:57 -07001325 SkBitmap bitmap;
1326 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Tim Murraya3145512012-12-04 17:59:29 -08001327
1328 bitmap.lockPixels();
1329 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001330 jlong id = (jlong)(uintptr_t)rsAllocationCreateTyped((RsContext)con,
Tim Murraya3145512012-12-04 17:59:29 -08001331 (RsType)type, (RsAllocationMipmapControl)mip,
Ashok Bhat98071552014-02-12 09:54:43 +00001332 (uint32_t)usage, (uintptr_t)ptr);
Tim Murraya3145512012-12-04 17:59:29 -08001333 bitmap.unlockPixels();
1334 return id;
1335}
1336
Tim Murray460a0492013-11-19 12:45:54 -08001337static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001338nAllocationCubeCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1339 jobject jbitmap, jint usage)
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001340{
John Recked207b92015-04-10 13:52:57 -07001341 SkBitmap bitmap;
1342 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001343
Jason Sams5476b452010-12-08 16:14:36 -08001344 bitmap.lockPixels();
1345 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001346 jlong id = (jlong)(uintptr_t)rsAllocationCubeCreateFromBitmap((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001347 (RsType)type, (RsAllocationMipmapControl)mip,
1348 ptr, bitmap.getSize(), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001349 bitmap.unlockPixels();
1350 return id;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001351}
1352
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001353static void
Tim Murray460a0492013-11-19 12:45:54 -08001354nAllocationCopyFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001355{
John Recked207b92015-04-10 13:52:57 -07001356 SkBitmap bitmap;
1357 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Samsf7086092011-01-12 13:28:37 -08001358 int w = bitmap.width();
1359 int h = bitmap.height();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001360
Jason Sams4ef66502010-12-10 16:03:15 -08001361 bitmap.lockPixels();
1362 const void* ptr = bitmap.getPixels();
Tim Murrayeff663f2013-11-15 13:08:30 -08001363 rsAllocation2DData((RsContext)con, (RsAllocation)alloc, 0, 0,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001364 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Tim Murray38faea32012-11-27 14:55:08 -08001365 w, h, ptr, bitmap.getSize(), 0);
Jason Sams4ef66502010-12-10 16:03:15 -08001366 bitmap.unlockPixels();
1367}
1368
1369static void
Tim Murray460a0492013-11-19 12:45:54 -08001370nAllocationCopyToBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Jason Sams4ef66502010-12-10 16:03:15 -08001371{
John Recked207b92015-04-10 13:52:57 -07001372 SkBitmap bitmap;
1373 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Sams4ef66502010-12-10 16:03:15 -08001374
1375 bitmap.lockPixels();
1376 void* ptr = bitmap.getPixels();
Tim Murrayeff663f2013-11-15 13:08:30 -08001377 rsAllocationCopyToBitmap((RsContext)con, (RsAllocation)alloc, ptr, bitmap.getSize());
Jason Sams4ef66502010-12-10 16:03:15 -08001378 bitmap.unlockPixels();
Alex Sakhartchouk835b8542011-07-20 14:33:10 -07001379 bitmap.notifyPixelsChanged();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001380}
1381
Stephen Hines414fa2c2014-04-17 01:02:42 -07001382// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Samsd19f10d2009-05-22 14:03:28 -07001383static void
Tim Murray460a0492013-11-19 12:45:54 -08001384nAllocationData1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001385 jint count, jobject data, jint sizeBytes, jint dataType, jint mSize,
1386 jboolean usePadding)
Jason Samsd19f10d2009-05-22 14:03:28 -07001387{
Jason Samse729a942013-11-06 11:22:02 -08001388 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001389 if (kLogApi) {
1390 ALOGD("nAllocation1DData, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1391 "dataType(%i)", (RsContext)con, (RsAllocation)alloc, offset, count, sizeBytes,
1392 dataType);
1393 }
Miao Wang87e908d2015-03-02 15:15:15 -08001394 PER_ARRAY_TYPE(nullptr, rsAllocation1DData, true,
1395 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -07001396}
1397
1398static void
Miao Wangc8e237e2015-02-20 18:36:32 -08001399nAllocationElementData(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1400 jint xoff, jint yoff, jint zoff,
1401 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Jason Sams49bdaf02010-08-31 13:50:42 -07001402{
1403 jint len = _env->GetArrayLength(data);
Andreas Gampe67333922014-11-10 20:35:59 -08001404 if (kLogApi) {
Miao Wangc8e237e2015-02-20 18:36:32 -08001405 ALOGD("nAllocationElementData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1406 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
Andreas Gampe67333922014-11-10 20:35:59 -08001407 sizeBytes);
1408 }
Chris Wailes488230c32014-08-14 11:22:40 -07001409 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001410 if (ptr == nullptr) {
1411 ALOGE("Failed to get Java array elements");
1412 return;
1413 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001414 rsAllocationElementData((RsContext)con, (RsAllocation)alloc,
1415 xoff, yoff, zoff,
1416 lod, ptr, sizeBytes, compIdx);
Jason Sams49bdaf02010-08-31 13:50:42 -07001417 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1418}
1419
Miao Wangc8e237e2015-02-20 18:36:32 -08001420
Stephen Hines414fa2c2014-04-17 01:02:42 -07001421// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Sams49bdaf02010-08-31 13:50:42 -07001422static void
Tim Murray460a0492013-11-19 12:45:54 -08001423nAllocationData2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001424 jint w, jint h, jobject data, jint sizeBytes, jint dataType, jint mSize,
1425 jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001426{
Jason Samse729a942013-11-06 11:22:02 -08001427 RsAllocation *alloc = (RsAllocation *)_alloc;
1428 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001429 if (kLogApi) {
1430 ALOGD("nAllocation2DData, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1431 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1432 }
Miao Wang87e908d2015-03-02 15:15:15 -08001433 int count = w * h;
1434 PER_ARRAY_TYPE(nullptr, rsAllocation2DData, true,
1435 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Samsd19f10d2009-05-22 14:03:28 -07001436}
1437
Stephen Hines414fa2c2014-04-17 01:02:42 -07001438// Copies from the Allocation pointed to by srcAlloc into the Allocation
1439// pointed to by dstAlloc.
Jason Sams40a29e82009-08-10 14:55:26 -07001440static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001441nAllocationData2D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001442 jlong dstAlloc, jint dstXoff, jint dstYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001443 jint dstMip, jint dstFace,
1444 jint width, jint height,
Tim Murray460a0492013-11-19 12:45:54 -08001445 jlong srcAlloc, jint srcXoff, jint srcYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001446 jint srcMip, jint srcFace)
1447{
Andreas Gampe67333922014-11-10 20:35:59 -08001448 if (kLogApi) {
1449 ALOGD("nAllocation2DData_s, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1450 " dstMip(%i), dstFace(%i), width(%i), height(%i),"
1451 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i), srcFace(%i)",
1452 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip, dstFace,
1453 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
1454 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001455
Tim Murrayeff663f2013-11-15 13:08:30 -08001456 rsAllocationCopy2DRange((RsContext)con,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001457 (RsAllocation)dstAlloc,
1458 dstXoff, dstYoff,
1459 dstMip, dstFace,
1460 width, height,
1461 (RsAllocation)srcAlloc,
1462 srcXoff, srcYoff,
1463 srcMip, srcFace);
1464}
1465
Stephen Hines414fa2c2014-04-17 01:02:42 -07001466// Copies from the Java object data into the Allocation pointed to by _alloc.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001467static void
Tim Murray460a0492013-11-19 12:45:54 -08001468nAllocationData3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001469 jint w, jint h, jint d, jobject data, jint sizeBytes, jint dataType,
1470 jint mSize, jboolean usePadding)
Jason Samsb05d6892013-04-09 15:59:24 -07001471{
Jason Samse729a942013-11-06 11:22:02 -08001472 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001473 if (kLogApi) {
1474 ALOGD("nAllocation3DData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1475 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1476 lod, w, h, d, sizeBytes);
1477 }
Miao Wang87e908d2015-03-02 15:15:15 -08001478 int count = w * h * d;
1479 PER_ARRAY_TYPE(nullptr, rsAllocation3DData, true,
1480 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Jason Samsb05d6892013-04-09 15:59:24 -07001481}
1482
Stephen Hines414fa2c2014-04-17 01:02:42 -07001483// Copies from the Allocation pointed to by srcAlloc into the Allocation
1484// pointed to by dstAlloc.
Jason Samsb05d6892013-04-09 15:59:24 -07001485static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001486nAllocationData3D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001487 jlong dstAlloc, jint dstXoff, jint dstYoff, jint dstZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001488 jint dstMip,
1489 jint width, jint height, jint depth,
Tim Murray460a0492013-11-19 12:45:54 -08001490 jlong srcAlloc, jint srcXoff, jint srcYoff, jint srcZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001491 jint srcMip)
1492{
Andreas Gampe67333922014-11-10 20:35:59 -08001493 if (kLogApi) {
1494 ALOGD("nAllocationData3D_alloc, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1495 " dstMip(%i), width(%i), height(%i),"
1496 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i)",
1497 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip,
1498 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip);
1499 }
Jason Samsb05d6892013-04-09 15:59:24 -07001500
Tim Murrayeff663f2013-11-15 13:08:30 -08001501 rsAllocationCopy3DRange((RsContext)con,
Jason Samsb05d6892013-04-09 15:59:24 -07001502 (RsAllocation)dstAlloc,
1503 dstXoff, dstYoff, dstZoff, dstMip,
1504 width, height, depth,
1505 (RsAllocation)srcAlloc,
1506 srcXoff, srcYoff, srcZoff, srcMip);
1507}
1508
Jason Sams21659ac2013-11-06 15:08:07 -08001509
Stephen Hines414fa2c2014-04-17 01:02:42 -07001510// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsb05d6892013-04-09 15:59:24 -07001511static void
Miao Wang87e908d2015-03-02 15:15:15 -08001512nAllocationRead(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jobject data, jint dataType,
1513 jint mSize, jboolean usePadding)
Jason Sams40a29e82009-08-10 14:55:26 -07001514{
Jason Sams21659ac2013-11-06 15:08:07 -08001515 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001516 if (kLogApi) {
1517 ALOGD("nAllocationRead, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
1518 }
Miao Wang87e908d2015-03-02 15:15:15 -08001519 int count = 0;
1520 PER_ARRAY_TYPE(0, rsAllocationRead, false,
1521 (RsContext)con, alloc, ptr, len * typeBytes);
Jason Sams40a29e82009-08-10 14:55:26 -07001522}
1523
Stephen Hines414fa2c2014-04-17 01:02:42 -07001524// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Sams40a29e82009-08-10 14:55:26 -07001525static void
Tim Murray460a0492013-11-19 12:45:54 -08001526nAllocationRead1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001527 jint count, jobject data, jint sizeBytes, jint dataType,
1528 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001529{
Jason Sams21659ac2013-11-06 15:08:07 -08001530 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001531 if (kLogApi) {
1532 ALOGD("nAllocation1DRead, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1533 "dataType(%i)", (RsContext)con, alloc, offset, count, sizeBytes, dataType);
1534 }
Miao Wang87e908d2015-03-02 15:15:15 -08001535 PER_ARRAY_TYPE(0, rsAllocation1DRead, false,
1536 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsfb9f82c2011-01-12 14:53:25 -08001537}
1538
Miao Wangc8e237e2015-02-20 18:36:32 -08001539// Copies from the Element in the Allocation pointed to by _alloc into the Java array data.
1540static void
Miao Wang45cec0a2015-03-04 16:40:21 -08001541nAllocationElementRead(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
Miao Wangc8e237e2015-02-20 18:36:32 -08001542 jint xoff, jint yoff, jint zoff,
Miao Wang45cec0a2015-03-04 16:40:21 -08001543 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Miao Wangc8e237e2015-02-20 18:36:32 -08001544{
Miao Wang45cec0a2015-03-04 16:40:21 -08001545 jint len = _env->GetArrayLength(data);
Miao Wangc8e237e2015-02-20 18:36:32 -08001546 if (kLogApi) {
Miao Wang45cec0a2015-03-04 16:40:21 -08001547 ALOGD("nAllocationElementRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1548 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
1549 sizeBytes);
Miao Wangc8e237e2015-02-20 18:36:32 -08001550 }
Miao Wang45cec0a2015-03-04 16:40:21 -08001551 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001552 if (ptr == nullptr) {
1553 ALOGE("Failed to get Java array elements");
1554 return;
1555 }
Miao Wang45cec0a2015-03-04 16:40:21 -08001556 rsAllocationElementRead((RsContext)con, (RsAllocation)alloc,
1557 xoff, yoff, zoff,
Jason Samsa7e25092015-03-11 11:00:00 -07001558 lod, ptr, sizeBytes, compIdx);
Miao Wangbfa5e652015-05-04 15:29:25 -07001559 _env->ReleaseByteArrayElements(data, ptr, 0);
Miao Wangc8e237e2015-02-20 18:36:32 -08001560}
1561
Stephen Hines414fa2c2014-04-17 01:02:42 -07001562// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsfb9f82c2011-01-12 14:53:25 -08001563static void
Tim Murray460a0492013-11-19 12:45:54 -08001564nAllocationRead2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001565 jint w, jint h, jobject data, jint sizeBytes, jint dataType,
1566 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001567{
Jason Sams21659ac2013-11-06 15:08:07 -08001568 RsAllocation *alloc = (RsAllocation *)_alloc;
1569 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001570 if (kLogApi) {
1571 ALOGD("nAllocation2DRead, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1572 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1573 }
Miao Wang87e908d2015-03-02 15:15:15 -08001574 int count = w * h;
1575 PER_ARRAY_TYPE(0, rsAllocation2DRead, false,
1576 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Sams40a29e82009-08-10 14:55:26 -07001577}
Miao Wang87e908d2015-03-02 15:15:15 -08001578
Miao Wangc8e237e2015-02-20 18:36:32 -08001579// Copies from the Allocation pointed to by _alloc into the Java object data.
1580static void
1581nAllocationRead3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001582 jint w, jint h, jint d, jobject data, int sizeBytes, int dataType,
1583 jint mSize, jboolean usePadding)
Miao Wangc8e237e2015-02-20 18:36:32 -08001584{
1585 RsAllocation *alloc = (RsAllocation *)_alloc;
1586 if (kLogApi) {
1587 ALOGD("nAllocation3DRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1588 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1589 lod, w, h, d, sizeBytes);
1590 }
Miao Wang87e908d2015-03-02 15:15:15 -08001591 int count = w * h * d;
1592 PER_ARRAY_TYPE(nullptr, rsAllocation3DRead, false,
1593 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Miao Wangc8e237e2015-02-20 18:36:32 -08001594}
Jason Samsd19f10d2009-05-22 14:03:28 -07001595
Tim Murray460a0492013-11-19 12:45:54 -08001596static jlong
1597nAllocationGetType(JNIEnv *_env, jobject _this, jlong con, jlong a)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001598{
Andreas Gampe67333922014-11-10 20:35:59 -08001599 if (kLogApi) {
1600 ALOGD("nAllocationGetType, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1601 }
Tim Murray3aa89c12014-08-18 17:51:22 -07001602 return (jlong)(uintptr_t) rsaAllocationGetType((RsContext)con, (RsAllocation)a);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001603}
1604
Jason Sams5edc6082010-10-05 13:32:49 -07001605static void
Tim Murray460a0492013-11-19 12:45:54 -08001606nAllocationResize1D(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint dimX)
Jason Sams5edc6082010-10-05 13:32:49 -07001607{
Andreas Gampe67333922014-11-10 20:35:59 -08001608 if (kLogApi) {
1609 ALOGD("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", (RsContext)con,
1610 (RsAllocation)alloc, dimX);
1611 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001612 rsAllocationResize1D((RsContext)con, (RsAllocation)alloc, dimX);
Jason Sams5edc6082010-10-05 13:32:49 -07001613}
1614
Jason Sams46ba27e32015-02-06 17:45:15 -08001615
1616static jlong
1617nAllocationAdapterCreate(JNIEnv *_env, jobject _this, jlong con, jlong basealloc, jlong type)
1618{
1619 if (kLogApi) {
1620 ALOGD("nAllocationAdapterCreate, con(%p), base(%p), type(%p)",
1621 (RsContext)con, (RsAllocation)basealloc, (RsElement)type);
1622 }
1623 return (jlong)(uintptr_t) rsAllocationAdapterCreate((RsContext)con, (RsType)type,
1624 (RsAllocation)basealloc);
1625
1626}
1627
1628static void
1629nAllocationAdapterOffset(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1630 jint x, jint y, jint z, jint face, jint lod,
1631 jint a1, jint a2, jint a3, jint a4)
1632{
1633 uint32_t params[] = {
1634 (uint32_t)x, (uint32_t)y, (uint32_t)z, (uint32_t)face,
1635 (uint32_t)lod, (uint32_t)a1, (uint32_t)a2, (uint32_t)a3, (uint32_t)a4
1636 };
1637 if (kLogApi) {
1638 ALOGD("nAllocationAdapterOffset, con(%p), alloc(%p), x(%i), y(%i), z(%i), face(%i), lod(%i), arrays(%i %i %i %i)",
1639 (RsContext)con, (RsAllocation)alloc, x, y, z, face, lod, a1, a2, a3, a4);
1640 }
1641 rsAllocationAdapterOffset((RsContext)con, (RsAllocation)alloc,
1642 params, sizeof(params));
1643}
1644
1645
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001646// -----------------------------------
1647
Tim Murray460a0492013-11-19 12:45:54 -08001648static jlong
1649nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con, jlong native_asset)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001650{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001651 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001652 ALOGV("______nFileA3D %p", asset);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001653
Tim Murray3aa89c12014-08-18 17:51:22 -07001654 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromMemory((RsContext)con, asset->getBuffer(false), asset->getLength());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001655 return id;
1656}
1657
Tim Murray460a0492013-11-19 12:45:54 -08001658static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001659nFileA3DCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001660{
1661 AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001662 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001663 return 0;
1664 }
1665
1666 AutoJavaStringToUTF8 str(_env, _path);
1667 Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
Chris Wailes488230c32014-08-14 11:22:40 -07001668 if (asset == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001669 return 0;
1670 }
1671
Tim Murray3aa89c12014-08-18 17:51:22 -07001672 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromAsset((RsContext)con, asset);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001673 return id;
1674}
1675
Tim Murray460a0492013-11-19 12:45:54 -08001676static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001677nFileA3DCreateFromFile(JNIEnv *_env, jobject _this, jlong con, jstring fileName)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001678{
1679 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001680 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromFile((RsContext)con, fileNameUTF.c_str());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001681
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001682 return id;
1683}
1684
Tim Murray460a0492013-11-19 12:45:54 -08001685static jint
1686nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001687{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001688 int32_t numEntries = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001689 rsaFileA3DGetNumIndexEntries((RsContext)con, &numEntries, (RsFile)fileA3D);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001690 return (jint)numEntries;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001691}
1692
1693static void
Tim Murray460a0492013-11-19 12:45:54 -08001694nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001695{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001696 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001697 RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
1698
Tim Murrayeff663f2013-11-15 13:08:30 -08001699 rsaFileA3DGetIndexEntries((RsContext)con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001700
1701 for(jint i = 0; i < numEntries; i ++) {
1702 _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
1703 _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
1704 }
1705
1706 free(fileEntries);
1707}
1708
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001709static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001710nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint index)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001711{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001712 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Tim Murray3aa89c12014-08-18 17:51:22 -07001713 jlong id = (jlong)(uintptr_t)rsaFileA3DGetEntryByIndex((RsContext)con, (uint32_t)index, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001714 return id;
1715}
Jason Samsd19f10d2009-05-22 14:03:28 -07001716
1717// -----------------------------------
1718
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001719static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001720nFontCreateFromFile(JNIEnv *_env, jobject _this, jlong con,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001721 jstring fileName, jfloat fontSize, jint dpi)
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001722{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001723 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001724 jlong id = (jlong)(uintptr_t)rsFontCreateFromFile((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001725 fileNameUTF.c_str(), fileNameUTF.length(),
1726 fontSize, dpi);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001727
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001728 return id;
1729}
1730
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001731static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001732nFontCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con,
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001733 jstring name, jfloat fontSize, jint dpi, jlong native_asset)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001734{
1735 Asset* asset = reinterpret_cast<Asset*>(native_asset);
1736 AutoJavaStringToUTF8 nameUTF(_env, name);
1737
Tim Murray3aa89c12014-08-18 17:51:22 -07001738 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001739 nameUTF.c_str(), nameUTF.length(),
1740 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001741 asset->getBuffer(false), asset->getLength());
1742 return id;
1743}
1744
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001745static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001746nFontCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001747 jfloat fontSize, jint dpi)
1748{
1749 AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001750 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001751 return 0;
1752 }
1753
1754 AutoJavaStringToUTF8 str(_env, _path);
1755 Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
Chris Wailes488230c32014-08-14 11:22:40 -07001756 if (asset == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001757 return 0;
1758 }
1759
Tim Murray3aa89c12014-08-18 17:51:22 -07001760 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001761 str.c_str(), str.length(),
1762 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001763 asset->getBuffer(false), asset->getLength());
1764 delete asset;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001765 return id;
1766}
1767
Jason Samsbd1c3ad2009-08-03 16:03:08 -07001768// -----------------------------------
1769
1770static void
Tim Murray460a0492013-11-19 12:45:54 -08001771nScriptBindAllocation(JNIEnv *_env, jobject _this, jlong con, jlong script, jlong alloc, jint slot)
Jason Samsd19f10d2009-05-22 14:03:28 -07001772{
Andreas Gampe67333922014-11-10 20:35:59 -08001773 if (kLogApi) {
1774 ALOGD("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", (RsContext)con,
1775 (RsScript)script, (RsAllocation)alloc, slot);
1776 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001777 rsScriptBindAllocation((RsContext)con, (RsScript)script, (RsAllocation)alloc, slot);
Jason Samsd19f10d2009-05-22 14:03:28 -07001778}
1779
1780static void
Tim Murray460a0492013-11-19 12:45:54 -08001781nScriptSetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jint val)
Jason Sams4d339932010-05-11 14:03:58 -07001782{
Andreas Gampe67333922014-11-10 20:35:59 -08001783 if (kLogApi) {
1784 ALOGD("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", (RsContext)con, (void *)script,
1785 slot, val);
1786 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001787 rsScriptSetVarI((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001788}
1789
Tim Murray7c4caad2013-04-10 16:21:40 -07001790static jint
Tim Murray460a0492013-11-19 12:45:54 -08001791nScriptGetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001792{
Andreas Gampe67333922014-11-10 20:35:59 -08001793 if (kLogApi) {
1794 ALOGD("nScriptGetVarI, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1795 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001796 int value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001797 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001798 return value;
1799}
1800
Jason Sams4d339932010-05-11 14:03:58 -07001801static void
Tim Murray460a0492013-11-19 12:45:54 -08001802nScriptSetVarObj(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001803{
Andreas Gampe67333922014-11-10 20:35:59 -08001804 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001805 ALOGD("nScriptSetVarObj, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001806 slot, val);
1807 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001808 rsScriptSetVarObj((RsContext)con, (RsScript)script, slot, (RsObjectBase)val);
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001809}
1810
1811static void
Tim Murray460a0492013-11-19 12:45:54 -08001812nScriptSetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Stephen Hines031ec58c2010-10-11 10:54:21 -07001813{
Andreas Gampe67333922014-11-10 20:35:59 -08001814 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001815 ALOGD("nScriptSetVarJ, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001816 slot, val);
1817 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001818 rsScriptSetVarJ((RsContext)con, (RsScript)script, slot, val);
Stephen Hines031ec58c2010-10-11 10:54:21 -07001819}
1820
Tim Murray7c4caad2013-04-10 16:21:40 -07001821static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001822nScriptGetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001823{
Andreas Gampe67333922014-11-10 20:35:59 -08001824 if (kLogApi) {
1825 ALOGD("nScriptGetVarJ, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1826 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001827 jlong value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001828 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001829 return value;
1830}
1831
Stephen Hines031ec58c2010-10-11 10:54:21 -07001832static void
Tim Murray460a0492013-11-19 12:45:54 -08001833nScriptSetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, float val)
Jason Sams4d339932010-05-11 14:03:58 -07001834{
Andreas Gampe67333922014-11-10 20:35:59 -08001835 if (kLogApi) {
1836 ALOGD("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", (RsContext)con, (void *)script,
1837 slot, val);
1838 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001839 rsScriptSetVarF((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001840}
1841
Tim Murray7c4caad2013-04-10 16:21:40 -07001842static jfloat
Tim Murray460a0492013-11-19 12:45:54 -08001843nScriptGetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001844{
Andreas Gampe67333922014-11-10 20:35:59 -08001845 if (kLogApi) {
1846 ALOGD("nScriptGetVarF, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1847 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001848 jfloat value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001849 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001850 return value;
1851}
1852
Jason Sams4d339932010-05-11 14:03:58 -07001853static void
Tim Murray460a0492013-11-19 12:45:54 -08001854nScriptSetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, double val)
Stephen Hinesca54ec32010-09-20 17:20:30 -07001855{
Andreas Gampe67333922014-11-10 20:35:59 -08001856 if (kLogApi) {
1857 ALOGD("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", (RsContext)con, (void *)script,
1858 slot, val);
1859 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001860 rsScriptSetVarD((RsContext)con, (RsScript)script, slot, val);
Stephen Hinesca54ec32010-09-20 17:20:30 -07001861}
1862
Tim Murray7c4caad2013-04-10 16:21:40 -07001863static jdouble
Tim Murray460a0492013-11-19 12:45:54 -08001864nScriptGetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001865{
Andreas Gampe67333922014-11-10 20:35:59 -08001866 if (kLogApi) {
1867 ALOGD("nScriptGetVarD, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1868 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001869 jdouble value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001870 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001871 return value;
1872}
1873
Stephen Hinesca54ec32010-09-20 17:20:30 -07001874static void
Tim Murray460a0492013-11-19 12:45:54 -08001875nScriptSetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001876{
Andreas Gampe67333922014-11-10 20:35:59 -08001877 if (kLogApi) {
1878 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1879 }
Jason Sams4d339932010-05-11 14:03:58 -07001880 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001881 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001882 if (ptr == nullptr) {
1883 ALOGE("Failed to get Java array elements");
1884 return;
1885 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001886 rsScriptSetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001887 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1888}
1889
Stephen Hinesadeb8092012-04-20 14:26:06 -07001890static void
Tim Murray460a0492013-11-19 12:45:54 -08001891nScriptGetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Tim Murray7c4caad2013-04-10 16:21:40 -07001892{
Andreas Gampe67333922014-11-10 20:35:59 -08001893 if (kLogApi) {
1894 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1895 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001896 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001897 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001898 if (ptr == nullptr) {
1899 ALOGE("Failed to get Java array elements");
1900 return;
1901 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001902 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Stephen Hines414fa2c2014-04-17 01:02:42 -07001903 _env->ReleaseByteArrayElements(data, ptr, 0);
Tim Murray7c4caad2013-04-10 16:21:40 -07001904}
1905
1906static void
Andreas Gampe67333922014-11-10 20:35:59 -08001907nScriptSetVarVE(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data,
1908 jlong elem, jintArray dims)
Stephen Hinesadeb8092012-04-20 14:26:06 -07001909{
Andreas Gampe67333922014-11-10 20:35:59 -08001910 if (kLogApi) {
1911 ALOGD("nScriptSetVarVE, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1912 }
Stephen Hinesadeb8092012-04-20 14:26:06 -07001913 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001914 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001915 if (ptr == nullptr) {
1916 ALOGE("Failed to get Java array elements");
1917 return;
1918 }
Stephen Hinesadeb8092012-04-20 14:26:06 -07001919 jint dimsLen = _env->GetArrayLength(dims) * sizeof(int);
Chris Wailes488230c32014-08-14 11:22:40 -07001920 jint *dimsPtr = _env->GetIntArrayElements(dims, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001921 if (dimsPtr == nullptr) {
1922 ALOGE("Failed to get Java array elements");
1923 return;
1924 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001925 rsScriptSetVarVE((RsContext)con, (RsScript)script, slot, ptr, len, (RsElement)elem,
Stephen Hinesbc5d3ee2014-06-25 00:03:39 -07001926 (const uint32_t*) dimsPtr, dimsLen);
Stephen Hinesadeb8092012-04-20 14:26:06 -07001927 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1928 _env->ReleaseIntArrayElements(dims, dimsPtr, JNI_ABORT);
1929}
1930
Jason Samsd19f10d2009-05-22 14:03:28 -07001931
1932static void
Tim Murray460a0492013-11-19 12:45:54 -08001933nScriptSetTimeZone(JNIEnv *_env, jobject _this, jlong con, jlong script, jbyteArray timeZone)
Jason Samsd19f10d2009-05-22 14:03:28 -07001934{
Andreas Gampe67333922014-11-10 20:35:59 -08001935 if (kLogApi) {
1936 ALOGD("nScriptCSetTimeZone, con(%p), s(%p)", (RsContext)con, (void *)script);
1937 }
Romain Guy584a3752009-07-30 18:45:01 -07001938
1939 jint length = _env->GetArrayLength(timeZone);
1940 jbyte* timeZone_ptr;
1941 timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
Miao Wangba8766c2015-10-12 17:24:13 -07001942 if (timeZone_ptr == nullptr) {
1943 ALOGE("Failed to get Java array elements");
1944 return;
1945 }
Romain Guy584a3752009-07-30 18:45:01 -07001946
Tim Murrayeff663f2013-11-15 13:08:30 -08001947 rsScriptSetTimeZone((RsContext)con, (RsScript)script, (const char *)timeZone_ptr, length);
Romain Guy584a3752009-07-30 18:45:01 -07001948
1949 if (timeZone_ptr) {
1950 _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
1951 }
1952}
1953
Jason Samsfbf0b9e2009-08-13 12:59:04 -07001954static void
Tim Murray460a0492013-11-19 12:45:54 -08001955nScriptInvoke(JNIEnv *_env, jobject _this, jlong con, jlong obj, jint slot)
Jason Samsbe2e8412009-09-16 15:04:38 -07001956{
Andreas Gampe67333922014-11-10 20:35:59 -08001957 if (kLogApi) {
1958 ALOGD("nScriptInvoke, con(%p), script(%p)", (RsContext)con, (void *)obj);
1959 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001960 rsScriptInvoke((RsContext)con, (RsScript)obj, slot);
Jason Samsbe2e8412009-09-16 15:04:38 -07001961}
1962
1963static void
Tim Murray460a0492013-11-19 12:45:54 -08001964nScriptInvokeV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001965{
Andreas Gampe67333922014-11-10 20:35:59 -08001966 if (kLogApi) {
1967 ALOGD("nScriptInvokeV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1968 }
Jason Sams4d339932010-05-11 14:03:58 -07001969 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001970 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001971 if (ptr == nullptr) {
1972 ALOGE("Failed to get Java array elements");
1973 return;
1974 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001975 rsScriptInvokeV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001976 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1977}
1978
Jason Sams6e494d32011-04-27 16:33:11 -07001979static void
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001980nScriptForEach(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
1981 jlongArray ains, jlong aout, jbyteArray params,
1982 jintArray limits)
Jason Sams6e494d32011-04-27 16:33:11 -07001983{
Andreas Gampe67333922014-11-10 20:35:59 -08001984 if (kLogApi) {
Chih-Hung Hsieh9eb9dd32015-05-06 14:42:04 -07001985 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 -08001986 }
Jason Sams6e494d32011-04-27 16:33:11 -07001987
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001988 jint in_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07001989 jlong *in_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07001990
Chris Wailes488230c32014-08-14 11:22:40 -07001991 RsAllocation *in_allocs = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07001992
Chris Wailes488230c32014-08-14 11:22:40 -07001993 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001994 in_len = _env->GetArrayLength(ains);
Yang Ni7b2a46f2015-05-05 12:41:19 -07001995 if (in_len > (jint)RS_KERNEL_MAX_ARGUMENTS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -07001996 ALOGE("Too many arguments in kernel launch.");
1997 // TODO (b/20758983): Report back to Java and throw an exception
1998 return;
1999 }
Chris Wailes94961062014-06-11 12:01:28 -07002000
Yang Ni17c2d7a2015-04-30 16:13:54 -07002001 in_ptr = _env->GetLongArrayElements(ains, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002002 if (in_ptr == nullptr) {
2003 ALOGE("Failed to get Java array elements");
2004 return;
2005 }
2006
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002007 if (sizeof(RsAllocation) == sizeof(jlong)) {
2008 in_allocs = (RsAllocation*)in_ptr;
Chris Wailes94961062014-06-11 12:01:28 -07002009
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002010 } else {
2011 // Convert from 64-bit jlong types to the native pointer type.
Chris Wailes94961062014-06-11 12:01:28 -07002012
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002013 in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
Yang Ni17c2d7a2015-04-30 16:13:54 -07002014 if (in_allocs == nullptr) {
2015 ALOGE("Failed launching kernel for lack of memory.");
2016 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2017 return;
2018 }
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002019
2020 for (int index = in_len; --index >= 0;) {
2021 in_allocs[index] = (RsAllocation)in_ptr[index];
2022 }
2023 }
Chris Wailes94961062014-06-11 12:01:28 -07002024 }
2025
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002026 jint param_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002027 jbyte *param_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002028
Chris Wailes488230c32014-08-14 11:22:40 -07002029 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002030 param_len = _env->GetArrayLength(params);
Chris Wailes488230c32014-08-14 11:22:40 -07002031 param_ptr = _env->GetByteArrayElements(params, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002032 if (param_ptr == nullptr) {
2033 ALOGE("Failed to get Java array elements");
2034 return;
2035 }
Chris Wailes94961062014-06-11 12:01:28 -07002036 }
2037
Chris Wailes488230c32014-08-14 11:22:40 -07002038 RsScriptCall sc, *sca = nullptr;
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002039 uint32_t sc_size = 0;
Chris Wailes94961062014-06-11 12:01:28 -07002040
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002041 jint limit_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002042 jint *limit_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002043
Chris Wailes488230c32014-08-14 11:22:40 -07002044 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002045 limit_len = _env->GetArrayLength(limits);
Chris Wailes488230c32014-08-14 11:22:40 -07002046 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002047 if (limit_ptr == nullptr) {
2048 ALOGE("Failed to get Java array elements");
2049 return;
2050 }
Chris Wailes94961062014-06-11 12:01:28 -07002051
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002052 assert(limit_len == 6);
Andreas Gampe67333922014-11-10 20:35:59 -08002053 UNUSED(limit_len); // As the assert might not be compiled.
Chris Wailes94961062014-06-11 12:01:28 -07002054
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002055 sc.xStart = limit_ptr[0];
2056 sc.xEnd = limit_ptr[1];
2057 sc.yStart = limit_ptr[2];
2058 sc.yEnd = limit_ptr[3];
2059 sc.zStart = limit_ptr[4];
2060 sc.zEnd = limit_ptr[5];
2061 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
Jason Sams14331ab2015-01-26 18:14:36 -08002062 sc.arrayStart = 0;
2063 sc.arrayEnd = 0;
2064 sc.array2Start = 0;
2065 sc.array2End = 0;
2066 sc.array3Start = 0;
2067 sc.array3End = 0;
2068 sc.array4Start = 0;
2069 sc.array4End = 0;
Chris Wailes94961062014-06-11 12:01:28 -07002070
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002071 sca = &sc;
Chris Wailes94961062014-06-11 12:01:28 -07002072 }
2073
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002074 rsScriptForEachMulti((RsContext)con, (RsScript)script, slot,
2075 in_allocs, in_len, (RsAllocation)aout,
2076 param_ptr, param_len, sca, sc_size);
Chris Wailes94961062014-06-11 12:01:28 -07002077
Chris Wailes488230c32014-08-14 11:22:40 -07002078 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002079 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
Chris Wailes94961062014-06-11 12:01:28 -07002080 }
2081
Chris Wailes488230c32014-08-14 11:22:40 -07002082 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002083 _env->ReleaseByteArrayElements(params, param_ptr, JNI_ABORT);
2084 }
2085
Chris Wailes488230c32014-08-14 11:22:40 -07002086 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002087 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
2088 }
Chris Wailes94961062014-06-11 12:01:28 -07002089}
2090
Matt Wala36eb1f72015-07-20 15:35:27 -07002091static void
2092nScriptReduce(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
2093 jlong ain, jlong aout, jintArray limits)
2094{
2095 if (kLogApi) {
2096 ALOGD("nScriptReduce, con(%p), s(%p), slot(%i) ain(%" PRId64 ") aout(%" PRId64 ")", (RsContext)con, (void *)script, slot, ain, aout);
2097 }
2098
2099 RsScriptCall sc, *sca = nullptr;
2100 uint32_t sc_size = 0;
2101
2102 jint limit_len = 0;
2103 jint *limit_ptr = nullptr;
2104
2105 // If the caller passed limits, reflect them in the RsScriptCall.
2106 if (limits != nullptr) {
2107 limit_len = _env->GetArrayLength(limits);
2108 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002109 if (limit_ptr == nullptr) {
2110 ALOGE("Failed to get Java array elements");
2111 return;
2112 }
Matt Wala36eb1f72015-07-20 15:35:27 -07002113
2114 // We expect to be passed an array [x1, x2] which specifies
2115 // the sub-range for a 1-dimensional reduction.
2116 assert(limit_len == 2);
2117 UNUSED(limit_len); // As the assert might not be compiled.
2118
2119 sc.xStart = limit_ptr[0];
2120 sc.xEnd = limit_ptr[1];
2121 sc.yStart = 0;
2122 sc.yEnd = 0;
2123 sc.zStart = 0;
2124 sc.zEnd = 0;
2125 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
2126 sc.arrayStart = 0;
2127 sc.arrayEnd = 0;
2128 sc.array2Start = 0;
2129 sc.array2End = 0;
2130 sc.array3Start = 0;
2131 sc.array3End = 0;
2132 sc.array4Start = 0;
2133 sc.array4End = 0;
2134
2135 sca = &sc;
2136 sc_size = sizeof(sc);
2137 }
2138
2139 rsScriptReduce((RsContext)con, (RsScript)script, slot,
2140 (RsAllocation)ain, (RsAllocation)aout,
2141 sca, sc_size);
2142
2143 if (limits != nullptr) {
2144 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
2145 }
2146}
2147
Jason Sams22534172009-08-04 16:58:20 -07002148// -----------------------------------
2149
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002150static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002151nScriptCCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Samse4a06c52011-03-16 16:29:28 -07002152 jstring resName, jstring cacheDir,
2153 jbyteArray scriptRef, jint length)
Jason Sams22534172009-08-04 16:58:20 -07002154{
Andreas Gampe67333922014-11-10 20:35:59 -08002155 if (kLogApi) {
2156 ALOGD("nScriptCCreate, con(%p)", (RsContext)con);
2157 }
Jason Sams22534172009-08-04 16:58:20 -07002158
Jason Samse4a06c52011-03-16 16:29:28 -07002159 AutoJavaStringToUTF8 resNameUTF(_env, resName);
2160 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002161 jlong ret = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002162 jbyte* script_ptr = nullptr;
Jack Palevich43702d82009-05-28 13:38:16 -07002163 jint _exception = 0;
2164 jint remaining;
Jack Palevich43702d82009-05-28 13:38:16 -07002165 if (!scriptRef) {
2166 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002167 //jniThrowException(_env, "java/lang/IllegalArgumentException", "script == null");
Jack Palevich43702d82009-05-28 13:38:16 -07002168 goto exit;
2169 }
Jack Palevich43702d82009-05-28 13:38:16 -07002170 if (length < 0) {
2171 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002172 //jniThrowException(_env, "java/lang/IllegalArgumentException", "length < 0");
Jack Palevich43702d82009-05-28 13:38:16 -07002173 goto exit;
2174 }
Jason Samse4a06c52011-03-16 16:29:28 -07002175 remaining = _env->GetArrayLength(scriptRef);
Jack Palevich43702d82009-05-28 13:38:16 -07002176 if (remaining < length) {
2177 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002178 //jniThrowException(_env, "java/lang/IllegalArgumentException",
2179 // "length > script.length - offset");
Jack Palevich43702d82009-05-28 13:38:16 -07002180 goto exit;
2181 }
Jason Samse4a06c52011-03-16 16:29:28 -07002182 script_ptr = (jbyte *)
Jack Palevich43702d82009-05-28 13:38:16 -07002183 _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
Miao Wangba8766c2015-10-12 17:24:13 -07002184 if (script_ptr == nullptr) {
2185 ALOGE("Failed to get Java array elements");
2186 return ret;
2187 }
Jack Palevich43702d82009-05-28 13:38:16 -07002188
Tim Murrayeff663f2013-11-15 13:08:30 -08002189 //rsScriptCSetText((RsContext)con, (const char *)script_ptr, length);
Jason Samse4a06c52011-03-16 16:29:28 -07002190
Tim Murray3aa89c12014-08-18 17:51:22 -07002191 ret = (jlong)(uintptr_t)rsScriptCCreate((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07002192 resNameUTF.c_str(), resNameUTF.length(),
2193 cacheDirUTF.c_str(), cacheDirUTF.length(),
Jason Samse4a06c52011-03-16 16:29:28 -07002194 (const char *)script_ptr, length);
Jason Sams39ddc9502009-06-05 17:35:09 -07002195
Jack Palevich43702d82009-05-28 13:38:16 -07002196exit:
Jason Samse4a06c52011-03-16 16:29:28 -07002197 if (script_ptr) {
2198 _env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr,
Jack Palevich43702d82009-05-28 13:38:16 -07002199 _exception ? JNI_ABORT: 0);
2200 }
Jason Samsd19f10d2009-05-22 14:03:28 -07002201
Tim Murray3aa89c12014-08-18 17:51:22 -07002202 return (jlong)(uintptr_t)ret;
Jason Samsd19f10d2009-05-22 14:03:28 -07002203}
2204
Tim Murray460a0492013-11-19 12:45:54 -08002205static jlong
2206nScriptIntrinsicCreate(JNIEnv *_env, jobject _this, jlong con, jint id, jlong eid)
Jason Sams6ab97682012-08-10 12:09:43 -07002207{
Andreas Gampe67333922014-11-10 20:35:59 -08002208 if (kLogApi) {
2209 ALOGD("nScriptIntrinsicCreate, con(%p) id(%i) element(%p)", (RsContext)con, id,
2210 (void *)eid);
2211 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002212 return (jlong)(uintptr_t)rsScriptIntrinsicCreate((RsContext)con, id, (RsElement)eid);
Jason Sams6ab97682012-08-10 12:09:43 -07002213}
2214
Tim Murray460a0492013-11-19 12:45:54 -08002215static jlong
2216nScriptKernelIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot, jint sig)
Jason Sams08a81582012-09-18 12:32:10 -07002217{
Andreas Gampe67333922014-11-10 20:35:59 -08002218 if (kLogApi) {
2219 ALOGD("nScriptKernelIDCreate, con(%p) script(%p), slot(%i), sig(%i)", (RsContext)con,
2220 (void *)sid, slot, sig);
2221 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002222 return (jlong)(uintptr_t)rsScriptKernelIDCreate((RsContext)con, (RsScript)sid, slot, sig);
Jason Sams08a81582012-09-18 12:32:10 -07002223}
2224
Tim Murray460a0492013-11-19 12:45:54 -08002225static jlong
Yang Nibe392ad2015-01-23 17:16:02 -08002226nScriptInvokeIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
2227{
2228 if (kLogApi) {
Elliott Hughes7ff53fa2015-02-05 21:36:10 -08002229 ALOGD("nScriptInvokeIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con,
Yang Nibe392ad2015-01-23 17:16:02 -08002230 (void *)sid, slot);
2231 }
2232 return (jlong)(uintptr_t)rsScriptInvokeIDCreate((RsContext)con, (RsScript)sid, slot);
2233}
2234
2235static jlong
Tim Murray460a0492013-11-19 12:45:54 -08002236nScriptFieldIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
Jason Sams08a81582012-09-18 12:32:10 -07002237{
Andreas Gampe67333922014-11-10 20:35:59 -08002238 if (kLogApi) {
2239 ALOGD("nScriptFieldIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con, (void *)sid,
2240 slot);
2241 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002242 return (jlong)(uintptr_t)rsScriptFieldIDCreate((RsContext)con, (RsScript)sid, slot);
Jason Sams08a81582012-09-18 12:32:10 -07002243}
2244
Tim Murray460a0492013-11-19 12:45:54 -08002245static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002246nScriptGroupCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _kernels, jlongArray _src,
2247 jlongArray _dstk, jlongArray _dstf, jlongArray _types)
Jason Sams08a81582012-09-18 12:32:10 -07002248{
Andreas Gampe67333922014-11-10 20:35:59 -08002249 if (kLogApi) {
2250 ALOGD("nScriptGroupCreate, con(%p)", (RsContext)con);
2251 }
Jason Sams08a81582012-09-18 12:32:10 -07002252
Ashok Bhat98071552014-02-12 09:54:43 +00002253 jint kernelsLen = _env->GetArrayLength(_kernels);
Chris Wailes488230c32014-08-14 11:22:40 -07002254 jlong *jKernelsPtr = _env->GetLongArrayElements(_kernels, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002255 if (jKernelsPtr == nullptr) {
2256 ALOGE("Failed to get Java array elements: kernels");
2257 return 0;
2258 }
Ashok Bhat98071552014-02-12 09:54:43 +00002259 RsScriptKernelID* kernelsPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * kernelsLen);
2260 for(int i = 0; i < kernelsLen; ++i) {
2261 kernelsPtr[i] = (RsScriptKernelID)jKernelsPtr[i];
2262 }
Jason Sams08a81582012-09-18 12:32:10 -07002263
Ashok Bhat98071552014-02-12 09:54:43 +00002264 jint srcLen = _env->GetArrayLength(_src);
Chris Wailes488230c32014-08-14 11:22:40 -07002265 jlong *jSrcPtr = _env->GetLongArrayElements(_src, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002266 if (jSrcPtr == nullptr) {
2267 ALOGE("Failed to get Java array elements: src");
2268 return 0;
2269 }
Ashok Bhat98071552014-02-12 09:54:43 +00002270 RsScriptKernelID* srcPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * srcLen);
2271 for(int i = 0; i < srcLen; ++i) {
2272 srcPtr[i] = (RsScriptKernelID)jSrcPtr[i];
2273 }
Jason Sams08a81582012-09-18 12:32:10 -07002274
Ashok Bhat98071552014-02-12 09:54:43 +00002275 jint dstkLen = _env->GetArrayLength(_dstk);
Chris Wailes488230c32014-08-14 11:22:40 -07002276 jlong *jDstkPtr = _env->GetLongArrayElements(_dstk, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002277 if (jDstkPtr == nullptr) {
2278 ALOGE("Failed to get Java array elements: dstk");
2279 return 0;
2280 }
Ashok Bhat98071552014-02-12 09:54:43 +00002281 RsScriptKernelID* dstkPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstkLen);
2282 for(int i = 0; i < dstkLen; ++i) {
2283 dstkPtr[i] = (RsScriptKernelID)jDstkPtr[i];
2284 }
2285
2286 jint dstfLen = _env->GetArrayLength(_dstf);
Chris Wailes488230c32014-08-14 11:22:40 -07002287 jlong *jDstfPtr = _env->GetLongArrayElements(_dstf, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002288 if (jDstfPtr == nullptr) {
2289 ALOGE("Failed to get Java array elements: dstf");
2290 return 0;
2291 }
Ashok Bhat98071552014-02-12 09:54:43 +00002292 RsScriptKernelID* dstfPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstfLen);
2293 for(int i = 0; i < dstfLen; ++i) {
2294 dstfPtr[i] = (RsScriptKernelID)jDstfPtr[i];
2295 }
2296
2297 jint typesLen = _env->GetArrayLength(_types);
Chris Wailes488230c32014-08-14 11:22:40 -07002298 jlong *jTypesPtr = _env->GetLongArrayElements(_types, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002299 if (jTypesPtr == nullptr) {
2300 ALOGE("Failed to get Java array elements: types");
2301 return 0;
2302 }
Ashok Bhat98071552014-02-12 09:54:43 +00002303 RsType* typesPtr = (RsType*) malloc(sizeof(RsType) * typesLen);
2304 for(int i = 0; i < typesLen; ++i) {
2305 typesPtr[i] = (RsType)jTypesPtr[i];
2306 }
2307
Tim Murray3aa89c12014-08-18 17:51:22 -07002308 jlong id = (jlong)(uintptr_t)rsScriptGroupCreate((RsContext)con,
Ashok Bhat98071552014-02-12 09:54:43 +00002309 (RsScriptKernelID *)kernelsPtr, kernelsLen * sizeof(RsScriptKernelID),
2310 (RsScriptKernelID *)srcPtr, srcLen * sizeof(RsScriptKernelID),
2311 (RsScriptKernelID *)dstkPtr, dstkLen * sizeof(RsScriptKernelID),
2312 (RsScriptFieldID *)dstfPtr, dstfLen * sizeof(RsScriptKernelID),
2313 (RsType *)typesPtr, typesLen * sizeof(RsType));
2314
2315 free(kernelsPtr);
2316 free(srcPtr);
2317 free(dstkPtr);
2318 free(dstfPtr);
2319 free(typesPtr);
2320 _env->ReleaseLongArrayElements(_kernels, jKernelsPtr, 0);
2321 _env->ReleaseLongArrayElements(_src, jSrcPtr, 0);
2322 _env->ReleaseLongArrayElements(_dstk, jDstkPtr, 0);
2323 _env->ReleaseLongArrayElements(_dstf, jDstfPtr, 0);
2324 _env->ReleaseLongArrayElements(_types, jTypesPtr, 0);
Jason Sams08a81582012-09-18 12:32:10 -07002325 return id;
2326}
2327
2328static void
Tim Murray460a0492013-11-19 12:45:54 -08002329nScriptGroupSetInput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002330{
Andreas Gampe67333922014-11-10 20:35:59 -08002331 if (kLogApi) {
2332 ALOGD("nScriptGroupSetInput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2333 (void *)gid, (void *)kid, (void *)alloc);
2334 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002335 rsScriptGroupSetInput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002336}
2337
2338static void
Tim Murray460a0492013-11-19 12:45:54 -08002339nScriptGroupSetOutput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002340{
Andreas Gampe67333922014-11-10 20:35:59 -08002341 if (kLogApi) {
2342 ALOGD("nScriptGroupSetOutput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2343 (void *)gid, (void *)kid, (void *)alloc);
2344 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002345 rsScriptGroupSetOutput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002346}
2347
2348static void
Tim Murray460a0492013-11-19 12:45:54 -08002349nScriptGroupExecute(JNIEnv *_env, jobject _this, jlong con, jlong gid)
Jason Sams08a81582012-09-18 12:32:10 -07002350{
Andreas Gampe67333922014-11-10 20:35:59 -08002351 if (kLogApi) {
2352 ALOGD("nScriptGroupSetOutput, con(%p) group(%p)", (RsContext)con, (void *)gid);
2353 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002354 rsScriptGroupExecute((RsContext)con, (RsScriptGroup)gid);
Jason Sams08a81582012-09-18 12:32:10 -07002355}
2356
Jason Samsd19f10d2009-05-22 14:03:28 -07002357// ---------------------------------------------------------------------------
2358
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002359static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002360nProgramStoreCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Sams331bf9b2011-04-06 11:23:54 -07002361 jboolean colorMaskR, jboolean colorMaskG, jboolean colorMaskB, jboolean colorMaskA,
2362 jboolean depthMask, jboolean ditherEnable,
2363 jint srcFunc, jint destFunc,
2364 jint depthFunc)
Jason Samsd19f10d2009-05-22 14:03:28 -07002365{
Andreas Gampe67333922014-11-10 20:35:59 -08002366 if (kLogApi) {
2367 ALOGD("nProgramStoreCreate, con(%p)", (RsContext)con);
2368 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002369 return (jlong)(uintptr_t)rsProgramStoreCreate((RsContext)con, colorMaskR, colorMaskG, colorMaskB, colorMaskA,
Jason Sams331bf9b2011-04-06 11:23:54 -07002370 depthMask, ditherEnable, (RsBlendSrcFunc)srcFunc,
2371 (RsBlendDstFunc)destFunc, (RsDepthFunc)depthFunc);
Jason Samsd19f10d2009-05-22 14:03:28 -07002372}
2373
Jason Sams0011bcf2009-12-15 12:58:36 -08002374// ---------------------------------------------------------------------------
2375
2376static void
Tim Murray460a0492013-11-19 12:45:54 -08002377nProgramBindConstants(JNIEnv *_env, jobject _this, jlong con, jlong vpv, jint slot, jlong a)
Jason Sams0011bcf2009-12-15 12:58:36 -08002378{
Andreas Gampe67333922014-11-10 20:35:59 -08002379 if (kLogApi) {
2380 ALOGD("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", (RsContext)con,
2381 (RsProgramVertex)vpv, slot, (RsAllocation)a);
2382 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002383 rsProgramBindConstants((RsContext)con, (RsProgram)vpv, slot, (RsAllocation)a);
Jason Sams0011bcf2009-12-15 12:58:36 -08002384}
Jason Sams54c0ec12009-11-30 14:49:55 -08002385
Jason Sams68afd012009-12-17 16:55:08 -08002386static void
Tim Murray460a0492013-11-19 12:45:54 -08002387nProgramBindTexture(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002388{
Andreas Gampe67333922014-11-10 20:35:59 -08002389 if (kLogApi) {
2390 ALOGD("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2391 (RsProgramFragment)vpf, slot, (RsAllocation)a);
2392 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002393 rsProgramBindTexture((RsContext)con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
Jason Sams68afd012009-12-17 16:55:08 -08002394}
2395
2396static void
Tim Murray460a0492013-11-19 12:45:54 -08002397nProgramBindSampler(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002398{
Andreas Gampe67333922014-11-10 20:35:59 -08002399 if (kLogApi) {
2400 ALOGD("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2401 (RsProgramFragment)vpf, slot, (RsSampler)a);
2402 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002403 rsProgramBindSampler((RsContext)con, (RsProgramFragment)vpf, slot, (RsSampler)a);
Jason Sams68afd012009-12-17 16:55:08 -08002404}
2405
Jason Samsd19f10d2009-05-22 14:03:28 -07002406// ---------------------------------------------------------------------------
2407
Tim Murray460a0492013-11-19 12:45:54 -08002408static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002409nProgramFragmentCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002410 jobjectArray texNames, jlongArray params)
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002411{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002412 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002413 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002414 jint paramLen = _env->GetArrayLength(params);
Miao Wangba8766c2015-10-12 17:24:13 -07002415 if (jParamPtr == nullptr) {
2416 ALOGE("Failed to get Java array elements");
2417 return 0;
2418 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002419
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002420 int texCount = _env->GetArrayLength(texNames);
2421 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2422 const char ** nameArray = names.c_str();
2423 size_t* sizeArray = names.c_str_len();
2424
Andreas Gampe67333922014-11-10 20:35:59 -08002425 if (kLogApi) {
2426 ALOGD("nProgramFragmentCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2427 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002428
Ashok Bhat98071552014-02-12 09:54:43 +00002429 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2430 for(int i = 0; i < paramLen; ++i) {
2431 paramPtr[i] = (uintptr_t)jParamPtr[i];
2432 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002433 jlong ret = (jlong)(uintptr_t)rsProgramFragmentCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002434 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002435 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002436
Ashok Bhat98071552014-02-12 09:54:43 +00002437 free(paramPtr);
2438 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002439 return ret;
2440}
2441
2442
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002443// ---------------------------------------------------------------------------
2444
Tim Murray460a0492013-11-19 12:45:54 -08002445static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002446nProgramVertexCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002447 jobjectArray texNames, jlongArray params)
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002448{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002449 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002450 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams0011bcf2009-12-15 12:58:36 -08002451 jint paramLen = _env->GetArrayLength(params);
Miao Wangba8766c2015-10-12 17:24:13 -07002452 if (jParamPtr == nullptr) {
2453 ALOGE("Failed to get Java array elements");
2454 return 0;
2455 }
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002456
Andreas Gampe67333922014-11-10 20:35:59 -08002457 if (kLogApi) {
2458 ALOGD("nProgramVertexCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2459 }
Jason Sams0011bcf2009-12-15 12:58:36 -08002460
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002461 int texCount = _env->GetArrayLength(texNames);
2462 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2463 const char ** nameArray = names.c_str();
2464 size_t* sizeArray = names.c_str_len();
2465
Ashok Bhat98071552014-02-12 09:54:43 +00002466 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2467 for(int i = 0; i < paramLen; ++i) {
2468 paramPtr[i] = (uintptr_t)jParamPtr[i];
2469 }
2470
Tim Murray3aa89c12014-08-18 17:51:22 -07002471 jlong ret = (jlong)(uintptr_t)rsProgramVertexCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002472 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002473 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002474
Ashok Bhat98071552014-02-12 09:54:43 +00002475 free(paramPtr);
2476 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams0011bcf2009-12-15 12:58:36 -08002477 return ret;
2478}
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002479
Jason Samsebfb4362009-09-23 13:57:02 -07002480// ---------------------------------------------------------------------------
2481
Tim Murray460a0492013-11-19 12:45:54 -08002482static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002483nProgramRasterCreate(JNIEnv *_env, jobject _this, jlong con, jboolean pointSprite, jint cull)
Jason Samsebfb4362009-09-23 13:57:02 -07002484{
Andreas Gampe67333922014-11-10 20:35:59 -08002485 if (kLogApi) {
2486 ALOGD("nProgramRasterCreate, con(%p), pointSprite(%i), cull(%i)", (RsContext)con,
2487 pointSprite, cull);
2488 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002489 return (jlong)(uintptr_t)rsProgramRasterCreate((RsContext)con, pointSprite, (RsCullMode)cull);
Jason Samsebfb4362009-09-23 13:57:02 -07002490}
2491
Jason Samsd19f10d2009-05-22 14:03:28 -07002492
2493// ---------------------------------------------------------------------------
2494
2495static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002496nContextBindRootScript(JNIEnv *_env, jobject _this, jlong con, jlong script)
Jason Samsd19f10d2009-05-22 14:03:28 -07002497{
Andreas Gampe67333922014-11-10 20:35:59 -08002498 if (kLogApi) {
2499 ALOGD("nContextBindRootScript, con(%p), script(%p)", (RsContext)con, (RsScript)script);
2500 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002501 rsContextBindRootScript((RsContext)con, (RsScript)script);
Jason Samsd19f10d2009-05-22 14:03:28 -07002502}
2503
2504static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002505nContextBindProgramStore(JNIEnv *_env, jobject _this, jlong con, jlong pfs)
Jason Samsd19f10d2009-05-22 14:03:28 -07002506{
Andreas Gampe67333922014-11-10 20:35:59 -08002507 if (kLogApi) {
2508 ALOGD("nContextBindProgramStore, con(%p), pfs(%p)", (RsContext)con, (RsProgramStore)pfs);
2509 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002510 rsContextBindProgramStore((RsContext)con, (RsProgramStore)pfs);
Jason Samsd19f10d2009-05-22 14:03:28 -07002511}
2512
2513static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002514nContextBindProgramFragment(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsd19f10d2009-05-22 14:03:28 -07002515{
Andreas Gampe67333922014-11-10 20:35:59 -08002516 if (kLogApi) {
2517 ALOGD("nContextBindProgramFragment, con(%p), pf(%p)", (RsContext)con,
2518 (RsProgramFragment)pf);
2519 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002520 rsContextBindProgramFragment((RsContext)con, (RsProgramFragment)pf);
Jason Samsd19f10d2009-05-22 14:03:28 -07002521}
2522
Jason Sams0826a6f2009-06-15 19:04:56 -07002523static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002524nContextBindProgramVertex(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Sams0826a6f2009-06-15 19:04:56 -07002525{
Andreas Gampe67333922014-11-10 20:35:59 -08002526 if (kLogApi) {
2527 ALOGD("nContextBindProgramVertex, con(%p), pf(%p)", (RsContext)con, (RsProgramVertex)pf);
2528 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002529 rsContextBindProgramVertex((RsContext)con, (RsProgramVertex)pf);
Jason Sams0826a6f2009-06-15 19:04:56 -07002530}
2531
Joe Onoratod7b37742009-08-09 22:57:44 -07002532static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002533nContextBindProgramRaster(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsebfb4362009-09-23 13:57:02 -07002534{
Andreas Gampe67333922014-11-10 20:35:59 -08002535 if (kLogApi) {
2536 ALOGD("nContextBindProgramRaster, con(%p), pf(%p)", (RsContext)con, (RsProgramRaster)pf);
2537 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002538 rsContextBindProgramRaster((RsContext)con, (RsProgramRaster)pf);
Jason Samsebfb4362009-09-23 13:57:02 -07002539}
2540
Joe Onoratod7b37742009-08-09 22:57:44 -07002541
Jason Sams02fb2cb2009-05-28 15:37:57 -07002542// ---------------------------------------------------------------------------
2543
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002544static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002545nSamplerCreate(JNIEnv *_env, jobject _this, jlong con, jint magFilter, jint minFilter,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002546 jint wrapS, jint wrapT, jint wrapR, jfloat aniso)
Jason Sams02fb2cb2009-05-28 15:37:57 -07002547{
Andreas Gampe67333922014-11-10 20:35:59 -08002548 if (kLogApi) {
2549 ALOGD("nSamplerCreate, con(%p)", (RsContext)con);
2550 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002551 return (jlong)(uintptr_t)rsSamplerCreate((RsContext)con,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002552 (RsSamplerValue)magFilter,
2553 (RsSamplerValue)minFilter,
2554 (RsSamplerValue)wrapS,
2555 (RsSamplerValue)wrapT,
2556 (RsSamplerValue)wrapR,
2557 aniso);
Jason Sams02fb2cb2009-05-28 15:37:57 -07002558}
2559
Jason Samsbba134c2009-06-22 15:49:21 -07002560// ---------------------------------------------------------------------------
2561
Tim Murray460a0492013-11-19 12:45:54 -08002562static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002563nMeshCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _vtx, jlongArray _idx, jintArray _prim)
Jason Samsbba134c2009-06-22 15:49:21 -07002564{
Andreas Gampe67333922014-11-10 20:35:59 -08002565 if (kLogApi) {
2566 ALOGD("nMeshCreate, con(%p)", (RsContext)con);
2567 }
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002568
2569 jint vtxLen = _env->GetArrayLength(_vtx);
Chris Wailes488230c32014-08-14 11:22:40 -07002570 jlong *jVtxPtr = _env->GetLongArrayElements(_vtx, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002571 if (jVtxPtr == nullptr) {
2572 ALOGE("Failed to get Java array elements: vtx");
2573 return 0;
2574 }
Ashok Bhat98071552014-02-12 09:54:43 +00002575 RsAllocation* vtxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * vtxLen);
2576 for(int i = 0; i < vtxLen; ++i) {
2577 vtxPtr[i] = (RsAllocation)(uintptr_t)jVtxPtr[i];
2578 }
2579
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002580 jint idxLen = _env->GetArrayLength(_idx);
Chris Wailes488230c32014-08-14 11:22:40 -07002581 jlong *jIdxPtr = _env->GetLongArrayElements(_idx, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002582 if (jIdxPtr == nullptr) {
2583 ALOGE("Failed to get Java array elements: idx");
2584 return 0;
2585 }
Ashok Bhat98071552014-02-12 09:54:43 +00002586 RsAllocation* idxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * idxLen);
2587 for(int i = 0; i < idxLen; ++i) {
2588 idxPtr[i] = (RsAllocation)(uintptr_t)jIdxPtr[i];
2589 }
2590
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002591 jint primLen = _env->GetArrayLength(_prim);
Chris Wailes488230c32014-08-14 11:22:40 -07002592 jint *primPtr = _env->GetIntArrayElements(_prim, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002593 if (primPtr == nullptr) {
2594 ALOGE("Failed to get Java array elements: prim");
2595 return 0;
2596 }
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002597
Tim Murray3aa89c12014-08-18 17:51:22 -07002598 jlong id = (jlong)(uintptr_t)rsMeshCreate((RsContext)con,
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002599 (RsAllocation *)vtxPtr, vtxLen,
2600 (RsAllocation *)idxPtr, idxLen,
2601 (uint32_t *)primPtr, primLen);
2602
Ashok Bhat98071552014-02-12 09:54:43 +00002603 free(vtxPtr);
2604 free(idxPtr);
2605 _env->ReleaseLongArrayElements(_vtx, jVtxPtr, 0);
2606 _env->ReleaseLongArrayElements(_idx, jIdxPtr, 0);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002607 _env->ReleaseIntArrayElements(_prim, primPtr, 0);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002608 return id;
2609}
2610
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002611static jint
Tim Murray460a0492013-11-19 12:45:54 -08002612nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002613{
Andreas Gampe67333922014-11-10 20:35:59 -08002614 if (kLogApi) {
2615 ALOGD("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2616 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002617 jint vtxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002618 rsaMeshGetVertexBufferCount((RsContext)con, (RsMesh)mesh, &vtxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002619 return vtxCount;
2620}
2621
2622static jint
Tim Murray460a0492013-11-19 12:45:54 -08002623nMeshGetIndexCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002624{
Andreas Gampe67333922014-11-10 20:35:59 -08002625 if (kLogApi) {
2626 ALOGD("nMeshGetIndexCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2627 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002628 jint idxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002629 rsaMeshGetIndexCount((RsContext)con, (RsMesh)mesh, &idxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002630 return idxCount;
2631}
2632
2633static void
Ashok Bhat98071552014-02-12 09:54:43 +00002634nMeshGetVertices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _ids, jint numVtxIDs)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002635{
Andreas Gampe67333922014-11-10 20:35:59 -08002636 if (kLogApi) {
2637 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2638 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002639
2640 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
Tim Murrayeff663f2013-11-15 13:08:30 -08002641 rsaMeshGetVertices((RsContext)con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002642
2643 for(jint i = 0; i < numVtxIDs; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002644 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002645 _env->SetLongArrayRegion(_ids, i, 1, &alloc);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002646 }
2647
2648 free(allocs);
2649}
2650
2651static void
Ashok Bhat98071552014-02-12 09:54:43 +00002652nMeshGetIndices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _idxIds, jintArray _primitives, jint numIndices)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002653{
Andreas Gampe67333922014-11-10 20:35:59 -08002654 if (kLogApi) {
2655 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2656 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002657
2658 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
2659 uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
2660
Tim Murrayeff663f2013-11-15 13:08:30 -08002661 rsaMeshGetIndices((RsContext)con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002662
2663 for(jint i = 0; i < numIndices; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002664 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002665 const jint prim = (jint)prims[i];
2666 _env->SetLongArrayRegion(_idxIds, i, 1, &alloc);
2667 _env->SetIntArrayRegion(_primitives, i, 1, &prim);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002668 }
2669
2670 free(allocs);
2671 free(prims);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002672}
2673
Tim Murray56f9e6f2014-05-16 11:47:26 -07002674static jint
2675nSystemGetPointerSize(JNIEnv *_env, jobject _this) {
2676 return (jint)sizeof(void*);
2677}
2678
2679
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002680// ---------------------------------------------------------------------------
2681
Jason Samsd19f10d2009-05-22 14:03:28 -07002682
Jason Sams94d8e90a2009-06-10 16:09:05 -07002683static const char *classPathName = "android/renderscript/RenderScript";
Jason Samsd19f10d2009-05-22 14:03:28 -07002684
Daniel Micay76f6a862015-09-19 17:31:01 -04002685static const JNINativeMethod methods[] = {
Jason Sams1c415172010-11-08 17:06:46 -08002686{"_nInit", "()V", (void*)_nInit },
Jason Samsea84a7c2009-09-04 14:42:41 -07002687
Tim Murrayeff663f2013-11-15 13:08:30 -08002688{"nDeviceCreate", "()J", (void*)nDeviceCreate },
2689{"nDeviceDestroy", "(J)V", (void*)nDeviceDestroy },
2690{"nDeviceSetConfig", "(JII)V", (void*)nDeviceSetConfig },
2691{"nContextGetUserMessage", "(J[I)I", (void*)nContextGetUserMessage },
2692{"nContextGetErrorMessage", "(J)Ljava/lang/String;", (void*)nContextGetErrorMessage },
2693{"nContextPeekMessage", "(J[I)I", (void*)nContextPeekMessage },
Jason Sams1c415172010-11-08 17:06:46 -08002694
Tim Murrayeff663f2013-11-15 13:08:30 -08002695{"nContextInitToClient", "(J)V", (void*)nContextInitToClient },
2696{"nContextDeinitToClient", "(J)V", (void*)nContextDeinitToClient },
Jason Samsd19f10d2009-05-22 14:03:28 -07002697
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002698
Jason Sams2e1872f2010-08-17 16:25:41 -07002699// All methods below are thread protected in java.
Tim Murrayeff663f2013-11-15 13:08:30 -08002700{"rsnContextCreate", "(JIII)J", (void*)nContextCreate },
2701{"rsnContextCreateGL", "(JIIIIIIIIIIIIFI)J", (void*)nContextCreateGL },
2702{"rsnContextFinish", "(J)V", (void*)nContextFinish },
2703{"rsnContextSetPriority", "(JI)V", (void*)nContextSetPriority },
Tim Murray47f31582015-04-07 15:43:24 -07002704{"rsnContextSetCacheDir", "(JLjava/lang/String;)V", (void*)nContextSetCacheDir },
Tim Murrayeff663f2013-11-15 13:08:30 -08002705{"rsnContextSetSurface", "(JIILandroid/view/Surface;)V", (void*)nContextSetSurface },
2706{"rsnContextDestroy", "(J)V", (void*)nContextDestroy },
2707{"rsnContextDump", "(JI)V", (void*)nContextDump },
2708{"rsnContextPause", "(J)V", (void*)nContextPause },
2709{"rsnContextResume", "(J)V", (void*)nContextResume },
2710{"rsnContextSendMessage", "(JI[I)V", (void*)nContextSendMessage },
Yang Ni281c3252014-10-24 08:52:24 -07002711{"rsnClosureCreate", "(JJJ[J[J[I[J[J)J", (void*)nClosureCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002712{"rsnInvokeClosureCreate", "(JJ[B[J[J[I)J", (void*)nInvokeClosureCreate },
Yang Ni281c3252014-10-24 08:52:24 -07002713{"rsnClosureSetArg", "(JJIJI)V", (void*)nClosureSetArg },
2714{"rsnClosureSetGlobal", "(JJJJI)V", (void*)nClosureSetGlobal },
Tim Murray460a0492013-11-19 12:45:54 -08002715{"rsnAssignName", "(JJ[B)V", (void*)nAssignName },
2716{"rsnGetName", "(JJ)Ljava/lang/String;", (void*)nGetName },
2717{"rsnObjDestroy", "(JJ)V", (void*)nObjDestroy },
Jason Sams64676f32009-07-08 18:01:53 -07002718
Tim Murray460a0492013-11-19 12:45:54 -08002719{"rsnFileA3DCreateFromFile", "(JLjava/lang/String;)J", (void*)nFileA3DCreateFromFile },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002720{"rsnFileA3DCreateFromAssetStream", "(JJ)J", (void*)nFileA3DCreateFromAssetStream },
Tim Murray460a0492013-11-19 12:45:54 -08002721{"rsnFileA3DCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;)J", (void*)nFileA3DCreateFromAsset },
2722{"rsnFileA3DGetNumIndexEntries", "(JJ)I", (void*)nFileA3DGetNumIndexEntries },
2723{"rsnFileA3DGetIndexEntries", "(JJI[I[Ljava/lang/String;)V", (void*)nFileA3DGetIndexEntries },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002724{"rsnFileA3DGetEntryByIndex", "(JJI)J", (void*)nFileA3DGetEntryByIndex },
Jason Samsd19f10d2009-05-22 14:03:28 -07002725
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002726{"rsnFontCreateFromFile", "(JLjava/lang/String;FI)J", (void*)nFontCreateFromFile },
2727{"rsnFontCreateFromAssetStream", "(JLjava/lang/String;FIJ)J", (void*)nFontCreateFromAssetStream },
2728{"rsnFontCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;FI)J", (void*)nFontCreateFromAsset },
Jason Samsd19f10d2009-05-22 14:03:28 -07002729
Tim Murray460a0492013-11-19 12:45:54 -08002730{"rsnElementCreate", "(JJIZI)J", (void*)nElementCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002731{"rsnElementCreate2", "(J[J[Ljava/lang/String;[I)J", (void*)nElementCreate2 },
Tim Murray460a0492013-11-19 12:45:54 -08002732{"rsnElementGetNativeData", "(JJ[I)V", (void*)nElementGetNativeData },
Ashok Bhat98071552014-02-12 09:54:43 +00002733{"rsnElementGetSubElements", "(JJ[J[Ljava/lang/String;[I)V", (void*)nElementGetSubElements },
Jason Samsd19f10d2009-05-22 14:03:28 -07002734
Tim Murray460a0492013-11-19 12:45:54 -08002735{"rsnTypeCreate", "(JJIIIZZI)J", (void*)nTypeCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002736{"rsnTypeGetNativeData", "(JJ[J)V", (void*)nTypeGetNativeData },
Jason Samsd19f10d2009-05-22 14:03:28 -07002737
Ashok Bhat98071552014-02-12 09:54:43 +00002738{"rsnAllocationCreateTyped", "(JJIIJ)J", (void*)nAllocationCreateTyped },
Tim Murray460a0492013-11-19 12:45:54 -08002739{"rsnAllocationCreateFromBitmap", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateFromBitmap },
2740{"rsnAllocationCreateBitmapBackedAllocation", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateBitmapBackedAllocation },
2741{"rsnAllocationCubeCreateFromBitmap","(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCubeCreateFromBitmap },
Jason Sams5476b452010-12-08 16:14:36 -08002742
Tim Murray460a0492013-11-19 12:45:54 -08002743{"rsnAllocationCopyFromBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyFromBitmap },
2744{"rsnAllocationCopyToBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyToBitmap },
Jason Sams4ef66502010-12-10 16:03:15 -08002745
Tim Murray460a0492013-11-19 12:45:54 -08002746{"rsnAllocationSyncAll", "(JJI)V", (void*)nAllocationSyncAll },
2747{"rsnAllocationGetSurface", "(JJ)Landroid/view/Surface;", (void*)nAllocationGetSurface },
2748{"rsnAllocationSetSurface", "(JJLandroid/view/Surface;)V", (void*)nAllocationSetSurface },
2749{"rsnAllocationIoSend", "(JJ)V", (void*)nAllocationIoSend },
2750{"rsnAllocationIoReceive", "(JJ)V", (void*)nAllocationIoReceive },
Miao Wang87e908d2015-03-02 15:15:15 -08002751{"rsnAllocationData1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData1D },
Miao Wangc8e237e2015-02-20 18:36:32 -08002752{"rsnAllocationElementData", "(JJIIIII[BI)V", (void*)nAllocationElementData },
Miao Wang87e908d2015-03-02 15:15:15 -08002753{"rsnAllocationData2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData2D },
Tim Murray460a0492013-11-19 12:45:54 -08002754{"rsnAllocationData2D", "(JJIIIIIIJIIII)V", (void*)nAllocationData2D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002755{"rsnAllocationData3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData3D },
Tim Murray460a0492013-11-19 12:45:54 -08002756{"rsnAllocationData3D", "(JJIIIIIIIJIIII)V", (void*)nAllocationData3D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002757{"rsnAllocationRead", "(JJLjava/lang/Object;IIZ)V", (void*)nAllocationRead },
2758{"rsnAllocationRead1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead1D },
Miao Wang45cec0a2015-03-04 16:40:21 -08002759{"rsnAllocationElementRead", "(JJIIIII[BI)V", (void*)nAllocationElementRead },
Miao Wang87e908d2015-03-02 15:15:15 -08002760{"rsnAllocationRead2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead2D },
2761{"rsnAllocationRead3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead3D },
Tim Murray460a0492013-11-19 12:45:54 -08002762{"rsnAllocationGetType", "(JJ)J", (void*)nAllocationGetType},
2763{"rsnAllocationResize1D", "(JJI)V", (void*)nAllocationResize1D },
2764{"rsnAllocationGenerateMipmaps", "(JJ)V", (void*)nAllocationGenerateMipmaps },
Jason Samsbd1c3ad2009-08-03 16:03:08 -07002765
Jason Sams46ba27e32015-02-06 17:45:15 -08002766{"rsnAllocationAdapterCreate", "(JJJ)J", (void*)nAllocationAdapterCreate },
2767{"rsnAllocationAdapterOffset", "(JJIIIIIIIII)V", (void*)nAllocationAdapterOffset },
2768
Tim Murray460a0492013-11-19 12:45:54 -08002769{"rsnScriptBindAllocation", "(JJJI)V", (void*)nScriptBindAllocation },
2770{"rsnScriptSetTimeZone", "(JJ[B)V", (void*)nScriptSetTimeZone },
2771{"rsnScriptInvoke", "(JJI)V", (void*)nScriptInvoke },
2772{"rsnScriptInvokeV", "(JJI[B)V", (void*)nScriptInvokeV },
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002773
2774{"rsnScriptForEach", "(JJI[JJ[B[I)V", (void*)nScriptForEach },
Matt Wala36eb1f72015-07-20 15:35:27 -07002775{"rsnScriptReduce", "(JJIJJ[I)V", (void*)nScriptReduce },
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002776
Tim Murray460a0492013-11-19 12:45:54 -08002777{"rsnScriptSetVarI", "(JJII)V", (void*)nScriptSetVarI },
2778{"rsnScriptGetVarI", "(JJI)I", (void*)nScriptGetVarI },
2779{"rsnScriptSetVarJ", "(JJIJ)V", (void*)nScriptSetVarJ },
2780{"rsnScriptGetVarJ", "(JJI)J", (void*)nScriptGetVarJ },
2781{"rsnScriptSetVarF", "(JJIF)V", (void*)nScriptSetVarF },
2782{"rsnScriptGetVarF", "(JJI)F", (void*)nScriptGetVarF },
2783{"rsnScriptSetVarD", "(JJID)V", (void*)nScriptSetVarD },
2784{"rsnScriptGetVarD", "(JJI)D", (void*)nScriptGetVarD },
2785{"rsnScriptSetVarV", "(JJI[B)V", (void*)nScriptSetVarV },
2786{"rsnScriptGetVarV", "(JJI[B)V", (void*)nScriptGetVarV },
2787{"rsnScriptSetVarVE", "(JJI[BJ[I)V", (void*)nScriptSetVarVE },
2788{"rsnScriptSetVarObj", "(JJIJ)V", (void*)nScriptSetVarObj },
Jason Samsd19f10d2009-05-22 14:03:28 -07002789
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002790{"rsnScriptCCreate", "(JLjava/lang/String;Ljava/lang/String;[BI)J", (void*)nScriptCCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002791{"rsnScriptIntrinsicCreate", "(JIJ)J", (void*)nScriptIntrinsicCreate },
2792{"rsnScriptKernelIDCreate", "(JJII)J", (void*)nScriptKernelIDCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002793{"rsnScriptInvokeIDCreate", "(JJI)J", (void*)nScriptInvokeIDCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002794{"rsnScriptFieldIDCreate", "(JJI)J", (void*)nScriptFieldIDCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002795{"rsnScriptGroupCreate", "(J[J[J[J[J[J)J", (void*)nScriptGroupCreate },
Yang Ni35be56c2015-04-02 17:47:56 -07002796{"rsnScriptGroup2Create", "(JLjava/lang/String;Ljava/lang/String;[J)J", (void*)nScriptGroup2Create },
Tim Murray460a0492013-11-19 12:45:54 -08002797{"rsnScriptGroupSetInput", "(JJJJ)V", (void*)nScriptGroupSetInput },
2798{"rsnScriptGroupSetOutput", "(JJJJ)V", (void*)nScriptGroupSetOutput },
2799{"rsnScriptGroupExecute", "(JJ)V", (void*)nScriptGroupExecute },
Yang Ni281c3252014-10-24 08:52:24 -07002800{"rsnScriptGroup2Execute", "(JJ)V", (void*)nScriptGroup2Execute },
Jason Sams0011bcf2009-12-15 12:58:36 -08002801
Tim Murray25207df2015-01-12 16:47:56 -08002802{"rsnScriptIntrinsicBLAS_Single", "(JJIIIIIIIIIFJJFJIIII)V", (void*)nScriptIntrinsicBLAS_Single },
2803{"rsnScriptIntrinsicBLAS_Double", "(JJIIIIIIIIIDJJDJIIII)V", (void*)nScriptIntrinsicBLAS_Double },
2804{"rsnScriptIntrinsicBLAS_Complex", "(JJIIIIIIIIIFFJJFFJIIII)V", (void*)nScriptIntrinsicBLAS_Complex },
2805{"rsnScriptIntrinsicBLAS_Z", "(JJIIIIIIIIIDDJJDDJIIII)V", (void*)nScriptIntrinsicBLAS_Z },
2806
Tim Murray9cb16a22015-04-01 11:07:16 -07002807{"rsnScriptIntrinsicBLAS_BNNM", "(JJIIIJIJIJII)V", (void*)nScriptIntrinsicBLAS_BNNM },
2808
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002809{"rsnProgramStoreCreate", "(JZZZZZZIII)J", (void*)nProgramStoreCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002810
Tim Murray460a0492013-11-19 12:45:54 -08002811{"rsnProgramBindConstants", "(JJIJ)V", (void*)nProgramBindConstants },
2812{"rsnProgramBindTexture", "(JJIJ)V", (void*)nProgramBindTexture },
2813{"rsnProgramBindSampler", "(JJIJ)V", (void*)nProgramBindSampler },
Jason Samsebfb4362009-09-23 13:57:02 -07002814
Ashok Bhat98071552014-02-12 09:54:43 +00002815{"rsnProgramFragmentCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramFragmentCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002816{"rsnProgramRasterCreate", "(JZI)J", (void*)nProgramRasterCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002817{"rsnProgramVertexCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramVertexCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002818
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002819{"rsnContextBindRootScript", "(JJ)V", (void*)nContextBindRootScript },
2820{"rsnContextBindProgramStore", "(JJ)V", (void*)nContextBindProgramStore },
2821{"rsnContextBindProgramFragment", "(JJ)V", (void*)nContextBindProgramFragment },
2822{"rsnContextBindProgramVertex", "(JJ)V", (void*)nContextBindProgramVertex },
2823{"rsnContextBindProgramRaster", "(JJ)V", (void*)nContextBindProgramRaster },
Jason Sams02fb2cb2009-05-28 15:37:57 -07002824
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002825{"rsnSamplerCreate", "(JIIIIIF)J", (void*)nSamplerCreate },
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002826
Ashok Bhat98071552014-02-12 09:54:43 +00002827{"rsnMeshCreate", "(J[J[J[I)J", (void*)nMeshCreate },
Jason Sams2e1872f2010-08-17 16:25:41 -07002828
Tim Murray460a0492013-11-19 12:45:54 -08002829{"rsnMeshGetVertexBufferCount", "(JJ)I", (void*)nMeshGetVertexBufferCount },
2830{"rsnMeshGetIndexCount", "(JJ)I", (void*)nMeshGetIndexCount },
Ashok Bhat98071552014-02-12 09:54:43 +00002831{"rsnMeshGetVertices", "(JJ[JI)V", (void*)nMeshGetVertices },
2832{"rsnMeshGetIndices", "(JJ[J[II)V", (void*)nMeshGetIndices },
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002833
Tim Murray56f9e6f2014-05-16 11:47:26 -07002834{"rsnSystemGetPointerSize", "()I", (void*)nSystemGetPointerSize },
Jason Samsd19f10d2009-05-22 14:03:28 -07002835};
2836
2837static int registerFuncs(JNIEnv *_env)
2838{
2839 return android::AndroidRuntime::registerNativeMethods(
2840 _env, classPathName, methods, NELEM(methods));
2841}
2842
2843// ---------------------------------------------------------------------------
2844
2845jint JNI_OnLoad(JavaVM* vm, void* reserved)
2846{
Chris Wailes488230c32014-08-14 11:22:40 -07002847 JNIEnv* env = nullptr;
Jason Samsd19f10d2009-05-22 14:03:28 -07002848 jint result = -1;
2849
Jason Samsd19f10d2009-05-22 14:03:28 -07002850 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
Steve Block3762c312012-01-06 19:20:56 +00002851 ALOGE("ERROR: GetEnv failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07002852 goto bail;
2853 }
Chris Wailes488230c32014-08-14 11:22:40 -07002854 assert(env != nullptr);
Jason Samsd19f10d2009-05-22 14:03:28 -07002855
2856 if (registerFuncs(env) < 0) {
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002857 ALOGE("ERROR: Renderscript native registration failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07002858 goto bail;
2859 }
2860
2861 /* success -- return valid version number */
2862 result = JNI_VERSION_1_4;
2863
2864bail:
2865 return result;
2866}