blob: 113241da4572a45bc1ec12237d6acbc63bf010eb [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
Stephen Hines4cbe25a2012-01-18 18:46:27 -08002 * Copyright (C) 2011-2012 The Android Open Source Project
Jason Samsd19f10d2009-05-22 14:03:28 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jason Samsd1516df2015-05-05 18:00:34 -070017#define LOG_TAG "RenderScript_jni"
Jason Samsf29ca502009-06-23 12:22:47 -070018
Jason Samsd19f10d2009-05-22 14:03:28 -070019#include <stdlib.h>
20#include <stdio.h>
21#include <fcntl.h>
22#include <unistd.h>
23#include <math.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070024#include <utils/misc.h>
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +010025#include <inttypes.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070026
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080027#include <androidfw/Asset.h>
28#include <androidfw/AssetManager.h>
29#include <androidfw/ResourceTypes.h>
Jason Samsf29ca502009-06-23 12:22:47 -070030
Jason Samsd19f10d2009-05-22 14:03:28 -070031#include "jni.h"
32#include "JNIHelp.h"
33#include "android_runtime/AndroidRuntime.h"
Jim Milleree956052010-08-19 18:56:00 -070034#include "android_runtime/android_view_Surface.h"
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -080035#include "android_runtime/android_util_AssetManager.h"
John Reckf4faeac2015-03-05 13:50:31 -080036#include "android/graphics/GraphicsJNI.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070037
Jason Sams1d6983a2012-02-16 16:07:49 -080038#include <rs.h>
39#include <rsEnv.h>
Jason Samsfb9aa9f2012-03-28 15:30:07 -070040#include <gui/Surface.h>
Andy McFaddend47f7d82012-12-18 09:48:38 -080041#include <gui/GLConsumer.h>
Jason Samsfaa32b32011-06-20 16:58:04 -070042#include <android_runtime/android_graphics_SurfaceTexture.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070043
Steve Block3762c312012-01-06 19:20:56 +000044//#define LOG_API ALOGE
Andreas Gampe67333922014-11-10 20:35:59 -080045static constexpr bool kLogApi = false;
Jason Samsd19f10d2009-05-22 14:03:28 -070046
47using namespace android;
48
Andreas Gampe67333922014-11-10 20:35:59 -080049template <typename... T>
50void UNUSED(T... t) {}
51
Stephen Hines414fa2c2014-04-17 01:02:42 -070052#define PER_ARRAY_TYPE(flag, fnc, readonly, ...) { \
Jason Samse729a942013-11-06 11:22:02 -080053 jint len = 0; \
Chris Wailes488230c32014-08-14 11:22:40 -070054 void *ptr = nullptr; \
Miao Wang87e908d2015-03-02 15:15:15 -080055 void *srcPtr = nullptr; \
Jason Sams21659ac2013-11-06 15:08:07 -080056 size_t typeBytes = 0; \
Stephen Hines414fa2c2014-04-17 01:02:42 -070057 jint relFlag = 0; \
58 if (readonly) { \
59 /* The on-release mode should only be JNI_ABORT for read-only accesses. */ \
Miao Wang87e908d2015-03-02 15:15:15 -080060 /* readonly = true, also indicates we are copying to the allocation . */ \
Stephen Hines414fa2c2014-04-17 01:02:42 -070061 relFlag = JNI_ABORT; \
62 } \
Jason Samse729a942013-11-06 11:22:02 -080063 switch(dataType) { \
64 case RS_TYPE_FLOAT_32: \
65 len = _env->GetArrayLength((jfloatArray)data); \
66 ptr = _env->GetFloatArrayElements((jfloatArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -070067 if (ptr == nullptr) { \
68 ALOGE("Failed to get Java array elements."); \
69 return; \
70 } \
Jason Sams21659ac2013-11-06 15:08:07 -080071 typeBytes = 4; \
Miao Wang87e908d2015-03-02 15:15:15 -080072 if (usePadding) { \
73 srcPtr = ptr; \
74 len = len / 3 * 4; \
75 if (count == 0) { \
76 count = len / 4; \
77 } \
78 ptr = malloc (len * typeBytes); \
79 if (readonly) { \
80 copyWithPadding(ptr, srcPtr, mSize, count); \
81 fnc(__VA_ARGS__); \
82 } else { \
83 fnc(__VA_ARGS__); \
84 copyWithUnPadding(srcPtr, ptr, mSize, count); \
85 } \
86 free(ptr); \
87 ptr = srcPtr; \
88 } else { \
89 fnc(__VA_ARGS__); \
90 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -070091 _env->ReleaseFloatArrayElements((jfloatArray)data, (jfloat *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -080092 return; \
93 case RS_TYPE_FLOAT_64: \
94 len = _env->GetArrayLength((jdoubleArray)data); \
95 ptr = _env->GetDoubleArrayElements((jdoubleArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -070096 if (ptr == nullptr) { \
97 ALOGE("Failed to get Java array elements."); \
98 return; \
99 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800100 typeBytes = 8; \
Miao Wang87e908d2015-03-02 15:15:15 -0800101 if (usePadding) { \
102 srcPtr = ptr; \
103 len = len / 3 * 4; \
104 if (count == 0) { \
105 count = len / 4; \
106 } \
107 ptr = malloc (len * typeBytes); \
108 if (readonly) { \
109 copyWithPadding(ptr, srcPtr, mSize, count); \
110 fnc(__VA_ARGS__); \
111 } else { \
112 fnc(__VA_ARGS__); \
113 copyWithUnPadding(srcPtr, ptr, mSize, count); \
114 } \
115 free(ptr); \
116 ptr = srcPtr; \
117 } else { \
118 fnc(__VA_ARGS__); \
119 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700120 _env->ReleaseDoubleArrayElements((jdoubleArray)data, (jdouble *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800121 return; \
122 case RS_TYPE_SIGNED_8: \
123 case RS_TYPE_UNSIGNED_8: \
124 len = _env->GetArrayLength((jbyteArray)data); \
125 ptr = _env->GetByteArrayElements((jbyteArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700126 if (ptr == nullptr) { \
127 ALOGE("Failed to get Java array elements."); \
128 return; \
129 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800130 typeBytes = 1; \
Miao Wang87e908d2015-03-02 15:15:15 -0800131 if (usePadding) { \
132 srcPtr = ptr; \
133 len = len / 3 * 4; \
134 if (count == 0) { \
135 count = len / 4; \
136 } \
137 ptr = malloc (len * typeBytes); \
138 if (readonly) { \
139 copyWithPadding(ptr, srcPtr, mSize, count); \
140 fnc(__VA_ARGS__); \
141 } else { \
142 fnc(__VA_ARGS__); \
143 copyWithUnPadding(srcPtr, ptr, mSize, count); \
144 } \
145 free(ptr); \
146 ptr = srcPtr; \
147 } else { \
148 fnc(__VA_ARGS__); \
149 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700150 _env->ReleaseByteArrayElements((jbyteArray)data, (jbyte*)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800151 return; \
152 case RS_TYPE_SIGNED_16: \
153 case RS_TYPE_UNSIGNED_16: \
154 len = _env->GetArrayLength((jshortArray)data); \
155 ptr = _env->GetShortArrayElements((jshortArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700156 if (ptr == nullptr) { \
157 ALOGE("Failed to get Java array elements."); \
158 return; \
159 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800160 typeBytes = 2; \
Miao Wang87e908d2015-03-02 15:15:15 -0800161 if (usePadding) { \
162 srcPtr = ptr; \
163 len = len / 3 * 4; \
164 if (count == 0) { \
165 count = len / 4; \
166 } \
167 ptr = malloc (len * typeBytes); \
168 if (readonly) { \
169 copyWithPadding(ptr, srcPtr, mSize, count); \
170 fnc(__VA_ARGS__); \
171 } else { \
172 fnc(__VA_ARGS__); \
173 copyWithUnPadding(srcPtr, ptr, mSize, count); \
174 } \
175 free(ptr); \
176 ptr = srcPtr; \
177 } else { \
178 fnc(__VA_ARGS__); \
179 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700180 _env->ReleaseShortArrayElements((jshortArray)data, (jshort *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800181 return; \
182 case RS_TYPE_SIGNED_32: \
183 case RS_TYPE_UNSIGNED_32: \
184 len = _env->GetArrayLength((jintArray)data); \
185 ptr = _env->GetIntArrayElements((jintArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700186 if (ptr == nullptr) { \
187 ALOGE("Failed to get Java array elements."); \
188 return; \
189 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800190 typeBytes = 4; \
Miao Wang87e908d2015-03-02 15:15:15 -0800191 if (usePadding) { \
192 srcPtr = ptr; \
193 len = len / 3 * 4; \
194 if (count == 0) { \
195 count = len / 4; \
196 } \
197 ptr = malloc (len * typeBytes); \
198 if (readonly) { \
199 copyWithPadding(ptr, srcPtr, mSize, count); \
200 fnc(__VA_ARGS__); \
201 } else { \
202 fnc(__VA_ARGS__); \
203 copyWithUnPadding(srcPtr, ptr, mSize, count); \
204 } \
205 free(ptr); \
206 ptr = srcPtr; \
207 } else { \
208 fnc(__VA_ARGS__); \
209 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700210 _env->ReleaseIntArrayElements((jintArray)data, (jint *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800211 return; \
212 case RS_TYPE_SIGNED_64: \
213 case RS_TYPE_UNSIGNED_64: \
214 len = _env->GetArrayLength((jlongArray)data); \
215 ptr = _env->GetLongArrayElements((jlongArray)data, flag); \
Miao Wangba8766c2015-10-12 17:24:13 -0700216 if (ptr == nullptr) { \
217 ALOGE("Failed to get Java array elements."); \
218 return; \
219 } \
Jason Sams21659ac2013-11-06 15:08:07 -0800220 typeBytes = 8; \
Miao Wang87e908d2015-03-02 15:15:15 -0800221 if (usePadding) { \
222 srcPtr = ptr; \
223 len = len / 3 * 4; \
224 if (count == 0) { \
225 count = len / 4; \
226 } \
227 ptr = malloc (len * typeBytes); \
228 if (readonly) { \
229 copyWithPadding(ptr, srcPtr, mSize, count); \
230 fnc(__VA_ARGS__); \
231 } else { \
232 fnc(__VA_ARGS__); \
233 copyWithUnPadding(srcPtr, ptr, mSize, count); \
234 } \
235 free(ptr); \
236 ptr = srcPtr; \
237 } else { \
238 fnc(__VA_ARGS__); \
239 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700240 _env->ReleaseLongArrayElements((jlongArray)data, (jlong *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800241 return; \
242 default: \
243 break; \
244 } \
Miao Wang87e908d2015-03-02 15:15:15 -0800245 UNUSED(len, ptr, srcPtr, typeBytes, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800246}
247
248
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800249class AutoJavaStringToUTF8 {
250public:
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800251 AutoJavaStringToUTF8(JNIEnv* env, jstring str) : fEnv(env), fJStr(str) {
Chris Wailes488230c32014-08-14 11:22:40 -0700252 fCStr = env->GetStringUTFChars(str, nullptr);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800253 fLength = env->GetStringUTFLength(str);
254 }
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800255 ~AutoJavaStringToUTF8() {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800256 fEnv->ReleaseStringUTFChars(fJStr, fCStr);
257 }
258 const char* c_str() const { return fCStr; }
259 jsize length() const { return fLength; }
260
261private:
262 JNIEnv* fEnv;
263 jstring fJStr;
264 const char* fCStr;
265 jsize fLength;
266};
267
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800268class AutoJavaStringArrayToUTF8 {
269public:
270 AutoJavaStringArrayToUTF8(JNIEnv* env, jobjectArray strings, jsize stringsLength)
271 : mEnv(env), mStrings(strings), mStringsLength(stringsLength) {
Chris Wailes488230c32014-08-14 11:22:40 -0700272 mCStrings = nullptr;
273 mSizeArray = nullptr;
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800274 if (stringsLength > 0) {
275 mCStrings = (const char **)calloc(stringsLength, sizeof(char *));
276 mSizeArray = (size_t*)calloc(stringsLength, sizeof(size_t));
277 for (jsize ct = 0; ct < stringsLength; ct ++) {
278 jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
Chris Wailes488230c32014-08-14 11:22:40 -0700279 mCStrings[ct] = mEnv->GetStringUTFChars(s, nullptr);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800280 mSizeArray[ct] = mEnv->GetStringUTFLength(s);
281 }
282 }
283 }
284 ~AutoJavaStringArrayToUTF8() {
285 for (jsize ct=0; ct < mStringsLength; ct++) {
286 jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
287 mEnv->ReleaseStringUTFChars(s, mCStrings[ct]);
288 }
289 free(mCStrings);
290 free(mSizeArray);
291 }
292 const char **c_str() const { return mCStrings; }
293 size_t *c_str_len() const { return mSizeArray; }
294 jsize length() const { return mStringsLength; }
295
296private:
297 JNIEnv *mEnv;
298 jobjectArray mStrings;
299 const char **mCStrings;
300 size_t *mSizeArray;
301 jsize mStringsLength;
302};
303
Jason Samsd19f10d2009-05-22 14:03:28 -0700304// ---------------------------------------------------------------------------
305
Jason Samsffe9f482009-06-01 17:45:53 -0700306static jfieldID gContextId = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -0700307
308static void _nInit(JNIEnv *_env, jclass _this)
309{
Tim Murrayeff663f2013-11-15 13:08:30 -0800310 gContextId = _env->GetFieldID(_this, "mContext", "J");
Jason Samsd19f10d2009-05-22 14:03:28 -0700311}
312
Jason Samsd19f10d2009-05-22 14:03:28 -0700313// ---------------------------------------------------------------------------
314
Miao Wang87e908d2015-03-02 15:15:15 -0800315static void copyWithPadding(void* ptr, void* srcPtr, int mSize, int count) {
316 int sizeBytesPad = mSize * 4;
317 int sizeBytes = mSize * 3;
318 uint8_t *dst = static_cast<uint8_t *>(ptr);
319 uint8_t *src = static_cast<uint8_t *>(srcPtr);
320 for (int i = 0; i < count; i++) {
321 memcpy(dst, src, sizeBytes);
322 dst += sizeBytesPad;
323 src += sizeBytes;
324 }
325}
326
327static void copyWithUnPadding(void* ptr, void* srcPtr, int mSize, int count) {
328 int sizeBytesPad = mSize * 4;
329 int sizeBytes = mSize * 3;
330 uint8_t *dst = static_cast<uint8_t *>(ptr);
331 uint8_t *src = static_cast<uint8_t *>(srcPtr);
332 for (int i = 0; i < count; i++) {
333 memcpy(dst, src, sizeBytes);
334 dst += sizeBytes;
335 src += sizeBytesPad;
336 }
337}
338
339
340// ---------------------------------------------------------------------------
Jason Sams3eaa338e2009-06-10 15:04:38 -0700341static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800342nContextFinish(JNIEnv *_env, jobject _this, jlong con)
Jason Sams96ed4cf2010-06-15 12:15:57 -0700343{
Andreas Gampe67333922014-11-10 20:35:59 -0800344 if (kLogApi) {
345 ALOGD("nContextFinish, con(%p)", (RsContext)con);
346 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800347 rsContextFinish((RsContext)con);
Jason Sams96ed4cf2010-06-15 12:15:57 -0700348}
349
Yang Ni281c3252014-10-24 08:52:24 -0700350static jlong
351nClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong kernelID,
352 jlong returnValue, jlongArray fieldIDArray,
353 jlongArray valueArray, jintArray sizeArray,
354 jlongArray depClosureArray, jlongArray depFieldIDArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700355 jlong ret = 0;
356
Yang Ni281c3252014-10-24 08:52:24 -0700357 jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
358 jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700359 if (jFieldIDs == nullptr) {
360 ALOGE("Failed to get Java array elements: fieldIDs.");
361 return ret;
362 }
363
Yang Ni281c3252014-10-24 08:52:24 -0700364 jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
365 jsize values_length = _env->GetArrayLength(valueArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700366 if (jValues == nullptr) {
367 ALOGE("Failed to get Java array elements: values.");
368 return ret;
369 }
370
Yang Ni17c2d7a2015-04-30 16:13:54 -0700371 jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
Yang Ni281c3252014-10-24 08:52:24 -0700372 jsize sizes_length = _env->GetArrayLength(sizeArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700373 if (jSizes == nullptr) {
374 ALOGE("Failed to get Java array elements: sizes.");
375 return ret;
376 }
377
Yang Ni281c3252014-10-24 08:52:24 -0700378 jlong* jDepClosures =
379 _env->GetLongArrayElements(depClosureArray, nullptr);
380 jsize depClosures_length = _env->GetArrayLength(depClosureArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700381 if (jDepClosures == nullptr) {
382 ALOGE("Failed to get Java array elements: depClosures.");
383 return ret;
384 }
385
Yang Ni281c3252014-10-24 08:52:24 -0700386 jlong* jDepFieldIDs =
387 _env->GetLongArrayElements(depFieldIDArray, nullptr);
388 jsize depFieldIDs_length = _env->GetArrayLength(depFieldIDArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700389 if (jDepFieldIDs == nullptr) {
390 ALOGE("Failed to get Java array elements: depFieldIDs.");
391 return ret;
392 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700393
394 size_t numValues, numDependencies;
395 RsScriptFieldID* fieldIDs;
Yang Ni17c2d7a2015-04-30 16:13:54 -0700396 RsClosure* depClosures;
397 RsScriptFieldID* depFieldIDs;
398
399 if (fieldIDs_length != values_length || values_length != sizes_length) {
400 ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
401 goto exit;
402 }
403
404 numValues = (size_t)fieldIDs_length;
405
406 if (depClosures_length != depFieldIDs_length) {
407 ALOGE("Unmatched closures and field IDs for dependencies in closure creation.");
408 goto exit;
409 }
410
411 numDependencies = (size_t)depClosures_length;
412
413 if (numDependencies > numValues) {
414 ALOGE("Unexpected number of dependencies in closure creation");
415 goto exit;
416 }
417
Yang Ni7b2a46f2015-05-05 12:41:19 -0700418 if (numValues > RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700419 ALOGE("Too many arguments or globals in closure creation");
420 goto exit;
421 }
422
423 fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
424 if (fieldIDs == nullptr) {
425 goto exit;
426 }
427
428 for (size_t i = 0; i < numValues; i++) {
429 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
430 }
431
Yang Ni17c2d7a2015-04-30 16:13:54 -0700432 depClosures = (RsClosure*)alloca(sizeof(RsClosure) * numDependencies);
433 if (depClosures == nullptr) {
434 goto exit;
435 }
436
437 for (size_t i = 0; i < numDependencies; i++) {
438 depClosures[i] = (RsClosure)jDepClosures[i];
439 }
440
441 depFieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numDependencies);
442 if (depFieldIDs == nullptr) {
443 goto exit;
444 }
445
446 for (size_t i = 0; i < numDependencies; i++) {
Yang Ni281c3252014-10-24 08:52:24 -0700447 depFieldIDs[i] = (RsClosure)jDepFieldIDs[i];
448 }
449
Yang Ni17c2d7a2015-04-30 16:13:54 -0700450 ret = (jlong)(uintptr_t)rsClosureCreate(
Yang Ni281c3252014-10-24 08:52:24 -0700451 (RsContext)con, (RsScriptKernelID)kernelID, (RsAllocation)returnValue,
Yang Ni263cc902015-11-10 13:27:04 -0800452 fieldIDs, numValues, jValues, numValues,
Yang Ni17c2d7a2015-04-30 16:13:54 -0700453 (int*)jSizes, numValues,
454 depClosures, numDependencies,
455 depFieldIDs, numDependencies);
456
457exit:
458
459 _env->ReleaseLongArrayElements(depFieldIDArray, jDepFieldIDs, JNI_ABORT);
460 _env->ReleaseLongArrayElements(depClosureArray, jDepClosures, JNI_ABORT);
461 _env->ReleaseIntArrayElements (sizeArray, jSizes, JNI_ABORT);
462 _env->ReleaseLongArrayElements(valueArray, jValues, JNI_ABORT);
463 _env->ReleaseLongArrayElements(fieldIDArray, jFieldIDs, JNI_ABORT);
464
465 return ret;
Yang Ni281c3252014-10-24 08:52:24 -0700466}
467
Yang Nibe392ad2015-01-23 17:16:02 -0800468static jlong
469nInvokeClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong invokeID,
470 jbyteArray paramArray, jlongArray fieldIDArray, jlongArray valueArray,
471 jintArray sizeArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700472 jlong ret = 0;
473
Yang Nibe392ad2015-01-23 17:16:02 -0800474 jbyte* jParams = _env->GetByteArrayElements(paramArray, nullptr);
475 jsize jParamLength = _env->GetArrayLength(paramArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700476 if (jParams == nullptr) {
477 ALOGE("Failed to get Java array elements: params.");
478 return ret;
479 }
480
Yang Nibe392ad2015-01-23 17:16:02 -0800481 jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
482 jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700483 if (jFieldIDs == nullptr) {
484 ALOGE("Failed to get Java array elements: fieldIDs.");
485 return ret;
486 }
487
Yang Ni17c2d7a2015-04-30 16:13:54 -0700488 jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
489 jsize values_length = _env->GetArrayLength(valueArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700490 if (jValues == nullptr) {
491 ALOGE("Failed to get Java array elements: values.");
492 return ret;
493 }
494
Yang Ni17c2d7a2015-04-30 16:13:54 -0700495 jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
496 jsize sizes_length = _env->GetArrayLength(sizeArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700497 if (jSizes == nullptr) {
498 ALOGE("Failed to get Java array elements: sizes.");
499 return ret;
500 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700501
502 size_t numValues;
503 RsScriptFieldID* fieldIDs;
Yang Ni17c2d7a2015-04-30 16:13:54 -0700504
505 if (fieldIDs_length != values_length || values_length != sizes_length) {
506 ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
507 goto exit;
508 }
509
510 numValues = (size_t) fieldIDs_length;
511
Yang Ni7b2a46f2015-05-05 12:41:19 -0700512 if (numValues > RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700513 ALOGE("Too many arguments or globals in closure creation");
514 goto exit;
515 }
516
517 fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
518 if (fieldIDs == nullptr) {
519 goto exit;
520 }
521
522 for (size_t i = 0; i< numValues; i++) {
Yang Nibe392ad2015-01-23 17:16:02 -0800523 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
524 }
525
Yang Ni17c2d7a2015-04-30 16:13:54 -0700526 ret = (jlong)(uintptr_t)rsInvokeClosureCreate(
Yang Nibe392ad2015-01-23 17:16:02 -0800527 (RsContext)con, (RsScriptInvokeID)invokeID, jParams, jParamLength,
Yang Ni263cc902015-11-10 13:27:04 -0800528 fieldIDs, numValues, jValues, numValues,
Yang Ni17c2d7a2015-04-30 16:13:54 -0700529 (int*)jSizes, numValues);
530
531exit:
532
533 _env->ReleaseIntArrayElements (sizeArray, jSizes, JNI_ABORT);
534 _env->ReleaseLongArrayElements(valueArray, jValues, JNI_ABORT);
535 _env->ReleaseLongArrayElements(fieldIDArray, jFieldIDs, JNI_ABORT);
536 _env->ReleaseByteArrayElements(paramArray, jParams, JNI_ABORT);
537
538 return ret;
Yang Nibe392ad2015-01-23 17:16:02 -0800539}
540
Yang Ni281c3252014-10-24 08:52:24 -0700541static void
542nClosureSetArg(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
543 jint index, jlong value, jint size) {
Yang Ni263cc902015-11-10 13:27:04 -0800544 // Size is signed with -1 indicating the value is an Allocation
Yang Ni281c3252014-10-24 08:52:24 -0700545 rsClosureSetArg((RsContext)con, (RsClosure)closureID, (uint32_t)index,
Yang Ni263cc902015-11-10 13:27:04 -0800546 (uintptr_t)value, size);
Yang Ni281c3252014-10-24 08:52:24 -0700547}
548
549static void
550nClosureSetGlobal(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
551 jlong fieldID, jlong value, jint size) {
Yang Ni263cc902015-11-10 13:27:04 -0800552 // Size is signed with -1 indicating the value is an Allocation
Yang Ni281c3252014-10-24 08:52:24 -0700553 rsClosureSetGlobal((RsContext)con, (RsClosure)closureID,
Yang Ni263cc902015-11-10 13:27:04 -0800554 (RsScriptFieldID)fieldID, (int64_t)value, size);
Yang Ni281c3252014-10-24 08:52:24 -0700555}
556
557static long
Yang Ni35be56c2015-04-02 17:47:56 -0700558nScriptGroup2Create(JNIEnv *_env, jobject _this, jlong con, jstring name,
Yang Niebf63402015-01-16 11:06:26 -0800559 jstring cacheDir, jlongArray closureArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700560 jlong ret = 0;
561
Yang Ni35be56c2015-04-02 17:47:56 -0700562 AutoJavaStringToUTF8 nameUTF(_env, name);
Yang Niebf63402015-01-16 11:06:26 -0800563 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
564
Yang Ni281c3252014-10-24 08:52:24 -0700565 jlong* jClosures = _env->GetLongArrayElements(closureArray, nullptr);
566 jsize numClosures = _env->GetArrayLength(closureArray);
Miao Wangba8766c2015-10-12 17:24:13 -0700567 if (jClosures == nullptr) {
568 ALOGE("Failed to get Java array elements: closures.");
569 return ret;
570 }
Yang Ni17c2d7a2015-04-30 16:13:54 -0700571
572 RsClosure* closures;
573
Yang Ni7b2a46f2015-05-05 12:41:19 -0700574 if (numClosures > (jsize) RS_SCRIPT_GROUP_MAX_NUMBER_CLOSURES) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700575 ALOGE("Too many closures in script group");
576 goto exit;
577 }
578
579 closures = (RsClosure*)alloca(sizeof(RsClosure) * numClosures);
580 if (closures == nullptr) {
581 goto exit;
582 }
583
Yang Ni281c3252014-10-24 08:52:24 -0700584 for (int i = 0; i < numClosures; i++) {
585 closures[i] = (RsClosure)jClosures[i];
586 }
587
Yang Ni17c2d7a2015-04-30 16:13:54 -0700588 ret = (jlong)(uintptr_t)rsScriptGroup2Create(
Yang Ni35be56c2015-04-02 17:47:56 -0700589 (RsContext)con, nameUTF.c_str(), nameUTF.length(),
590 cacheDirUTF.c_str(), cacheDirUTF.length(),
Yang Niebf63402015-01-16 11:06:26 -0800591 closures, numClosures);
Yang Ni17c2d7a2015-04-30 16:13:54 -0700592
593exit:
594
595 _env->ReleaseLongArrayElements(closureArray, jClosures, JNI_ABORT);
596
597 return ret;
Yang Ni281c3252014-10-24 08:52:24 -0700598}
599
600static void
601nScriptGroup2Execute(JNIEnv *_env, jobject _this, jlong con, jlong groupID) {
602 rsScriptGroupExecute((RsContext)con, (RsScriptGroup2)groupID);
603}
604
Jason Sams96ed4cf2010-06-15 12:15:57 -0700605static void
Tim Murray25207df2015-01-12 16:47:56 -0800606nScriptIntrinsicBLAS_Single(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
607 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
608 jfloat alpha, jlong A, jlong B, jfloat beta, jlong C, jint incX, jint incY,
609 jint KL, jint KU) {
610 RsBlasCall call;
611 memset(&call, 0, sizeof(call));
612 call.func = (RsBlasFunction)func;
613 call.transA = (RsBlasTranspose)TransA;
614 call.transB = (RsBlasTranspose)TransB;
615 call.side = (RsBlasSide)Side;
616 call.uplo = (RsBlasUplo)Uplo;
617 call.diag = (RsBlasDiag)Diag;
618 call.M = M;
619 call.N = N;
620 call.K = K;
621 call.alpha.f = alpha;
622 call.beta.f = beta;
623 call.incX = incX;
624 call.incY = incY;
625 call.KL = KL;
626 call.KU = KU;
627
628 RsAllocation in_allocs[3];
629 in_allocs[0] = (RsAllocation)A;
630 in_allocs[1] = (RsAllocation)B;
631 in_allocs[2] = (RsAllocation)C;
632
633 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
634 in_allocs, sizeof(in_allocs), nullptr,
635 &call, sizeof(call), nullptr, 0);
636}
637
638static void
639nScriptIntrinsicBLAS_Double(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
640 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
641 jdouble alpha, jlong A, jlong B, jdouble beta, jlong C, jint incX, jint incY,
642 jint KL, jint KU) {
643 RsBlasCall call;
644 memset(&call, 0, sizeof(call));
645 call.func = (RsBlasFunction)func;
646 call.transA = (RsBlasTranspose)TransA;
647 call.transB = (RsBlasTranspose)TransB;
648 call.side = (RsBlasSide)Side;
649 call.uplo = (RsBlasUplo)Uplo;
650 call.diag = (RsBlasDiag)Diag;
651 call.M = M;
652 call.N = N;
653 call.K = K;
654 call.alpha.d = alpha;
655 call.beta.d = beta;
656 call.incX = incX;
657 call.incY = incY;
658 call.KL = KL;
659 call.KU = KU;
660
661 RsAllocation in_allocs[3];
662 in_allocs[0] = (RsAllocation)A;
663 in_allocs[1] = (RsAllocation)B;
664 in_allocs[2] = (RsAllocation)C;
665
666 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700667 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800668 &call, sizeof(call), nullptr, 0);
669}
670
671static void
672nScriptIntrinsicBLAS_Complex(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
673 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
674 jfloat alphaX, jfloat alphaY, jlong A, jlong B, jfloat betaX,
675 jfloat betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
676 RsBlasCall call;
677 memset(&call, 0, sizeof(call));
678 call.func = (RsBlasFunction)func;
679 call.transA = (RsBlasTranspose)TransA;
680 call.transB = (RsBlasTranspose)TransB;
681 call.side = (RsBlasSide)Side;
682 call.uplo = (RsBlasUplo)Uplo;
683 call.diag = (RsBlasDiag)Diag;
684 call.M = M;
685 call.N = N;
686 call.K = K;
687 call.alpha.c.r = alphaX;
688 call.alpha.c.i = alphaY;
689 call.beta.c.r = betaX;
Miao Wang82585b32015-04-30 13:44:49 -0700690 call.beta.c.i = betaY;
Tim Murray25207df2015-01-12 16:47:56 -0800691 call.incX = incX;
692 call.incY = incY;
693 call.KL = KL;
694 call.KU = KU;
695
696 RsAllocation in_allocs[3];
697 in_allocs[0] = (RsAllocation)A;
698 in_allocs[1] = (RsAllocation)B;
699 in_allocs[2] = (RsAllocation)C;
700
701 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700702 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800703 &call, sizeof(call), nullptr, 0);
704}
705
706static void
707nScriptIntrinsicBLAS_Z(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
708 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
709 jdouble alphaX, jdouble alphaY, jlong A, jlong B, jdouble betaX,
710 jdouble betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
711 RsBlasCall call;
712 memset(&call, 0, sizeof(call));
713 call.func = (RsBlasFunction)func;
714 call.transA = (RsBlasTranspose)TransA;
715 call.transB = (RsBlasTranspose)TransB;
716 call.side = (RsBlasSide)Side;
717 call.uplo = (RsBlasUplo)Uplo;
718 call.diag = (RsBlasDiag)Diag;
719 call.M = M;
720 call.N = N;
721 call.K = K;
722 call.alpha.z.r = alphaX;
723 call.alpha.z.i = alphaY;
724 call.beta.z.r = betaX;
Miao Wang82585b32015-04-30 13:44:49 -0700725 call.beta.z.i = betaY;
Tim Murray25207df2015-01-12 16:47:56 -0800726 call.incX = incX;
727 call.incY = incY;
728 call.KL = KL;
729 call.KU = KU;
730
731 RsAllocation in_allocs[3];
732 in_allocs[0] = (RsAllocation)A;
733 in_allocs[1] = (RsAllocation)B;
734 in_allocs[2] = (RsAllocation)C;
735
736 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700737 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray25207df2015-01-12 16:47:56 -0800738 &call, sizeof(call), nullptr, 0);
739}
740
741
742static void
Tim Murray9cb16a22015-04-01 11:07:16 -0700743nScriptIntrinsicBLAS_BNNM(JNIEnv *_env, jobject _this, jlong con, jlong id, jint M, jint N, jint K,
744 jlong A, jint a_offset, jlong B, jint b_offset, jlong C, jint c_offset,
745 jint c_mult_int) {
746 RsBlasCall call;
747 memset(&call, 0, sizeof(call));
748 call.func = RsBlas_bnnm;
749 call.M = M;
750 call.N = N;
751 call.K = K;
Miao Wang25148062015-06-29 17:43:03 -0700752 call.a_offset = a_offset & 0xFF;
753 call.b_offset = b_offset & 0xFF;
Tim Murray9cb16a22015-04-01 11:07:16 -0700754 call.c_offset = c_offset;
755 call.c_mult_int = c_mult_int;
756
757 RsAllocation in_allocs[3];
758 in_allocs[0] = (RsAllocation)A;
759 in_allocs[1] = (RsAllocation)B;
760 in_allocs[2] = (RsAllocation)C;
761
762 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
Miao Wang0b34f2a2015-09-30 15:38:07 -0700763 in_allocs, NELEM(in_allocs), nullptr,
Tim Murray9cb16a22015-04-01 11:07:16 -0700764 &call, sizeof(call), nullptr, 0);
765}
766
767
768static void
Tim Murray460a0492013-11-19 12:45:54 -0800769nAssignName(JNIEnv *_env, jobject _this, jlong con, jlong obj, jbyteArray str)
Jason Sams3eaa338e2009-06-10 15:04:38 -0700770{
Andreas Gampe67333922014-11-10 20:35:59 -0800771 if (kLogApi) {
772 ALOGD("nAssignName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
773 }
Jason Sams3eaa338e2009-06-10 15:04:38 -0700774 jint len = _env->GetArrayLength(str);
775 jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
Miao Wangba8766c2015-10-12 17:24:13 -0700776 if (cptr == nullptr) {
777 ALOGE("Failed to get Java array elements");
778 return;
779 }
780
Tim Murrayeff663f2013-11-15 13:08:30 -0800781 rsAssignName((RsContext)con, (void *)obj, (const char *)cptr, len);
Jason Sams3eaa338e2009-06-10 15:04:38 -0700782 _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
783}
784
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700785static jstring
Tim Murray460a0492013-11-19 12:45:54 -0800786nGetName(JNIEnv *_env, jobject _this, jlong con, jlong obj)
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700787{
Andreas Gampe67333922014-11-10 20:35:59 -0800788 if (kLogApi) {
789 ALOGD("nGetName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
790 }
Chris Wailes488230c32014-08-14 11:22:40 -0700791 const char *name = nullptr;
Tim Murrayeff663f2013-11-15 13:08:30 -0800792 rsaGetName((RsContext)con, (void *)obj, &name);
Chris Wailes488230c32014-08-14 11:22:40 -0700793 if(name == nullptr || strlen(name) == 0) {
794 return nullptr;
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700795 }
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700796 return _env->NewStringUTF(name);
797}
798
Jason Sams7ce033d2009-08-18 14:14:24 -0700799static void
Tim Murray460a0492013-11-19 12:45:54 -0800800nObjDestroy(JNIEnv *_env, jobject _this, jlong con, jlong obj)
Jason Sams7ce033d2009-08-18 14:14:24 -0700801{
Andreas Gampe67333922014-11-10 20:35:59 -0800802 if (kLogApi) {
803 ALOGD("nObjDestroy, con(%p) obj(%p)", (RsContext)con, (void *)obj);
804 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800805 rsObjDestroy((RsContext)con, (void *)obj);
Jason Sams7ce033d2009-08-18 14:14:24 -0700806}
807
Jason Sams3eaa338e2009-06-10 15:04:38 -0700808// ---------------------------------------------------------------------------
809
Tim Murrayeff663f2013-11-15 13:08:30 -0800810static jlong
Jason Samsd19f10d2009-05-22 14:03:28 -0700811nDeviceCreate(JNIEnv *_env, jobject _this)
812{
Andreas Gampe67333922014-11-10 20:35:59 -0800813 if (kLogApi) {
814 ALOGD("nDeviceCreate");
815 }
Tim Murray3aa89c12014-08-18 17:51:22 -0700816 return (jlong)(uintptr_t)rsDeviceCreate();
Jason Samsd19f10d2009-05-22 14:03:28 -0700817}
818
819static void
Tim Murray5eaf4682014-01-10 11:25:52 -0800820nDeviceDestroy(JNIEnv *_env, jobject _this, jlong dev)
Jason Samsd19f10d2009-05-22 14:03:28 -0700821{
Andreas Gampe67333922014-11-10 20:35:59 -0800822 if (kLogApi) {
823 ALOGD("nDeviceDestroy");
824 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700825 return rsDeviceDestroy((RsDevice)dev);
826}
827
Jason Samsebfb4362009-09-23 13:57:02 -0700828static void
Tim Murray5eaf4682014-01-10 11:25:52 -0800829nDeviceSetConfig(JNIEnv *_env, jobject _this, jlong dev, jint p, jint value)
Jason Samsebfb4362009-09-23 13:57:02 -0700830{
Andreas Gampe67333922014-11-10 20:35:59 -0800831 if (kLogApi) {
832 ALOGD("nDeviceSetConfig dev(%p), param(%i), value(%i)", (void *)dev, p, value);
833 }
Jason Samsebfb4362009-09-23 13:57:02 -0700834 return rsDeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value);
835}
836
Tim Murrayeff663f2013-11-15 13:08:30 -0800837static jlong
Jason Sams81cd2b12014-12-02 12:36:43 -0800838nContextCreate(JNIEnv *_env, jobject _this, jlong dev, jint flags, jint sdkVer, jint contextType)
Jason Samsd19f10d2009-05-22 14:03:28 -0700839{
Andreas Gampe67333922014-11-10 20:35:59 -0800840 if (kLogApi) {
841 ALOGD("nContextCreate");
842 }
Jason Sams81cd2b12014-12-02 12:36:43 -0800843 return (jlong)(uintptr_t)rsContextCreate((RsDevice)dev, 0, sdkVer, (RsContextType)contextType, flags);
Jason Sams704ff642010-02-09 16:05:07 -0800844}
845
Tim Murrayeff663f2013-11-15 13:08:30 -0800846static jlong
Tim Murray5eaf4682014-01-10 11:25:52 -0800847nContextCreateGL(JNIEnv *_env, jobject _this, jlong dev, jint ver, jint sdkVer,
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000848 jint colorMin, jint colorPref,
849 jint alphaMin, jint alphaPref,
850 jint depthMin, jint depthPref,
851 jint stencilMin, jint stencilPref,
852 jint samplesMin, jint samplesPref, jfloat samplesQ,
853 jint dpi)
Jason Sams704ff642010-02-09 16:05:07 -0800854{
Jason Sams11c8af92010-10-13 15:31:10 -0700855 RsSurfaceConfig sc;
856 sc.alphaMin = alphaMin;
857 sc.alphaPref = alphaPref;
858 sc.colorMin = colorMin;
859 sc.colorPref = colorPref;
860 sc.depthMin = depthMin;
861 sc.depthPref = depthPref;
862 sc.samplesMin = samplesMin;
863 sc.samplesPref = samplesPref;
864 sc.samplesQ = samplesQ;
865
Andreas Gampe67333922014-11-10 20:35:59 -0800866 if (kLogApi) {
867 ALOGD("nContextCreateGL");
868 }
Tim Murray3aa89c12014-08-18 17:51:22 -0700869 return (jlong)(uintptr_t)rsContextCreateGL((RsDevice)dev, ver, sdkVer, sc, dpi);
Jason Samsd19f10d2009-05-22 14:03:28 -0700870}
871
872static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800873nContextSetPriority(JNIEnv *_env, jobject _this, jlong con, jint p)
Jason Sams7d787b42009-11-15 12:14:26 -0800874{
Andreas Gampe67333922014-11-10 20:35:59 -0800875 if (kLogApi) {
876 ALOGD("ContextSetPriority, con(%p), priority(%i)", (RsContext)con, p);
877 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800878 rsContextSetPriority((RsContext)con, p);
Jason Sams7d787b42009-11-15 12:14:26 -0800879}
880
Tim Murray47f31582015-04-07 15:43:24 -0700881static void
882nContextSetCacheDir(JNIEnv *_env, jobject _this, jlong con, jstring cacheDir)
883{
884 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
885
886 if (kLogApi) {
887 ALOGD("ContextSetCacheDir, con(%p), cacheDir(%s)", (RsContext)con, cacheDirUTF.c_str());
888 }
889 rsContextSetCacheDir((RsContext)con, cacheDirUTF.c_str(), cacheDirUTF.length());
890}
891
Jason Sams7d787b42009-11-15 12:14:26 -0800892
893
894static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800895nContextSetSurface(JNIEnv *_env, jobject _this, jlong con, jint width, jint height, jobject wnd)
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800896{
Andreas Gampe67333922014-11-10 20:35:59 -0800897 if (kLogApi) {
898 ALOGD("nContextSetSurface, con(%p), width(%i), height(%i), surface(%p)", (RsContext)con,
899 width, height, (Surface *)wnd);
900 }
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800901
Chris Wailes488230c32014-08-14 11:22:40 -0700902 ANativeWindow * window = nullptr;
903 if (wnd == nullptr) {
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800904
905 } else {
Jeff Brown64a55af2012-08-26 02:47:39 -0700906 window = android_view_Surface_getNativeWindow(_env, wnd).get();
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800907 }
908
Tim Murrayeff663f2013-11-15 13:08:30 -0800909 rsContextSetSurface((RsContext)con, width, height, window);
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800910}
911
912static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800913nContextDestroy(JNIEnv *_env, jobject _this, jlong con)
Jason Samsd19f10d2009-05-22 14:03:28 -0700914{
Andreas Gampe67333922014-11-10 20:35:59 -0800915 if (kLogApi) {
916 ALOGD("nContextDestroy, con(%p)", (RsContext)con);
917 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800918 rsContextDestroy((RsContext)con);
Jason Samsd19f10d2009-05-22 14:03:28 -0700919}
920
Jason Sams715333b2009-11-17 17:26:46 -0800921static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800922nContextDump(JNIEnv *_env, jobject _this, jlong con, jint bits)
Jason Sams715333b2009-11-17 17:26:46 -0800923{
Andreas Gampe67333922014-11-10 20:35:59 -0800924 if (kLogApi) {
925 ALOGD("nContextDump, con(%p) bits(%i)", (RsContext)con, bits);
926 }
Jason Sams715333b2009-11-17 17:26:46 -0800927 rsContextDump((RsContext)con, bits);
928}
Jason Samsd19f10d2009-05-22 14:03:28 -0700929
930static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800931nContextPause(JNIEnv *_env, jobject _this, jlong con)
Jason Sams65e7aa52009-09-24 17:38:20 -0700932{
Andreas Gampe67333922014-11-10 20:35:59 -0800933 if (kLogApi) {
934 ALOGD("nContextPause, con(%p)", (RsContext)con);
935 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800936 rsContextPause((RsContext)con);
Jason Sams65e7aa52009-09-24 17:38:20 -0700937}
938
939static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800940nContextResume(JNIEnv *_env, jobject _this, jlong con)
Jason Sams65e7aa52009-09-24 17:38:20 -0700941{
Andreas Gampe67333922014-11-10 20:35:59 -0800942 if (kLogApi) {
943 ALOGD("nContextResume, con(%p)", (RsContext)con);
944 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800945 rsContextResume((RsContext)con);
Jason Sams65e7aa52009-09-24 17:38:20 -0700946}
947
Jason Sams1c415172010-11-08 17:06:46 -0800948
949static jstring
Tim Murrayeff663f2013-11-15 13:08:30 -0800950nContextGetErrorMessage(JNIEnv *_env, jobject _this, jlong con)
Jason Sams1c415172010-11-08 17:06:46 -0800951{
Andreas Gampe67333922014-11-10 20:35:59 -0800952 if (kLogApi) {
953 ALOGD("nContextGetErrorMessage, con(%p)", (RsContext)con);
954 }
Jason Sams1c415172010-11-08 17:06:46 -0800955 char buf[1024];
956
957 size_t receiveLen;
958 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800959 int id = rsContextGetMessage((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -0700960 buf, sizeof(buf),
961 &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -0700962 &subID, sizeof(subID));
Jason Sams1c415172010-11-08 17:06:46 -0800963 if (!id && receiveLen) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +0100964 ALOGV("message receive buffer too small. %zu", receiveLen);
Jason Sams1c415172010-11-08 17:06:46 -0800965 }
966 return _env->NewStringUTF(buf);
967}
968
Jason Samsedbfabd2011-05-17 15:01:29 -0700969static jint
Tim Murrayeff663f2013-11-15 13:08:30 -0800970nContextGetUserMessage(JNIEnv *_env, jobject _this, jlong con, jintArray data)
Jason Sams516c3192009-10-06 13:58:47 -0700971{
Jason Sams516c3192009-10-06 13:58:47 -0700972 jint len = _env->GetArrayLength(data);
Andreas Gampe67333922014-11-10 20:35:59 -0800973 if (kLogApi) {
974 ALOGD("nContextGetMessage, con(%p), len(%i)", (RsContext)con, len);
975 }
Chris Wailes488230c32014-08-14 11:22:40 -0700976 jint *ptr = _env->GetIntArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -0700977 if (ptr == nullptr) {
978 ALOGE("Failed to get Java array elements");
979 return 0;
980 }
Jason Sams516c3192009-10-06 13:58:47 -0700981 size_t receiveLen;
Jason Sams1c415172010-11-08 17:06:46 -0800982 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800983 int id = rsContextGetMessage((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -0700984 ptr, len * 4,
985 &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -0700986 &subID, sizeof(subID));
Jason Sams516c3192009-10-06 13:58:47 -0700987 if (!id && receiveLen) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +0100988 ALOGV("message receive buffer too small. %zu", receiveLen);
Jason Sams516c3192009-10-06 13:58:47 -0700989 }
990 _env->ReleaseIntArrayElements(data, ptr, 0);
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000991 return (jint)id;
Jason Sams1c415172010-11-08 17:06:46 -0800992}
993
994static jint
Tim Murrayeff663f2013-11-15 13:08:30 -0800995nContextPeekMessage(JNIEnv *_env, jobject _this, jlong con, jintArray auxData)
Jason Sams1c415172010-11-08 17:06:46 -0800996{
Andreas Gampe67333922014-11-10 20:35:59 -0800997 if (kLogApi) {
998 ALOGD("nContextPeekMessage, con(%p)", (RsContext)con);
999 }
Chris Wailes488230c32014-08-14 11:22:40 -07001000 jint *auxDataPtr = _env->GetIntArrayElements(auxData, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001001 if (auxDataPtr == nullptr) {
1002 ALOGE("Failed to get Java array elements");
1003 return 0;
1004 }
Jason Sams1c415172010-11-08 17:06:46 -08001005 size_t receiveLen;
1006 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -08001007 int id = rsContextPeekMessage((RsContext)con, &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -07001008 &subID, sizeof(subID));
Jason Sams1c415172010-11-08 17:06:46 -08001009 auxDataPtr[0] = (jint)subID;
1010 auxDataPtr[1] = (jint)receiveLen;
1011 _env->ReleaseIntArrayElements(auxData, auxDataPtr, 0);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001012 return (jint)id;
Jason Sams516c3192009-10-06 13:58:47 -07001013}
1014
Tim Murrayeff663f2013-11-15 13:08:30 -08001015static void nContextInitToClient(JNIEnv *_env, jobject _this, jlong con)
Jason Sams516c3192009-10-06 13:58:47 -07001016{
Andreas Gampe67333922014-11-10 20:35:59 -08001017 if (kLogApi) {
1018 ALOGD("nContextInitToClient, con(%p)", (RsContext)con);
1019 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001020 rsContextInitToClient((RsContext)con);
Jason Sams516c3192009-10-06 13:58:47 -07001021}
1022
Tim Murrayeff663f2013-11-15 13:08:30 -08001023static void nContextDeinitToClient(JNIEnv *_env, jobject _this, jlong con)
Jason Sams516c3192009-10-06 13:58:47 -07001024{
Andreas Gampe67333922014-11-10 20:35:59 -08001025 if (kLogApi) {
1026 ALOGD("nContextDeinitToClient, con(%p)", (RsContext)con);
1027 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001028 rsContextDeinitToClient((RsContext)con);
Jason Sams516c3192009-10-06 13:58:47 -07001029}
1030
Jason Sams455d6442013-02-05 19:20:18 -08001031static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001032nContextSendMessage(JNIEnv *_env, jobject _this, jlong con, jint id, jintArray data)
Jason Sams455d6442013-02-05 19:20:18 -08001033{
Chris Wailes488230c32014-08-14 11:22:40 -07001034 jint *ptr = nullptr;
Jason Sams455d6442013-02-05 19:20:18 -08001035 jint len = 0;
1036 if (data) {
1037 len = _env->GetArrayLength(data);
Stephen Hines4a043c12014-08-21 23:20:32 -07001038 ptr = _env->GetIntArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001039 if (ptr == nullptr) {
1040 ALOGE("Failed to get Java array elements");
1041 return;
1042 }
Jason Sams455d6442013-02-05 19:20:18 -08001043 }
Andreas Gampe67333922014-11-10 20:35:59 -08001044 if (kLogApi) {
1045 ALOGD("nContextSendMessage, con(%p), id(%i), len(%i)", (RsContext)con, id, len);
1046 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001047 rsContextSendMessage((RsContext)con, id, (const uint8_t *)ptr, len * sizeof(int));
Jason Sams455d6442013-02-05 19:20:18 -08001048 if (data) {
1049 _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
1050 }
1051}
1052
1053
Jason Sams516c3192009-10-06 13:58:47 -07001054
Tim Murray460a0492013-11-19 12:45:54 -08001055static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001056nElementCreate(JNIEnv *_env, jobject _this, jlong con, jlong type, jint kind, jboolean norm,
1057 jint size)
Jason Samsd19f10d2009-05-22 14:03:28 -07001058{
Andreas Gampe67333922014-11-10 20:35:59 -08001059 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001060 ALOGD("nElementCreate, con(%p), type(%" PRId64 "), kind(%i), norm(%i), size(%i)", (RsContext)con,
Andreas Gampe67333922014-11-10 20:35:59 -08001061 type, kind, norm, size);
1062 }
1063 return (jlong)(uintptr_t)rsElementCreate((RsContext)con, (RsDataType)type, (RsDataKind)kind,
1064 norm, size);
Jason Samsd19f10d2009-05-22 14:03:28 -07001065}
1066
Tim Murray460a0492013-11-19 12:45:54 -08001067static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001068nElementCreate2(JNIEnv *_env, jobject _this, jlong con,
Ashok Bhat98071552014-02-12 09:54:43 +00001069 jlongArray _ids, jobjectArray _names, jintArray _arraySizes)
Jason Samsd19f10d2009-05-22 14:03:28 -07001070{
Jason Sams718cd1f2009-12-23 14:35:29 -08001071 int fieldCount = _env->GetArrayLength(_ids);
Andreas Gampe67333922014-11-10 20:35:59 -08001072 if (kLogApi) {
1073 ALOGD("nElementCreate2, con(%p)", (RsContext)con);
1074 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001075
Chris Wailes488230c32014-08-14 11:22:40 -07001076 jlong *jIds = _env->GetLongArrayElements(_ids, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001077 if (jIds == nullptr) {
1078 ALOGE("Failed to get Java array elements: ids");
1079 return 0;
1080 }
Chris Wailes488230c32014-08-14 11:22:40 -07001081 jint *jArraySizes = _env->GetIntArrayElements(_arraySizes, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001082 if (jArraySizes == nullptr) {
1083 ALOGE("Failed to get Java array elements: arraySizes");
1084 return 0;
1085 }
Ashok Bhat98071552014-02-12 09:54:43 +00001086
1087 RsElement *ids = (RsElement*)malloc(fieldCount * sizeof(RsElement));
1088 uint32_t *arraySizes = (uint32_t *)malloc(fieldCount * sizeof(uint32_t));
1089
1090 for(int i = 0; i < fieldCount; i ++) {
1091 ids[i] = (RsElement)jIds[i];
1092 arraySizes[i] = (uint32_t)jArraySizes[i];
1093 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001094
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08001095 AutoJavaStringArrayToUTF8 names(_env, _names, fieldCount);
1096
1097 const char **nameArray = names.c_str();
1098 size_t *sizeArray = names.c_str_len();
1099
Tim Murray3aa89c12014-08-18 17:51:22 -07001100 jlong id = (jlong)(uintptr_t)rsElementCreate2((RsContext)con,
Ashok Bhat98071552014-02-12 09:54:43 +00001101 (const RsElement *)ids, fieldCount,
Jason Sams7a22e102011-05-06 14:14:30 -07001102 nameArray, fieldCount * sizeof(size_t), sizeArray,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001103 (const uint32_t *)arraySizes, fieldCount);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08001104
Ashok Bhat98071552014-02-12 09:54:43 +00001105 free(ids);
1106 free(arraySizes);
1107 _env->ReleaseLongArrayElements(_ids, jIds, JNI_ABORT);
1108 _env->ReleaseIntArrayElements(_arraySizes, jArraySizes, JNI_ABORT);
1109
Tim Murray3aa89c12014-08-18 17:51:22 -07001110 return (jlong)(uintptr_t)id;
Jason Samsd19f10d2009-05-22 14:03:28 -07001111}
1112
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001113static void
Tim Murray460a0492013-11-19 12:45:54 -08001114nElementGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jintArray _elementData)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001115{
1116 int dataSize = _env->GetArrayLength(_elementData);
Andreas Gampe67333922014-11-10 20:35:59 -08001117 if (kLogApi) {
1118 ALOGD("nElementGetNativeData, con(%p)", (RsContext)con);
1119 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001120
1121 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
1122 assert(dataSize == 5);
1123
Narayan Kamath78c0ce52014-03-19 10:15:51 +00001124 uintptr_t elementData[5];
Tim Murrayeff663f2013-11-15 13:08:30 -08001125 rsaElementGetNativeData((RsContext)con, (RsElement)id, elementData, dataSize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001126
1127 for(jint i = 0; i < dataSize; i ++) {
Ashok Bhat98071552014-02-12 09:54:43 +00001128 const jint data = (jint)elementData[i];
1129 _env->SetIntArrayRegion(_elementData, i, 1, &data);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001130 }
1131}
1132
1133
1134static void
Tim Murray460a0492013-11-19 12:45:54 -08001135nElementGetSubElements(JNIEnv *_env, jobject _this, jlong con, jlong id,
Ashok Bhat98071552014-02-12 09:54:43 +00001136 jlongArray _IDs,
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -07001137 jobjectArray _names,
1138 jintArray _arraySizes)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001139{
Ashok Bhat98071552014-02-12 09:54:43 +00001140 uint32_t dataSize = _env->GetArrayLength(_IDs);
Andreas Gampe67333922014-11-10 20:35:59 -08001141 if (kLogApi) {
1142 ALOGD("nElementGetSubElements, con(%p)", (RsContext)con);
1143 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001144
Ashok Bhat98071552014-02-12 09:54:43 +00001145 uintptr_t *ids = (uintptr_t*)malloc(dataSize * sizeof(uintptr_t));
1146 const char **names = (const char **)malloc(dataSize * sizeof(const char *));
Narayan Kamath78c0ce52014-03-19 10:15:51 +00001147 uint32_t *arraySizes = (uint32_t *)malloc(dataSize * sizeof(uint32_t));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001148
Andreas Gampe67333922014-11-10 20:35:59 -08001149 rsaElementGetSubElements((RsContext)con, (RsElement)id, ids, names, arraySizes,
1150 (uint32_t)dataSize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001151
Ashok Bhat98071552014-02-12 09:54:43 +00001152 for(uint32_t i = 0; i < dataSize; i++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07001153 const jlong id = (jlong)(uintptr_t)ids[i];
Ashok Bhat98071552014-02-12 09:54:43 +00001154 const jint arraySize = (jint)arraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001155 _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
Ashok Bhat98071552014-02-12 09:54:43 +00001156 _env->SetLongArrayRegion(_IDs, i, 1, &id);
1157 _env->SetIntArrayRegion(_arraySizes, i, 1, &arraySize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001158 }
1159
1160 free(ids);
1161 free(names);
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -07001162 free(arraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001163}
1164
Jason Samsd19f10d2009-05-22 14:03:28 -07001165// -----------------------------------
1166
Tim Murray460a0492013-11-19 12:45:54 -08001167static jlong
1168nTypeCreate(JNIEnv *_env, jobject _this, jlong con, jlong eid,
Jason Samsb109cc72013-01-07 18:20:12 -08001169 jint dimx, jint dimy, jint dimz, jboolean mips, jboolean faces, jint yuv)
Jason Samsd19f10d2009-05-22 14:03:28 -07001170{
Andreas Gampe67333922014-11-10 20:35:59 -08001171 if (kLogApi) {
1172 ALOGD("nTypeCreate, con(%p) eid(%p), x(%i), y(%i), z(%i), mips(%i), faces(%i), yuv(%i)",
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001173 (RsContext)con, (void*)eid, dimx, dimy, dimz, mips, faces, yuv);
Andreas Gampe67333922014-11-10 20:35:59 -08001174 }
Jason Sams3b9c52a2010-10-14 17:48:46 -07001175
Andreas Gampe67333922014-11-10 20:35:59 -08001176 return (jlong)(uintptr_t)rsTypeCreate((RsContext)con, (RsElement)eid, dimx, dimy, dimz, mips,
1177 faces, yuv);
Jason Samsd19f10d2009-05-22 14:03:28 -07001178}
1179
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001180static void
Ashok Bhat98071552014-02-12 09:54:43 +00001181nTypeGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jlongArray _typeData)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001182{
1183 // We are packing 6 items: mDimX; mDimY; mDimZ;
1184 // mDimLOD; mDimFaces; mElement; into typeData
1185 int elementCount = _env->GetArrayLength(_typeData);
1186
1187 assert(elementCount == 6);
Andreas Gampe67333922014-11-10 20:35:59 -08001188 if (kLogApi) {
1189 ALOGD("nTypeGetNativeData, con(%p)", (RsContext)con);
1190 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001191
Ashok Bhat98071552014-02-12 09:54:43 +00001192 uintptr_t typeData[6];
Tim Murrayeff663f2013-11-15 13:08:30 -08001193 rsaTypeGetNativeData((RsContext)con, (RsType)id, typeData, 6);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001194
1195 for(jint i = 0; i < elementCount; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07001196 const jlong data = (jlong)(uintptr_t)typeData[i];
Ashok Bhat98071552014-02-12 09:54:43 +00001197 _env->SetLongArrayRegion(_typeData, i, 1, &data);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001198 }
1199}
1200
Jason Samsd19f10d2009-05-22 14:03:28 -07001201// -----------------------------------
1202
Tim Murray460a0492013-11-19 12:45:54 -08001203static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001204nAllocationCreateTyped(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mips, jint usage,
1205 jlong pointer)
Jason Samsd19f10d2009-05-22 14:03:28 -07001206{
Andreas Gampe67333922014-11-10 20:35:59 -08001207 if (kLogApi) {
1208 ALOGD("nAllocationCreateTyped, con(%p), type(%p), mip(%i), usage(%i), ptr(%p)",
1209 (RsContext)con, (RsElement)type, mips, usage, (void *)pointer);
1210 }
1211 return (jlong)(uintptr_t) rsAllocationCreateTyped((RsContext)con, (RsType)type,
1212 (RsAllocationMipmapControl)mips,
1213 (uint32_t)usage, (uintptr_t)pointer);
Jason Samsd19f10d2009-05-22 14:03:28 -07001214}
1215
Jason Samsd19f10d2009-05-22 14:03:28 -07001216static void
Tim Murray460a0492013-11-19 12:45:54 -08001217nAllocationSyncAll(JNIEnv *_env, jobject _this, jlong con, jlong a, jint bits)
Jason Sams5476b452010-12-08 16:14:36 -08001218{
Andreas Gampe67333922014-11-10 20:35:59 -08001219 if (kLogApi) {
1220 ALOGD("nAllocationSyncAll, con(%p), a(%p), bits(0x%08x)", (RsContext)con, (RsAllocation)a,
1221 bits);
1222 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001223 rsAllocationSyncAll((RsContext)con, (RsAllocation)a, (RsAllocationUsageType)bits);
Jason Sams5476b452010-12-08 16:14:36 -08001224}
1225
Jason Sams72226e02013-02-22 12:45:54 -08001226static jobject
Tim Murray460a0492013-11-19 12:45:54 -08001227nAllocationGetSurface(JNIEnv *_env, jobject _this, jlong con, jlong a)
Jason Sams615e7ce2012-01-13 14:01:20 -08001228{
Andreas Gampe67333922014-11-10 20:35:59 -08001229 if (kLogApi) {
1230 ALOGD("nAllocationGetSurface, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1231 }
Jason Sams615e7ce2012-01-13 14:01:20 -08001232
Andreas Gampe67333922014-11-10 20:35:59 -08001233 IGraphicBufferProducer *v = (IGraphicBufferProducer *)rsAllocationGetSurface((RsContext)con,
1234 (RsAllocation)a);
Jason Sams72226e02013-02-22 12:45:54 -08001235 sp<IGraphicBufferProducer> bp = v;
Chris Wailes488230c32014-08-14 11:22:40 -07001236 v->decStrong(nullptr);
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001237
Jason Sams72226e02013-02-22 12:45:54 -08001238 jobject o = android_view_Surface_createFromIGraphicBufferProducer(_env, bp);
1239 return o;
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001240}
1241
1242static void
Tim Murray460a0492013-11-19 12:45:54 -08001243nAllocationSetSurface(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject sur)
Jason Sams163766c2012-02-15 12:04:24 -08001244{
Andreas Gampe67333922014-11-10 20:35:59 -08001245 if (kLogApi) {
1246 ALOGD("nAllocationSetSurface, con(%p), alloc(%p), surface(%p)", (RsContext)con,
1247 (RsAllocation)alloc, (Surface *)sur);
1248 }
Jason Sams163766c2012-02-15 12:04:24 -08001249
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001250 sp<Surface> s;
Jason Sams163766c2012-02-15 12:04:24 -08001251 if (sur != 0) {
Jeff Brown64a55af2012-08-26 02:47:39 -07001252 s = android_view_Surface_getSurface(_env, sur);
Jason Sams163766c2012-02-15 12:04:24 -08001253 }
1254
Andreas Gampe67333922014-11-10 20:35:59 -08001255 rsAllocationSetSurface((RsContext)con, (RsAllocation)alloc,
1256 static_cast<ANativeWindow *>(s.get()));
Jason Sams163766c2012-02-15 12:04:24 -08001257}
1258
1259static void
Tim Murray460a0492013-11-19 12:45:54 -08001260nAllocationIoSend(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Sams163766c2012-02-15 12:04:24 -08001261{
Andreas Gampe67333922014-11-10 20:35:59 -08001262 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001263 ALOGD("nAllocationIoSend, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
Andreas Gampe67333922014-11-10 20:35:59 -08001264 }
Tim Murray460a0492013-11-19 12:45:54 -08001265 rsAllocationIoSend((RsContext)con, (RsAllocation)alloc);
Jason Sams163766c2012-02-15 12:04:24 -08001266}
1267
1268static void
Tim Murray460a0492013-11-19 12:45:54 -08001269nAllocationIoReceive(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Sams163766c2012-02-15 12:04:24 -08001270{
Andreas Gampe67333922014-11-10 20:35:59 -08001271 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001272 ALOGD("nAllocationIoReceive, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
Andreas Gampe67333922014-11-10 20:35:59 -08001273 }
Tim Murray460a0492013-11-19 12:45:54 -08001274 rsAllocationIoReceive((RsContext)con, (RsAllocation)alloc);
Jason Sams163766c2012-02-15 12:04:24 -08001275}
1276
1277
1278static void
Tim Murray460a0492013-11-19 12:45:54 -08001279nAllocationGenerateMipmaps(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Samsf7086092011-01-12 13:28:37 -08001280{
Andreas Gampe67333922014-11-10 20:35:59 -08001281 if (kLogApi) {
1282 ALOGD("nAllocationGenerateMipmaps, con(%p), a(%p)", (RsContext)con, (RsAllocation)alloc);
1283 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001284 rsAllocationGenerateMipmaps((RsContext)con, (RsAllocation)alloc);
Jason Samsf7086092011-01-12 13:28:37 -08001285}
1286
Tim Murray460a0492013-11-19 12:45:54 -08001287static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001288nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1289 jobject jbitmap, jint usage)
Jason Samsffe9f482009-06-01 17:45:53 -07001290{
John Recked207b92015-04-10 13:52:57 -07001291 SkBitmap bitmap;
1292 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Samsffe9f482009-06-01 17:45:53 -07001293
Jason Sams5476b452010-12-08 16:14:36 -08001294 bitmap.lockPixels();
1295 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001296 jlong id = (jlong)(uintptr_t)rsAllocationCreateFromBitmap((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001297 (RsType)type, (RsAllocationMipmapControl)mip,
1298 ptr, bitmap.getSize(), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001299 bitmap.unlockPixels();
1300 return id;
Jason Samsffe9f482009-06-01 17:45:53 -07001301}
Jason Samsfe08d992009-05-27 14:45:32 -07001302
Tim Murray460a0492013-11-19 12:45:54 -08001303static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001304nAllocationCreateBitmapBackedAllocation(JNIEnv *_env, jobject _this, jlong con, jlong type,
1305 jint mip, jobject jbitmap, jint usage)
Tim Murraya3145512012-12-04 17:59:29 -08001306{
John Recked207b92015-04-10 13:52:57 -07001307 SkBitmap bitmap;
1308 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Tim Murraya3145512012-12-04 17:59:29 -08001309
1310 bitmap.lockPixels();
1311 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001312 jlong id = (jlong)(uintptr_t)rsAllocationCreateTyped((RsContext)con,
Tim Murraya3145512012-12-04 17:59:29 -08001313 (RsType)type, (RsAllocationMipmapControl)mip,
Ashok Bhat98071552014-02-12 09:54:43 +00001314 (uint32_t)usage, (uintptr_t)ptr);
Tim Murraya3145512012-12-04 17:59:29 -08001315 bitmap.unlockPixels();
1316 return id;
1317}
1318
Tim Murray460a0492013-11-19 12:45:54 -08001319static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001320nAllocationCubeCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1321 jobject jbitmap, jint usage)
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001322{
John Recked207b92015-04-10 13:52:57 -07001323 SkBitmap bitmap;
1324 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001325
Jason Sams5476b452010-12-08 16:14:36 -08001326 bitmap.lockPixels();
1327 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001328 jlong id = (jlong)(uintptr_t)rsAllocationCubeCreateFromBitmap((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001329 (RsType)type, (RsAllocationMipmapControl)mip,
1330 ptr, bitmap.getSize(), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001331 bitmap.unlockPixels();
1332 return id;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001333}
1334
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001335static void
Tim Murray460a0492013-11-19 12:45:54 -08001336nAllocationCopyFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001337{
John Recked207b92015-04-10 13:52:57 -07001338 SkBitmap bitmap;
1339 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Samsf7086092011-01-12 13:28:37 -08001340 int w = bitmap.width();
1341 int h = bitmap.height();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001342
Jason Sams4ef66502010-12-10 16:03:15 -08001343 bitmap.lockPixels();
1344 const void* ptr = bitmap.getPixels();
Tim Murrayeff663f2013-11-15 13:08:30 -08001345 rsAllocation2DData((RsContext)con, (RsAllocation)alloc, 0, 0,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001346 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Tim Murray38faea302012-11-27 14:55:08 -08001347 w, h, ptr, bitmap.getSize(), 0);
Jason Sams4ef66502010-12-10 16:03:15 -08001348 bitmap.unlockPixels();
1349}
1350
1351static void
Tim Murray460a0492013-11-19 12:45:54 -08001352nAllocationCopyToBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Jason Sams4ef66502010-12-10 16:03:15 -08001353{
John Recked207b92015-04-10 13:52:57 -07001354 SkBitmap bitmap;
1355 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Sams4ef66502010-12-10 16:03:15 -08001356
1357 bitmap.lockPixels();
1358 void* ptr = bitmap.getPixels();
Tim Murrayeff663f2013-11-15 13:08:30 -08001359 rsAllocationCopyToBitmap((RsContext)con, (RsAllocation)alloc, ptr, bitmap.getSize());
Jason Sams4ef66502010-12-10 16:03:15 -08001360 bitmap.unlockPixels();
Alex Sakhartchouk835b8542011-07-20 14:33:10 -07001361 bitmap.notifyPixelsChanged();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001362}
1363
Stephen Hines414fa2c2014-04-17 01:02:42 -07001364// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Samsd19f10d2009-05-22 14:03:28 -07001365static void
Tim Murray460a0492013-11-19 12:45:54 -08001366nAllocationData1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001367 jint count, jobject data, jint sizeBytes, jint dataType, jint mSize,
1368 jboolean usePadding)
Jason Samsd19f10d2009-05-22 14:03:28 -07001369{
Jason Samse729a942013-11-06 11:22:02 -08001370 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001371 if (kLogApi) {
1372 ALOGD("nAllocation1DData, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1373 "dataType(%i)", (RsContext)con, (RsAllocation)alloc, offset, count, sizeBytes,
1374 dataType);
1375 }
Miao Wang87e908d2015-03-02 15:15:15 -08001376 PER_ARRAY_TYPE(nullptr, rsAllocation1DData, true,
1377 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -07001378}
1379
1380static void
Miao Wangc8e237e2015-02-20 18:36:32 -08001381nAllocationElementData(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1382 jint xoff, jint yoff, jint zoff,
1383 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Jason Sams49bdaf02010-08-31 13:50:42 -07001384{
1385 jint len = _env->GetArrayLength(data);
Andreas Gampe67333922014-11-10 20:35:59 -08001386 if (kLogApi) {
Miao Wangc8e237e2015-02-20 18:36:32 -08001387 ALOGD("nAllocationElementData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1388 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
Andreas Gampe67333922014-11-10 20:35:59 -08001389 sizeBytes);
1390 }
Chris Wailes488230c32014-08-14 11:22:40 -07001391 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001392 if (ptr == nullptr) {
1393 ALOGE("Failed to get Java array elements");
1394 return;
1395 }
Miao Wangc8e237e2015-02-20 18:36:32 -08001396 rsAllocationElementData((RsContext)con, (RsAllocation)alloc,
1397 xoff, yoff, zoff,
1398 lod, ptr, sizeBytes, compIdx);
Jason Sams49bdaf02010-08-31 13:50:42 -07001399 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1400}
1401
Miao Wangc8e237e2015-02-20 18:36:32 -08001402
Stephen Hines414fa2c2014-04-17 01:02:42 -07001403// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Sams49bdaf02010-08-31 13:50:42 -07001404static void
Tim Murray460a0492013-11-19 12:45:54 -08001405nAllocationData2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001406 jint w, jint h, jobject data, jint sizeBytes, jint dataType, jint mSize,
1407 jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001408{
Jason Samse729a942013-11-06 11:22:02 -08001409 RsAllocation *alloc = (RsAllocation *)_alloc;
1410 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001411 if (kLogApi) {
1412 ALOGD("nAllocation2DData, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1413 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1414 }
Miao Wang87e908d2015-03-02 15:15:15 -08001415 int count = w * h;
1416 PER_ARRAY_TYPE(nullptr, rsAllocation2DData, true,
1417 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Samsd19f10d2009-05-22 14:03:28 -07001418}
1419
Stephen Hines414fa2c2014-04-17 01:02:42 -07001420// Copies from the Allocation pointed to by srcAlloc into the Allocation
1421// pointed to by dstAlloc.
Jason Sams40a29e82009-08-10 14:55:26 -07001422static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001423nAllocationData2D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001424 jlong dstAlloc, jint dstXoff, jint dstYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001425 jint dstMip, jint dstFace,
1426 jint width, jint height,
Tim Murray460a0492013-11-19 12:45:54 -08001427 jlong srcAlloc, jint srcXoff, jint srcYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001428 jint srcMip, jint srcFace)
1429{
Andreas Gampe67333922014-11-10 20:35:59 -08001430 if (kLogApi) {
1431 ALOGD("nAllocation2DData_s, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1432 " dstMip(%i), dstFace(%i), width(%i), height(%i),"
1433 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i), srcFace(%i)",
1434 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip, dstFace,
1435 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
1436 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001437
Tim Murrayeff663f2013-11-15 13:08:30 -08001438 rsAllocationCopy2DRange((RsContext)con,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001439 (RsAllocation)dstAlloc,
1440 dstXoff, dstYoff,
1441 dstMip, dstFace,
1442 width, height,
1443 (RsAllocation)srcAlloc,
1444 srcXoff, srcYoff,
1445 srcMip, srcFace);
1446}
1447
Stephen Hines414fa2c2014-04-17 01:02:42 -07001448// Copies from the Java object data into the Allocation pointed to by _alloc.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001449static void
Tim Murray460a0492013-11-19 12:45:54 -08001450nAllocationData3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001451 jint w, jint h, jint d, jobject data, jint sizeBytes, jint dataType,
1452 jint mSize, jboolean usePadding)
Jason Samsb05d6892013-04-09 15:59:24 -07001453{
Jason Samse729a942013-11-06 11:22:02 -08001454 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001455 if (kLogApi) {
1456 ALOGD("nAllocation3DData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1457 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1458 lod, w, h, d, sizeBytes);
1459 }
Miao Wang87e908d2015-03-02 15:15:15 -08001460 int count = w * h * d;
1461 PER_ARRAY_TYPE(nullptr, rsAllocation3DData, true,
1462 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Jason Samsb05d6892013-04-09 15:59:24 -07001463}
1464
Stephen Hines414fa2c2014-04-17 01:02:42 -07001465// Copies from the Allocation pointed to by srcAlloc into the Allocation
1466// pointed to by dstAlloc.
Jason Samsb05d6892013-04-09 15:59:24 -07001467static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001468nAllocationData3D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001469 jlong dstAlloc, jint dstXoff, jint dstYoff, jint dstZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001470 jint dstMip,
1471 jint width, jint height, jint depth,
Tim Murray460a0492013-11-19 12:45:54 -08001472 jlong srcAlloc, jint srcXoff, jint srcYoff, jint srcZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001473 jint srcMip)
1474{
Andreas Gampe67333922014-11-10 20:35:59 -08001475 if (kLogApi) {
1476 ALOGD("nAllocationData3D_alloc, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1477 " dstMip(%i), width(%i), height(%i),"
1478 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i)",
1479 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip,
1480 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip);
1481 }
Jason Samsb05d6892013-04-09 15:59:24 -07001482
Tim Murrayeff663f2013-11-15 13:08:30 -08001483 rsAllocationCopy3DRange((RsContext)con,
Jason Samsb05d6892013-04-09 15:59:24 -07001484 (RsAllocation)dstAlloc,
1485 dstXoff, dstYoff, dstZoff, dstMip,
1486 width, height, depth,
1487 (RsAllocation)srcAlloc,
1488 srcXoff, srcYoff, srcZoff, srcMip);
1489}
1490
Jason Sams21659ac2013-11-06 15:08:07 -08001491
Stephen Hines414fa2c2014-04-17 01:02:42 -07001492// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsb05d6892013-04-09 15:59:24 -07001493static void
Miao Wang87e908d2015-03-02 15:15:15 -08001494nAllocationRead(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jobject data, jint dataType,
1495 jint mSize, jboolean usePadding)
Jason Sams40a29e82009-08-10 14:55:26 -07001496{
Jason Sams21659ac2013-11-06 15:08:07 -08001497 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001498 if (kLogApi) {
1499 ALOGD("nAllocationRead, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
1500 }
Miao Wang87e908d2015-03-02 15:15:15 -08001501 int count = 0;
1502 PER_ARRAY_TYPE(0, rsAllocationRead, false,
1503 (RsContext)con, alloc, ptr, len * typeBytes);
Jason Sams40a29e82009-08-10 14:55:26 -07001504}
1505
Stephen Hines414fa2c2014-04-17 01:02:42 -07001506// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Sams40a29e82009-08-10 14:55:26 -07001507static void
Tim Murray460a0492013-11-19 12:45:54 -08001508nAllocationRead1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001509 jint count, jobject data, jint sizeBytes, jint dataType,
1510 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001511{
Jason Sams21659ac2013-11-06 15:08:07 -08001512 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001513 if (kLogApi) {
1514 ALOGD("nAllocation1DRead, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1515 "dataType(%i)", (RsContext)con, alloc, offset, count, sizeBytes, dataType);
1516 }
Miao Wang87e908d2015-03-02 15:15:15 -08001517 PER_ARRAY_TYPE(0, rsAllocation1DRead, false,
1518 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsfb9f82c2011-01-12 14:53:25 -08001519}
1520
Miao Wangc8e237e2015-02-20 18:36:32 -08001521// Copies from the Element in the Allocation pointed to by _alloc into the Java array data.
1522static void
Miao Wang45cec0a2015-03-04 16:40:21 -08001523nAllocationElementRead(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
Miao Wangc8e237e2015-02-20 18:36:32 -08001524 jint xoff, jint yoff, jint zoff,
Miao Wang45cec0a2015-03-04 16:40:21 -08001525 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Miao Wangc8e237e2015-02-20 18:36:32 -08001526{
Miao Wang45cec0a2015-03-04 16:40:21 -08001527 jint len = _env->GetArrayLength(data);
Miao Wangc8e237e2015-02-20 18:36:32 -08001528 if (kLogApi) {
Miao Wang45cec0a2015-03-04 16:40:21 -08001529 ALOGD("nAllocationElementRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1530 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
1531 sizeBytes);
Miao Wangc8e237e2015-02-20 18:36:32 -08001532 }
Miao Wang45cec0a2015-03-04 16:40:21 -08001533 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001534 if (ptr == nullptr) {
1535 ALOGE("Failed to get Java array elements");
1536 return;
1537 }
Miao Wang45cec0a2015-03-04 16:40:21 -08001538 rsAllocationElementRead((RsContext)con, (RsAllocation)alloc,
1539 xoff, yoff, zoff,
Jason Samsa7e25092015-03-11 11:00:00 -07001540 lod, ptr, sizeBytes, compIdx);
Miao Wangbfa5e652015-05-04 15:29:25 -07001541 _env->ReleaseByteArrayElements(data, ptr, 0);
Miao Wangc8e237e2015-02-20 18:36:32 -08001542}
1543
Stephen Hines414fa2c2014-04-17 01:02:42 -07001544// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsfb9f82c2011-01-12 14:53:25 -08001545static void
Tim Murray460a0492013-11-19 12:45:54 -08001546nAllocationRead2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001547 jint w, jint h, jobject data, jint sizeBytes, jint dataType,
1548 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001549{
Jason Sams21659ac2013-11-06 15:08:07 -08001550 RsAllocation *alloc = (RsAllocation *)_alloc;
1551 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001552 if (kLogApi) {
1553 ALOGD("nAllocation2DRead, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1554 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1555 }
Miao Wang87e908d2015-03-02 15:15:15 -08001556 int count = w * h;
1557 PER_ARRAY_TYPE(0, rsAllocation2DRead, false,
1558 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Sams40a29e82009-08-10 14:55:26 -07001559}
Miao Wang87e908d2015-03-02 15:15:15 -08001560
Miao Wangc8e237e2015-02-20 18:36:32 -08001561// Copies from the Allocation pointed to by _alloc into the Java object data.
1562static void
1563nAllocationRead3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001564 jint w, jint h, jint d, jobject data, int sizeBytes, int dataType,
1565 jint mSize, jboolean usePadding)
Miao Wangc8e237e2015-02-20 18:36:32 -08001566{
1567 RsAllocation *alloc = (RsAllocation *)_alloc;
1568 if (kLogApi) {
1569 ALOGD("nAllocation3DRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1570 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1571 lod, w, h, d, sizeBytes);
1572 }
Miao Wang87e908d2015-03-02 15:15:15 -08001573 int count = w * h * d;
1574 PER_ARRAY_TYPE(nullptr, rsAllocation3DRead, false,
1575 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Miao Wangc8e237e2015-02-20 18:36:32 -08001576}
Jason Samsd19f10d2009-05-22 14:03:28 -07001577
Tim Murray460a0492013-11-19 12:45:54 -08001578static jlong
1579nAllocationGetType(JNIEnv *_env, jobject _this, jlong con, jlong a)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001580{
Andreas Gampe67333922014-11-10 20:35:59 -08001581 if (kLogApi) {
1582 ALOGD("nAllocationGetType, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1583 }
Tim Murray3aa89c12014-08-18 17:51:22 -07001584 return (jlong)(uintptr_t) rsaAllocationGetType((RsContext)con, (RsAllocation)a);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001585}
1586
Jason Sams5edc6082010-10-05 13:32:49 -07001587static void
Tim Murray460a0492013-11-19 12:45:54 -08001588nAllocationResize1D(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint dimX)
Jason Sams5edc6082010-10-05 13:32:49 -07001589{
Andreas Gampe67333922014-11-10 20:35:59 -08001590 if (kLogApi) {
1591 ALOGD("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", (RsContext)con,
1592 (RsAllocation)alloc, dimX);
1593 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001594 rsAllocationResize1D((RsContext)con, (RsAllocation)alloc, dimX);
Jason Sams5edc6082010-10-05 13:32:49 -07001595}
1596
Jason Sams46ba27e32015-02-06 17:45:15 -08001597
1598static jlong
1599nAllocationAdapterCreate(JNIEnv *_env, jobject _this, jlong con, jlong basealloc, jlong type)
1600{
1601 if (kLogApi) {
1602 ALOGD("nAllocationAdapterCreate, con(%p), base(%p), type(%p)",
1603 (RsContext)con, (RsAllocation)basealloc, (RsElement)type);
1604 }
1605 return (jlong)(uintptr_t) rsAllocationAdapterCreate((RsContext)con, (RsType)type,
1606 (RsAllocation)basealloc);
1607
1608}
1609
1610static void
1611nAllocationAdapterOffset(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1612 jint x, jint y, jint z, jint face, jint lod,
1613 jint a1, jint a2, jint a3, jint a4)
1614{
1615 uint32_t params[] = {
1616 (uint32_t)x, (uint32_t)y, (uint32_t)z, (uint32_t)face,
1617 (uint32_t)lod, (uint32_t)a1, (uint32_t)a2, (uint32_t)a3, (uint32_t)a4
1618 };
1619 if (kLogApi) {
1620 ALOGD("nAllocationAdapterOffset, con(%p), alloc(%p), x(%i), y(%i), z(%i), face(%i), lod(%i), arrays(%i %i %i %i)",
1621 (RsContext)con, (RsAllocation)alloc, x, y, z, face, lod, a1, a2, a3, a4);
1622 }
1623 rsAllocationAdapterOffset((RsContext)con, (RsAllocation)alloc,
1624 params, sizeof(params));
1625}
1626
1627
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001628// -----------------------------------
1629
Tim Murray460a0492013-11-19 12:45:54 -08001630static jlong
1631nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con, jlong native_asset)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001632{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001633 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001634 ALOGV("______nFileA3D %p", asset);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001635
Tim Murray3aa89c12014-08-18 17:51:22 -07001636 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromMemory((RsContext)con, asset->getBuffer(false), asset->getLength());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001637 return id;
1638}
1639
Tim Murray460a0492013-11-19 12:45:54 -08001640static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001641nFileA3DCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001642{
1643 AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001644 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001645 return 0;
1646 }
1647
1648 AutoJavaStringToUTF8 str(_env, _path);
1649 Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
Chris Wailes488230c32014-08-14 11:22:40 -07001650 if (asset == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001651 return 0;
1652 }
1653
Tim Murray3aa89c12014-08-18 17:51:22 -07001654 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromAsset((RsContext)con, asset);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001655 return id;
1656}
1657
Tim Murray460a0492013-11-19 12:45:54 -08001658static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001659nFileA3DCreateFromFile(JNIEnv *_env, jobject _this, jlong con, jstring fileName)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001660{
1661 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001662 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromFile((RsContext)con, fileNameUTF.c_str());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001663
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001664 return id;
1665}
1666
Tim Murray460a0492013-11-19 12:45:54 -08001667static jint
1668nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001669{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001670 int32_t numEntries = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001671 rsaFileA3DGetNumIndexEntries((RsContext)con, &numEntries, (RsFile)fileA3D);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001672 return (jint)numEntries;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001673}
1674
1675static void
Tim Murray460a0492013-11-19 12:45:54 -08001676nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001677{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001678 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001679 RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
1680
Tim Murrayeff663f2013-11-15 13:08:30 -08001681 rsaFileA3DGetIndexEntries((RsContext)con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001682
1683 for(jint i = 0; i < numEntries; i ++) {
1684 _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
1685 _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
1686 }
1687
1688 free(fileEntries);
1689}
1690
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001691static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001692nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint index)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001693{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001694 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Tim Murray3aa89c12014-08-18 17:51:22 -07001695 jlong id = (jlong)(uintptr_t)rsaFileA3DGetEntryByIndex((RsContext)con, (uint32_t)index, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001696 return id;
1697}
Jason Samsd19f10d2009-05-22 14:03:28 -07001698
1699// -----------------------------------
1700
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001701static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001702nFontCreateFromFile(JNIEnv *_env, jobject _this, jlong con,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001703 jstring fileName, jfloat fontSize, jint dpi)
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001704{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001705 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001706 jlong id = (jlong)(uintptr_t)rsFontCreateFromFile((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001707 fileNameUTF.c_str(), fileNameUTF.length(),
1708 fontSize, dpi);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001709
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001710 return id;
1711}
1712
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001713static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001714nFontCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con,
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001715 jstring name, jfloat fontSize, jint dpi, jlong native_asset)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001716{
1717 Asset* asset = reinterpret_cast<Asset*>(native_asset);
1718 AutoJavaStringToUTF8 nameUTF(_env, name);
1719
Tim Murray3aa89c12014-08-18 17:51:22 -07001720 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001721 nameUTF.c_str(), nameUTF.length(),
1722 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001723 asset->getBuffer(false), asset->getLength());
1724 return id;
1725}
1726
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001727static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001728nFontCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001729 jfloat fontSize, jint dpi)
1730{
1731 AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001732 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001733 return 0;
1734 }
1735
1736 AutoJavaStringToUTF8 str(_env, _path);
1737 Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
Chris Wailes488230c32014-08-14 11:22:40 -07001738 if (asset == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001739 return 0;
1740 }
1741
Tim Murray3aa89c12014-08-18 17:51:22 -07001742 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001743 str.c_str(), str.length(),
1744 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001745 asset->getBuffer(false), asset->getLength());
1746 delete asset;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001747 return id;
1748}
1749
Jason Samsbd1c3ad2009-08-03 16:03:08 -07001750// -----------------------------------
1751
1752static void
Tim Murray460a0492013-11-19 12:45:54 -08001753nScriptBindAllocation(JNIEnv *_env, jobject _this, jlong con, jlong script, jlong alloc, jint slot)
Jason Samsd19f10d2009-05-22 14:03:28 -07001754{
Andreas Gampe67333922014-11-10 20:35:59 -08001755 if (kLogApi) {
1756 ALOGD("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", (RsContext)con,
1757 (RsScript)script, (RsAllocation)alloc, slot);
1758 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001759 rsScriptBindAllocation((RsContext)con, (RsScript)script, (RsAllocation)alloc, slot);
Jason Samsd19f10d2009-05-22 14:03:28 -07001760}
1761
1762static void
Tim Murray460a0492013-11-19 12:45:54 -08001763nScriptSetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jint val)
Jason Sams4d339932010-05-11 14:03:58 -07001764{
Andreas Gampe67333922014-11-10 20:35:59 -08001765 if (kLogApi) {
1766 ALOGD("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", (RsContext)con, (void *)script,
1767 slot, val);
1768 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001769 rsScriptSetVarI((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001770}
1771
Tim Murray7c4caad2013-04-10 16:21:40 -07001772static jint
Tim Murray460a0492013-11-19 12:45:54 -08001773nScriptGetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001774{
Andreas Gampe67333922014-11-10 20:35:59 -08001775 if (kLogApi) {
1776 ALOGD("nScriptGetVarI, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1777 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001778 int value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001779 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001780 return value;
1781}
1782
Jason Sams4d339932010-05-11 14:03:58 -07001783static void
Tim Murray460a0492013-11-19 12:45:54 -08001784nScriptSetVarObj(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001785{
Andreas Gampe67333922014-11-10 20:35:59 -08001786 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001787 ALOGD("nScriptSetVarObj, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001788 slot, val);
1789 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001790 rsScriptSetVarObj((RsContext)con, (RsScript)script, slot, (RsObjectBase)val);
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001791}
1792
1793static void
Tim Murray460a0492013-11-19 12:45:54 -08001794nScriptSetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Stephen Hines031ec58c2010-10-11 10:54:21 -07001795{
Andreas Gampe67333922014-11-10 20:35:59 -08001796 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001797 ALOGD("nScriptSetVarJ, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001798 slot, val);
1799 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001800 rsScriptSetVarJ((RsContext)con, (RsScript)script, slot, val);
Stephen Hines031ec58c2010-10-11 10:54:21 -07001801}
1802
Tim Murray7c4caad2013-04-10 16:21:40 -07001803static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001804nScriptGetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001805{
Andreas Gampe67333922014-11-10 20:35:59 -08001806 if (kLogApi) {
1807 ALOGD("nScriptGetVarJ, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1808 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001809 jlong value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001810 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001811 return value;
1812}
1813
Stephen Hines031ec58c2010-10-11 10:54:21 -07001814static void
Tim Murray460a0492013-11-19 12:45:54 -08001815nScriptSetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, float val)
Jason Sams4d339932010-05-11 14:03:58 -07001816{
Andreas Gampe67333922014-11-10 20:35:59 -08001817 if (kLogApi) {
1818 ALOGD("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", (RsContext)con, (void *)script,
1819 slot, val);
1820 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001821 rsScriptSetVarF((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001822}
1823
Tim Murray7c4caad2013-04-10 16:21:40 -07001824static jfloat
Tim Murray460a0492013-11-19 12:45:54 -08001825nScriptGetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001826{
Andreas Gampe67333922014-11-10 20:35:59 -08001827 if (kLogApi) {
1828 ALOGD("nScriptGetVarF, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1829 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001830 jfloat value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001831 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001832 return value;
1833}
1834
Jason Sams4d339932010-05-11 14:03:58 -07001835static void
Tim Murray460a0492013-11-19 12:45:54 -08001836nScriptSetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, double val)
Stephen Hinesca54ec32010-09-20 17:20:30 -07001837{
Andreas Gampe67333922014-11-10 20:35:59 -08001838 if (kLogApi) {
1839 ALOGD("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", (RsContext)con, (void *)script,
1840 slot, val);
1841 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001842 rsScriptSetVarD((RsContext)con, (RsScript)script, slot, val);
Stephen Hinesca54ec32010-09-20 17:20:30 -07001843}
1844
Tim Murray7c4caad2013-04-10 16:21:40 -07001845static jdouble
Tim Murray460a0492013-11-19 12:45:54 -08001846nScriptGetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001847{
Andreas Gampe67333922014-11-10 20:35:59 -08001848 if (kLogApi) {
1849 ALOGD("nScriptGetVarD, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1850 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001851 jdouble value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001852 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001853 return value;
1854}
1855
Stephen Hinesca54ec32010-09-20 17:20:30 -07001856static void
Tim Murray460a0492013-11-19 12:45:54 -08001857nScriptSetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001858{
Andreas Gampe67333922014-11-10 20:35:59 -08001859 if (kLogApi) {
1860 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1861 }
Jason Sams4d339932010-05-11 14:03:58 -07001862 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001863 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001864 if (ptr == nullptr) {
1865 ALOGE("Failed to get Java array elements");
1866 return;
1867 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001868 rsScriptSetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001869 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1870}
1871
Stephen Hinesadeb8092012-04-20 14:26:06 -07001872static void
Tim Murray460a0492013-11-19 12:45:54 -08001873nScriptGetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Tim Murray7c4caad2013-04-10 16:21:40 -07001874{
Andreas Gampe67333922014-11-10 20:35:59 -08001875 if (kLogApi) {
1876 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1877 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001878 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001879 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001880 if (ptr == nullptr) {
1881 ALOGE("Failed to get Java array elements");
1882 return;
1883 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001884 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Stephen Hines414fa2c2014-04-17 01:02:42 -07001885 _env->ReleaseByteArrayElements(data, ptr, 0);
Tim Murray7c4caad2013-04-10 16:21:40 -07001886}
1887
1888static void
Andreas Gampe67333922014-11-10 20:35:59 -08001889nScriptSetVarVE(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data,
1890 jlong elem, jintArray dims)
Stephen Hinesadeb8092012-04-20 14:26:06 -07001891{
Andreas Gampe67333922014-11-10 20:35:59 -08001892 if (kLogApi) {
1893 ALOGD("nScriptSetVarVE, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1894 }
Stephen Hinesadeb8092012-04-20 14:26:06 -07001895 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001896 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001897 if (ptr == nullptr) {
1898 ALOGE("Failed to get Java array elements");
1899 return;
1900 }
Stephen Hinesadeb8092012-04-20 14:26:06 -07001901 jint dimsLen = _env->GetArrayLength(dims) * sizeof(int);
Chris Wailes488230c32014-08-14 11:22:40 -07001902 jint *dimsPtr = _env->GetIntArrayElements(dims, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001903 if (dimsPtr == nullptr) {
1904 ALOGE("Failed to get Java array elements");
1905 return;
1906 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001907 rsScriptSetVarVE((RsContext)con, (RsScript)script, slot, ptr, len, (RsElement)elem,
Stephen Hinesbc5d3ee2014-06-25 00:03:39 -07001908 (const uint32_t*) dimsPtr, dimsLen);
Stephen Hinesadeb8092012-04-20 14:26:06 -07001909 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1910 _env->ReleaseIntArrayElements(dims, dimsPtr, JNI_ABORT);
1911}
1912
Jason Samsd19f10d2009-05-22 14:03:28 -07001913
1914static void
Tim Murray460a0492013-11-19 12:45:54 -08001915nScriptSetTimeZone(JNIEnv *_env, jobject _this, jlong con, jlong script, jbyteArray timeZone)
Jason Samsd19f10d2009-05-22 14:03:28 -07001916{
Andreas Gampe67333922014-11-10 20:35:59 -08001917 if (kLogApi) {
1918 ALOGD("nScriptCSetTimeZone, con(%p), s(%p)", (RsContext)con, (void *)script);
1919 }
Romain Guy584a3752009-07-30 18:45:01 -07001920
1921 jint length = _env->GetArrayLength(timeZone);
1922 jbyte* timeZone_ptr;
1923 timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
Miao Wangba8766c2015-10-12 17:24:13 -07001924 if (timeZone_ptr == nullptr) {
1925 ALOGE("Failed to get Java array elements");
1926 return;
1927 }
Romain Guy584a3752009-07-30 18:45:01 -07001928
Tim Murrayeff663f2013-11-15 13:08:30 -08001929 rsScriptSetTimeZone((RsContext)con, (RsScript)script, (const char *)timeZone_ptr, length);
Romain Guy584a3752009-07-30 18:45:01 -07001930
1931 if (timeZone_ptr) {
1932 _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
1933 }
1934}
1935
Jason Samsfbf0b9e2009-08-13 12:59:04 -07001936static void
Tim Murray460a0492013-11-19 12:45:54 -08001937nScriptInvoke(JNIEnv *_env, jobject _this, jlong con, jlong obj, jint slot)
Jason Samsbe2e8412009-09-16 15:04:38 -07001938{
Andreas Gampe67333922014-11-10 20:35:59 -08001939 if (kLogApi) {
1940 ALOGD("nScriptInvoke, con(%p), script(%p)", (RsContext)con, (void *)obj);
1941 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001942 rsScriptInvoke((RsContext)con, (RsScript)obj, slot);
Jason Samsbe2e8412009-09-16 15:04:38 -07001943}
1944
1945static void
Tim Murray460a0492013-11-19 12:45:54 -08001946nScriptInvokeV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001947{
Andreas Gampe67333922014-11-10 20:35:59 -08001948 if (kLogApi) {
1949 ALOGD("nScriptInvokeV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1950 }
Jason Sams4d339932010-05-11 14:03:58 -07001951 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001952 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001953 if (ptr == nullptr) {
1954 ALOGE("Failed to get Java array elements");
1955 return;
1956 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001957 rsScriptInvokeV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001958 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1959}
1960
Jason Sams6e494d32011-04-27 16:33:11 -07001961static void
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001962nScriptForEach(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
1963 jlongArray ains, jlong aout, jbyteArray params,
1964 jintArray limits)
Jason Sams6e494d32011-04-27 16:33:11 -07001965{
Andreas Gampe67333922014-11-10 20:35:59 -08001966 if (kLogApi) {
Chih-Hung Hsieh9eb9dd32015-05-06 14:42:04 -07001967 ALOGD("nScriptForEach, con(%p), s(%p), slot(%i) ains(%p) aout(%" PRId64 ")", (RsContext)con, (void *)script, slot, ains, aout);
Andreas Gampe67333922014-11-10 20:35:59 -08001968 }
Jason Sams6e494d32011-04-27 16:33:11 -07001969
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001970 jint in_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07001971 jlong *in_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07001972
Chris Wailes488230c32014-08-14 11:22:40 -07001973 RsAllocation *in_allocs = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07001974
Chris Wailes488230c32014-08-14 11:22:40 -07001975 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001976 in_len = _env->GetArrayLength(ains);
Yang Ni7b2a46f2015-05-05 12:41:19 -07001977 if (in_len > (jint)RS_KERNEL_MAX_ARGUMENTS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -07001978 ALOGE("Too many arguments in kernel launch.");
1979 // TODO (b/20758983): Report back to Java and throw an exception
1980 return;
1981 }
Chris Wailes94961062014-06-11 12:01:28 -07001982
Yang Ni17c2d7a2015-04-30 16:13:54 -07001983 in_ptr = _env->GetLongArrayElements(ains, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07001984 if (in_ptr == nullptr) {
1985 ALOGE("Failed to get Java array elements");
1986 return;
1987 }
1988
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001989 if (sizeof(RsAllocation) == sizeof(jlong)) {
1990 in_allocs = (RsAllocation*)in_ptr;
Chris Wailes94961062014-06-11 12:01:28 -07001991
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001992 } else {
1993 // Convert from 64-bit jlong types to the native pointer type.
Chris Wailes94961062014-06-11 12:01:28 -07001994
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001995 in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
Yang Ni17c2d7a2015-04-30 16:13:54 -07001996 if (in_allocs == nullptr) {
1997 ALOGE("Failed launching kernel for lack of memory.");
1998 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
1999 return;
2000 }
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002001
2002 for (int index = in_len; --index >= 0;) {
2003 in_allocs[index] = (RsAllocation)in_ptr[index];
2004 }
2005 }
Chris Wailes94961062014-06-11 12:01:28 -07002006 }
2007
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002008 jint param_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002009 jbyte *param_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002010
Chris Wailes488230c32014-08-14 11:22:40 -07002011 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002012 param_len = _env->GetArrayLength(params);
Chris Wailes488230c32014-08-14 11:22:40 -07002013 param_ptr = _env->GetByteArrayElements(params, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002014 if (param_ptr == nullptr) {
2015 ALOGE("Failed to get Java array elements");
2016 return;
2017 }
Chris Wailes94961062014-06-11 12:01:28 -07002018 }
2019
Chris Wailes488230c32014-08-14 11:22:40 -07002020 RsScriptCall sc, *sca = nullptr;
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002021 uint32_t sc_size = 0;
Chris Wailes94961062014-06-11 12:01:28 -07002022
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002023 jint limit_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002024 jint *limit_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07002025
Chris Wailes488230c32014-08-14 11:22:40 -07002026 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002027 limit_len = _env->GetArrayLength(limits);
Chris Wailes488230c32014-08-14 11:22:40 -07002028 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002029 if (limit_ptr == nullptr) {
2030 ALOGE("Failed to get Java array elements");
2031 return;
2032 }
Chris Wailes94961062014-06-11 12:01:28 -07002033
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002034 assert(limit_len == 6);
Andreas Gampe67333922014-11-10 20:35:59 -08002035 UNUSED(limit_len); // As the assert might not be compiled.
Chris Wailes94961062014-06-11 12:01:28 -07002036
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002037 sc.xStart = limit_ptr[0];
2038 sc.xEnd = limit_ptr[1];
2039 sc.yStart = limit_ptr[2];
2040 sc.yEnd = limit_ptr[3];
2041 sc.zStart = limit_ptr[4];
2042 sc.zEnd = limit_ptr[5];
2043 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
Jason Sams14331ab2015-01-26 18:14:36 -08002044 sc.arrayStart = 0;
2045 sc.arrayEnd = 0;
2046 sc.array2Start = 0;
2047 sc.array2End = 0;
2048 sc.array3Start = 0;
2049 sc.array3End = 0;
2050 sc.array4Start = 0;
2051 sc.array4End = 0;
Chris Wailes94961062014-06-11 12:01:28 -07002052
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002053 sca = &sc;
Chris Wailes94961062014-06-11 12:01:28 -07002054 }
2055
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002056 rsScriptForEachMulti((RsContext)con, (RsScript)script, slot,
2057 in_allocs, in_len, (RsAllocation)aout,
2058 param_ptr, param_len, sca, sc_size);
Chris Wailes94961062014-06-11 12:01:28 -07002059
Chris Wailes488230c32014-08-14 11:22:40 -07002060 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002061 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
Chris Wailes94961062014-06-11 12:01:28 -07002062 }
2063
Chris Wailes488230c32014-08-14 11:22:40 -07002064 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002065 _env->ReleaseByteArrayElements(params, param_ptr, JNI_ABORT);
2066 }
2067
Chris Wailes488230c32014-08-14 11:22:40 -07002068 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002069 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
2070 }
Chris Wailes94961062014-06-11 12:01:28 -07002071}
2072
Matt Wala36eb1f72015-07-20 15:35:27 -07002073static void
2074nScriptReduce(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
2075 jlong ain, jlong aout, jintArray limits)
2076{
2077 if (kLogApi) {
2078 ALOGD("nScriptReduce, con(%p), s(%p), slot(%i) ain(%" PRId64 ") aout(%" PRId64 ")", (RsContext)con, (void *)script, slot, ain, aout);
2079 }
2080
2081 RsScriptCall sc, *sca = nullptr;
2082 uint32_t sc_size = 0;
2083
2084 jint limit_len = 0;
2085 jint *limit_ptr = nullptr;
2086
2087 // If the caller passed limits, reflect them in the RsScriptCall.
2088 if (limits != nullptr) {
2089 limit_len = _env->GetArrayLength(limits);
2090 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002091 if (limit_ptr == nullptr) {
2092 ALOGE("Failed to get Java array elements");
2093 return;
2094 }
Matt Wala36eb1f72015-07-20 15:35:27 -07002095
2096 // We expect to be passed an array [x1, x2] which specifies
2097 // the sub-range for a 1-dimensional reduction.
2098 assert(limit_len == 2);
2099 UNUSED(limit_len); // As the assert might not be compiled.
2100
2101 sc.xStart = limit_ptr[0];
2102 sc.xEnd = limit_ptr[1];
2103 sc.yStart = 0;
2104 sc.yEnd = 0;
2105 sc.zStart = 0;
2106 sc.zEnd = 0;
2107 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
2108 sc.arrayStart = 0;
2109 sc.arrayEnd = 0;
2110 sc.array2Start = 0;
2111 sc.array2End = 0;
2112 sc.array3Start = 0;
2113 sc.array3End = 0;
2114 sc.array4Start = 0;
2115 sc.array4End = 0;
2116
2117 sca = &sc;
2118 sc_size = sizeof(sc);
2119 }
2120
2121 rsScriptReduce((RsContext)con, (RsScript)script, slot,
2122 (RsAllocation)ain, (RsAllocation)aout,
2123 sca, sc_size);
2124
2125 if (limits != nullptr) {
2126 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
2127 }
2128}
2129
Jason Sams22534172009-08-04 16:58:20 -07002130// -----------------------------------
2131
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002132static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002133nScriptCCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Samse4a06c52011-03-16 16:29:28 -07002134 jstring resName, jstring cacheDir,
2135 jbyteArray scriptRef, jint length)
Jason Sams22534172009-08-04 16:58:20 -07002136{
Andreas Gampe67333922014-11-10 20:35:59 -08002137 if (kLogApi) {
2138 ALOGD("nScriptCCreate, con(%p)", (RsContext)con);
2139 }
Jason Sams22534172009-08-04 16:58:20 -07002140
Jason Samse4a06c52011-03-16 16:29:28 -07002141 AutoJavaStringToUTF8 resNameUTF(_env, resName);
2142 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002143 jlong ret = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07002144 jbyte* script_ptr = nullptr;
Jack Palevich43702d82009-05-28 13:38:16 -07002145 jint _exception = 0;
2146 jint remaining;
Jack Palevich43702d82009-05-28 13:38:16 -07002147 if (!scriptRef) {
2148 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002149 //jniThrowException(_env, "java/lang/IllegalArgumentException", "script == null");
Jack Palevich43702d82009-05-28 13:38:16 -07002150 goto exit;
2151 }
Jack Palevich43702d82009-05-28 13:38:16 -07002152 if (length < 0) {
2153 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002154 //jniThrowException(_env, "java/lang/IllegalArgumentException", "length < 0");
Jack Palevich43702d82009-05-28 13:38:16 -07002155 goto exit;
2156 }
Jason Samse4a06c52011-03-16 16:29:28 -07002157 remaining = _env->GetArrayLength(scriptRef);
Jack Palevich43702d82009-05-28 13:38:16 -07002158 if (remaining < length) {
2159 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07002160 //jniThrowException(_env, "java/lang/IllegalArgumentException",
2161 // "length > script.length - offset");
Jack Palevich43702d82009-05-28 13:38:16 -07002162 goto exit;
2163 }
Jason Samse4a06c52011-03-16 16:29:28 -07002164 script_ptr = (jbyte *)
Jack Palevich43702d82009-05-28 13:38:16 -07002165 _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
Miao Wangba8766c2015-10-12 17:24:13 -07002166 if (script_ptr == nullptr) {
2167 ALOGE("Failed to get Java array elements");
2168 return ret;
2169 }
Jack Palevich43702d82009-05-28 13:38:16 -07002170
Tim Murrayeff663f2013-11-15 13:08:30 -08002171 //rsScriptCSetText((RsContext)con, (const char *)script_ptr, length);
Jason Samse4a06c52011-03-16 16:29:28 -07002172
Tim Murray3aa89c12014-08-18 17:51:22 -07002173 ret = (jlong)(uintptr_t)rsScriptCCreate((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07002174 resNameUTF.c_str(), resNameUTF.length(),
2175 cacheDirUTF.c_str(), cacheDirUTF.length(),
Jason Samse4a06c52011-03-16 16:29:28 -07002176 (const char *)script_ptr, length);
Jason Sams39ddc9502009-06-05 17:35:09 -07002177
Jack Palevich43702d82009-05-28 13:38:16 -07002178exit:
Jason Samse4a06c52011-03-16 16:29:28 -07002179 if (script_ptr) {
2180 _env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr,
Jack Palevich43702d82009-05-28 13:38:16 -07002181 _exception ? JNI_ABORT: 0);
2182 }
Jason Samsd19f10d2009-05-22 14:03:28 -07002183
Tim Murray3aa89c12014-08-18 17:51:22 -07002184 return (jlong)(uintptr_t)ret;
Jason Samsd19f10d2009-05-22 14:03:28 -07002185}
2186
Tim Murray460a0492013-11-19 12:45:54 -08002187static jlong
2188nScriptIntrinsicCreate(JNIEnv *_env, jobject _this, jlong con, jint id, jlong eid)
Jason Sams6ab97682012-08-10 12:09:43 -07002189{
Andreas Gampe67333922014-11-10 20:35:59 -08002190 if (kLogApi) {
2191 ALOGD("nScriptIntrinsicCreate, con(%p) id(%i) element(%p)", (RsContext)con, id,
2192 (void *)eid);
2193 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002194 return (jlong)(uintptr_t)rsScriptIntrinsicCreate((RsContext)con, id, (RsElement)eid);
Jason Sams6ab97682012-08-10 12:09:43 -07002195}
2196
Tim Murray460a0492013-11-19 12:45:54 -08002197static jlong
2198nScriptKernelIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot, jint sig)
Jason Sams08a81582012-09-18 12:32:10 -07002199{
Andreas Gampe67333922014-11-10 20:35:59 -08002200 if (kLogApi) {
2201 ALOGD("nScriptKernelIDCreate, con(%p) script(%p), slot(%i), sig(%i)", (RsContext)con,
2202 (void *)sid, slot, sig);
2203 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002204 return (jlong)(uintptr_t)rsScriptKernelIDCreate((RsContext)con, (RsScript)sid, slot, sig);
Jason Sams08a81582012-09-18 12:32:10 -07002205}
2206
Tim Murray460a0492013-11-19 12:45:54 -08002207static jlong
Yang Nibe392ad2015-01-23 17:16:02 -08002208nScriptInvokeIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
2209{
2210 if (kLogApi) {
Elliott Hughes7ff53fa2015-02-05 21:36:10 -08002211 ALOGD("nScriptInvokeIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con,
Yang Nibe392ad2015-01-23 17:16:02 -08002212 (void *)sid, slot);
2213 }
2214 return (jlong)(uintptr_t)rsScriptInvokeIDCreate((RsContext)con, (RsScript)sid, slot);
2215}
2216
2217static jlong
Tim Murray460a0492013-11-19 12:45:54 -08002218nScriptFieldIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
Jason Sams08a81582012-09-18 12:32:10 -07002219{
Andreas Gampe67333922014-11-10 20:35:59 -08002220 if (kLogApi) {
2221 ALOGD("nScriptFieldIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con, (void *)sid,
2222 slot);
2223 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002224 return (jlong)(uintptr_t)rsScriptFieldIDCreate((RsContext)con, (RsScript)sid, slot);
Jason Sams08a81582012-09-18 12:32:10 -07002225}
2226
Tim Murray460a0492013-11-19 12:45:54 -08002227static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002228nScriptGroupCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _kernels, jlongArray _src,
2229 jlongArray _dstk, jlongArray _dstf, jlongArray _types)
Jason Sams08a81582012-09-18 12:32:10 -07002230{
Andreas Gampe67333922014-11-10 20:35:59 -08002231 if (kLogApi) {
2232 ALOGD("nScriptGroupCreate, con(%p)", (RsContext)con);
2233 }
Jason Sams08a81582012-09-18 12:32:10 -07002234
Ashok Bhat98071552014-02-12 09:54:43 +00002235 jint kernelsLen = _env->GetArrayLength(_kernels);
Chris Wailes488230c32014-08-14 11:22:40 -07002236 jlong *jKernelsPtr = _env->GetLongArrayElements(_kernels, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002237 if (jKernelsPtr == nullptr) {
2238 ALOGE("Failed to get Java array elements: kernels");
2239 return 0;
2240 }
Ashok Bhat98071552014-02-12 09:54:43 +00002241 RsScriptKernelID* kernelsPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * kernelsLen);
2242 for(int i = 0; i < kernelsLen; ++i) {
2243 kernelsPtr[i] = (RsScriptKernelID)jKernelsPtr[i];
2244 }
Jason Sams08a81582012-09-18 12:32:10 -07002245
Ashok Bhat98071552014-02-12 09:54:43 +00002246 jint srcLen = _env->GetArrayLength(_src);
Chris Wailes488230c32014-08-14 11:22:40 -07002247 jlong *jSrcPtr = _env->GetLongArrayElements(_src, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002248 if (jSrcPtr == nullptr) {
2249 ALOGE("Failed to get Java array elements: src");
2250 return 0;
2251 }
Ashok Bhat98071552014-02-12 09:54:43 +00002252 RsScriptKernelID* srcPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * srcLen);
2253 for(int i = 0; i < srcLen; ++i) {
2254 srcPtr[i] = (RsScriptKernelID)jSrcPtr[i];
2255 }
Jason Sams08a81582012-09-18 12:32:10 -07002256
Ashok Bhat98071552014-02-12 09:54:43 +00002257 jint dstkLen = _env->GetArrayLength(_dstk);
Chris Wailes488230c32014-08-14 11:22:40 -07002258 jlong *jDstkPtr = _env->GetLongArrayElements(_dstk, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002259 if (jDstkPtr == nullptr) {
2260 ALOGE("Failed to get Java array elements: dstk");
2261 return 0;
2262 }
Ashok Bhat98071552014-02-12 09:54:43 +00002263 RsScriptKernelID* dstkPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstkLen);
2264 for(int i = 0; i < dstkLen; ++i) {
2265 dstkPtr[i] = (RsScriptKernelID)jDstkPtr[i];
2266 }
2267
2268 jint dstfLen = _env->GetArrayLength(_dstf);
Chris Wailes488230c32014-08-14 11:22:40 -07002269 jlong *jDstfPtr = _env->GetLongArrayElements(_dstf, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002270 if (jDstfPtr == nullptr) {
2271 ALOGE("Failed to get Java array elements: dstf");
2272 return 0;
2273 }
Ashok Bhat98071552014-02-12 09:54:43 +00002274 RsScriptKernelID* dstfPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstfLen);
2275 for(int i = 0; i < dstfLen; ++i) {
2276 dstfPtr[i] = (RsScriptKernelID)jDstfPtr[i];
2277 }
2278
2279 jint typesLen = _env->GetArrayLength(_types);
Chris Wailes488230c32014-08-14 11:22:40 -07002280 jlong *jTypesPtr = _env->GetLongArrayElements(_types, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002281 if (jTypesPtr == nullptr) {
2282 ALOGE("Failed to get Java array elements: types");
2283 return 0;
2284 }
Ashok Bhat98071552014-02-12 09:54:43 +00002285 RsType* typesPtr = (RsType*) malloc(sizeof(RsType) * typesLen);
2286 for(int i = 0; i < typesLen; ++i) {
2287 typesPtr[i] = (RsType)jTypesPtr[i];
2288 }
2289
Tim Murray3aa89c12014-08-18 17:51:22 -07002290 jlong id = (jlong)(uintptr_t)rsScriptGroupCreate((RsContext)con,
Ashok Bhat98071552014-02-12 09:54:43 +00002291 (RsScriptKernelID *)kernelsPtr, kernelsLen * sizeof(RsScriptKernelID),
2292 (RsScriptKernelID *)srcPtr, srcLen * sizeof(RsScriptKernelID),
2293 (RsScriptKernelID *)dstkPtr, dstkLen * sizeof(RsScriptKernelID),
2294 (RsScriptFieldID *)dstfPtr, dstfLen * sizeof(RsScriptKernelID),
2295 (RsType *)typesPtr, typesLen * sizeof(RsType));
2296
2297 free(kernelsPtr);
2298 free(srcPtr);
2299 free(dstkPtr);
2300 free(dstfPtr);
2301 free(typesPtr);
2302 _env->ReleaseLongArrayElements(_kernels, jKernelsPtr, 0);
2303 _env->ReleaseLongArrayElements(_src, jSrcPtr, 0);
2304 _env->ReleaseLongArrayElements(_dstk, jDstkPtr, 0);
2305 _env->ReleaseLongArrayElements(_dstf, jDstfPtr, 0);
2306 _env->ReleaseLongArrayElements(_types, jTypesPtr, 0);
Jason Sams08a81582012-09-18 12:32:10 -07002307 return id;
2308}
2309
2310static void
Tim Murray460a0492013-11-19 12:45:54 -08002311nScriptGroupSetInput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002312{
Andreas Gampe67333922014-11-10 20:35:59 -08002313 if (kLogApi) {
2314 ALOGD("nScriptGroupSetInput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2315 (void *)gid, (void *)kid, (void *)alloc);
2316 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002317 rsScriptGroupSetInput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002318}
2319
2320static void
Tim Murray460a0492013-11-19 12:45:54 -08002321nScriptGroupSetOutput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002322{
Andreas Gampe67333922014-11-10 20:35:59 -08002323 if (kLogApi) {
2324 ALOGD("nScriptGroupSetOutput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2325 (void *)gid, (void *)kid, (void *)alloc);
2326 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002327 rsScriptGroupSetOutput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002328}
2329
2330static void
Tim Murray460a0492013-11-19 12:45:54 -08002331nScriptGroupExecute(JNIEnv *_env, jobject _this, jlong con, jlong gid)
Jason Sams08a81582012-09-18 12:32:10 -07002332{
Andreas Gampe67333922014-11-10 20:35:59 -08002333 if (kLogApi) {
2334 ALOGD("nScriptGroupSetOutput, con(%p) group(%p)", (RsContext)con, (void *)gid);
2335 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002336 rsScriptGroupExecute((RsContext)con, (RsScriptGroup)gid);
Jason Sams08a81582012-09-18 12:32:10 -07002337}
2338
Jason Samsd19f10d2009-05-22 14:03:28 -07002339// ---------------------------------------------------------------------------
2340
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002341static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002342nProgramStoreCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Sams331bf9b2011-04-06 11:23:54 -07002343 jboolean colorMaskR, jboolean colorMaskG, jboolean colorMaskB, jboolean colorMaskA,
2344 jboolean depthMask, jboolean ditherEnable,
2345 jint srcFunc, jint destFunc,
2346 jint depthFunc)
Jason Samsd19f10d2009-05-22 14:03:28 -07002347{
Andreas Gampe67333922014-11-10 20:35:59 -08002348 if (kLogApi) {
2349 ALOGD("nProgramStoreCreate, con(%p)", (RsContext)con);
2350 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002351 return (jlong)(uintptr_t)rsProgramStoreCreate((RsContext)con, colorMaskR, colorMaskG, colorMaskB, colorMaskA,
Jason Sams331bf9b2011-04-06 11:23:54 -07002352 depthMask, ditherEnable, (RsBlendSrcFunc)srcFunc,
2353 (RsBlendDstFunc)destFunc, (RsDepthFunc)depthFunc);
Jason Samsd19f10d2009-05-22 14:03:28 -07002354}
2355
Jason Sams0011bcf2009-12-15 12:58:36 -08002356// ---------------------------------------------------------------------------
2357
2358static void
Tim Murray460a0492013-11-19 12:45:54 -08002359nProgramBindConstants(JNIEnv *_env, jobject _this, jlong con, jlong vpv, jint slot, jlong a)
Jason Sams0011bcf2009-12-15 12:58:36 -08002360{
Andreas Gampe67333922014-11-10 20:35:59 -08002361 if (kLogApi) {
2362 ALOGD("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", (RsContext)con,
2363 (RsProgramVertex)vpv, slot, (RsAllocation)a);
2364 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002365 rsProgramBindConstants((RsContext)con, (RsProgram)vpv, slot, (RsAllocation)a);
Jason Sams0011bcf2009-12-15 12:58:36 -08002366}
Jason Sams54c0ec12009-11-30 14:49:55 -08002367
Jason Sams68afd012009-12-17 16:55:08 -08002368static void
Tim Murray460a0492013-11-19 12:45:54 -08002369nProgramBindTexture(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002370{
Andreas Gampe67333922014-11-10 20:35:59 -08002371 if (kLogApi) {
2372 ALOGD("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2373 (RsProgramFragment)vpf, slot, (RsAllocation)a);
2374 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002375 rsProgramBindTexture((RsContext)con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
Jason Sams68afd012009-12-17 16:55:08 -08002376}
2377
2378static void
Tim Murray460a0492013-11-19 12:45:54 -08002379nProgramBindSampler(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002380{
Andreas Gampe67333922014-11-10 20:35:59 -08002381 if (kLogApi) {
2382 ALOGD("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2383 (RsProgramFragment)vpf, slot, (RsSampler)a);
2384 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002385 rsProgramBindSampler((RsContext)con, (RsProgramFragment)vpf, slot, (RsSampler)a);
Jason Sams68afd012009-12-17 16:55:08 -08002386}
2387
Jason Samsd19f10d2009-05-22 14:03:28 -07002388// ---------------------------------------------------------------------------
2389
Tim Murray460a0492013-11-19 12:45:54 -08002390static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002391nProgramFragmentCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002392 jobjectArray texNames, jlongArray params)
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002393{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002394 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002395 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002396 jint paramLen = _env->GetArrayLength(params);
Miao Wangba8766c2015-10-12 17:24:13 -07002397 if (jParamPtr == nullptr) {
2398 ALOGE("Failed to get Java array elements");
2399 return 0;
2400 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002401
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002402 int texCount = _env->GetArrayLength(texNames);
2403 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2404 const char ** nameArray = names.c_str();
2405 size_t* sizeArray = names.c_str_len();
2406
Andreas Gampe67333922014-11-10 20:35:59 -08002407 if (kLogApi) {
2408 ALOGD("nProgramFragmentCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2409 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002410
Ashok Bhat98071552014-02-12 09:54:43 +00002411 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2412 for(int i = 0; i < paramLen; ++i) {
2413 paramPtr[i] = (uintptr_t)jParamPtr[i];
2414 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002415 jlong ret = (jlong)(uintptr_t)rsProgramFragmentCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002416 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002417 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002418
Ashok Bhat98071552014-02-12 09:54:43 +00002419 free(paramPtr);
2420 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002421 return ret;
2422}
2423
2424
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002425// ---------------------------------------------------------------------------
2426
Tim Murray460a0492013-11-19 12:45:54 -08002427static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002428nProgramVertexCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002429 jobjectArray texNames, jlongArray params)
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002430{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002431 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002432 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams0011bcf2009-12-15 12:58:36 -08002433 jint paramLen = _env->GetArrayLength(params);
Miao Wangba8766c2015-10-12 17:24:13 -07002434 if (jParamPtr == nullptr) {
2435 ALOGE("Failed to get Java array elements");
2436 return 0;
2437 }
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002438
Andreas Gampe67333922014-11-10 20:35:59 -08002439 if (kLogApi) {
2440 ALOGD("nProgramVertexCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2441 }
Jason Sams0011bcf2009-12-15 12:58:36 -08002442
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002443 int texCount = _env->GetArrayLength(texNames);
2444 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2445 const char ** nameArray = names.c_str();
2446 size_t* sizeArray = names.c_str_len();
2447
Ashok Bhat98071552014-02-12 09:54:43 +00002448 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2449 for(int i = 0; i < paramLen; ++i) {
2450 paramPtr[i] = (uintptr_t)jParamPtr[i];
2451 }
2452
Tim Murray3aa89c12014-08-18 17:51:22 -07002453 jlong ret = (jlong)(uintptr_t)rsProgramVertexCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002454 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002455 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002456
Ashok Bhat98071552014-02-12 09:54:43 +00002457 free(paramPtr);
2458 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams0011bcf2009-12-15 12:58:36 -08002459 return ret;
2460}
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002461
Jason Samsebfb4362009-09-23 13:57:02 -07002462// ---------------------------------------------------------------------------
2463
Tim Murray460a0492013-11-19 12:45:54 -08002464static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002465nProgramRasterCreate(JNIEnv *_env, jobject _this, jlong con, jboolean pointSprite, jint cull)
Jason Samsebfb4362009-09-23 13:57:02 -07002466{
Andreas Gampe67333922014-11-10 20:35:59 -08002467 if (kLogApi) {
2468 ALOGD("nProgramRasterCreate, con(%p), pointSprite(%i), cull(%i)", (RsContext)con,
2469 pointSprite, cull);
2470 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002471 return (jlong)(uintptr_t)rsProgramRasterCreate((RsContext)con, pointSprite, (RsCullMode)cull);
Jason Samsebfb4362009-09-23 13:57:02 -07002472}
2473
Jason Samsd19f10d2009-05-22 14:03:28 -07002474
2475// ---------------------------------------------------------------------------
2476
2477static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002478nContextBindRootScript(JNIEnv *_env, jobject _this, jlong con, jlong script)
Jason Samsd19f10d2009-05-22 14:03:28 -07002479{
Andreas Gampe67333922014-11-10 20:35:59 -08002480 if (kLogApi) {
2481 ALOGD("nContextBindRootScript, con(%p), script(%p)", (RsContext)con, (RsScript)script);
2482 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002483 rsContextBindRootScript((RsContext)con, (RsScript)script);
Jason Samsd19f10d2009-05-22 14:03:28 -07002484}
2485
2486static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002487nContextBindProgramStore(JNIEnv *_env, jobject _this, jlong con, jlong pfs)
Jason Samsd19f10d2009-05-22 14:03:28 -07002488{
Andreas Gampe67333922014-11-10 20:35:59 -08002489 if (kLogApi) {
2490 ALOGD("nContextBindProgramStore, con(%p), pfs(%p)", (RsContext)con, (RsProgramStore)pfs);
2491 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002492 rsContextBindProgramStore((RsContext)con, (RsProgramStore)pfs);
Jason Samsd19f10d2009-05-22 14:03:28 -07002493}
2494
2495static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002496nContextBindProgramFragment(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsd19f10d2009-05-22 14:03:28 -07002497{
Andreas Gampe67333922014-11-10 20:35:59 -08002498 if (kLogApi) {
2499 ALOGD("nContextBindProgramFragment, con(%p), pf(%p)", (RsContext)con,
2500 (RsProgramFragment)pf);
2501 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002502 rsContextBindProgramFragment((RsContext)con, (RsProgramFragment)pf);
Jason Samsd19f10d2009-05-22 14:03:28 -07002503}
2504
Jason Sams0826a6f2009-06-15 19:04:56 -07002505static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002506nContextBindProgramVertex(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Sams0826a6f2009-06-15 19:04:56 -07002507{
Andreas Gampe67333922014-11-10 20:35:59 -08002508 if (kLogApi) {
2509 ALOGD("nContextBindProgramVertex, con(%p), pf(%p)", (RsContext)con, (RsProgramVertex)pf);
2510 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002511 rsContextBindProgramVertex((RsContext)con, (RsProgramVertex)pf);
Jason Sams0826a6f2009-06-15 19:04:56 -07002512}
2513
Joe Onoratod7b37742009-08-09 22:57:44 -07002514static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002515nContextBindProgramRaster(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsebfb4362009-09-23 13:57:02 -07002516{
Andreas Gampe67333922014-11-10 20:35:59 -08002517 if (kLogApi) {
2518 ALOGD("nContextBindProgramRaster, con(%p), pf(%p)", (RsContext)con, (RsProgramRaster)pf);
2519 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002520 rsContextBindProgramRaster((RsContext)con, (RsProgramRaster)pf);
Jason Samsebfb4362009-09-23 13:57:02 -07002521}
2522
Joe Onoratod7b37742009-08-09 22:57:44 -07002523
Jason Sams02fb2cb2009-05-28 15:37:57 -07002524// ---------------------------------------------------------------------------
2525
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002526static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002527nSamplerCreate(JNIEnv *_env, jobject _this, jlong con, jint magFilter, jint minFilter,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002528 jint wrapS, jint wrapT, jint wrapR, jfloat aniso)
Jason Sams02fb2cb2009-05-28 15:37:57 -07002529{
Andreas Gampe67333922014-11-10 20:35:59 -08002530 if (kLogApi) {
2531 ALOGD("nSamplerCreate, con(%p)", (RsContext)con);
2532 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002533 return (jlong)(uintptr_t)rsSamplerCreate((RsContext)con,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002534 (RsSamplerValue)magFilter,
2535 (RsSamplerValue)minFilter,
2536 (RsSamplerValue)wrapS,
2537 (RsSamplerValue)wrapT,
2538 (RsSamplerValue)wrapR,
2539 aniso);
Jason Sams02fb2cb2009-05-28 15:37:57 -07002540}
2541
Jason Samsbba134c2009-06-22 15:49:21 -07002542// ---------------------------------------------------------------------------
2543
Tim Murray460a0492013-11-19 12:45:54 -08002544static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002545nMeshCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _vtx, jlongArray _idx, jintArray _prim)
Jason Samsbba134c2009-06-22 15:49:21 -07002546{
Andreas Gampe67333922014-11-10 20:35:59 -08002547 if (kLogApi) {
2548 ALOGD("nMeshCreate, con(%p)", (RsContext)con);
2549 }
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002550
2551 jint vtxLen = _env->GetArrayLength(_vtx);
Chris Wailes488230c32014-08-14 11:22:40 -07002552 jlong *jVtxPtr = _env->GetLongArrayElements(_vtx, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002553 if (jVtxPtr == nullptr) {
2554 ALOGE("Failed to get Java array elements: vtx");
2555 return 0;
2556 }
Ashok Bhat98071552014-02-12 09:54:43 +00002557 RsAllocation* vtxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * vtxLen);
2558 for(int i = 0; i < vtxLen; ++i) {
2559 vtxPtr[i] = (RsAllocation)(uintptr_t)jVtxPtr[i];
2560 }
2561
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002562 jint idxLen = _env->GetArrayLength(_idx);
Chris Wailes488230c32014-08-14 11:22:40 -07002563 jlong *jIdxPtr = _env->GetLongArrayElements(_idx, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002564 if (jIdxPtr == nullptr) {
2565 ALOGE("Failed to get Java array elements: idx");
2566 return 0;
2567 }
Ashok Bhat98071552014-02-12 09:54:43 +00002568 RsAllocation* idxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * idxLen);
2569 for(int i = 0; i < idxLen; ++i) {
2570 idxPtr[i] = (RsAllocation)(uintptr_t)jIdxPtr[i];
2571 }
2572
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002573 jint primLen = _env->GetArrayLength(_prim);
Chris Wailes488230c32014-08-14 11:22:40 -07002574 jint *primPtr = _env->GetIntArrayElements(_prim, nullptr);
Miao Wangba8766c2015-10-12 17:24:13 -07002575 if (primPtr == nullptr) {
2576 ALOGE("Failed to get Java array elements: prim");
2577 return 0;
2578 }
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002579
Tim Murray3aa89c12014-08-18 17:51:22 -07002580 jlong id = (jlong)(uintptr_t)rsMeshCreate((RsContext)con,
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002581 (RsAllocation *)vtxPtr, vtxLen,
2582 (RsAllocation *)idxPtr, idxLen,
2583 (uint32_t *)primPtr, primLen);
2584
Ashok Bhat98071552014-02-12 09:54:43 +00002585 free(vtxPtr);
2586 free(idxPtr);
2587 _env->ReleaseLongArrayElements(_vtx, jVtxPtr, 0);
2588 _env->ReleaseLongArrayElements(_idx, jIdxPtr, 0);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002589 _env->ReleaseIntArrayElements(_prim, primPtr, 0);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002590 return id;
2591}
2592
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002593static jint
Tim Murray460a0492013-11-19 12:45:54 -08002594nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002595{
Andreas Gampe67333922014-11-10 20:35:59 -08002596 if (kLogApi) {
2597 ALOGD("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2598 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002599 jint vtxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002600 rsaMeshGetVertexBufferCount((RsContext)con, (RsMesh)mesh, &vtxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002601 return vtxCount;
2602}
2603
2604static jint
Tim Murray460a0492013-11-19 12:45:54 -08002605nMeshGetIndexCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002606{
Andreas Gampe67333922014-11-10 20:35:59 -08002607 if (kLogApi) {
2608 ALOGD("nMeshGetIndexCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2609 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002610 jint idxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002611 rsaMeshGetIndexCount((RsContext)con, (RsMesh)mesh, &idxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002612 return idxCount;
2613}
2614
2615static void
Ashok Bhat98071552014-02-12 09:54:43 +00002616nMeshGetVertices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _ids, jint numVtxIDs)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002617{
Andreas Gampe67333922014-11-10 20:35:59 -08002618 if (kLogApi) {
2619 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2620 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002621
2622 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
Tim Murrayeff663f2013-11-15 13:08:30 -08002623 rsaMeshGetVertices((RsContext)con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002624
2625 for(jint i = 0; i < numVtxIDs; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002626 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002627 _env->SetLongArrayRegion(_ids, i, 1, &alloc);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002628 }
2629
2630 free(allocs);
2631}
2632
2633static void
Ashok Bhat98071552014-02-12 09:54:43 +00002634nMeshGetIndices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _idxIds, jintArray _primitives, jint numIndices)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002635{
Andreas Gampe67333922014-11-10 20:35:59 -08002636 if (kLogApi) {
2637 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2638 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002639
2640 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
2641 uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
2642
Tim Murrayeff663f2013-11-15 13:08:30 -08002643 rsaMeshGetIndices((RsContext)con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002644
2645 for(jint i = 0; i < numIndices; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002646 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002647 const jint prim = (jint)prims[i];
2648 _env->SetLongArrayRegion(_idxIds, i, 1, &alloc);
2649 _env->SetIntArrayRegion(_primitives, i, 1, &prim);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002650 }
2651
2652 free(allocs);
2653 free(prims);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002654}
2655
Tim Murray56f9e6f2014-05-16 11:47:26 -07002656static jint
2657nSystemGetPointerSize(JNIEnv *_env, jobject _this) {
2658 return (jint)sizeof(void*);
2659}
2660
2661
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002662// ---------------------------------------------------------------------------
2663
Jason Samsd19f10d2009-05-22 14:03:28 -07002664
Jason Sams94d8e90a2009-06-10 16:09:05 -07002665static const char *classPathName = "android/renderscript/RenderScript";
Jason Samsd19f10d2009-05-22 14:03:28 -07002666
Daniel Micay76f6a862015-09-19 17:31:01 -04002667static const JNINativeMethod methods[] = {
Jason Sams1c415172010-11-08 17:06:46 -08002668{"_nInit", "()V", (void*)_nInit },
Jason Samsea84a7c2009-09-04 14:42:41 -07002669
Tim Murrayeff663f2013-11-15 13:08:30 -08002670{"nDeviceCreate", "()J", (void*)nDeviceCreate },
2671{"nDeviceDestroy", "(J)V", (void*)nDeviceDestroy },
2672{"nDeviceSetConfig", "(JII)V", (void*)nDeviceSetConfig },
2673{"nContextGetUserMessage", "(J[I)I", (void*)nContextGetUserMessage },
2674{"nContextGetErrorMessage", "(J)Ljava/lang/String;", (void*)nContextGetErrorMessage },
2675{"nContextPeekMessage", "(J[I)I", (void*)nContextPeekMessage },
Jason Sams1c415172010-11-08 17:06:46 -08002676
Tim Murrayeff663f2013-11-15 13:08:30 -08002677{"nContextInitToClient", "(J)V", (void*)nContextInitToClient },
2678{"nContextDeinitToClient", "(J)V", (void*)nContextDeinitToClient },
Jason Samsd19f10d2009-05-22 14:03:28 -07002679
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002680
Jason Sams2e1872f2010-08-17 16:25:41 -07002681// All methods below are thread protected in java.
Tim Murrayeff663f2013-11-15 13:08:30 -08002682{"rsnContextCreate", "(JIII)J", (void*)nContextCreate },
2683{"rsnContextCreateGL", "(JIIIIIIIIIIIIFI)J", (void*)nContextCreateGL },
2684{"rsnContextFinish", "(J)V", (void*)nContextFinish },
2685{"rsnContextSetPriority", "(JI)V", (void*)nContextSetPriority },
Tim Murray47f31582015-04-07 15:43:24 -07002686{"rsnContextSetCacheDir", "(JLjava/lang/String;)V", (void*)nContextSetCacheDir },
Tim Murrayeff663f2013-11-15 13:08:30 -08002687{"rsnContextSetSurface", "(JIILandroid/view/Surface;)V", (void*)nContextSetSurface },
2688{"rsnContextDestroy", "(J)V", (void*)nContextDestroy },
2689{"rsnContextDump", "(JI)V", (void*)nContextDump },
2690{"rsnContextPause", "(J)V", (void*)nContextPause },
2691{"rsnContextResume", "(J)V", (void*)nContextResume },
2692{"rsnContextSendMessage", "(JI[I)V", (void*)nContextSendMessage },
Yang Ni281c3252014-10-24 08:52:24 -07002693{"rsnClosureCreate", "(JJJ[J[J[I[J[J)J", (void*)nClosureCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002694{"rsnInvokeClosureCreate", "(JJ[B[J[J[I)J", (void*)nInvokeClosureCreate },
Yang Ni281c3252014-10-24 08:52:24 -07002695{"rsnClosureSetArg", "(JJIJI)V", (void*)nClosureSetArg },
2696{"rsnClosureSetGlobal", "(JJJJI)V", (void*)nClosureSetGlobal },
Tim Murray460a0492013-11-19 12:45:54 -08002697{"rsnAssignName", "(JJ[B)V", (void*)nAssignName },
2698{"rsnGetName", "(JJ)Ljava/lang/String;", (void*)nGetName },
2699{"rsnObjDestroy", "(JJ)V", (void*)nObjDestroy },
Jason Sams64676f32009-07-08 18:01:53 -07002700
Tim Murray460a0492013-11-19 12:45:54 -08002701{"rsnFileA3DCreateFromFile", "(JLjava/lang/String;)J", (void*)nFileA3DCreateFromFile },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002702{"rsnFileA3DCreateFromAssetStream", "(JJ)J", (void*)nFileA3DCreateFromAssetStream },
Tim Murray460a0492013-11-19 12:45:54 -08002703{"rsnFileA3DCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;)J", (void*)nFileA3DCreateFromAsset },
2704{"rsnFileA3DGetNumIndexEntries", "(JJ)I", (void*)nFileA3DGetNumIndexEntries },
2705{"rsnFileA3DGetIndexEntries", "(JJI[I[Ljava/lang/String;)V", (void*)nFileA3DGetIndexEntries },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002706{"rsnFileA3DGetEntryByIndex", "(JJI)J", (void*)nFileA3DGetEntryByIndex },
Jason Samsd19f10d2009-05-22 14:03:28 -07002707
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002708{"rsnFontCreateFromFile", "(JLjava/lang/String;FI)J", (void*)nFontCreateFromFile },
2709{"rsnFontCreateFromAssetStream", "(JLjava/lang/String;FIJ)J", (void*)nFontCreateFromAssetStream },
2710{"rsnFontCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;FI)J", (void*)nFontCreateFromAsset },
Jason Samsd19f10d2009-05-22 14:03:28 -07002711
Tim Murray460a0492013-11-19 12:45:54 -08002712{"rsnElementCreate", "(JJIZI)J", (void*)nElementCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002713{"rsnElementCreate2", "(J[J[Ljava/lang/String;[I)J", (void*)nElementCreate2 },
Tim Murray460a0492013-11-19 12:45:54 -08002714{"rsnElementGetNativeData", "(JJ[I)V", (void*)nElementGetNativeData },
Ashok Bhat98071552014-02-12 09:54:43 +00002715{"rsnElementGetSubElements", "(JJ[J[Ljava/lang/String;[I)V", (void*)nElementGetSubElements },
Jason Samsd19f10d2009-05-22 14:03:28 -07002716
Tim Murray460a0492013-11-19 12:45:54 -08002717{"rsnTypeCreate", "(JJIIIZZI)J", (void*)nTypeCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002718{"rsnTypeGetNativeData", "(JJ[J)V", (void*)nTypeGetNativeData },
Jason Samsd19f10d2009-05-22 14:03:28 -07002719
Ashok Bhat98071552014-02-12 09:54:43 +00002720{"rsnAllocationCreateTyped", "(JJIIJ)J", (void*)nAllocationCreateTyped },
Tim Murray460a0492013-11-19 12:45:54 -08002721{"rsnAllocationCreateFromBitmap", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateFromBitmap },
2722{"rsnAllocationCreateBitmapBackedAllocation", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateBitmapBackedAllocation },
2723{"rsnAllocationCubeCreateFromBitmap","(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCubeCreateFromBitmap },
Jason Sams5476b452010-12-08 16:14:36 -08002724
Tim Murray460a0492013-11-19 12:45:54 -08002725{"rsnAllocationCopyFromBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyFromBitmap },
2726{"rsnAllocationCopyToBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyToBitmap },
Jason Sams4ef66502010-12-10 16:03:15 -08002727
Tim Murray460a0492013-11-19 12:45:54 -08002728{"rsnAllocationSyncAll", "(JJI)V", (void*)nAllocationSyncAll },
2729{"rsnAllocationGetSurface", "(JJ)Landroid/view/Surface;", (void*)nAllocationGetSurface },
2730{"rsnAllocationSetSurface", "(JJLandroid/view/Surface;)V", (void*)nAllocationSetSurface },
2731{"rsnAllocationIoSend", "(JJ)V", (void*)nAllocationIoSend },
2732{"rsnAllocationIoReceive", "(JJ)V", (void*)nAllocationIoReceive },
Miao Wang87e908d2015-03-02 15:15:15 -08002733{"rsnAllocationData1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData1D },
Miao Wangc8e237e2015-02-20 18:36:32 -08002734{"rsnAllocationElementData", "(JJIIIII[BI)V", (void*)nAllocationElementData },
Miao Wang87e908d2015-03-02 15:15:15 -08002735{"rsnAllocationData2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData2D },
Tim Murray460a0492013-11-19 12:45:54 -08002736{"rsnAllocationData2D", "(JJIIIIIIJIIII)V", (void*)nAllocationData2D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002737{"rsnAllocationData3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData3D },
Tim Murray460a0492013-11-19 12:45:54 -08002738{"rsnAllocationData3D", "(JJIIIIIIIJIIII)V", (void*)nAllocationData3D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002739{"rsnAllocationRead", "(JJLjava/lang/Object;IIZ)V", (void*)nAllocationRead },
2740{"rsnAllocationRead1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead1D },
Miao Wang45cec0a2015-03-04 16:40:21 -08002741{"rsnAllocationElementRead", "(JJIIIII[BI)V", (void*)nAllocationElementRead },
Miao Wang87e908d2015-03-02 15:15:15 -08002742{"rsnAllocationRead2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead2D },
2743{"rsnAllocationRead3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead3D },
Tim Murray460a0492013-11-19 12:45:54 -08002744{"rsnAllocationGetType", "(JJ)J", (void*)nAllocationGetType},
2745{"rsnAllocationResize1D", "(JJI)V", (void*)nAllocationResize1D },
2746{"rsnAllocationGenerateMipmaps", "(JJ)V", (void*)nAllocationGenerateMipmaps },
Jason Samsbd1c3ad2009-08-03 16:03:08 -07002747
Jason Sams46ba27e32015-02-06 17:45:15 -08002748{"rsnAllocationAdapterCreate", "(JJJ)J", (void*)nAllocationAdapterCreate },
2749{"rsnAllocationAdapterOffset", "(JJIIIIIIIII)V", (void*)nAllocationAdapterOffset },
2750
Tim Murray460a0492013-11-19 12:45:54 -08002751{"rsnScriptBindAllocation", "(JJJI)V", (void*)nScriptBindAllocation },
2752{"rsnScriptSetTimeZone", "(JJ[B)V", (void*)nScriptSetTimeZone },
2753{"rsnScriptInvoke", "(JJI)V", (void*)nScriptInvoke },
2754{"rsnScriptInvokeV", "(JJI[B)V", (void*)nScriptInvokeV },
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002755
2756{"rsnScriptForEach", "(JJI[JJ[B[I)V", (void*)nScriptForEach },
Matt Wala36eb1f72015-07-20 15:35:27 -07002757{"rsnScriptReduce", "(JJIJJ[I)V", (void*)nScriptReduce },
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002758
Tim Murray460a0492013-11-19 12:45:54 -08002759{"rsnScriptSetVarI", "(JJII)V", (void*)nScriptSetVarI },
2760{"rsnScriptGetVarI", "(JJI)I", (void*)nScriptGetVarI },
2761{"rsnScriptSetVarJ", "(JJIJ)V", (void*)nScriptSetVarJ },
2762{"rsnScriptGetVarJ", "(JJI)J", (void*)nScriptGetVarJ },
2763{"rsnScriptSetVarF", "(JJIF)V", (void*)nScriptSetVarF },
2764{"rsnScriptGetVarF", "(JJI)F", (void*)nScriptGetVarF },
2765{"rsnScriptSetVarD", "(JJID)V", (void*)nScriptSetVarD },
2766{"rsnScriptGetVarD", "(JJI)D", (void*)nScriptGetVarD },
2767{"rsnScriptSetVarV", "(JJI[B)V", (void*)nScriptSetVarV },
2768{"rsnScriptGetVarV", "(JJI[B)V", (void*)nScriptGetVarV },
2769{"rsnScriptSetVarVE", "(JJI[BJ[I)V", (void*)nScriptSetVarVE },
2770{"rsnScriptSetVarObj", "(JJIJ)V", (void*)nScriptSetVarObj },
Jason Samsd19f10d2009-05-22 14:03:28 -07002771
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002772{"rsnScriptCCreate", "(JLjava/lang/String;Ljava/lang/String;[BI)J", (void*)nScriptCCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002773{"rsnScriptIntrinsicCreate", "(JIJ)J", (void*)nScriptIntrinsicCreate },
2774{"rsnScriptKernelIDCreate", "(JJII)J", (void*)nScriptKernelIDCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002775{"rsnScriptInvokeIDCreate", "(JJI)J", (void*)nScriptInvokeIDCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002776{"rsnScriptFieldIDCreate", "(JJI)J", (void*)nScriptFieldIDCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002777{"rsnScriptGroupCreate", "(J[J[J[J[J[J)J", (void*)nScriptGroupCreate },
Yang Ni35be56c2015-04-02 17:47:56 -07002778{"rsnScriptGroup2Create", "(JLjava/lang/String;Ljava/lang/String;[J)J", (void*)nScriptGroup2Create },
Tim Murray460a0492013-11-19 12:45:54 -08002779{"rsnScriptGroupSetInput", "(JJJJ)V", (void*)nScriptGroupSetInput },
2780{"rsnScriptGroupSetOutput", "(JJJJ)V", (void*)nScriptGroupSetOutput },
2781{"rsnScriptGroupExecute", "(JJ)V", (void*)nScriptGroupExecute },
Yang Ni281c3252014-10-24 08:52:24 -07002782{"rsnScriptGroup2Execute", "(JJ)V", (void*)nScriptGroup2Execute },
Jason Sams0011bcf2009-12-15 12:58:36 -08002783
Tim Murray25207df2015-01-12 16:47:56 -08002784{"rsnScriptIntrinsicBLAS_Single", "(JJIIIIIIIIIFJJFJIIII)V", (void*)nScriptIntrinsicBLAS_Single },
2785{"rsnScriptIntrinsicBLAS_Double", "(JJIIIIIIIIIDJJDJIIII)V", (void*)nScriptIntrinsicBLAS_Double },
2786{"rsnScriptIntrinsicBLAS_Complex", "(JJIIIIIIIIIFFJJFFJIIII)V", (void*)nScriptIntrinsicBLAS_Complex },
2787{"rsnScriptIntrinsicBLAS_Z", "(JJIIIIIIIIIDDJJDDJIIII)V", (void*)nScriptIntrinsicBLAS_Z },
2788
Tim Murray9cb16a22015-04-01 11:07:16 -07002789{"rsnScriptIntrinsicBLAS_BNNM", "(JJIIIJIJIJII)V", (void*)nScriptIntrinsicBLAS_BNNM },
2790
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002791{"rsnProgramStoreCreate", "(JZZZZZZIII)J", (void*)nProgramStoreCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002792
Tim Murray460a0492013-11-19 12:45:54 -08002793{"rsnProgramBindConstants", "(JJIJ)V", (void*)nProgramBindConstants },
2794{"rsnProgramBindTexture", "(JJIJ)V", (void*)nProgramBindTexture },
2795{"rsnProgramBindSampler", "(JJIJ)V", (void*)nProgramBindSampler },
Jason Samsebfb4362009-09-23 13:57:02 -07002796
Ashok Bhat98071552014-02-12 09:54:43 +00002797{"rsnProgramFragmentCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramFragmentCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002798{"rsnProgramRasterCreate", "(JZI)J", (void*)nProgramRasterCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002799{"rsnProgramVertexCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramVertexCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002800
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002801{"rsnContextBindRootScript", "(JJ)V", (void*)nContextBindRootScript },
2802{"rsnContextBindProgramStore", "(JJ)V", (void*)nContextBindProgramStore },
2803{"rsnContextBindProgramFragment", "(JJ)V", (void*)nContextBindProgramFragment },
2804{"rsnContextBindProgramVertex", "(JJ)V", (void*)nContextBindProgramVertex },
2805{"rsnContextBindProgramRaster", "(JJ)V", (void*)nContextBindProgramRaster },
Jason Sams02fb2cb2009-05-28 15:37:57 -07002806
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002807{"rsnSamplerCreate", "(JIIIIIF)J", (void*)nSamplerCreate },
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002808
Ashok Bhat98071552014-02-12 09:54:43 +00002809{"rsnMeshCreate", "(J[J[J[I)J", (void*)nMeshCreate },
Jason Sams2e1872f2010-08-17 16:25:41 -07002810
Tim Murray460a0492013-11-19 12:45:54 -08002811{"rsnMeshGetVertexBufferCount", "(JJ)I", (void*)nMeshGetVertexBufferCount },
2812{"rsnMeshGetIndexCount", "(JJ)I", (void*)nMeshGetIndexCount },
Ashok Bhat98071552014-02-12 09:54:43 +00002813{"rsnMeshGetVertices", "(JJ[JI)V", (void*)nMeshGetVertices },
2814{"rsnMeshGetIndices", "(JJ[J[II)V", (void*)nMeshGetIndices },
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002815
Tim Murray56f9e6f2014-05-16 11:47:26 -07002816{"rsnSystemGetPointerSize", "()I", (void*)nSystemGetPointerSize },
Jason Samsd19f10d2009-05-22 14:03:28 -07002817};
2818
2819static int registerFuncs(JNIEnv *_env)
2820{
2821 return android::AndroidRuntime::registerNativeMethods(
2822 _env, classPathName, methods, NELEM(methods));
2823}
2824
2825// ---------------------------------------------------------------------------
2826
2827jint JNI_OnLoad(JavaVM* vm, void* reserved)
2828{
Chris Wailes488230c32014-08-14 11:22:40 -07002829 JNIEnv* env = nullptr;
Jason Samsd19f10d2009-05-22 14:03:28 -07002830 jint result = -1;
2831
Jason Samsd19f10d2009-05-22 14:03:28 -07002832 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
Steve Block3762c312012-01-06 19:20:56 +00002833 ALOGE("ERROR: GetEnv failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07002834 goto bail;
2835 }
Chris Wailes488230c32014-08-14 11:22:40 -07002836 assert(env != nullptr);
Jason Samsd19f10d2009-05-22 14:03:28 -07002837
2838 if (registerFuncs(env) < 0) {
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002839 ALOGE("ERROR: Renderscript native registration failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07002840 goto bail;
2841 }
2842
2843 /* success -- return valid version number */
2844 result = JNI_VERSION_1_4;
2845
2846bail:
2847 return result;
2848}