blob: 58d0fce9604f8be16f3876384b7b0ea9c67c46d7 [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;
Yang Ni17c2d7a2015-04-30 16:13:54 -070046static constexpr size_t kMaxNumberArgsAndBindings = 1000;
47static constexpr size_t kMaxNumberClosuresInScriptGroup = 1000000;
48static constexpr size_t kMaxNumberKernelArguments = 256;
Jason Samsd19f10d2009-05-22 14:03:28 -070049
50using namespace android;
51
Andreas Gampe67333922014-11-10 20:35:59 -080052template <typename... T>
53void UNUSED(T... t) {}
54
Stephen Hines414fa2c2014-04-17 01:02:42 -070055#define PER_ARRAY_TYPE(flag, fnc, readonly, ...) { \
Jason Samse729a942013-11-06 11:22:02 -080056 jint len = 0; \
Chris Wailes488230c32014-08-14 11:22:40 -070057 void *ptr = nullptr; \
Miao Wang87e908d2015-03-02 15:15:15 -080058 void *srcPtr = nullptr; \
Jason Sams21659ac2013-11-06 15:08:07 -080059 size_t typeBytes = 0; \
Stephen Hines414fa2c2014-04-17 01:02:42 -070060 jint relFlag = 0; \
61 if (readonly) { \
62 /* The on-release mode should only be JNI_ABORT for read-only accesses. */ \
Miao Wang87e908d2015-03-02 15:15:15 -080063 /* readonly = true, also indicates we are copying to the allocation . */ \
Stephen Hines414fa2c2014-04-17 01:02:42 -070064 relFlag = JNI_ABORT; \
65 } \
Jason Samse729a942013-11-06 11:22:02 -080066 switch(dataType) { \
67 case RS_TYPE_FLOAT_32: \
68 len = _env->GetArrayLength((jfloatArray)data); \
69 ptr = _env->GetFloatArrayElements((jfloatArray)data, flag); \
Jason Sams21659ac2013-11-06 15:08:07 -080070 typeBytes = 4; \
Miao Wang87e908d2015-03-02 15:15:15 -080071 if (usePadding) { \
72 srcPtr = ptr; \
73 len = len / 3 * 4; \
74 if (count == 0) { \
75 count = len / 4; \
76 } \
77 ptr = malloc (len * typeBytes); \
78 if (readonly) { \
79 copyWithPadding(ptr, srcPtr, mSize, count); \
80 fnc(__VA_ARGS__); \
81 } else { \
82 fnc(__VA_ARGS__); \
83 copyWithUnPadding(srcPtr, ptr, mSize, count); \
84 } \
85 free(ptr); \
86 ptr = srcPtr; \
87 } else { \
88 fnc(__VA_ARGS__); \
89 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -070090 _env->ReleaseFloatArrayElements((jfloatArray)data, (jfloat *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -080091 return; \
92 case RS_TYPE_FLOAT_64: \
93 len = _env->GetArrayLength((jdoubleArray)data); \
94 ptr = _env->GetDoubleArrayElements((jdoubleArray)data, flag); \
Jason Sams21659ac2013-11-06 15:08:07 -080095 typeBytes = 8; \
Miao Wang87e908d2015-03-02 15:15:15 -080096 if (usePadding) { \
97 srcPtr = ptr; \
98 len = len / 3 * 4; \
99 if (count == 0) { \
100 count = len / 4; \
101 } \
102 ptr = malloc (len * typeBytes); \
103 if (readonly) { \
104 copyWithPadding(ptr, srcPtr, mSize, count); \
105 fnc(__VA_ARGS__); \
106 } else { \
107 fnc(__VA_ARGS__); \
108 copyWithUnPadding(srcPtr, ptr, mSize, count); \
109 } \
110 free(ptr); \
111 ptr = srcPtr; \
112 } else { \
113 fnc(__VA_ARGS__); \
114 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700115 _env->ReleaseDoubleArrayElements((jdoubleArray)data, (jdouble *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800116 return; \
117 case RS_TYPE_SIGNED_8: \
118 case RS_TYPE_UNSIGNED_8: \
119 len = _env->GetArrayLength((jbyteArray)data); \
120 ptr = _env->GetByteArrayElements((jbyteArray)data, flag); \
Jason Sams21659ac2013-11-06 15:08:07 -0800121 typeBytes = 1; \
Miao Wang87e908d2015-03-02 15:15:15 -0800122 if (usePadding) { \
123 srcPtr = ptr; \
124 len = len / 3 * 4; \
125 if (count == 0) { \
126 count = len / 4; \
127 } \
128 ptr = malloc (len * typeBytes); \
129 if (readonly) { \
130 copyWithPadding(ptr, srcPtr, mSize, count); \
131 fnc(__VA_ARGS__); \
132 } else { \
133 fnc(__VA_ARGS__); \
134 copyWithUnPadding(srcPtr, ptr, mSize, count); \
135 } \
136 free(ptr); \
137 ptr = srcPtr; \
138 } else { \
139 fnc(__VA_ARGS__); \
140 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700141 _env->ReleaseByteArrayElements((jbyteArray)data, (jbyte*)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800142 return; \
143 case RS_TYPE_SIGNED_16: \
144 case RS_TYPE_UNSIGNED_16: \
145 len = _env->GetArrayLength((jshortArray)data); \
146 ptr = _env->GetShortArrayElements((jshortArray)data, flag); \
Jason Sams21659ac2013-11-06 15:08:07 -0800147 typeBytes = 2; \
Miao Wang87e908d2015-03-02 15:15:15 -0800148 if (usePadding) { \
149 srcPtr = ptr; \
150 len = len / 3 * 4; \
151 if (count == 0) { \
152 count = len / 4; \
153 } \
154 ptr = malloc (len * typeBytes); \
155 if (readonly) { \
156 copyWithPadding(ptr, srcPtr, mSize, count); \
157 fnc(__VA_ARGS__); \
158 } else { \
159 fnc(__VA_ARGS__); \
160 copyWithUnPadding(srcPtr, ptr, mSize, count); \
161 } \
162 free(ptr); \
163 ptr = srcPtr; \
164 } else { \
165 fnc(__VA_ARGS__); \
166 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700167 _env->ReleaseShortArrayElements((jshortArray)data, (jshort *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800168 return; \
169 case RS_TYPE_SIGNED_32: \
170 case RS_TYPE_UNSIGNED_32: \
171 len = _env->GetArrayLength((jintArray)data); \
172 ptr = _env->GetIntArrayElements((jintArray)data, flag); \
Jason Sams21659ac2013-11-06 15:08:07 -0800173 typeBytes = 4; \
Miao Wang87e908d2015-03-02 15:15:15 -0800174 if (usePadding) { \
175 srcPtr = ptr; \
176 len = len / 3 * 4; \
177 if (count == 0) { \
178 count = len / 4; \
179 } \
180 ptr = malloc (len * typeBytes); \
181 if (readonly) { \
182 copyWithPadding(ptr, srcPtr, mSize, count); \
183 fnc(__VA_ARGS__); \
184 } else { \
185 fnc(__VA_ARGS__); \
186 copyWithUnPadding(srcPtr, ptr, mSize, count); \
187 } \
188 free(ptr); \
189 ptr = srcPtr; \
190 } else { \
191 fnc(__VA_ARGS__); \
192 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700193 _env->ReleaseIntArrayElements((jintArray)data, (jint *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800194 return; \
195 case RS_TYPE_SIGNED_64: \
196 case RS_TYPE_UNSIGNED_64: \
197 len = _env->GetArrayLength((jlongArray)data); \
198 ptr = _env->GetLongArrayElements((jlongArray)data, flag); \
Jason Sams21659ac2013-11-06 15:08:07 -0800199 typeBytes = 8; \
Miao Wang87e908d2015-03-02 15:15:15 -0800200 if (usePadding) { \
201 srcPtr = ptr; \
202 len = len / 3 * 4; \
203 if (count == 0) { \
204 count = len / 4; \
205 } \
206 ptr = malloc (len * typeBytes); \
207 if (readonly) { \
208 copyWithPadding(ptr, srcPtr, mSize, count); \
209 fnc(__VA_ARGS__); \
210 } else { \
211 fnc(__VA_ARGS__); \
212 copyWithUnPadding(srcPtr, ptr, mSize, count); \
213 } \
214 free(ptr); \
215 ptr = srcPtr; \
216 } else { \
217 fnc(__VA_ARGS__); \
218 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700219 _env->ReleaseLongArrayElements((jlongArray)data, (jlong *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800220 return; \
221 default: \
222 break; \
223 } \
Miao Wang87e908d2015-03-02 15:15:15 -0800224 UNUSED(len, ptr, srcPtr, typeBytes, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800225}
226
227
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800228class AutoJavaStringToUTF8 {
229public:
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800230 AutoJavaStringToUTF8(JNIEnv* env, jstring str) : fEnv(env), fJStr(str) {
Chris Wailes488230c32014-08-14 11:22:40 -0700231 fCStr = env->GetStringUTFChars(str, nullptr);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800232 fLength = env->GetStringUTFLength(str);
233 }
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800234 ~AutoJavaStringToUTF8() {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800235 fEnv->ReleaseStringUTFChars(fJStr, fCStr);
236 }
237 const char* c_str() const { return fCStr; }
238 jsize length() const { return fLength; }
239
240private:
241 JNIEnv* fEnv;
242 jstring fJStr;
243 const char* fCStr;
244 jsize fLength;
245};
246
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800247class AutoJavaStringArrayToUTF8 {
248public:
249 AutoJavaStringArrayToUTF8(JNIEnv* env, jobjectArray strings, jsize stringsLength)
250 : mEnv(env), mStrings(strings), mStringsLength(stringsLength) {
Chris Wailes488230c32014-08-14 11:22:40 -0700251 mCStrings = nullptr;
252 mSizeArray = nullptr;
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800253 if (stringsLength > 0) {
254 mCStrings = (const char **)calloc(stringsLength, sizeof(char *));
255 mSizeArray = (size_t*)calloc(stringsLength, sizeof(size_t));
256 for (jsize ct = 0; ct < stringsLength; ct ++) {
257 jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
Chris Wailes488230c32014-08-14 11:22:40 -0700258 mCStrings[ct] = mEnv->GetStringUTFChars(s, nullptr);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800259 mSizeArray[ct] = mEnv->GetStringUTFLength(s);
260 }
261 }
262 }
263 ~AutoJavaStringArrayToUTF8() {
264 for (jsize ct=0; ct < mStringsLength; ct++) {
265 jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
266 mEnv->ReleaseStringUTFChars(s, mCStrings[ct]);
267 }
268 free(mCStrings);
269 free(mSizeArray);
270 }
271 const char **c_str() const { return mCStrings; }
272 size_t *c_str_len() const { return mSizeArray; }
273 jsize length() const { return mStringsLength; }
274
275private:
276 JNIEnv *mEnv;
277 jobjectArray mStrings;
278 const char **mCStrings;
279 size_t *mSizeArray;
280 jsize mStringsLength;
281};
282
Jason Samsd19f10d2009-05-22 14:03:28 -0700283// ---------------------------------------------------------------------------
284
Jason Samsffe9f482009-06-01 17:45:53 -0700285static jfieldID gContextId = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -0700286
287static void _nInit(JNIEnv *_env, jclass _this)
288{
Tim Murrayeff663f2013-11-15 13:08:30 -0800289 gContextId = _env->GetFieldID(_this, "mContext", "J");
Jason Samsd19f10d2009-05-22 14:03:28 -0700290}
291
Jason Samsd19f10d2009-05-22 14:03:28 -0700292// ---------------------------------------------------------------------------
293
Miao Wang87e908d2015-03-02 15:15:15 -0800294static void copyWithPadding(void* ptr, void* srcPtr, int mSize, int count) {
295 int sizeBytesPad = mSize * 4;
296 int sizeBytes = mSize * 3;
297 uint8_t *dst = static_cast<uint8_t *>(ptr);
298 uint8_t *src = static_cast<uint8_t *>(srcPtr);
299 for (int i = 0; i < count; i++) {
300 memcpy(dst, src, sizeBytes);
301 dst += sizeBytesPad;
302 src += sizeBytes;
303 }
304}
305
306static void copyWithUnPadding(void* ptr, void* srcPtr, int mSize, int count) {
307 int sizeBytesPad = mSize * 4;
308 int sizeBytes = mSize * 3;
309 uint8_t *dst = static_cast<uint8_t *>(ptr);
310 uint8_t *src = static_cast<uint8_t *>(srcPtr);
311 for (int i = 0; i < count; i++) {
312 memcpy(dst, src, sizeBytes);
313 dst += sizeBytes;
314 src += sizeBytesPad;
315 }
316}
317
318
319// ---------------------------------------------------------------------------
Jason Sams3eaa338e2009-06-10 15:04:38 -0700320static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800321nContextFinish(JNIEnv *_env, jobject _this, jlong con)
Jason Sams96ed4cf2010-06-15 12:15:57 -0700322{
Andreas Gampe67333922014-11-10 20:35:59 -0800323 if (kLogApi) {
324 ALOGD("nContextFinish, con(%p)", (RsContext)con);
325 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800326 rsContextFinish((RsContext)con);
Jason Sams96ed4cf2010-06-15 12:15:57 -0700327}
328
Yang Ni281c3252014-10-24 08:52:24 -0700329static jlong
330nClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong kernelID,
331 jlong returnValue, jlongArray fieldIDArray,
332 jlongArray valueArray, jintArray sizeArray,
333 jlongArray depClosureArray, jlongArray depFieldIDArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700334 jlong ret = 0;
335
Yang Ni281c3252014-10-24 08:52:24 -0700336 jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
337 jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
Yang Ni281c3252014-10-24 08:52:24 -0700338 jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
339 jsize values_length = _env->GetArrayLength(valueArray);
Yang Ni17c2d7a2015-04-30 16:13:54 -0700340 jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
Yang Ni281c3252014-10-24 08:52:24 -0700341 jsize sizes_length = _env->GetArrayLength(sizeArray);
Yang Ni281c3252014-10-24 08:52:24 -0700342 jlong* jDepClosures =
343 _env->GetLongArrayElements(depClosureArray, nullptr);
344 jsize depClosures_length = _env->GetArrayLength(depClosureArray);
Yang Ni281c3252014-10-24 08:52:24 -0700345 jlong* jDepFieldIDs =
346 _env->GetLongArrayElements(depFieldIDArray, nullptr);
347 jsize depFieldIDs_length = _env->GetArrayLength(depFieldIDArray);
Yang Ni17c2d7a2015-04-30 16:13:54 -0700348
349 size_t numValues, numDependencies;
350 RsScriptFieldID* fieldIDs;
351 uintptr_t* values;
352 RsClosure* depClosures;
353 RsScriptFieldID* depFieldIDs;
354
355 if (fieldIDs_length != values_length || values_length != sizes_length) {
356 ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
357 goto exit;
358 }
359
360 numValues = (size_t)fieldIDs_length;
361
362 if (depClosures_length != depFieldIDs_length) {
363 ALOGE("Unmatched closures and field IDs for dependencies in closure creation.");
364 goto exit;
365 }
366
367 numDependencies = (size_t)depClosures_length;
368
369 if (numDependencies > numValues) {
370 ALOGE("Unexpected number of dependencies in closure creation");
371 goto exit;
372 }
373
374 if (numValues > kMaxNumberArgsAndBindings) {
375 ALOGE("Too many arguments or globals in closure creation");
376 goto exit;
377 }
378
379 fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
380 if (fieldIDs == nullptr) {
381 goto exit;
382 }
383
384 for (size_t i = 0; i < numValues; i++) {
385 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
386 }
387
388 values = (uintptr_t*)alloca(sizeof(uintptr_t) * numValues);
389 if (values == nullptr) {
390 goto exit;
391 }
392
393 for (size_t i = 0; i < numValues; i++) {
394 values[i] = (uintptr_t)jValues[i];
395 }
396
397 depClosures = (RsClosure*)alloca(sizeof(RsClosure) * numDependencies);
398 if (depClosures == nullptr) {
399 goto exit;
400 }
401
402 for (size_t i = 0; i < numDependencies; i++) {
403 depClosures[i] = (RsClosure)jDepClosures[i];
404 }
405
406 depFieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numDependencies);
407 if (depFieldIDs == nullptr) {
408 goto exit;
409 }
410
411 for (size_t i = 0; i < numDependencies; i++) {
Yang Ni281c3252014-10-24 08:52:24 -0700412 depFieldIDs[i] = (RsClosure)jDepFieldIDs[i];
413 }
414
Yang Ni17c2d7a2015-04-30 16:13:54 -0700415 ret = (jlong)(uintptr_t)rsClosureCreate(
Yang Ni281c3252014-10-24 08:52:24 -0700416 (RsContext)con, (RsScriptKernelID)kernelID, (RsAllocation)returnValue,
Yang Ni17c2d7a2015-04-30 16:13:54 -0700417 fieldIDs, numValues, values, numValues,
418 (int*)jSizes, numValues,
419 depClosures, numDependencies,
420 depFieldIDs, numDependencies);
421
422exit:
423
424 _env->ReleaseLongArrayElements(depFieldIDArray, jDepFieldIDs, JNI_ABORT);
425 _env->ReleaseLongArrayElements(depClosureArray, jDepClosures, JNI_ABORT);
426 _env->ReleaseIntArrayElements (sizeArray, jSizes, JNI_ABORT);
427 _env->ReleaseLongArrayElements(valueArray, jValues, JNI_ABORT);
428 _env->ReleaseLongArrayElements(fieldIDArray, jFieldIDs, JNI_ABORT);
429
430 return ret;
Yang Ni281c3252014-10-24 08:52:24 -0700431}
432
Yang Nibe392ad2015-01-23 17:16:02 -0800433static jlong
434nInvokeClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong invokeID,
435 jbyteArray paramArray, jlongArray fieldIDArray, jlongArray valueArray,
436 jintArray sizeArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700437 jlong ret = 0;
438
Yang Nibe392ad2015-01-23 17:16:02 -0800439 jbyte* jParams = _env->GetByteArrayElements(paramArray, nullptr);
440 jsize jParamLength = _env->GetArrayLength(paramArray);
Yang Nibe392ad2015-01-23 17:16:02 -0800441 jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
442 jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
Yang Ni17c2d7a2015-04-30 16:13:54 -0700443 jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
444 jsize values_length = _env->GetArrayLength(valueArray);
445 jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
446 jsize sizes_length = _env->GetArrayLength(sizeArray);
447
448 size_t numValues;
449 RsScriptFieldID* fieldIDs;
450 uintptr_t* values;
451
452 if (fieldIDs_length != values_length || values_length != sizes_length) {
453 ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
454 goto exit;
455 }
456
457 numValues = (size_t) fieldIDs_length;
458
459 if (numValues > kMaxNumberArgsAndBindings) {
460 ALOGE("Too many arguments or globals in closure creation");
461 goto exit;
462 }
463
464 fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
465 if (fieldIDs == nullptr) {
466 goto exit;
467 }
468
469 for (size_t i = 0; i< numValues; i++) {
Yang Nibe392ad2015-01-23 17:16:02 -0800470 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
471 }
472
Yang Ni17c2d7a2015-04-30 16:13:54 -0700473 values = (uintptr_t*)alloca(sizeof(uintptr_t) * numValues);
474 if (values == nullptr) {
475 goto exit;
476 }
477
478 for (size_t i = 0; i < numValues; i++) {
Yang Nibe392ad2015-01-23 17:16:02 -0800479 values[i] = (uintptr_t)jValues[i];
480 }
481
Yang Ni17c2d7a2015-04-30 16:13:54 -0700482 ret = (jlong)(uintptr_t)rsInvokeClosureCreate(
Yang Nibe392ad2015-01-23 17:16:02 -0800483 (RsContext)con, (RsScriptInvokeID)invokeID, jParams, jParamLength,
Yang Ni17c2d7a2015-04-30 16:13:54 -0700484 fieldIDs, numValues, values, numValues,
485 (int*)jSizes, numValues);
486
487exit:
488
489 _env->ReleaseIntArrayElements (sizeArray, jSizes, JNI_ABORT);
490 _env->ReleaseLongArrayElements(valueArray, jValues, JNI_ABORT);
491 _env->ReleaseLongArrayElements(fieldIDArray, jFieldIDs, JNI_ABORT);
492 _env->ReleaseByteArrayElements(paramArray, jParams, JNI_ABORT);
493
494 return ret;
Yang Nibe392ad2015-01-23 17:16:02 -0800495}
496
Yang Ni281c3252014-10-24 08:52:24 -0700497static void
498nClosureSetArg(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
499 jint index, jlong value, jint size) {
500 rsClosureSetArg((RsContext)con, (RsClosure)closureID, (uint32_t)index,
501 (uintptr_t)value, (size_t)size);
502}
503
504static void
505nClosureSetGlobal(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
506 jlong fieldID, jlong value, jint size) {
507 rsClosureSetGlobal((RsContext)con, (RsClosure)closureID,
508 (RsScriptFieldID)fieldID, (uintptr_t)value, (size_t)size);
509}
510
511static long
Yang Ni35be56c2015-04-02 17:47:56 -0700512nScriptGroup2Create(JNIEnv *_env, jobject _this, jlong con, jstring name,
Yang Niebf63402015-01-16 11:06:26 -0800513 jstring cacheDir, jlongArray closureArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700514 jlong ret = 0;
515
Yang Ni35be56c2015-04-02 17:47:56 -0700516 AutoJavaStringToUTF8 nameUTF(_env, name);
Yang Niebf63402015-01-16 11:06:26 -0800517 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
518
Yang Ni281c3252014-10-24 08:52:24 -0700519 jlong* jClosures = _env->GetLongArrayElements(closureArray, nullptr);
520 jsize numClosures = _env->GetArrayLength(closureArray);
Yang Ni17c2d7a2015-04-30 16:13:54 -0700521
522 RsClosure* closures;
523
524 if (numClosures > (jsize) kMaxNumberClosuresInScriptGroup) {
525 ALOGE("Too many closures in script group");
526 goto exit;
527 }
528
529 closures = (RsClosure*)alloca(sizeof(RsClosure) * numClosures);
530 if (closures == nullptr) {
531 goto exit;
532 }
533
Yang Ni281c3252014-10-24 08:52:24 -0700534 for (int i = 0; i < numClosures; i++) {
535 closures[i] = (RsClosure)jClosures[i];
536 }
537
Yang Ni17c2d7a2015-04-30 16:13:54 -0700538 ret = (jlong)(uintptr_t)rsScriptGroup2Create(
Yang Ni35be56c2015-04-02 17:47:56 -0700539 (RsContext)con, nameUTF.c_str(), nameUTF.length(),
540 cacheDirUTF.c_str(), cacheDirUTF.length(),
Yang Niebf63402015-01-16 11:06:26 -0800541 closures, numClosures);
Yang Ni17c2d7a2015-04-30 16:13:54 -0700542
543exit:
544
545 _env->ReleaseLongArrayElements(closureArray, jClosures, JNI_ABORT);
546
547 return ret;
Yang Ni281c3252014-10-24 08:52:24 -0700548}
549
550static void
551nScriptGroup2Execute(JNIEnv *_env, jobject _this, jlong con, jlong groupID) {
552 rsScriptGroupExecute((RsContext)con, (RsScriptGroup2)groupID);
553}
554
Jason Sams96ed4cf2010-06-15 12:15:57 -0700555static void
Tim Murray25207df2015-01-12 16:47:56 -0800556nScriptIntrinsicBLAS_Single(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
557 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
558 jfloat alpha, jlong A, jlong B, jfloat beta, jlong C, jint incX, jint incY,
559 jint KL, jint KU) {
560 RsBlasCall call;
561 memset(&call, 0, sizeof(call));
562 call.func = (RsBlasFunction)func;
563 call.transA = (RsBlasTranspose)TransA;
564 call.transB = (RsBlasTranspose)TransB;
565 call.side = (RsBlasSide)Side;
566 call.uplo = (RsBlasUplo)Uplo;
567 call.diag = (RsBlasDiag)Diag;
568 call.M = M;
569 call.N = N;
570 call.K = K;
571 call.alpha.f = alpha;
572 call.beta.f = beta;
573 call.incX = incX;
574 call.incY = incY;
575 call.KL = KL;
576 call.KU = KU;
577
578 RsAllocation in_allocs[3];
579 in_allocs[0] = (RsAllocation)A;
580 in_allocs[1] = (RsAllocation)B;
581 in_allocs[2] = (RsAllocation)C;
582
583 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
584 in_allocs, sizeof(in_allocs), nullptr,
585 &call, sizeof(call), nullptr, 0);
586}
587
588static void
589nScriptIntrinsicBLAS_Double(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
590 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
591 jdouble alpha, jlong A, jlong B, jdouble beta, jlong C, jint incX, jint incY,
592 jint KL, jint KU) {
593 RsBlasCall call;
594 memset(&call, 0, sizeof(call));
595 call.func = (RsBlasFunction)func;
596 call.transA = (RsBlasTranspose)TransA;
597 call.transB = (RsBlasTranspose)TransB;
598 call.side = (RsBlasSide)Side;
599 call.uplo = (RsBlasUplo)Uplo;
600 call.diag = (RsBlasDiag)Diag;
601 call.M = M;
602 call.N = N;
603 call.K = K;
604 call.alpha.d = alpha;
605 call.beta.d = beta;
606 call.incX = incX;
607 call.incY = incY;
608 call.KL = KL;
609 call.KU = KU;
610
611 RsAllocation in_allocs[3];
612 in_allocs[0] = (RsAllocation)A;
613 in_allocs[1] = (RsAllocation)B;
614 in_allocs[2] = (RsAllocation)C;
615
616 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
617 in_allocs, sizeof(in_allocs), nullptr,
618 &call, sizeof(call), nullptr, 0);
619}
620
621static void
622nScriptIntrinsicBLAS_Complex(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
623 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
624 jfloat alphaX, jfloat alphaY, jlong A, jlong B, jfloat betaX,
625 jfloat betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
626 RsBlasCall call;
627 memset(&call, 0, sizeof(call));
628 call.func = (RsBlasFunction)func;
629 call.transA = (RsBlasTranspose)TransA;
630 call.transB = (RsBlasTranspose)TransB;
631 call.side = (RsBlasSide)Side;
632 call.uplo = (RsBlasUplo)Uplo;
633 call.diag = (RsBlasDiag)Diag;
634 call.M = M;
635 call.N = N;
636 call.K = K;
637 call.alpha.c.r = alphaX;
638 call.alpha.c.i = alphaY;
639 call.beta.c.r = betaX;
Miao Wang82585b32015-04-30 13:44:49 -0700640 call.beta.c.i = betaY;
Tim Murray25207df2015-01-12 16:47:56 -0800641 call.incX = incX;
642 call.incY = incY;
643 call.KL = KL;
644 call.KU = KU;
645
646 RsAllocation in_allocs[3];
647 in_allocs[0] = (RsAllocation)A;
648 in_allocs[1] = (RsAllocation)B;
649 in_allocs[2] = (RsAllocation)C;
650
651 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
652 in_allocs, sizeof(in_allocs), nullptr,
653 &call, sizeof(call), nullptr, 0);
654}
655
656static void
657nScriptIntrinsicBLAS_Z(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
658 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
659 jdouble alphaX, jdouble alphaY, jlong A, jlong B, jdouble betaX,
660 jdouble betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
661 RsBlasCall call;
662 memset(&call, 0, sizeof(call));
663 call.func = (RsBlasFunction)func;
664 call.transA = (RsBlasTranspose)TransA;
665 call.transB = (RsBlasTranspose)TransB;
666 call.side = (RsBlasSide)Side;
667 call.uplo = (RsBlasUplo)Uplo;
668 call.diag = (RsBlasDiag)Diag;
669 call.M = M;
670 call.N = N;
671 call.K = K;
672 call.alpha.z.r = alphaX;
673 call.alpha.z.i = alphaY;
674 call.beta.z.r = betaX;
Miao Wang82585b32015-04-30 13:44:49 -0700675 call.beta.z.i = betaY;
Tim Murray25207df2015-01-12 16:47:56 -0800676 call.incX = incX;
677 call.incY = incY;
678 call.KL = KL;
679 call.KU = KU;
680
681 RsAllocation in_allocs[3];
682 in_allocs[0] = (RsAllocation)A;
683 in_allocs[1] = (RsAllocation)B;
684 in_allocs[2] = (RsAllocation)C;
685
686 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
687 in_allocs, sizeof(in_allocs), nullptr,
688 &call, sizeof(call), nullptr, 0);
689}
690
691
692static void
Tim Murray9cb16a22015-04-01 11:07:16 -0700693nScriptIntrinsicBLAS_BNNM(JNIEnv *_env, jobject _this, jlong con, jlong id, jint M, jint N, jint K,
694 jlong A, jint a_offset, jlong B, jint b_offset, jlong C, jint c_offset,
695 jint c_mult_int) {
696 RsBlasCall call;
697 memset(&call, 0, sizeof(call));
698 call.func = RsBlas_bnnm;
699 call.M = M;
700 call.N = N;
701 call.K = K;
702 call.a_offset = a_offset;
703 call.b_offset = b_offset;
704 call.c_offset = c_offset;
705 call.c_mult_int = c_mult_int;
706
707 RsAllocation in_allocs[3];
708 in_allocs[0] = (RsAllocation)A;
709 in_allocs[1] = (RsAllocation)B;
710 in_allocs[2] = (RsAllocation)C;
711
712 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
713 in_allocs, sizeof(in_allocs), nullptr,
714 &call, sizeof(call), nullptr, 0);
715}
716
717
718static void
Tim Murray460a0492013-11-19 12:45:54 -0800719nAssignName(JNIEnv *_env, jobject _this, jlong con, jlong obj, jbyteArray str)
Jason Sams3eaa338e2009-06-10 15:04:38 -0700720{
Andreas Gampe67333922014-11-10 20:35:59 -0800721 if (kLogApi) {
722 ALOGD("nAssignName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
723 }
Jason Sams3eaa338e2009-06-10 15:04:38 -0700724 jint len = _env->GetArrayLength(str);
725 jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
Tim Murrayeff663f2013-11-15 13:08:30 -0800726 rsAssignName((RsContext)con, (void *)obj, (const char *)cptr, len);
Jason Sams3eaa338e2009-06-10 15:04:38 -0700727 _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
728}
729
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700730static jstring
Tim Murray460a0492013-11-19 12:45:54 -0800731nGetName(JNIEnv *_env, jobject _this, jlong con, jlong obj)
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700732{
Andreas Gampe67333922014-11-10 20:35:59 -0800733 if (kLogApi) {
734 ALOGD("nGetName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
735 }
Chris Wailes488230c32014-08-14 11:22:40 -0700736 const char *name = nullptr;
Tim Murrayeff663f2013-11-15 13:08:30 -0800737 rsaGetName((RsContext)con, (void *)obj, &name);
Chris Wailes488230c32014-08-14 11:22:40 -0700738 if(name == nullptr || strlen(name) == 0) {
739 return nullptr;
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700740 }
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700741 return _env->NewStringUTF(name);
742}
743
Jason Sams7ce033d2009-08-18 14:14:24 -0700744static void
Tim Murray460a0492013-11-19 12:45:54 -0800745nObjDestroy(JNIEnv *_env, jobject _this, jlong con, jlong obj)
Jason Sams7ce033d2009-08-18 14:14:24 -0700746{
Andreas Gampe67333922014-11-10 20:35:59 -0800747 if (kLogApi) {
748 ALOGD("nObjDestroy, con(%p) obj(%p)", (RsContext)con, (void *)obj);
749 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800750 rsObjDestroy((RsContext)con, (void *)obj);
Jason Sams7ce033d2009-08-18 14:14:24 -0700751}
752
Jason Sams3eaa338e2009-06-10 15:04:38 -0700753// ---------------------------------------------------------------------------
754
Tim Murrayeff663f2013-11-15 13:08:30 -0800755static jlong
Jason Samsd19f10d2009-05-22 14:03:28 -0700756nDeviceCreate(JNIEnv *_env, jobject _this)
757{
Andreas Gampe67333922014-11-10 20:35:59 -0800758 if (kLogApi) {
759 ALOGD("nDeviceCreate");
760 }
Tim Murray3aa89c12014-08-18 17:51:22 -0700761 return (jlong)(uintptr_t)rsDeviceCreate();
Jason Samsd19f10d2009-05-22 14:03:28 -0700762}
763
764static void
Tim Murray5eaf4682014-01-10 11:25:52 -0800765nDeviceDestroy(JNIEnv *_env, jobject _this, jlong dev)
Jason Samsd19f10d2009-05-22 14:03:28 -0700766{
Andreas Gampe67333922014-11-10 20:35:59 -0800767 if (kLogApi) {
768 ALOGD("nDeviceDestroy");
769 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700770 return rsDeviceDestroy((RsDevice)dev);
771}
772
Jason Samsebfb4362009-09-23 13:57:02 -0700773static void
Tim Murray5eaf4682014-01-10 11:25:52 -0800774nDeviceSetConfig(JNIEnv *_env, jobject _this, jlong dev, jint p, jint value)
Jason Samsebfb4362009-09-23 13:57:02 -0700775{
Andreas Gampe67333922014-11-10 20:35:59 -0800776 if (kLogApi) {
777 ALOGD("nDeviceSetConfig dev(%p), param(%i), value(%i)", (void *)dev, p, value);
778 }
Jason Samsebfb4362009-09-23 13:57:02 -0700779 return rsDeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value);
780}
781
Tim Murrayeff663f2013-11-15 13:08:30 -0800782static jlong
Jason Sams81cd2b12014-12-02 12:36:43 -0800783nContextCreate(JNIEnv *_env, jobject _this, jlong dev, jint flags, jint sdkVer, jint contextType)
Jason Samsd19f10d2009-05-22 14:03:28 -0700784{
Andreas Gampe67333922014-11-10 20:35:59 -0800785 if (kLogApi) {
786 ALOGD("nContextCreate");
787 }
Jason Sams81cd2b12014-12-02 12:36:43 -0800788 return (jlong)(uintptr_t)rsContextCreate((RsDevice)dev, 0, sdkVer, (RsContextType)contextType, flags);
Jason Sams704ff642010-02-09 16:05:07 -0800789}
790
Tim Murrayeff663f2013-11-15 13:08:30 -0800791static jlong
Tim Murray5eaf4682014-01-10 11:25:52 -0800792nContextCreateGL(JNIEnv *_env, jobject _this, jlong dev, jint ver, jint sdkVer,
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000793 jint colorMin, jint colorPref,
794 jint alphaMin, jint alphaPref,
795 jint depthMin, jint depthPref,
796 jint stencilMin, jint stencilPref,
797 jint samplesMin, jint samplesPref, jfloat samplesQ,
798 jint dpi)
Jason Sams704ff642010-02-09 16:05:07 -0800799{
Jason Sams11c8af92010-10-13 15:31:10 -0700800 RsSurfaceConfig sc;
801 sc.alphaMin = alphaMin;
802 sc.alphaPref = alphaPref;
803 sc.colorMin = colorMin;
804 sc.colorPref = colorPref;
805 sc.depthMin = depthMin;
806 sc.depthPref = depthPref;
807 sc.samplesMin = samplesMin;
808 sc.samplesPref = samplesPref;
809 sc.samplesQ = samplesQ;
810
Andreas Gampe67333922014-11-10 20:35:59 -0800811 if (kLogApi) {
812 ALOGD("nContextCreateGL");
813 }
Tim Murray3aa89c12014-08-18 17:51:22 -0700814 return (jlong)(uintptr_t)rsContextCreateGL((RsDevice)dev, ver, sdkVer, sc, dpi);
Jason Samsd19f10d2009-05-22 14:03:28 -0700815}
816
817static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800818nContextSetPriority(JNIEnv *_env, jobject _this, jlong con, jint p)
Jason Sams7d787b42009-11-15 12:14:26 -0800819{
Andreas Gampe67333922014-11-10 20:35:59 -0800820 if (kLogApi) {
821 ALOGD("ContextSetPriority, con(%p), priority(%i)", (RsContext)con, p);
822 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800823 rsContextSetPriority((RsContext)con, p);
Jason Sams7d787b42009-11-15 12:14:26 -0800824}
825
Tim Murray47f31582015-04-07 15:43:24 -0700826static void
827nContextSetCacheDir(JNIEnv *_env, jobject _this, jlong con, jstring cacheDir)
828{
829 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
830
831 if (kLogApi) {
832 ALOGD("ContextSetCacheDir, con(%p), cacheDir(%s)", (RsContext)con, cacheDirUTF.c_str());
833 }
834 rsContextSetCacheDir((RsContext)con, cacheDirUTF.c_str(), cacheDirUTF.length());
835}
836
Jason Sams7d787b42009-11-15 12:14:26 -0800837
838
839static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800840nContextSetSurface(JNIEnv *_env, jobject _this, jlong con, jint width, jint height, jobject wnd)
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800841{
Andreas Gampe67333922014-11-10 20:35:59 -0800842 if (kLogApi) {
843 ALOGD("nContextSetSurface, con(%p), width(%i), height(%i), surface(%p)", (RsContext)con,
844 width, height, (Surface *)wnd);
845 }
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800846
Chris Wailes488230c32014-08-14 11:22:40 -0700847 ANativeWindow * window = nullptr;
848 if (wnd == nullptr) {
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800849
850 } else {
Jeff Brown64a55af2012-08-26 02:47:39 -0700851 window = android_view_Surface_getNativeWindow(_env, wnd).get();
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800852 }
853
Tim Murrayeff663f2013-11-15 13:08:30 -0800854 rsContextSetSurface((RsContext)con, width, height, window);
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800855}
856
857static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800858nContextDestroy(JNIEnv *_env, jobject _this, jlong con)
Jason Samsd19f10d2009-05-22 14:03:28 -0700859{
Andreas Gampe67333922014-11-10 20:35:59 -0800860 if (kLogApi) {
861 ALOGD("nContextDestroy, con(%p)", (RsContext)con);
862 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800863 rsContextDestroy((RsContext)con);
Jason Samsd19f10d2009-05-22 14:03:28 -0700864}
865
Jason Sams715333b2009-11-17 17:26:46 -0800866static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800867nContextDump(JNIEnv *_env, jobject _this, jlong con, jint bits)
Jason Sams715333b2009-11-17 17:26:46 -0800868{
Andreas Gampe67333922014-11-10 20:35:59 -0800869 if (kLogApi) {
870 ALOGD("nContextDump, con(%p) bits(%i)", (RsContext)con, bits);
871 }
Jason Sams715333b2009-11-17 17:26:46 -0800872 rsContextDump((RsContext)con, bits);
873}
Jason Samsd19f10d2009-05-22 14:03:28 -0700874
875static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800876nContextPause(JNIEnv *_env, jobject _this, jlong con)
Jason Sams65e7aa52009-09-24 17:38:20 -0700877{
Andreas Gampe67333922014-11-10 20:35:59 -0800878 if (kLogApi) {
879 ALOGD("nContextPause, con(%p)", (RsContext)con);
880 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800881 rsContextPause((RsContext)con);
Jason Sams65e7aa52009-09-24 17:38:20 -0700882}
883
884static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800885nContextResume(JNIEnv *_env, jobject _this, jlong con)
Jason Sams65e7aa52009-09-24 17:38:20 -0700886{
Andreas Gampe67333922014-11-10 20:35:59 -0800887 if (kLogApi) {
888 ALOGD("nContextResume, con(%p)", (RsContext)con);
889 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800890 rsContextResume((RsContext)con);
Jason Sams65e7aa52009-09-24 17:38:20 -0700891}
892
Jason Sams1c415172010-11-08 17:06:46 -0800893
894static jstring
Tim Murrayeff663f2013-11-15 13:08:30 -0800895nContextGetErrorMessage(JNIEnv *_env, jobject _this, jlong con)
Jason Sams1c415172010-11-08 17:06:46 -0800896{
Andreas Gampe67333922014-11-10 20:35:59 -0800897 if (kLogApi) {
898 ALOGD("nContextGetErrorMessage, con(%p)", (RsContext)con);
899 }
Jason Sams1c415172010-11-08 17:06:46 -0800900 char buf[1024];
901
902 size_t receiveLen;
903 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800904 int id = rsContextGetMessage((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -0700905 buf, sizeof(buf),
906 &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -0700907 &subID, sizeof(subID));
Jason Sams1c415172010-11-08 17:06:46 -0800908 if (!id && receiveLen) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +0100909 ALOGV("message receive buffer too small. %zu", receiveLen);
Jason Sams1c415172010-11-08 17:06:46 -0800910 }
911 return _env->NewStringUTF(buf);
912}
913
Jason Samsedbfabd2011-05-17 15:01:29 -0700914static jint
Tim Murrayeff663f2013-11-15 13:08:30 -0800915nContextGetUserMessage(JNIEnv *_env, jobject _this, jlong con, jintArray data)
Jason Sams516c3192009-10-06 13:58:47 -0700916{
Jason Sams516c3192009-10-06 13:58:47 -0700917 jint len = _env->GetArrayLength(data);
Andreas Gampe67333922014-11-10 20:35:59 -0800918 if (kLogApi) {
919 ALOGD("nContextGetMessage, con(%p), len(%i)", (RsContext)con, len);
920 }
Chris Wailes488230c32014-08-14 11:22:40 -0700921 jint *ptr = _env->GetIntArrayElements(data, nullptr);
Jason Sams516c3192009-10-06 13:58:47 -0700922 size_t receiveLen;
Jason Sams1c415172010-11-08 17:06:46 -0800923 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800924 int id = rsContextGetMessage((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -0700925 ptr, len * 4,
926 &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -0700927 &subID, sizeof(subID));
Jason Sams516c3192009-10-06 13:58:47 -0700928 if (!id && receiveLen) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +0100929 ALOGV("message receive buffer too small. %zu", receiveLen);
Jason Sams516c3192009-10-06 13:58:47 -0700930 }
931 _env->ReleaseIntArrayElements(data, ptr, 0);
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000932 return (jint)id;
Jason Sams1c415172010-11-08 17:06:46 -0800933}
934
935static jint
Tim Murrayeff663f2013-11-15 13:08:30 -0800936nContextPeekMessage(JNIEnv *_env, jobject _this, jlong con, jintArray auxData)
Jason Sams1c415172010-11-08 17:06:46 -0800937{
Andreas Gampe67333922014-11-10 20:35:59 -0800938 if (kLogApi) {
939 ALOGD("nContextPeekMessage, con(%p)", (RsContext)con);
940 }
Chris Wailes488230c32014-08-14 11:22:40 -0700941 jint *auxDataPtr = _env->GetIntArrayElements(auxData, nullptr);
Jason Sams1c415172010-11-08 17:06:46 -0800942 size_t receiveLen;
943 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800944 int id = rsContextPeekMessage((RsContext)con, &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -0700945 &subID, sizeof(subID));
Jason Sams1c415172010-11-08 17:06:46 -0800946 auxDataPtr[0] = (jint)subID;
947 auxDataPtr[1] = (jint)receiveLen;
948 _env->ReleaseIntArrayElements(auxData, auxDataPtr, 0);
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000949 return (jint)id;
Jason Sams516c3192009-10-06 13:58:47 -0700950}
951
Tim Murrayeff663f2013-11-15 13:08:30 -0800952static void nContextInitToClient(JNIEnv *_env, jobject _this, jlong con)
Jason Sams516c3192009-10-06 13:58:47 -0700953{
Andreas Gampe67333922014-11-10 20:35:59 -0800954 if (kLogApi) {
955 ALOGD("nContextInitToClient, con(%p)", (RsContext)con);
956 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800957 rsContextInitToClient((RsContext)con);
Jason Sams516c3192009-10-06 13:58:47 -0700958}
959
Tim Murrayeff663f2013-11-15 13:08:30 -0800960static void nContextDeinitToClient(JNIEnv *_env, jobject _this, jlong con)
Jason Sams516c3192009-10-06 13:58:47 -0700961{
Andreas Gampe67333922014-11-10 20:35:59 -0800962 if (kLogApi) {
963 ALOGD("nContextDeinitToClient, con(%p)", (RsContext)con);
964 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800965 rsContextDeinitToClient((RsContext)con);
Jason Sams516c3192009-10-06 13:58:47 -0700966}
967
Jason Sams455d6442013-02-05 19:20:18 -0800968static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800969nContextSendMessage(JNIEnv *_env, jobject _this, jlong con, jint id, jintArray data)
Jason Sams455d6442013-02-05 19:20:18 -0800970{
Chris Wailes488230c32014-08-14 11:22:40 -0700971 jint *ptr = nullptr;
Jason Sams455d6442013-02-05 19:20:18 -0800972 jint len = 0;
973 if (data) {
974 len = _env->GetArrayLength(data);
Stephen Hines4a043c12014-08-21 23:20:32 -0700975 ptr = _env->GetIntArrayElements(data, nullptr);
Jason Sams455d6442013-02-05 19:20:18 -0800976 }
Andreas Gampe67333922014-11-10 20:35:59 -0800977 if (kLogApi) {
978 ALOGD("nContextSendMessage, con(%p), id(%i), len(%i)", (RsContext)con, id, len);
979 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800980 rsContextSendMessage((RsContext)con, id, (const uint8_t *)ptr, len * sizeof(int));
Jason Sams455d6442013-02-05 19:20:18 -0800981 if (data) {
982 _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
983 }
984}
985
986
Jason Sams516c3192009-10-06 13:58:47 -0700987
Tim Murray460a0492013-11-19 12:45:54 -0800988static jlong
Andreas Gampe67333922014-11-10 20:35:59 -0800989nElementCreate(JNIEnv *_env, jobject _this, jlong con, jlong type, jint kind, jboolean norm,
990 jint size)
Jason Samsd19f10d2009-05-22 14:03:28 -0700991{
Andreas Gampe67333922014-11-10 20:35:59 -0800992 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +0100993 ALOGD("nElementCreate, con(%p), type(%" PRId64 "), kind(%i), norm(%i), size(%i)", (RsContext)con,
Andreas Gampe67333922014-11-10 20:35:59 -0800994 type, kind, norm, size);
995 }
996 return (jlong)(uintptr_t)rsElementCreate((RsContext)con, (RsDataType)type, (RsDataKind)kind,
997 norm, size);
Jason Samsd19f10d2009-05-22 14:03:28 -0700998}
999
Tim Murray460a0492013-11-19 12:45:54 -08001000static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001001nElementCreate2(JNIEnv *_env, jobject _this, jlong con,
Ashok Bhat98071552014-02-12 09:54:43 +00001002 jlongArray _ids, jobjectArray _names, jintArray _arraySizes)
Jason Samsd19f10d2009-05-22 14:03:28 -07001003{
Jason Sams718cd1f2009-12-23 14:35:29 -08001004 int fieldCount = _env->GetArrayLength(_ids);
Andreas Gampe67333922014-11-10 20:35:59 -08001005 if (kLogApi) {
1006 ALOGD("nElementCreate2, con(%p)", (RsContext)con);
1007 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001008
Chris Wailes488230c32014-08-14 11:22:40 -07001009 jlong *jIds = _env->GetLongArrayElements(_ids, nullptr);
1010 jint *jArraySizes = _env->GetIntArrayElements(_arraySizes, nullptr);
Ashok Bhat98071552014-02-12 09:54:43 +00001011
1012 RsElement *ids = (RsElement*)malloc(fieldCount * sizeof(RsElement));
1013 uint32_t *arraySizes = (uint32_t *)malloc(fieldCount * sizeof(uint32_t));
1014
1015 for(int i = 0; i < fieldCount; i ++) {
1016 ids[i] = (RsElement)jIds[i];
1017 arraySizes[i] = (uint32_t)jArraySizes[i];
1018 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001019
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08001020 AutoJavaStringArrayToUTF8 names(_env, _names, fieldCount);
1021
1022 const char **nameArray = names.c_str();
1023 size_t *sizeArray = names.c_str_len();
1024
Tim Murray3aa89c12014-08-18 17:51:22 -07001025 jlong id = (jlong)(uintptr_t)rsElementCreate2((RsContext)con,
Ashok Bhat98071552014-02-12 09:54:43 +00001026 (const RsElement *)ids, fieldCount,
Jason Sams7a22e102011-05-06 14:14:30 -07001027 nameArray, fieldCount * sizeof(size_t), sizeArray,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001028 (const uint32_t *)arraySizes, fieldCount);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08001029
Ashok Bhat98071552014-02-12 09:54:43 +00001030 free(ids);
1031 free(arraySizes);
1032 _env->ReleaseLongArrayElements(_ids, jIds, JNI_ABORT);
1033 _env->ReleaseIntArrayElements(_arraySizes, jArraySizes, JNI_ABORT);
1034
Tim Murray3aa89c12014-08-18 17:51:22 -07001035 return (jlong)(uintptr_t)id;
Jason Samsd19f10d2009-05-22 14:03:28 -07001036}
1037
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001038static void
Tim Murray460a0492013-11-19 12:45:54 -08001039nElementGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jintArray _elementData)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001040{
1041 int dataSize = _env->GetArrayLength(_elementData);
Andreas Gampe67333922014-11-10 20:35:59 -08001042 if (kLogApi) {
1043 ALOGD("nElementGetNativeData, con(%p)", (RsContext)con);
1044 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001045
1046 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
1047 assert(dataSize == 5);
1048
Narayan Kamath78c0ce52014-03-19 10:15:51 +00001049 uintptr_t elementData[5];
Tim Murrayeff663f2013-11-15 13:08:30 -08001050 rsaElementGetNativeData((RsContext)con, (RsElement)id, elementData, dataSize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001051
1052 for(jint i = 0; i < dataSize; i ++) {
Ashok Bhat98071552014-02-12 09:54:43 +00001053 const jint data = (jint)elementData[i];
1054 _env->SetIntArrayRegion(_elementData, i, 1, &data);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001055 }
1056}
1057
1058
1059static void
Tim Murray460a0492013-11-19 12:45:54 -08001060nElementGetSubElements(JNIEnv *_env, jobject _this, jlong con, jlong id,
Ashok Bhat98071552014-02-12 09:54:43 +00001061 jlongArray _IDs,
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -07001062 jobjectArray _names,
1063 jintArray _arraySizes)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001064{
Ashok Bhat98071552014-02-12 09:54:43 +00001065 uint32_t dataSize = _env->GetArrayLength(_IDs);
Andreas Gampe67333922014-11-10 20:35:59 -08001066 if (kLogApi) {
1067 ALOGD("nElementGetSubElements, con(%p)", (RsContext)con);
1068 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001069
Ashok Bhat98071552014-02-12 09:54:43 +00001070 uintptr_t *ids = (uintptr_t*)malloc(dataSize * sizeof(uintptr_t));
1071 const char **names = (const char **)malloc(dataSize * sizeof(const char *));
Narayan Kamath78c0ce52014-03-19 10:15:51 +00001072 uint32_t *arraySizes = (uint32_t *)malloc(dataSize * sizeof(uint32_t));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001073
Andreas Gampe67333922014-11-10 20:35:59 -08001074 rsaElementGetSubElements((RsContext)con, (RsElement)id, ids, names, arraySizes,
1075 (uint32_t)dataSize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001076
Ashok Bhat98071552014-02-12 09:54:43 +00001077 for(uint32_t i = 0; i < dataSize; i++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07001078 const jlong id = (jlong)(uintptr_t)ids[i];
Ashok Bhat98071552014-02-12 09:54:43 +00001079 const jint arraySize = (jint)arraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001080 _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
Ashok Bhat98071552014-02-12 09:54:43 +00001081 _env->SetLongArrayRegion(_IDs, i, 1, &id);
1082 _env->SetIntArrayRegion(_arraySizes, i, 1, &arraySize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001083 }
1084
1085 free(ids);
1086 free(names);
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -07001087 free(arraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001088}
1089
Jason Samsd19f10d2009-05-22 14:03:28 -07001090// -----------------------------------
1091
Tim Murray460a0492013-11-19 12:45:54 -08001092static jlong
1093nTypeCreate(JNIEnv *_env, jobject _this, jlong con, jlong eid,
Jason Samsb109cc72013-01-07 18:20:12 -08001094 jint dimx, jint dimy, jint dimz, jboolean mips, jboolean faces, jint yuv)
Jason Samsd19f10d2009-05-22 14:03:28 -07001095{
Andreas Gampe67333922014-11-10 20:35:59 -08001096 if (kLogApi) {
1097 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 +01001098 (RsContext)con, (void*)eid, dimx, dimy, dimz, mips, faces, yuv);
Andreas Gampe67333922014-11-10 20:35:59 -08001099 }
Jason Sams3b9c52a2010-10-14 17:48:46 -07001100
Andreas Gampe67333922014-11-10 20:35:59 -08001101 return (jlong)(uintptr_t)rsTypeCreate((RsContext)con, (RsElement)eid, dimx, dimy, dimz, mips,
1102 faces, yuv);
Jason Samsd19f10d2009-05-22 14:03:28 -07001103}
1104
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001105static void
Ashok Bhat98071552014-02-12 09:54:43 +00001106nTypeGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jlongArray _typeData)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001107{
1108 // We are packing 6 items: mDimX; mDimY; mDimZ;
1109 // mDimLOD; mDimFaces; mElement; into typeData
1110 int elementCount = _env->GetArrayLength(_typeData);
1111
1112 assert(elementCount == 6);
Andreas Gampe67333922014-11-10 20:35:59 -08001113 if (kLogApi) {
1114 ALOGD("nTypeGetNativeData, con(%p)", (RsContext)con);
1115 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001116
Ashok Bhat98071552014-02-12 09:54:43 +00001117 uintptr_t typeData[6];
Tim Murrayeff663f2013-11-15 13:08:30 -08001118 rsaTypeGetNativeData((RsContext)con, (RsType)id, typeData, 6);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001119
1120 for(jint i = 0; i < elementCount; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07001121 const jlong data = (jlong)(uintptr_t)typeData[i];
Ashok Bhat98071552014-02-12 09:54:43 +00001122 _env->SetLongArrayRegion(_typeData, i, 1, &data);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001123 }
1124}
1125
Jason Samsd19f10d2009-05-22 14:03:28 -07001126// -----------------------------------
1127
Tim Murray460a0492013-11-19 12:45:54 -08001128static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001129nAllocationCreateTyped(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mips, jint usage,
1130 jlong pointer)
Jason Samsd19f10d2009-05-22 14:03:28 -07001131{
Andreas Gampe67333922014-11-10 20:35:59 -08001132 if (kLogApi) {
1133 ALOGD("nAllocationCreateTyped, con(%p), type(%p), mip(%i), usage(%i), ptr(%p)",
1134 (RsContext)con, (RsElement)type, mips, usage, (void *)pointer);
1135 }
1136 return (jlong)(uintptr_t) rsAllocationCreateTyped((RsContext)con, (RsType)type,
1137 (RsAllocationMipmapControl)mips,
1138 (uint32_t)usage, (uintptr_t)pointer);
Jason Samsd19f10d2009-05-22 14:03:28 -07001139}
1140
Jason Samsd19f10d2009-05-22 14:03:28 -07001141static void
Tim Murray460a0492013-11-19 12:45:54 -08001142nAllocationSyncAll(JNIEnv *_env, jobject _this, jlong con, jlong a, jint bits)
Jason Sams5476b452010-12-08 16:14:36 -08001143{
Andreas Gampe67333922014-11-10 20:35:59 -08001144 if (kLogApi) {
1145 ALOGD("nAllocationSyncAll, con(%p), a(%p), bits(0x%08x)", (RsContext)con, (RsAllocation)a,
1146 bits);
1147 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001148 rsAllocationSyncAll((RsContext)con, (RsAllocation)a, (RsAllocationUsageType)bits);
Jason Sams5476b452010-12-08 16:14:36 -08001149}
1150
Jason Sams72226e02013-02-22 12:45:54 -08001151static jobject
Tim Murray460a0492013-11-19 12:45:54 -08001152nAllocationGetSurface(JNIEnv *_env, jobject _this, jlong con, jlong a)
Jason Sams615e7ce2012-01-13 14:01:20 -08001153{
Andreas Gampe67333922014-11-10 20:35:59 -08001154 if (kLogApi) {
1155 ALOGD("nAllocationGetSurface, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1156 }
Jason Sams615e7ce2012-01-13 14:01:20 -08001157
Andreas Gampe67333922014-11-10 20:35:59 -08001158 IGraphicBufferProducer *v = (IGraphicBufferProducer *)rsAllocationGetSurface((RsContext)con,
1159 (RsAllocation)a);
Jason Sams72226e02013-02-22 12:45:54 -08001160 sp<IGraphicBufferProducer> bp = v;
Chris Wailes488230c32014-08-14 11:22:40 -07001161 v->decStrong(nullptr);
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001162
Jason Sams72226e02013-02-22 12:45:54 -08001163 jobject o = android_view_Surface_createFromIGraphicBufferProducer(_env, bp);
1164 return o;
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001165}
1166
1167static void
Tim Murray460a0492013-11-19 12:45:54 -08001168nAllocationSetSurface(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject sur)
Jason Sams163766c2012-02-15 12:04:24 -08001169{
Andreas Gampe67333922014-11-10 20:35:59 -08001170 if (kLogApi) {
1171 ALOGD("nAllocationSetSurface, con(%p), alloc(%p), surface(%p)", (RsContext)con,
1172 (RsAllocation)alloc, (Surface *)sur);
1173 }
Jason Sams163766c2012-02-15 12:04:24 -08001174
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001175 sp<Surface> s;
Jason Sams163766c2012-02-15 12:04:24 -08001176 if (sur != 0) {
Jeff Brown64a55af2012-08-26 02:47:39 -07001177 s = android_view_Surface_getSurface(_env, sur);
Jason Sams163766c2012-02-15 12:04:24 -08001178 }
1179
Andreas Gampe67333922014-11-10 20:35:59 -08001180 rsAllocationSetSurface((RsContext)con, (RsAllocation)alloc,
1181 static_cast<ANativeWindow *>(s.get()));
Jason Sams163766c2012-02-15 12:04:24 -08001182}
1183
1184static void
Tim Murray460a0492013-11-19 12:45:54 -08001185nAllocationIoSend(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Sams163766c2012-02-15 12:04:24 -08001186{
Andreas Gampe67333922014-11-10 20:35:59 -08001187 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001188 ALOGD("nAllocationIoSend, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
Andreas Gampe67333922014-11-10 20:35:59 -08001189 }
Tim Murray460a0492013-11-19 12:45:54 -08001190 rsAllocationIoSend((RsContext)con, (RsAllocation)alloc);
Jason Sams163766c2012-02-15 12:04:24 -08001191}
1192
1193static void
Tim Murray460a0492013-11-19 12:45:54 -08001194nAllocationIoReceive(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Sams163766c2012-02-15 12:04:24 -08001195{
Andreas Gampe67333922014-11-10 20:35:59 -08001196 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001197 ALOGD("nAllocationIoReceive, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
Andreas Gampe67333922014-11-10 20:35:59 -08001198 }
Tim Murray460a0492013-11-19 12:45:54 -08001199 rsAllocationIoReceive((RsContext)con, (RsAllocation)alloc);
Jason Sams163766c2012-02-15 12:04:24 -08001200}
1201
1202
1203static void
Tim Murray460a0492013-11-19 12:45:54 -08001204nAllocationGenerateMipmaps(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Samsf7086092011-01-12 13:28:37 -08001205{
Andreas Gampe67333922014-11-10 20:35:59 -08001206 if (kLogApi) {
1207 ALOGD("nAllocationGenerateMipmaps, con(%p), a(%p)", (RsContext)con, (RsAllocation)alloc);
1208 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001209 rsAllocationGenerateMipmaps((RsContext)con, (RsAllocation)alloc);
Jason Samsf7086092011-01-12 13:28:37 -08001210}
1211
Tim Murray460a0492013-11-19 12:45:54 -08001212static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001213nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1214 jobject jbitmap, jint usage)
Jason Samsffe9f482009-06-01 17:45:53 -07001215{
John Recked207b92015-04-10 13:52:57 -07001216 SkBitmap bitmap;
1217 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Samsffe9f482009-06-01 17:45:53 -07001218
Jason Sams5476b452010-12-08 16:14:36 -08001219 bitmap.lockPixels();
1220 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001221 jlong id = (jlong)(uintptr_t)rsAllocationCreateFromBitmap((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001222 (RsType)type, (RsAllocationMipmapControl)mip,
1223 ptr, bitmap.getSize(), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001224 bitmap.unlockPixels();
1225 return id;
Jason Samsffe9f482009-06-01 17:45:53 -07001226}
Jason Samsfe08d992009-05-27 14:45:32 -07001227
Tim Murray460a0492013-11-19 12:45:54 -08001228static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001229nAllocationCreateBitmapBackedAllocation(JNIEnv *_env, jobject _this, jlong con, jlong type,
1230 jint mip, jobject jbitmap, jint usage)
Tim Murraya3145512012-12-04 17:59:29 -08001231{
John Recked207b92015-04-10 13:52:57 -07001232 SkBitmap bitmap;
1233 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Tim Murraya3145512012-12-04 17:59:29 -08001234
1235 bitmap.lockPixels();
1236 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001237 jlong id = (jlong)(uintptr_t)rsAllocationCreateTyped((RsContext)con,
Tim Murraya3145512012-12-04 17:59:29 -08001238 (RsType)type, (RsAllocationMipmapControl)mip,
Ashok Bhat98071552014-02-12 09:54:43 +00001239 (uint32_t)usage, (uintptr_t)ptr);
Tim Murraya3145512012-12-04 17:59:29 -08001240 bitmap.unlockPixels();
1241 return id;
1242}
1243
Tim Murray460a0492013-11-19 12:45:54 -08001244static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001245nAllocationCubeCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1246 jobject jbitmap, jint usage)
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001247{
John Recked207b92015-04-10 13:52:57 -07001248 SkBitmap bitmap;
1249 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001250
Jason Sams5476b452010-12-08 16:14:36 -08001251 bitmap.lockPixels();
1252 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001253 jlong id = (jlong)(uintptr_t)rsAllocationCubeCreateFromBitmap((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001254 (RsType)type, (RsAllocationMipmapControl)mip,
1255 ptr, bitmap.getSize(), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001256 bitmap.unlockPixels();
1257 return id;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001258}
1259
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001260static void
Tim Murray460a0492013-11-19 12:45:54 -08001261nAllocationCopyFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001262{
John Recked207b92015-04-10 13:52:57 -07001263 SkBitmap bitmap;
1264 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Samsf7086092011-01-12 13:28:37 -08001265 int w = bitmap.width();
1266 int h = bitmap.height();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001267
Jason Sams4ef66502010-12-10 16:03:15 -08001268 bitmap.lockPixels();
1269 const void* ptr = bitmap.getPixels();
Tim Murrayeff663f2013-11-15 13:08:30 -08001270 rsAllocation2DData((RsContext)con, (RsAllocation)alloc, 0, 0,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001271 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Tim Murray38faea302012-11-27 14:55:08 -08001272 w, h, ptr, bitmap.getSize(), 0);
Jason Sams4ef66502010-12-10 16:03:15 -08001273 bitmap.unlockPixels();
1274}
1275
1276static void
Tim Murray460a0492013-11-19 12:45:54 -08001277nAllocationCopyToBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Jason Sams4ef66502010-12-10 16:03:15 -08001278{
John Recked207b92015-04-10 13:52:57 -07001279 SkBitmap bitmap;
1280 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Sams4ef66502010-12-10 16:03:15 -08001281
1282 bitmap.lockPixels();
1283 void* ptr = bitmap.getPixels();
Tim Murrayeff663f2013-11-15 13:08:30 -08001284 rsAllocationCopyToBitmap((RsContext)con, (RsAllocation)alloc, ptr, bitmap.getSize());
Jason Sams4ef66502010-12-10 16:03:15 -08001285 bitmap.unlockPixels();
Alex Sakhartchouk835b8542011-07-20 14:33:10 -07001286 bitmap.notifyPixelsChanged();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001287}
1288
Stephen Hines414fa2c2014-04-17 01:02:42 -07001289// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Samsd19f10d2009-05-22 14:03:28 -07001290static void
Tim Murray460a0492013-11-19 12:45:54 -08001291nAllocationData1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001292 jint count, jobject data, jint sizeBytes, jint dataType, jint mSize,
1293 jboolean usePadding)
Jason Samsd19f10d2009-05-22 14:03:28 -07001294{
Jason Samse729a942013-11-06 11:22:02 -08001295 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001296 if (kLogApi) {
1297 ALOGD("nAllocation1DData, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1298 "dataType(%i)", (RsContext)con, (RsAllocation)alloc, offset, count, sizeBytes,
1299 dataType);
1300 }
Miao Wang87e908d2015-03-02 15:15:15 -08001301 PER_ARRAY_TYPE(nullptr, rsAllocation1DData, true,
1302 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -07001303}
1304
1305static void
Miao Wangc8e237e2015-02-20 18:36:32 -08001306nAllocationElementData(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1307 jint xoff, jint yoff, jint zoff,
1308 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Jason Sams49bdaf02010-08-31 13:50:42 -07001309{
1310 jint len = _env->GetArrayLength(data);
Andreas Gampe67333922014-11-10 20:35:59 -08001311 if (kLogApi) {
Miao Wangc8e237e2015-02-20 18:36:32 -08001312 ALOGD("nAllocationElementData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1313 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
Andreas Gampe67333922014-11-10 20:35:59 -08001314 sizeBytes);
1315 }
Chris Wailes488230c32014-08-14 11:22:40 -07001316 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangc8e237e2015-02-20 18:36:32 -08001317 rsAllocationElementData((RsContext)con, (RsAllocation)alloc,
1318 xoff, yoff, zoff,
1319 lod, ptr, sizeBytes, compIdx);
Jason Sams49bdaf02010-08-31 13:50:42 -07001320 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1321}
1322
Miao Wangc8e237e2015-02-20 18:36:32 -08001323
Stephen Hines414fa2c2014-04-17 01:02:42 -07001324// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Sams49bdaf02010-08-31 13:50:42 -07001325static void
Tim Murray460a0492013-11-19 12:45:54 -08001326nAllocationData2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001327 jint w, jint h, jobject data, jint sizeBytes, jint dataType, jint mSize,
1328 jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001329{
Jason Samse729a942013-11-06 11:22:02 -08001330 RsAllocation *alloc = (RsAllocation *)_alloc;
1331 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001332 if (kLogApi) {
1333 ALOGD("nAllocation2DData, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1334 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1335 }
Miao Wang87e908d2015-03-02 15:15:15 -08001336 int count = w * h;
1337 PER_ARRAY_TYPE(nullptr, rsAllocation2DData, true,
1338 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Samsd19f10d2009-05-22 14:03:28 -07001339}
1340
Stephen Hines414fa2c2014-04-17 01:02:42 -07001341// Copies from the Allocation pointed to by srcAlloc into the Allocation
1342// pointed to by dstAlloc.
Jason Sams40a29e82009-08-10 14:55:26 -07001343static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001344nAllocationData2D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001345 jlong dstAlloc, jint dstXoff, jint dstYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001346 jint dstMip, jint dstFace,
1347 jint width, jint height,
Tim Murray460a0492013-11-19 12:45:54 -08001348 jlong srcAlloc, jint srcXoff, jint srcYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001349 jint srcMip, jint srcFace)
1350{
Andreas Gampe67333922014-11-10 20:35:59 -08001351 if (kLogApi) {
1352 ALOGD("nAllocation2DData_s, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1353 " dstMip(%i), dstFace(%i), width(%i), height(%i),"
1354 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i), srcFace(%i)",
1355 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip, dstFace,
1356 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
1357 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001358
Tim Murrayeff663f2013-11-15 13:08:30 -08001359 rsAllocationCopy2DRange((RsContext)con,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001360 (RsAllocation)dstAlloc,
1361 dstXoff, dstYoff,
1362 dstMip, dstFace,
1363 width, height,
1364 (RsAllocation)srcAlloc,
1365 srcXoff, srcYoff,
1366 srcMip, srcFace);
1367}
1368
Stephen Hines414fa2c2014-04-17 01:02:42 -07001369// Copies from the Java object data into the Allocation pointed to by _alloc.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001370static void
Tim Murray460a0492013-11-19 12:45:54 -08001371nAllocationData3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001372 jint w, jint h, jint d, jobject data, jint sizeBytes, jint dataType,
1373 jint mSize, jboolean usePadding)
Jason Samsb05d6892013-04-09 15:59:24 -07001374{
Jason Samse729a942013-11-06 11:22:02 -08001375 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001376 if (kLogApi) {
1377 ALOGD("nAllocation3DData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1378 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1379 lod, w, h, d, sizeBytes);
1380 }
Miao Wang87e908d2015-03-02 15:15:15 -08001381 int count = w * h * d;
1382 PER_ARRAY_TYPE(nullptr, rsAllocation3DData, true,
1383 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Jason Samsb05d6892013-04-09 15:59:24 -07001384}
1385
Stephen Hines414fa2c2014-04-17 01:02:42 -07001386// Copies from the Allocation pointed to by srcAlloc into the Allocation
1387// pointed to by dstAlloc.
Jason Samsb05d6892013-04-09 15:59:24 -07001388static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001389nAllocationData3D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001390 jlong dstAlloc, jint dstXoff, jint dstYoff, jint dstZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001391 jint dstMip,
1392 jint width, jint height, jint depth,
Tim Murray460a0492013-11-19 12:45:54 -08001393 jlong srcAlloc, jint srcXoff, jint srcYoff, jint srcZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001394 jint srcMip)
1395{
Andreas Gampe67333922014-11-10 20:35:59 -08001396 if (kLogApi) {
1397 ALOGD("nAllocationData3D_alloc, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1398 " dstMip(%i), width(%i), height(%i),"
1399 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i)",
1400 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip,
1401 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip);
1402 }
Jason Samsb05d6892013-04-09 15:59:24 -07001403
Tim Murrayeff663f2013-11-15 13:08:30 -08001404 rsAllocationCopy3DRange((RsContext)con,
Jason Samsb05d6892013-04-09 15:59:24 -07001405 (RsAllocation)dstAlloc,
1406 dstXoff, dstYoff, dstZoff, dstMip,
1407 width, height, depth,
1408 (RsAllocation)srcAlloc,
1409 srcXoff, srcYoff, srcZoff, srcMip);
1410}
1411
Jason Sams21659ac2013-11-06 15:08:07 -08001412
Stephen Hines414fa2c2014-04-17 01:02:42 -07001413// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsb05d6892013-04-09 15:59:24 -07001414static void
Miao Wang87e908d2015-03-02 15:15:15 -08001415nAllocationRead(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jobject data, jint dataType,
1416 jint mSize, jboolean usePadding)
Jason Sams40a29e82009-08-10 14:55:26 -07001417{
Jason Sams21659ac2013-11-06 15:08:07 -08001418 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001419 if (kLogApi) {
1420 ALOGD("nAllocationRead, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
1421 }
Miao Wang87e908d2015-03-02 15:15:15 -08001422 int count = 0;
1423 PER_ARRAY_TYPE(0, rsAllocationRead, false,
1424 (RsContext)con, alloc, ptr, len * typeBytes);
Jason Sams40a29e82009-08-10 14:55:26 -07001425}
1426
Stephen Hines414fa2c2014-04-17 01:02:42 -07001427// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Sams40a29e82009-08-10 14:55:26 -07001428static void
Tim Murray460a0492013-11-19 12:45:54 -08001429nAllocationRead1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001430 jint count, jobject data, jint sizeBytes, jint dataType,
1431 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001432{
Jason Sams21659ac2013-11-06 15:08:07 -08001433 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001434 if (kLogApi) {
1435 ALOGD("nAllocation1DRead, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1436 "dataType(%i)", (RsContext)con, alloc, offset, count, sizeBytes, dataType);
1437 }
Miao Wang87e908d2015-03-02 15:15:15 -08001438 PER_ARRAY_TYPE(0, rsAllocation1DRead, false,
1439 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsfb9f82c2011-01-12 14:53:25 -08001440}
1441
Miao Wangc8e237e2015-02-20 18:36:32 -08001442// Copies from the Element in the Allocation pointed to by _alloc into the Java array data.
1443static void
Miao Wang45cec0a2015-03-04 16:40:21 -08001444nAllocationElementRead(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
Miao Wangc8e237e2015-02-20 18:36:32 -08001445 jint xoff, jint yoff, jint zoff,
Miao Wang45cec0a2015-03-04 16:40:21 -08001446 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Miao Wangc8e237e2015-02-20 18:36:32 -08001447{
Miao Wang45cec0a2015-03-04 16:40:21 -08001448 jint len = _env->GetArrayLength(data);
Miao Wangc8e237e2015-02-20 18:36:32 -08001449 if (kLogApi) {
Miao Wang45cec0a2015-03-04 16:40:21 -08001450 ALOGD("nAllocationElementRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1451 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
1452 sizeBytes);
Miao Wangc8e237e2015-02-20 18:36:32 -08001453 }
Miao Wang45cec0a2015-03-04 16:40:21 -08001454 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
1455 rsAllocationElementRead((RsContext)con, (RsAllocation)alloc,
1456 xoff, yoff, zoff,
Jason Samsa7e25092015-03-11 11:00:00 -07001457 lod, ptr, sizeBytes, compIdx);
Miao Wang45cec0a2015-03-04 16:40:21 -08001458 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
Miao Wangc8e237e2015-02-20 18:36:32 -08001459}
1460
Stephen Hines414fa2c2014-04-17 01:02:42 -07001461// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsfb9f82c2011-01-12 14:53:25 -08001462static void
Tim Murray460a0492013-11-19 12:45:54 -08001463nAllocationRead2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001464 jint w, jint h, jobject data, jint sizeBytes, jint dataType,
1465 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001466{
Jason Sams21659ac2013-11-06 15:08:07 -08001467 RsAllocation *alloc = (RsAllocation *)_alloc;
1468 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001469 if (kLogApi) {
1470 ALOGD("nAllocation2DRead, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1471 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1472 }
Miao Wang87e908d2015-03-02 15:15:15 -08001473 int count = w * h;
1474 PER_ARRAY_TYPE(0, rsAllocation2DRead, false,
1475 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Sams40a29e82009-08-10 14:55:26 -07001476}
Miao Wang87e908d2015-03-02 15:15:15 -08001477
Miao Wangc8e237e2015-02-20 18:36:32 -08001478// Copies from the Allocation pointed to by _alloc into the Java object data.
1479static void
1480nAllocationRead3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001481 jint w, jint h, jint d, jobject data, int sizeBytes, int dataType,
1482 jint mSize, jboolean usePadding)
Miao Wangc8e237e2015-02-20 18:36:32 -08001483{
1484 RsAllocation *alloc = (RsAllocation *)_alloc;
1485 if (kLogApi) {
1486 ALOGD("nAllocation3DRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1487 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1488 lod, w, h, d, sizeBytes);
1489 }
Miao Wang87e908d2015-03-02 15:15:15 -08001490 int count = w * h * d;
1491 PER_ARRAY_TYPE(nullptr, rsAllocation3DRead, false,
1492 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Miao Wangc8e237e2015-02-20 18:36:32 -08001493}
Jason Samsd19f10d2009-05-22 14:03:28 -07001494
Tim Murray460a0492013-11-19 12:45:54 -08001495static jlong
1496nAllocationGetType(JNIEnv *_env, jobject _this, jlong con, jlong a)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001497{
Andreas Gampe67333922014-11-10 20:35:59 -08001498 if (kLogApi) {
1499 ALOGD("nAllocationGetType, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1500 }
Tim Murray3aa89c12014-08-18 17:51:22 -07001501 return (jlong)(uintptr_t) rsaAllocationGetType((RsContext)con, (RsAllocation)a);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001502}
1503
Jason Sams5edc6082010-10-05 13:32:49 -07001504static void
Tim Murray460a0492013-11-19 12:45:54 -08001505nAllocationResize1D(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint dimX)
Jason Sams5edc6082010-10-05 13:32:49 -07001506{
Andreas Gampe67333922014-11-10 20:35:59 -08001507 if (kLogApi) {
1508 ALOGD("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", (RsContext)con,
1509 (RsAllocation)alloc, dimX);
1510 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001511 rsAllocationResize1D((RsContext)con, (RsAllocation)alloc, dimX);
Jason Sams5edc6082010-10-05 13:32:49 -07001512}
1513
Jason Sams46ba27e32015-02-06 17:45:15 -08001514
1515static jlong
1516nAllocationAdapterCreate(JNIEnv *_env, jobject _this, jlong con, jlong basealloc, jlong type)
1517{
1518 if (kLogApi) {
1519 ALOGD("nAllocationAdapterCreate, con(%p), base(%p), type(%p)",
1520 (RsContext)con, (RsAllocation)basealloc, (RsElement)type);
1521 }
1522 return (jlong)(uintptr_t) rsAllocationAdapterCreate((RsContext)con, (RsType)type,
1523 (RsAllocation)basealloc);
1524
1525}
1526
1527static void
1528nAllocationAdapterOffset(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1529 jint x, jint y, jint z, jint face, jint lod,
1530 jint a1, jint a2, jint a3, jint a4)
1531{
1532 uint32_t params[] = {
1533 (uint32_t)x, (uint32_t)y, (uint32_t)z, (uint32_t)face,
1534 (uint32_t)lod, (uint32_t)a1, (uint32_t)a2, (uint32_t)a3, (uint32_t)a4
1535 };
1536 if (kLogApi) {
1537 ALOGD("nAllocationAdapterOffset, con(%p), alloc(%p), x(%i), y(%i), z(%i), face(%i), lod(%i), arrays(%i %i %i %i)",
1538 (RsContext)con, (RsAllocation)alloc, x, y, z, face, lod, a1, a2, a3, a4);
1539 }
1540 rsAllocationAdapterOffset((RsContext)con, (RsAllocation)alloc,
1541 params, sizeof(params));
1542}
1543
1544
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001545// -----------------------------------
1546
Tim Murray460a0492013-11-19 12:45:54 -08001547static jlong
1548nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con, jlong native_asset)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001549{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001550 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001551 ALOGV("______nFileA3D %p", asset);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001552
Tim Murray3aa89c12014-08-18 17:51:22 -07001553 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromMemory((RsContext)con, asset->getBuffer(false), asset->getLength());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001554 return id;
1555}
1556
Tim Murray460a0492013-11-19 12:45:54 -08001557static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001558nFileA3DCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001559{
1560 AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001561 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001562 return 0;
1563 }
1564
1565 AutoJavaStringToUTF8 str(_env, _path);
1566 Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
Chris Wailes488230c32014-08-14 11:22:40 -07001567 if (asset == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001568 return 0;
1569 }
1570
Tim Murray3aa89c12014-08-18 17:51:22 -07001571 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromAsset((RsContext)con, asset);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001572 return id;
1573}
1574
Tim Murray460a0492013-11-19 12:45:54 -08001575static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001576nFileA3DCreateFromFile(JNIEnv *_env, jobject _this, jlong con, jstring fileName)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001577{
1578 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001579 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromFile((RsContext)con, fileNameUTF.c_str());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001580
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001581 return id;
1582}
1583
Tim Murray460a0492013-11-19 12:45:54 -08001584static jint
1585nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001586{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001587 int32_t numEntries = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001588 rsaFileA3DGetNumIndexEntries((RsContext)con, &numEntries, (RsFile)fileA3D);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001589 return (jint)numEntries;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001590}
1591
1592static void
Tim Murray460a0492013-11-19 12:45:54 -08001593nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001594{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001595 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001596 RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
1597
Tim Murrayeff663f2013-11-15 13:08:30 -08001598 rsaFileA3DGetIndexEntries((RsContext)con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001599
1600 for(jint i = 0; i < numEntries; i ++) {
1601 _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
1602 _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
1603 }
1604
1605 free(fileEntries);
1606}
1607
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001608static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001609nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint index)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001610{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001611 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Tim Murray3aa89c12014-08-18 17:51:22 -07001612 jlong id = (jlong)(uintptr_t)rsaFileA3DGetEntryByIndex((RsContext)con, (uint32_t)index, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001613 return id;
1614}
Jason Samsd19f10d2009-05-22 14:03:28 -07001615
1616// -----------------------------------
1617
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001618static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001619nFontCreateFromFile(JNIEnv *_env, jobject _this, jlong con,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001620 jstring fileName, jfloat fontSize, jint dpi)
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001621{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001622 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001623 jlong id = (jlong)(uintptr_t)rsFontCreateFromFile((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001624 fileNameUTF.c_str(), fileNameUTF.length(),
1625 fontSize, dpi);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001626
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001627 return id;
1628}
1629
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001630static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001631nFontCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con,
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001632 jstring name, jfloat fontSize, jint dpi, jlong native_asset)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001633{
1634 Asset* asset = reinterpret_cast<Asset*>(native_asset);
1635 AutoJavaStringToUTF8 nameUTF(_env, name);
1636
Tim Murray3aa89c12014-08-18 17:51:22 -07001637 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001638 nameUTF.c_str(), nameUTF.length(),
1639 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001640 asset->getBuffer(false), asset->getLength());
1641 return id;
1642}
1643
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001644static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001645nFontCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001646 jfloat fontSize, jint dpi)
1647{
1648 AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001649 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001650 return 0;
1651 }
1652
1653 AutoJavaStringToUTF8 str(_env, _path);
1654 Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
Chris Wailes488230c32014-08-14 11:22:40 -07001655 if (asset == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001656 return 0;
1657 }
1658
Tim Murray3aa89c12014-08-18 17:51:22 -07001659 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001660 str.c_str(), str.length(),
1661 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001662 asset->getBuffer(false), asset->getLength());
1663 delete asset;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001664 return id;
1665}
1666
Jason Samsbd1c3ad2009-08-03 16:03:08 -07001667// -----------------------------------
1668
1669static void
Tim Murray460a0492013-11-19 12:45:54 -08001670nScriptBindAllocation(JNIEnv *_env, jobject _this, jlong con, jlong script, jlong alloc, jint slot)
Jason Samsd19f10d2009-05-22 14:03:28 -07001671{
Andreas Gampe67333922014-11-10 20:35:59 -08001672 if (kLogApi) {
1673 ALOGD("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", (RsContext)con,
1674 (RsScript)script, (RsAllocation)alloc, slot);
1675 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001676 rsScriptBindAllocation((RsContext)con, (RsScript)script, (RsAllocation)alloc, slot);
Jason Samsd19f10d2009-05-22 14:03:28 -07001677}
1678
1679static void
Tim Murray460a0492013-11-19 12:45:54 -08001680nScriptSetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jint val)
Jason Sams4d339932010-05-11 14:03:58 -07001681{
Andreas Gampe67333922014-11-10 20:35:59 -08001682 if (kLogApi) {
1683 ALOGD("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", (RsContext)con, (void *)script,
1684 slot, val);
1685 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001686 rsScriptSetVarI((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001687}
1688
Tim Murray7c4caad2013-04-10 16:21:40 -07001689static jint
Tim Murray460a0492013-11-19 12:45:54 -08001690nScriptGetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001691{
Andreas Gampe67333922014-11-10 20:35:59 -08001692 if (kLogApi) {
1693 ALOGD("nScriptGetVarI, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1694 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001695 int value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001696 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001697 return value;
1698}
1699
Jason Sams4d339932010-05-11 14:03:58 -07001700static void
Tim Murray460a0492013-11-19 12:45:54 -08001701nScriptSetVarObj(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001702{
Andreas Gampe67333922014-11-10 20:35:59 -08001703 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001704 ALOGD("nScriptSetVarObj, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001705 slot, val);
1706 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001707 rsScriptSetVarObj((RsContext)con, (RsScript)script, slot, (RsObjectBase)val);
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001708}
1709
1710static void
Tim Murray460a0492013-11-19 12:45:54 -08001711nScriptSetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Stephen Hines031ec58c2010-10-11 10:54:21 -07001712{
Andreas Gampe67333922014-11-10 20:35:59 -08001713 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001714 ALOGD("nScriptSetVarJ, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001715 slot, val);
1716 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001717 rsScriptSetVarJ((RsContext)con, (RsScript)script, slot, val);
Stephen Hines031ec58c2010-10-11 10:54:21 -07001718}
1719
Tim Murray7c4caad2013-04-10 16:21:40 -07001720static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001721nScriptGetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001722{
Andreas Gampe67333922014-11-10 20:35:59 -08001723 if (kLogApi) {
1724 ALOGD("nScriptGetVarJ, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1725 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001726 jlong value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001727 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001728 return value;
1729}
1730
Stephen Hines031ec58c2010-10-11 10:54:21 -07001731static void
Tim Murray460a0492013-11-19 12:45:54 -08001732nScriptSetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, float val)
Jason Sams4d339932010-05-11 14:03:58 -07001733{
Andreas Gampe67333922014-11-10 20:35:59 -08001734 if (kLogApi) {
1735 ALOGD("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", (RsContext)con, (void *)script,
1736 slot, val);
1737 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001738 rsScriptSetVarF((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001739}
1740
Tim Murray7c4caad2013-04-10 16:21:40 -07001741static jfloat
Tim Murray460a0492013-11-19 12:45:54 -08001742nScriptGetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001743{
Andreas Gampe67333922014-11-10 20:35:59 -08001744 if (kLogApi) {
1745 ALOGD("nScriptGetVarF, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1746 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001747 jfloat value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001748 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001749 return value;
1750}
1751
Jason Sams4d339932010-05-11 14:03:58 -07001752static void
Tim Murray460a0492013-11-19 12:45:54 -08001753nScriptSetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, double val)
Stephen Hinesca54ec32010-09-20 17:20:30 -07001754{
Andreas Gampe67333922014-11-10 20:35:59 -08001755 if (kLogApi) {
1756 ALOGD("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", (RsContext)con, (void *)script,
1757 slot, val);
1758 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001759 rsScriptSetVarD((RsContext)con, (RsScript)script, slot, val);
Stephen Hinesca54ec32010-09-20 17:20:30 -07001760}
1761
Tim Murray7c4caad2013-04-10 16:21:40 -07001762static jdouble
Tim Murray460a0492013-11-19 12:45:54 -08001763nScriptGetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001764{
Andreas Gampe67333922014-11-10 20:35:59 -08001765 if (kLogApi) {
1766 ALOGD("nScriptGetVarD, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1767 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001768 jdouble value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001769 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001770 return value;
1771}
1772
Stephen Hinesca54ec32010-09-20 17:20:30 -07001773static void
Tim Murray460a0492013-11-19 12:45:54 -08001774nScriptSetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001775{
Andreas Gampe67333922014-11-10 20:35:59 -08001776 if (kLogApi) {
1777 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1778 }
Jason Sams4d339932010-05-11 14:03:58 -07001779 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001780 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Tim Murrayeff663f2013-11-15 13:08:30 -08001781 rsScriptSetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001782 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1783}
1784
Stephen Hinesadeb8092012-04-20 14:26:06 -07001785static void
Tim Murray460a0492013-11-19 12:45:54 -08001786nScriptGetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Tim Murray7c4caad2013-04-10 16:21:40 -07001787{
Andreas Gampe67333922014-11-10 20:35:59 -08001788 if (kLogApi) {
1789 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1790 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001791 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001792 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Tim Murrayeff663f2013-11-15 13:08:30 -08001793 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Stephen Hines414fa2c2014-04-17 01:02:42 -07001794 _env->ReleaseByteArrayElements(data, ptr, 0);
Tim Murray7c4caad2013-04-10 16:21:40 -07001795}
1796
1797static void
Andreas Gampe67333922014-11-10 20:35:59 -08001798nScriptSetVarVE(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data,
1799 jlong elem, jintArray dims)
Stephen Hinesadeb8092012-04-20 14:26:06 -07001800{
Andreas Gampe67333922014-11-10 20:35:59 -08001801 if (kLogApi) {
1802 ALOGD("nScriptSetVarVE, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1803 }
Stephen Hinesadeb8092012-04-20 14:26:06 -07001804 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001805 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Stephen Hinesadeb8092012-04-20 14:26:06 -07001806 jint dimsLen = _env->GetArrayLength(dims) * sizeof(int);
Chris Wailes488230c32014-08-14 11:22:40 -07001807 jint *dimsPtr = _env->GetIntArrayElements(dims, nullptr);
Tim Murrayeff663f2013-11-15 13:08:30 -08001808 rsScriptSetVarVE((RsContext)con, (RsScript)script, slot, ptr, len, (RsElement)elem,
Stephen Hinesbc5d3ee2014-06-25 00:03:39 -07001809 (const uint32_t*) dimsPtr, dimsLen);
Stephen Hinesadeb8092012-04-20 14:26:06 -07001810 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1811 _env->ReleaseIntArrayElements(dims, dimsPtr, JNI_ABORT);
1812}
1813
Jason Samsd19f10d2009-05-22 14:03:28 -07001814
1815static void
Tim Murray460a0492013-11-19 12:45:54 -08001816nScriptSetTimeZone(JNIEnv *_env, jobject _this, jlong con, jlong script, jbyteArray timeZone)
Jason Samsd19f10d2009-05-22 14:03:28 -07001817{
Andreas Gampe67333922014-11-10 20:35:59 -08001818 if (kLogApi) {
1819 ALOGD("nScriptCSetTimeZone, con(%p), s(%p)", (RsContext)con, (void *)script);
1820 }
Romain Guy584a3752009-07-30 18:45:01 -07001821
1822 jint length = _env->GetArrayLength(timeZone);
1823 jbyte* timeZone_ptr;
1824 timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
1825
Tim Murrayeff663f2013-11-15 13:08:30 -08001826 rsScriptSetTimeZone((RsContext)con, (RsScript)script, (const char *)timeZone_ptr, length);
Romain Guy584a3752009-07-30 18:45:01 -07001827
1828 if (timeZone_ptr) {
1829 _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
1830 }
1831}
1832
Jason Samsfbf0b9e2009-08-13 12:59:04 -07001833static void
Tim Murray460a0492013-11-19 12:45:54 -08001834nScriptInvoke(JNIEnv *_env, jobject _this, jlong con, jlong obj, jint slot)
Jason Samsbe2e8412009-09-16 15:04:38 -07001835{
Andreas Gampe67333922014-11-10 20:35:59 -08001836 if (kLogApi) {
1837 ALOGD("nScriptInvoke, con(%p), script(%p)", (RsContext)con, (void *)obj);
1838 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001839 rsScriptInvoke((RsContext)con, (RsScript)obj, slot);
Jason Samsbe2e8412009-09-16 15:04:38 -07001840}
1841
1842static void
Tim Murray460a0492013-11-19 12:45:54 -08001843nScriptInvokeV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001844{
Andreas Gampe67333922014-11-10 20:35:59 -08001845 if (kLogApi) {
1846 ALOGD("nScriptInvokeV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1847 }
Jason Sams4d339932010-05-11 14:03:58 -07001848 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001849 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Tim Murrayeff663f2013-11-15 13:08:30 -08001850 rsScriptInvokeV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001851 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1852}
1853
Jason Sams6e494d32011-04-27 16:33:11 -07001854static void
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001855nScriptForEach(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
1856 jlongArray ains, jlong aout, jbyteArray params,
1857 jintArray limits)
Jason Sams6e494d32011-04-27 16:33:11 -07001858{
Andreas Gampe67333922014-11-10 20:35:59 -08001859 if (kLogApi) {
Jason Samsd1516df2015-05-05 18:00:34 -07001860 ALOGD("nScriptForEach, con(%p), s(%p), slot(%i) ains(%p) aout(%lli)", (RsContext)con, (void *)script, slot, ains, aout);
Andreas Gampe67333922014-11-10 20:35:59 -08001861 }
Jason Sams6e494d32011-04-27 16:33:11 -07001862
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001863 jint in_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07001864 jlong *in_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07001865
Chris Wailes488230c32014-08-14 11:22:40 -07001866 RsAllocation *in_allocs = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07001867
Chris Wailes488230c32014-08-14 11:22:40 -07001868 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001869 in_len = _env->GetArrayLength(ains);
Yang Ni17c2d7a2015-04-30 16:13:54 -07001870 if (in_len > (jint)kMaxNumberKernelArguments) {
1871 ALOGE("Too many arguments in kernel launch.");
1872 // TODO (b/20758983): Report back to Java and throw an exception
1873 return;
1874 }
Chris Wailes94961062014-06-11 12:01:28 -07001875
Yang Ni17c2d7a2015-04-30 16:13:54 -07001876 // TODO (b/20760800): Check in_ptr is not null
1877 in_ptr = _env->GetLongArrayElements(ains, nullptr);
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001878 if (sizeof(RsAllocation) == sizeof(jlong)) {
1879 in_allocs = (RsAllocation*)in_ptr;
Chris Wailes94961062014-06-11 12:01:28 -07001880
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001881 } else {
1882 // Convert from 64-bit jlong types to the native pointer type.
Chris Wailes94961062014-06-11 12:01:28 -07001883
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001884 in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
Yang Ni17c2d7a2015-04-30 16:13:54 -07001885 if (in_allocs == nullptr) {
1886 ALOGE("Failed launching kernel for lack of memory.");
1887 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
1888 return;
1889 }
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001890
1891 for (int index = in_len; --index >= 0;) {
1892 in_allocs[index] = (RsAllocation)in_ptr[index];
1893 }
1894 }
Chris Wailes94961062014-06-11 12:01:28 -07001895 }
1896
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001897 jint param_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07001898 jbyte *param_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07001899
Chris Wailes488230c32014-08-14 11:22:40 -07001900 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001901 param_len = _env->GetArrayLength(params);
Chris Wailes488230c32014-08-14 11:22:40 -07001902 param_ptr = _env->GetByteArrayElements(params, nullptr);
Chris Wailes94961062014-06-11 12:01:28 -07001903 }
1904
Chris Wailes488230c32014-08-14 11:22:40 -07001905 RsScriptCall sc, *sca = nullptr;
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001906 uint32_t sc_size = 0;
Chris Wailes94961062014-06-11 12:01:28 -07001907
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001908 jint limit_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07001909 jint *limit_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07001910
Chris Wailes488230c32014-08-14 11:22:40 -07001911 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001912 limit_len = _env->GetArrayLength(limits);
Chris Wailes488230c32014-08-14 11:22:40 -07001913 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
Chris Wailes94961062014-06-11 12:01:28 -07001914
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001915 assert(limit_len == 6);
Andreas Gampe67333922014-11-10 20:35:59 -08001916 UNUSED(limit_len); // As the assert might not be compiled.
Chris Wailes94961062014-06-11 12:01:28 -07001917
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001918 sc.xStart = limit_ptr[0];
1919 sc.xEnd = limit_ptr[1];
1920 sc.yStart = limit_ptr[2];
1921 sc.yEnd = limit_ptr[3];
1922 sc.zStart = limit_ptr[4];
1923 sc.zEnd = limit_ptr[5];
1924 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
Jason Sams14331ab2015-01-26 18:14:36 -08001925 sc.arrayStart = 0;
1926 sc.arrayEnd = 0;
1927 sc.array2Start = 0;
1928 sc.array2End = 0;
1929 sc.array3Start = 0;
1930 sc.array3End = 0;
1931 sc.array4Start = 0;
1932 sc.array4End = 0;
Chris Wailes94961062014-06-11 12:01:28 -07001933
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001934 sca = &sc;
Chris Wailes94961062014-06-11 12:01:28 -07001935 }
1936
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001937 rsScriptForEachMulti((RsContext)con, (RsScript)script, slot,
1938 in_allocs, in_len, (RsAllocation)aout,
1939 param_ptr, param_len, sca, sc_size);
Chris Wailes94961062014-06-11 12:01:28 -07001940
Chris Wailes488230c32014-08-14 11:22:40 -07001941 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001942 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
Chris Wailes94961062014-06-11 12:01:28 -07001943 }
1944
Chris Wailes488230c32014-08-14 11:22:40 -07001945 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001946 _env->ReleaseByteArrayElements(params, param_ptr, JNI_ABORT);
1947 }
1948
Chris Wailes488230c32014-08-14 11:22:40 -07001949 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001950 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
1951 }
Chris Wailes94961062014-06-11 12:01:28 -07001952}
1953
Jason Sams22534172009-08-04 16:58:20 -07001954// -----------------------------------
1955
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001956static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001957nScriptCCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Samse4a06c52011-03-16 16:29:28 -07001958 jstring resName, jstring cacheDir,
1959 jbyteArray scriptRef, jint length)
Jason Sams22534172009-08-04 16:58:20 -07001960{
Andreas Gampe67333922014-11-10 20:35:59 -08001961 if (kLogApi) {
1962 ALOGD("nScriptCCreate, con(%p)", (RsContext)con);
1963 }
Jason Sams22534172009-08-04 16:58:20 -07001964
Jason Samse4a06c52011-03-16 16:29:28 -07001965 AutoJavaStringToUTF8 resNameUTF(_env, resName);
1966 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001967 jlong ret = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07001968 jbyte* script_ptr = nullptr;
Jack Palevich43702d82009-05-28 13:38:16 -07001969 jint _exception = 0;
1970 jint remaining;
Jack Palevich43702d82009-05-28 13:38:16 -07001971 if (!scriptRef) {
1972 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07001973 //jniThrowException(_env, "java/lang/IllegalArgumentException", "script == null");
Jack Palevich43702d82009-05-28 13:38:16 -07001974 goto exit;
1975 }
Jack Palevich43702d82009-05-28 13:38:16 -07001976 if (length < 0) {
1977 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07001978 //jniThrowException(_env, "java/lang/IllegalArgumentException", "length < 0");
Jack Palevich43702d82009-05-28 13:38:16 -07001979 goto exit;
1980 }
Jason Samse4a06c52011-03-16 16:29:28 -07001981 remaining = _env->GetArrayLength(scriptRef);
Jack Palevich43702d82009-05-28 13:38:16 -07001982 if (remaining < length) {
1983 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07001984 //jniThrowException(_env, "java/lang/IllegalArgumentException",
1985 // "length > script.length - offset");
Jack Palevich43702d82009-05-28 13:38:16 -07001986 goto exit;
1987 }
Jason Samse4a06c52011-03-16 16:29:28 -07001988 script_ptr = (jbyte *)
Jack Palevich43702d82009-05-28 13:38:16 -07001989 _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
Jack Palevich43702d82009-05-28 13:38:16 -07001990
Tim Murrayeff663f2013-11-15 13:08:30 -08001991 //rsScriptCSetText((RsContext)con, (const char *)script_ptr, length);
Jason Samse4a06c52011-03-16 16:29:28 -07001992
Tim Murray3aa89c12014-08-18 17:51:22 -07001993 ret = (jlong)(uintptr_t)rsScriptCCreate((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001994 resNameUTF.c_str(), resNameUTF.length(),
1995 cacheDirUTF.c_str(), cacheDirUTF.length(),
Jason Samse4a06c52011-03-16 16:29:28 -07001996 (const char *)script_ptr, length);
Jason Sams39ddc9502009-06-05 17:35:09 -07001997
Jack Palevich43702d82009-05-28 13:38:16 -07001998exit:
Jason Samse4a06c52011-03-16 16:29:28 -07001999 if (script_ptr) {
2000 _env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr,
Jack Palevich43702d82009-05-28 13:38:16 -07002001 _exception ? JNI_ABORT: 0);
2002 }
Jason Samsd19f10d2009-05-22 14:03:28 -07002003
Tim Murray3aa89c12014-08-18 17:51:22 -07002004 return (jlong)(uintptr_t)ret;
Jason Samsd19f10d2009-05-22 14:03:28 -07002005}
2006
Tim Murray460a0492013-11-19 12:45:54 -08002007static jlong
2008nScriptIntrinsicCreate(JNIEnv *_env, jobject _this, jlong con, jint id, jlong eid)
Jason Sams6ab97682012-08-10 12:09:43 -07002009{
Andreas Gampe67333922014-11-10 20:35:59 -08002010 if (kLogApi) {
2011 ALOGD("nScriptIntrinsicCreate, con(%p) id(%i) element(%p)", (RsContext)con, id,
2012 (void *)eid);
2013 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002014 return (jlong)(uintptr_t)rsScriptIntrinsicCreate((RsContext)con, id, (RsElement)eid);
Jason Sams6ab97682012-08-10 12:09:43 -07002015}
2016
Tim Murray460a0492013-11-19 12:45:54 -08002017static jlong
2018nScriptKernelIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot, jint sig)
Jason Sams08a81582012-09-18 12:32:10 -07002019{
Andreas Gampe67333922014-11-10 20:35:59 -08002020 if (kLogApi) {
2021 ALOGD("nScriptKernelIDCreate, con(%p) script(%p), slot(%i), sig(%i)", (RsContext)con,
2022 (void *)sid, slot, sig);
2023 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002024 return (jlong)(uintptr_t)rsScriptKernelIDCreate((RsContext)con, (RsScript)sid, slot, sig);
Jason Sams08a81582012-09-18 12:32:10 -07002025}
2026
Tim Murray460a0492013-11-19 12:45:54 -08002027static jlong
Yang Nibe392ad2015-01-23 17:16:02 -08002028nScriptInvokeIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
2029{
2030 if (kLogApi) {
Elliott Hughes7ff53fa2015-02-05 21:36:10 -08002031 ALOGD("nScriptInvokeIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con,
Yang Nibe392ad2015-01-23 17:16:02 -08002032 (void *)sid, slot);
2033 }
2034 return (jlong)(uintptr_t)rsScriptInvokeIDCreate((RsContext)con, (RsScript)sid, slot);
2035}
2036
2037static jlong
Tim Murray460a0492013-11-19 12:45:54 -08002038nScriptFieldIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
Jason Sams08a81582012-09-18 12:32:10 -07002039{
Andreas Gampe67333922014-11-10 20:35:59 -08002040 if (kLogApi) {
2041 ALOGD("nScriptFieldIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con, (void *)sid,
2042 slot);
2043 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002044 return (jlong)(uintptr_t)rsScriptFieldIDCreate((RsContext)con, (RsScript)sid, slot);
Jason Sams08a81582012-09-18 12:32:10 -07002045}
2046
Tim Murray460a0492013-11-19 12:45:54 -08002047static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002048nScriptGroupCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _kernels, jlongArray _src,
2049 jlongArray _dstk, jlongArray _dstf, jlongArray _types)
Jason Sams08a81582012-09-18 12:32:10 -07002050{
Andreas Gampe67333922014-11-10 20:35:59 -08002051 if (kLogApi) {
2052 ALOGD("nScriptGroupCreate, con(%p)", (RsContext)con);
2053 }
Jason Sams08a81582012-09-18 12:32:10 -07002054
Ashok Bhat98071552014-02-12 09:54:43 +00002055 jint kernelsLen = _env->GetArrayLength(_kernels);
Chris Wailes488230c32014-08-14 11:22:40 -07002056 jlong *jKernelsPtr = _env->GetLongArrayElements(_kernels, nullptr);
Ashok Bhat98071552014-02-12 09:54:43 +00002057 RsScriptKernelID* kernelsPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * kernelsLen);
2058 for(int i = 0; i < kernelsLen; ++i) {
2059 kernelsPtr[i] = (RsScriptKernelID)jKernelsPtr[i];
2060 }
Jason Sams08a81582012-09-18 12:32:10 -07002061
Ashok Bhat98071552014-02-12 09:54:43 +00002062 jint srcLen = _env->GetArrayLength(_src);
Chris Wailes488230c32014-08-14 11:22:40 -07002063 jlong *jSrcPtr = _env->GetLongArrayElements(_src, nullptr);
Ashok Bhat98071552014-02-12 09:54:43 +00002064 RsScriptKernelID* srcPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * srcLen);
2065 for(int i = 0; i < srcLen; ++i) {
2066 srcPtr[i] = (RsScriptKernelID)jSrcPtr[i];
2067 }
Jason Sams08a81582012-09-18 12:32:10 -07002068
Ashok Bhat98071552014-02-12 09:54:43 +00002069 jint dstkLen = _env->GetArrayLength(_dstk);
Chris Wailes488230c32014-08-14 11:22:40 -07002070 jlong *jDstkPtr = _env->GetLongArrayElements(_dstk, nullptr);
Ashok Bhat98071552014-02-12 09:54:43 +00002071 RsScriptKernelID* dstkPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstkLen);
2072 for(int i = 0; i < dstkLen; ++i) {
2073 dstkPtr[i] = (RsScriptKernelID)jDstkPtr[i];
2074 }
2075
2076 jint dstfLen = _env->GetArrayLength(_dstf);
Chris Wailes488230c32014-08-14 11:22:40 -07002077 jlong *jDstfPtr = _env->GetLongArrayElements(_dstf, nullptr);
Ashok Bhat98071552014-02-12 09:54:43 +00002078 RsScriptKernelID* dstfPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstfLen);
2079 for(int i = 0; i < dstfLen; ++i) {
2080 dstfPtr[i] = (RsScriptKernelID)jDstfPtr[i];
2081 }
2082
2083 jint typesLen = _env->GetArrayLength(_types);
Chris Wailes488230c32014-08-14 11:22:40 -07002084 jlong *jTypesPtr = _env->GetLongArrayElements(_types, nullptr);
Ashok Bhat98071552014-02-12 09:54:43 +00002085 RsType* typesPtr = (RsType*) malloc(sizeof(RsType) * typesLen);
2086 for(int i = 0; i < typesLen; ++i) {
2087 typesPtr[i] = (RsType)jTypesPtr[i];
2088 }
2089
Tim Murray3aa89c12014-08-18 17:51:22 -07002090 jlong id = (jlong)(uintptr_t)rsScriptGroupCreate((RsContext)con,
Ashok Bhat98071552014-02-12 09:54:43 +00002091 (RsScriptKernelID *)kernelsPtr, kernelsLen * sizeof(RsScriptKernelID),
2092 (RsScriptKernelID *)srcPtr, srcLen * sizeof(RsScriptKernelID),
2093 (RsScriptKernelID *)dstkPtr, dstkLen * sizeof(RsScriptKernelID),
2094 (RsScriptFieldID *)dstfPtr, dstfLen * sizeof(RsScriptKernelID),
2095 (RsType *)typesPtr, typesLen * sizeof(RsType));
2096
2097 free(kernelsPtr);
2098 free(srcPtr);
2099 free(dstkPtr);
2100 free(dstfPtr);
2101 free(typesPtr);
2102 _env->ReleaseLongArrayElements(_kernels, jKernelsPtr, 0);
2103 _env->ReleaseLongArrayElements(_src, jSrcPtr, 0);
2104 _env->ReleaseLongArrayElements(_dstk, jDstkPtr, 0);
2105 _env->ReleaseLongArrayElements(_dstf, jDstfPtr, 0);
2106 _env->ReleaseLongArrayElements(_types, jTypesPtr, 0);
Jason Sams08a81582012-09-18 12:32:10 -07002107 return id;
2108}
2109
2110static void
Tim Murray460a0492013-11-19 12:45:54 -08002111nScriptGroupSetInput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002112{
Andreas Gampe67333922014-11-10 20:35:59 -08002113 if (kLogApi) {
2114 ALOGD("nScriptGroupSetInput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2115 (void *)gid, (void *)kid, (void *)alloc);
2116 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002117 rsScriptGroupSetInput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002118}
2119
2120static void
Tim Murray460a0492013-11-19 12:45:54 -08002121nScriptGroupSetOutput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002122{
Andreas Gampe67333922014-11-10 20:35:59 -08002123 if (kLogApi) {
2124 ALOGD("nScriptGroupSetOutput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2125 (void *)gid, (void *)kid, (void *)alloc);
2126 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002127 rsScriptGroupSetOutput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002128}
2129
2130static void
Tim Murray460a0492013-11-19 12:45:54 -08002131nScriptGroupExecute(JNIEnv *_env, jobject _this, jlong con, jlong gid)
Jason Sams08a81582012-09-18 12:32:10 -07002132{
Andreas Gampe67333922014-11-10 20:35:59 -08002133 if (kLogApi) {
2134 ALOGD("nScriptGroupSetOutput, con(%p) group(%p)", (RsContext)con, (void *)gid);
2135 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002136 rsScriptGroupExecute((RsContext)con, (RsScriptGroup)gid);
Jason Sams08a81582012-09-18 12:32:10 -07002137}
2138
Jason Samsd19f10d2009-05-22 14:03:28 -07002139// ---------------------------------------------------------------------------
2140
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002141static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002142nProgramStoreCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Sams331bf9b2011-04-06 11:23:54 -07002143 jboolean colorMaskR, jboolean colorMaskG, jboolean colorMaskB, jboolean colorMaskA,
2144 jboolean depthMask, jboolean ditherEnable,
2145 jint srcFunc, jint destFunc,
2146 jint depthFunc)
Jason Samsd19f10d2009-05-22 14:03:28 -07002147{
Andreas Gampe67333922014-11-10 20:35:59 -08002148 if (kLogApi) {
2149 ALOGD("nProgramStoreCreate, con(%p)", (RsContext)con);
2150 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002151 return (jlong)(uintptr_t)rsProgramStoreCreate((RsContext)con, colorMaskR, colorMaskG, colorMaskB, colorMaskA,
Jason Sams331bf9b2011-04-06 11:23:54 -07002152 depthMask, ditherEnable, (RsBlendSrcFunc)srcFunc,
2153 (RsBlendDstFunc)destFunc, (RsDepthFunc)depthFunc);
Jason Samsd19f10d2009-05-22 14:03:28 -07002154}
2155
Jason Sams0011bcf2009-12-15 12:58:36 -08002156// ---------------------------------------------------------------------------
2157
2158static void
Tim Murray460a0492013-11-19 12:45:54 -08002159nProgramBindConstants(JNIEnv *_env, jobject _this, jlong con, jlong vpv, jint slot, jlong a)
Jason Sams0011bcf2009-12-15 12:58:36 -08002160{
Andreas Gampe67333922014-11-10 20:35:59 -08002161 if (kLogApi) {
2162 ALOGD("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", (RsContext)con,
2163 (RsProgramVertex)vpv, slot, (RsAllocation)a);
2164 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002165 rsProgramBindConstants((RsContext)con, (RsProgram)vpv, slot, (RsAllocation)a);
Jason Sams0011bcf2009-12-15 12:58:36 -08002166}
Jason Sams54c0ec12009-11-30 14:49:55 -08002167
Jason Sams68afd012009-12-17 16:55:08 -08002168static void
Tim Murray460a0492013-11-19 12:45:54 -08002169nProgramBindTexture(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002170{
Andreas Gampe67333922014-11-10 20:35:59 -08002171 if (kLogApi) {
2172 ALOGD("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2173 (RsProgramFragment)vpf, slot, (RsAllocation)a);
2174 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002175 rsProgramBindTexture((RsContext)con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
Jason Sams68afd012009-12-17 16:55:08 -08002176}
2177
2178static void
Tim Murray460a0492013-11-19 12:45:54 -08002179nProgramBindSampler(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002180{
Andreas Gampe67333922014-11-10 20:35:59 -08002181 if (kLogApi) {
2182 ALOGD("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2183 (RsProgramFragment)vpf, slot, (RsSampler)a);
2184 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002185 rsProgramBindSampler((RsContext)con, (RsProgramFragment)vpf, slot, (RsSampler)a);
Jason Sams68afd012009-12-17 16:55:08 -08002186}
2187
Jason Samsd19f10d2009-05-22 14:03:28 -07002188// ---------------------------------------------------------------------------
2189
Tim Murray460a0492013-11-19 12:45:54 -08002190static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002191nProgramFragmentCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002192 jobjectArray texNames, jlongArray params)
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002193{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002194 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002195 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002196 jint paramLen = _env->GetArrayLength(params);
2197
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002198 int texCount = _env->GetArrayLength(texNames);
2199 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2200 const char ** nameArray = names.c_str();
2201 size_t* sizeArray = names.c_str_len();
2202
Andreas Gampe67333922014-11-10 20:35:59 -08002203 if (kLogApi) {
2204 ALOGD("nProgramFragmentCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2205 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002206
Ashok Bhat98071552014-02-12 09:54:43 +00002207 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2208 for(int i = 0; i < paramLen; ++i) {
2209 paramPtr[i] = (uintptr_t)jParamPtr[i];
2210 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002211 jlong ret = (jlong)(uintptr_t)rsProgramFragmentCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002212 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002213 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002214
Ashok Bhat98071552014-02-12 09:54:43 +00002215 free(paramPtr);
2216 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002217 return ret;
2218}
2219
2220
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002221// ---------------------------------------------------------------------------
2222
Tim Murray460a0492013-11-19 12:45:54 -08002223static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002224nProgramVertexCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002225 jobjectArray texNames, jlongArray params)
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002226{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002227 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002228 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams0011bcf2009-12-15 12:58:36 -08002229 jint paramLen = _env->GetArrayLength(params);
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002230
Andreas Gampe67333922014-11-10 20:35:59 -08002231 if (kLogApi) {
2232 ALOGD("nProgramVertexCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2233 }
Jason Sams0011bcf2009-12-15 12:58:36 -08002234
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002235 int texCount = _env->GetArrayLength(texNames);
2236 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2237 const char ** nameArray = names.c_str();
2238 size_t* sizeArray = names.c_str_len();
2239
Ashok Bhat98071552014-02-12 09:54:43 +00002240 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2241 for(int i = 0; i < paramLen; ++i) {
2242 paramPtr[i] = (uintptr_t)jParamPtr[i];
2243 }
2244
Tim Murray3aa89c12014-08-18 17:51:22 -07002245 jlong ret = (jlong)(uintptr_t)rsProgramVertexCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002246 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002247 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002248
Ashok Bhat98071552014-02-12 09:54:43 +00002249 free(paramPtr);
2250 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams0011bcf2009-12-15 12:58:36 -08002251 return ret;
2252}
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002253
Jason Samsebfb4362009-09-23 13:57:02 -07002254// ---------------------------------------------------------------------------
2255
Tim Murray460a0492013-11-19 12:45:54 -08002256static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002257nProgramRasterCreate(JNIEnv *_env, jobject _this, jlong con, jboolean pointSprite, jint cull)
Jason Samsebfb4362009-09-23 13:57:02 -07002258{
Andreas Gampe67333922014-11-10 20:35:59 -08002259 if (kLogApi) {
2260 ALOGD("nProgramRasterCreate, con(%p), pointSprite(%i), cull(%i)", (RsContext)con,
2261 pointSprite, cull);
2262 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002263 return (jlong)(uintptr_t)rsProgramRasterCreate((RsContext)con, pointSprite, (RsCullMode)cull);
Jason Samsebfb4362009-09-23 13:57:02 -07002264}
2265
Jason Samsd19f10d2009-05-22 14:03:28 -07002266
2267// ---------------------------------------------------------------------------
2268
2269static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002270nContextBindRootScript(JNIEnv *_env, jobject _this, jlong con, jlong script)
Jason Samsd19f10d2009-05-22 14:03:28 -07002271{
Andreas Gampe67333922014-11-10 20:35:59 -08002272 if (kLogApi) {
2273 ALOGD("nContextBindRootScript, con(%p), script(%p)", (RsContext)con, (RsScript)script);
2274 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002275 rsContextBindRootScript((RsContext)con, (RsScript)script);
Jason Samsd19f10d2009-05-22 14:03:28 -07002276}
2277
2278static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002279nContextBindProgramStore(JNIEnv *_env, jobject _this, jlong con, jlong pfs)
Jason Samsd19f10d2009-05-22 14:03:28 -07002280{
Andreas Gampe67333922014-11-10 20:35:59 -08002281 if (kLogApi) {
2282 ALOGD("nContextBindProgramStore, con(%p), pfs(%p)", (RsContext)con, (RsProgramStore)pfs);
2283 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002284 rsContextBindProgramStore((RsContext)con, (RsProgramStore)pfs);
Jason Samsd19f10d2009-05-22 14:03:28 -07002285}
2286
2287static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002288nContextBindProgramFragment(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsd19f10d2009-05-22 14:03:28 -07002289{
Andreas Gampe67333922014-11-10 20:35:59 -08002290 if (kLogApi) {
2291 ALOGD("nContextBindProgramFragment, con(%p), pf(%p)", (RsContext)con,
2292 (RsProgramFragment)pf);
2293 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002294 rsContextBindProgramFragment((RsContext)con, (RsProgramFragment)pf);
Jason Samsd19f10d2009-05-22 14:03:28 -07002295}
2296
Jason Sams0826a6f2009-06-15 19:04:56 -07002297static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002298nContextBindProgramVertex(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Sams0826a6f2009-06-15 19:04:56 -07002299{
Andreas Gampe67333922014-11-10 20:35:59 -08002300 if (kLogApi) {
2301 ALOGD("nContextBindProgramVertex, con(%p), pf(%p)", (RsContext)con, (RsProgramVertex)pf);
2302 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002303 rsContextBindProgramVertex((RsContext)con, (RsProgramVertex)pf);
Jason Sams0826a6f2009-06-15 19:04:56 -07002304}
2305
Joe Onoratod7b37742009-08-09 22:57:44 -07002306static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002307nContextBindProgramRaster(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsebfb4362009-09-23 13:57:02 -07002308{
Andreas Gampe67333922014-11-10 20:35:59 -08002309 if (kLogApi) {
2310 ALOGD("nContextBindProgramRaster, con(%p), pf(%p)", (RsContext)con, (RsProgramRaster)pf);
2311 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002312 rsContextBindProgramRaster((RsContext)con, (RsProgramRaster)pf);
Jason Samsebfb4362009-09-23 13:57:02 -07002313}
2314
Joe Onoratod7b37742009-08-09 22:57:44 -07002315
Jason Sams02fb2cb2009-05-28 15:37:57 -07002316// ---------------------------------------------------------------------------
2317
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002318static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002319nSamplerCreate(JNIEnv *_env, jobject _this, jlong con, jint magFilter, jint minFilter,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002320 jint wrapS, jint wrapT, jint wrapR, jfloat aniso)
Jason Sams02fb2cb2009-05-28 15:37:57 -07002321{
Andreas Gampe67333922014-11-10 20:35:59 -08002322 if (kLogApi) {
2323 ALOGD("nSamplerCreate, con(%p)", (RsContext)con);
2324 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002325 return (jlong)(uintptr_t)rsSamplerCreate((RsContext)con,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002326 (RsSamplerValue)magFilter,
2327 (RsSamplerValue)minFilter,
2328 (RsSamplerValue)wrapS,
2329 (RsSamplerValue)wrapT,
2330 (RsSamplerValue)wrapR,
2331 aniso);
Jason Sams02fb2cb2009-05-28 15:37:57 -07002332}
2333
Jason Samsbba134c2009-06-22 15:49:21 -07002334// ---------------------------------------------------------------------------
2335
Tim Murray460a0492013-11-19 12:45:54 -08002336static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002337nMeshCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _vtx, jlongArray _idx, jintArray _prim)
Jason Samsbba134c2009-06-22 15:49:21 -07002338{
Andreas Gampe67333922014-11-10 20:35:59 -08002339 if (kLogApi) {
2340 ALOGD("nMeshCreate, con(%p)", (RsContext)con);
2341 }
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002342
2343 jint vtxLen = _env->GetArrayLength(_vtx);
Chris Wailes488230c32014-08-14 11:22:40 -07002344 jlong *jVtxPtr = _env->GetLongArrayElements(_vtx, nullptr);
Ashok Bhat98071552014-02-12 09:54:43 +00002345 RsAllocation* vtxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * vtxLen);
2346 for(int i = 0; i < vtxLen; ++i) {
2347 vtxPtr[i] = (RsAllocation)(uintptr_t)jVtxPtr[i];
2348 }
2349
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002350 jint idxLen = _env->GetArrayLength(_idx);
Chris Wailes488230c32014-08-14 11:22:40 -07002351 jlong *jIdxPtr = _env->GetLongArrayElements(_idx, nullptr);
Ashok Bhat98071552014-02-12 09:54:43 +00002352 RsAllocation* idxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * idxLen);
2353 for(int i = 0; i < idxLen; ++i) {
2354 idxPtr[i] = (RsAllocation)(uintptr_t)jIdxPtr[i];
2355 }
2356
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002357 jint primLen = _env->GetArrayLength(_prim);
Chris Wailes488230c32014-08-14 11:22:40 -07002358 jint *primPtr = _env->GetIntArrayElements(_prim, nullptr);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002359
Tim Murray3aa89c12014-08-18 17:51:22 -07002360 jlong id = (jlong)(uintptr_t)rsMeshCreate((RsContext)con,
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002361 (RsAllocation *)vtxPtr, vtxLen,
2362 (RsAllocation *)idxPtr, idxLen,
2363 (uint32_t *)primPtr, primLen);
2364
Ashok Bhat98071552014-02-12 09:54:43 +00002365 free(vtxPtr);
2366 free(idxPtr);
2367 _env->ReleaseLongArrayElements(_vtx, jVtxPtr, 0);
2368 _env->ReleaseLongArrayElements(_idx, jIdxPtr, 0);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002369 _env->ReleaseIntArrayElements(_prim, primPtr, 0);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002370 return id;
2371}
2372
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002373static jint
Tim Murray460a0492013-11-19 12:45:54 -08002374nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002375{
Andreas Gampe67333922014-11-10 20:35:59 -08002376 if (kLogApi) {
2377 ALOGD("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2378 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002379 jint vtxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002380 rsaMeshGetVertexBufferCount((RsContext)con, (RsMesh)mesh, &vtxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002381 return vtxCount;
2382}
2383
2384static jint
Tim Murray460a0492013-11-19 12:45:54 -08002385nMeshGetIndexCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002386{
Andreas Gampe67333922014-11-10 20:35:59 -08002387 if (kLogApi) {
2388 ALOGD("nMeshGetIndexCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2389 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002390 jint idxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002391 rsaMeshGetIndexCount((RsContext)con, (RsMesh)mesh, &idxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002392 return idxCount;
2393}
2394
2395static void
Ashok Bhat98071552014-02-12 09:54:43 +00002396nMeshGetVertices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _ids, jint numVtxIDs)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002397{
Andreas Gampe67333922014-11-10 20:35:59 -08002398 if (kLogApi) {
2399 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2400 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002401
2402 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
Tim Murrayeff663f2013-11-15 13:08:30 -08002403 rsaMeshGetVertices((RsContext)con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002404
2405 for(jint i = 0; i < numVtxIDs; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002406 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002407 _env->SetLongArrayRegion(_ids, i, 1, &alloc);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002408 }
2409
2410 free(allocs);
2411}
2412
2413static void
Ashok Bhat98071552014-02-12 09:54:43 +00002414nMeshGetIndices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _idxIds, jintArray _primitives, jint numIndices)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002415{
Andreas Gampe67333922014-11-10 20:35:59 -08002416 if (kLogApi) {
2417 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2418 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002419
2420 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
2421 uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
2422
Tim Murrayeff663f2013-11-15 13:08:30 -08002423 rsaMeshGetIndices((RsContext)con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002424
2425 for(jint i = 0; i < numIndices; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002426 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002427 const jint prim = (jint)prims[i];
2428 _env->SetLongArrayRegion(_idxIds, i, 1, &alloc);
2429 _env->SetIntArrayRegion(_primitives, i, 1, &prim);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002430 }
2431
2432 free(allocs);
2433 free(prims);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002434}
2435
Tim Murray56f9e6f2014-05-16 11:47:26 -07002436static jint
2437nSystemGetPointerSize(JNIEnv *_env, jobject _this) {
2438 return (jint)sizeof(void*);
2439}
2440
2441
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002442// ---------------------------------------------------------------------------
2443
Jason Samsd19f10d2009-05-22 14:03:28 -07002444
Jason Sams94d8e90a2009-06-10 16:09:05 -07002445static const char *classPathName = "android/renderscript/RenderScript";
Jason Samsd19f10d2009-05-22 14:03:28 -07002446
2447static JNINativeMethod methods[] = {
Jason Sams1c415172010-11-08 17:06:46 -08002448{"_nInit", "()V", (void*)_nInit },
Jason Samsea84a7c2009-09-04 14:42:41 -07002449
Tim Murrayeff663f2013-11-15 13:08:30 -08002450{"nDeviceCreate", "()J", (void*)nDeviceCreate },
2451{"nDeviceDestroy", "(J)V", (void*)nDeviceDestroy },
2452{"nDeviceSetConfig", "(JII)V", (void*)nDeviceSetConfig },
2453{"nContextGetUserMessage", "(J[I)I", (void*)nContextGetUserMessage },
2454{"nContextGetErrorMessage", "(J)Ljava/lang/String;", (void*)nContextGetErrorMessage },
2455{"nContextPeekMessage", "(J[I)I", (void*)nContextPeekMessage },
Jason Sams1c415172010-11-08 17:06:46 -08002456
Tim Murrayeff663f2013-11-15 13:08:30 -08002457{"nContextInitToClient", "(J)V", (void*)nContextInitToClient },
2458{"nContextDeinitToClient", "(J)V", (void*)nContextDeinitToClient },
Jason Samsd19f10d2009-05-22 14:03:28 -07002459
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002460
Jason Sams2e1872f2010-08-17 16:25:41 -07002461// All methods below are thread protected in java.
Tim Murrayeff663f2013-11-15 13:08:30 -08002462{"rsnContextCreate", "(JIII)J", (void*)nContextCreate },
2463{"rsnContextCreateGL", "(JIIIIIIIIIIIIFI)J", (void*)nContextCreateGL },
2464{"rsnContextFinish", "(J)V", (void*)nContextFinish },
2465{"rsnContextSetPriority", "(JI)V", (void*)nContextSetPriority },
Tim Murray47f31582015-04-07 15:43:24 -07002466{"rsnContextSetCacheDir", "(JLjava/lang/String;)V", (void*)nContextSetCacheDir },
Tim Murrayeff663f2013-11-15 13:08:30 -08002467{"rsnContextSetSurface", "(JIILandroid/view/Surface;)V", (void*)nContextSetSurface },
2468{"rsnContextDestroy", "(J)V", (void*)nContextDestroy },
2469{"rsnContextDump", "(JI)V", (void*)nContextDump },
2470{"rsnContextPause", "(J)V", (void*)nContextPause },
2471{"rsnContextResume", "(J)V", (void*)nContextResume },
2472{"rsnContextSendMessage", "(JI[I)V", (void*)nContextSendMessage },
Yang Ni281c3252014-10-24 08:52:24 -07002473{"rsnClosureCreate", "(JJJ[J[J[I[J[J)J", (void*)nClosureCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002474{"rsnInvokeClosureCreate", "(JJ[B[J[J[I)J", (void*)nInvokeClosureCreate },
Yang Ni281c3252014-10-24 08:52:24 -07002475{"rsnClosureSetArg", "(JJIJI)V", (void*)nClosureSetArg },
2476{"rsnClosureSetGlobal", "(JJJJI)V", (void*)nClosureSetGlobal },
Tim Murray460a0492013-11-19 12:45:54 -08002477{"rsnAssignName", "(JJ[B)V", (void*)nAssignName },
2478{"rsnGetName", "(JJ)Ljava/lang/String;", (void*)nGetName },
2479{"rsnObjDestroy", "(JJ)V", (void*)nObjDestroy },
Jason Sams64676f32009-07-08 18:01:53 -07002480
Tim Murray460a0492013-11-19 12:45:54 -08002481{"rsnFileA3DCreateFromFile", "(JLjava/lang/String;)J", (void*)nFileA3DCreateFromFile },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002482{"rsnFileA3DCreateFromAssetStream", "(JJ)J", (void*)nFileA3DCreateFromAssetStream },
Tim Murray460a0492013-11-19 12:45:54 -08002483{"rsnFileA3DCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;)J", (void*)nFileA3DCreateFromAsset },
2484{"rsnFileA3DGetNumIndexEntries", "(JJ)I", (void*)nFileA3DGetNumIndexEntries },
2485{"rsnFileA3DGetIndexEntries", "(JJI[I[Ljava/lang/String;)V", (void*)nFileA3DGetIndexEntries },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002486{"rsnFileA3DGetEntryByIndex", "(JJI)J", (void*)nFileA3DGetEntryByIndex },
Jason Samsd19f10d2009-05-22 14:03:28 -07002487
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002488{"rsnFontCreateFromFile", "(JLjava/lang/String;FI)J", (void*)nFontCreateFromFile },
2489{"rsnFontCreateFromAssetStream", "(JLjava/lang/String;FIJ)J", (void*)nFontCreateFromAssetStream },
2490{"rsnFontCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;FI)J", (void*)nFontCreateFromAsset },
Jason Samsd19f10d2009-05-22 14:03:28 -07002491
Tim Murray460a0492013-11-19 12:45:54 -08002492{"rsnElementCreate", "(JJIZI)J", (void*)nElementCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002493{"rsnElementCreate2", "(J[J[Ljava/lang/String;[I)J", (void*)nElementCreate2 },
Tim Murray460a0492013-11-19 12:45:54 -08002494{"rsnElementGetNativeData", "(JJ[I)V", (void*)nElementGetNativeData },
Ashok Bhat98071552014-02-12 09:54:43 +00002495{"rsnElementGetSubElements", "(JJ[J[Ljava/lang/String;[I)V", (void*)nElementGetSubElements },
Jason Samsd19f10d2009-05-22 14:03:28 -07002496
Tim Murray460a0492013-11-19 12:45:54 -08002497{"rsnTypeCreate", "(JJIIIZZI)J", (void*)nTypeCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002498{"rsnTypeGetNativeData", "(JJ[J)V", (void*)nTypeGetNativeData },
Jason Samsd19f10d2009-05-22 14:03:28 -07002499
Ashok Bhat98071552014-02-12 09:54:43 +00002500{"rsnAllocationCreateTyped", "(JJIIJ)J", (void*)nAllocationCreateTyped },
Tim Murray460a0492013-11-19 12:45:54 -08002501{"rsnAllocationCreateFromBitmap", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateFromBitmap },
2502{"rsnAllocationCreateBitmapBackedAllocation", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateBitmapBackedAllocation },
2503{"rsnAllocationCubeCreateFromBitmap","(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCubeCreateFromBitmap },
Jason Sams5476b452010-12-08 16:14:36 -08002504
Tim Murray460a0492013-11-19 12:45:54 -08002505{"rsnAllocationCopyFromBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyFromBitmap },
2506{"rsnAllocationCopyToBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyToBitmap },
Jason Sams4ef66502010-12-10 16:03:15 -08002507
Tim Murray460a0492013-11-19 12:45:54 -08002508{"rsnAllocationSyncAll", "(JJI)V", (void*)nAllocationSyncAll },
2509{"rsnAllocationGetSurface", "(JJ)Landroid/view/Surface;", (void*)nAllocationGetSurface },
2510{"rsnAllocationSetSurface", "(JJLandroid/view/Surface;)V", (void*)nAllocationSetSurface },
2511{"rsnAllocationIoSend", "(JJ)V", (void*)nAllocationIoSend },
2512{"rsnAllocationIoReceive", "(JJ)V", (void*)nAllocationIoReceive },
Miao Wang87e908d2015-03-02 15:15:15 -08002513{"rsnAllocationData1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData1D },
Miao Wangc8e237e2015-02-20 18:36:32 -08002514{"rsnAllocationElementData", "(JJIIIII[BI)V", (void*)nAllocationElementData },
Miao Wang87e908d2015-03-02 15:15:15 -08002515{"rsnAllocationData2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData2D },
Tim Murray460a0492013-11-19 12:45:54 -08002516{"rsnAllocationData2D", "(JJIIIIIIJIIII)V", (void*)nAllocationData2D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002517{"rsnAllocationData3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData3D },
Tim Murray460a0492013-11-19 12:45:54 -08002518{"rsnAllocationData3D", "(JJIIIIIIIJIIII)V", (void*)nAllocationData3D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002519{"rsnAllocationRead", "(JJLjava/lang/Object;IIZ)V", (void*)nAllocationRead },
2520{"rsnAllocationRead1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead1D },
Miao Wang45cec0a2015-03-04 16:40:21 -08002521{"rsnAllocationElementRead", "(JJIIIII[BI)V", (void*)nAllocationElementRead },
Miao Wang87e908d2015-03-02 15:15:15 -08002522{"rsnAllocationRead2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead2D },
2523{"rsnAllocationRead3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead3D },
Tim Murray460a0492013-11-19 12:45:54 -08002524{"rsnAllocationGetType", "(JJ)J", (void*)nAllocationGetType},
2525{"rsnAllocationResize1D", "(JJI)V", (void*)nAllocationResize1D },
2526{"rsnAllocationGenerateMipmaps", "(JJ)V", (void*)nAllocationGenerateMipmaps },
Jason Samsbd1c3ad2009-08-03 16:03:08 -07002527
Jason Sams46ba27e32015-02-06 17:45:15 -08002528{"rsnAllocationAdapterCreate", "(JJJ)J", (void*)nAllocationAdapterCreate },
2529{"rsnAllocationAdapterOffset", "(JJIIIIIIIII)V", (void*)nAllocationAdapterOffset },
2530
Tim Murray460a0492013-11-19 12:45:54 -08002531{"rsnScriptBindAllocation", "(JJJI)V", (void*)nScriptBindAllocation },
2532{"rsnScriptSetTimeZone", "(JJ[B)V", (void*)nScriptSetTimeZone },
2533{"rsnScriptInvoke", "(JJI)V", (void*)nScriptInvoke },
2534{"rsnScriptInvokeV", "(JJI[B)V", (void*)nScriptInvokeV },
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002535
2536{"rsnScriptForEach", "(JJI[JJ[B[I)V", (void*)nScriptForEach },
2537
Tim Murray460a0492013-11-19 12:45:54 -08002538{"rsnScriptSetVarI", "(JJII)V", (void*)nScriptSetVarI },
2539{"rsnScriptGetVarI", "(JJI)I", (void*)nScriptGetVarI },
2540{"rsnScriptSetVarJ", "(JJIJ)V", (void*)nScriptSetVarJ },
2541{"rsnScriptGetVarJ", "(JJI)J", (void*)nScriptGetVarJ },
2542{"rsnScriptSetVarF", "(JJIF)V", (void*)nScriptSetVarF },
2543{"rsnScriptGetVarF", "(JJI)F", (void*)nScriptGetVarF },
2544{"rsnScriptSetVarD", "(JJID)V", (void*)nScriptSetVarD },
2545{"rsnScriptGetVarD", "(JJI)D", (void*)nScriptGetVarD },
2546{"rsnScriptSetVarV", "(JJI[B)V", (void*)nScriptSetVarV },
2547{"rsnScriptGetVarV", "(JJI[B)V", (void*)nScriptGetVarV },
2548{"rsnScriptSetVarVE", "(JJI[BJ[I)V", (void*)nScriptSetVarVE },
2549{"rsnScriptSetVarObj", "(JJIJ)V", (void*)nScriptSetVarObj },
Jason Samsd19f10d2009-05-22 14:03:28 -07002550
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002551{"rsnScriptCCreate", "(JLjava/lang/String;Ljava/lang/String;[BI)J", (void*)nScriptCCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002552{"rsnScriptIntrinsicCreate", "(JIJ)J", (void*)nScriptIntrinsicCreate },
2553{"rsnScriptKernelIDCreate", "(JJII)J", (void*)nScriptKernelIDCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002554{"rsnScriptInvokeIDCreate", "(JJI)J", (void*)nScriptInvokeIDCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002555{"rsnScriptFieldIDCreate", "(JJI)J", (void*)nScriptFieldIDCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002556{"rsnScriptGroupCreate", "(J[J[J[J[J[J)J", (void*)nScriptGroupCreate },
Yang Ni35be56c2015-04-02 17:47:56 -07002557{"rsnScriptGroup2Create", "(JLjava/lang/String;Ljava/lang/String;[J)J", (void*)nScriptGroup2Create },
Tim Murray460a0492013-11-19 12:45:54 -08002558{"rsnScriptGroupSetInput", "(JJJJ)V", (void*)nScriptGroupSetInput },
2559{"rsnScriptGroupSetOutput", "(JJJJ)V", (void*)nScriptGroupSetOutput },
2560{"rsnScriptGroupExecute", "(JJ)V", (void*)nScriptGroupExecute },
Yang Ni281c3252014-10-24 08:52:24 -07002561{"rsnScriptGroup2Execute", "(JJ)V", (void*)nScriptGroup2Execute },
Jason Sams0011bcf2009-12-15 12:58:36 -08002562
Tim Murray25207df2015-01-12 16:47:56 -08002563{"rsnScriptIntrinsicBLAS_Single", "(JJIIIIIIIIIFJJFJIIII)V", (void*)nScriptIntrinsicBLAS_Single },
2564{"rsnScriptIntrinsicBLAS_Double", "(JJIIIIIIIIIDJJDJIIII)V", (void*)nScriptIntrinsicBLAS_Double },
2565{"rsnScriptIntrinsicBLAS_Complex", "(JJIIIIIIIIIFFJJFFJIIII)V", (void*)nScriptIntrinsicBLAS_Complex },
2566{"rsnScriptIntrinsicBLAS_Z", "(JJIIIIIIIIIDDJJDDJIIII)V", (void*)nScriptIntrinsicBLAS_Z },
2567
Tim Murray9cb16a22015-04-01 11:07:16 -07002568{"rsnScriptIntrinsicBLAS_BNNM", "(JJIIIJIJIJII)V", (void*)nScriptIntrinsicBLAS_BNNM },
2569
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002570{"rsnProgramStoreCreate", "(JZZZZZZIII)J", (void*)nProgramStoreCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002571
Tim Murray460a0492013-11-19 12:45:54 -08002572{"rsnProgramBindConstants", "(JJIJ)V", (void*)nProgramBindConstants },
2573{"rsnProgramBindTexture", "(JJIJ)V", (void*)nProgramBindTexture },
2574{"rsnProgramBindSampler", "(JJIJ)V", (void*)nProgramBindSampler },
Jason Samsebfb4362009-09-23 13:57:02 -07002575
Ashok Bhat98071552014-02-12 09:54:43 +00002576{"rsnProgramFragmentCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramFragmentCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002577{"rsnProgramRasterCreate", "(JZI)J", (void*)nProgramRasterCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002578{"rsnProgramVertexCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramVertexCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002579
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002580{"rsnContextBindRootScript", "(JJ)V", (void*)nContextBindRootScript },
2581{"rsnContextBindProgramStore", "(JJ)V", (void*)nContextBindProgramStore },
2582{"rsnContextBindProgramFragment", "(JJ)V", (void*)nContextBindProgramFragment },
2583{"rsnContextBindProgramVertex", "(JJ)V", (void*)nContextBindProgramVertex },
2584{"rsnContextBindProgramRaster", "(JJ)V", (void*)nContextBindProgramRaster },
Jason Sams02fb2cb2009-05-28 15:37:57 -07002585
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002586{"rsnSamplerCreate", "(JIIIIIF)J", (void*)nSamplerCreate },
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002587
Ashok Bhat98071552014-02-12 09:54:43 +00002588{"rsnMeshCreate", "(J[J[J[I)J", (void*)nMeshCreate },
Jason Sams2e1872f2010-08-17 16:25:41 -07002589
Tim Murray460a0492013-11-19 12:45:54 -08002590{"rsnMeshGetVertexBufferCount", "(JJ)I", (void*)nMeshGetVertexBufferCount },
2591{"rsnMeshGetIndexCount", "(JJ)I", (void*)nMeshGetIndexCount },
Ashok Bhat98071552014-02-12 09:54:43 +00002592{"rsnMeshGetVertices", "(JJ[JI)V", (void*)nMeshGetVertices },
2593{"rsnMeshGetIndices", "(JJ[J[II)V", (void*)nMeshGetIndices },
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002594
Tim Murray56f9e6f2014-05-16 11:47:26 -07002595{"rsnSystemGetPointerSize", "()I", (void*)nSystemGetPointerSize },
Jason Samsd19f10d2009-05-22 14:03:28 -07002596};
2597
2598static int registerFuncs(JNIEnv *_env)
2599{
2600 return android::AndroidRuntime::registerNativeMethods(
2601 _env, classPathName, methods, NELEM(methods));
2602}
2603
2604// ---------------------------------------------------------------------------
2605
2606jint JNI_OnLoad(JavaVM* vm, void* reserved)
2607{
Chris Wailes488230c32014-08-14 11:22:40 -07002608 JNIEnv* env = nullptr;
Jason Samsd19f10d2009-05-22 14:03:28 -07002609 jint result = -1;
2610
Jason Samsd19f10d2009-05-22 14:03:28 -07002611 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
Steve Block3762c312012-01-06 19:20:56 +00002612 ALOGE("ERROR: GetEnv failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07002613 goto bail;
2614 }
Chris Wailes488230c32014-08-14 11:22:40 -07002615 assert(env != nullptr);
Jason Samsd19f10d2009-05-22 14:03:28 -07002616
2617 if (registerFuncs(env) < 0) {
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002618 ALOGE("ERROR: Renderscript native registration failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07002619 goto bail;
2620 }
2621
2622 /* success -- return valid version number */
2623 result = JNI_VERSION_1_4;
2624
2625bail:
2626 return result;
2627}