blob: 39540709b16e59f79074cfccf47ebe4ba5f5b014 [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
Stephen Hines4cbe25a2012-01-18 18:46:27 -08002 * Copyright (C) 2011-2012 The Android Open Source Project
Jason Samsd19f10d2009-05-22 14:03:28 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jason Samsd1516df2015-05-05 18:00:34 -070017#define LOG_TAG "RenderScript_jni"
Jason Samsf29ca502009-06-23 12:22:47 -070018
Jason Samsd19f10d2009-05-22 14:03:28 -070019#include <stdlib.h>
20#include <stdio.h>
21#include <fcntl.h>
22#include <unistd.h>
23#include <math.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070024#include <utils/misc.h>
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +010025#include <inttypes.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070026
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080027#include <androidfw/Asset.h>
28#include <androidfw/AssetManager.h>
29#include <androidfw/ResourceTypes.h>
Jason Samsf29ca502009-06-23 12:22:47 -070030
Jason Samsd19f10d2009-05-22 14:03:28 -070031#include "jni.h"
32#include "JNIHelp.h"
33#include "android_runtime/AndroidRuntime.h"
Jim Milleree956052010-08-19 18:56:00 -070034#include "android_runtime/android_view_Surface.h"
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -080035#include "android_runtime/android_util_AssetManager.h"
John Reckf4faeac2015-03-05 13:50:31 -080036#include "android/graphics/GraphicsJNI.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070037
Jason Sams1d6983a2012-02-16 16:07:49 -080038#include <rs.h>
39#include <rsEnv.h>
Jason Samsfb9aa9f2012-03-28 15:30:07 -070040#include <gui/Surface.h>
Andy McFaddend47f7d82012-12-18 09:48:38 -080041#include <gui/GLConsumer.h>
Jason Samsfaa32b32011-06-20 16:58:04 -070042#include <android_runtime/android_graphics_SurfaceTexture.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070043
Steve Block3762c312012-01-06 19:20:56 +000044//#define LOG_API ALOGE
Andreas Gampe67333922014-11-10 20:35:59 -080045static constexpr bool kLogApi = false;
Jason Samsd19f10d2009-05-22 14:03:28 -070046
47using namespace android;
48
Andreas Gampe67333922014-11-10 20:35:59 -080049template <typename... T>
50void UNUSED(T... t) {}
51
Stephen Hines414fa2c2014-04-17 01:02:42 -070052#define PER_ARRAY_TYPE(flag, fnc, readonly, ...) { \
Jason Samse729a942013-11-06 11:22:02 -080053 jint len = 0; \
Chris Wailes488230c32014-08-14 11:22:40 -070054 void *ptr = nullptr; \
Miao Wang87e908d2015-03-02 15:15:15 -080055 void *srcPtr = nullptr; \
Jason Sams21659ac2013-11-06 15:08:07 -080056 size_t typeBytes = 0; \
Stephen Hines414fa2c2014-04-17 01:02:42 -070057 jint relFlag = 0; \
58 if (readonly) { \
59 /* The on-release mode should only be JNI_ABORT for read-only accesses. */ \
Miao Wang87e908d2015-03-02 15:15:15 -080060 /* readonly = true, also indicates we are copying to the allocation . */ \
Stephen Hines414fa2c2014-04-17 01:02:42 -070061 relFlag = JNI_ABORT; \
62 } \
Jason Samse729a942013-11-06 11:22:02 -080063 switch(dataType) { \
64 case RS_TYPE_FLOAT_32: \
65 len = _env->GetArrayLength((jfloatArray)data); \
66 ptr = _env->GetFloatArrayElements((jfloatArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -070067 if (ptr == nullptr) { \
68 ALOGE("Failed to get Java array elements."); \
69 return; \
70 } \
Jason Sams21659ac2013-11-06 15:08:07 -080071 typeBytes = 4; \
Miao Wang87e908d2015-03-02 15:15:15 -080072 if (usePadding) { \
73 srcPtr = ptr; \
74 len = len / 3 * 4; \
75 if (count == 0) { \
76 count = len / 4; \
77 } \
78 ptr = malloc (len * typeBytes); \
79 if (readonly) { \
80 copyWithPadding(ptr, srcPtr, mSize, count); \
81 fnc(__VA_ARGS__); \
82 } else { \
83 fnc(__VA_ARGS__); \
84 copyWithUnPadding(srcPtr, ptr, mSize, count); \
85 } \
86 free(ptr); \
87 ptr = srcPtr; \
88 } else { \
89 fnc(__VA_ARGS__); \
90 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -070091 _env->ReleaseFloatArrayElements((jfloatArray)data, (jfloat *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -080092 return; \
93 case RS_TYPE_FLOAT_64: \
94 len = _env->GetArrayLength((jdoubleArray)data); \
95 ptr = _env->GetDoubleArrayElements((jdoubleArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -070096 if (ptr == nullptr) { \
97 ALOGE("Failed to get Java array elements."); \
98 return; \
99 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800100 typeBytes = 8; \
Miao Wang87e908d2015-03-02 15:15:15 -0800101 if (usePadding) { \
102 srcPtr = ptr; \
103 len = len / 3 * 4; \
104 if (count == 0) { \
105 count = len / 4; \
106 } \
107 ptr = malloc (len * typeBytes); \
108 if (readonly) { \
109 copyWithPadding(ptr, srcPtr, mSize, count); \
110 fnc(__VA_ARGS__); \
111 } else { \
112 fnc(__VA_ARGS__); \
113 copyWithUnPadding(srcPtr, ptr, mSize, count); \
114 } \
115 free(ptr); \
116 ptr = srcPtr; \
117 } else { \
118 fnc(__VA_ARGS__); \
119 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700120 _env->ReleaseDoubleArrayElements((jdoubleArray)data, (jdouble *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800121 return; \
122 case RS_TYPE_SIGNED_8: \
123 case RS_TYPE_UNSIGNED_8: \
124 len = _env->GetArrayLength((jbyteArray)data); \
125 ptr = _env->GetByteArrayElements((jbyteArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700126 if (ptr == nullptr) { \
127 ALOGE("Failed to get Java array elements."); \
128 return; \
129 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800130 typeBytes = 1; \
Miao Wang87e908d2015-03-02 15:15:15 -0800131 if (usePadding) { \
132 srcPtr = ptr; \
133 len = len / 3 * 4; \
134 if (count == 0) { \
135 count = len / 4; \
136 } \
137 ptr = malloc (len * typeBytes); \
138 if (readonly) { \
139 copyWithPadding(ptr, srcPtr, mSize, count); \
140 fnc(__VA_ARGS__); \
141 } else { \
142 fnc(__VA_ARGS__); \
143 copyWithUnPadding(srcPtr, ptr, mSize, count); \
144 } \
145 free(ptr); \
146 ptr = srcPtr; \
147 } else { \
148 fnc(__VA_ARGS__); \
149 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700150 _env->ReleaseByteArrayElements((jbyteArray)data, (jbyte*)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800151 return; \
152 case RS_TYPE_SIGNED_16: \
153 case RS_TYPE_UNSIGNED_16: \
154 len = _env->GetArrayLength((jshortArray)data); \
155 ptr = _env->GetShortArrayElements((jshortArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700156 if (ptr == nullptr) { \
157 ALOGE("Failed to get Java array elements."); \
158 return; \
159 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800160 typeBytes = 2; \
Miao Wang87e908d2015-03-02 15:15:15 -0800161 if (usePadding) { \
162 srcPtr = ptr; \
163 len = len / 3 * 4; \
164 if (count == 0) { \
165 count = len / 4; \
166 } \
167 ptr = malloc (len * typeBytes); \
168 if (readonly) { \
169 copyWithPadding(ptr, srcPtr, mSize, count); \
170 fnc(__VA_ARGS__); \
171 } else { \
172 fnc(__VA_ARGS__); \
173 copyWithUnPadding(srcPtr, ptr, mSize, count); \
174 } \
175 free(ptr); \
176 ptr = srcPtr; \
177 } else { \
178 fnc(__VA_ARGS__); \
179 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700180 _env->ReleaseShortArrayElements((jshortArray)data, (jshort *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800181 return; \
182 case RS_TYPE_SIGNED_32: \
183 case RS_TYPE_UNSIGNED_32: \
184 len = _env->GetArrayLength((jintArray)data); \
185 ptr = _env->GetIntArrayElements((jintArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700186 if (ptr == nullptr) { \
187 ALOGE("Failed to get Java array elements."); \
188 return; \
189 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800190 typeBytes = 4; \
Miao Wang87e908d2015-03-02 15:15:15 -0800191 if (usePadding) { \
192 srcPtr = ptr; \
193 len = len / 3 * 4; \
194 if (count == 0) { \
195 count = len / 4; \
196 } \
197 ptr = malloc (len * typeBytes); \
198 if (readonly) { \
199 copyWithPadding(ptr, srcPtr, mSize, count); \
200 fnc(__VA_ARGS__); \
201 } else { \
202 fnc(__VA_ARGS__); \
203 copyWithUnPadding(srcPtr, ptr, mSize, count); \
204 } \
205 free(ptr); \
206 ptr = srcPtr; \
207 } else { \
208 fnc(__VA_ARGS__); \
209 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700210 _env->ReleaseIntArrayElements((jintArray)data, (jint *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800211 return; \
212 case RS_TYPE_SIGNED_64: \
213 case RS_TYPE_UNSIGNED_64: \
214 len = _env->GetArrayLength((jlongArray)data); \
215 ptr = _env->GetLongArrayElements((jlongArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700216 if (ptr == nullptr) { \
217 ALOGE("Failed to get Java array elements."); \
218 return; \
219 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800220 typeBytes = 8; \
Miao Wang87e908d2015-03-02 15:15:15 -0800221 if (usePadding) { \
222 srcPtr = ptr; \
223 len = len / 3 * 4; \
224 if (count == 0) { \
225 count = len / 4; \
226 } \
227 ptr = malloc (len * typeBytes); \
228 if (readonly) { \
229 copyWithPadding(ptr, srcPtr, mSize, count); \
230 fnc(__VA_ARGS__); \
231 } else { \
232 fnc(__VA_ARGS__); \
233 copyWithUnPadding(srcPtr, ptr, mSize, count); \
234 } \
235 free(ptr); \
236 ptr = srcPtr; \
237 } else { \
238 fnc(__VA_ARGS__); \
239 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700240 _env->ReleaseLongArrayElements((jlongArray)data, (jlong *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800241 return; \
242 default: \
243 break; \
244 } \
Miao Wang87e908d2015-03-02 15:15:15 -0800245 UNUSED(len, ptr, srcPtr, typeBytes, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800246}
247
248
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800249class AutoJavaStringToUTF8 {
250public:
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800251 AutoJavaStringToUTF8(JNIEnv* env, jstring str) : fEnv(env), fJStr(str) {
Chris Wailes488230c32014-08-14 11:22:40 -0700252 fCStr = env->GetStringUTFChars(str, nullptr);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800253 fLength = env->GetStringUTFLength(str);
254 }
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800255 ~AutoJavaStringToUTF8() {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800256 fEnv->ReleaseStringUTFChars(fJStr, fCStr);
257 }
258 const char* c_str() const { return fCStr; }
259 jsize length() const { return fLength; }
260
261private:
262 JNIEnv* fEnv;
263 jstring fJStr;
264 const char* fCStr;
265 jsize fLength;
266};
267
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800268class AutoJavaStringArrayToUTF8 {
269public:
270 AutoJavaStringArrayToUTF8(JNIEnv* env, jobjectArray strings, jsize stringsLength)
271 : mEnv(env), mStrings(strings), mStringsLength(stringsLength) {
Chris Wailes488230c32014-08-14 11:22:40 -0700272 mCStrings = nullptr;
273 mSizeArray = nullptr;
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800274 if (stringsLength > 0) {
275 mCStrings = (const char **)calloc(stringsLength, sizeof(char *));
276 mSizeArray = (size_t*)calloc(stringsLength, sizeof(size_t));
277 for (jsize ct = 0; ct < stringsLength; ct ++) {
278 jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
Chris Wailes488230c32014-08-14 11:22:40 -0700279 mCStrings[ct] = mEnv->GetStringUTFChars(s, nullptr);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800280 mSizeArray[ct] = mEnv->GetStringUTFLength(s);
281 }
282 }
283 }
284 ~AutoJavaStringArrayToUTF8() {
285 for (jsize ct=0; ct < mStringsLength; ct++) {
286 jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
287 mEnv->ReleaseStringUTFChars(s, mCStrings[ct]);
288 }
289 free(mCStrings);
290 free(mSizeArray);
291 }
292 const char **c_str() const { return mCStrings; }
293 size_t *c_str_len() const { return mSizeArray; }
294 jsize length() const { return mStringsLength; }
295
296private:
297 JNIEnv *mEnv;
298 jobjectArray mStrings;
299 const char **mCStrings;
300 size_t *mSizeArray;
301 jsize mStringsLength;
302};
303
Jason Samsd19f10d2009-05-22 14:03:28 -0700304// ---------------------------------------------------------------------------
305
Jason Samsffe9f482009-06-01 17:45:53 -0700306static jfieldID gContextId = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -0700307
308static void _nInit(JNIEnv *_env, jclass _this)
309{
Tim Murrayeff663f2013-11-15 13:08:30 -0800310 gContextId = _env->GetFieldID(_this, "mContext", "J");
Jason Samsd19f10d2009-05-22 14:03:28 -0700311}
312
Jason Samsd19f10d2009-05-22 14:03:28 -0700313// ---------------------------------------------------------------------------
314
Miao Wang87e908d2015-03-02 15:15:15 -0800315static void copyWithPadding(void* ptr, void* srcPtr, int mSize, int count) {
316 int sizeBytesPad = mSize * 4;
317 int sizeBytes = mSize * 3;
318 uint8_t *dst = static_cast<uint8_t *>(ptr);
319 uint8_t *src = static_cast<uint8_t *>(srcPtr);
320 for (int i = 0; i < count; i++) {
321 memcpy(dst, src, sizeBytes);
322 dst += sizeBytesPad;
323 src += sizeBytes;
324 }
325}
326
327static void copyWithUnPadding(void* ptr, void* srcPtr, int mSize, int count) {
328 int sizeBytesPad = mSize * 4;
329 int sizeBytes = mSize * 3;
330 uint8_t *dst = static_cast<uint8_t *>(ptr);
331 uint8_t *src = static_cast<uint8_t *>(srcPtr);
332 for (int i = 0; i < count; i++) {
333 memcpy(dst, src, sizeBytes);
334 dst += sizeBytes;
335 src += sizeBytesPad;
336 }
337}
338
339
340// ---------------------------------------------------------------------------
Jason Sams3eaa338e2009-06-10 15:04:38 -0700341static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800342nContextFinish(JNIEnv *_env, jobject _this, jlong con)
Jason Sams96ed4cf2010-06-15 12:15:57 -0700343{
Andreas Gampe67333922014-11-10 20:35:59 -0800344 if (kLogApi) {
345 ALOGD("nContextFinish, con(%p)", (RsContext)con);
346 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800347 rsContextFinish((RsContext)con);
Jason Sams96ed4cf2010-06-15 12:15:57 -0700348}
349
Yang Ni281c3252014-10-24 08:52:24 -0700350static jlong
351nClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong kernelID,
352 jlong returnValue, jlongArray fieldIDArray,
353 jlongArray valueArray, jintArray sizeArray,
354 jlongArray depClosureArray, jlongArray depFieldIDArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700355 jlong ret = 0;
356
Yang Ni281c3252014-10-24 08:52:24 -0700357 jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
358 jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700359 if (jFieldIDs == nullptr) {
360 ALOGE("Failed to get Java array elements: fieldIDs.");
361 return ret;
362 }
363
Yang Ni281c3252014-10-24 08:52:24 -0700364 jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
365 jsize values_length = _env->GetArrayLength(valueArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700366 if (jValues == nullptr) {
367 ALOGE("Failed to get Java array elements: values.");
368 return ret;
369 }
370
Yang Ni17c2d7a2015-04-30 16:13:54 -0700371 jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
Yang Ni281c3252014-10-24 08:52:24 -0700372 jsize sizes_length = _env->GetArrayLength(sizeArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700373 if (jSizes == nullptr) {
374 ALOGE("Failed to get Java array elements: sizes.");
375 return ret;
376 }
377
Yang Ni281c3252014-10-24 08:52:24 -0700378 jlong* jDepClosures =
379 _env->GetLongArrayElements(depClosureArray, nullptr);
380 jsize depClosures_length = _env->GetArrayLength(depClosureArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700381 if (jDepClosures == nullptr) {
382 ALOGE("Failed to get Java array elements: depClosures.");
383 return ret;
384 }
385
Yang Ni281c3252014-10-24 08:52:24 -0700386 jlong* jDepFieldIDs =
387 _env->GetLongArrayElements(depFieldIDArray, nullptr);
388 jsize depFieldIDs_length = _env->GetArrayLength(depFieldIDArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700389 if (jDepFieldIDs == nullptr) {
390 ALOGE("Failed to get Java array elements: depFieldIDs.");
391 return ret;
392 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700393
394 size_t numValues, numDependencies;
395 RsScriptFieldID* fieldIDs;
Yang Ni17c2d7a2015-04-30 16:13:54 -0700396 RsClosure* depClosures;
397 RsScriptFieldID* depFieldIDs;
398
399 if (fieldIDs_length != values_length || values_length != sizes_length) {
400 ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
401 goto exit;
402 }
403
404 numValues = (size_t)fieldIDs_length;
405
406 if (depClosures_length != depFieldIDs_length) {
407 ALOGE("Unmatched closures and field IDs for dependencies in closure creation.");
408 goto exit;
409 }
410
411 numDependencies = (size_t)depClosures_length;
412
413 if (numDependencies > numValues) {
414 ALOGE("Unexpected number of dependencies in closure creation");
415 goto exit;
416 }
417
Yang Ni7b2a46f2015-05-05 12:41:19 -0700418 if (numValues > RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700419 ALOGE("Too many arguments or globals in closure creation");
420 goto exit;
421 }
422
423 fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
424 if (fieldIDs == nullptr) {
425 goto exit;
426 }
427
428 for (size_t i = 0; i < numValues; i++) {
429 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
430 }
431
Yang Ni17c2d7a2015-04-30 16:13:54 -0700432 depClosures = (RsClosure*)alloca(sizeof(RsClosure) * numDependencies);
433 if (depClosures == nullptr) {
434 goto exit;
435 }
436
437 for (size_t i = 0; i < numDependencies; i++) {
438 depClosures[i] = (RsClosure)jDepClosures[i];
439 }
440
441 depFieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numDependencies);
442 if (depFieldIDs == nullptr) {
443 goto exit;
444 }
445
446 for (size_t i = 0; i < numDependencies; i++) {
Yang Ni281c3252014-10-24 08:52:24 -0700447 depFieldIDs[i] = (RsClosure)jDepFieldIDs[i];
448 }
449
Yang Ni17c2d7a2015-04-30 16:13:54 -0700450 ret = (jlong)(uintptr_t)rsClosureCreate(
Yang Ni281c3252014-10-24 08:52:24 -0700451 (RsContext)con, (RsScriptKernelID)kernelID, (RsAllocation)returnValue,
Yang Ni263cc902015-11-10 13:27:04 -0800452 fieldIDs, numValues, jValues, numValues,
Yang Ni17c2d7a2015-04-30 16:13:54 -0700453 (int*)jSizes, numValues,
454 depClosures, numDependencies,
455 depFieldIDs, numDependencies);
456
457exit:
458
459 _env->ReleaseLongArrayElements(depFieldIDArray, jDepFieldIDs, JNI_ABORT);
460 _env->ReleaseLongArrayElements(depClosureArray, jDepClosures, JNI_ABORT);
461 _env->ReleaseIntArrayElements (sizeArray, jSizes, JNI_ABORT);
462 _env->ReleaseLongArrayElements(valueArray, jValues, JNI_ABORT);
463 _env->ReleaseLongArrayElements(fieldIDArray, jFieldIDs, JNI_ABORT);
464
465 return ret;
Yang Ni281c3252014-10-24 08:52:24 -0700466}
467
Yang Nibe392ad2015-01-23 17:16:02 -0800468static jlong
469nInvokeClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong invokeID,
470 jbyteArray paramArray, jlongArray fieldIDArray, jlongArray valueArray,
471 jintArray sizeArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700472 jlong ret = 0;
473
Yang Nibe392ad2015-01-23 17:16:02 -0800474 jbyte* jParams = _env->GetByteArrayElements(paramArray, nullptr);
475 jsize jParamLength = _env->GetArrayLength(paramArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700476 if (jParams == nullptr) {
477 ALOGE("Failed to get Java array elements: params.");
478 return ret;
479 }
480
Yang Nibe392ad2015-01-23 17:16:02 -0800481 jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
482 jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700483 if (jFieldIDs == nullptr) {
484 ALOGE("Failed to get Java array elements: fieldIDs.");
485 return ret;
486 }
487
Yang Ni17c2d7a2015-04-30 16:13:54 -0700488 jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
489 jsize values_length = _env->GetArrayLength(valueArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700490 if (jValues == nullptr) {
491 ALOGE("Failed to get Java array elements: values.");
492 return ret;
493 }
494
Yang Ni17c2d7a2015-04-30 16:13:54 -0700495 jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
496 jsize sizes_length = _env->GetArrayLength(sizeArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700497 if (jSizes == nullptr) {
498 ALOGE("Failed to get Java array elements: sizes.");
499 return ret;
500 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700501
502 size_t numValues;
503 RsScriptFieldID* fieldIDs;
Yang Ni17c2d7a2015-04-30 16:13:54 -0700504
505 if (fieldIDs_length != values_length || values_length != sizes_length) {
506 ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
507 goto exit;
508 }
509
510 numValues = (size_t) fieldIDs_length;
511
Yang Ni7b2a46f2015-05-05 12:41:19 -0700512 if (numValues > RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700513 ALOGE("Too many arguments or globals in closure creation");
514 goto exit;
515 }
516
517 fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
518 if (fieldIDs == nullptr) {
519 goto exit;
520 }
521
522 for (size_t i = 0; i< numValues; i++) {
Yang Nibe392ad2015-01-23 17:16:02 -0800523 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
524 }
525
Yang Ni17c2d7a2015-04-30 16:13:54 -0700526 ret = (jlong)(uintptr_t)rsInvokeClosureCreate(
Yang Nibe392ad2015-01-23 17:16:02 -0800527 (RsContext)con, (RsScriptInvokeID)invokeID, jParams, jParamLength,
Yang Ni263cc902015-11-10 13:27:04 -0800528 fieldIDs, numValues, jValues, numValues,
Yang Ni17c2d7a2015-04-30 16:13:54 -0700529 (int*)jSizes, numValues);
530
531exit:
532
533 _env->ReleaseIntArrayElements (sizeArray, jSizes, JNI_ABORT);
534 _env->ReleaseLongArrayElements(valueArray, jValues, JNI_ABORT);
535 _env->ReleaseLongArrayElements(fieldIDArray, jFieldIDs, JNI_ABORT);
536 _env->ReleaseByteArrayElements(paramArray, jParams, JNI_ABORT);
537
538 return ret;
Yang Nibe392ad2015-01-23 17:16:02 -0800539}
540
Yang Ni281c3252014-10-24 08:52:24 -0700541static void
542nClosureSetArg(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
543 jint index, jlong value, jint size) {
Yang Ni263cc902015-11-10 13:27:04 -0800544 // Size is signed with -1 indicating the value is an Allocation
Yang Ni281c3252014-10-24 08:52:24 -0700545 rsClosureSetArg((RsContext)con, (RsClosure)closureID, (uint32_t)index,
Yang Ni263cc902015-11-10 13:27:04 -0800546 (uintptr_t)value, size);
Yang Ni281c3252014-10-24 08:52:24 -0700547}
548
549static void
550nClosureSetGlobal(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
551 jlong fieldID, jlong value, jint size) {
Yang Ni263cc902015-11-10 13:27:04 -0800552 // Size is signed with -1 indicating the value is an Allocation
Yang Ni281c3252014-10-24 08:52:24 -0700553 rsClosureSetGlobal((RsContext)con, (RsClosure)closureID,
Yang Ni263cc902015-11-10 13:27:04 -0800554 (RsScriptFieldID)fieldID, (int64_t)value, size);
Yang Ni281c3252014-10-24 08:52:24 -0700555}
556
557static long
Yang Ni35be56c2015-04-02 17:47:56 -0700558nScriptGroup2Create(JNIEnv *_env, jobject _this, jlong con, jstring name,
Yang Niebf63402015-01-16 11:06:26 -0800559 jstring cacheDir, jlongArray closureArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700560 jlong ret = 0;
561
Yang Ni35be56c2015-04-02 17:47:56 -0700562 AutoJavaStringToUTF8 nameUTF(_env, name);
Yang Niebf63402015-01-16 11:06:26 -0800563 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
564
Yang Ni281c3252014-10-24 08:52:24 -0700565 jlong* jClosures = _env->GetLongArrayElements(closureArray, nullptr);
566 jsize numClosures = _env->GetArrayLength(closureArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700567 if (jClosures == nullptr) {
568 ALOGE("Failed to get Java array elements: closures.");
569 return ret;
570 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700571
572 RsClosure* closures;
573
Yang Ni7b2a46f2015-05-05 12:41:19 -0700574 if (numClosures > (jsize) RS_SCRIPT_GROUP_MAX_NUMBER_CLOSURES) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700575 ALOGE("Too many closures in script group");
576 goto exit;
577 }
578
579 closures = (RsClosure*)alloca(sizeof(RsClosure) * numClosures);
580 if (closures == nullptr) {
581 goto exit;
582 }
583
Yang Ni281c3252014-10-24 08:52:24 -0700584 for (int i = 0; i < numClosures; i++) {
585 closures[i] = (RsClosure)jClosures[i];
586 }
587
Yang Ni17c2d7a2015-04-30 16:13:54 -0700588 ret = (jlong)(uintptr_t)rsScriptGroup2Create(
Yang Ni35be56c2015-04-02 17:47:56 -0700589 (RsContext)con, nameUTF.c_str(), nameUTF.length(),
590 cacheDirUTF.c_str(), cacheDirUTF.length(),
Yang Niebf63402015-01-16 11:06:26 -0800591 closures, numClosures);
Yang Ni17c2d7a2015-04-30 16:13:54 -0700592
593exit:
594
595 _env->ReleaseLongArrayElements(closureArray, jClosures, JNI_ABORT);
596
597 return ret;
Yang Ni281c3252014-10-24 08:52:24 -0700598}
599
600static void
601nScriptGroup2Execute(JNIEnv *_env, jobject _this, jlong con, jlong groupID) {
602 rsScriptGroupExecute((RsContext)con, (RsScriptGroup2)groupID);
603}
604
Jason Sams96ed4cf2010-06-15 12:15:57 -0700605static void
Tim Murray25207df2015-01-12 16:47:56 -0800606nScriptIntrinsicBLAS_Single(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
607 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
608 jfloat alpha, jlong A, jlong B, jfloat beta, jlong C, jint incX, jint incY,
609 jint KL, jint KU) {
610 RsBlasCall call;
611 memset(&call, 0, sizeof(call));
612 call.func = (RsBlasFunction)func;
613 call.transA = (RsBlasTranspose)TransA;
614 call.transB = (RsBlasTranspose)TransB;
615 call.side = (RsBlasSide)Side;
616 call.uplo = (RsBlasUplo)Uplo;
617 call.diag = (RsBlasDiag)Diag;
618 call.M = M;
619 call.N = N;
620 call.K = K;
621 call.alpha.f = alpha;
622 call.beta.f = beta;
623 call.incX = incX;
624 call.incY = incY;
625 call.KL = KL;
626 call.KU = KU;
627
628 RsAllocation in_allocs[3];
629 in_allocs[0] = (RsAllocation)A;
630 in_allocs[1] = (RsAllocation)B;
631 in_allocs[2] = (RsAllocation)C;
632
633 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
634 in_allocs, sizeof(in_allocs), nullptr,
635 &call, sizeof(call), nullptr, 0);
636}
637
638static void
639nScriptIntrinsicBLAS_Double(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
640 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
641 jdouble alpha, jlong A, jlong B, jdouble beta, jlong C, jint incX, jint incY,
642 jint KL, jint KU) {
643 RsBlasCall call;
644 memset(&call, 0, sizeof(call));
645 call.func = (RsBlasFunction)func;
646 call.transA = (RsBlasTranspose)TransA;
647 call.transB = (RsBlasTranspose)TransB;
648 call.side = (RsBlasSide)Side;
649 call.uplo = (RsBlasUplo)Uplo;
650 call.diag = (RsBlasDiag)Diag;
651 call.M = M;
652 call.N = N;
653 call.K = K;
654 call.alpha.d = alpha;
655 call.beta.d = beta;
656 call.incX = incX;
657 call.incY = incY;
658 call.KL = KL;
659 call.KU = KU;
660
661 RsAllocation in_allocs[3];
662 in_allocs[0] = (RsAllocation)A;
663 in_allocs[1] = (RsAllocation)B;
664 in_allocs[2] = (RsAllocation)C;
665
666 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700667 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800668 &call, sizeof(call), nullptr, 0);
669}
670
671static void
672nScriptIntrinsicBLAS_Complex(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
673 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
674 jfloat alphaX, jfloat alphaY, jlong A, jlong B, jfloat betaX,
675 jfloat betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
676 RsBlasCall call;
677 memset(&call, 0, sizeof(call));
678 call.func = (RsBlasFunction)func;
679 call.transA = (RsBlasTranspose)TransA;
680 call.transB = (RsBlasTranspose)TransB;
681 call.side = (RsBlasSide)Side;
682 call.uplo = (RsBlasUplo)Uplo;
683 call.diag = (RsBlasDiag)Diag;
684 call.M = M;
685 call.N = N;
686 call.K = K;
687 call.alpha.c.r = alphaX;
688 call.alpha.c.i = alphaY;
689 call.beta.c.r = betaX;
Miao Wang82585b32015-04-30 13:44:49 -0700690 call.beta.c.i = betaY;
Tim Murray25207df2015-01-12 16:47:56 -0800691 call.incX = incX;
692 call.incY = incY;
693 call.KL = KL;
694 call.KU = KU;
695
696 RsAllocation in_allocs[3];
697 in_allocs[0] = (RsAllocation)A;
698 in_allocs[1] = (RsAllocation)B;
699 in_allocs[2] = (RsAllocation)C;
700
701 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700702 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800703 &call, sizeof(call), nullptr, 0);
704}
705
706static void
707nScriptIntrinsicBLAS_Z(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
708 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
709 jdouble alphaX, jdouble alphaY, jlong A, jlong B, jdouble betaX,
710 jdouble betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
711 RsBlasCall call;
712 memset(&call, 0, sizeof(call));
713 call.func = (RsBlasFunction)func;
714 call.transA = (RsBlasTranspose)TransA;
715 call.transB = (RsBlasTranspose)TransB;
716 call.side = (RsBlasSide)Side;
717 call.uplo = (RsBlasUplo)Uplo;
718 call.diag = (RsBlasDiag)Diag;
719 call.M = M;
720 call.N = N;
721 call.K = K;
722 call.alpha.z.r = alphaX;
723 call.alpha.z.i = alphaY;
724 call.beta.z.r = betaX;
Miao Wang82585b32015-04-30 13:44:49 -0700725 call.beta.z.i = betaY;
Tim Murray25207df2015-01-12 16:47:56 -0800726 call.incX = incX;
727 call.incY = incY;
728 call.KL = KL;
729 call.KU = KU;
730
731 RsAllocation in_allocs[3];
732 in_allocs[0] = (RsAllocation)A;
733 in_allocs[1] = (RsAllocation)B;
734 in_allocs[2] = (RsAllocation)C;
735
736 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700737 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800738 &call, sizeof(call), nullptr, 0);
739}
740
741
742static void
Tim Murray9cb16a22015-04-01 11:07:16 -0700743nScriptIntrinsicBLAS_BNNM(JNIEnv *_env, jobject _this, jlong con, jlong id, jint M, jint N, jint K,
744 jlong A, jint a_offset, jlong B, jint b_offset, jlong C, jint c_offset,
745 jint c_mult_int) {
746 RsBlasCall call;
747 memset(&call, 0, sizeof(call));
748 call.func = RsBlas_bnnm;
749 call.M = M;
750 call.N = N;
751 call.K = K;
Miao Wang25148062015-06-29 17:43:03 -0700752 call.a_offset = a_offset & 0xFF;
753 call.b_offset = b_offset & 0xFF;
Tim Murray9cb16a22015-04-01 11:07:16 -0700754 call.c_offset = c_offset;
755 call.c_mult_int = c_mult_int;
756
757 RsAllocation in_allocs[3];
758 in_allocs[0] = (RsAllocation)A;
759 in_allocs[1] = (RsAllocation)B;
760 in_allocs[2] = (RsAllocation)C;
761
762 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700763 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray9cb16a22015-04-01 11:07:16 -0700764 &call, sizeof(call), nullptr, 0);
765}
766
767
768static void
Tim Murray460a0492013-11-19 12:45:54 -0800769nAssignName(JNIEnv *_env, jobject _this, jlong con, jlong obj, jbyteArray str)
Jason Sams3eaa338e2009-06-10 15:04:38 -0700770{
Andreas Gampe67333922014-11-10 20:35:59 -0800771 if (kLogApi) {
772 ALOGD("nAssignName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
773 }
Jason Sams3eaa338e2009-06-10 15:04:38 -0700774 jint len = _env->GetArrayLength(str);
775 jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
Miao Wangba8766c2015-10-12 17:24:13 -0700776 if (cptr == nullptr) {
777 ALOGE("Failed to get Java array elements");
778 return;
779 }
780
Tim Murrayeff663f2013-11-15 13:08:30 -0800781 rsAssignName((RsContext)con, (void *)obj, (const char *)cptr, len);
Jason Sams3eaa338e2009-06-10 15:04:38 -0700782 _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
783}
784
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700785static jstring
Tim Murray460a0492013-11-19 12:45:54 -0800786nGetName(JNIEnv *_env, jobject _this, jlong con, jlong obj)
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700787{
Andreas Gampe67333922014-11-10 20:35:59 -0800788 if (kLogApi) {
789 ALOGD("nGetName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
790 }
Chris Wailes488230c32014-08-14 11:22:40 -0700791 const char *name = nullptr;
Tim Murrayeff663f2013-11-15 13:08:30 -0800792 rsaGetName((RsContext)con, (void *)obj, &name);
Chris Wailes488230c32014-08-14 11:22:40 -0700793 if(name == nullptr || strlen(name) == 0) {
794 return nullptr;
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700795 }
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700796 return _env->NewStringUTF(name);
797}
798
Jason Sams7ce033d2009-08-18 14:14:24 -0700799static void
Tim Murray460a0492013-11-19 12:45:54 -0800800nObjDestroy(JNIEnv *_env, jobject _this, jlong con, jlong obj)
Jason Sams7ce033d2009-08-18 14:14:24 -0700801{
Andreas Gampe67333922014-11-10 20:35:59 -0800802 if (kLogApi) {
803 ALOGD("nObjDestroy, con(%p) obj(%p)", (RsContext)con, (void *)obj);
804 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800805 rsObjDestroy((RsContext)con, (void *)obj);
Jason Sams7ce033d2009-08-18 14:14:24 -0700806}
807
Jason Sams3eaa338e2009-06-10 15:04:38 -0700808// ---------------------------------------------------------------------------
809
Tim Murrayeff663f2013-11-15 13:08:30 -0800810static jlong
Jason Samsd19f10d2009-05-22 14:03:28 -0700811nDeviceCreate(JNIEnv *_env, jobject _this)
812{
Andreas Gampe67333922014-11-10 20:35:59 -0800813 if (kLogApi) {
814 ALOGD("nDeviceCreate");
815 }
Tim Murray3aa89c12014-08-18 17:51:22 -0700816 return (jlong)(uintptr_t)rsDeviceCreate();
Jason Samsd19f10d2009-05-22 14:03:28 -0700817}
818
819static void
Tim Murray5eaf4682014-01-10 11:25:52 -0800820nDeviceDestroy(JNIEnv *_env, jobject _this, jlong dev)
Jason Samsd19f10d2009-05-22 14:03:28 -0700821{
Andreas Gampe67333922014-11-10 20:35:59 -0800822 if (kLogApi) {
823 ALOGD("nDeviceDestroy");
824 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700825 return rsDeviceDestroy((RsDevice)dev);
826}
827
Jason Samsebfb4362009-09-23 13:57:02 -0700828static void
Tim Murray5eaf4682014-01-10 11:25:52 -0800829nDeviceSetConfig(JNIEnv *_env, jobject _this, jlong dev, jint p, jint value)
Jason Samsebfb4362009-09-23 13:57:02 -0700830{
Andreas Gampe67333922014-11-10 20:35:59 -0800831 if (kLogApi) {
832 ALOGD("nDeviceSetConfig dev(%p), param(%i), value(%i)", (void *)dev, p, value);
833 }
Jason Samsebfb4362009-09-23 13:57:02 -0700834 return rsDeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value);
835}
836
Tim Murrayeff663f2013-11-15 13:08:30 -0800837static jlong
Jason Sams81cd2b12014-12-02 12:36:43 -0800838nContextCreate(JNIEnv *_env, jobject _this, jlong dev, jint flags, jint sdkVer, jint contextType)
Jason Samsd19f10d2009-05-22 14:03:28 -0700839{
Andreas Gampe67333922014-11-10 20:35:59 -0800840 if (kLogApi) {
841 ALOGD("nContextCreate");
842 }
Jason Sams81cd2b12014-12-02 12:36:43 -0800843 return (jlong)(uintptr_t)rsContextCreate((RsDevice)dev, 0, sdkVer, (RsContextType)contextType, flags);
Jason Sams704ff642010-02-09 16:05:07 -0800844}
845
Tim Murrayeff663f2013-11-15 13:08:30 -0800846static jlong
Tim Murray5eaf4682014-01-10 11:25:52 -0800847nContextCreateGL(JNIEnv *_env, jobject _this, jlong dev, jint ver, jint sdkVer,
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000848 jint colorMin, jint colorPref,
849 jint alphaMin, jint alphaPref,
850 jint depthMin, jint depthPref,
851 jint stencilMin, jint stencilPref,
852 jint samplesMin, jint samplesPref, jfloat samplesQ,
853 jint dpi)
Jason Sams704ff642010-02-09 16:05:07 -0800854{
Jason Sams11c8af92010-10-13 15:31:10 -0700855 RsSurfaceConfig sc;
856 sc.alphaMin = alphaMin;
857 sc.alphaPref = alphaPref;
858 sc.colorMin = colorMin;
859 sc.colorPref = colorPref;
860 sc.depthMin = depthMin;
861 sc.depthPref = depthPref;
862 sc.samplesMin = samplesMin;
863 sc.samplesPref = samplesPref;
864 sc.samplesQ = samplesQ;
865
Andreas Gampe67333922014-11-10 20:35:59 -0800866 if (kLogApi) {
867 ALOGD("nContextCreateGL");
868 }
Tim Murray3aa89c12014-08-18 17:51:22 -0700869 return (jlong)(uintptr_t)rsContextCreateGL((RsDevice)dev, ver, sdkVer, sc, dpi);
Jason Samsd19f10d2009-05-22 14:03:28 -0700870}
871
872static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800873nContextSetPriority(JNIEnv *_env, jobject _this, jlong con, jint p)
Jason Sams7d787b42009-11-15 12:14:26 -0800874{
Andreas Gampe67333922014-11-10 20:35:59 -0800875 if (kLogApi) {
876 ALOGD("ContextSetPriority, con(%p), priority(%i)", (RsContext)con, p);
877 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800878 rsContextSetPriority((RsContext)con, p);
Jason Sams7d787b42009-11-15 12:14:26 -0800879}
880
Tim Murray47f31582015-04-07 15:43:24 -0700881static void
882nContextSetCacheDir(JNIEnv *_env, jobject _this, jlong con, jstring cacheDir)
883{
884 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
885
886 if (kLogApi) {
887 ALOGD("ContextSetCacheDir, con(%p), cacheDir(%s)", (RsContext)con, cacheDirUTF.c_str());
888 }
889 rsContextSetCacheDir((RsContext)con, cacheDirUTF.c_str(), cacheDirUTF.length());
890}
891
Jason Sams7d787b42009-11-15 12:14:26 -0800892
893
894static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800895nContextSetSurface(JNIEnv *_env, jobject _this, jlong con, jint width, jint height, jobject wnd)
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800896{
Andreas Gampe67333922014-11-10 20:35:59 -0800897 if (kLogApi) {
898 ALOGD("nContextSetSurface, con(%p), width(%i), height(%i), surface(%p)", (RsContext)con,
899 width, height, (Surface *)wnd);
900 }
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800901
Chris Wailes488230c32014-08-14 11:22:40 -0700902 ANativeWindow * window = nullptr;
903 if (wnd == nullptr) {
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800904
905 } else {
Jeff Brown64a55af2012-08-26 02:47:39 -0700906 window = android_view_Surface_getNativeWindow(_env, wnd).get();
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800907 }
908
Tim Murrayeff663f2013-11-15 13:08:30 -0800909 rsContextSetSurface((RsContext)con, width, height, window);
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800910}
911
912static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800913nContextDestroy(JNIEnv *_env, jobject _this, jlong con)
Jason Samsd19f10d2009-05-22 14:03:28 -0700914{
Andreas Gampe67333922014-11-10 20:35:59 -0800915 if (kLogApi) {
916 ALOGD("nContextDestroy, con(%p)", (RsContext)con);
917 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800918 rsContextDestroy((RsContext)con);
Jason Samsd19f10d2009-05-22 14:03:28 -0700919}
920
Jason Sams715333b2009-11-17 17:26:46 -0800921static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800922nContextDump(JNIEnv *_env, jobject _this, jlong con, jint bits)
Jason Sams715333b2009-11-17 17:26:46 -0800923{
Andreas Gampe67333922014-11-10 20:35:59 -0800924 if (kLogApi) {
925 ALOGD("nContextDump, con(%p) bits(%i)", (RsContext)con, bits);
926 }
Jason Sams715333b2009-11-17 17:26:46 -0800927 rsContextDump((RsContext)con, bits);
928}
Jason Samsd19f10d2009-05-22 14:03:28 -0700929
930static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800931nContextPause(JNIEnv *_env, jobject _this, jlong con)
Jason Sams65e7aa52009-09-24 17:38:20 -0700932{
Andreas Gampe67333922014-11-10 20:35:59 -0800933 if (kLogApi) {
934 ALOGD("nContextPause, con(%p)", (RsContext)con);
935 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800936 rsContextPause((RsContext)con);
Jason Sams65e7aa52009-09-24 17:38:20 -0700937}
938
939static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800940nContextResume(JNIEnv *_env, jobject _this, jlong con)
Jason Sams65e7aa52009-09-24 17:38:20 -0700941{
Andreas Gampe67333922014-11-10 20:35:59 -0800942 if (kLogApi) {
943 ALOGD("nContextResume, con(%p)", (RsContext)con);
944 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800945 rsContextResume((RsContext)con);
Jason Sams65e7aa52009-09-24 17:38:20 -0700946}
947
Jason Sams1c415172010-11-08 17:06:46 -0800948
949static jstring
Tim Murrayeff663f2013-11-15 13:08:30 -0800950nContextGetErrorMessage(JNIEnv *_env, jobject _this, jlong con)
Jason Sams1c415172010-11-08 17:06:46 -0800951{
Andreas Gampe67333922014-11-10 20:35:59 -0800952 if (kLogApi) {
953 ALOGD("nContextGetErrorMessage, con(%p)", (RsContext)con);
954 }
Jason Sams1c415172010-11-08 17:06:46 -0800955 char buf[1024];
956
957 size_t receiveLen;
958 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800959 int id = rsContextGetMessage((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -0700960 buf, sizeof(buf),
961 &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -0700962 &subID, sizeof(subID));
Jason Sams1c415172010-11-08 17:06:46 -0800963 if (!id && receiveLen) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +0100964 ALOGV("message receive buffer too small. %zu", receiveLen);
Jason Sams1c415172010-11-08 17:06:46 -0800965 }
966 return _env->NewStringUTF(buf);
967}
968
Jason Samsedbfabd2011-05-17 15:01:29 -0700969static jint
Tim Murrayeff663f2013-11-15 13:08:30 -0800970nContextGetUserMessage(JNIEnv *_env, jobject _this, jlong con, jintArray data)
Jason Sams516c3192009-10-06 13:58:47 -0700971{
Jason Sams516c3192009-10-06 13:58:47 -0700972 jint len = _env->GetArrayLength(data);
Andreas Gampe67333922014-11-10 20:35:59 -0800973 if (kLogApi) {
974 ALOGD("nContextGetMessage, con(%p), len(%i)", (RsContext)con, len);
975 }
Chris Wailes488230c32014-08-14 11:22:40 -0700976 jint *ptr = _env->GetIntArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -0700977 if (ptr == nullptr) {
978 ALOGE("Failed to get Java array elements");
979 return 0;
980 }
Jason Sams516c3192009-10-06 13:58:47 -0700981 size_t receiveLen;
Jason Sams1c415172010-11-08 17:06:46 -0800982 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800983 int id = rsContextGetMessage((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -0700984 ptr, len * 4,
985 &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -0700986 &subID, sizeof(subID));
Jason Sams516c3192009-10-06 13:58:47 -0700987 if (!id && receiveLen) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +0100988 ALOGV("message receive buffer too small. %zu", receiveLen);
Jason Sams516c3192009-10-06 13:58:47 -0700989 }
990 _env->ReleaseIntArrayElements(data, ptr, 0);
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000991 return (jint)id;
Jason Sams1c415172010-11-08 17:06:46 -0800992}
993
994static jint
Tim Murrayeff663f2013-11-15 13:08:30 -0800995nContextPeekMessage(JNIEnv *_env, jobject _this, jlong con, jintArray auxData)
Jason Sams1c415172010-11-08 17:06:46 -0800996{
Andreas Gampe67333922014-11-10 20:35:59 -0800997 if (kLogApi) {
998 ALOGD("nContextPeekMessage, con(%p)", (RsContext)con);
999 }
Chris Wailes488230c32014-08-14 11:22:40 -07001000 jint *auxDataPtr = _env->GetIntArrayElements(auxData, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001001 if (auxDataPtr == nullptr) {
1002 ALOGE("Failed to get Java array elements");
1003 return 0;
1004 }
Jason Sams1c415172010-11-08 17:06:46 -08001005 size_t receiveLen;
1006 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -08001007 int id = rsContextPeekMessage((RsContext)con, &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -07001008 &subID, sizeof(subID));
Jason Sams1c415172010-11-08 17:06:46 -08001009 auxDataPtr[0] = (jint)subID;
1010 auxDataPtr[1] = (jint)receiveLen;
1011 _env->ReleaseIntArrayElements(auxData, auxDataPtr, 0);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001012 return (jint)id;
Jason Sams516c3192009-10-06 13:58:47 -07001013}
1014
Tim Murrayeff663f2013-11-15 13:08:30 -08001015static void nContextInitToClient(JNIEnv *_env, jobject _this, jlong con)
Jason Sams516c3192009-10-06 13:58:47 -07001016{
Andreas Gampe67333922014-11-10 20:35:59 -08001017 if (kLogApi) {
1018 ALOGD("nContextInitToClient, con(%p)", (RsContext)con);
1019 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001020 rsContextInitToClient((RsContext)con);
Jason Sams516c3192009-10-06 13:58:47 -07001021}
1022
Tim Murrayeff663f2013-11-15 13:08:30 -08001023static void nContextDeinitToClient(JNIEnv *_env, jobject _this, jlong con)
Jason Sams516c3192009-10-06 13:58:47 -07001024{
Andreas Gampe67333922014-11-10 20:35:59 -08001025 if (kLogApi) {
1026 ALOGD("nContextDeinitToClient, con(%p)", (RsContext)con);
1027 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001028 rsContextDeinitToClient((RsContext)con);
Jason Sams516c3192009-10-06 13:58:47 -07001029}
1030
Jason Sams455d6442013-02-05 19:20:18 -08001031static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001032nContextSendMessage(JNIEnv *_env, jobject _this, jlong con, jint id, jintArray data)
Jason Sams455d6442013-02-05 19:20:18 -08001033{
Chris Wailes488230c32014-08-14 11:22:40 -07001034 jint *ptr = nullptr;
Jason Sams455d6442013-02-05 19:20:18 -08001035 jint len = 0;
1036 if (data) {
1037 len = _env->GetArrayLength(data);
Stephen Hines4a043c12014-08-21 23:20:32 -07001038 ptr = _env->GetIntArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001039 if (ptr == nullptr) {
1040 ALOGE("Failed to get Java array elements");
1041 return;
1042 }
Jason Sams455d6442013-02-05 19:20:18 -08001043 }
Andreas Gampe67333922014-11-10 20:35:59 -08001044 if (kLogApi) {
1045 ALOGD("nContextSendMessage, con(%p), id(%i), len(%i)", (RsContext)con, id, len);
1046 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001047 rsContextSendMessage((RsContext)con, id, (const uint8_t *)ptr, len * sizeof(int));
Jason Sams455d6442013-02-05 19:20:18 -08001048 if (data) {
1049 _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
1050 }
1051}
1052
1053
Jason Sams516c3192009-10-06 13:58:47 -07001054
Tim Murray460a0492013-11-19 12:45:54 -08001055static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001056nElementCreate(JNIEnv *_env, jobject _this, jlong con, jlong type, jint kind, jboolean norm,
1057 jint size)
Jason Samsd19f10d2009-05-22 14:03:28 -07001058{
Andreas Gampe67333922014-11-10 20:35:59 -08001059 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001060 ALOGD("nElementCreate, con(%p), type(%" PRId64 "), kind(%i), norm(%i), size(%i)", (RsContext)con,
Andreas Gampe67333922014-11-10 20:35:59 -08001061 type, kind, norm, size);
1062 }
1063 return (jlong)(uintptr_t)rsElementCreate((RsContext)con, (RsDataType)type, (RsDataKind)kind,
1064 norm, size);
Jason Samsd19f10d2009-05-22 14:03:28 -07001065}
1066
Tim Murray460a0492013-11-19 12:45:54 -08001067static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001068nElementCreate2(JNIEnv *_env, jobject _this, jlong con,
Ashok Bhat98071552014-02-12 09:54:43 +00001069 jlongArray _ids, jobjectArray _names, jintArray _arraySizes)
Jason Samsd19f10d2009-05-22 14:03:28 -07001070{
Jason Sams718cd1f2009-12-23 14:35:29 -08001071 int fieldCount = _env->GetArrayLength(_ids);
Andreas Gampe67333922014-11-10 20:35:59 -08001072 if (kLogApi) {
1073 ALOGD("nElementCreate2, con(%p)", (RsContext)con);
1074 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001075
Chris Wailes488230c32014-08-14 11:22:40 -07001076 jlong *jIds = _env->GetLongArrayElements(_ids, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001077 if (jIds == nullptr) {
1078 ALOGE("Failed to get Java array elements: ids");
1079 return 0;
1080 }
Chris Wailes488230c32014-08-14 11:22:40 -07001081 jint *jArraySizes = _env->GetIntArrayElements(_arraySizes, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001082 if (jArraySizes == nullptr) {
1083 ALOGE("Failed to get Java array elements: arraySizes");
1084 return 0;
1085 }
Ashok Bhat98071552014-02-12 09:54:43 +00001086
1087 RsElement *ids = (RsElement*)malloc(fieldCount * sizeof(RsElement));
1088 uint32_t *arraySizes = (uint32_t *)malloc(fieldCount * sizeof(uint32_t));
1089
1090 for(int i = 0; i < fieldCount; i ++) {
1091 ids[i] = (RsElement)jIds[i];
1092 arraySizes[i] = (uint32_t)jArraySizes[i];
1093 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001094
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08001095 AutoJavaStringArrayToUTF8 names(_env, _names, fieldCount);
1096
1097 const char **nameArray = names.c_str();
1098 size_t *sizeArray = names.c_str_len();
1099
Tim Murray3aa89c12014-08-18 17:51:22 -07001100 jlong id = (jlong)(uintptr_t)rsElementCreate2((RsContext)con,
Ashok Bhat98071552014-02-12 09:54:43 +00001101 (const RsElement *)ids, fieldCount,
Jason Sams7a22e102011-05-06 14:14:30 -07001102 nameArray, fieldCount * sizeof(size_t), sizeArray,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001103 (const uint32_t *)arraySizes, fieldCount);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08001104
Ashok Bhat98071552014-02-12 09:54:43 +00001105 free(ids);
1106 free(arraySizes);
1107 _env->ReleaseLongArrayElements(_ids, jIds, JNI_ABORT);
1108 _env->ReleaseIntArrayElements(_arraySizes, jArraySizes, JNI_ABORT);
1109
Tim Murray3aa89c12014-08-18 17:51:22 -07001110 return (jlong)(uintptr_t)id;
Jason Samsd19f10d2009-05-22 14:03:28 -07001111}
1112
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001113static void
Tim Murray460a0492013-11-19 12:45:54 -08001114nElementGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jintArray _elementData)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001115{
1116 int dataSize = _env->GetArrayLength(_elementData);
Andreas Gampe67333922014-11-10 20:35:59 -08001117 if (kLogApi) {
1118 ALOGD("nElementGetNativeData, con(%p)", (RsContext)con);
1119 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001120
1121 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
1122 assert(dataSize == 5);
1123
Narayan Kamath78c0ce52014-03-19 10:15:51 +00001124 uintptr_t elementData[5];
Tim Murrayeff663f2013-11-15 13:08:30 -08001125 rsaElementGetNativeData((RsContext)con, (RsElement)id, elementData, dataSize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001126
1127 for(jint i = 0; i < dataSize; i ++) {
Ashok Bhat98071552014-02-12 09:54:43 +00001128 const jint data = (jint)elementData[i];
1129 _env->SetIntArrayRegion(_elementData, i, 1, &data);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001130 }
1131}
1132
1133
1134static void
Tim Murray460a0492013-11-19 12:45:54 -08001135nElementGetSubElements(JNIEnv *_env, jobject _this, jlong con, jlong id,
Ashok Bhat98071552014-02-12 09:54:43 +00001136 jlongArray _IDs,
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -07001137 jobjectArray _names,
1138 jintArray _arraySizes)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001139{
Ashok Bhat98071552014-02-12 09:54:43 +00001140 uint32_t dataSize = _env->GetArrayLength(_IDs);
Andreas Gampe67333922014-11-10 20:35:59 -08001141 if (kLogApi) {
1142 ALOGD("nElementGetSubElements, con(%p)", (RsContext)con);
1143 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001144
Ashok Bhat98071552014-02-12 09:54:43 +00001145 uintptr_t *ids = (uintptr_t*)malloc(dataSize * sizeof(uintptr_t));
1146 const char **names = (const char **)malloc(dataSize * sizeof(const char *));
Narayan Kamath78c0ce52014-03-19 10:15:51 +00001147 uint32_t *arraySizes = (uint32_t *)malloc(dataSize * sizeof(uint32_t));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001148
Andreas Gampe67333922014-11-10 20:35:59 -08001149 rsaElementGetSubElements((RsContext)con, (RsElement)id, ids, names, arraySizes,
1150 (uint32_t)dataSize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001151
Ashok Bhat98071552014-02-12 09:54:43 +00001152 for(uint32_t i = 0; i < dataSize; i++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07001153 const jlong id = (jlong)(uintptr_t)ids[i];
Ashok Bhat98071552014-02-12 09:54:43 +00001154 const jint arraySize = (jint)arraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001155 _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
Ashok Bhat98071552014-02-12 09:54:43 +00001156 _env->SetLongArrayRegion(_IDs, i, 1, &id);
1157 _env->SetIntArrayRegion(_arraySizes, i, 1, &arraySize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001158 }
1159
1160 free(ids);
1161 free(names);
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -07001162 free(arraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001163}
1164
Jason Samsd19f10d2009-05-22 14:03:28 -07001165// -----------------------------------
1166
Tim Murray460a0492013-11-19 12:45:54 -08001167static jlong
1168nTypeCreate(JNIEnv *_env, jobject _this, jlong con, jlong eid,
Jason Samsb109cc72013-01-07 18:20:12 -08001169 jint dimx, jint dimy, jint dimz, jboolean mips, jboolean faces, jint yuv)
Jason Samsd19f10d2009-05-22 14:03:28 -07001170{
Andreas Gampe67333922014-11-10 20:35:59 -08001171 if (kLogApi) {
1172 ALOGD("nTypeCreate, con(%p) eid(%p), x(%i), y(%i), z(%i), mips(%i), faces(%i), yuv(%i)",
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001173 (RsContext)con, (void*)eid, dimx, dimy, dimz, mips, faces, yuv);
Andreas Gampe67333922014-11-10 20:35:59 -08001174 }
Jason Sams3b9c52a2010-10-14 17:48:46 -07001175
Andreas Gampe67333922014-11-10 20:35:59 -08001176 return (jlong)(uintptr_t)rsTypeCreate((RsContext)con, (RsElement)eid, dimx, dimy, dimz, mips,
1177 faces, yuv);
Jason Samsd19f10d2009-05-22 14:03:28 -07001178}
1179
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001180static void
Ashok Bhat98071552014-02-12 09:54:43 +00001181nTypeGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jlongArray _typeData)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001182{
1183 // We are packing 6 items: mDimX; mDimY; mDimZ;
1184 // mDimLOD; mDimFaces; mElement; into typeData
1185 int elementCount = _env->GetArrayLength(_typeData);
1186
1187 assert(elementCount == 6);
Andreas Gampe67333922014-11-10 20:35:59 -08001188 if (kLogApi) {
1189 ALOGD("nTypeGetNativeData, con(%p)", (RsContext)con);
1190 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001191
Ashok Bhat98071552014-02-12 09:54:43 +00001192 uintptr_t typeData[6];
Tim Murrayeff663f2013-11-15 13:08:30 -08001193 rsaTypeGetNativeData((RsContext)con, (RsType)id, typeData, 6);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001194
1195 for(jint i = 0; i < elementCount; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07001196 const jlong data = (jlong)(uintptr_t)typeData[i];
Ashok Bhat98071552014-02-12 09:54:43 +00001197 _env->SetLongArrayRegion(_typeData, i, 1, &data);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001198 }
1199}
1200
Jason Samsd19f10d2009-05-22 14:03:28 -07001201// -----------------------------------
1202
Tim Murray460a0492013-11-19 12:45:54 -08001203static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001204nAllocationCreateTyped(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mips, jint usage,
1205 jlong pointer)
Jason Samsd19f10d2009-05-22 14:03:28 -07001206{
Andreas Gampe67333922014-11-10 20:35:59 -08001207 if (kLogApi) {
1208 ALOGD("nAllocationCreateTyped, con(%p), type(%p), mip(%i), usage(%i), ptr(%p)",
1209 (RsContext)con, (RsElement)type, mips, usage, (void *)pointer);
1210 }
1211 return (jlong)(uintptr_t) rsAllocationCreateTyped((RsContext)con, (RsType)type,
1212 (RsAllocationMipmapControl)mips,
1213 (uint32_t)usage, (uintptr_t)pointer);
Jason Samsd19f10d2009-05-22 14:03:28 -07001214}
1215
Jason Samsd19f10d2009-05-22 14:03:28 -07001216static void
Tim Murray460a0492013-11-19 12:45:54 -08001217nAllocationSyncAll(JNIEnv *_env, jobject _this, jlong con, jlong a, jint bits)
Jason Sams5476b452010-12-08 16:14:36 -08001218{
Andreas Gampe67333922014-11-10 20:35:59 -08001219 if (kLogApi) {
1220 ALOGD("nAllocationSyncAll, con(%p), a(%p), bits(0x%08x)", (RsContext)con, (RsAllocation)a,
1221 bits);
1222 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001223 rsAllocationSyncAll((RsContext)con, (RsAllocation)a, (RsAllocationUsageType)bits);
Jason Sams5476b452010-12-08 16:14:36 -08001224}
1225
Jason Sams72226e02013-02-22 12:45:54 -08001226static jobject
Tim Murray460a0492013-11-19 12:45:54 -08001227nAllocationGetSurface(JNIEnv *_env, jobject _this, jlong con, jlong a)
Jason Sams615e7ce2012-01-13 14:01:20 -08001228{
Andreas Gampe67333922014-11-10 20:35:59 -08001229 if (kLogApi) {
1230 ALOGD("nAllocationGetSurface, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1231 }
Jason Sams615e7ce2012-01-13 14:01:20 -08001232
Andreas Gampe67333922014-11-10 20:35:59 -08001233 IGraphicBufferProducer *v = (IGraphicBufferProducer *)rsAllocationGetSurface((RsContext)con,
1234 (RsAllocation)a);
Jason Sams72226e02013-02-22 12:45:54 -08001235 sp<IGraphicBufferProducer> bp = v;
Chris Wailes488230c32014-08-14 11:22:40 -07001236 v->decStrong(nullptr);
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001237
Jason Sams72226e02013-02-22 12:45:54 -08001238 jobject o = android_view_Surface_createFromIGraphicBufferProducer(_env, bp);
1239 return o;
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001240}
1241
1242static void
Tim Murray460a0492013-11-19 12:45:54 -08001243nAllocationSetSurface(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject sur)
Jason Sams163766c2012-02-15 12:04:24 -08001244{
Andreas Gampe67333922014-11-10 20:35:59 -08001245 if (kLogApi) {
1246 ALOGD("nAllocationSetSurface, con(%p), alloc(%p), surface(%p)", (RsContext)con,
1247 (RsAllocation)alloc, (Surface *)sur);
1248 }
Jason Sams163766c2012-02-15 12:04:24 -08001249
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001250 sp<Surface> s;
Jason Sams163766c2012-02-15 12:04:24 -08001251 if (sur != 0) {
Jeff Brown64a55af2012-08-26 02:47:39 -07001252 s = android_view_Surface_getSurface(_env, sur);
Jason Sams163766c2012-02-15 12:04:24 -08001253 }
1254
Andreas Gampe67333922014-11-10 20:35:59 -08001255 rsAllocationSetSurface((RsContext)con, (RsAllocation)alloc,
1256 static_cast<ANativeWindow *>(s.get()));
Jason Sams163766c2012-02-15 12:04:24 -08001257}
1258
1259static void
Tim Murray460a0492013-11-19 12:45:54 -08001260nAllocationIoSend(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Sams163766c2012-02-15 12:04:24 -08001261{
Andreas Gampe67333922014-11-10 20:35:59 -08001262 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001263 ALOGD("nAllocationIoSend, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
Andreas Gampe67333922014-11-10 20:35:59 -08001264 }
Tim Murray460a0492013-11-19 12:45:54 -08001265 rsAllocationIoSend((RsContext)con, (RsAllocation)alloc);
Jason Sams163766c2012-02-15 12:04:24 -08001266}
1267
1268static void
Tim Murray460a0492013-11-19 12:45:54 -08001269nAllocationIoReceive(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Sams163766c2012-02-15 12:04:24 -08001270{
Andreas Gampe67333922014-11-10 20:35:59 -08001271 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001272 ALOGD("nAllocationIoReceive, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
Andreas Gampe67333922014-11-10 20:35:59 -08001273 }
Tim Murray460a0492013-11-19 12:45:54 -08001274 rsAllocationIoReceive((RsContext)con, (RsAllocation)alloc);
Jason Sams163766c2012-02-15 12:04:24 -08001275}
1276
1277
1278static void
Tim Murray460a0492013-11-19 12:45:54 -08001279nAllocationGenerateMipmaps(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Samsf7086092011-01-12 13:28:37 -08001280{
Andreas Gampe67333922014-11-10 20:35:59 -08001281 if (kLogApi) {
1282 ALOGD("nAllocationGenerateMipmaps, con(%p), a(%p)", (RsContext)con, (RsAllocation)alloc);
1283 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001284 rsAllocationGenerateMipmaps((RsContext)con, (RsAllocation)alloc);
Jason Samsf7086092011-01-12 13:28:37 -08001285}
1286
Tim Murray460a0492013-11-19 12:45:54 -08001287static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001288nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1289 jobject jbitmap, jint usage)
Jason Samsffe9f482009-06-01 17:45:53 -07001290{
John Recked207b92015-04-10 13:52:57 -07001291 SkBitmap bitmap;
1292 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Samsffe9f482009-06-01 17:45:53 -07001293
Jason Sams5476b452010-12-08 16:14:36 -08001294 bitmap.lockPixels();
1295 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001296 jlong id = (jlong)(uintptr_t)rsAllocationCreateFromBitmap((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001297 (RsType)type, (RsAllocationMipmapControl)mip,
1298 ptr, bitmap.getSize(), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001299 bitmap.unlockPixels();
1300 return id;
Jason Samsffe9f482009-06-01 17:45:53 -07001301}
Jason Samsfe08d992009-05-27 14:45:32 -07001302
Tim Murray460a0492013-11-19 12:45:54 -08001303static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001304nAllocationCreateBitmapBackedAllocation(JNIEnv *_env, jobject _this, jlong con, jlong type,
1305 jint mip, jobject jbitmap, jint usage)
Tim Murraya3145512012-12-04 17:59:29 -08001306{
John Recked207b92015-04-10 13:52:57 -07001307 SkBitmap bitmap;
1308 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Tim Murraya3145512012-12-04 17:59:29 -08001309
1310 bitmap.lockPixels();
1311 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001312 jlong id = (jlong)(uintptr_t)rsAllocationCreateTyped((RsContext)con,
Tim Murraya3145512012-12-04 17:59:29 -08001313 (RsType)type, (RsAllocationMipmapControl)mip,
Ashok Bhat98071552014-02-12 09:54:43 +00001314 (uint32_t)usage, (uintptr_t)ptr);
Tim Murraya3145512012-12-04 17:59:29 -08001315 bitmap.unlockPixels();
1316 return id;
1317}
1318
Tim Murray460a0492013-11-19 12:45:54 -08001319static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001320nAllocationCubeCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1321 jobject jbitmap, jint usage)
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001322{
John Recked207b92015-04-10 13:52:57 -07001323 SkBitmap bitmap;
1324 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001325
Jason Sams5476b452010-12-08 16:14:36 -08001326 bitmap.lockPixels();
1327 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001328 jlong id = (jlong)(uintptr_t)rsAllocationCubeCreateFromBitmap((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001329 (RsType)type, (RsAllocationMipmapControl)mip,
1330 ptr, bitmap.getSize(), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001331 bitmap.unlockPixels();
1332 return id;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001333}
1334
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001335static void
Tim Murray460a0492013-11-19 12:45:54 -08001336nAllocationCopyFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001337{
John Recked207b92015-04-10 13:52:57 -07001338 SkBitmap bitmap;
1339 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Samsf7086092011-01-12 13:28:37 -08001340 int w = bitmap.width();
1341 int h = bitmap.height();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001342
Jason Sams4ef66502010-12-10 16:03:15 -08001343 bitmap.lockPixels();
1344 const void* ptr = bitmap.getPixels();
Tim Murrayeff663f2013-11-15 13:08:30 -08001345 rsAllocation2DData((RsContext)con, (RsAllocation)alloc, 0, 0,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001346 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Tim Murray38faea302012-11-27 14:55:08 -08001347 w, h, ptr, bitmap.getSize(), 0);
Jason Sams4ef66502010-12-10 16:03:15 -08001348 bitmap.unlockPixels();
1349}
1350
1351static void
Tim Murray460a0492013-11-19 12:45:54 -08001352nAllocationCopyToBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Jason Sams4ef66502010-12-10 16:03:15 -08001353{
John Recked207b92015-04-10 13:52:57 -07001354 SkBitmap bitmap;
1355 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Sams4ef66502010-12-10 16:03:15 -08001356
1357 bitmap.lockPixels();
1358 void* ptr = bitmap.getPixels();
Tim Murrayeff663f2013-11-15 13:08:30 -08001359 rsAllocationCopyToBitmap((RsContext)con, (RsAllocation)alloc, ptr, bitmap.getSize());
Jason Sams4ef66502010-12-10 16:03:15 -08001360 bitmap.unlockPixels();
Alex Sakhartchouk835b8542011-07-20 14:33:10 -07001361 bitmap.notifyPixelsChanged();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001362}
1363
Stephen Hines414fa2c2014-04-17 01:02:42 -07001364// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Samsd19f10d2009-05-22 14:03:28 -07001365static void
Tim Murray460a0492013-11-19 12:45:54 -08001366nAllocationData1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001367 jint count, jobject data, jint sizeBytes, jint dataType, jint mSize,
1368 jboolean usePadding)
Jason Samsd19f10d2009-05-22 14:03:28 -07001369{
Jason Samse729a942013-11-06 11:22:02 -08001370 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001371 if (kLogApi) {
1372 ALOGD("nAllocation1DData, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1373 "dataType(%i)", (RsContext)con, (RsAllocation)alloc, offset, count, sizeBytes,
1374 dataType);
1375 }
Miao Wang87e908d2015-03-02 15:15:15 -08001376 PER_ARRAY_TYPE(nullptr, rsAllocation1DData, true,
1377 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -07001378}
1379
1380static void
Miao Wangc8e237e2015-02-20 18:36:32 -08001381nAllocationElementData(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1382 jint xoff, jint yoff, jint zoff,
1383 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Jason Sams49bdaf02010-08-31 13:50:42 -07001384{
1385 jint len = _env->GetArrayLength(data);
Andreas Gampe67333922014-11-10 20:35:59 -08001386 if (kLogApi) {
Miao Wangc8e237e2015-02-20 18:36:32 -08001387 ALOGD("nAllocationElementData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1388 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
Andreas Gampe67333922014-11-10 20:35:59 -08001389 sizeBytes);
1390 }
Chris Wailes488230c32014-08-14 11:22:40 -07001391 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001392 if (ptr == nullptr) {
1393 ALOGE("Failed to get Java array elements");
1394 return;
1395 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001396 rsAllocationElementData((RsContext)con, (RsAllocation)alloc,
1397 xoff, yoff, zoff,
1398 lod, ptr, sizeBytes, compIdx);
Jason Sams49bdaf02010-08-31 13:50:42 -07001399 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1400}
1401
Miao Wangc8e237e2015-02-20 18:36:32 -08001402
Stephen Hines414fa2c2014-04-17 01:02:42 -07001403// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Sams49bdaf02010-08-31 13:50:42 -07001404static void
Tim Murray460a0492013-11-19 12:45:54 -08001405nAllocationData2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001406 jint w, jint h, jobject data, jint sizeBytes, jint dataType, jint mSize,
1407 jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001408{
Jason Samse729a942013-11-06 11:22:02 -08001409 RsAllocation *alloc = (RsAllocation *)_alloc;
1410 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001411 if (kLogApi) {
1412 ALOGD("nAllocation2DData, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1413 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1414 }
Miao Wang87e908d2015-03-02 15:15:15 -08001415 int count = w * h;
1416 PER_ARRAY_TYPE(nullptr, rsAllocation2DData, true,
1417 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Samsd19f10d2009-05-22 14:03:28 -07001418}
1419
Stephen Hines414fa2c2014-04-17 01:02:42 -07001420// Copies from the Allocation pointed to by srcAlloc into the Allocation
1421// pointed to by dstAlloc.
Jason Sams40a29e82009-08-10 14:55:26 -07001422static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001423nAllocationData2D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001424 jlong dstAlloc, jint dstXoff, jint dstYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001425 jint dstMip, jint dstFace,
1426 jint width, jint height,
Tim Murray460a0492013-11-19 12:45:54 -08001427 jlong srcAlloc, jint srcXoff, jint srcYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001428 jint srcMip, jint srcFace)
1429{
Andreas Gampe67333922014-11-10 20:35:59 -08001430 if (kLogApi) {
1431 ALOGD("nAllocation2DData_s, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1432 " dstMip(%i), dstFace(%i), width(%i), height(%i),"
1433 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i), srcFace(%i)",
1434 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip, dstFace,
1435 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
1436 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001437
Tim Murrayeff663f2013-11-15 13:08:30 -08001438 rsAllocationCopy2DRange((RsContext)con,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001439 (RsAllocation)dstAlloc,
1440 dstXoff, dstYoff,
1441 dstMip, dstFace,
1442 width, height,
1443 (RsAllocation)srcAlloc,
1444 srcXoff, srcYoff,
1445 srcMip, srcFace);
1446}
1447
Stephen Hines414fa2c2014-04-17 01:02:42 -07001448// Copies from the Java object data into the Allocation pointed to by _alloc.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001449static void
Tim Murray460a0492013-11-19 12:45:54 -08001450nAllocationData3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001451 jint w, jint h, jint d, jobject data, jint sizeBytes, jint dataType,
1452 jint mSize, jboolean usePadding)
Jason Samsb05d6892013-04-09 15:59:24 -07001453{
Jason Samse729a942013-11-06 11:22:02 -08001454 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001455 if (kLogApi) {
1456 ALOGD("nAllocation3DData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1457 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1458 lod, w, h, d, sizeBytes);
1459 }
Miao Wang87e908d2015-03-02 15:15:15 -08001460 int count = w * h * d;
1461 PER_ARRAY_TYPE(nullptr, rsAllocation3DData, true,
1462 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Jason Samsb05d6892013-04-09 15:59:24 -07001463}
1464
Stephen Hines414fa2c2014-04-17 01:02:42 -07001465// Copies from the Allocation pointed to by srcAlloc into the Allocation
1466// pointed to by dstAlloc.
Jason Samsb05d6892013-04-09 15:59:24 -07001467static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001468nAllocationData3D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001469 jlong dstAlloc, jint dstXoff, jint dstYoff, jint dstZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001470 jint dstMip,
1471 jint width, jint height, jint depth,
Tim Murray460a0492013-11-19 12:45:54 -08001472 jlong srcAlloc, jint srcXoff, jint srcYoff, jint srcZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001473 jint srcMip)
1474{
Andreas Gampe67333922014-11-10 20:35:59 -08001475 if (kLogApi) {
1476 ALOGD("nAllocationData3D_alloc, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1477 " dstMip(%i), width(%i), height(%i),"
1478 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i)",
1479 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip,
1480 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip);
1481 }
Jason Samsb05d6892013-04-09 15:59:24 -07001482
Tim Murrayeff663f2013-11-15 13:08:30 -08001483 rsAllocationCopy3DRange((RsContext)con,
Jason Samsb05d6892013-04-09 15:59:24 -07001484 (RsAllocation)dstAlloc,
1485 dstXoff, dstYoff, dstZoff, dstMip,
1486 width, height, depth,
1487 (RsAllocation)srcAlloc,
1488 srcXoff, srcYoff, srcZoff, srcMip);
1489}
1490
Jason Sams21659ac2013-11-06 15:08:07 -08001491
Stephen Hines414fa2c2014-04-17 01:02:42 -07001492// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsb05d6892013-04-09 15:59:24 -07001493static void
Miao Wang87e908d2015-03-02 15:15:15 -08001494nAllocationRead(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jobject data, jint dataType,
1495 jint mSize, jboolean usePadding)
Jason Sams40a29e82009-08-10 14:55:26 -07001496{
Jason Sams21659ac2013-11-06 15:08:07 -08001497 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001498 if (kLogApi) {
1499 ALOGD("nAllocationRead, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
1500 }
Miao Wang87e908d2015-03-02 15:15:15 -08001501 int count = 0;
1502 PER_ARRAY_TYPE(0, rsAllocationRead, false,
1503 (RsContext)con, alloc, ptr, len * typeBytes);
Jason Sams40a29e82009-08-10 14:55:26 -07001504}
1505
Stephen Hines414fa2c2014-04-17 01:02:42 -07001506// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Sams40a29e82009-08-10 14:55:26 -07001507static void
Tim Murray460a0492013-11-19 12:45:54 -08001508nAllocationRead1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001509 jint count, jobject data, jint sizeBytes, jint dataType,
1510 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001511{
Jason Sams21659ac2013-11-06 15:08:07 -08001512 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001513 if (kLogApi) {
1514 ALOGD("nAllocation1DRead, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1515 "dataType(%i)", (RsContext)con, alloc, offset, count, sizeBytes, dataType);
1516 }
Miao Wang87e908d2015-03-02 15:15:15 -08001517 PER_ARRAY_TYPE(0, rsAllocation1DRead, false,
1518 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsfb9f82c2011-01-12 14:53:25 -08001519}
1520
Miao Wangc8e237e2015-02-20 18:36:32 -08001521// Copies from the Element in the Allocation pointed to by _alloc into the Java array data.
1522static void
Miao Wang45cec0a2015-03-04 16:40:21 -08001523nAllocationElementRead(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
Miao Wangc8e237e2015-02-20 18:36:32 -08001524 jint xoff, jint yoff, jint zoff,
Miao Wang45cec0a2015-03-04 16:40:21 -08001525 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Miao Wangc8e237e2015-02-20 18:36:32 -08001526{
Miao Wang45cec0a2015-03-04 16:40:21 -08001527 jint len = _env->GetArrayLength(data);
Miao Wangc8e237e2015-02-20 18:36:32 -08001528 if (kLogApi) {
Miao Wang45cec0a2015-03-04 16:40:21 -08001529 ALOGD("nAllocationElementRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1530 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
1531 sizeBytes);
Miao Wangc8e237e2015-02-20 18:36:32 -08001532 }
Miao Wang45cec0a2015-03-04 16:40:21 -08001533 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001534 if (ptr == nullptr) {
1535 ALOGE("Failed to get Java array elements");
1536 return;
1537 }
Miao Wang45cec0a2015-03-04 16:40:21 -08001538 rsAllocationElementRead((RsContext)con, (RsAllocation)alloc,
1539 xoff, yoff, zoff,
Jason Samsa7e25092015-03-11 11:00:00 -07001540 lod, ptr, sizeBytes, compIdx);
Miao Wangbfa5e652015-05-04 15:29:25 -07001541 _env->ReleaseByteArrayElements(data, ptr, 0);
Miao Wangc8e237e2015-02-20 18:36:32 -08001542}
1543
Stephen Hines414fa2c2014-04-17 01:02:42 -07001544// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsfb9f82c2011-01-12 14:53:25 -08001545static void
Tim Murray460a0492013-11-19 12:45:54 -08001546nAllocationRead2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001547 jint w, jint h, jobject data, jint sizeBytes, jint dataType,
1548 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001549{
Jason Sams21659ac2013-11-06 15:08:07 -08001550 RsAllocation *alloc = (RsAllocation *)_alloc;
1551 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001552 if (kLogApi) {
1553 ALOGD("nAllocation2DRead, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1554 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1555 }
Miao Wang87e908d2015-03-02 15:15:15 -08001556 int count = w * h;
1557 PER_ARRAY_TYPE(0, rsAllocation2DRead, false,
1558 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Sams40a29e82009-08-10 14:55:26 -07001559}
Miao Wang87e908d2015-03-02 15:15:15 -08001560
Miao Wangc8e237e2015-02-20 18:36:32 -08001561// Copies from the Allocation pointed to by _alloc into the Java object data.
1562static void
1563nAllocationRead3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001564 jint w, jint h, jint d, jobject data, int sizeBytes, int dataType,
1565 jint mSize, jboolean usePadding)
Miao Wangc8e237e2015-02-20 18:36:32 -08001566{
1567 RsAllocation *alloc = (RsAllocation *)_alloc;
1568 if (kLogApi) {
1569 ALOGD("nAllocation3DRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1570 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1571 lod, w, h, d, sizeBytes);
1572 }
Miao Wang87e908d2015-03-02 15:15:15 -08001573 int count = w * h * d;
1574 PER_ARRAY_TYPE(nullptr, rsAllocation3DRead, false,
1575 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Miao Wangc8e237e2015-02-20 18:36:32 -08001576}
Jason Samsd19f10d2009-05-22 14:03:28 -07001577
Tim Murray460a0492013-11-19 12:45:54 -08001578static jlong
1579nAllocationGetType(JNIEnv *_env, jobject _this, jlong con, jlong a)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001580{
Andreas Gampe67333922014-11-10 20:35:59 -08001581 if (kLogApi) {
1582 ALOGD("nAllocationGetType, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1583 }
Tim Murray3aa89c12014-08-18 17:51:22 -07001584 return (jlong)(uintptr_t) rsaAllocationGetType((RsContext)con, (RsAllocation)a);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001585}
1586
Jason Sams5edc6082010-10-05 13:32:49 -07001587static void
Tim Murray460a0492013-11-19 12:45:54 -08001588nAllocationResize1D(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint dimX)
Jason Sams5edc6082010-10-05 13:32:49 -07001589{
Andreas Gampe67333922014-11-10 20:35:59 -08001590 if (kLogApi) {
1591 ALOGD("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", (RsContext)con,
1592 (RsAllocation)alloc, dimX);
1593 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001594 rsAllocationResize1D((RsContext)con, (RsAllocation)alloc, dimX);
Jason Sams5edc6082010-10-05 13:32:49 -07001595}
1596
Jason Sams46ba27e32015-02-06 17:45:15 -08001597
1598static jlong
1599nAllocationAdapterCreate(JNIEnv *_env, jobject _this, jlong con, jlong basealloc, jlong type)
1600{
1601 if (kLogApi) {
1602 ALOGD("nAllocationAdapterCreate, con(%p), base(%p), type(%p)",
1603 (RsContext)con, (RsAllocation)basealloc, (RsElement)type);
1604 }
1605 return (jlong)(uintptr_t) rsAllocationAdapterCreate((RsContext)con, (RsType)type,
1606 (RsAllocation)basealloc);
1607
1608}
1609
1610static void
1611nAllocationAdapterOffset(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1612 jint x, jint y, jint z, jint face, jint lod,
1613 jint a1, jint a2, jint a3, jint a4)
1614{
1615 uint32_t params[] = {
1616 (uint32_t)x, (uint32_t)y, (uint32_t)z, (uint32_t)face,
1617 (uint32_t)lod, (uint32_t)a1, (uint32_t)a2, (uint32_t)a3, (uint32_t)a4
1618 };
1619 if (kLogApi) {
1620 ALOGD("nAllocationAdapterOffset, con(%p), alloc(%p), x(%i), y(%i), z(%i), face(%i), lod(%i), arrays(%i %i %i %i)",
1621 (RsContext)con, (RsAllocation)alloc, x, y, z, face, lod, a1, a2, a3, a4);
1622 }
1623 rsAllocationAdapterOffset((RsContext)con, (RsAllocation)alloc,
1624 params, sizeof(params));
1625}
1626
1627
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001628// -----------------------------------
1629
Tim Murray460a0492013-11-19 12:45:54 -08001630static jlong
1631nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con, jlong native_asset)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001632{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001633 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001634 ALOGV("______nFileA3D %p", asset);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001635
Tim Murray3aa89c12014-08-18 17:51:22 -07001636 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromMemory((RsContext)con, asset->getBuffer(false), asset->getLength());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001637 return id;
1638}
1639
Tim Murray460a0492013-11-19 12:45:54 -08001640static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001641nFileA3DCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001642{
1643 AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001644 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001645 return 0;
1646 }
1647
1648 AutoJavaStringToUTF8 str(_env, _path);
1649 Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
Chris Wailes488230c32014-08-14 11:22:40 -07001650 if (asset == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001651 return 0;
1652 }
1653
Tim Murray3aa89c12014-08-18 17:51:22 -07001654 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromAsset((RsContext)con, asset);
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 -08001659nFileA3DCreateFromFile(JNIEnv *_env, jobject _this, jlong con, jstring fileName)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001660{
1661 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001662 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromFile((RsContext)con, fileNameUTF.c_str());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001663
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001664 return id;
1665}
1666
Tim Murray460a0492013-11-19 12:45:54 -08001667static jint
1668nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001669{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001670 int32_t numEntries = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001671 rsaFileA3DGetNumIndexEntries((RsContext)con, &numEntries, (RsFile)fileA3D);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001672 return (jint)numEntries;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001673}
1674
1675static void
Tim Murray460a0492013-11-19 12:45:54 -08001676nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001677{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001678 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001679 RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
1680
Tim Murrayeff663f2013-11-15 13:08:30 -08001681 rsaFileA3DGetIndexEntries((RsContext)con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001682
1683 for(jint i = 0; i < numEntries; i ++) {
1684 _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
1685 _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
1686 }
1687
1688 free(fileEntries);
1689}
1690
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001691static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001692nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint index)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001693{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001694 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Tim Murray3aa89c12014-08-18 17:51:22 -07001695 jlong id = (jlong)(uintptr_t)rsaFileA3DGetEntryByIndex((RsContext)con, (uint32_t)index, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001696 return id;
1697}
Jason Samsd19f10d2009-05-22 14:03:28 -07001698
1699// -----------------------------------
1700
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001701static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001702nFontCreateFromFile(JNIEnv *_env, jobject _this, jlong con,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001703 jstring fileName, jfloat fontSize, jint dpi)
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001704{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001705 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001706 jlong id = (jlong)(uintptr_t)rsFontCreateFromFile((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001707 fileNameUTF.c_str(), fileNameUTF.length(),
1708 fontSize, dpi);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001709
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001710 return id;
1711}
1712
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001713static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001714nFontCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con,
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001715 jstring name, jfloat fontSize, jint dpi, jlong native_asset)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001716{
1717 Asset* asset = reinterpret_cast<Asset*>(native_asset);
1718 AutoJavaStringToUTF8 nameUTF(_env, name);
1719
Tim Murray3aa89c12014-08-18 17:51:22 -07001720 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001721 nameUTF.c_str(), nameUTF.length(),
1722 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001723 asset->getBuffer(false), asset->getLength());
1724 return id;
1725}
1726
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001727static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001728nFontCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001729 jfloat fontSize, jint dpi)
1730{
1731 AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001732 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001733 return 0;
1734 }
1735
1736 AutoJavaStringToUTF8 str(_env, _path);
1737 Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
Chris Wailes488230c32014-08-14 11:22:40 -07001738 if (asset == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001739 return 0;
1740 }
1741
Tim Murray3aa89c12014-08-18 17:51:22 -07001742 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001743 str.c_str(), str.length(),
1744 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001745 asset->getBuffer(false), asset->getLength());
1746 delete asset;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001747 return id;
1748}
1749
Jason Samsbd1c3ad2009-08-03 16:03:08 -07001750// -----------------------------------
1751
1752static void
Tim Murray460a0492013-11-19 12:45:54 -08001753nScriptBindAllocation(JNIEnv *_env, jobject _this, jlong con, jlong script, jlong alloc, jint slot)
Jason Samsd19f10d2009-05-22 14:03:28 -07001754{
Andreas Gampe67333922014-11-10 20:35:59 -08001755 if (kLogApi) {
1756 ALOGD("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", (RsContext)con,
1757 (RsScript)script, (RsAllocation)alloc, slot);
1758 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001759 rsScriptBindAllocation((RsContext)con, (RsScript)script, (RsAllocation)alloc, slot);
Jason Samsd19f10d2009-05-22 14:03:28 -07001760}
1761
1762static void
Tim Murray460a0492013-11-19 12:45:54 -08001763nScriptSetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jint val)
Jason Sams4d339932010-05-11 14:03:58 -07001764{
Andreas Gampe67333922014-11-10 20:35:59 -08001765 if (kLogApi) {
1766 ALOGD("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", (RsContext)con, (void *)script,
1767 slot, val);
1768 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001769 rsScriptSetVarI((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001770}
1771
Tim Murray7c4caad2013-04-10 16:21:40 -07001772static jint
Tim Murray460a0492013-11-19 12:45:54 -08001773nScriptGetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001774{
Andreas Gampe67333922014-11-10 20:35:59 -08001775 if (kLogApi) {
1776 ALOGD("nScriptGetVarI, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1777 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001778 int value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001779 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001780 return value;
1781}
1782
Jason Sams4d339932010-05-11 14:03:58 -07001783static void
Tim Murray460a0492013-11-19 12:45:54 -08001784nScriptSetVarObj(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001785{
Andreas Gampe67333922014-11-10 20:35:59 -08001786 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001787 ALOGD("nScriptSetVarObj, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001788 slot, val);
1789 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001790 rsScriptSetVarObj((RsContext)con, (RsScript)script, slot, (RsObjectBase)val);
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001791}
1792
1793static void
Tim Murray460a0492013-11-19 12:45:54 -08001794nScriptSetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Stephen Hines031ec58c2010-10-11 10:54:21 -07001795{
Andreas Gampe67333922014-11-10 20:35:59 -08001796 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001797 ALOGD("nScriptSetVarJ, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001798 slot, val);
1799 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001800 rsScriptSetVarJ((RsContext)con, (RsScript)script, slot, val);
Stephen Hines031ec58c2010-10-11 10:54:21 -07001801}
1802
Tim Murray7c4caad2013-04-10 16:21:40 -07001803static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001804nScriptGetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001805{
Andreas Gampe67333922014-11-10 20:35:59 -08001806 if (kLogApi) {
1807 ALOGD("nScriptGetVarJ, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1808 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001809 jlong value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001810 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001811 return value;
1812}
1813
Stephen Hines031ec58c2010-10-11 10:54:21 -07001814static void
Tim Murray460a0492013-11-19 12:45:54 -08001815nScriptSetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, float val)
Jason Sams4d339932010-05-11 14:03:58 -07001816{
Andreas Gampe67333922014-11-10 20:35:59 -08001817 if (kLogApi) {
1818 ALOGD("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", (RsContext)con, (void *)script,
1819 slot, val);
1820 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001821 rsScriptSetVarF((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001822}
1823
Tim Murray7c4caad2013-04-10 16:21:40 -07001824static jfloat
Tim Murray460a0492013-11-19 12:45:54 -08001825nScriptGetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001826{
Andreas Gampe67333922014-11-10 20:35:59 -08001827 if (kLogApi) {
1828 ALOGD("nScriptGetVarF, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1829 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001830 jfloat value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001831 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001832 return value;
1833}
1834
Jason Sams4d339932010-05-11 14:03:58 -07001835static void
Tim Murray460a0492013-11-19 12:45:54 -08001836nScriptSetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, double val)
Stephen Hinesca54ec32010-09-20 17:20:30 -07001837{
Andreas Gampe67333922014-11-10 20:35:59 -08001838 if (kLogApi) {
1839 ALOGD("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", (RsContext)con, (void *)script,
1840 slot, val);
1841 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001842 rsScriptSetVarD((RsContext)con, (RsScript)script, slot, val);
Stephen Hinesca54ec32010-09-20 17:20:30 -07001843}
1844
Tim Murray7c4caad2013-04-10 16:21:40 -07001845static jdouble
Tim Murray460a0492013-11-19 12:45:54 -08001846nScriptGetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001847{
Andreas Gampe67333922014-11-10 20:35:59 -08001848 if (kLogApi) {
1849 ALOGD("nScriptGetVarD, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1850 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001851 jdouble value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001852 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001853 return value;
1854}
1855
Stephen Hinesca54ec32010-09-20 17:20:30 -07001856static void
Tim Murray460a0492013-11-19 12:45:54 -08001857nScriptSetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001858{
Andreas Gampe67333922014-11-10 20:35:59 -08001859 if (kLogApi) {
1860 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1861 }
Jason Sams4d339932010-05-11 14:03:58 -07001862 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001863 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001864 if (ptr == nullptr) {
1865 ALOGE("Failed to get Java array elements");
1866 return;
1867 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001868 rsScriptSetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001869 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1870}
1871
Stephen Hinesadeb8092012-04-20 14:26:06 -07001872static void
Tim Murray460a0492013-11-19 12:45:54 -08001873nScriptGetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Tim Murray7c4caad2013-04-10 16:21:40 -07001874{
Andreas Gampe67333922014-11-10 20:35:59 -08001875 if (kLogApi) {
1876 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1877 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001878 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001879 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001880 if (ptr == nullptr) {
1881 ALOGE("Failed to get Java array elements");
1882 return;
1883 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001884 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Stephen Hines414fa2c2014-04-17 01:02:42 -07001885 _env->ReleaseByteArrayElements(data, ptr, 0);
Tim Murray7c4caad2013-04-10 16:21:40 -07001886}
1887
1888static void
Andreas Gampe67333922014-11-10 20:35:59 -08001889nScriptSetVarVE(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data,
1890 jlong elem, jintArray dims)
Stephen Hinesadeb8092012-04-20 14:26:06 -07001891{
Andreas Gampe67333922014-11-10 20:35:59 -08001892 if (kLogApi) {
1893 ALOGD("nScriptSetVarVE, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1894 }
Stephen Hinesadeb8092012-04-20 14:26:06 -07001895 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001896 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001897 if (ptr == nullptr) {
1898 ALOGE("Failed to get Java array elements");
1899 return;
1900 }
Stephen Hinesadeb8092012-04-20 14:26:06 -07001901 jint dimsLen = _env->GetArrayLength(dims) * sizeof(int);
Chris Wailes488230c32014-08-14 11:22:40 -07001902 jint *dimsPtr = _env->GetIntArrayElements(dims, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001903 if (dimsPtr == nullptr) {
1904 ALOGE("Failed to get Java array elements");
1905 return;
1906 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001907 rsScriptSetVarVE((RsContext)con, (RsScript)script, slot, ptr, len, (RsElement)elem,
Stephen Hinesbc5d3ee2014-06-25 00:03:39 -07001908 (const uint32_t*) dimsPtr, dimsLen);
Stephen Hinesadeb8092012-04-20 14:26:06 -07001909 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1910 _env->ReleaseIntArrayElements(dims, dimsPtr, JNI_ABORT);
1911}
1912
Jason Samsd19f10d2009-05-22 14:03:28 -07001913
1914static void
Tim Murray460a0492013-11-19 12:45:54 -08001915nScriptSetTimeZone(JNIEnv *_env, jobject _this, jlong con, jlong script, jbyteArray timeZone)
Jason Samsd19f10d2009-05-22 14:03:28 -07001916{
Andreas Gampe67333922014-11-10 20:35:59 -08001917 if (kLogApi) {
1918 ALOGD("nScriptCSetTimeZone, con(%p), s(%p)", (RsContext)con, (void *)script);
1919 }
Romain Guy584a3752009-07-30 18:45:01 -07001920
1921 jint length = _env->GetArrayLength(timeZone);
1922 jbyte* timeZone_ptr;
1923 timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
Miao Wangba8766c2015-10-12 17:24:13 -07001924 if (timeZone_ptr == nullptr) {
1925 ALOGE("Failed to get Java array elements");
1926 return;
1927 }
Romain Guy584a3752009-07-30 18:45:01 -07001928
Tim Murrayeff663f2013-11-15 13:08:30 -08001929 rsScriptSetTimeZone((RsContext)con, (RsScript)script, (const char *)timeZone_ptr, length);
Romain Guy584a3752009-07-30 18:45:01 -07001930
1931 if (timeZone_ptr) {
1932 _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
1933 }
1934}
1935
Jason Samsfbf0b9e2009-08-13 12:59:04 -07001936static void
Tim Murray460a0492013-11-19 12:45:54 -08001937nScriptInvoke(JNIEnv *_env, jobject _this, jlong con, jlong obj, jint slot)
Jason Samsbe2e8412009-09-16 15:04:38 -07001938{
Andreas Gampe67333922014-11-10 20:35:59 -08001939 if (kLogApi) {
1940 ALOGD("nScriptInvoke, con(%p), script(%p)", (RsContext)con, (void *)obj);
1941 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001942 rsScriptInvoke((RsContext)con, (RsScript)obj, slot);
Jason Samsbe2e8412009-09-16 15:04:38 -07001943}
1944
1945static void
Tim Murray460a0492013-11-19 12:45:54 -08001946nScriptInvokeV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001947{
Andreas Gampe67333922014-11-10 20:35:59 -08001948 if (kLogApi) {
1949 ALOGD("nScriptInvokeV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1950 }
Jason Sams4d339932010-05-11 14:03:58 -07001951 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001952 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001953 if (ptr == nullptr) {
1954 ALOGE("Failed to get Java array elements");
1955 return;
1956 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001957 rsScriptInvokeV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001958 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1959}
1960
Jason Sams6e494d32011-04-27 16:33:11 -07001961static void
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001962nScriptForEach(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
1963 jlongArray ains, jlong aout, jbyteArray params,
1964 jintArray limits)
Jason Sams6e494d32011-04-27 16:33:11 -07001965{
Andreas Gampe67333922014-11-10 20:35:59 -08001966 if (kLogApi) {
Chih-Hung Hsieh9eb9dd32015-05-06 14:42:04 -07001967 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 -08001968 }
Jason Sams6e494d32011-04-27 16:33:11 -07001969
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001970 jint in_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07001971 jlong *in_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07001972
Chris Wailes488230c32014-08-14 11:22:40 -07001973 RsAllocation *in_allocs = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07001974
Chris Wailes488230c32014-08-14 11:22:40 -07001975 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001976 in_len = _env->GetArrayLength(ains);
Yang Ni7b2a46f2015-05-05 12:41:19 -07001977 if (in_len > (jint)RS_KERNEL_MAX_ARGUMENTS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -07001978 ALOGE("Too many arguments in kernel launch.");
1979 // TODO (b/20758983): Report back to Java and throw an exception
1980 return;
1981 }
Chris Wailes94961062014-06-11 12:01:28 -07001982
Yang Ni17c2d7a2015-04-30 16:13:54 -07001983 in_ptr = _env->GetLongArrayElements(ains, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001984 if (in_ptr == nullptr) {
1985 ALOGE("Failed to get Java array elements");
1986 return;
1987 }
1988
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001989 if (sizeof(RsAllocation) == sizeof(jlong)) {
1990 in_allocs = (RsAllocation*)in_ptr;
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001991 } else {
1992 // Convert from 64-bit jlong types to the native pointer type.
Chris Wailes94961062014-06-11 12:01:28 -07001993
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001994 in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
Yang Ni17c2d7a2015-04-30 16:13:54 -07001995 if (in_allocs == nullptr) {
1996 ALOGE("Failed launching kernel for lack of memory.");
1997 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
1998 return;
1999 }
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002000
2001 for (int index = in_len; --index >= 0;) {
2002 in_allocs[index] = (RsAllocation)in_ptr[index];
2003 }
2004 }
Chris Wailes94961062014-06-11 12:01:28 -07002005 }
2006
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002007 jint param_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002008 jbyte *param_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002009
Chris Wailes488230c32014-08-14 11:22:40 -07002010 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002011 param_len = _env->GetArrayLength(params);
Chris Wailes488230c32014-08-14 11:22:40 -07002012 param_ptr = _env->GetByteArrayElements(params, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002013 if (param_ptr == nullptr) {
2014 ALOGE("Failed to get Java array elements");
2015 return;
2016 }
Chris Wailes94961062014-06-11 12:01:28 -07002017 }
2018
Chris Wailes488230c32014-08-14 11:22:40 -07002019 RsScriptCall sc, *sca = nullptr;
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002020 uint32_t sc_size = 0;
Chris Wailes94961062014-06-11 12:01:28 -07002021
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002022 jint limit_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002023 jint *limit_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002024
Chris Wailes488230c32014-08-14 11:22:40 -07002025 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002026 limit_len = _env->GetArrayLength(limits);
Chris Wailes488230c32014-08-14 11:22:40 -07002027 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002028 if (limit_ptr == nullptr) {
2029 ALOGE("Failed to get Java array elements");
2030 return;
2031 }
Chris Wailes94961062014-06-11 12:01:28 -07002032
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002033 assert(limit_len == 6);
Andreas Gampe67333922014-11-10 20:35:59 -08002034 UNUSED(limit_len); // As the assert might not be compiled.
Chris Wailes94961062014-06-11 12:01:28 -07002035
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002036 sc.xStart = limit_ptr[0];
2037 sc.xEnd = limit_ptr[1];
2038 sc.yStart = limit_ptr[2];
2039 sc.yEnd = limit_ptr[3];
2040 sc.zStart = limit_ptr[4];
2041 sc.zEnd = limit_ptr[5];
2042 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
Jason Sams14331ab2015-01-26 18:14:36 -08002043 sc.arrayStart = 0;
2044 sc.arrayEnd = 0;
2045 sc.array2Start = 0;
2046 sc.array2End = 0;
2047 sc.array3Start = 0;
2048 sc.array3End = 0;
2049 sc.array4Start = 0;
2050 sc.array4End = 0;
Chris Wailes94961062014-06-11 12:01:28 -07002051
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002052 sca = &sc;
Chris Wailes94961062014-06-11 12:01:28 -07002053 }
2054
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002055 rsScriptForEachMulti((RsContext)con, (RsScript)script, slot,
2056 in_allocs, in_len, (RsAllocation)aout,
2057 param_ptr, param_len, sca, sc_size);
Chris Wailes94961062014-06-11 12:01:28 -07002058
Chris Wailes488230c32014-08-14 11:22:40 -07002059 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002060 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
Chris Wailes94961062014-06-11 12:01:28 -07002061 }
2062
Chris Wailes488230c32014-08-14 11:22:40 -07002063 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002064 _env->ReleaseByteArrayElements(params, param_ptr, JNI_ABORT);
2065 }
2066
Chris Wailes488230c32014-08-14 11:22:40 -07002067 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002068 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
2069 }
Chris Wailes94961062014-06-11 12:01:28 -07002070}
2071
Matt Wala36eb1f72015-07-20 15:35:27 -07002072static void
2073nScriptReduce(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
2074 jlong ain, jlong aout, jintArray limits)
2075{
2076 if (kLogApi) {
2077 ALOGD("nScriptReduce, con(%p), s(%p), slot(%i) ain(%" PRId64 ") aout(%" PRId64 ")", (RsContext)con, (void *)script, slot, ain, aout);
2078 }
2079
2080 RsScriptCall sc, *sca = nullptr;
2081 uint32_t sc_size = 0;
2082
2083 jint limit_len = 0;
2084 jint *limit_ptr = nullptr;
2085
2086 // If the caller passed limits, reflect them in the RsScriptCall.
2087 if (limits != nullptr) {
2088 limit_len = _env->GetArrayLength(limits);
2089 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002090 if (limit_ptr == nullptr) {
2091 ALOGE("Failed to get Java array elements");
2092 return;
2093 }
Matt Wala36eb1f72015-07-20 15:35:27 -07002094
2095 // We expect to be passed an array [x1, x2] which specifies
2096 // the sub-range for a 1-dimensional reduction.
2097 assert(limit_len == 2);
2098 UNUSED(limit_len); // As the assert might not be compiled.
2099
2100 sc.xStart = limit_ptr[0];
2101 sc.xEnd = limit_ptr[1];
2102 sc.yStart = 0;
2103 sc.yEnd = 0;
2104 sc.zStart = 0;
2105 sc.zEnd = 0;
2106 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
2107 sc.arrayStart = 0;
2108 sc.arrayEnd = 0;
2109 sc.array2Start = 0;
2110 sc.array2End = 0;
2111 sc.array3Start = 0;
2112 sc.array3End = 0;
2113 sc.array4Start = 0;
2114 sc.array4End = 0;
2115
2116 sca = &sc;
2117 sc_size = sizeof(sc);
2118 }
2119
2120 rsScriptReduce((RsContext)con, (RsScript)script, slot,
2121 (RsAllocation)ain, (RsAllocation)aout,
2122 sca, sc_size);
2123
2124 if (limits != nullptr) {
2125 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
2126 }
2127}
2128
David Gross26ef7a732016-01-12 12:19:15 -08002129static void
2130nScriptReduceNew(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
2131 jlongArray ains, jlong aout, jintArray limits)
2132{
2133 if (kLogApi) {
2134 ALOGD("nScriptReduceNew, con(%p), s(%p), slot(%i) ains(%p) aout(%" PRId64 ")", (RsContext)con, (void *)script, slot, ains, aout);
2135 }
2136
2137 if (ains == nullptr) {
2138 ALOGE("At least one input required.");
2139 // TODO (b/20758983): Report back to Java and throw an exception
2140 return;
2141 }
2142 jint in_len = _env->GetArrayLength(ains);
2143 if (in_len > (jint)RS_KERNEL_MAX_ARGUMENTS) {
2144 ALOGE("Too many arguments in kernel launch.");
2145 // TODO (b/20758983): Report back to Java and throw an exception
2146 return;
2147 }
2148
2149 jlong *in_ptr = _env->GetLongArrayElements(ains, nullptr);
2150 if (in_ptr == nullptr) {
2151 ALOGE("Failed to get Java array elements");
2152 // TODO (b/20758983): Report back to Java and throw an exception
2153 return;
2154 }
2155
2156 RsAllocation *in_allocs = nullptr;
2157 if (sizeof(RsAllocation) == sizeof(jlong)) {
2158 in_allocs = (RsAllocation*)in_ptr;
2159 } else {
2160 // Convert from 64-bit jlong types to the native pointer type.
2161
2162 in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
2163 if (in_allocs == nullptr) {
2164 ALOGE("Failed launching kernel for lack of memory.");
2165 // TODO (b/20758983): Report back to Java and throw an exception
2166 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2167 return;
2168 }
2169
2170 for (int index = in_len; --index >= 0;) {
2171 in_allocs[index] = (RsAllocation)in_ptr[index];
2172 }
2173 }
2174
2175 RsScriptCall sc, *sca = nullptr;
2176 uint32_t sc_size = 0;
2177
2178 jint limit_len = 0;
2179 jint *limit_ptr = nullptr;
2180
2181 if (limits != nullptr) {
2182 limit_len = _env->GetArrayLength(limits);
2183 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
2184 if (limit_ptr == nullptr) {
2185 ALOGE("Failed to get Java array elements");
2186 // TODO (b/20758983): Report back to Java and throw an exception
2187 return;
2188 }
2189
2190 assert(limit_len == 6);
2191 UNUSED(limit_len); // As the assert might not be compiled.
2192
2193 sc.xStart = limit_ptr[0];
2194 sc.xEnd = limit_ptr[1];
2195 sc.yStart = limit_ptr[2];
2196 sc.yEnd = limit_ptr[3];
2197 sc.zStart = limit_ptr[4];
2198 sc.zEnd = limit_ptr[5];
2199 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
2200 sc.arrayStart = 0;
2201 sc.arrayEnd = 0;
2202 sc.array2Start = 0;
2203 sc.array2End = 0;
2204 sc.array3Start = 0;
2205 sc.array3End = 0;
2206 sc.array4Start = 0;
2207 sc.array4End = 0;
2208
2209 sca = &sc;
2210 sc_size = sizeof(sc);
2211 }
2212
2213 rsScriptReduceNew((RsContext)con, (RsScript)script, slot,
2214 in_allocs, in_len, (RsAllocation)aout,
2215 sca, sc_size);
2216
2217 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
2218
2219 if (limits != nullptr) {
2220 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
2221 }
2222}
2223
Jason Sams22534172009-08-04 16:58:20 -07002224// -----------------------------------
2225
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002226static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002227nScriptCCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Samse4a06c52011-03-16 16:29:28 -07002228 jstring resName, jstring cacheDir,
2229 jbyteArray scriptRef, jint length)
Jason Sams22534172009-08-04 16:58:20 -07002230{
Andreas Gampe67333922014-11-10 20:35:59 -08002231 if (kLogApi) {
2232 ALOGD("nScriptCCreate, con(%p)", (RsContext)con);
2233 }
Jason Sams22534172009-08-04 16:58:20 -07002234
Jason Samse4a06c52011-03-16 16:29:28 -07002235 AutoJavaStringToUTF8 resNameUTF(_env, resName);
2236 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002237 jlong ret = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002238 jbyte* script_ptr = nullptr;
Jack Palevich43702d82009-05-28 13:38:16 -07002239 jint _exception = 0;
2240 jint remaining;
Jack Palevich43702d82009-05-28 13:38:16 -07002241 if (!scriptRef) {
2242 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002243 //jniThrowException(_env, "java/lang/IllegalArgumentException", "script == null");
Jack Palevich43702d82009-05-28 13:38:16 -07002244 goto exit;
2245 }
Jack Palevich43702d82009-05-28 13:38:16 -07002246 if (length < 0) {
2247 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002248 //jniThrowException(_env, "java/lang/IllegalArgumentException", "length < 0");
Jack Palevich43702d82009-05-28 13:38:16 -07002249 goto exit;
2250 }
Jason Samse4a06c52011-03-16 16:29:28 -07002251 remaining = _env->GetArrayLength(scriptRef);
Jack Palevich43702d82009-05-28 13:38:16 -07002252 if (remaining < length) {
2253 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002254 //jniThrowException(_env, "java/lang/IllegalArgumentException",
2255 // "length > script.length - offset");
Jack Palevich43702d82009-05-28 13:38:16 -07002256 goto exit;
2257 }
Jason Samse4a06c52011-03-16 16:29:28 -07002258 script_ptr = (jbyte *)
Jack Palevich43702d82009-05-28 13:38:16 -07002259 _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
Miao Wangba8766c2015-10-12 17:24:13 -07002260 if (script_ptr == nullptr) {
2261 ALOGE("Failed to get Java array elements");
2262 return ret;
2263 }
Jack Palevich43702d82009-05-28 13:38:16 -07002264
Tim Murrayeff663f2013-11-15 13:08:30 -08002265 //rsScriptCSetText((RsContext)con, (const char *)script_ptr, length);
Jason Samse4a06c52011-03-16 16:29:28 -07002266
Tim Murray3aa89c12014-08-18 17:51:22 -07002267 ret = (jlong)(uintptr_t)rsScriptCCreate((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07002268 resNameUTF.c_str(), resNameUTF.length(),
2269 cacheDirUTF.c_str(), cacheDirUTF.length(),
Jason Samse4a06c52011-03-16 16:29:28 -07002270 (const char *)script_ptr, length);
Jason Sams39ddc9502009-06-05 17:35:09 -07002271
Jack Palevich43702d82009-05-28 13:38:16 -07002272exit:
Jason Samse4a06c52011-03-16 16:29:28 -07002273 if (script_ptr) {
2274 _env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr,
Jack Palevich43702d82009-05-28 13:38:16 -07002275 _exception ? JNI_ABORT: 0);
2276 }
Jason Samsd19f10d2009-05-22 14:03:28 -07002277
Tim Murray3aa89c12014-08-18 17:51:22 -07002278 return (jlong)(uintptr_t)ret;
Jason Samsd19f10d2009-05-22 14:03:28 -07002279}
2280
Tim Murray460a0492013-11-19 12:45:54 -08002281static jlong
2282nScriptIntrinsicCreate(JNIEnv *_env, jobject _this, jlong con, jint id, jlong eid)
Jason Sams6ab97682012-08-10 12:09:43 -07002283{
Andreas Gampe67333922014-11-10 20:35:59 -08002284 if (kLogApi) {
2285 ALOGD("nScriptIntrinsicCreate, con(%p) id(%i) element(%p)", (RsContext)con, id,
2286 (void *)eid);
2287 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002288 return (jlong)(uintptr_t)rsScriptIntrinsicCreate((RsContext)con, id, (RsElement)eid);
Jason Sams6ab97682012-08-10 12:09:43 -07002289}
2290
Tim Murray460a0492013-11-19 12:45:54 -08002291static jlong
2292nScriptKernelIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot, jint sig)
Jason Sams08a81582012-09-18 12:32:10 -07002293{
Andreas Gampe67333922014-11-10 20:35:59 -08002294 if (kLogApi) {
2295 ALOGD("nScriptKernelIDCreate, con(%p) script(%p), slot(%i), sig(%i)", (RsContext)con,
2296 (void *)sid, slot, sig);
2297 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002298 return (jlong)(uintptr_t)rsScriptKernelIDCreate((RsContext)con, (RsScript)sid, slot, sig);
Jason Sams08a81582012-09-18 12:32:10 -07002299}
2300
Tim Murray460a0492013-11-19 12:45:54 -08002301static jlong
Yang Nibe392ad2015-01-23 17:16:02 -08002302nScriptInvokeIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
2303{
2304 if (kLogApi) {
Elliott Hughes7ff53fa2015-02-05 21:36:10 -08002305 ALOGD("nScriptInvokeIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con,
Yang Nibe392ad2015-01-23 17:16:02 -08002306 (void *)sid, slot);
2307 }
2308 return (jlong)(uintptr_t)rsScriptInvokeIDCreate((RsContext)con, (RsScript)sid, slot);
2309}
2310
2311static jlong
Tim Murray460a0492013-11-19 12:45:54 -08002312nScriptFieldIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
Jason Sams08a81582012-09-18 12:32:10 -07002313{
Andreas Gampe67333922014-11-10 20:35:59 -08002314 if (kLogApi) {
2315 ALOGD("nScriptFieldIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con, (void *)sid,
2316 slot);
2317 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002318 return (jlong)(uintptr_t)rsScriptFieldIDCreate((RsContext)con, (RsScript)sid, slot);
Jason Sams08a81582012-09-18 12:32:10 -07002319}
2320
Tim Murray460a0492013-11-19 12:45:54 -08002321static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002322nScriptGroupCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _kernels, jlongArray _src,
2323 jlongArray _dstk, jlongArray _dstf, jlongArray _types)
Jason Sams08a81582012-09-18 12:32:10 -07002324{
Andreas Gampe67333922014-11-10 20:35:59 -08002325 if (kLogApi) {
2326 ALOGD("nScriptGroupCreate, con(%p)", (RsContext)con);
2327 }
Jason Sams08a81582012-09-18 12:32:10 -07002328
Ashok Bhat98071552014-02-12 09:54:43 +00002329 jint kernelsLen = _env->GetArrayLength(_kernels);
Chris Wailes488230c32014-08-14 11:22:40 -07002330 jlong *jKernelsPtr = _env->GetLongArrayElements(_kernels, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002331 if (jKernelsPtr == nullptr) {
2332 ALOGE("Failed to get Java array elements: kernels");
2333 return 0;
2334 }
Ashok Bhat98071552014-02-12 09:54:43 +00002335 RsScriptKernelID* kernelsPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * kernelsLen);
2336 for(int i = 0; i < kernelsLen; ++i) {
2337 kernelsPtr[i] = (RsScriptKernelID)jKernelsPtr[i];
2338 }
Jason Sams08a81582012-09-18 12:32:10 -07002339
Ashok Bhat98071552014-02-12 09:54:43 +00002340 jint srcLen = _env->GetArrayLength(_src);
Chris Wailes488230c32014-08-14 11:22:40 -07002341 jlong *jSrcPtr = _env->GetLongArrayElements(_src, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002342 if (jSrcPtr == nullptr) {
2343 ALOGE("Failed to get Java array elements: src");
2344 return 0;
2345 }
Ashok Bhat98071552014-02-12 09:54:43 +00002346 RsScriptKernelID* srcPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * srcLen);
2347 for(int i = 0; i < srcLen; ++i) {
2348 srcPtr[i] = (RsScriptKernelID)jSrcPtr[i];
2349 }
Jason Sams08a81582012-09-18 12:32:10 -07002350
Ashok Bhat98071552014-02-12 09:54:43 +00002351 jint dstkLen = _env->GetArrayLength(_dstk);
Chris Wailes488230c32014-08-14 11:22:40 -07002352 jlong *jDstkPtr = _env->GetLongArrayElements(_dstk, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002353 if (jDstkPtr == nullptr) {
2354 ALOGE("Failed to get Java array elements: dstk");
2355 return 0;
2356 }
Ashok Bhat98071552014-02-12 09:54:43 +00002357 RsScriptKernelID* dstkPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstkLen);
2358 for(int i = 0; i < dstkLen; ++i) {
2359 dstkPtr[i] = (RsScriptKernelID)jDstkPtr[i];
2360 }
2361
2362 jint dstfLen = _env->GetArrayLength(_dstf);
Chris Wailes488230c32014-08-14 11:22:40 -07002363 jlong *jDstfPtr = _env->GetLongArrayElements(_dstf, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002364 if (jDstfPtr == nullptr) {
2365 ALOGE("Failed to get Java array elements: dstf");
2366 return 0;
2367 }
Ashok Bhat98071552014-02-12 09:54:43 +00002368 RsScriptKernelID* dstfPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstfLen);
2369 for(int i = 0; i < dstfLen; ++i) {
2370 dstfPtr[i] = (RsScriptKernelID)jDstfPtr[i];
2371 }
2372
2373 jint typesLen = _env->GetArrayLength(_types);
Chris Wailes488230c32014-08-14 11:22:40 -07002374 jlong *jTypesPtr = _env->GetLongArrayElements(_types, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002375 if (jTypesPtr == nullptr) {
2376 ALOGE("Failed to get Java array elements: types");
2377 return 0;
2378 }
Ashok Bhat98071552014-02-12 09:54:43 +00002379 RsType* typesPtr = (RsType*) malloc(sizeof(RsType) * typesLen);
2380 for(int i = 0; i < typesLen; ++i) {
2381 typesPtr[i] = (RsType)jTypesPtr[i];
2382 }
2383
Tim Murray3aa89c12014-08-18 17:51:22 -07002384 jlong id = (jlong)(uintptr_t)rsScriptGroupCreate((RsContext)con,
Ashok Bhat98071552014-02-12 09:54:43 +00002385 (RsScriptKernelID *)kernelsPtr, kernelsLen * sizeof(RsScriptKernelID),
2386 (RsScriptKernelID *)srcPtr, srcLen * sizeof(RsScriptKernelID),
2387 (RsScriptKernelID *)dstkPtr, dstkLen * sizeof(RsScriptKernelID),
2388 (RsScriptFieldID *)dstfPtr, dstfLen * sizeof(RsScriptKernelID),
2389 (RsType *)typesPtr, typesLen * sizeof(RsType));
2390
2391 free(kernelsPtr);
2392 free(srcPtr);
2393 free(dstkPtr);
2394 free(dstfPtr);
2395 free(typesPtr);
2396 _env->ReleaseLongArrayElements(_kernels, jKernelsPtr, 0);
2397 _env->ReleaseLongArrayElements(_src, jSrcPtr, 0);
2398 _env->ReleaseLongArrayElements(_dstk, jDstkPtr, 0);
2399 _env->ReleaseLongArrayElements(_dstf, jDstfPtr, 0);
2400 _env->ReleaseLongArrayElements(_types, jTypesPtr, 0);
Jason Sams08a81582012-09-18 12:32:10 -07002401 return id;
2402}
2403
2404static void
Tim Murray460a0492013-11-19 12:45:54 -08002405nScriptGroupSetInput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002406{
Andreas Gampe67333922014-11-10 20:35:59 -08002407 if (kLogApi) {
2408 ALOGD("nScriptGroupSetInput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2409 (void *)gid, (void *)kid, (void *)alloc);
2410 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002411 rsScriptGroupSetInput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002412}
2413
2414static void
Tim Murray460a0492013-11-19 12:45:54 -08002415nScriptGroupSetOutput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002416{
Andreas Gampe67333922014-11-10 20:35:59 -08002417 if (kLogApi) {
2418 ALOGD("nScriptGroupSetOutput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2419 (void *)gid, (void *)kid, (void *)alloc);
2420 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002421 rsScriptGroupSetOutput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002422}
2423
2424static void
Tim Murray460a0492013-11-19 12:45:54 -08002425nScriptGroupExecute(JNIEnv *_env, jobject _this, jlong con, jlong gid)
Jason Sams08a81582012-09-18 12:32:10 -07002426{
Andreas Gampe67333922014-11-10 20:35:59 -08002427 if (kLogApi) {
2428 ALOGD("nScriptGroupSetOutput, con(%p) group(%p)", (RsContext)con, (void *)gid);
2429 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002430 rsScriptGroupExecute((RsContext)con, (RsScriptGroup)gid);
Jason Sams08a81582012-09-18 12:32:10 -07002431}
2432
Jason Samsd19f10d2009-05-22 14:03:28 -07002433// ---------------------------------------------------------------------------
2434
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002435static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002436nProgramStoreCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Sams331bf9b2011-04-06 11:23:54 -07002437 jboolean colorMaskR, jboolean colorMaskG, jboolean colorMaskB, jboolean colorMaskA,
2438 jboolean depthMask, jboolean ditherEnable,
2439 jint srcFunc, jint destFunc,
2440 jint depthFunc)
Jason Samsd19f10d2009-05-22 14:03:28 -07002441{
Andreas Gampe67333922014-11-10 20:35:59 -08002442 if (kLogApi) {
2443 ALOGD("nProgramStoreCreate, con(%p)", (RsContext)con);
2444 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002445 return (jlong)(uintptr_t)rsProgramStoreCreate((RsContext)con, colorMaskR, colorMaskG, colorMaskB, colorMaskA,
Jason Sams331bf9b2011-04-06 11:23:54 -07002446 depthMask, ditherEnable, (RsBlendSrcFunc)srcFunc,
2447 (RsBlendDstFunc)destFunc, (RsDepthFunc)depthFunc);
Jason Samsd19f10d2009-05-22 14:03:28 -07002448}
2449
Jason Sams0011bcf2009-12-15 12:58:36 -08002450// ---------------------------------------------------------------------------
2451
2452static void
Tim Murray460a0492013-11-19 12:45:54 -08002453nProgramBindConstants(JNIEnv *_env, jobject _this, jlong con, jlong vpv, jint slot, jlong a)
Jason Sams0011bcf2009-12-15 12:58:36 -08002454{
Andreas Gampe67333922014-11-10 20:35:59 -08002455 if (kLogApi) {
2456 ALOGD("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", (RsContext)con,
2457 (RsProgramVertex)vpv, slot, (RsAllocation)a);
2458 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002459 rsProgramBindConstants((RsContext)con, (RsProgram)vpv, slot, (RsAllocation)a);
Jason Sams0011bcf2009-12-15 12:58:36 -08002460}
Jason Sams54c0ec12009-11-30 14:49:55 -08002461
Jason Sams68afd012009-12-17 16:55:08 -08002462static void
Tim Murray460a0492013-11-19 12:45:54 -08002463nProgramBindTexture(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002464{
Andreas Gampe67333922014-11-10 20:35:59 -08002465 if (kLogApi) {
2466 ALOGD("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2467 (RsProgramFragment)vpf, slot, (RsAllocation)a);
2468 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002469 rsProgramBindTexture((RsContext)con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
Jason Sams68afd012009-12-17 16:55:08 -08002470}
2471
2472static void
Tim Murray460a0492013-11-19 12:45:54 -08002473nProgramBindSampler(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002474{
Andreas Gampe67333922014-11-10 20:35:59 -08002475 if (kLogApi) {
2476 ALOGD("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2477 (RsProgramFragment)vpf, slot, (RsSampler)a);
2478 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002479 rsProgramBindSampler((RsContext)con, (RsProgramFragment)vpf, slot, (RsSampler)a);
Jason Sams68afd012009-12-17 16:55:08 -08002480}
2481
Jason Samsd19f10d2009-05-22 14:03:28 -07002482// ---------------------------------------------------------------------------
2483
Tim Murray460a0492013-11-19 12:45:54 -08002484static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002485nProgramFragmentCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002486 jobjectArray texNames, jlongArray params)
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002487{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002488 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002489 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002490 jint paramLen = _env->GetArrayLength(params);
Miao Wangba8766c2015-10-12 17:24:13 -07002491 if (jParamPtr == nullptr) {
2492 ALOGE("Failed to get Java array elements");
2493 return 0;
2494 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002495
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002496 int texCount = _env->GetArrayLength(texNames);
2497 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2498 const char ** nameArray = names.c_str();
2499 size_t* sizeArray = names.c_str_len();
2500
Andreas Gampe67333922014-11-10 20:35:59 -08002501 if (kLogApi) {
2502 ALOGD("nProgramFragmentCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2503 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002504
Ashok Bhat98071552014-02-12 09:54:43 +00002505 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2506 for(int i = 0; i < paramLen; ++i) {
2507 paramPtr[i] = (uintptr_t)jParamPtr[i];
2508 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002509 jlong ret = (jlong)(uintptr_t)rsProgramFragmentCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002510 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002511 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002512
Ashok Bhat98071552014-02-12 09:54:43 +00002513 free(paramPtr);
2514 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002515 return ret;
2516}
2517
2518
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002519// ---------------------------------------------------------------------------
2520
Tim Murray460a0492013-11-19 12:45:54 -08002521static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002522nProgramVertexCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002523 jobjectArray texNames, jlongArray params)
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002524{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002525 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002526 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams0011bcf2009-12-15 12:58:36 -08002527 jint paramLen = _env->GetArrayLength(params);
Miao Wangba8766c2015-10-12 17:24:13 -07002528 if (jParamPtr == nullptr) {
2529 ALOGE("Failed to get Java array elements");
2530 return 0;
2531 }
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002532
Andreas Gampe67333922014-11-10 20:35:59 -08002533 if (kLogApi) {
2534 ALOGD("nProgramVertexCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2535 }
Jason Sams0011bcf2009-12-15 12:58:36 -08002536
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002537 int texCount = _env->GetArrayLength(texNames);
2538 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2539 const char ** nameArray = names.c_str();
2540 size_t* sizeArray = names.c_str_len();
2541
Ashok Bhat98071552014-02-12 09:54:43 +00002542 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2543 for(int i = 0; i < paramLen; ++i) {
2544 paramPtr[i] = (uintptr_t)jParamPtr[i];
2545 }
2546
Tim Murray3aa89c12014-08-18 17:51:22 -07002547 jlong ret = (jlong)(uintptr_t)rsProgramVertexCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002548 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002549 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002550
Ashok Bhat98071552014-02-12 09:54:43 +00002551 free(paramPtr);
2552 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams0011bcf2009-12-15 12:58:36 -08002553 return ret;
2554}
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002555
Jason Samsebfb4362009-09-23 13:57:02 -07002556// ---------------------------------------------------------------------------
2557
Tim Murray460a0492013-11-19 12:45:54 -08002558static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002559nProgramRasterCreate(JNIEnv *_env, jobject _this, jlong con, jboolean pointSprite, jint cull)
Jason Samsebfb4362009-09-23 13:57:02 -07002560{
Andreas Gampe67333922014-11-10 20:35:59 -08002561 if (kLogApi) {
2562 ALOGD("nProgramRasterCreate, con(%p), pointSprite(%i), cull(%i)", (RsContext)con,
2563 pointSprite, cull);
2564 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002565 return (jlong)(uintptr_t)rsProgramRasterCreate((RsContext)con, pointSprite, (RsCullMode)cull);
Jason Samsebfb4362009-09-23 13:57:02 -07002566}
2567
Jason Samsd19f10d2009-05-22 14:03:28 -07002568
2569// ---------------------------------------------------------------------------
2570
2571static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002572nContextBindRootScript(JNIEnv *_env, jobject _this, jlong con, jlong script)
Jason Samsd19f10d2009-05-22 14:03:28 -07002573{
Andreas Gampe67333922014-11-10 20:35:59 -08002574 if (kLogApi) {
2575 ALOGD("nContextBindRootScript, con(%p), script(%p)", (RsContext)con, (RsScript)script);
2576 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002577 rsContextBindRootScript((RsContext)con, (RsScript)script);
Jason Samsd19f10d2009-05-22 14:03:28 -07002578}
2579
2580static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002581nContextBindProgramStore(JNIEnv *_env, jobject _this, jlong con, jlong pfs)
Jason Samsd19f10d2009-05-22 14:03:28 -07002582{
Andreas Gampe67333922014-11-10 20:35:59 -08002583 if (kLogApi) {
2584 ALOGD("nContextBindProgramStore, con(%p), pfs(%p)", (RsContext)con, (RsProgramStore)pfs);
2585 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002586 rsContextBindProgramStore((RsContext)con, (RsProgramStore)pfs);
Jason Samsd19f10d2009-05-22 14:03:28 -07002587}
2588
2589static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002590nContextBindProgramFragment(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsd19f10d2009-05-22 14:03:28 -07002591{
Andreas Gampe67333922014-11-10 20:35:59 -08002592 if (kLogApi) {
2593 ALOGD("nContextBindProgramFragment, con(%p), pf(%p)", (RsContext)con,
2594 (RsProgramFragment)pf);
2595 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002596 rsContextBindProgramFragment((RsContext)con, (RsProgramFragment)pf);
Jason Samsd19f10d2009-05-22 14:03:28 -07002597}
2598
Jason Sams0826a6f2009-06-15 19:04:56 -07002599static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002600nContextBindProgramVertex(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Sams0826a6f2009-06-15 19:04:56 -07002601{
Andreas Gampe67333922014-11-10 20:35:59 -08002602 if (kLogApi) {
2603 ALOGD("nContextBindProgramVertex, con(%p), pf(%p)", (RsContext)con, (RsProgramVertex)pf);
2604 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002605 rsContextBindProgramVertex((RsContext)con, (RsProgramVertex)pf);
Jason Sams0826a6f2009-06-15 19:04:56 -07002606}
2607
Joe Onoratod7b37742009-08-09 22:57:44 -07002608static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002609nContextBindProgramRaster(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsebfb4362009-09-23 13:57:02 -07002610{
Andreas Gampe67333922014-11-10 20:35:59 -08002611 if (kLogApi) {
2612 ALOGD("nContextBindProgramRaster, con(%p), pf(%p)", (RsContext)con, (RsProgramRaster)pf);
2613 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002614 rsContextBindProgramRaster((RsContext)con, (RsProgramRaster)pf);
Jason Samsebfb4362009-09-23 13:57:02 -07002615}
2616
Joe Onoratod7b37742009-08-09 22:57:44 -07002617
Jason Sams02fb2cb2009-05-28 15:37:57 -07002618// ---------------------------------------------------------------------------
2619
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002620static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002621nSamplerCreate(JNIEnv *_env, jobject _this, jlong con, jint magFilter, jint minFilter,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002622 jint wrapS, jint wrapT, jint wrapR, jfloat aniso)
Jason Sams02fb2cb2009-05-28 15:37:57 -07002623{
Andreas Gampe67333922014-11-10 20:35:59 -08002624 if (kLogApi) {
2625 ALOGD("nSamplerCreate, con(%p)", (RsContext)con);
2626 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002627 return (jlong)(uintptr_t)rsSamplerCreate((RsContext)con,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002628 (RsSamplerValue)magFilter,
2629 (RsSamplerValue)minFilter,
2630 (RsSamplerValue)wrapS,
2631 (RsSamplerValue)wrapT,
2632 (RsSamplerValue)wrapR,
2633 aniso);
Jason Sams02fb2cb2009-05-28 15:37:57 -07002634}
2635
Jason Samsbba134c2009-06-22 15:49:21 -07002636// ---------------------------------------------------------------------------
2637
Tim Murray460a0492013-11-19 12:45:54 -08002638static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002639nMeshCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _vtx, jlongArray _idx, jintArray _prim)
Jason Samsbba134c2009-06-22 15:49:21 -07002640{
Andreas Gampe67333922014-11-10 20:35:59 -08002641 if (kLogApi) {
2642 ALOGD("nMeshCreate, con(%p)", (RsContext)con);
2643 }
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002644
2645 jint vtxLen = _env->GetArrayLength(_vtx);
Chris Wailes488230c32014-08-14 11:22:40 -07002646 jlong *jVtxPtr = _env->GetLongArrayElements(_vtx, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002647 if (jVtxPtr == nullptr) {
2648 ALOGE("Failed to get Java array elements: vtx");
2649 return 0;
2650 }
Ashok Bhat98071552014-02-12 09:54:43 +00002651 RsAllocation* vtxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * vtxLen);
2652 for(int i = 0; i < vtxLen; ++i) {
2653 vtxPtr[i] = (RsAllocation)(uintptr_t)jVtxPtr[i];
2654 }
2655
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002656 jint idxLen = _env->GetArrayLength(_idx);
Chris Wailes488230c32014-08-14 11:22:40 -07002657 jlong *jIdxPtr = _env->GetLongArrayElements(_idx, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002658 if (jIdxPtr == nullptr) {
2659 ALOGE("Failed to get Java array elements: idx");
2660 return 0;
2661 }
Ashok Bhat98071552014-02-12 09:54:43 +00002662 RsAllocation* idxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * idxLen);
2663 for(int i = 0; i < idxLen; ++i) {
2664 idxPtr[i] = (RsAllocation)(uintptr_t)jIdxPtr[i];
2665 }
2666
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002667 jint primLen = _env->GetArrayLength(_prim);
Chris Wailes488230c32014-08-14 11:22:40 -07002668 jint *primPtr = _env->GetIntArrayElements(_prim, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002669 if (primPtr == nullptr) {
2670 ALOGE("Failed to get Java array elements: prim");
2671 return 0;
2672 }
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002673
Tim Murray3aa89c12014-08-18 17:51:22 -07002674 jlong id = (jlong)(uintptr_t)rsMeshCreate((RsContext)con,
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002675 (RsAllocation *)vtxPtr, vtxLen,
2676 (RsAllocation *)idxPtr, idxLen,
2677 (uint32_t *)primPtr, primLen);
2678
Ashok Bhat98071552014-02-12 09:54:43 +00002679 free(vtxPtr);
2680 free(idxPtr);
2681 _env->ReleaseLongArrayElements(_vtx, jVtxPtr, 0);
2682 _env->ReleaseLongArrayElements(_idx, jIdxPtr, 0);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002683 _env->ReleaseIntArrayElements(_prim, primPtr, 0);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002684 return id;
2685}
2686
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002687static jint
Tim Murray460a0492013-11-19 12:45:54 -08002688nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002689{
Andreas Gampe67333922014-11-10 20:35:59 -08002690 if (kLogApi) {
2691 ALOGD("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2692 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002693 jint vtxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002694 rsaMeshGetVertexBufferCount((RsContext)con, (RsMesh)mesh, &vtxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002695 return vtxCount;
2696}
2697
2698static jint
Tim Murray460a0492013-11-19 12:45:54 -08002699nMeshGetIndexCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002700{
Andreas Gampe67333922014-11-10 20:35:59 -08002701 if (kLogApi) {
2702 ALOGD("nMeshGetIndexCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2703 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002704 jint idxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002705 rsaMeshGetIndexCount((RsContext)con, (RsMesh)mesh, &idxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002706 return idxCount;
2707}
2708
2709static void
Ashok Bhat98071552014-02-12 09:54:43 +00002710nMeshGetVertices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _ids, jint numVtxIDs)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002711{
Andreas Gampe67333922014-11-10 20:35:59 -08002712 if (kLogApi) {
2713 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2714 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002715
2716 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
Tim Murrayeff663f2013-11-15 13:08:30 -08002717 rsaMeshGetVertices((RsContext)con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002718
2719 for(jint i = 0; i < numVtxIDs; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002720 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002721 _env->SetLongArrayRegion(_ids, i, 1, &alloc);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002722 }
2723
2724 free(allocs);
2725}
2726
2727static void
Ashok Bhat98071552014-02-12 09:54:43 +00002728nMeshGetIndices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _idxIds, jintArray _primitives, jint numIndices)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002729{
Andreas Gampe67333922014-11-10 20:35:59 -08002730 if (kLogApi) {
2731 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2732 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002733
2734 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
2735 uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
2736
Tim Murrayeff663f2013-11-15 13:08:30 -08002737 rsaMeshGetIndices((RsContext)con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002738
2739 for(jint i = 0; i < numIndices; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002740 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002741 const jint prim = (jint)prims[i];
2742 _env->SetLongArrayRegion(_idxIds, i, 1, &alloc);
2743 _env->SetIntArrayRegion(_primitives, i, 1, &prim);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002744 }
2745
2746 free(allocs);
2747 free(prims);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002748}
2749
Tim Murray56f9e6f2014-05-16 11:47:26 -07002750static jint
2751nSystemGetPointerSize(JNIEnv *_env, jobject _this) {
2752 return (jint)sizeof(void*);
2753}
2754
2755
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002756// ---------------------------------------------------------------------------
2757
Jason Samsd19f10d2009-05-22 14:03:28 -07002758
Jason Sams94d8e90a2009-06-10 16:09:05 -07002759static const char *classPathName = "android/renderscript/RenderScript";
Jason Samsd19f10d2009-05-22 14:03:28 -07002760
Daniel Micay76f6a862015-09-19 17:31:01 -04002761static const JNINativeMethod methods[] = {
Jason Sams1c415172010-11-08 17:06:46 -08002762{"_nInit", "()V", (void*)_nInit },
Jason Samsea84a7c2009-09-04 14:42:41 -07002763
Tim Murrayeff663f2013-11-15 13:08:30 -08002764{"nDeviceCreate", "()J", (void*)nDeviceCreate },
2765{"nDeviceDestroy", "(J)V", (void*)nDeviceDestroy },
2766{"nDeviceSetConfig", "(JII)V", (void*)nDeviceSetConfig },
2767{"nContextGetUserMessage", "(J[I)I", (void*)nContextGetUserMessage },
2768{"nContextGetErrorMessage", "(J)Ljava/lang/String;", (void*)nContextGetErrorMessage },
2769{"nContextPeekMessage", "(J[I)I", (void*)nContextPeekMessage },
Jason Sams1c415172010-11-08 17:06:46 -08002770
Tim Murrayeff663f2013-11-15 13:08:30 -08002771{"nContextInitToClient", "(J)V", (void*)nContextInitToClient },
2772{"nContextDeinitToClient", "(J)V", (void*)nContextDeinitToClient },
Jason Samsd19f10d2009-05-22 14:03:28 -07002773
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002774
Jason Sams2e1872f2010-08-17 16:25:41 -07002775// All methods below are thread protected in java.
Tim Murrayeff663f2013-11-15 13:08:30 -08002776{"rsnContextCreate", "(JIII)J", (void*)nContextCreate },
2777{"rsnContextCreateGL", "(JIIIIIIIIIIIIFI)J", (void*)nContextCreateGL },
2778{"rsnContextFinish", "(J)V", (void*)nContextFinish },
2779{"rsnContextSetPriority", "(JI)V", (void*)nContextSetPriority },
Tim Murray47f31582015-04-07 15:43:24 -07002780{"rsnContextSetCacheDir", "(JLjava/lang/String;)V", (void*)nContextSetCacheDir },
Tim Murrayeff663f2013-11-15 13:08:30 -08002781{"rsnContextSetSurface", "(JIILandroid/view/Surface;)V", (void*)nContextSetSurface },
2782{"rsnContextDestroy", "(J)V", (void*)nContextDestroy },
2783{"rsnContextDump", "(JI)V", (void*)nContextDump },
2784{"rsnContextPause", "(J)V", (void*)nContextPause },
2785{"rsnContextResume", "(J)V", (void*)nContextResume },
2786{"rsnContextSendMessage", "(JI[I)V", (void*)nContextSendMessage },
Yang Ni281c3252014-10-24 08:52:24 -07002787{"rsnClosureCreate", "(JJJ[J[J[I[J[J)J", (void*)nClosureCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002788{"rsnInvokeClosureCreate", "(JJ[B[J[J[I)J", (void*)nInvokeClosureCreate },
Yang Ni281c3252014-10-24 08:52:24 -07002789{"rsnClosureSetArg", "(JJIJI)V", (void*)nClosureSetArg },
2790{"rsnClosureSetGlobal", "(JJJJI)V", (void*)nClosureSetGlobal },
Tim Murray460a0492013-11-19 12:45:54 -08002791{"rsnAssignName", "(JJ[B)V", (void*)nAssignName },
2792{"rsnGetName", "(JJ)Ljava/lang/String;", (void*)nGetName },
2793{"rsnObjDestroy", "(JJ)V", (void*)nObjDestroy },
Jason Sams64676f32009-07-08 18:01:53 -07002794
Tim Murray460a0492013-11-19 12:45:54 -08002795{"rsnFileA3DCreateFromFile", "(JLjava/lang/String;)J", (void*)nFileA3DCreateFromFile },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002796{"rsnFileA3DCreateFromAssetStream", "(JJ)J", (void*)nFileA3DCreateFromAssetStream },
Tim Murray460a0492013-11-19 12:45:54 -08002797{"rsnFileA3DCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;)J", (void*)nFileA3DCreateFromAsset },
2798{"rsnFileA3DGetNumIndexEntries", "(JJ)I", (void*)nFileA3DGetNumIndexEntries },
2799{"rsnFileA3DGetIndexEntries", "(JJI[I[Ljava/lang/String;)V", (void*)nFileA3DGetIndexEntries },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002800{"rsnFileA3DGetEntryByIndex", "(JJI)J", (void*)nFileA3DGetEntryByIndex },
Jason Samsd19f10d2009-05-22 14:03:28 -07002801
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002802{"rsnFontCreateFromFile", "(JLjava/lang/String;FI)J", (void*)nFontCreateFromFile },
2803{"rsnFontCreateFromAssetStream", "(JLjava/lang/String;FIJ)J", (void*)nFontCreateFromAssetStream },
2804{"rsnFontCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;FI)J", (void*)nFontCreateFromAsset },
Jason Samsd19f10d2009-05-22 14:03:28 -07002805
Tim Murray460a0492013-11-19 12:45:54 -08002806{"rsnElementCreate", "(JJIZI)J", (void*)nElementCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002807{"rsnElementCreate2", "(J[J[Ljava/lang/String;[I)J", (void*)nElementCreate2 },
Tim Murray460a0492013-11-19 12:45:54 -08002808{"rsnElementGetNativeData", "(JJ[I)V", (void*)nElementGetNativeData },
Ashok Bhat98071552014-02-12 09:54:43 +00002809{"rsnElementGetSubElements", "(JJ[J[Ljava/lang/String;[I)V", (void*)nElementGetSubElements },
Jason Samsd19f10d2009-05-22 14:03:28 -07002810
Tim Murray460a0492013-11-19 12:45:54 -08002811{"rsnTypeCreate", "(JJIIIZZI)J", (void*)nTypeCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002812{"rsnTypeGetNativeData", "(JJ[J)V", (void*)nTypeGetNativeData },
Jason Samsd19f10d2009-05-22 14:03:28 -07002813
Ashok Bhat98071552014-02-12 09:54:43 +00002814{"rsnAllocationCreateTyped", "(JJIIJ)J", (void*)nAllocationCreateTyped },
Tim Murray460a0492013-11-19 12:45:54 -08002815{"rsnAllocationCreateFromBitmap", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateFromBitmap },
2816{"rsnAllocationCreateBitmapBackedAllocation", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateBitmapBackedAllocation },
2817{"rsnAllocationCubeCreateFromBitmap","(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCubeCreateFromBitmap },
Jason Sams5476b452010-12-08 16:14:36 -08002818
Tim Murray460a0492013-11-19 12:45:54 -08002819{"rsnAllocationCopyFromBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyFromBitmap },
2820{"rsnAllocationCopyToBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyToBitmap },
Jason Sams4ef66502010-12-10 16:03:15 -08002821
Tim Murray460a0492013-11-19 12:45:54 -08002822{"rsnAllocationSyncAll", "(JJI)V", (void*)nAllocationSyncAll },
2823{"rsnAllocationGetSurface", "(JJ)Landroid/view/Surface;", (void*)nAllocationGetSurface },
2824{"rsnAllocationSetSurface", "(JJLandroid/view/Surface;)V", (void*)nAllocationSetSurface },
2825{"rsnAllocationIoSend", "(JJ)V", (void*)nAllocationIoSend },
2826{"rsnAllocationIoReceive", "(JJ)V", (void*)nAllocationIoReceive },
Miao Wang87e908d2015-03-02 15:15:15 -08002827{"rsnAllocationData1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData1D },
Miao Wangc8e237e2015-02-20 18:36:32 -08002828{"rsnAllocationElementData", "(JJIIIII[BI)V", (void*)nAllocationElementData },
Miao Wang87e908d2015-03-02 15:15:15 -08002829{"rsnAllocationData2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData2D },
Tim Murray460a0492013-11-19 12:45:54 -08002830{"rsnAllocationData2D", "(JJIIIIIIJIIII)V", (void*)nAllocationData2D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002831{"rsnAllocationData3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData3D },
Tim Murray460a0492013-11-19 12:45:54 -08002832{"rsnAllocationData3D", "(JJIIIIIIIJIIII)V", (void*)nAllocationData3D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002833{"rsnAllocationRead", "(JJLjava/lang/Object;IIZ)V", (void*)nAllocationRead },
2834{"rsnAllocationRead1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead1D },
Miao Wang45cec0a2015-03-04 16:40:21 -08002835{"rsnAllocationElementRead", "(JJIIIII[BI)V", (void*)nAllocationElementRead },
Miao Wang87e908d2015-03-02 15:15:15 -08002836{"rsnAllocationRead2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead2D },
2837{"rsnAllocationRead3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead3D },
Tim Murray460a0492013-11-19 12:45:54 -08002838{"rsnAllocationGetType", "(JJ)J", (void*)nAllocationGetType},
2839{"rsnAllocationResize1D", "(JJI)V", (void*)nAllocationResize1D },
2840{"rsnAllocationGenerateMipmaps", "(JJ)V", (void*)nAllocationGenerateMipmaps },
Jason Samsbd1c3ad2009-08-03 16:03:08 -07002841
Jason Sams46ba27e32015-02-06 17:45:15 -08002842{"rsnAllocationAdapterCreate", "(JJJ)J", (void*)nAllocationAdapterCreate },
2843{"rsnAllocationAdapterOffset", "(JJIIIIIIIII)V", (void*)nAllocationAdapterOffset },
2844
Tim Murray460a0492013-11-19 12:45:54 -08002845{"rsnScriptBindAllocation", "(JJJI)V", (void*)nScriptBindAllocation },
2846{"rsnScriptSetTimeZone", "(JJ[B)V", (void*)nScriptSetTimeZone },
2847{"rsnScriptInvoke", "(JJI)V", (void*)nScriptInvoke },
2848{"rsnScriptInvokeV", "(JJI[B)V", (void*)nScriptInvokeV },
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002849
2850{"rsnScriptForEach", "(JJI[JJ[B[I)V", (void*)nScriptForEach },
Matt Wala36eb1f72015-07-20 15:35:27 -07002851{"rsnScriptReduce", "(JJIJJ[I)V", (void*)nScriptReduce },
David Gross26ef7a732016-01-12 12:19:15 -08002852{"rsnScriptReduceNew", "(JJI[JJ[I)V", (void*)nScriptReduceNew },
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002853
Tim Murray460a0492013-11-19 12:45:54 -08002854{"rsnScriptSetVarI", "(JJII)V", (void*)nScriptSetVarI },
2855{"rsnScriptGetVarI", "(JJI)I", (void*)nScriptGetVarI },
2856{"rsnScriptSetVarJ", "(JJIJ)V", (void*)nScriptSetVarJ },
2857{"rsnScriptGetVarJ", "(JJI)J", (void*)nScriptGetVarJ },
2858{"rsnScriptSetVarF", "(JJIF)V", (void*)nScriptSetVarF },
2859{"rsnScriptGetVarF", "(JJI)F", (void*)nScriptGetVarF },
2860{"rsnScriptSetVarD", "(JJID)V", (void*)nScriptSetVarD },
2861{"rsnScriptGetVarD", "(JJI)D", (void*)nScriptGetVarD },
2862{"rsnScriptSetVarV", "(JJI[B)V", (void*)nScriptSetVarV },
2863{"rsnScriptGetVarV", "(JJI[B)V", (void*)nScriptGetVarV },
2864{"rsnScriptSetVarVE", "(JJI[BJ[I)V", (void*)nScriptSetVarVE },
2865{"rsnScriptSetVarObj", "(JJIJ)V", (void*)nScriptSetVarObj },
Jason Samsd19f10d2009-05-22 14:03:28 -07002866
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002867{"rsnScriptCCreate", "(JLjava/lang/String;Ljava/lang/String;[BI)J", (void*)nScriptCCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002868{"rsnScriptIntrinsicCreate", "(JIJ)J", (void*)nScriptIntrinsicCreate },
2869{"rsnScriptKernelIDCreate", "(JJII)J", (void*)nScriptKernelIDCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002870{"rsnScriptInvokeIDCreate", "(JJI)J", (void*)nScriptInvokeIDCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002871{"rsnScriptFieldIDCreate", "(JJI)J", (void*)nScriptFieldIDCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002872{"rsnScriptGroupCreate", "(J[J[J[J[J[J)J", (void*)nScriptGroupCreate },
Yang Ni35be56c2015-04-02 17:47:56 -07002873{"rsnScriptGroup2Create", "(JLjava/lang/String;Ljava/lang/String;[J)J", (void*)nScriptGroup2Create },
Tim Murray460a0492013-11-19 12:45:54 -08002874{"rsnScriptGroupSetInput", "(JJJJ)V", (void*)nScriptGroupSetInput },
2875{"rsnScriptGroupSetOutput", "(JJJJ)V", (void*)nScriptGroupSetOutput },
2876{"rsnScriptGroupExecute", "(JJ)V", (void*)nScriptGroupExecute },
Yang Ni281c3252014-10-24 08:52:24 -07002877{"rsnScriptGroup2Execute", "(JJ)V", (void*)nScriptGroup2Execute },
Jason Sams0011bcf2009-12-15 12:58:36 -08002878
Tim Murray25207df2015-01-12 16:47:56 -08002879{"rsnScriptIntrinsicBLAS_Single", "(JJIIIIIIIIIFJJFJIIII)V", (void*)nScriptIntrinsicBLAS_Single },
2880{"rsnScriptIntrinsicBLAS_Double", "(JJIIIIIIIIIDJJDJIIII)V", (void*)nScriptIntrinsicBLAS_Double },
2881{"rsnScriptIntrinsicBLAS_Complex", "(JJIIIIIIIIIFFJJFFJIIII)V", (void*)nScriptIntrinsicBLAS_Complex },
2882{"rsnScriptIntrinsicBLAS_Z", "(JJIIIIIIIIIDDJJDDJIIII)V", (void*)nScriptIntrinsicBLAS_Z },
2883
Tim Murray9cb16a22015-04-01 11:07:16 -07002884{"rsnScriptIntrinsicBLAS_BNNM", "(JJIIIJIJIJII)V", (void*)nScriptIntrinsicBLAS_BNNM },
2885
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002886{"rsnProgramStoreCreate", "(JZZZZZZIII)J", (void*)nProgramStoreCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002887
Tim Murray460a0492013-11-19 12:45:54 -08002888{"rsnProgramBindConstants", "(JJIJ)V", (void*)nProgramBindConstants },
2889{"rsnProgramBindTexture", "(JJIJ)V", (void*)nProgramBindTexture },
2890{"rsnProgramBindSampler", "(JJIJ)V", (void*)nProgramBindSampler },
Jason Samsebfb4362009-09-23 13:57:02 -07002891
Ashok Bhat98071552014-02-12 09:54:43 +00002892{"rsnProgramFragmentCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramFragmentCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002893{"rsnProgramRasterCreate", "(JZI)J", (void*)nProgramRasterCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002894{"rsnProgramVertexCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramVertexCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002895
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002896{"rsnContextBindRootScript", "(JJ)V", (void*)nContextBindRootScript },
2897{"rsnContextBindProgramStore", "(JJ)V", (void*)nContextBindProgramStore },
2898{"rsnContextBindProgramFragment", "(JJ)V", (void*)nContextBindProgramFragment },
2899{"rsnContextBindProgramVertex", "(JJ)V", (void*)nContextBindProgramVertex },
2900{"rsnContextBindProgramRaster", "(JJ)V", (void*)nContextBindProgramRaster },
Jason Sams02fb2cb2009-05-28 15:37:57 -07002901
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002902{"rsnSamplerCreate", "(JIIIIIF)J", (void*)nSamplerCreate },
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002903
Ashok Bhat98071552014-02-12 09:54:43 +00002904{"rsnMeshCreate", "(J[J[J[I)J", (void*)nMeshCreate },
Jason Sams2e1872f2010-08-17 16:25:41 -07002905
Tim Murray460a0492013-11-19 12:45:54 -08002906{"rsnMeshGetVertexBufferCount", "(JJ)I", (void*)nMeshGetVertexBufferCount },
2907{"rsnMeshGetIndexCount", "(JJ)I", (void*)nMeshGetIndexCount },
Ashok Bhat98071552014-02-12 09:54:43 +00002908{"rsnMeshGetVertices", "(JJ[JI)V", (void*)nMeshGetVertices },
2909{"rsnMeshGetIndices", "(JJ[J[II)V", (void*)nMeshGetIndices },
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002910
Tim Murray56f9e6f2014-05-16 11:47:26 -07002911{"rsnSystemGetPointerSize", "()I", (void*)nSystemGetPointerSize },
Jason Samsd19f10d2009-05-22 14:03:28 -07002912};
2913
2914static int registerFuncs(JNIEnv *_env)
2915{
2916 return android::AndroidRuntime::registerNativeMethods(
2917 _env, classPathName, methods, NELEM(methods));
2918}
2919
2920// ---------------------------------------------------------------------------
2921
2922jint JNI_OnLoad(JavaVM* vm, void* reserved)
2923{
Chris Wailes488230c32014-08-14 11:22:40 -07002924 JNIEnv* env = nullptr;
Jason Samsd19f10d2009-05-22 14:03:28 -07002925 jint result = -1;
2926
Jason Samsd19f10d2009-05-22 14:03:28 -07002927 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
Steve Block3762c312012-01-06 19:20:56 +00002928 ALOGE("ERROR: GetEnv failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07002929 goto bail;
2930 }
Chris Wailes488230c32014-08-14 11:22:40 -07002931 assert(env != nullptr);
Jason Samsd19f10d2009-05-22 14:03:28 -07002932
2933 if (registerFuncs(env) < 0) {
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002934 ALOGE("ERROR: Renderscript native registration failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07002935 goto bail;
2936 }
2937
2938 /* success -- return valid version number */
2939 result = JNI_VERSION_1_4;
2940
2941bail:
2942 return result;
2943}